blob: 4c63eeba14566d4568adef09ec82c8391a1458f4 [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 {
474#ifdef OS2
475 int i;
476
477 for (i = 0; i < len; i++)
478 if (buf[i] == 0)
479 buf[i] = K_NUL;
480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 return len;
482 }
483 }
484}
485
486 static void
487handle_resize()
488{
489 do_resize = FALSE;
490 shell_resized();
491}
492
493/*
494 * return non-zero if a character is available
495 */
496 int
497mch_char_avail()
498{
499 return WaitForChar(0L);
500}
501
502#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
503# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000504# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505# endif
506# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
507# include <sys/sysctl.h>
508# endif
509# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
510# include <sys/sysinfo.h>
511# endif
512
513/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000514 * Return total amount of memory available in Kbyte.
515 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 long_u
518mch_total_mem(special)
Bram Moolenaar78a15312009-05-15 19:33:18 +0000519 int special UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
521# ifdef __EMX__
Bram Moolenaar914572a2007-05-01 11:37:47 +0000522 return ulimit(3, 0L) >> 10; /* always 32MB? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523# else
524 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000525 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526
527# ifdef HAVE_SYSCTL
528 int mib[2], physmem;
529 size_t len;
530
531 /* BSD way of getting the amount of RAM available. */
532 mib[0] = CTL_HW;
533 mib[1] = HW_USERMEM;
534 len = sizeof(physmem);
535 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
536 mem = (long_u)physmem;
537# endif
538
539# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
540 if (mem == 0)
541 {
542 struct sysinfo sinfo;
543
544 /* Linux way of getting amount of RAM available */
545 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000546 {
547# ifdef HAVE_SYSINFO_MEM_UNIT
548 /* avoid overflow as much as possible */
549 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
550 {
551 sinfo.mem_unit = sinfo.mem_unit >> 1;
552 --shiftright;
553 }
554 mem = sinfo.totalram * sinfo.mem_unit;
555# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 mem = sinfo.totalram;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000557# endif
558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 }
560# endif
561
562# ifdef HAVE_SYSCONF
563 if (mem == 0)
564 {
565 long pagesize, pagecount;
566
567 /* Solaris way of getting amount of RAM available */
568 pagesize = sysconf(_SC_PAGESIZE);
569 pagecount = sysconf(_SC_PHYS_PAGES);
570 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000571 {
572 /* avoid overflow as much as possible */
573 while (shiftright > 0 && (pagesize & 1) == 0)
574 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000575 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000576 --shiftright;
577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
581# endif
582
583 /* Return the minimum of the physical memory and the user limit, because
584 * using more than the user limit may cause Vim to be terminated. */
585# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
586 {
587 struct rlimit rlp;
588
589 if (getrlimit(RLIMIT_DATA, &rlp) == 0
590 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
591# ifdef RLIM_INFINITY
592 && rlp.rlim_cur != RLIM_INFINITY
593# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000594 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000596 {
597 mem = (long_u)rlp.rlim_cur;
598 shiftright = 10;
599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
601# endif
602
603 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000604 return mem >> shiftright;
605 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606# endif
607}
608#endif
609
610 void
611mch_delay(msec, ignoreinput)
612 long msec;
613 int ignoreinput;
614{
615 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000616#ifdef FEAT_MZSCHEME
617 long total = msec; /* remember original value */
618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
620 if (ignoreinput)
621 {
622 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000623 * here. But we don't want QUIT to kill us (CTRL-\ used in a
624 * shell may produce SIGQUIT). */
625 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 old_tmode = curr_tmode;
627 if (curr_tmode == TMODE_RAW)
628 settmode(TMODE_SLEEP);
629
630 /*
631 * Everybody sleeps in a different way...
632 * Prefer nanosleep(), some versions of usleep() can only sleep up to
633 * one second.
634 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000635#ifdef FEAT_MZSCHEME
636 do
637 {
638 /* if total is large enough, wait by portions in p_mzq */
639 if (total > p_mzq)
640 msec = p_mzq;
641 else
642 msec = total;
643 total -= msec;
644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef HAVE_NANOSLEEP
646 {
647 struct timespec ts;
648
649 ts.tv_sec = msec / 1000;
650 ts.tv_nsec = (msec % 1000) * 1000000;
651 (void)nanosleep(&ts, NULL);
652 }
653#else
654# ifdef HAVE_USLEEP
655 while (msec >= 1000)
656 {
657 usleep((unsigned int)(999 * 1000));
658 msec -= 999;
659 }
660 usleep((unsigned int)(msec * 1000));
661# else
662# ifndef HAVE_SELECT
663 poll(NULL, 0, (int)msec);
664# else
665# ifdef __EMX__
666 _sleep2(msec);
667# else
668 {
669 struct timeval tv;
670
671 tv.tv_sec = msec / 1000;
672 tv.tv_usec = (msec % 1000) * 1000;
673 /*
674 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
675 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
676 */
677 select(0, NULL, NULL, NULL, &tv);
678 }
679# endif /* __EMX__ */
680# endif /* HAVE_SELECT */
681# endif /* HAVE_NANOSLEEP */
682#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000683#ifdef FEAT_MZSCHEME
684 }
685 while (total > 0);
686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
688 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000689 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 }
691 else
692 WaitForChar(msec);
693}
694
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000695#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
697# define HAVE_CHECK_STACK_GROWTH
698/*
699 * Support for checking for an almost-out-of-stack-space situation.
700 */
701
702/*
703 * Return a pointer to an item on the stack. Used to find out if the stack
704 * grows up or down.
705 */
706static void check_stack_growth __ARGS((char *p));
707static int stack_grows_downwards;
708
709/*
710 * Find out if the stack grows upwards or downwards.
711 * "p" points to a variable on the stack of the caller.
712 */
713 static void
714check_stack_growth(p)
715 char *p;
716{
717 int i;
718
719 stack_grows_downwards = (p > (char *)&i);
720}
721#endif
722
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000723#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724static char *stack_limit = NULL;
725
726#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
727# include <pthread.h>
728# include <pthread_np.h>
729#endif
730
731/*
732 * Find out until how var the stack can grow without getting into trouble.
733 * Called when starting up and when switching to the signal stack in
734 * deathtrap().
735 */
736 static void
737get_stack_limit()
738{
739 struct rlimit rlp;
740 int i;
741 long lim;
742
743 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
744 * limit doesn't fit in a long (rlim_cur might be "long long"). */
745 if (getrlimit(RLIMIT_STACK, &rlp) == 0
746 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
747# ifdef RLIM_INFINITY
748 && rlp.rlim_cur != RLIM_INFINITY
749# endif
750 )
751 {
752 lim = (long)rlp.rlim_cur;
753#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
754 {
755 pthread_attr_t attr;
756 size_t size;
757
758 /* On FreeBSD the initial thread always has a fixed stack size, no
759 * matter what the limits are set to. Normally it's 1 Mbyte. */
760 pthread_attr_init(&attr);
761 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
762 {
763 pthread_attr_getstacksize(&attr, &size);
764 if (lim > (long)size)
765 lim = (long)size;
766 }
767 pthread_attr_destroy(&attr);
768 }
769#endif
770 if (stack_grows_downwards)
771 {
772 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
773 if (stack_limit >= (char *)&i)
774 /* overflow, set to 1/16 of current stack position */
775 stack_limit = (char *)((long)&i / 16L);
776 }
777 else
778 {
779 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
780 if (stack_limit <= (char *)&i)
781 stack_limit = NULL; /* overflow */
782 }
783 }
784}
785
786/*
787 * Return FAIL when running out of stack space.
788 * "p" must point to any variable local to the caller that's on the stack.
789 */
790 int
791mch_stackcheck(p)
792 char *p;
793{
794 if (stack_limit != NULL)
795 {
796 if (stack_grows_downwards)
797 {
798 if (p < stack_limit)
799 return FAIL;
800 }
801 else if (p > stack_limit)
802 return FAIL;
803 }
804 return OK;
805}
806#endif
807
808#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
809/*
810 * Support for using the signal stack.
811 * This helps when we run out of stack space, which causes a SIGSEGV. The
812 * signal handler then must run on another stack, since the normal stack is
813 * completely full.
814 */
815
Bram Moolenaar39766a72013-11-03 00:41:00 +0100816#if defined(HAVE_AVAILABILITYMACROS_H)
Bram Moolenaar4cc95d12013-11-02 21:49:32 +0100817# include <AvailabilityMacros.h>
818#endif
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820#ifndef SIGSTKSZ
821# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
822#endif
823
824# ifdef HAVE_SIGALTSTACK
825static stack_t sigstk; /* for sigaltstack() */
826# else
827static struct sigstack sigstk; /* for sigstack() */
828# endif
829
830static void init_signal_stack __ARGS((void));
831static char *signal_stack;
832
833 static void
834init_signal_stack()
835{
836 if (signal_stack != NULL)
837 {
838# ifdef HAVE_SIGALTSTACK
Bram Moolenaar1a3d0862007-08-30 09:47:38 +0000839# if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \
840 || MAC_OS_X_VERSION_MAX_ALLOWED <= 1040)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 /* missing prototype. Adding it to osdef?.h.in doesn't work, because
842 * "struct sigaltstack" needs to be declared. */
843 extern int sigaltstack __ARGS((const struct sigaltstack *ss, struct sigaltstack *oss));
844# endif
845
846# ifdef HAVE_SS_BASE
847 sigstk.ss_base = signal_stack;
848# else
849 sigstk.ss_sp = signal_stack;
850# endif
851 sigstk.ss_size = SIGSTKSZ;
852 sigstk.ss_flags = 0;
853 (void)sigaltstack(&sigstk, NULL);
854# else
855 sigstk.ss_sp = signal_stack;
856 if (stack_grows_downwards)
857 sigstk.ss_sp += SIGSTKSZ - 1;
858 sigstk.ss_onstack = 0;
859 (void)sigstack(&sigstk, NULL);
860# endif
861 }
862}
863#endif
864
865/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000866 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 * will barf when the second argument to signal() is ``wrong''.
868 * Let me try it with a few tricky defines from my own osdef.h (jw).
869 */
870#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 static RETSIGTYPE
872sig_winch SIGDEFARG(sigarg)
873{
874 /* this is not required on all systems, but it doesn't hurt anybody */
875 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
876 do_resize = TRUE;
877 SIGRETURN;
878}
879#endif
880
881#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 static RETSIGTYPE
883catch_sigint SIGDEFARG(sigarg)
884{
885 /* this is not required on all systems, but it doesn't hurt anybody */
886 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
887 got_int = TRUE;
888 SIGRETURN;
889}
890#endif
891
892#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 static RETSIGTYPE
894catch_sigpwr SIGDEFARG(sigarg)
895{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000896 /* this is not required on all systems, but it doesn't hurt anybody */
897 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 /*
899 * I'm not sure we get the SIGPWR signal when the system is really going
900 * down or when the batteries are almost empty. Just preserve the swap
901 * files and don't exit, that can't do any harm.
902 */
903 ml_sync_all(FALSE, FALSE);
904 SIGRETURN;
905}
906#endif
907
908#ifdef SET_SIG_ALARM
909/*
910 * signal function for alarm().
911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 static RETSIGTYPE
913sig_alarm SIGDEFARG(sigarg)
914{
915 /* doesn't do anything, just to break a system call */
916 sig_alarm_called = TRUE;
917 SIGRETURN;
918}
919#endif
920
Bram Moolenaar44ecf652005-03-07 23:09:59 +0000921#if (defined(HAVE_SETJMP_H) \
922 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
923 || defined(FEAT_LIBCALL))) \
924 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925/*
926 * A simplistic version of setjmp() that only allows one level of using.
927 * Don't call twice before calling mch_endjmp()!.
928 * Usage:
929 * mch_startjmp();
930 * if (SETJMP(lc_jump_env) != 0)
931 * {
932 * mch_didjmp();
933 * EMSG("crash!");
934 * }
935 * else
936 * {
937 * do_the_work;
938 * mch_endjmp();
939 * }
940 * Note: Can't move SETJMP() here, because a function calling setjmp() must
941 * not return before the saved environment is used.
942 * Returns OK for normal return, FAIL when the protected code caused a
943 * problem and LONGJMP() was used.
944 */
945 void
946mch_startjmp()
947{
948#ifdef SIGHASARG
949 lc_signal = 0;
950#endif
951 lc_active = TRUE;
952}
953
954 void
955mch_endjmp()
956{
957 lc_active = FALSE;
958}
959
960 void
961mch_didjmp()
962{
963# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
964 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
965 * otherwise catching the signal only works once. */
966 init_signal_stack();
967# endif
968}
969#endif
970
971/*
972 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200973 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200975 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
976 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 */
978 static RETSIGTYPE
979deathtrap SIGDEFARG(sigarg)
980{
981 static int entered = 0; /* count the number of times we got here.
982 Note: when memory has been corrupted
983 this may get an arbitrary value! */
984#ifdef SIGHASARG
985 int i;
986#endif
987
988#if defined(HAVE_SETJMP_H)
989 /*
990 * Catch a crash in protected code.
991 * Restores the environment saved in lc_jump_env, which looks like
992 * SETJMP() returns 1.
993 */
994 if (lc_active)
995 {
996# if defined(SIGHASARG)
997 lc_signal = sigarg;
998# endif
999 lc_active = FALSE; /* don't jump again */
1000 LONGJMP(lc_jump_env, 1);
1001 /* NOTREACHED */
1002 }
1003#endif
1004
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001005#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001006# ifdef SIGQUIT
1007 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1008 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1009 * pressing CTRL-\, but we don't want Vim to exit then. */
1010 if (in_mch_delay && sigarg == SIGQUIT)
1011 SIGRETURN;
1012# endif
1013
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001014 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1015 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1016 * free(). Calling free() again may then cause a crash. */
1017 if (entered == 0
1018 && (0
1019# ifdef SIGHUP
1020 || sigarg == SIGHUP
1021# endif
1022# ifdef SIGQUIT
1023 || sigarg == SIGQUIT
1024# endif
1025# ifdef SIGTERM
1026 || sigarg == SIGTERM
1027# endif
1028# ifdef SIGPWR
1029 || sigarg == SIGPWR
1030# endif
1031# ifdef SIGUSR1
1032 || sigarg == SIGUSR1
1033# endif
1034# ifdef SIGUSR2
1035 || sigarg == SIGUSR2
1036# endif
1037 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001038 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001039 SIGRETURN;
1040#endif
1041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 /* Remember how often we have been called. */
1043 ++entered;
1044
1045#ifdef FEAT_EVAL
1046 /* Set the v:dying variable. */
1047 set_vim_var_nr(VV_DYING, (long)entered);
1048#endif
1049
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001050#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 /* Since we are now using the signal stack, need to reset the stack
1052 * limit. Otherwise using a regexp will fail. */
1053 get_stack_limit();
1054#endif
1055
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001056#if 0
1057 /* This is for opening gdb the moment Vim crashes.
1058 * You need to manually adjust the file name and Vim executable name.
1059 * Suggested by SungHyun Nam. */
1060 {
1061# define VI_GDB_FILE "/tmp/vimgdb"
1062# define VIM_NAME "/usr/bin/vim"
1063 FILE *fp = fopen(VI_GDB_FILE, "w");
1064 if (fp)
1065 {
1066 fprintf(fp,
1067 "file %s\n"
1068 "attach %d\n"
1069 "set height 1000\n"
1070 "bt full\n"
1071 , VIM_NAME, getpid());
1072 fclose(fp);
1073 system("xterm -e gdb -x "VI_GDB_FILE);
1074 unlink(VI_GDB_FILE);
1075 }
1076 }
1077#endif
1078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#ifdef SIGHASARG
1080 /* try to find the name of this signal */
1081 for (i = 0; signal_info[i].sig != -1; i++)
1082 if (sigarg == signal_info[i].sig)
1083 break;
1084 deadly_signal = sigarg;
1085#endif
1086
1087 full_screen = FALSE; /* don't write message to the GUI, it might be
1088 * part of the problem... */
1089 /*
1090 * If something goes wrong after entering here, we may get here again.
1091 * When this happens, give a message and try to exit nicely (resetting the
1092 * terminal mode, etc.)
1093 * When this happens twice, just exit, don't even try to give a message,
1094 * stack may be corrupt or something weird.
1095 * When this still happens again (or memory was corrupted in such a way
1096 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1097 */
1098 if (entered >= 3)
1099 {
1100 reset_signals(); /* don't catch any signals anymore */
1101 may_core_dump();
1102 if (entered >= 4)
1103 _exit(8);
1104 exit(7);
1105 }
1106 if (entered == 2)
1107 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001108 /* No translation, it may call malloc(). */
1109 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 out_flush();
1111 getout(1);
1112 }
1113
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001114 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001116 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 signal_info[i].name);
1118#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001119 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001121
1122 /* Preserve files and exit. This sets the really_exiting flag to prevent
1123 * calling free(). */
1124 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
Bram Moolenaar009b2592004-10-24 19:18:58 +00001126#ifdef NBDEBUG
1127 reset_signals();
1128 may_core_dump();
1129 abort();
1130#endif
1131
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 SIGRETURN;
1133}
1134
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001135#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136/*
1137 * On Solaris with multi-threading, suspending might not work immediately.
1138 * Catch the SIGCONT signal, which will be used as an indication whether the
1139 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001140 *
1141 * On Linux, signal is not always handled immediately either.
1142 * See https://bugs.launchpad.net/bugs/291373
1143 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001144 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001146static volatile int sigcont_received;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147static RETSIGTYPE sigcont_handler __ARGS(SIGPROTOARG);
1148
1149/*
1150 * signal handler for SIGCONT
1151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 static RETSIGTYPE
1153sigcont_handler SIGDEFARG(sigarg)
1154{
1155 sigcont_received = TRUE;
1156 SIGRETURN;
1157}
1158#endif
1159
Bram Moolenaar62b42182010-09-21 22:09:37 +02001160# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
1161static void loose_clipboard __ARGS((void));
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001162# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001163static void save_clipboard __ARGS((void));
1164static void restore_clipboard __ARGS((void));
1165
1166static void *clip_star_save = NULL;
1167static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001168# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001169
1170/*
1171 * Called when Vim is going to sleep or execute a shell command.
1172 * We can't respond to requests for the X selections. Lose them, otherwise
1173 * other applications will hang. But first copy the text to cut buffer 0.
1174 */
1175 static void
1176loose_clipboard()
1177{
1178 if (clip_star.owned || clip_plus.owned)
1179 {
1180 x11_export_final_selection();
1181 if (clip_star.owned)
1182 clip_lose_selection(&clip_star);
1183 if (clip_plus.owned)
1184 clip_lose_selection(&clip_plus);
1185 if (x11_display != NULL)
1186 XFlush(x11_display);
1187 }
1188}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001189
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001190# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001191/*
1192 * Save clipboard text to restore later.
1193 */
1194 static void
1195save_clipboard()
1196{
1197 if (clip_star.owned)
1198 clip_star_save = get_register('*', TRUE);
1199 if (clip_plus.owned)
1200 clip_plus_save = get_register('+', TRUE);
1201}
1202
1203/*
1204 * Restore clipboard text if no one own the X selection.
1205 */
1206 static void
1207restore_clipboard()
1208{
1209 if (clip_star_save != NULL)
1210 {
1211 if (!clip_gen_owner_exists(&clip_star))
1212 put_register('*', clip_star_save);
1213 else
1214 free_register(clip_star_save);
1215 clip_star_save = NULL;
1216 }
1217 if (clip_plus_save != NULL)
1218 {
1219 if (!clip_gen_owner_exists(&clip_plus))
1220 put_register('+', clip_plus_save);
1221 else
1222 free_register(clip_plus_save);
1223 clip_plus_save = NULL;
1224 }
1225}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001226# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001227#endif
1228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229/*
1230 * If the machine has job control, use it to suspend the program,
1231 * otherwise fake it by starting a new shell.
1232 */
1233 void
1234mch_suspend()
1235{
1236 /* BeOS does have SIGTSTP, but it doesn't work. */
1237#if defined(SIGTSTP) && !defined(__BEOS__)
1238 out_flush(); /* needed to make cursor visible on some systems */
1239 settmode(TMODE_COOK);
1240 out_flush(); /* needed to disable mouse on some systems */
1241
1242# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001243 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244# endif
1245
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001246# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 sigcont_received = FALSE;
1248# endif
1249 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001250# if defined(_REENTRANT) && defined(SIGCONT)
1251 /*
1252 * Wait for the SIGCONT signal to be handled. It generally happens
1253 * immediately, but somehow not all the time. Do not call pause()
1254 * because there would be race condition which would hang Vim if
1255 * signal happened in between the test of sigcont_received and the
1256 * call to pause(). If signal is not yet received, call sleep(0)
1257 * to just yield CPU. Signal should then be received. If somehow
1258 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1259 * further if signal is not received after 1+2+3+4 ms (not expected
1260 * to happen).
1261 */
1262 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001263 long wait_time;
1264 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001265 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001266 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268# endif
1269
1270# ifdef FEAT_TITLE
1271 /*
1272 * Set oldtitle to NULL, so the current title is obtained again.
1273 */
1274 vim_free(oldtitle);
1275 oldtitle = NULL;
1276# endif
1277 settmode(TMODE_RAW);
1278 need_check_timestamps = TRUE;
1279 did_check_timestamps = FALSE;
1280#else
1281 suspend_shell();
1282#endif
1283}
1284
1285 void
1286mch_init()
1287{
1288 Columns = 80;
1289 Rows = 24;
1290
1291 out_flush();
1292 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001293
Bram Moolenaar56718732006-03-15 22:53:57 +00001294#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001295 mac_conv_init();
1296#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001297#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1298 win_clip_init();
1299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300}
1301
1302 static void
1303set_signals()
1304{
1305#if defined(SIGWINCH)
1306 /*
1307 * WINDOW CHANGE signal is handled with sig_winch().
1308 */
1309 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1310#endif
1311
1312 /*
1313 * We want the STOP signal to work, to make mch_suspend() work.
1314 * For "rvim" the STOP signal is ignored.
1315 */
1316#ifdef SIGTSTP
1317 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1318#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001319#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 signal(SIGCONT, sigcont_handler);
1321#endif
1322
1323 /*
1324 * We want to ignore breaking of PIPEs.
1325 */
1326#ifdef SIGPIPE
1327 signal(SIGPIPE, SIG_IGN);
1328#endif
1329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001331 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332#endif
1333
1334 /*
1335 * Ignore alarm signals (Perl's alarm() generates it).
1336 */
1337#ifdef SIGALRM
1338 signal(SIGALRM, SIG_IGN);
1339#endif
1340
1341 /*
1342 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1343 * work will be lost.
1344 */
1345#ifdef SIGPWR
1346 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1347#endif
1348
1349 /*
1350 * Arrange for other signals to gracefully shutdown Vim.
1351 */
1352 catch_signals(deathtrap, SIG_ERR);
1353
1354#if defined(FEAT_GUI) && defined(SIGHUP)
1355 /*
1356 * When the GUI is running, ignore the hangup signal.
1357 */
1358 if (gui.in_use)
1359 signal(SIGHUP, SIG_IGN);
1360#endif
1361}
1362
Bram Moolenaardf177f62005-02-22 08:39:57 +00001363#if defined(SIGINT) || defined(PROTO)
1364/*
1365 * Catch CTRL-C (only works while in Cooked mode).
1366 */
1367 static void
1368catch_int_signal()
1369{
1370 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1371}
1372#endif
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 void
1375reset_signals()
1376{
1377 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001378#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /* SIGCONT isn't in the list, because its default action is ignore */
1380 signal(SIGCONT, SIG_DFL);
1381#endif
1382}
1383
1384 static void
1385catch_signals(func_deadly, func_other)
1386 RETSIGTYPE (*func_deadly)();
1387 RETSIGTYPE (*func_other)();
1388{
1389 int i;
1390
1391 for (i = 0; signal_info[i].sig != -1; i++)
1392 if (signal_info[i].deadly)
1393 {
1394#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1395 struct sigaction sa;
1396
1397 /* Setup to use the alternate stack for the signal function. */
1398 sa.sa_handler = func_deadly;
1399 sigemptyset(&sa.sa_mask);
1400# if defined(__linux__) && defined(_REENTRANT)
1401 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1402 * thread handling in combination with using the alternate stack:
1403 * pthread library functions try to use the stack pointer to
1404 * identify the current thread, causing a SEGV signal, which
1405 * recursively calls deathtrap() and hangs. */
1406 sa.sa_flags = 0;
1407# else
1408 sa.sa_flags = SA_ONSTACK;
1409# endif
1410 sigaction(signal_info[i].sig, &sa, NULL);
1411#else
1412# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1413 struct sigvec sv;
1414
1415 /* Setup to use the alternate stack for the signal function. */
1416 sv.sv_handler = func_deadly;
1417 sv.sv_mask = 0;
1418 sv.sv_flags = SV_ONSTACK;
1419 sigvec(signal_info[i].sig, &sv, NULL);
1420# else
1421 signal(signal_info[i].sig, func_deadly);
1422# endif
1423#endif
1424 }
1425 else if (func_other != SIG_ERR)
1426 signal(signal_info[i].sig, func_other);
1427}
1428
1429/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001430 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001431 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1432 * return TRUE
1433 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1434 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001435 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001436 * Returns TRUE when Vim should exit.
1437 */
1438 int
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001439vim_handle_signal(sig)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001440 int sig;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001441{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001442 static int got_signal = 0;
1443 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001444
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001445 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001446 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001447 case SIGNAL_BLOCK: blocked = TRUE;
1448 break;
1449
1450 case SIGNAL_UNBLOCK: blocked = FALSE;
1451 if (got_signal != 0)
1452 {
1453 kill(getpid(), got_signal);
1454 got_signal = 0;
1455 }
1456 break;
1457
1458 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001459 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001460 got_signal = sig;
1461#ifdef SIGPWR
1462 if (sig != SIGPWR)
1463#endif
1464 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001465 break;
1466 }
1467 return FALSE;
1468}
1469
1470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 * Check_win checks whether we have an interactive stdout.
1472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 int
1474mch_check_win(argc, argv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001475 int argc UNUSED;
1476 char **argv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
1478#ifdef OS2
1479 /*
1480 * Store argv[0], may be used for $VIM. Only use it if it is an absolute
1481 * name, mostly it's just "vim" and found in the path, which is unusable.
1482 */
1483 if (mch_isFullName(argv[0]))
1484 exe_name = vim_strsave((char_u *)argv[0]);
1485#endif
1486 if (isatty(1))
1487 return OK;
1488 return FAIL;
1489}
1490
1491/*
1492 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1493 */
1494 int
1495mch_input_isatty()
1496{
1497 if (isatty(read_cmd_fd))
1498 return TRUE;
1499 return FALSE;
1500}
1501
1502#ifdef FEAT_X11
1503
1504# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) \
1505 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1506
1507static void xopen_message __ARGS((struct timeval *tvp));
1508
1509/*
1510 * Give a message about the elapsed time for opening the X window.
1511 */
1512 static void
1513xopen_message(tvp)
1514 struct timeval *tvp; /* must contain start time */
1515{
1516 struct timeval end_tv;
1517
1518 /* Compute elapsed time. */
1519 gettimeofday(&end_tv, NULL);
1520 smsg((char_u *)_("Opening the X display took %ld msec"),
1521 (end_tv.tv_sec - tvp->tv_sec) * 1000L
Bram Moolenaar051b7822005-05-19 21:00:46 +00001522 + (end_tv.tv_usec - tvp->tv_usec) / 1000L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523}
1524# endif
1525#endif
1526
1527#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1528/*
1529 * A few functions shared by X11 title and clipboard code.
1530 */
1531static int x_error_handler __ARGS((Display *dpy, XErrorEvent *error_event));
1532static int x_error_check __ARGS((Display *dpy, XErrorEvent *error_event));
1533static int x_connect_to_server __ARGS((void));
1534static int test_x11_window __ARGS((Display *dpy));
1535
1536static int got_x_error = FALSE;
1537
1538/*
1539 * X Error handler, otherwise X just exits! (very rude) -- webb
1540 */
1541 static int
1542x_error_handler(dpy, error_event)
1543 Display *dpy;
1544 XErrorEvent *error_event;
1545{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001546 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 STRCAT(IObuff, _("\nVim: Got X error\n"));
1548
1549 /* We cannot print a message and continue, because no X calls are allowed
1550 * here (causes my system to hang). Silently continuing might be an
1551 * alternative... */
1552 preserve_exit(); /* preserve files and exit */
1553
1554 return 0; /* NOTREACHED */
1555}
1556
1557/*
1558 * Another X Error handler, just used to check for errors.
1559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 static int
1561x_error_check(dpy, error_event)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001562 Display *dpy UNUSED;
1563 XErrorEvent *error_event UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 got_x_error = TRUE;
1566 return 0;
1567}
1568
1569#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1570# if defined(HAVE_SETJMP_H)
1571/*
1572 * An X IO Error handler, used to catch error while opening the display.
1573 */
1574static int x_IOerror_check __ARGS((Display *dpy));
1575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 static int
1577x_IOerror_check(dpy)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001578 Display *dpy UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 /* This function should not return, it causes exit(). Longjump instead. */
1581 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001582# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001583 return 0; /* avoid the compiler complains about missing return value */
1584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585}
1586# endif
1587
1588/*
1589 * An X IO Error handler, used to catch terminal errors.
1590 */
1591static int x_IOerror_handler __ARGS((Display *dpy));
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001592static void may_restore_clipboard __ARGS((void));
1593static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 static int
1596x_IOerror_handler(dpy)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001597 Display *dpy UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
1599 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001600 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 x11_window = 0;
1602 x11_display = NULL;
1603 xterm_Shell = (Widget)0;
1604
1605 /* This function should not return, it causes exit(). Longjump instead. */
1606 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001607# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001608 return 0; /* avoid the compiler complains about missing return value */
1609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001611
1612/*
1613 * If the X11 connection was lost try to restore it.
1614 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001615 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001616 */
1617 static void
1618may_restore_clipboard()
1619{
1620 if (xterm_dpy_was_reset)
1621 {
1622 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001623
1624# ifndef LESSTIF_VERSION
1625 /* This has been reported to avoid Vim getting stuck. */
1626 if (app_context != (XtAppContext)NULL)
1627 {
1628 XtDestroyApplicationContext(app_context);
1629 app_context = (XtAppContext)NULL;
1630 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1631 }
1632# endif
1633
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001634 setup_term_clip();
1635 get_x11_title(FALSE);
1636 }
1637}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638#endif
1639
1640/*
1641 * Return TRUE when connection to the X server is desired.
1642 */
1643 static int
1644x_connect_to_server()
1645{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646#if defined(FEAT_CLIENTSERVER)
1647 if (x_force_connect)
1648 return TRUE;
1649#endif
1650 if (x_no_connect)
1651 return FALSE;
1652
1653 /* Check for a match with "exclude:" from 'clipboard'. */
1654 if (clip_exclude_prog != NULL)
1655 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001656 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 return FALSE;
1658 }
1659 return TRUE;
1660}
1661
1662/*
1663 * Test if "dpy" and x11_window are valid by getting the window title.
1664 * I don't actually want it yet, so there may be a simpler call to use, but
1665 * this will cause the error handler x_error_check() to be called if anything
1666 * is wrong, such as the window pointer being invalid (as can happen when the
1667 * user changes his DISPLAY, but not his WINDOWID) -- webb
1668 */
1669 static int
1670test_x11_window(dpy)
1671 Display *dpy;
1672{
1673 int (*old_handler)();
1674 XTextProperty text_prop;
1675
1676 old_handler = XSetErrorHandler(x_error_check);
1677 got_x_error = FALSE;
1678 if (XGetWMName(dpy, x11_window, &text_prop))
1679 XFree((void *)text_prop.value);
1680 XSync(dpy, False);
1681 (void)XSetErrorHandler(old_handler);
1682
1683 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001684 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
1686 return (got_x_error ? FAIL : OK);
1687}
1688#endif
1689
1690#ifdef FEAT_TITLE
1691
1692#ifdef FEAT_X11
1693
1694static int get_x11_thing __ARGS((int get_title, int test_only));
1695
1696/*
1697 * try to get x11 window and display
1698 *
1699 * return FAIL for failure, OK otherwise
1700 */
1701 static int
1702get_x11_windis()
1703{
1704 char *winid;
1705 static int result = -1;
1706#define XD_NONE 0 /* x11_display not set here */
1707#define XD_HERE 1 /* x11_display opened here */
1708#define XD_GUI 2 /* x11_display used from gui.dpy */
1709#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1710 static int x11_display_from = XD_NONE;
1711 static int did_set_error_handler = FALSE;
1712
1713 if (!did_set_error_handler)
1714 {
1715 /* X just exits if it finds an error otherwise! */
1716 (void)XSetErrorHandler(x_error_handler);
1717 did_set_error_handler = TRUE;
1718 }
1719
Bram Moolenaar9372a112005-12-06 19:59:18 +00001720#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (gui.in_use)
1722 {
1723 /*
1724 * If the X11 display was opened here before, for the window where Vim
1725 * was started, close that one now to avoid a memory leak.
1726 */
1727 if (x11_display_from == XD_HERE && x11_display != NULL)
1728 {
1729 XCloseDisplay(x11_display);
1730 x11_display_from = XD_NONE;
1731 }
1732 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1733 {
1734 x11_display_from = XD_GUI;
1735 return OK;
1736 }
1737 x11_display = NULL;
1738 return FAIL;
1739 }
1740 else if (x11_display_from == XD_GUI)
1741 {
1742 /* GUI must have stopped somehow, clear x11_display */
1743 x11_window = 0;
1744 x11_display = NULL;
1745 x11_display_from = XD_NONE;
1746 }
1747#endif
1748
1749 /* When started with the "-X" argument, don't try connecting. */
1750 if (!x_connect_to_server())
1751 return FAIL;
1752
1753 /*
1754 * If WINDOWID not set, should try another method to find out
1755 * what the current window number is. The only code I know for
1756 * this is very complicated.
1757 * We assume that zero is invalid for WINDOWID.
1758 */
1759 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1760 x11_window = (Window)atol(winid);
1761
1762#ifdef FEAT_XCLIPBOARD
1763 if (xterm_dpy != NULL && x11_window != 0)
1764 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001765 /* We may have checked it already, but Gnome terminal can move us to
1766 * another window, so we need to check every time. */
1767 if (x11_display_from != XD_XTERM)
1768 {
1769 /*
1770 * If the X11 display was opened here before, for the window where
1771 * Vim was started, close that one now to avoid a memory leak.
1772 */
1773 if (x11_display_from == XD_HERE && x11_display != NULL)
1774 XCloseDisplay(x11_display);
1775 x11_display = xterm_dpy;
1776 x11_display_from = XD_XTERM;
1777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (test_x11_window(x11_display) == FAIL)
1779 {
1780 /* probably bad $WINDOWID */
1781 x11_window = 0;
1782 x11_display = NULL;
1783 x11_display_from = XD_NONE;
1784 return FAIL;
1785 }
1786 return OK;
1787 }
1788#endif
1789
1790 if (x11_window == 0 || x11_display == NULL)
1791 result = -1;
1792
1793 if (result != -1) /* Have already been here and set this */
1794 return result; /* Don't do all these X calls again */
1795
1796 if (x11_window != 0 && x11_display == NULL)
1797 {
1798#ifdef SET_SIG_ALARM
1799 RETSIGTYPE (*sig_save)();
1800#endif
1801#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1802 struct timeval start_tv;
1803
1804 if (p_verbose > 0)
1805 gettimeofday(&start_tv, NULL);
1806#endif
1807
1808#ifdef SET_SIG_ALARM
1809 /*
1810 * Opening the Display may hang if the DISPLAY setting is wrong, or
1811 * the network connection is bad. Set an alarm timer to get out.
1812 */
1813 sig_alarm_called = FALSE;
1814 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1815 (RETSIGTYPE (*)())sig_alarm);
1816 alarm(2);
1817#endif
1818 x11_display = XOpenDisplay(NULL);
1819
1820#ifdef SET_SIG_ALARM
1821 alarm(0);
1822 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1823 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001824 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825#endif
1826 if (x11_display != NULL)
1827 {
1828# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1829 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001830 {
1831 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 xopen_message(&start_tv);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001833 verbose_leave();
1834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835# endif
1836 if (test_x11_window(x11_display) == FAIL)
1837 {
1838 /* Maybe window id is bad */
1839 x11_window = 0;
1840 XCloseDisplay(x11_display);
1841 x11_display = NULL;
1842 }
1843 else
1844 x11_display_from = XD_HERE;
1845 }
1846 }
1847 if (x11_window == 0 || x11_display == NULL)
1848 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001849
1850# ifdef FEAT_EVAL
1851 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1852# endif
1853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 return (result = OK);
1855}
1856
1857/*
1858 * Determine original x11 Window Title
1859 */
1860 static int
1861get_x11_title(test_only)
1862 int test_only;
1863{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001864 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865}
1866
1867/*
1868 * Determine original x11 Window icon
1869 */
1870 static int
1871get_x11_icon(test_only)
1872 int test_only;
1873{
1874 int retval = FALSE;
1875
1876 retval = get_x11_thing(FALSE, test_only);
1877
1878 /* could not get old icon, use terminal name */
1879 if (oldicon == NULL && !test_only)
1880 {
1881 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001882 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001884 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 }
1886
1887 return retval;
1888}
1889
1890 static int
1891get_x11_thing(get_title, test_only)
1892 int get_title; /* get title string */
1893 int test_only;
1894{
1895 XTextProperty text_prop;
1896 int retval = FALSE;
1897 Status status;
1898
1899 if (get_x11_windis() == OK)
1900 {
1901 /* Get window/icon name if any */
1902 if (get_title)
1903 status = XGetWMName(x11_display, x11_window, &text_prop);
1904 else
1905 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1906
1907 /*
1908 * If terminal is xterm, then x11_window may be a child window of the
1909 * outer xterm window that actually contains the window/icon name, so
1910 * keep traversing up the tree until a window with a title/icon is
1911 * found.
1912 */
1913 /* Previously this was only done for xterm and alikes. I don't see a
1914 * reason why it would fail for other terminal emulators.
1915 * if (term_is_xterm) */
1916 {
1917 Window root;
1918 Window parent;
1919 Window win = x11_window;
1920 Window *children;
1921 unsigned int num_children;
1922
1923 while (!status || text_prop.value == NULL)
1924 {
1925 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1926 &num_children))
1927 break;
1928 if (children)
1929 XFree((void *)children);
1930 if (parent == root || parent == 0)
1931 break;
1932
1933 win = parent;
1934 if (get_title)
1935 status = XGetWMName(x11_display, win, &text_prop);
1936 else
1937 status = XGetWMIconName(x11_display, win, &text_prop);
1938 }
1939 }
1940 if (status && text_prop.value != NULL)
1941 {
1942 retval = TRUE;
1943 if (!test_only)
1944 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001945#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
1946 if (text_prop.encoding == XA_STRING
1947# ifdef FEAT_MBYTE
1948 && !has_mbyte
1949# endif
1950 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
1952#endif
1953 if (get_title)
1954 oldtitle = vim_strsave((char_u *)text_prop.value);
1955 else
1956 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001957#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959 else
1960 {
1961 char **cl;
1962 Status transform_status;
1963 int n = 0;
1964
1965 transform_status = XmbTextPropertyToTextList(x11_display,
1966 &text_prop,
1967 &cl, &n);
1968 if (transform_status >= Success && n > 0 && cl[0])
1969 {
1970 if (get_title)
1971 oldtitle = vim_strsave((char_u *) cl[0]);
1972 else
1973 oldicon = vim_strsave((char_u *) cl[0]);
1974 XFreeStringList(cl);
1975 }
1976 else
1977 {
1978 if (get_title)
1979 oldtitle = vim_strsave((char_u *)text_prop.value);
1980 else
1981 oldicon = vim_strsave((char_u *)text_prop.value);
1982 }
1983 }
1984#endif
1985 }
1986 XFree((void *)text_prop.value);
1987 }
1988 }
1989 return retval;
1990}
1991
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02001992/* Xutf8 functions are not avaialble on older systems. Note that on some
1993 * systems X_HAVE_UTF8_STRING may be defined in a header file but
1994 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
1995 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02001997# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998# define USE_UTF8_STRING
1999# endif
2000#endif
2001
2002/*
2003 * Set x11 Window Title
2004 *
2005 * get_x11_windis() must be called before this and have returned OK
2006 */
2007 static void
2008set_x11_title(title)
2009 char_u *title;
2010{
2011 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2012 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2013 * supported everywhere and STRING doesn't work for multi-byte titles.
2014 */
2015#ifdef USE_UTF8_STRING
2016 if (enc_utf8)
2017 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2018 NULL, NULL, 0, NULL, NULL, NULL);
2019 else
2020#endif
2021 {
2022#if XtSpecificationRelease >= 4
2023# ifdef FEAT_XFONTSET
2024 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2025 NULL, NULL, 0, NULL, NULL, NULL);
2026# else
2027 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002028 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
2030 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002031 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 XSetWMProperties(x11_display, x11_window, &text_prop,
2033 NULL, NULL, 0, NULL, NULL, NULL);
2034# endif
2035#else
2036 XStoreName(x11_display, x11_window, (char *)title);
2037#endif
2038 }
2039 XFlush(x11_display);
2040}
2041
2042/*
2043 * Set x11 Window icon
2044 *
2045 * get_x11_windis() must be called before this and have returned OK
2046 */
2047 static void
2048set_x11_icon(icon)
2049 char_u *icon;
2050{
2051 /* See above for comments about using X*SetWMProperties(). */
2052#ifdef USE_UTF8_STRING
2053 if (enc_utf8)
2054 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2055 NULL, 0, NULL, NULL, NULL);
2056 else
2057#endif
2058 {
2059#if XtSpecificationRelease >= 4
2060# ifdef FEAT_XFONTSET
2061 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2062 NULL, 0, NULL, NULL, NULL);
2063# else
2064 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002065 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002067 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2069 NULL, 0, NULL, NULL, NULL);
2070# endif
2071#else
2072 XSetIconName(x11_display, x11_window, (char *)icon);
2073#endif
2074 }
2075 XFlush(x11_display);
2076}
2077
2078#else /* FEAT_X11 */
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 static int
2081get_x11_title(test_only)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002082 int test_only UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083{
2084 return FALSE;
2085}
2086
2087 static int
2088get_x11_icon(test_only)
2089 int test_only;
2090{
2091 if (!test_only)
2092 {
2093 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002094 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002096 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 }
2098 return FALSE;
2099}
2100
2101#endif /* FEAT_X11 */
2102
2103 int
2104mch_can_restore_title()
2105{
2106 return get_x11_title(TRUE);
2107}
2108
2109 int
2110mch_can_restore_icon()
2111{
2112 return get_x11_icon(TRUE);
2113}
2114
2115/*
2116 * Set the window title and icon.
2117 */
2118 void
2119mch_settitle(title, icon)
2120 char_u *title;
2121 char_u *icon;
2122{
2123 int type = 0;
2124 static int recursive = 0;
2125
2126 if (T_NAME == NULL) /* no terminal name (yet) */
2127 return;
2128 if (title == NULL && icon == NULL) /* nothing to do */
2129 return;
2130
2131 /* When one of the X11 functions causes a deadly signal, we get here again
2132 * recursively. Avoid hanging then (something is probably locked). */
2133 if (recursive)
2134 return;
2135 ++recursive;
2136
2137 /*
2138 * if the window ID and the display is known, we may use X11 calls
2139 */
2140#ifdef FEAT_X11
2141 if (get_x11_windis() == OK)
2142 type = 1;
2143#else
2144# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2145 if (gui.in_use)
2146 type = 1;
2147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148#endif
2149
2150 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002151 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 * than x11 calls, because the x11 calls don't always work
2153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if ((type || *T_TS != NUL) && title != NULL)
2155 {
2156 if (oldtitle == NULL
2157#ifdef FEAT_GUI
2158 && !gui.in_use
2159#endif
2160 ) /* first call but not in GUI, save title */
2161 (void)get_x11_title(FALSE);
2162
2163 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2164 term_settitle(title);
2165#ifdef FEAT_X11
2166 else
2167# ifdef FEAT_GUI_GTK
2168 if (!gui.in_use) /* don't do this if GTK+ is running */
2169# endif
2170 set_x11_title(title); /* x11 */
2171#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002172#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2174 else
2175 gui_mch_settitle(title, icon);
2176#endif
2177 did_set_title = TRUE;
2178 }
2179
2180 if ((type || *T_CIS != NUL) && icon != NULL)
2181 {
2182 if (oldicon == NULL
2183#ifdef FEAT_GUI
2184 && !gui.in_use
2185#endif
2186 ) /* first call, save icon */
2187 get_x11_icon(FALSE);
2188
2189 if (*T_CIS != NUL)
2190 {
2191 out_str(T_CIS); /* set icon start */
2192 out_str_nf(icon);
2193 out_str(T_CIE); /* set icon end */
2194 out_flush();
2195 }
2196#ifdef FEAT_X11
2197 else
2198# ifdef FEAT_GUI_GTK
2199 if (!gui.in_use) /* don't do this if GTK+ is running */
2200# endif
2201 set_x11_icon(icon); /* x11 */
2202#endif
2203 did_set_icon = TRUE;
2204 }
2205 --recursive;
2206}
2207
2208/*
2209 * Restore the window/icon title.
2210 * "which" is one of:
2211 * 1 only restore title
2212 * 2 only restore icon
2213 * 3 restore title and icon
2214 */
2215 void
2216mch_restore_title(which)
2217 int which;
2218{
2219 /* only restore the title or icon when it has been set */
2220 mch_settitle(((which & 1) && did_set_title) ?
2221 (oldtitle ? oldtitle : p_titleold) : NULL,
2222 ((which & 2) && did_set_icon) ? oldicon : NULL);
2223}
2224
2225#endif /* FEAT_TITLE */
2226
2227/*
2228 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002229 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 */
2231 int
2232vim_is_xterm(name)
2233 char_u *name;
2234{
2235 if (name == NULL)
2236 return FALSE;
2237 return (STRNICMP(name, "xterm", 5) == 0
2238 || STRNICMP(name, "nxterm", 6) == 0
2239 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002240 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 || STRNICMP(name, "rxvt", 4) == 0
2242 || STRCMP(name, "builtin_xterm") == 0);
2243}
2244
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002245#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2246/*
2247 * Return TRUE if "name" appears to be that of a terminal
2248 * known to support the xterm-style mouse protocol.
2249 * Relies on term_is_xterm having been set to its correct value.
2250 */
2251 int
2252use_xterm_like_mouse(name)
2253 char_u *name;
2254{
2255 return (name != NULL
2256 && (term_is_xterm || STRNICMP(name, "screen", 6) == 0));
2257}
2258#endif
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2261/*
2262 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2263 * Return 1 for "xterm".
2264 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002265 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002266 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 */
2268 int
2269use_xterm_mouse()
2270{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002271 if (ttym_flags == TTYM_SGR)
2272 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002273 if (ttym_flags == TTYM_URXVT)
2274 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (ttym_flags == TTYM_XTERM2)
2276 return 2;
2277 if (ttym_flags == TTYM_XTERM)
2278 return 1;
2279 return 0;
2280}
2281#endif
2282
2283 int
2284vim_is_iris(name)
2285 char_u *name;
2286{
2287 if (name == NULL)
2288 return FALSE;
2289 return (STRNICMP(name, "iris-ansi", 9) == 0
2290 || STRCMP(name, "builtin_iris-ansi") == 0);
2291}
2292
2293 int
2294vim_is_vt300(name)
2295 char_u *name;
2296{
2297 if (name == NULL)
2298 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002299 /* catch VT100 - VT5xx */
2300 return ((STRNICMP(name, "vt", 2) == 0
2301 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 || STRCMP(name, "builtin_vt320") == 0);
2303}
2304
2305/*
2306 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2307 * This should include all windowed terminal emulators.
2308 */
2309 int
2310vim_is_fastterm(name)
2311 char_u *name;
2312{
2313 if (name == NULL)
2314 return FALSE;
2315 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2316 return TRUE;
2317 return ( STRNICMP(name, "hpterm", 6) == 0
2318 || STRNICMP(name, "sun-cmd", 7) == 0
2319 || STRNICMP(name, "screen", 6) == 0
2320 || STRNICMP(name, "dtterm", 6) == 0);
2321}
2322
2323/*
2324 * Insert user name in s[len].
2325 * Return OK if a name found.
2326 */
2327 int
2328mch_get_user_name(s, len)
2329 char_u *s;
2330 int len;
2331{
2332#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002333 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 return OK;
2335#else
2336 return mch_get_uname(getuid(), s, len);
2337#endif
2338}
2339
2340/*
2341 * Insert user name for "uid" in s[len].
2342 * Return OK if a name found.
2343 */
2344 int
2345mch_get_uname(uid, s, len)
2346 uid_t uid;
2347 char_u *s;
2348 int len;
2349{
2350#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2351 struct passwd *pw;
2352
2353 if ((pw = getpwuid(uid)) != NULL
2354 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2355 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002356 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 return OK;
2358 }
2359#endif
2360 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2361 return FAIL; /* a number is not a name */
2362}
2363
2364/*
2365 * Insert host name is s[len].
2366 */
2367
2368#ifdef HAVE_SYS_UTSNAME_H
2369 void
2370mch_get_host_name(s, len)
2371 char_u *s;
2372 int len;
2373{
2374 struct utsname vutsname;
2375
2376 if (uname(&vutsname) < 0)
2377 *s = NUL;
2378 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002379 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380}
2381#else /* HAVE_SYS_UTSNAME_H */
2382
2383# ifdef HAVE_SYS_SYSTEMINFO_H
2384# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2385# endif
2386
2387 void
2388mch_get_host_name(s, len)
2389 char_u *s;
2390 int len;
2391{
2392# ifdef VAXC
2393 vaxc$gethostname((char *)s, len);
2394# else
2395 gethostname((char *)s, len);
2396# endif
2397 s[len - 1] = NUL; /* make sure it's terminated */
2398}
2399#endif /* HAVE_SYS_UTSNAME_H */
2400
2401/*
2402 * return process ID
2403 */
2404 long
2405mch_get_pid()
2406{
2407 return (long)getpid();
2408}
2409
2410#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
2411static char *strerror __ARGS((int));
2412
2413 static char *
2414strerror(err)
2415 int err;
2416{
2417 extern int sys_nerr;
2418 extern char *sys_errlist[];
2419 static char er[20];
2420
2421 if (err > 0 && err < sys_nerr)
2422 return (sys_errlist[err]);
2423 sprintf(er, "Error %d", err);
2424 return er;
2425}
2426#endif
2427
2428/*
2429 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2430 * Return OK for success, FAIL for failure.
2431 */
2432 int
2433mch_dirname(buf, len)
2434 char_u *buf;
2435 int len;
2436{
2437#if defined(USE_GETCWD)
2438 if (getcwd((char *)buf, len) == NULL)
2439 {
2440 STRCPY(buf, strerror(errno));
2441 return FAIL;
2442 }
2443 return OK;
2444#else
2445 return (getwd((char *)buf) != NULL ? OK : FAIL);
2446#endif
2447}
2448
2449#if defined(OS2) || defined(PROTO)
2450/*
2451 * Replace all slashes by backslashes.
2452 * When 'shellslash' set do it the other way around.
2453 */
2454 void
2455slash_adjust(p)
2456 char_u *p;
2457{
2458 while (*p)
2459 {
2460 if (*p == psepcN)
2461 *p = psepc;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002462 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464}
2465#endif
2466
2467/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002468 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 *
2470 * return FAIL for failure, OK for success
2471 */
2472 int
2473mch_FullName(fname, buf, len, force)
2474 char_u *fname, *buf;
2475 int len;
2476 int force; /* also expand when already absolute path */
2477{
2478 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002479#ifdef OS2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 int only_drive; /* file name is only a drive letter */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002481#endif
2482#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 int fd = -1;
2484 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 char_u olddir[MAXPATHL];
2487 char_u *p;
2488 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002489#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002490 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2491 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002492#endif
2493
Bram Moolenaar38323e42007-03-06 19:22:53 +00002494#ifdef VMS
2495 fname = vms_fixfilename(fname);
2496#endif
2497
Bram Moolenaara2442432007-04-26 14:26:37 +00002498#ifdef __CYGWIN__
2499 /*
2500 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2501 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002502# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2503 cygwin_conv_path(CCP_WIN_A_TO_POSIX, fname, posix_fname, MAXPATHL);
2504# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002505 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002506# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002507 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002508#endif
2509
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002510 /* Expand it if forced or not an absolute path.
2511 * Do not do it for "/file", the result is always "/". */
2512 if ((force || !mch_isFullName(fname))
2513 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
2515 /*
2516 * If the file name has a path, change to that directory for a moment,
2517 * and then do the getwd() (and get back to where we were).
2518 * This will get the correct path name with "../" things.
2519 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002520#ifdef OS2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 only_drive = 0;
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002522 if (p != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 || ((p = vim_strrchr(fname, '\\')) != NULL)
2524 || (((p = vim_strchr(fname, ':')) != NULL) && ++only_drive))
Bram Moolenaar38323e42007-03-06 19:22:53 +00002525#else
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002526 if (p != NULL)
Bram Moolenaar38323e42007-03-06 19:22:53 +00002527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002529#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 /*
2531 * Use fchdir() if possible, it's said to be faster and more
2532 * reliable. But on SunOS 4 it might not work. Check this by
2533 * doing a fchdir() right now.
2534 */
2535 if (!dont_fchdir)
2536 {
2537 fd = open(".", O_RDONLY | O_EXTRA, 0);
2538 if (fd >= 0 && fchdir(fd) < 0)
2539 {
2540 close(fd);
2541 fd = -1;
2542 dont_fchdir = TRUE; /* don't try again */
2543 }
2544 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
2547 /* Only change directory when we are sure we can return to where
2548 * we are now. After doing "su" chdir(".") might not work. */
2549 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002550#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 (mch_dirname(olddir, MAXPATHL) == FAIL
2554 || mch_chdir((char *)olddir) != 0))
2555 {
2556 p = NULL; /* can't get current dir: don't chdir */
2557 retval = FAIL;
2558 }
2559 else
2560 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002561#ifdef OS2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 /*
2563 * compensate for case where ':' from "D:" was the only
2564 * path separator detected in the file name; the _next_
2565 * character has to be removed, and then restored later.
2566 */
2567 if (only_drive)
2568 p++;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 /* The directory is copied into buf[], to be able to remove
2571 * the file name without changing it (could be a string in
2572 * read-only memory) */
2573 if (p - fname >= len)
2574 retval = FAIL;
2575 else
2576 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002577 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (mch_chdir((char *)buf))
2579 retval = FAIL;
2580 else
2581 fname = p + 1;
2582 *buf = NUL;
2583 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002584#ifdef OS2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (only_drive)
2586 {
2587 p--;
2588 if (retval != FAIL)
2589 fname--;
2590 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 }
2593 }
2594 if (mch_dirname(buf, len) == FAIL)
2595 {
2596 retval = FAIL;
2597 *buf = NUL;
2598 }
2599 if (p != NULL)
2600 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002601#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 if (fd >= 0)
2603 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002604 if (p_verbose >= 5)
2605 {
2606 verbose_enter();
2607 MSG("fchdir() to previous dir");
2608 verbose_leave();
2609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 l = fchdir(fd);
2611 close(fd);
2612 }
2613 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 l = mch_chdir((char *)olddir);
2616 if (l != 0)
2617 EMSG(_(e_prev_dir));
2618 }
2619
2620 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002621 if (l >= len - 1)
2622 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002623#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002624 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002626 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002631 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 return FAIL;
2633
2634 /* Do not append ".", "/dir/." is equal to "/dir". */
2635 if (STRCMP(fname, ".") != 0)
2636 STRCAT(buf, fname);
2637
2638 return OK;
2639}
2640
2641/*
2642 * Return TRUE if "fname" does not depend on the current directory.
2643 */
2644 int
2645mch_isFullName(fname)
2646 char_u *fname;
2647{
2648#ifdef __EMX__
2649 return _fnisabs(fname);
2650#else
2651# ifdef VMS
2652 return ( fname[0] == '/' || fname[0] == '.' ||
2653 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2654 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2655 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
2656# else
2657 return (*fname == '/' || *fname == '~');
2658# endif
2659#endif
2660}
2661
Bram Moolenaar24552be2005-12-10 20:17:30 +00002662#if defined(USE_FNAME_CASE) || defined(PROTO)
2663/*
2664 * Set the case of the file name, if it already exists. This will cause the
2665 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002666 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002667 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002668 void
2669fname_case(name, len)
2670 char_u *name;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002671 int len UNUSED; /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002672{
2673 struct stat st;
2674 char_u *slash, *tail;
2675 DIR *dirp;
2676 struct dirent *dp;
2677
2678 if (lstat((char *)name, &st) >= 0)
2679 {
2680 /* Open the directory where the file is located. */
2681 slash = vim_strrchr(name, '/');
2682 if (slash == NULL)
2683 {
2684 dirp = opendir(".");
2685 tail = name;
2686 }
2687 else
2688 {
2689 *slash = NUL;
2690 dirp = opendir((char *)name);
2691 *slash = '/';
2692 tail = slash + 1;
2693 }
2694
2695 if (dirp != NULL)
2696 {
2697 while ((dp = readdir(dirp)) != NULL)
2698 {
2699 /* Only accept names that differ in case and are the same byte
2700 * length. TODO: accept different length name. */
2701 if (STRICMP(tail, dp->d_name) == 0
2702 && STRLEN(tail) == STRLEN(dp->d_name))
2703 {
2704 char_u newname[MAXPATHL + 1];
2705 struct stat st2;
2706
2707 /* Verify the inode is equal. */
2708 vim_strncpy(newname, name, MAXPATHL);
2709 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2710 MAXPATHL - (tail - name));
2711 if (lstat((char *)newname, &st2) >= 0
2712 && st.st_ino == st2.st_ino
2713 && st.st_dev == st2.st_dev)
2714 {
2715 STRCPY(tail, dp->d_name);
2716 break;
2717 }
2718 }
2719 }
2720
2721 closedir(dirp);
2722 }
2723 }
2724}
2725#endif
2726
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727/*
2728 * Get file permissions for 'name'.
2729 * Returns -1 when it doesn't exist.
2730 */
2731 long
2732mch_getperm(name)
2733 char_u *name;
2734{
2735 struct stat statb;
2736
2737 /* Keep the #ifdef outside of stat(), it may be a macro. */
2738#ifdef VMS
2739 if (stat((char *)vms_fixfilename(name), &statb))
2740#else
2741 if (stat((char *)name, &statb))
2742#endif
2743 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002744#ifdef __INTERIX
2745 /* The top bit makes the value negative, which means the file doesn't
2746 * exist. Remove the bit, we don't use it. */
2747 return statb.st_mode & ~S_ADDACE;
2748#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751}
2752
2753/*
2754 * set file permission for 'name' to 'perm'
2755 *
2756 * return FAIL for failure, OK otherwise
2757 */
2758 int
2759mch_setperm(name, perm)
2760 char_u *name;
2761 long perm;
2762{
2763 return (chmod((char *)
2764#ifdef VMS
2765 vms_fixfilename(name),
2766#else
2767 name,
2768#endif
2769 (mode_t)perm) == 0 ? OK : FAIL);
2770}
2771
2772#if defined(HAVE_ACL) || defined(PROTO)
2773# ifdef HAVE_SYS_ACL_H
2774# include <sys/acl.h>
2775# endif
2776# ifdef HAVE_SYS_ACCESS_H
2777# include <sys/access.h>
2778# endif
2779
2780# ifdef HAVE_SOLARIS_ACL
2781typedef struct vim_acl_solaris_T {
2782 int acl_cnt;
2783 aclent_t *acl_entry;
2784} vim_acl_solaris_T;
2785# endif
2786
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002787#if defined(HAVE_SELINUX) || defined(PROTO)
2788/*
2789 * Copy security info from "from_file" to "to_file".
2790 */
2791 void
2792mch_copy_sec(from_file, to_file)
2793 char_u *from_file;
2794 char_u *to_file;
2795{
2796 if (from_file == NULL)
2797 return;
2798
2799 if (selinux_enabled == -1)
2800 selinux_enabled = is_selinux_enabled();
2801
2802 if (selinux_enabled > 0)
2803 {
2804 security_context_t from_context = NULL;
2805 security_context_t to_context = NULL;
2806
2807 if (getfilecon((char *)from_file, &from_context) < 0)
2808 {
2809 /* If the filesystem doesn't support extended attributes,
2810 the original had no special security context and the
2811 target cannot have one either. */
2812 if (errno == EOPNOTSUPP)
2813 return;
2814
2815 MSG_PUTS(_("\nCould not get security context for "));
2816 msg_outtrans(from_file);
2817 msg_putchar('\n');
2818 return;
2819 }
2820 if (getfilecon((char *)to_file, &to_context) < 0)
2821 {
2822 MSG_PUTS(_("\nCould not get security context for "));
2823 msg_outtrans(to_file);
2824 msg_putchar('\n');
2825 freecon (from_context);
2826 return ;
2827 }
2828 if (strcmp(from_context, to_context) != 0)
2829 {
2830 if (setfilecon((char *)to_file, from_context) < 0)
2831 {
2832 MSG_PUTS(_("\nCould not set security context for "));
2833 msg_outtrans(to_file);
2834 msg_putchar('\n');
2835 }
2836 }
2837 freecon(to_context);
2838 freecon(from_context);
2839 }
2840}
2841#endif /* HAVE_SELINUX */
2842
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002843#if defined(HAVE_SMACK) && !defined(PROTO)
2844/*
2845 * Copy security info from "from_file" to "to_file".
2846 */
2847 void
2848mch_copy_sec(from_file, to_file)
2849 char_u *from_file;
2850 char_u *to_file;
2851{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002852 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002853 {
2854 XATTR_NAME_SMACK,
2855 XATTR_NAME_SMACKEXEC,
2856 XATTR_NAME_SMACKMMAP
2857 };
2858
2859 char buffer[SMACK_LABEL_LEN];
2860 const char *name;
2861 int index;
2862 int ret;
2863 ssize_t size;
2864
2865 if (from_file == NULL)
2866 return;
2867
2868 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2869 / sizeof(smack_copied_attributes)[0]) ; index++)
2870 {
2871 /* get the name of the attribute to copy */
2872 name = smack_copied_attributes[index];
2873
2874 /* get the value of the attribute in buffer */
2875 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2876 if (size >= 0)
2877 {
2878 /* copy the attribute value of buffer */
2879 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2880 if (ret < 0)
2881 {
2882 MSG_PUTS(_("Could not set security context "));
2883 MSG_PUTS(name);
2884 MSG_PUTS(_(" for "));
2885 msg_outtrans(to_file);
2886 msg_putchar('\n');
2887 }
2888 }
2889 else
2890 {
2891 /* what reason of not having the attribute value? */
2892 switch (errno)
2893 {
2894 case ENOTSUP:
2895 /* extended attributes aren't supported or enabled */
2896 /* should a message be echoed? not sure... */
2897 return; /* leave because it isn't usefull to continue */
2898
2899 case ERANGE:
2900 default:
2901 /* no enough size OR unexpected error */
2902 MSG_PUTS(_("Could not get security context "));
2903 MSG_PUTS(name);
2904 MSG_PUTS(_(" for "));
2905 msg_outtrans(from_file);
2906 MSG_PUTS(_(". Removing it!\n"));
2907 /* FALLTHROUGH to remove the attribute */
2908
2909 case ENODATA:
2910 /* no attribute of this name */
2911 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002912 /* Silently ignore errors, apparently this happens when
2913 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002914 break;
2915 }
2916 }
2917 }
2918}
2919#endif /* HAVE_SMACK */
2920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921/*
2922 * Return a pointer to the ACL of file "fname" in allocated memory.
2923 * Return NULL if the ACL is not available for whatever reason.
2924 */
2925 vim_acl_T
2926mch_get_acl(fname)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002927 char_u *fname UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
2929 vim_acl_T ret = NULL;
2930#ifdef HAVE_POSIX_ACL
2931 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2932#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002933#ifdef HAVE_SOLARIS_ZFS_ACL
2934 acl_t *aclent;
2935
2936 if (acl_get((char *)fname, 0, &aclent) < 0)
2937 return NULL;
2938 ret = (vim_acl_T)aclent;
2939#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940#ifdef HAVE_SOLARIS_ACL
2941 vim_acl_solaris_T *aclent;
2942
2943 aclent = malloc(sizeof(vim_acl_solaris_T));
2944 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2945 {
2946 free(aclent);
2947 return NULL;
2948 }
2949 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2950 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2951 {
2952 free(aclent->acl_entry);
2953 free(aclent);
2954 return NULL;
2955 }
2956 ret = (vim_acl_T)aclent;
2957#else
2958#if defined(HAVE_AIX_ACL)
2959 int aclsize;
2960 struct acl *aclent;
2961
2962 aclsize = sizeof(struct acl);
2963 aclent = malloc(aclsize);
2964 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2965 {
2966 if (errno == ENOSPC)
2967 {
2968 aclsize = aclent->acl_len;
2969 aclent = realloc(aclent, aclsize);
2970 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2971 {
2972 free(aclent);
2973 return NULL;
2974 }
2975 }
2976 else
2977 {
2978 free(aclent);
2979 return NULL;
2980 }
2981 }
2982 ret = (vim_acl_T)aclent;
2983#endif /* HAVE_AIX_ACL */
2984#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002985#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986#endif /* HAVE_POSIX_ACL */
2987 return ret;
2988}
2989
2990/*
2991 * Set the ACL of file "fname" to "acl" (unless it's NULL).
2992 */
2993 void
2994mch_set_acl(fname, aclent)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002995 char_u *fname UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 vim_acl_T aclent;
2997{
2998 if (aclent == NULL)
2999 return;
3000#ifdef HAVE_POSIX_ACL
3001 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3002#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003003#ifdef HAVE_SOLARIS_ZFS_ACL
3004 acl_set((char *)fname, (acl_t *)aclent);
3005#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006#ifdef HAVE_SOLARIS_ACL
3007 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3008 ((vim_acl_solaris_T *)aclent)->acl_entry);
3009#else
3010#ifdef HAVE_AIX_ACL
3011 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3012#endif /* HAVE_AIX_ACL */
3013#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003014#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015#endif /* HAVE_POSIX_ACL */
3016}
3017
3018 void
3019mch_free_acl(aclent)
3020 vim_acl_T aclent;
3021{
3022 if (aclent == NULL)
3023 return;
3024#ifdef HAVE_POSIX_ACL
3025 acl_free((acl_t)aclent);
3026#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003027#ifdef HAVE_SOLARIS_ZFS_ACL
3028 acl_free((acl_t *)aclent);
3029#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#ifdef HAVE_SOLARIS_ACL
3031 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3032 free(aclent);
3033#else
3034#ifdef HAVE_AIX_ACL
3035 free(aclent);
3036#endif /* HAVE_AIX_ACL */
3037#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003038#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039#endif /* HAVE_POSIX_ACL */
3040}
3041#endif
3042
3043/*
3044 * Set hidden flag for "name".
3045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 void
3047mch_hide(name)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003048 char_u *name UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
3050 /* can't hide a file */
3051}
3052
3053/*
3054 * return TRUE if "name" is a directory
3055 * return FALSE if "name" is not a directory
3056 * return FALSE for error
3057 */
3058 int
3059mch_isdir(name)
3060 char_u *name;
3061{
3062 struct stat statb;
3063
3064 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3065 return FALSE;
3066 if (stat((char *)name, &statb))
3067 return FALSE;
3068#ifdef _POSIX_SOURCE
3069 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3070#else
3071 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3072#endif
3073}
3074
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075static int executable_file __ARGS((char_u *name));
3076
3077/*
3078 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3079 */
3080 static int
3081executable_file(name)
3082 char_u *name;
3083{
3084 struct stat st;
3085
3086 if (stat((char *)name, &st))
3087 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003088#ifdef VMS
3089 /* Like on Unix system file can have executable rights but not necessarily
3090 * be an executable, but on Unix is not a default for an ordianry file to
3091 * have an executable flag - on VMS it is in most cases.
3092 * Therefore, this check does not have any sense - let keep us to the
3093 * conventions instead:
3094 * *.COM and *.EXE files are the executables - the rest are not. This is
3095 * not ideal but better then it was.
3096 */
3097 int vms_executable = 0;
3098 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3099 {
3100 if (strstr(vms_tolower((char*)name),".exe") != NULL
3101 || strstr(vms_tolower((char*)name),".com")!= NULL)
3102 vms_executable = 1;
3103 }
3104 return vms_executable;
3105#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108}
3109
3110/*
3111 * Return 1 if "name" can be found in $PATH and executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003112 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 * Return -1 if unknown.
3114 */
3115 int
Bram Moolenaarb5971142015-03-21 17:32:19 +01003116mch_can_exe(name, path, use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 char_u *name;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003118 char_u **path;
Bram Moolenaarb5971142015-03-21 17:32:19 +01003119 int use_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 char_u *buf;
3122 char_u *p, *e;
3123 int retval;
3124
Bram Moolenaarb5971142015-03-21 17:32:19 +01003125 /* When "use_path" is false and if it's an absolute or relative path don't
3126 * need to use $PATH. */
3127 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3128 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003129 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003130 /* There must be a path separator, files in the current directory
3131 * can't be executed. */
3132 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003133 {
3134 if (path != NULL)
3135 {
3136 if (name[0] == '.')
3137 *path = FullName_save(name, TRUE);
3138 else
3139 *path = vim_strsave(name);
3140 }
3141 return TRUE;
3142 }
3143 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 p = (char_u *)getenv("PATH");
3147 if (p == NULL || *p == NUL)
3148 return -1;
3149 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3150 if (buf == NULL)
3151 return -1;
3152
3153 /*
3154 * Walk through all entries in $PATH to check if "name" exists there and
3155 * is an executable file.
3156 */
3157 for (;;)
3158 {
3159 e = (char_u *)strchr((char *)p, ':');
3160 if (e == NULL)
3161 e = p + STRLEN(p);
3162 if (e - p <= 1) /* empty entry means current dir */
3163 STRCPY(buf, "./");
3164 else
3165 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003166 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 add_pathsep(buf);
3168 }
3169 STRCAT(buf, name);
3170 retval = executable_file(buf);
3171 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003172 {
3173 if (path != NULL)
3174 {
3175 if (buf[0] == '.')
3176 *path = FullName_save(buf, TRUE);
3177 else
3178 *path = vim_strsave(buf);
3179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
3183 if (*e != ':')
3184 break;
3185 p = e + 1;
3186 }
3187
3188 vim_free(buf);
3189 return retval;
3190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192/*
3193 * Check what "name" is:
3194 * NODE_NORMAL: file or directory (or doesn't exist)
3195 * NODE_WRITABLE: writable device, socket, fifo, etc.
3196 * NODE_OTHER: non-writable things
3197 */
3198 int
3199mch_nodetype(name)
3200 char_u *name;
3201{
3202 struct stat st;
3203
3204 if (stat((char *)name, &st))
3205 return NODE_NORMAL;
3206 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3207 return NODE_NORMAL;
3208#ifndef OS2
3209 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3210 return NODE_OTHER;
3211#endif
3212 /* Everything else is writable? */
3213 return NODE_WRITABLE;
3214}
3215
3216 void
3217mch_early_init()
3218{
3219#ifdef HAVE_CHECK_STACK_GROWTH
3220 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 check_stack_growth((char *)&i);
3223
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003224# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 get_stack_limit();
3226# endif
3227
3228#endif
3229
3230 /*
3231 * Setup an alternative stack for signals. Helps to catch signals when
3232 * running out of stack space.
3233 * Use of sigaltstack() is preferred, it's more portable.
3234 * Ignore any errors.
3235 */
3236#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003237 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 init_signal_stack();
3239#endif
3240}
3241
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003242#if defined(EXITFREE) || defined(PROTO)
3243 void
3244mch_free_mem()
3245{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003246# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3247 if (clip_star.owned)
3248 clip_lose_selection(&clip_star);
3249 if (clip_plus.owned)
3250 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003251# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003252# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003253 if (xterm_Shell != (Widget)0)
3254 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003255# ifndef LESSTIF_VERSION
3256 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003257 if (xterm_dpy != NULL)
3258 XtCloseDisplay(xterm_dpy);
3259 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003260 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003261 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003262# ifdef FEAT_X11
3263 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3264# endif
3265 }
3266# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003267# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003268# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003269 if (x11_display != NULL
3270# ifdef FEAT_XCLIPBOARD
3271 && x11_display != xterm_dpy
3272# endif
3273 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003274 XCloseDisplay(x11_display);
3275# endif
3276# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
3277 vim_free(signal_stack);
3278 signal_stack = NULL;
3279# endif
3280# ifdef FEAT_TITLE
3281 vim_free(oldtitle);
3282 vim_free(oldicon);
3283# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003284}
3285#endif
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287static void exit_scroll __ARGS((void));
3288
3289/*
3290 * Output a newline when exiting.
3291 * Make sure the newline goes to the same stream as the text.
3292 */
3293 static void
3294exit_scroll()
3295{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003296 if (silent_mode)
3297 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 if (newline_on_exit || msg_didout)
3299 {
3300 if (msg_use_printf())
3301 {
3302 if (info_message)
3303 mch_msg("\n");
3304 else
3305 mch_errmsg("\r\n");
3306 }
3307 else
3308 out_char('\n');
3309 }
3310 else
3311 {
3312 restore_cterm_colors(); /* get original colors back */
3313 msg_clr_eos_force(); /* clear the rest of the display */
3314 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3315 }
3316}
3317
3318 void
3319mch_exit(r)
3320 int r;
3321{
3322 exiting = TRUE;
3323
3324#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3325 x11_export_final_selection();
3326#endif
3327
3328#ifdef FEAT_GUI
3329 if (!gui.in_use)
3330#endif
3331 {
3332 settmode(TMODE_COOK);
3333#ifdef FEAT_TITLE
3334 mch_restore_title(3); /* restore xterm title and icon name */
3335#endif
3336 /*
3337 * When t_ti is not empty but it doesn't cause swapping terminal
3338 * pages, need to output a newline when msg_didout is set. But when
3339 * t_ti does swap pages it should not go to the shell page. Do this
3340 * before stoptermcap().
3341 */
3342 if (swapping_screen() && !newline_on_exit)
3343 exit_scroll();
3344
3345 /* Stop termcap: May need to check for T_CRV response, which
3346 * requires RAW mode. */
3347 stoptermcap();
3348
3349 /*
3350 * A newline is only required after a message in the alternate screen.
3351 * This is set to TRUE by wait_return().
3352 */
3353 if (!swapping_screen() || newline_on_exit)
3354 exit_scroll();
3355
3356 /* Cursor may have been switched off without calling starttermcap()
3357 * when doing "vim -u vimrc" and vimrc contains ":q". */
3358 if (full_screen)
3359 cursor_on();
3360 }
3361 out_flush();
3362 ml_close_all(TRUE); /* remove all memfiles */
3363 may_core_dump();
3364#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 gui_exit(r);
3367#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003368
Bram Moolenaar56718732006-03-15 22:53:57 +00003369#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003370 mac_conv_cleanup();
3371#endif
3372
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#ifdef __QNX__
3374 /* A core dump won't be created if the signal handler
3375 * doesn't return, so we can't call exit() */
3376 if (deadly_signal != 0)
3377 return;
3378#endif
3379
Bram Moolenaar009b2592004-10-24 19:18:58 +00003380#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003381 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003382#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003383
3384#ifdef EXITFREE
3385 free_all_mem();
3386#endif
3387
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 exit(r);
3389}
3390
3391 static void
3392may_core_dump()
3393{
3394 if (deadly_signal != 0)
3395 {
3396 signal(deadly_signal, SIG_DFL);
3397 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3398 }
3399}
3400
3401#ifndef VMS
3402
3403 void
3404mch_settmode(tmode)
3405 int tmode;
3406{
3407 static int first = TRUE;
3408
3409 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3410#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3411 /*
3412 * for "new" tty systems
3413 */
3414# ifdef HAVE_TERMIOS_H
3415 static struct termios told;
3416 struct termios tnew;
3417# else
3418 static struct termio told;
3419 struct termio tnew;
3420# endif
3421
3422 if (first)
3423 {
3424 first = FALSE;
3425# if defined(HAVE_TERMIOS_H)
3426 tcgetattr(read_cmd_fd, &told);
3427# else
3428 ioctl(read_cmd_fd, TCGETA, &told);
3429# endif
3430 }
3431
3432 tnew = told;
3433 if (tmode == TMODE_RAW)
3434 {
3435 /*
3436 * ~ICRNL enables typing ^V^M
3437 */
3438 tnew.c_iflag &= ~ICRNL;
3439 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3440# if defined(IEXTEN) && !defined(__MINT__)
3441 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3442 /* but it breaks function keys on MINT */
3443# endif
3444 );
3445# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3446 tnew.c_oflag &= ~ONLCR;
3447# endif
3448 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3449 tnew.c_cc[VTIME] = 0; /* don't wait */
3450 }
3451 else if (tmode == TMODE_SLEEP)
3452 tnew.c_lflag &= ~(ECHO);
3453
3454# if defined(HAVE_TERMIOS_H)
3455 {
3456 int n = 10;
3457
3458 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3459 * few times. */
3460 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3461 && errno == EINTR && n > 0)
3462 --n;
3463 }
3464# else
3465 ioctl(read_cmd_fd, TCSETA, &tnew);
3466# endif
3467
3468#else
3469
3470 /*
3471 * for "old" tty systems
3472 */
3473# ifndef TIOCSETN
3474# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3475# endif
3476 static struct sgttyb ttybold;
3477 struct sgttyb ttybnew;
3478
3479 if (first)
3480 {
3481 first = FALSE;
3482 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3483 }
3484
3485 ttybnew = ttybold;
3486 if (tmode == TMODE_RAW)
3487 {
3488 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3489 ttybnew.sg_flags |= RAW;
3490 }
3491 else if (tmode == TMODE_SLEEP)
3492 ttybnew.sg_flags &= ~(ECHO);
3493 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3494#endif
3495 curr_tmode = tmode;
3496}
3497
3498/*
3499 * Try to get the code for "t_kb" from the stty setting
3500 *
3501 * Even if termcap claims a backspace key, the user's setting *should*
3502 * prevail. stty knows more about reality than termcap does, and if
3503 * somebody's usual erase key is DEL (which, for most BSD users, it will
3504 * be), they're going to get really annoyed if their erase key starts
3505 * doing forward deletes for no reason. (Eric Fischer)
3506 */
3507 void
3508get_stty()
3509{
3510 char_u buf[2];
3511 char_u *p;
3512
3513 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3514#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3515 /* for "new" tty systems */
3516# ifdef HAVE_TERMIOS_H
3517 struct termios keys;
3518# else
3519 struct termio keys;
3520# endif
3521
3522# if defined(HAVE_TERMIOS_H)
3523 if (tcgetattr(read_cmd_fd, &keys) != -1)
3524# else
3525 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1)
3526# endif
3527 {
3528 buf[0] = keys.c_cc[VERASE];
3529 intr_char = keys.c_cc[VINTR];
3530#else
3531 /* for "old" tty systems */
3532 struct sgttyb keys;
3533
3534 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1)
3535 {
3536 buf[0] = keys.sg_erase;
3537 intr_char = keys.sg_kill;
3538#endif
3539 buf[1] = NUL;
3540 add_termcode((char_u *)"kb", buf, FALSE);
3541
3542 /*
3543 * If <BS> and <DEL> are now the same, redefine <DEL>.
3544 */
3545 p = find_termcode((char_u *)"kD");
3546 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3547 do_fixdel(NULL);
3548 }
3549#if 0
3550 } /* to keep cindent happy */
3551#endif
3552}
3553
3554#endif /* VMS */
3555
3556#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
3557/*
3558 * Set mouse clicks on or off.
3559 */
3560 void
3561mch_setmouse(on)
3562 int on;
3563{
3564 static int ison = FALSE;
3565 int xterm_mouse_vers;
3566
3567 if (on == ison) /* return quickly if nothing to do */
3568 return;
3569
3570 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003571
3572# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003573 if (ttym_flags == TTYM_URXVT)
3574 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003575 out_str_nf((char_u *)
3576 (on
3577 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3578 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
3579 ison = on;
3580 }
3581# endif
3582
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003583# ifdef FEAT_MOUSE_SGR
3584 if (ttym_flags == TTYM_SGR)
3585 {
3586 out_str_nf((char_u *)
3587 (on
3588 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3589 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
3590 ison = on;
3591 }
3592# endif
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (xterm_mouse_vers > 0)
3595 {
3596 if (on) /* enable mouse events, use mouse tracking if available */
3597 out_str_nf((char_u *)
3598 (xterm_mouse_vers > 1
3599 ? IF_EB("\033[?1002h", ESC_STR "[?1002h")
3600 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3601 else /* disable mouse events, could probably always send the same */
3602 out_str_nf((char_u *)
3603 (xterm_mouse_vers > 1
3604 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3605 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
3606 ison = on;
3607 }
3608
3609# ifdef FEAT_MOUSE_DEC
3610 else if (ttym_flags == TTYM_DEC)
3611 {
3612 if (on) /* enable mouse events */
3613 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3614 else /* disable mouse events */
3615 out_str_nf((char_u *)"\033['z");
3616 ison = on;
3617 }
3618# endif
3619
3620# ifdef FEAT_MOUSE_GPM
3621 else
3622 {
3623 if (on)
3624 {
3625 if (gpm_open())
3626 ison = TRUE;
3627 }
3628 else
3629 {
3630 gpm_close();
3631 ison = FALSE;
3632 }
3633 }
3634# endif
3635
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003636# ifdef FEAT_SYSMOUSE
3637 else
3638 {
3639 if (on)
3640 {
3641 if (sysmouse_open() == OK)
3642 ison = TRUE;
3643 }
3644 else
3645 {
3646 sysmouse_close();
3647 ison = FALSE;
3648 }
3649 }
3650# endif
3651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652# ifdef FEAT_MOUSE_JSB
3653 else
3654 {
3655 if (on)
3656 {
3657 /* D - Enable Mouse up/down messages
3658 * L - Enable Left Button Reporting
3659 * M - Enable Middle Button Reporting
3660 * R - Enable Right Button Reporting
3661 * K - Enable SHIFT and CTRL key Reporting
3662 * + - Enable Advanced messaging of mouse moves and up/down messages
3663 * Q - Quiet No Ack
3664 * # - Numeric value of mouse pointer required
3665 * 0 = Multiview 2000 cursor, used as standard
3666 * 1 = Windows Arrow
3667 * 2 = Windows I Beam
3668 * 3 = Windows Hour Glass
3669 * 4 = Windows Cross Hair
3670 * 5 = Windows UP Arrow
3671 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003672# ifdef JSBTERM_MOUSE_NONADVANCED
3673 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3675 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003676# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3678 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003679# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 ison = TRUE;
3681 }
3682 else
3683 {
3684 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3685 ESC_STR "[0~ZwQ" ESC_STR "\\"));
3686 ison = FALSE;
3687 }
3688 }
3689# endif
3690# ifdef FEAT_MOUSE_PTERM
3691 else
3692 {
3693 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3694 if (on)
3695 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3696 else
3697 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
3698 ison = on;
3699 }
3700# endif
3701}
3702
3703/*
3704 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3705 */
3706 void
3707check_mouse_termcode()
3708{
3709# ifdef FEAT_MOUSE_XTERM
3710 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003711# ifdef FEAT_MOUSE_URXVT
3712 && use_xterm_mouse() != 3
3713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714# ifdef FEAT_GUI
3715 && !gui.in_use
3716# endif
3717 )
3718 {
3719 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003720 ? IF_EB("\233M", CSI_STR "M")
3721 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (*p_mouse != NUL)
3723 {
3724 /* force mouse off and maybe on to send possibly new mouse
3725 * activation sequence to the xterm, with(out) drag tracing. */
3726 mch_setmouse(FALSE);
3727 setmouse();
3728 }
3729 }
3730 else
3731 del_mouse_termcode(KS_MOUSE);
3732# endif
3733
3734# ifdef FEAT_MOUSE_GPM
3735 if (!use_xterm_mouse()
3736# ifdef FEAT_GUI
3737 && !gui.in_use
3738# endif
3739 )
3740 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3741# endif
3742
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003743# ifdef FEAT_SYSMOUSE
3744 if (!use_xterm_mouse()
3745# ifdef FEAT_GUI
3746 && !gui.in_use
3747# endif
3748 )
3749 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3750# endif
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003753 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 if (!use_xterm_mouse()
3755# ifdef FEAT_GUI
3756 && !gui.in_use
3757# endif
3758 )
3759 set_mouse_termcode(KS_JSBTERM_MOUSE,
3760 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3761 else
3762 del_mouse_termcode(KS_JSBTERM_MOUSE);
3763# endif
3764
3765# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003766 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 * define it in the GUI or when using an xterm. */
3768 if (!use_xterm_mouse()
3769# ifdef FEAT_GUI
3770 && !gui.in_use
3771# endif
3772 )
3773 set_mouse_termcode(KS_NETTERM_MOUSE,
3774 (char_u *)IF_EB("\033}", ESC_STR "}"));
3775 else
3776 del_mouse_termcode(KS_NETTERM_MOUSE);
3777# endif
3778
3779# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003780 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003781 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782# ifdef FEAT_GUI
3783 && !gui.in_use
3784# endif
3785 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003786 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3787 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 else
3789 del_mouse_termcode(KS_DEC_MOUSE);
3790# endif
3791# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003792 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003793 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794# ifdef FEAT_GUI
3795 && !gui.in_use
3796# endif
3797 )
3798 set_mouse_termcode(KS_PTERM_MOUSE,
3799 (char_u *) IF_EB("\033[", ESC_STR "["));
3800 else
3801 del_mouse_termcode(KS_PTERM_MOUSE);
3802# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003803# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003804 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003805 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003806# ifdef FEAT_GUI
3807 && !gui.in_use
3808# endif
3809 )
3810 {
3811 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3812 ? IF_EB("\233", CSI_STR)
3813 : IF_EB("\033[", ESC_STR "[")));
3814
3815 if (*p_mouse != NUL)
3816 {
3817 mch_setmouse(FALSE);
3818 setmouse();
3819 }
3820 }
3821 else
3822 del_mouse_termcode(KS_URXVT_MOUSE);
3823# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003824# ifdef FEAT_MOUSE_SGR
Bram Moolenaar24dc2302014-05-13 20:19:58 +02003825 /* There is no conflict with xterm mouse */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003826 if (use_xterm_mouse() == 4
3827# ifdef FEAT_GUI
3828 && !gui.in_use
3829# endif
3830 )
3831 {
3832 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3833 ? IF_EB("\233<", CSI_STR "<")
3834 : IF_EB("\033[<", ESC_STR "[<")));
3835
3836 if (*p_mouse != NUL)
3837 {
3838 mch_setmouse(FALSE);
3839 setmouse();
3840 }
3841 }
3842 else
3843 del_mouse_termcode(KS_SGR_MOUSE);
3844# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845}
3846#endif
3847
3848/*
3849 * set screen mode, always fails.
3850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 int
3852mch_screenmode(arg)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003853 char_u *arg UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
3855 EMSG(_(e_screenmode));
3856 return FAIL;
3857}
3858
3859#ifndef VMS
3860
3861/*
3862 * Try to get the current window size:
3863 * 1. with an ioctl(), most accurate method
3864 * 2. from the environment variables LINES and COLUMNS
3865 * 3. from the termcap
3866 * 4. keep using the old values
3867 * Return OK when size could be determined, FAIL otherwise.
3868 */
3869 int
3870mch_get_shellsize()
3871{
3872 long rows = 0;
3873 long columns = 0;
3874 char_u *p;
3875
3876 /*
3877 * For OS/2 use _scrsize().
3878 */
3879# ifdef __EMX__
3880 {
3881 int s[2];
3882
3883 _scrsize(s);
3884 columns = s[0];
3885 rows = s[1];
3886 }
3887# endif
3888
3889 /*
3890 * 1. try using an ioctl. It is the most accurate method.
3891 *
3892 * Try using TIOCGWINSZ first, some systems that have it also define
3893 * TIOCGSIZE but don't have a struct ttysize.
3894 */
3895# ifdef TIOCGWINSZ
3896 {
3897 struct winsize ws;
3898 int fd = 1;
3899
3900 /* When stdout is not a tty, use stdin for the ioctl(). */
3901 if (!isatty(fd) && isatty(read_cmd_fd))
3902 fd = read_cmd_fd;
3903 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3904 {
3905 columns = ws.ws_col;
3906 rows = ws.ws_row;
3907 }
3908 }
3909# else /* TIOCGWINSZ */
3910# ifdef TIOCGSIZE
3911 {
3912 struct ttysize ts;
3913 int fd = 1;
3914
3915 /* When stdout is not a tty, use stdin for the ioctl(). */
3916 if (!isatty(fd) && isatty(read_cmd_fd))
3917 fd = read_cmd_fd;
3918 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
3919 {
3920 columns = ts.ts_cols;
3921 rows = ts.ts_lines;
3922 }
3923 }
3924# endif /* TIOCGSIZE */
3925# endif /* TIOCGWINSZ */
3926
3927 /*
3928 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003929 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
3930 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003932 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 {
3934 if ((p = (char_u *)getenv("LINES")))
3935 rows = atoi((char *)p);
3936 if ((p = (char_u *)getenv("COLUMNS")))
3937 columns = atoi((char *)p);
3938 }
3939
3940#ifdef HAVE_TGETENT
3941 /*
3942 * 3. try reading "co" and "li" entries from termcap
3943 */
3944 if (columns == 0 || rows == 0)
3945 getlinecol(&columns, &rows);
3946#endif
3947
3948 /*
3949 * 4. If everything fails, use the old values
3950 */
3951 if (columns <= 0 || rows <= 0)
3952 return FAIL;
3953
3954 Rows = rows;
3955 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02003956 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 return OK;
3958}
3959
3960/*
3961 * Try to set the window size to Rows and Columns.
3962 */
3963 void
3964mch_set_shellsize()
3965{
3966 if (*T_CWS)
3967 {
3968 /*
3969 * NOTE: if you get an error here that term_set_winsize() is
3970 * undefined, check the output of configure. It could probably not
3971 * find a ncurses, termcap or termlib library.
3972 */
3973 term_set_winsize((int)Rows, (int)Columns);
3974 out_flush();
3975 screen_start(); /* don't know where cursor is now */
3976 }
3977}
3978
3979#endif /* VMS */
3980
3981/*
3982 * Rows and/or Columns has changed.
3983 */
3984 void
3985mch_new_shellsize()
3986{
3987 /* Nothing to do. */
3988}
3989
Bram Moolenaar205b8862011-09-07 15:04:31 +02003990/*
3991 * Wait for process "child" to end.
3992 * Return "child" if it exited properly, <= 0 on error.
3993 */
3994 static pid_t
3995wait4pid(child, status)
3996 pid_t child;
3997 waitstatus *status;
3998{
3999 pid_t wait_pid = 0;
4000
4001 while (wait_pid != child)
4002 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004003 /* When compiled with Python threads are probably used, in which case
4004 * wait() sometimes hangs for no obvious reason. Use waitpid()
4005 * instead and loop (like the GUI). Also needed for other interfaces,
4006 * they might call system(). */
4007# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004008 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004009# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004010 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004011# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004012 if (wait_pid == 0)
4013 {
Bram Moolenaar75676462013-01-30 14:55:42 +01004014 /* Wait for 10 msec before trying again. */
Bram Moolenaar205b8862011-09-07 15:04:31 +02004015 mch_delay(10L, TRUE);
4016 continue;
4017 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004018 if (wait_pid <= 0
4019# ifdef ECHILD
4020 && errno == ECHILD
4021# endif
4022 )
4023 break;
4024 }
4025 return wait_pid;
4026}
4027
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 int
4029mch_call_shell(cmd, options)
4030 char_u *cmd;
4031 int options; /* SHELL_*, see vim.h */
4032{
4033#ifdef VMS
4034 char *ifn = NULL;
4035 char *ofn = NULL;
4036#endif
4037 int tmode = cur_tmode;
4038#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
4039 int x;
4040# ifndef __EMX__
4041 char_u *newcmd; /* only needed for unix */
4042# else
4043 /*
4044 * Set the preferred shell in the EMXSHELL environment variable (but
4045 * only if it is different from what is already in the environment).
4046 * Emx then takes care of whether to use "/c" or "-c" in an
4047 * intelligent way. Simply pass the whole thing to emx's system() call.
4048 * Emx also starts an interactive shell if system() is passed an empty
4049 * string.
4050 */
4051 char_u *p, *old;
4052
4053 if (((old = (char_u *)getenv("EMXSHELL")) == NULL) || STRCMP(old, p_sh))
4054 {
4055 /* should check HAVE_SETENV, but I know we don't have it. */
4056 p = alloc(10 + strlen(p_sh));
4057 if (p)
4058 {
4059 sprintf((char *)p, "EMXSHELL=%s", p_sh);
4060 putenv((char *)p); /* don't free the pointer! */
4061 }
4062 }
4063# endif
4064
4065 out_flush();
4066
4067 if (options & SHELL_COOKED)
4068 settmode(TMODE_COOK); /* set to normal mode */
4069
Bram Moolenaar62b42182010-09-21 22:09:37 +02004070# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004071 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004072 loose_clipboard();
4073# endif
4074
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075# ifdef __EMX__
4076 if (cmd == NULL)
4077 x = system(""); /* this starts an interactive shell in emx */
4078 else
4079 x = system((char *)cmd);
4080 /* system() returns -1 when error occurs in starting shell */
4081 if (x == -1 && !emsg_silent)
4082 {
4083 MSG_PUTS(_("\nCannot execute shell "));
4084 msg_outtrans(p_sh);
4085 msg_putchar('\n');
4086 }
4087# else /* not __EMX__ */
4088 if (cmd == NULL)
4089 x = system((char *)p_sh);
4090 else
4091 {
4092# ifdef VMS
4093 if (ofn = strchr((char *)cmd, '>'))
4094 *ofn++ = '\0';
4095 if (ifn = strchr((char *)cmd, '<'))
4096 {
4097 char *p;
4098
4099 *ifn++ = '\0';
4100 p = strchr(ifn,' '); /* chop off any trailing spaces */
4101 if (p)
4102 *p = '\0';
4103 }
4104 if (ofn)
4105 x = vms_sys((char *)cmd, ofn, ifn);
4106 else
4107 x = system((char *)cmd);
4108# else
4109 newcmd = lalloc(STRLEN(p_sh)
4110 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4111 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4112 if (newcmd == NULL)
4113 x = 0;
4114 else
4115 {
4116 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4117 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4118 (char *)p_shcf,
4119 (char *)cmd);
4120 x = system((char *)newcmd);
4121 vim_free(newcmd);
4122 }
4123# endif
4124 }
4125# ifdef VMS
4126 x = vms_sys_status(x);
4127# endif
4128 if (emsg_silent)
4129 ;
4130 else if (x == 127)
4131 MSG_PUTS(_("\nCannot execute shell sh\n"));
4132# endif /* __EMX__ */
4133 else if (x && !(options & SHELL_SILENT))
4134 {
4135 MSG_PUTS(_("\nshell returned "));
4136 msg_outnum((long)x);
4137 msg_putchar('\n');
4138 }
4139
4140 if (tmode == TMODE_RAW)
4141 settmode(TMODE_RAW); /* set to raw mode */
4142# ifdef FEAT_TITLE
4143 resettitle();
4144# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004145# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4146 restore_clipboard();
4147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 return x;
4149
4150#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4151
Bram Moolenaardf177f62005-02-22 08:39:57 +00004152# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4153 127, some shells use that already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
4155 char_u *newcmd = NULL;
4156 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004157 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 pid_t wait_pid = 0;
4159# ifdef HAVE_UNION_WAIT
4160 union wait status;
4161# else
4162 int status = -1;
4163# endif
4164 int retval = -1;
4165 char **argv = NULL;
4166 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004167 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 int i;
4169 char_u *p;
4170 int inquote;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004172# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 int pty_slave_fd = -1;
4174 char *tty_name;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004175# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004176 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 int fd_fromshell[2];
4178 int pipe_error = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004179# ifdef HAVE_SETENV
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 char envbuf[50];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004181# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 static char envbuf_Rows[20];
4183 static char envbuf_Columns[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004185 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
Bram Moolenaar62b42182010-09-21 22:09:37 +02004187 newcmd = vim_strsave(p_sh);
4188 if (newcmd == NULL) /* out of memory */
4189 goto error;
4190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 out_flush();
4192 if (options & SHELL_COOKED)
4193 settmode(TMODE_COOK); /* set to normal mode */
4194
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004195 /*
4196 * Do this loop twice:
4197 * 1: find number of arguments
4198 * 2: separate them and build argv[]
4199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 for (i = 0; i < 2; ++i)
4201 {
4202 p = newcmd;
4203 inquote = FALSE;
4204 argc = 0;
4205 for (;;)
4206 {
4207 if (i == 1)
4208 argv[argc] = (char *)p;
4209 ++argc;
4210 while (*p && (inquote || (*p != ' ' && *p != TAB)))
4211 {
4212 if (*p == '"')
4213 inquote = !inquote;
4214 ++p;
4215 }
4216 if (*p == NUL)
4217 break;
4218 if (i == 1)
4219 *p++ = NUL;
4220 p = skipwhite(p);
4221 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004222 if (argv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004224 /*
4225 * Account for possible multiple args in p_shcf.
4226 */
4227 p = p_shcf;
4228 for (;;)
4229 {
4230 p = skiptowhite(p);
4231 if (*p == NUL)
4232 break;
4233 ++argc;
4234 p = skipwhite(p);
4235 }
4236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 argv = (char **)alloc((unsigned)((argc + 4) * sizeof(char *)));
4238 if (argv == NULL) /* out of memory */
4239 goto error;
4240 }
4241 }
4242 if (cmd != NULL)
4243 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004244 char_u *s;
4245
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 if (extra_shell_arg != NULL)
4247 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004248
4249 /* Break 'shellcmdflag' into white separated parts. This doesn't
4250 * handle quoted strings, they are very unlikely to appear. */
4251 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4252 if (p_shcf_copy == NULL) /* out of memory */
4253 goto error;
4254 s = p_shcf_copy;
4255 p = p_shcf;
4256 while (*p != NUL)
4257 {
4258 argv[argc++] = (char *)s;
4259 while (*p && *p != ' ' && *p != TAB)
4260 *s++ = *p++;
4261 *s++ = NUL;
4262 p = skipwhite(p);
4263 }
4264
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 argv[argc++] = (char *)cmd;
4266 }
4267 argv[argc] = NULL;
4268
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004270 * For the GUI, when writing the output into the buffer and when reading
4271 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4272 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004274 if ((options & (SHELL_READ|SHELL_WRITE))
4275# ifdef FEAT_GUI
4276 || (gui.in_use && show_shell_mess)
4277# endif
4278 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004280# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 /*
4282 * Try to open a master pty.
4283 * If this works, open the slave pty.
4284 * If the slave can't be opened, close the master pty.
4285 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004286 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
4288 pty_master_fd = OpenPTY(&tty_name); /* open pty */
Bram Moolenaare70172e2011-08-04 19:36:52 +02004289 if (pty_master_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 {
Bram Moolenaare70172e2011-08-04 19:36:52 +02004291 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4292 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4293 * adding O_NOCTTY always works when defined. */
4294#ifdef O_NOCTTY
4295 pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4296#else
4297 pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4298#endif
4299 if (pty_slave_fd < 0)
4300 {
4301 close(pty_master_fd);
4302 pty_master_fd = -1;
4303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 }
4306 /*
4307 * If not opening a pty or it didn't work, try using pipes.
4308 */
4309 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
4312 pipe_error = (pipe(fd_toshell) < 0);
4313 if (!pipe_error) /* pipe create OK */
4314 {
4315 pipe_error = (pipe(fd_fromshell) < 0);
4316 if (pipe_error) /* pipe create failed */
4317 {
4318 close(fd_toshell[0]);
4319 close(fd_toshell[1]);
4320 }
4321 }
4322 if (pipe_error)
4323 {
4324 MSG_PUTS(_("\nCannot create pipes\n"));
4325 out_flush();
4326 }
4327 }
4328 }
4329
4330 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 {
4332# ifdef __BEOS__
4333 beos_cleanup_read_thread();
4334# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004335
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if ((pid = fork()) == -1) /* maybe we should use vfork() */
4337 {
4338 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004339 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004341 || (gui.in_use && show_shell_mess)
4342# endif
4343 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004345# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if (pty_master_fd >= 0) /* close the pseudo tty */
4347 {
4348 close(pty_master_fd);
4349 close(pty_slave_fd);
4350 }
4351 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004352# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
4354 close(fd_toshell[0]);
4355 close(fd_toshell[1]);
4356 close(fd_fromshell[0]);
4357 close(fd_fromshell[1]);
4358 }
4359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 else if (pid == 0) /* child */
4362 {
4363 reset_signals(); /* handle signals normally */
4364
4365 if (!show_shell_mess || (options & SHELL_EXPAND))
4366 {
4367 int fd;
4368
4369 /*
4370 * Don't want to show any message from the shell. Can't just
4371 * close stdout and stderr though, because some systems will
4372 * break if you try to write to them after that, so we must
4373 * use dup() to replace them with something else -- webb
4374 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4375 * waiting for input.
4376 */
4377 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4378 fclose(stdin);
4379 fclose(stdout);
4380 fclose(stderr);
4381
4382 /*
4383 * If any of these open()'s and dup()'s fail, we just continue
4384 * anyway. It's not fatal, and on most systems it will make
4385 * no difference at all. On a few it will cause the execvp()
4386 * to exit with a non-zero status even when the completion
4387 * could be done, which is nothing too serious. If the open()
4388 * or dup() failed we'd just do the same thing ourselves
4389 * anyway -- webb
4390 */
4391 if (fd >= 0)
4392 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004393 ignored = dup(fd); /* To replace stdin (fd 0) */
4394 ignored = dup(fd); /* To replace stdout (fd 1) */
4395 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
4397 /* Don't need this now that we've duplicated it */
4398 close(fd);
4399 }
4400 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004401 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004403 || gui.in_use
4404# endif
4405 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {
4407
Bram Moolenaardf177f62005-02-22 08:39:57 +00004408# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004409 /* Create our own process group, so that the child and all its
4410 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004411 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004412 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004413 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004414 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004415# if defined(SIGHUP)
4416 /* When doing "!xterm&" and 'shell' is bash: the shell
4417 * will exit and send SIGHUP to all processes in its
4418 * group, killing the just started process. Ignore SIGHUP
4419 * to avoid that. (suggested by Simon Schubert)
4420 */
4421 signal(SIGHUP, SIG_IGN);
4422# endif
4423 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004424# endif
4425# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004426 if (pty_slave_fd >= 0)
4427 {
4428 /* push stream discipline modules */
4429 if (options & SHELL_COOKED)
4430 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004432 /* Try to become controlling tty (probably doesn't work,
4433 * unless run by root) */
4434 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004436 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 /* Simulate to have a dumb terminal (for now) */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004439# ifdef HAVE_SETENV
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 setenv("TERM", "dumb", 1);
4441 sprintf((char *)envbuf, "%ld", Rows);
4442 setenv("ROWS", (char *)envbuf, 1);
4443 sprintf((char *)envbuf, "%ld", Rows);
4444 setenv("LINES", (char *)envbuf, 1);
4445 sprintf((char *)envbuf, "%ld", Columns);
4446 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004447# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 /*
4449 * Putenv does not copy the string, it has to remain valid.
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004450 * Use a static array to avoid losing allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 */
4452 putenv("TERM=dumb");
4453 sprintf(envbuf_Rows, "ROWS=%ld", Rows);
4454 putenv(envbuf_Rows);
4455 sprintf(envbuf_Rows, "LINES=%ld", Rows);
4456 putenv(envbuf_Rows);
4457 sprintf(envbuf_Columns, "COLUMNS=%ld", Columns);
4458 putenv(envbuf_Columns);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004459# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaara5792f52005-11-23 21:25:05 +00004461 /*
4462 * stderr is only redirected when using the GUI, so that a
4463 * program like gpg can still access the terminal to get a
4464 * passphrase using stderr.
4465 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004466# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 if (pty_master_fd >= 0)
4468 {
4469 close(pty_master_fd); /* close master side of pty */
4470
4471 /* set up stdin/stdout/stderr for the child */
4472 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004473 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004475 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004476 if (gui.in_use)
4477 {
4478 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004479 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
4482 close(pty_slave_fd); /* has been dupped, close it now */
4483 }
4484 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
4487 /* set up stdin for the child */
4488 close(fd_toshell[1]);
4489 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004490 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 close(fd_toshell[0]);
4492
4493 /* set up stdout for the child */
4494 close(fd_fromshell[0]);
4495 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004496 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 close(fd_fromshell[1]);
4498
Bram Moolenaara5792f52005-11-23 21:25:05 +00004499# ifdef FEAT_GUI
4500 if (gui.in_use)
4501 {
4502 /* set up stderr for the child */
4503 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004504 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004505 }
4506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004509
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 /*
4511 * There is no type cast for the argv, because the type may be
4512 * different on different machines. This may cause a warning
4513 * message with strict compilers, don't worry about it.
4514 * Call _exit() instead of exit() to avoid closing the connection
4515 * to the X server (esp. with GTK, which uses atexit()).
4516 */
4517 execvp(argv[0], argv);
4518 _exit(EXEC_FAILED); /* exec failed, return failure code */
4519 }
4520 else /* parent */
4521 {
4522 /*
4523 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004524 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 */
4526 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004527 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 /*
4530 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004531 * This is also used to pipe stdin/stdout to/from the external
4532 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004534 if ((options & (SHELL_READ|SHELL_WRITE))
4535# ifdef FEAT_GUI
4536 || (gui.in_use && show_shell_mess)
4537# endif
4538 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004540# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004542# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4546 int ta_len = 0; /* valid bytes in ta_buf[] */
4547 int len;
4548 int p_more_save;
4549 int old_State;
4550 int c;
4551 int toshell_fd;
4552 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004553 garray_T ga;
4554 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004555# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4556 struct timeval start_tv;
4557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaardf177f62005-02-22 08:39:57 +00004559# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (pty_master_fd >= 0)
4561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 fromshell_fd = pty_master_fd;
4563 toshell_fd = dup(pty_master_fd);
4564 }
4565 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 {
4568 close(fd_toshell[0]);
4569 close(fd_fromshell[1]);
4570 toshell_fd = fd_toshell[1];
4571 fromshell_fd = fd_fromshell[0];
4572 }
4573
4574 /*
4575 * Write to the child if there are typed characters.
4576 * Read from the child if there are characters available.
4577 * Repeat the reading a few times if more characters are
4578 * available. Need to check for typed keys now and then, but
4579 * not too often (delays when no chars are available).
4580 * This loop is quit if no characters can be read from the pty
4581 * (WaitForChar detected special condition), or there are no
4582 * characters available and the child has exited.
4583 * Only check if the child has exited when there is no more
4584 * output. The child may exit before all the output has
4585 * been printed.
4586 *
4587 * Currently this busy loops!
4588 * This can probably dead-lock when the write blocks!
4589 */
4590 p_more_save = p_more;
4591 p_more = FALSE;
4592 old_State = State;
4593 State = EXTERNCMD; /* don't redraw at window resize */
4594
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004595 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004596 {
4597 /* Fork a process that will write the lines to the
4598 * external program. */
4599 if ((wpid = fork()) == -1)
4600 {
4601 MSG_PUTS(_("\nCannot fork\n"));
4602 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004603 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004604 {
4605 linenr_T lnum = curbuf->b_op_start.lnum;
4606 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004607 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004608 size_t l;
4609
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004610 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004611 for (;;)
4612 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004613 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004614 if (l == 0)
4615 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004616 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004617 /* NL -> NUL translation */
4618 len = write(toshell_fd, "", (size_t)1);
4619 else
4620 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004621 char_u *s = vim_strchr(lp + written, NL);
4622
Bram Moolenaar89d40322006-08-29 15:30:07 +00004623 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004624 s == NULL ? l
4625 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004626 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004627 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004628 {
4629 /* Finished a line, add a NL, unless this line
4630 * should not have one. */
4631 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004632 || (!curbuf->b_p_bin
4633 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004634 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004635 && (lnum !=
4636 curbuf->b_ml.ml_line_count
4637 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004638 ignored = write(toshell_fd, "\n",
4639 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004640 ++lnum;
4641 if (lnum > curbuf->b_op_end.lnum)
4642 {
4643 /* finished all the lines, close pipe */
4644 close(toshell_fd);
4645 toshell_fd = -1;
4646 break;
4647 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004648 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004649 written = 0;
4650 }
4651 else if (len > 0)
4652 written += len;
4653 }
4654 _exit(0);
4655 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004656 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657 {
4658 close(toshell_fd);
4659 toshell_fd = -1;
4660 }
4661 }
4662
4663 if (options & SHELL_READ)
4664 ga_init2(&ga, 1, BUFLEN);
4665
4666 noread_cnt = 0;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004667# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4668 gettimeofday(&start_tv, NULL);
4669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 for (;;)
4671 {
4672 /*
4673 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004674 * if there are any.
4675 * Don't do this if we are expanding wild cards (would eat
4676 * typeahead).
4677 * Don't do this when filtering and terminal is in cooked
4678 * mode, the shell command will handle the I/O. Avoids
4679 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004680 * Don't get characters when the child has already
4681 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004682 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004683 * while (noread_cnt > 4), avoids that ":r !ls" eats
4684 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 */
4686 len = 0;
4687 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004688 && ((options &
4689 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4690 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004691# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004692 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004693# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004694 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004695 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004696 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004698 if (ta_len == 0)
4699 {
4700 /* Get extra characters when we don't have any.
4701 * Reset the counter and timer. */
4702 noread_cnt = 0;
4703# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4704 gettimeofday(&start_tv, NULL);
4705# endif
4706 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4707 }
4708 if (ta_len > 0 || len > 0)
4709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 /*
4711 * For pipes:
4712 * Check for CTRL-C: send interrupt signal to child.
4713 * Check for CTRL-D: EOF, close pipe to child.
4714 */
4715 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4716 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004717# ifdef SIGINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 /*
4719 * Send SIGINT to the child's group or all
4720 * processes in our group.
4721 */
4722 if (ta_buf[ta_len] == Ctrl_C
4723 || ta_buf[ta_len] == intr_char)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004724 {
4725# ifdef HAVE_SETSID
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 kill(-pid, SIGINT);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004727# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 kill(0, SIGINT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004730 if (wpid > 0)
4731 kill(wpid, SIGINT);
4732 }
4733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (pty_master_fd < 0 && toshell_fd >= 0
4735 && ta_buf[ta_len] == Ctrl_D)
4736 {
4737 close(toshell_fd);
4738 toshell_fd = -1;
4739 }
4740 }
4741
4742 /* replace K_BS by <BS> and K_DEL by <DEL> */
4743 for (i = ta_len; i < ta_len + len; ++i)
4744 {
4745 if (ta_buf[i] == CSI && len - i > 2)
4746 {
4747 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4748 if (c == K_DEL || c == K_KDEL || c == K_BS)
4749 {
4750 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4751 (size_t)(len - i - 2));
4752 if (c == K_DEL || c == K_KDEL)
4753 ta_buf[i] = DEL;
4754 else
4755 ta_buf[i] = Ctrl_H;
4756 len -= 2;
4757 }
4758 }
4759 else if (ta_buf[i] == '\r')
4760 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004761# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004763 i += (*mb_ptr2len_len)(ta_buf + i,
4764 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767
4768 /*
4769 * For pipes: echo the typed characters.
4770 * For a pty this does not seem to work.
4771 */
4772 if (pty_master_fd < 0)
4773 {
4774 for (i = ta_len; i < ta_len + len; ++i)
4775 {
4776 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4777 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004778# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else if (has_mbyte)
4780 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004781 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
4783 msg_outtrans_len(ta_buf + i, l);
4784 i += l - 1;
4785 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 else
4788 msg_outtrans_len(ta_buf + i, 1);
4789 }
4790 windgoto(msg_row, msg_col);
4791 out_flush();
4792 }
4793
4794 ta_len += len;
4795
4796 /*
4797 * Write the characters to the child, unless EOF has
4798 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004799 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004800 * When writing buffer lines, drop the typed
4801 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004803 if (options & SHELL_WRITE)
4804 ta_len = 0;
4805 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 {
4807 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
4808 if (len > 0)
4809 {
4810 ta_len -= len;
4811 mch_memmove(ta_buf, ta_buf + len, ta_len);
4812 }
4813 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816
Bram Moolenaardf177f62005-02-22 08:39:57 +00004817 if (got_int)
4818 {
4819 /* CTRL-C sends a signal to the child, we ignore it
4820 * ourselves */
4821# ifdef HAVE_SETSID
4822 kill(-pid, SIGINT);
4823# else
4824 kill(0, SIGINT);
4825# endif
4826 if (wpid > 0)
4827 kill(wpid, SIGINT);
4828 got_int = FALSE;
4829 }
4830
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 /*
4832 * Check if the child has any characters to be printed.
4833 * Read them and write them to our window. Repeat this as
4834 * long as there is something to do, avoid the 10ms wait
4835 * for mch_inchar(), or sending typeahead characters to
4836 * the external process.
4837 * TODO: This should handle escape sequences, compatible
4838 * to some terminal (vt52?).
4839 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004840 ++noread_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 while (RealWaitForChar(fromshell_fd, 10L, NULL))
4842 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004843 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00004844# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004846# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00004848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 );
4850 if (len <= 0) /* end of file or error */
4851 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004852
4853 noread_cnt = 0;
4854 if (options & SHELL_READ)
4855 {
4856 /* Do NUL -> NL translation, append NL separated
4857 * lines to the current buffer. */
4858 for (i = 0; i < len; ++i)
4859 {
4860 if (buffer[i] == NL)
4861 append_ga_line(&ga);
4862 else if (buffer[i] == NUL)
4863 ga_append(&ga, NL);
4864 else
4865 ga_append(&ga, buffer[i]);
4866 }
4867 }
4868# ifdef FEAT_MBYTE
4869 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
4871 int l;
4872
Bram Moolenaardf177f62005-02-22 08:39:57 +00004873 len += buffer_off;
4874 buffer[len] = NUL;
4875
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 /* Check if the last character in buffer[] is
4877 * incomplete, keep these bytes for the next
4878 * round. */
4879 for (p = buffer; p < buffer + len; p += l)
4880 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004881 l = mb_cptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (l == 0)
4883 l = 1; /* NUL byte? */
4884 else if (MB_BYTE2LEN(*p) != l)
4885 break;
4886 }
4887 if (p == buffer) /* no complete character */
4888 {
4889 /* avoid getting stuck at an illegal byte */
4890 if (len >= 12)
4891 ++p;
4892 else
4893 {
4894 buffer_off = len;
4895 continue;
4896 }
4897 }
4898 c = *p;
4899 *p = NUL;
4900 msg_puts(buffer);
4901 if (p < buffer + len)
4902 {
4903 *p = c;
4904 buffer_off = (buffer + len) - p;
4905 mch_memmove(buffer, p, buffer_off);
4906 continue;
4907 }
4908 buffer_off = 0;
4909 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004910# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
4913 buffer[len] = NUL;
4914 msg_puts(buffer);
4915 }
4916
4917 windgoto(msg_row, msg_col);
4918 cursor_on();
4919 out_flush();
4920 if (got_int)
4921 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004922
4923# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4924 {
4925 struct timeval now_tv;
4926 long msec;
4927
4928 /* Avoid that we keep looping here without
4929 * checking for a CTRL-C for a long time. Don't
4930 * break out too often to avoid losing typeahead. */
4931 gettimeofday(&now_tv, NULL);
4932 msec = (now_tv.tv_sec - start_tv.tv_sec) * 1000L
4933 + (now_tv.tv_usec - start_tv.tv_usec) / 1000L;
4934 if (msec > 2000)
4935 {
4936 noread_cnt = 5;
4937 break;
4938 }
4939 }
4940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004943 /* If we already detected the child has finished break the
4944 * loop now. */
4945 if (wait_pid == pid)
4946 break;
4947
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 /*
4949 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004950 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004952# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004953 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004954# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4958 || (wait_pid == pid && WIFEXITED(status)))
4959 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004960 /* Don't break the loop yet, try reading more
4961 * characters from "fromshell_fd" first. When using
4962 * pipes there might still be something to read and
4963 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004966 else
4967 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004968
Bram Moolenaar95a51352013-03-21 22:53:50 +01004969# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004970 /* Handle any X events, e.g. serving the clipboard. */
4971 clip_update();
4972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974finished:
4975 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004976 if (options & SHELL_READ)
4977 {
4978 if (ga.ga_len > 0)
4979 {
4980 append_ga_line(&ga);
4981 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004982 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004983 }
4984 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004985 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004986 ga_clear(&ga);
4987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 /*
4990 * Give all typeahead that wasn't used back to ui_inchar().
4991 */
4992 if (ta_len)
4993 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 State = old_State;
4995 if (toshell_fd >= 0)
4996 close(toshell_fd);
4997 close(fromshell_fd);
4998 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01004999# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005000 else
5001 {
5002 /*
5003 * Similar to the loop above, but only handle X events, no
5004 * I/O.
5005 */
5006 for (;;)
5007 {
5008 if (got_int)
5009 {
5010 /* CTRL-C sends a signal to the child, we ignore it
5011 * ourselves */
5012# ifdef HAVE_SETSID
5013 kill(-pid, SIGINT);
5014# else
5015 kill(0, SIGINT);
5016# endif
5017 got_int = FALSE;
5018 }
5019# ifdef __NeXT__
5020 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5021# else
5022 wait_pid = waitpid(pid, &status, WNOHANG);
5023# endif
5024 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5025 || (wait_pid == pid && WIFEXITED(status)))
5026 {
5027 wait_pid = pid;
5028 break;
5029 }
5030
5031 /* Handle any X events, e.g. serving the clipboard. */
5032 clip_update();
5033
5034 mch_delay(10L, TRUE);
5035 }
5036 }
5037# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
5039 /*
5040 * Wait until our child has exited.
5041 * Ignore wait() returning pids of other children and returning
5042 * because of some signal like SIGWINCH.
5043 * Don't wait if wait_pid was already set above, indicating the
5044 * child already exited.
5045 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005046 if (wait_pid != pid)
5047 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
Bram Moolenaar624891f2010-10-13 16:22:09 +02005049# ifdef FEAT_GUI
5050 /* Close slave side of pty. Only do this after the child has
5051 * exited, otherwise the child may hang when it tries to write on
5052 * the pty. */
5053 if (pty_master_fd >= 0)
5054 close(pty_slave_fd);
5055# endif
5056
Bram Moolenaardf177f62005-02-22 08:39:57 +00005057 /* Make sure the child that writes to the external program is
5058 * dead. */
5059 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005060 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005061 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005062 wait4pid(wpid, NULL);
5063 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005064
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 /*
5066 * Set to raw mode right now, otherwise a CTRL-C after
5067 * catch_signals() will kill Vim.
5068 */
5069 if (tmode == TMODE_RAW)
5070 settmode(TMODE_RAW);
5071 did_settmode = TRUE;
5072 set_signals();
5073
5074 if (WIFEXITED(status))
5075 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005076 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005078 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 if (retval == EXEC_FAILED)
5081 {
5082 MSG_PUTS(_("\nCannot execute shell "));
5083 msg_outtrans(p_sh);
5084 msg_putchar('\n');
5085 }
5086 else if (!(options & SHELL_SILENT))
5087 {
5088 MSG_PUTS(_("\nshell returned "));
5089 msg_outnum((long)retval);
5090 msg_putchar('\n');
5091 }
5092 }
5093 }
5094 else
5095 MSG_PUTS(_("\nCommand terminated\n"));
5096 }
5097 }
5098 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005099 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
5101error:
5102 if (!did_settmode)
5103 if (tmode == TMODE_RAW)
5104 settmode(TMODE_RAW); /* set to raw mode */
5105# ifdef FEAT_TITLE
5106 resettitle();
5107# endif
5108 vim_free(newcmd);
5109
5110 return retval;
5111
5112#endif /* USE_SYSTEM */
5113}
5114
5115/*
5116 * Check for CTRL-C typed by reading all available characters.
5117 * In cooked mode we should get SIGINT, no need to check.
5118 */
5119 void
5120mch_breakcheck()
5121{
5122 if (curr_tmode == TMODE_RAW && RealWaitForChar(read_cmd_fd, 0L, NULL))
5123 fill_input_buf(FALSE);
5124}
5125
5126/*
5127 * Wait "msec" msec until a character is available from the keyboard or from
5128 * inbuf[]. msec == -1 will block forever.
5129 * When a GUI is being used, this will never get called -- webb
5130 */
5131 static int
5132WaitForChar(msec)
5133 long msec;
5134{
5135#ifdef FEAT_MOUSE_GPM
5136 int gpm_process_wanted;
5137#endif
5138#ifdef FEAT_XCLIPBOARD
5139 int rest;
5140#endif
5141 int avail;
5142
5143 if (input_available()) /* something in inbuf[] */
5144 return 1;
5145
5146#if defined(FEAT_MOUSE_DEC)
5147 /* May need to query the mouse position. */
5148 if (WantQueryMouse)
5149 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005150 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5152 }
5153#endif
5154
5155 /*
5156 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5157 * events. This is a bit complicated, because they might both be defined.
5158 */
5159#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5160# ifdef FEAT_XCLIPBOARD
5161 rest = 0;
5162 if (do_xterm_trace())
5163 rest = msec;
5164# endif
5165 do
5166 {
5167# ifdef FEAT_XCLIPBOARD
5168 if (rest != 0)
5169 {
5170 msec = XT_TRACE_DELAY;
5171 if (rest >= 0 && rest < XT_TRACE_DELAY)
5172 msec = rest;
5173 if (rest >= 0)
5174 rest -= msec;
5175 }
5176# endif
5177# ifdef FEAT_MOUSE_GPM
5178 gpm_process_wanted = 0;
5179 avail = RealWaitForChar(read_cmd_fd, msec, &gpm_process_wanted);
5180# else
5181 avail = RealWaitForChar(read_cmd_fd, msec, NULL);
5182# endif
5183 if (!avail)
5184 {
5185 if (input_available())
5186 return 1;
5187# ifdef FEAT_XCLIPBOARD
5188 if (rest == 0 || !do_xterm_trace())
5189# endif
5190 break;
5191 }
5192 }
5193 while (FALSE
5194# ifdef FEAT_MOUSE_GPM
5195 || (gpm_process_wanted && mch_gpm_process() == 0)
5196# endif
5197# ifdef FEAT_XCLIPBOARD
5198 || (!avail && rest != 0)
5199# endif
5200 );
5201
5202#else
5203 avail = RealWaitForChar(read_cmd_fd, msec, NULL);
5204#endif
5205 return avail;
5206}
5207
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005208#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209/*
5210 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005211 * "msec" == 0 will check for characters once.
5212 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 * When a GUI is being used, this will not be used for input -- webb
5214 * Returns also, when a request from Sniff is waiting -- toni.
5215 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005216 * Or when a clientserver message is on the queue.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218#if defined(__BEOS__)
5219 int
5220#else
5221 static int
5222#endif
5223RealWaitForChar(fd, msec, check_for_gpm)
5224 int fd;
5225 long msec;
Bram Moolenaar78a15312009-05-15 19:33:18 +00005226 int *check_for_gpm UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
5228 int ret;
Bram Moolenaar67c53842010-05-22 18:28:27 +02005229#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005230 int nb_fd = netbeans_filedesc();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005231#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005232#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 static int busy = FALSE;
5234
5235 /* May retry getting characters after an event was handled. */
5236# define MAY_LOOP
5237
5238# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
5239 /* Remember at what time we started, so that we know how much longer we
5240 * should wait after being interrupted. */
5241# define USE_START_TV
5242 struct timeval start_tv;
5243
5244 if (msec > 0 && (
5245# ifdef FEAT_XCLIPBOARD
5246 xterm_Shell != (Widget)0
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005247# if defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 ||
5249# endif
5250# endif
5251# ifdef USE_XSMP
5252 xsmp_icefd != -1
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005253# ifdef FEAT_MZSCHEME
5254 ||
5255# endif
5256# endif
5257# ifdef FEAT_MZSCHEME
5258 (mzthreads_allowed() && p_mzq > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259# endif
5260 ))
5261 gettimeofday(&start_tv, NULL);
5262# endif
5263
5264 /* Handle being called recursively. This may happen for the session
5265 * manager stuff, it may save the file, which does a breakcheck. */
5266 if (busy)
5267 return 0;
5268#endif
5269
5270#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005271 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272#endif
5273 {
5274#ifdef MAY_LOOP
5275 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005276# ifdef FEAT_MZSCHEME
5277 int mzquantum_used = FALSE;
5278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#endif
5280#ifndef HAVE_SELECT
Bram Moolenaar67c53842010-05-22 18:28:27 +02005281 struct pollfd fds[6];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 int nfd;
5283# ifdef FEAT_XCLIPBOARD
5284 int xterm_idx = -1;
5285# endif
5286# ifdef FEAT_MOUSE_GPM
5287 int gpm_idx = -1;
5288# endif
5289# ifdef USE_XSMP
5290 int xsmp_idx = -1;
5291# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005292# ifdef FEAT_NETBEANS_INTG
5293 int nb_idx = -1;
5294# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005295 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005297# ifdef FEAT_MZSCHEME
5298 mzvim_check_threads();
5299 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5300 {
5301 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
5302 mzquantum_used = TRUE;
5303 }
5304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 fds[0].fd = fd;
5306 fds[0].events = POLLIN;
5307 nfd = 1;
5308
5309# ifdef FEAT_SNIFF
5310# define SNIFF_IDX 1
5311 if (want_sniff_request)
5312 {
5313 fds[SNIFF_IDX].fd = fd_from_sniff;
5314 fds[SNIFF_IDX].events = POLLIN;
5315 nfd++;
5316 }
5317# endif
5318# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005319 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 if (xterm_Shell != (Widget)0)
5321 {
5322 xterm_idx = nfd;
5323 fds[nfd].fd = ConnectionNumber(xterm_dpy);
5324 fds[nfd].events = POLLIN;
5325 nfd++;
5326 }
5327# endif
5328# ifdef FEAT_MOUSE_GPM
5329 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5330 {
5331 gpm_idx = nfd;
5332 fds[nfd].fd = gpm_fd;
5333 fds[nfd].events = POLLIN;
5334 nfd++;
5335 }
5336# endif
5337# ifdef USE_XSMP
5338 if (xsmp_icefd != -1)
5339 {
5340 xsmp_idx = nfd;
5341 fds[nfd].fd = xsmp_icefd;
5342 fds[nfd].events = POLLIN;
5343 nfd++;
5344 }
5345# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005346#ifdef FEAT_NETBEANS_INTG
5347 if (nb_fd != -1)
5348 {
5349 nb_idx = nfd;
5350 fds[nfd].fd = nb_fd;
5351 fds[nfd].events = POLLIN;
5352 nfd++;
5353 }
5354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005356 ret = poll(fds, nfd, towait);
5357# ifdef FEAT_MZSCHEME
5358 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005359 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005360 finished = FALSE;
5361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
5363# ifdef FEAT_SNIFF
5364 if (ret < 0)
5365 sniff_disconnect(1);
5366 else if (want_sniff_request)
5367 {
5368 if (fds[SNIFF_IDX].revents & POLLHUP)
5369 sniff_disconnect(1);
5370 if (fds[SNIFF_IDX].revents & POLLIN)
5371 sniff_request_waiting = 1;
5372 }
5373# endif
5374# ifdef FEAT_XCLIPBOARD
5375 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
5376 {
5377 xterm_update(); /* Maybe we should hand out clipboard */
5378 if (--ret == 0 && !input_available())
5379 /* Try again */
5380 finished = FALSE;
5381 }
5382# endif
5383# ifdef FEAT_MOUSE_GPM
5384 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
5385 {
5386 *check_for_gpm = 1;
5387 }
5388# endif
5389# ifdef USE_XSMP
5390 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
5391 {
5392 if (fds[xsmp_idx].revents & POLLIN)
5393 {
5394 busy = TRUE;
5395 xsmp_handle_requests();
5396 busy = FALSE;
5397 }
5398 else if (fds[xsmp_idx].revents & POLLHUP)
5399 {
5400 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005401 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 xsmp_close();
5403 }
5404 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005405 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005408#ifdef FEAT_NETBEANS_INTG
5409 if (ret > 0 && nb_idx != -1 && fds[nb_idx].revents & POLLIN)
5410 {
5411 netbeans_read();
5412 --ret;
5413 }
5414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
5416
5417#else /* HAVE_SELECT */
5418
5419 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005420 struct timeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 fd_set rfds, efds;
5422 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005423 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005425# ifdef FEAT_MZSCHEME
5426 mzvim_check_threads();
5427 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5428 {
5429 towait = p_mzq; /* don't wait longer than 'mzquantum' */
5430 mzquantum_used = TRUE;
5431 }
5432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433# ifdef __EMX__
5434 /* don't check for incoming chars if not in raw mode, because select()
5435 * always returns TRUE then (in some version of emx.dll) */
5436 if (curr_tmode != TMODE_RAW)
5437 return 0;
5438# endif
5439
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005440 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005442 tv.tv_sec = towait / 1000;
5443 tv.tv_usec = (towait % 1000) * (1000000/1000);
5444 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005446 else
5447 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
5449 /*
5450 * Select on ready for reading and exceptional condition (end of file).
5451 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005452select_eintr:
5453 FD_ZERO(&rfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 FD_ZERO(&efds);
5455 FD_SET(fd, &rfds);
5456# if !defined(__QNX__) && !defined(__CYGWIN32__)
5457 /* For QNX select() always returns 1 if this is set. Why? */
5458 FD_SET(fd, &efds);
5459# endif
5460 maxfd = fd;
5461
5462# ifdef FEAT_SNIFF
5463 if (want_sniff_request)
5464 {
5465 FD_SET(fd_from_sniff, &rfds);
5466 FD_SET(fd_from_sniff, &efds);
5467 if (maxfd < fd_from_sniff)
5468 maxfd = fd_from_sniff;
5469 }
5470# endif
5471# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005472 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 if (xterm_Shell != (Widget)0)
5474 {
5475 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
5476 if (maxfd < ConnectionNumber(xterm_dpy))
5477 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005478
5479 /* An event may have already been read but not handled. In
5480 * particulary, XFlush may cause this. */
5481 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483# endif
5484# ifdef FEAT_MOUSE_GPM
5485 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5486 {
5487 FD_SET(gpm_fd, &rfds);
5488 FD_SET(gpm_fd, &efds);
5489 if (maxfd < gpm_fd)
5490 maxfd = gpm_fd;
5491 }
5492# endif
5493# ifdef USE_XSMP
5494 if (xsmp_icefd != -1)
5495 {
5496 FD_SET(xsmp_icefd, &rfds);
5497 FD_SET(xsmp_icefd, &efds);
5498 if (maxfd < xsmp_icefd)
5499 maxfd = xsmp_icefd;
5500 }
5501# endif
Bram Moolenaardd82d692012-08-15 17:26:57 +02005502# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar67c53842010-05-22 18:28:27 +02005503 if (nb_fd != -1)
5504 {
5505 FD_SET(nb_fd, &rfds);
5506 if (maxfd < nb_fd)
5507 maxfd = nb_fd;
5508 }
Bram Moolenaardd82d692012-08-15 17:26:57 +02005509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005511 ret = select(maxfd + 1, &rfds, NULL, &efds, tvp);
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005512# ifdef EINTR
5513 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005514 {
5515 /* Check whether window has been resized, EINTR may be caused by
5516 * SIGWINCH. */
5517 if (do_resize)
5518 handle_resize();
5519
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005520 /* Interrupted by a signal, need to try again. We ignore msec
5521 * here, because we do want to check even after a timeout if
5522 * characters are available. Needed for reading output of an
5523 * external command after the process has finished. */
5524 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005525 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005526# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00005527# ifdef __TANDEM
5528 if (ret == -1 && errno == ENOTSUP)
5529 {
5530 FD_ZERO(&rfds);
5531 FD_ZERO(&efds);
5532 ret = 0;
5533 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005534# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005535# ifdef FEAT_MZSCHEME
5536 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005537 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005538 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539# endif
5540
5541# ifdef FEAT_SNIFF
5542 if (ret < 0 )
5543 sniff_disconnect(1);
5544 else if (ret > 0 && want_sniff_request)
5545 {
5546 if (FD_ISSET(fd_from_sniff, &efds))
5547 sniff_disconnect(1);
5548 if (FD_ISSET(fd_from_sniff, &rfds))
5549 sniff_request_waiting = 1;
5550 }
5551# endif
5552# ifdef FEAT_XCLIPBOARD
5553 if (ret > 0 && xterm_Shell != (Widget)0
5554 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
5555 {
5556 xterm_update(); /* Maybe we should hand out clipboard */
5557 /* continue looping when we only got the X event and the input
5558 * buffer is empty */
5559 if (--ret == 0 && !input_available())
5560 {
5561 /* Try again */
5562 finished = FALSE;
5563 }
5564 }
5565# endif
5566# ifdef FEAT_MOUSE_GPM
5567 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
5568 {
5569 if (FD_ISSET(gpm_fd, &efds))
5570 gpm_close();
5571 else if (FD_ISSET(gpm_fd, &rfds))
5572 *check_for_gpm = 1;
5573 }
5574# endif
5575# ifdef USE_XSMP
5576 if (ret > 0 && xsmp_icefd != -1)
5577 {
5578 if (FD_ISSET(xsmp_icefd, &efds))
5579 {
5580 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005581 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 xsmp_close();
5583 if (--ret == 0)
5584 finished = FALSE; /* keep going if event was only one */
5585 }
5586 else if (FD_ISSET(xsmp_icefd, &rfds))
5587 {
5588 busy = TRUE;
5589 xsmp_handle_requests();
5590 busy = FALSE;
5591 if (--ret == 0)
5592 finished = FALSE; /* keep going if event was only one */
5593 }
5594 }
5595# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005596#ifdef FEAT_NETBEANS_INTG
5597 if (ret > 0 && nb_fd != -1 && FD_ISSET(nb_fd, &rfds))
5598 {
5599 netbeans_read();
5600 --ret;
5601 }
5602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
5604#endif /* HAVE_SELECT */
5605
5606#ifdef MAY_LOOP
5607 if (finished || msec == 0)
5608 break;
5609
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005610# ifdef FEAT_CLIENTSERVER
5611 if (server_waiting())
5612 break;
5613# endif
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 /* We're going to loop around again, find out for how long */
5616 if (msec > 0)
5617 {
5618# ifdef USE_START_TV
5619 struct timeval mtv;
5620
5621 /* Compute remaining wait time. */
5622 gettimeofday(&mtv, NULL);
5623 msec -= (mtv.tv_sec - start_tv.tv_sec) * 1000L
5624 + (mtv.tv_usec - start_tv.tv_usec) / 1000L;
5625# else
5626 /* Guess we got interrupted halfway. */
5627 msec = msec / 2;
5628# endif
5629 if (msec <= 0)
5630 break; /* waited long enough */
5631 }
5632#endif
5633 }
5634
5635 return (ret > 0);
5636}
5637
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639/*
Bram Moolenaar02743632005-07-25 20:42:36 +00005640 * Expand a path into all matching files and/or directories. Handles "*",
5641 * "?", "[a-z]", "**", etc.
5642 * "path" has backslashes before chars that are not to be expanded.
5643 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 */
5645 int
5646mch_expandpath(gap, path, flags)
5647 garray_T *gap;
5648 char_u *path;
5649 int flags; /* EW_* flags */
5650{
Bram Moolenaar02743632005-07-25 20:42:36 +00005651 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652}
5653#endif
5654
5655/*
5656 * mch_expand_wildcards() - this code does wild-card pattern matching using
5657 * the shell
5658 *
5659 * return OK for success, FAIL for error (you may lose some memory) and put
5660 * an error message in *file.
5661 *
5662 * num_pat is number of input patterns
5663 * pat is array of pointers to input patterns
5664 * num_file is pointer to number of matched file names
5665 * file is pointer to array of pointers to matched file names
5666 */
5667
5668#ifndef SEEK_SET
5669# define SEEK_SET 0
5670#endif
5671#ifndef SEEK_END
5672# define SEEK_END 2
5673#endif
5674
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005675#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00005676
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 int
5678mch_expand_wildcards(num_pat, pat, num_file, file, flags)
5679 int num_pat;
5680 char_u **pat;
5681 int *num_file;
5682 char_u ***file;
5683 int flags; /* EW_* flags */
5684{
5685 int i;
5686 size_t len;
5687 char_u *p;
5688 int dir;
5689#ifdef __EMX__
Bram Moolenaarc7247912008-01-13 12:54:11 +00005690 /*
5691 * This is the OS/2 implementation.
5692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693# define EXPL_ALLOC_INC 16
5694 char_u **expl_files;
5695 size_t files_alloced, files_free;
5696 char_u *buf;
5697 int has_wildcard;
5698
5699 *num_file = 0; /* default: no files found */
5700 files_alloced = EXPL_ALLOC_INC; /* how much space is allocated */
5701 files_free = EXPL_ALLOC_INC; /* how much space is not used */
5702 *file = (char_u **)alloc(sizeof(char_u **) * files_alloced);
5703 if (*file == NULL)
5704 return FAIL;
5705
5706 for (; num_pat > 0; num_pat--, pat++)
5707 {
5708 expl_files = NULL;
5709 if (vim_strchr(*pat, '$') || vim_strchr(*pat, '~'))
5710 /* expand environment var or home dir */
5711 buf = expand_env_save(*pat);
5712 else
5713 buf = vim_strsave(*pat);
5714 expl_files = NULL;
Bram Moolenaard8b02732005-01-14 21:48:43 +00005715 has_wildcard = mch_has_exp_wildcard(buf); /* (still) wildcards? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (has_wildcard) /* yes, so expand them */
5717 expl_files = (char_u **)_fnexplode(buf);
5718
5719 /*
5720 * return value of buf if no wildcards left,
5721 * OR if no match AND EW_NOTFOUND is set.
5722 */
5723 if ((!has_wildcard && ((flags & EW_NOTFOUND) || mch_getperm(buf) >= 0))
5724 || (expl_files == NULL && (flags & EW_NOTFOUND)))
5725 { /* simply save the current contents of *buf */
5726 expl_files = (char_u **)alloc(sizeof(char_u **) * 2);
5727 if (expl_files != NULL)
5728 {
5729 expl_files[0] = vim_strsave(buf);
5730 expl_files[1] = NULL;
5731 }
5732 }
5733 vim_free(buf);
5734
5735 /*
5736 * Count number of names resulting from expansion,
5737 * At the same time add a backslash to the end of names that happen to
5738 * be directories, and replace slashes with backslashes.
5739 */
5740 if (expl_files)
5741 {
5742 for (i = 0; (p = expl_files[i]) != NULL; i++)
5743 {
5744 dir = mch_isdir(p);
5745 /* If we don't want dirs and this is one, skip it */
5746 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
5747 continue;
5748
Bram Moolenaara2031822006-03-07 22:29:51 +00005749 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01005750 if (!dir && (flags & EW_EXEC)
5751 && !mch_can_exe(p, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00005752 continue;
5753
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (--files_free == 0)
5755 {
5756 /* need more room in table of pointers */
5757 files_alloced += EXPL_ALLOC_INC;
5758 *file = (char_u **)vim_realloc(*file,
5759 sizeof(char_u **) * files_alloced);
5760 if (*file == NULL)
5761 {
5762 EMSG(_(e_outofmem));
5763 *num_file = 0;
5764 return FAIL;
5765 }
5766 files_free = EXPL_ALLOC_INC;
5767 }
5768 slash_adjust(p);
5769 if (dir)
5770 {
5771 /* For a directory we add a '/', unless it's already
5772 * there. */
5773 len = STRLEN(p);
5774 if (((*file)[*num_file] = alloc(len + 2)) != NULL)
5775 {
5776 STRCPY((*file)[*num_file], p);
Bram Moolenaar654b5b52006-06-22 17:47:10 +00005777 if (!after_pathsep((*file)[*num_file],
5778 (*file)[*num_file] + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 {
5780 (*file)[*num_file][len] = psepc;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005781 (*file)[*num_file][len + 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783 }
5784 }
5785 else
5786 {
5787 (*file)[*num_file] = vim_strsave(p);
5788 }
5789
5790 /*
5791 * Error message already given by either alloc or vim_strsave.
5792 * Should return FAIL, but returning OK works also.
5793 */
5794 if ((*file)[*num_file] == NULL)
5795 break;
5796 (*num_file)++;
5797 }
5798 _fnexplodefree((char **)expl_files);
5799 }
5800 }
5801 return OK;
5802
5803#else /* __EMX__ */
Bram Moolenaarc7247912008-01-13 12:54:11 +00005804 /*
5805 * This is the non-OS/2 implementation (really Unix).
5806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 int j;
5808 char_u *tempname;
5809 char_u *command;
5810 FILE *fd;
5811 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005812#define STYLE_ECHO 0 /* use "echo", the default */
5813#define STYLE_GLOB 1 /* use "glob", for csh */
5814#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
5815#define STYLE_PRINT 3 /* use "print -N", for zsh */
5816#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
5817 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 int shell_style = STYLE_ECHO;
5819 int check_spaces;
5820 static int did_find_nul = FALSE;
5821 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005822 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005823 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
5825 *num_file = 0; /* default: no files found */
5826 *file = NULL;
5827
5828 /*
5829 * If there are no wildcards, just copy the names to allocated memory.
5830 * Saves a lot of time, because we don't have to start a new shell.
5831 */
5832 if (!have_wildcard(num_pat, pat))
5833 return save_patterns(num_pat, pat, num_file, file);
5834
Bram Moolenaar0e634da2005-07-20 21:57:28 +00005835# ifdef HAVE_SANDBOX
5836 /* Don't allow any shell command in the sandbox. */
5837 if (sandbox != 0 && check_secure())
5838 return FAIL;
5839# endif
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 /*
5842 * Don't allow the use of backticks in secure and restricted mode.
5843 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00005844 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 for (i = 0; i < num_pat; ++i)
5846 if (vim_strchr(pat[i], '`') != NULL
5847 && (check_restricted() || check_secure()))
5848 return FAIL;
5849
5850 /*
5851 * get a name for the temp file
5852 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005853 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {
5855 EMSG(_(e_notmp));
5856 return FAIL;
5857 }
5858
5859 /*
5860 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00005861 * file.
5862 * STYLE_BT: NL separated
5863 * If expanding `cmd` execute it directly.
5864 * STYLE_GLOB: NUL separated
5865 * If we use *csh, "glob" will work better than "echo".
5866 * STYLE_PRINT: NL or NUL separated
5867 * If we use *zsh, "print -N" will work better than "glob".
5868 * STYLE_VIMGLOB: NL separated
5869 * If we use *sh*, we define "vimglob()".
5870 * STYLE_ECHO: space separated.
5871 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 */
5873 if (num_pat == 1 && *pat[0] == '`'
5874 && (len = STRLEN(pat[0])) > 2
5875 && *(pat[0] + len - 1) == '`')
5876 shell_style = STYLE_BT;
5877 else if ((len = STRLEN(p_sh)) >= 3)
5878 {
5879 if (STRCMP(p_sh + len - 3, "csh") == 0)
5880 shell_style = STYLE_GLOB;
5881 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
5882 shell_style = STYLE_PRINT;
5883 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00005884 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
5885 "sh") != NULL)
5886 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
Bram Moolenaarc7247912008-01-13 12:54:11 +00005888 /* Compute the length of the command. We need 2 extra bytes: for the
5889 * optional '&' and for the NUL.
5890 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005892 if (shell_style == STYLE_VIMGLOB)
5893 len += STRLEN(sh_vimglob_func);
5894
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005895 for (i = 0; i < num_pat; ++i)
5896 {
5897 /* Count the length of the patterns in the same way as they are put in
5898 * "command" below. */
5899#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005901#else
5902 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00005903 for (j = 0; pat[i][j] != NUL; ++j)
5904 {
5905 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
5906 ++len; /* may add a backslash */
5907 ++len;
5908 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005909#endif
5910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 command = alloc(len);
5912 if (command == NULL)
5913 {
5914 /* out of memory */
5915 vim_free(tempname);
5916 return FAIL;
5917 }
5918
5919 /*
5920 * Build the shell command:
5921 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
5922 * recognizes this).
5923 * - Add the shell command to print the expanded names.
5924 * - Add the temp file name.
5925 * - Add the file name patterns.
5926 */
5927 if (shell_style == STYLE_BT)
5928 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00005929 /* change `command; command& ` to (command; command ) */
5930 STRCPY(command, "(");
5931 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005933 *p-- = ')'; /* remove last backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 while (p > command && vim_iswhite(*p))
5935 --p;
5936 if (*p == '&') /* remove trailing '&' */
5937 {
5938 ampersent = TRUE;
5939 *p = ' ';
5940 }
5941 STRCAT(command, ">");
5942 }
5943 else
5944 {
5945 if (flags & EW_NOTFOUND)
5946 STRCPY(command, "set nonomatch; ");
5947 else
5948 STRCPY(command, "unset nonomatch; ");
5949 if (shell_style == STYLE_GLOB)
5950 STRCAT(command, "glob >");
5951 else if (shell_style == STYLE_PRINT)
5952 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00005953 else if (shell_style == STYLE_VIMGLOB)
5954 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 else
5956 STRCAT(command, "echo >");
5957 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00005958
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00005960
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 if (shell_style != STYLE_BT)
5962 for (i = 0; i < num_pat; ++i)
5963 {
5964 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00005965 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005966 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967#ifdef USE_SYSTEM
5968 STRCAT(command, " \"");
5969 STRCAT(command, pat[i]);
5970 STRCAT(command, "\"");
5971#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00005972 int intick = FALSE;
5973
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 p = command + STRLEN(command);
5975 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00005976 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005977 {
5978 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00005979 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005980 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
5981 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005982 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00005983 * backslash inside backticks, before a special character
5984 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005985 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00005986 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
5987 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005988 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00005989 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005990 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02005991 else if (!intick
5992 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
5993 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00005994 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02005995 * when inside ``. And not for $var when EW_KEEPDOLLAR is
5996 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00005997 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00005998
5999 /* Copy one character. */
6000 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 *p = NUL;
6003#endif
6004 }
6005 if (flags & EW_SILENT)
6006 show_shell_mess = FALSE;
6007 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006008 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
6010 /*
6011 * Using zsh -G: If a pattern has no matches, it is just deleted from
6012 * the argument list, otherwise zsh gives an error message and doesn't
6013 * expand any other pattern.
6014 */
6015 if (shell_style == STYLE_PRINT)
6016 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6017
6018 /*
6019 * If we use -f then shell variables set in .cshrc won't get expanded.
6020 * vi can do it, so we will too, but it is only necessary if there is a "$"
6021 * in one of the patterns, otherwise we can still use the fast option.
6022 */
6023 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6024 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6025
6026 /*
6027 * execute the shell command
6028 */
6029 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6030
6031 /* When running in the background, give it some time to create the temp
6032 * file, but don't wait for it to finish. */
6033 if (ampersent)
6034 mch_delay(10L, TRUE);
6035
6036 extra_shell_arg = NULL; /* cleanup */
6037 show_shell_mess = TRUE;
6038 vim_free(command);
6039
Bram Moolenaarc7247912008-01-13 12:54:11 +00006040 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 {
6042 mch_remove(tempname);
6043 vim_free(tempname);
6044 /*
6045 * With interactive completion, the error message is not printed.
6046 * However with USE_SYSTEM, I don't know how to turn off error messages
6047 * from the shell, so screen may still get messed up -- webb.
6048 */
6049#ifndef USE_SYSTEM
6050 if (!(flags & EW_SILENT))
6051#endif
6052 {
6053 redraw_later_clear(); /* probably messed up screen */
6054 msg_putchar('\n'); /* clear bottom line quickly */
6055 cmdline_row = Rows - 1; /* continue on last line */
6056#ifdef USE_SYSTEM
6057 if (!(flags & EW_SILENT))
6058#endif
6059 {
6060 MSG(_(e_wildexpand));
6061 msg_start(); /* don't overwrite this message */
6062 }
6063 }
6064 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6065 * EW_NOTFOUND is given */
6066 if (shell_style == STYLE_BT)
6067 return FAIL;
6068 goto notfound;
6069 }
6070
6071 /*
6072 * read the names from the file into memory
6073 */
6074 fd = fopen((char *)tempname, READBIN);
6075 if (fd == NULL)
6076 {
6077 /* Something went wrong, perhaps a file name with a special char. */
6078 if (!(flags & EW_SILENT))
6079 {
6080 MSG(_(e_wildexpand));
6081 msg_start(); /* don't overwrite this message */
6082 }
6083 vim_free(tempname);
6084 goto notfound;
6085 }
6086 fseek(fd, 0L, SEEK_END);
6087 len = ftell(fd); /* get size of temp file */
6088 fseek(fd, 0L, SEEK_SET);
6089 buffer = alloc(len + 1);
6090 if (buffer == NULL)
6091 {
6092 /* out of memory */
6093 mch_remove(tempname);
6094 vim_free(tempname);
6095 fclose(fd);
6096 return FAIL;
6097 }
6098 i = fread((char *)buffer, 1, len, fd);
6099 fclose(fd);
6100 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006101 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 {
6103 /* unexpected read error */
6104 EMSG2(_(e_notread), tempname);
6105 vim_free(tempname);
6106 vim_free(buffer);
6107 return FAIL;
6108 }
6109 vim_free(tempname);
6110
Bram Moolenaarc7247912008-01-13 12:54:11 +00006111# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6113 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006114 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6116 *p++ = buffer[i];
6117 len = p - buffer;
6118# endif
6119
6120
6121 /* file names are separated with Space */
6122 if (shell_style == STYLE_ECHO)
6123 {
6124 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6125 p = buffer;
6126 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6127 {
6128 while (*p != ' ' && *p != '\n')
6129 ++p;
6130 p = skipwhite(p); /* skip to next entry */
6131 }
6132 }
6133 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006134 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
6136 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6137 p = buffer;
6138 for (i = 0; *p != NUL; ++i) /* count number of entries */
6139 {
6140 while (*p != '\n' && *p != NUL)
6141 ++p;
6142 if (*p != NUL)
6143 ++p;
6144 p = skipwhite(p); /* skip leading white space */
6145 }
6146 }
6147 /* file names are separated with NUL */
6148 else
6149 {
6150 /*
6151 * Some versions of zsh use spaces instead of NULs to separate
6152 * results. Only do this when there is no NUL before the end of the
6153 * buffer, otherwise we would never be able to use file names with
6154 * embedded spaces when zsh does use NULs.
6155 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6156 * don't check for spaces again.
6157 */
6158 check_spaces = FALSE;
6159 if (shell_style == STYLE_PRINT && !did_find_nul)
6160 {
6161 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006162 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006163 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 did_find_nul = TRUE;
6165 else
6166 check_spaces = TRUE;
6167 }
6168
6169 /*
6170 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6171 * already is one, for STYLE_GLOB it needs to be added.
6172 */
6173 if (len && buffer[len - 1] == NUL)
6174 --len;
6175 else
6176 buffer[len] = NUL;
6177 i = 0;
6178 for (p = buffer; p < buffer + len; ++p)
6179 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6180 {
6181 ++i;
6182 *p = NUL;
6183 }
6184 if (len)
6185 ++i; /* count last entry */
6186 }
6187 if (i == 0)
6188 {
6189 /*
6190 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6191 * /bin/sh will happily expand it to nothing rather than returning an
6192 * error; and hey, it's good to check anyway -- webb.
6193 */
6194 vim_free(buffer);
6195 goto notfound;
6196 }
6197 *num_file = i;
6198 *file = (char_u **)alloc(sizeof(char_u *) * i);
6199 if (*file == NULL)
6200 {
6201 /* out of memory */
6202 vim_free(buffer);
6203 return FAIL;
6204 }
6205
6206 /*
6207 * Isolate the individual file names.
6208 */
6209 p = buffer;
6210 for (i = 0; i < *num_file; ++i)
6211 {
6212 (*file)[i] = p;
6213 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006214 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6215 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006217 while (!(shell_style == STYLE_ECHO && *p == ' ')
6218 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 ++p;
6220 if (p == buffer + len) /* last entry */
6221 *p = NUL;
6222 else
6223 {
6224 *p++ = NUL;
6225 p = skipwhite(p); /* skip to next entry */
6226 }
6227 }
6228 else /* NUL separates */
6229 {
6230 while (*p && p < buffer + len) /* skip entry */
6231 ++p;
6232 ++p; /* skip NUL */
6233 }
6234 }
6235
6236 /*
6237 * Move the file names to allocated memory.
6238 */
6239 for (j = 0, i = 0; i < *num_file; ++i)
6240 {
6241 /* Require the files to exist. Helps when using /bin/sh */
6242 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6243 continue;
6244
6245 /* check if this entry should be included */
6246 dir = (mch_isdir((*file)[i]));
6247 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6248 continue;
6249
Bram Moolenaara2031822006-03-07 22:29:51 +00006250 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006251 if (!dir && (flags & EW_EXEC)
6252 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006253 continue;
6254
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6256 if (p)
6257 {
6258 STRCPY(p, (*file)[i]);
6259 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006260 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 (*file)[j++] = p;
6262 }
6263 }
6264 vim_free(buffer);
6265 *num_file = j;
6266
6267 if (*num_file == 0) /* rejected all entries */
6268 {
6269 vim_free(*file);
6270 *file = NULL;
6271 goto notfound;
6272 }
6273
6274 return OK;
6275
6276notfound:
6277 if (flags & EW_NOTFOUND)
6278 return save_patterns(num_pat, pat, num_file, file);
6279 return FAIL;
6280
6281#endif /* __EMX__ */
6282}
6283
6284#endif /* VMS */
6285
6286#ifndef __EMX__
6287 static int
6288save_patterns(num_pat, pat, num_file, file)
6289 int num_pat;
6290 char_u **pat;
6291 int *num_file;
6292 char_u ***file;
6293{
6294 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006295 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
6297 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6298 if (*file == NULL)
6299 return FAIL;
6300 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006301 {
6302 s = vim_strsave(pat[i]);
6303 if (s != NULL)
6304 /* Be compatible with expand_filename(): halve the number of
6305 * backslashes. */
6306 backslash_halve(s);
6307 (*file)[i] = s;
6308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 *num_file = num_pat;
6310 return OK;
6311}
6312#endif
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314/*
6315 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6316 * expand.
6317 */
6318 int
6319mch_has_exp_wildcard(p)
6320 char_u *p;
6321{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006322 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 {
6324#ifndef OS2
6325 if (*p == '\\' && p[1] != NUL)
6326 ++p;
6327 else
6328#endif
6329 if (vim_strchr((char_u *)
6330#ifdef VMS
6331 "*?%"
6332#else
6333# ifdef OS2
6334 "*?"
6335# else
6336 "*?[{'"
6337# endif
6338#endif
6339 , *p) != NULL)
6340 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 }
6342 return FALSE;
6343}
6344
6345/*
6346 * Return TRUE if the string "p" contains a wildcard.
6347 * Don't recognize '~' at the end as a wildcard.
6348 */
6349 int
6350mch_has_wildcard(p)
6351 char_u *p;
6352{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006353 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 {
6355#ifndef OS2
6356 if (*p == '\\' && p[1] != NUL)
6357 ++p;
6358 else
6359#endif
6360 if (vim_strchr((char_u *)
6361#ifdef VMS
6362 "*?%$"
6363#else
6364# ifdef OS2
6365# ifdef VIM_BACKTICK
6366 "*?$`"
6367# else
6368 "*?$"
6369# endif
6370# else
6371 "*?[{`'$"
6372# endif
6373#endif
6374 , *p) != NULL
6375 || (*p == '~' && p[1] != NUL))
6376 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 }
6378 return FALSE;
6379}
6380
6381#ifndef __EMX__
6382 static int
6383have_wildcard(num, file)
6384 int num;
6385 char_u **file;
6386{
6387 int i;
6388
6389 for (i = 0; i < num; i++)
6390 if (mch_has_wildcard(file[i]))
6391 return 1;
6392 return 0;
6393}
6394
6395 static int
6396have_dollars(num, file)
6397 int num;
6398 char_u **file;
6399{
6400 int i;
6401
6402 for (i = 0; i < num; i++)
6403 if (vim_strchr(file[i], '$') != NULL)
6404 return TRUE;
6405 return FALSE;
6406}
6407#endif /* ifndef __EMX__ */
6408
6409#ifndef HAVE_RENAME
6410/*
6411 * Scaled-down version of rename(), which is missing in Xenix.
6412 * This version can only move regular files and will fail if the
6413 * destination exists.
6414 */
6415 int
6416mch_rename(src, dest)
6417 const char *src, *dest;
6418{
6419 struct stat st;
6420
6421 if (stat(dest, &st) >= 0) /* fail if destination exists */
6422 return -1;
6423 if (link(src, dest) != 0) /* link file to new name */
6424 return -1;
6425 if (mch_remove(src) == 0) /* delete link to old name */
6426 return 0;
6427 return -1;
6428}
6429#endif /* !HAVE_RENAME */
6430
6431#ifdef FEAT_MOUSE_GPM
6432/*
6433 * Initializes connection with gpm (if it isn't already opened)
6434 * Return 1 if succeeded (or connection already opened), 0 if failed
6435 */
6436 static int
6437gpm_open()
6438{
6439 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6440
6441 if (!gpm_flag)
6442 {
6443 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6444 gpm_connect.defaultMask = ~GPM_HARD;
6445 /* Default handling for mouse move*/
6446 gpm_connect.minMod = 0; /* Handle any modifier keys */
6447 gpm_connect.maxMod = 0xffff;
6448 if (Gpm_Open(&gpm_connect, 0) > 0)
6449 {
6450 /* gpm library tries to handling TSTP causes
6451 * problems. Anyways, we close connection to Gpm whenever
6452 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006453 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006455# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 return 1; /* succeed */
6459 }
6460 if (gpm_fd == -2)
6461 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6462 return 0;
6463 }
6464 return 1; /* already open */
6465}
6466
6467/*
6468 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 */
6470 static void
6471gpm_close()
6472{
6473 if (gpm_flag && gpm_fd >= 0) /* if Open */
6474 Gpm_Close();
6475}
6476
6477/* Reads gpm event and adds special keys to input buf. Returns length of
6478 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02006479 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 */
6481 static int
6482mch_gpm_process()
6483{
6484 int button;
6485 static Gpm_Event gpm_event;
6486 char_u string[6];
6487 int_u vim_modifiers;
6488 int row,col;
6489 unsigned char buttons_mask;
6490 unsigned char gpm_modifiers;
6491 static unsigned char old_buttons = 0;
6492
6493 Gpm_GetEvent(&gpm_event);
6494
6495#ifdef FEAT_GUI
6496 /* Don't put events in the input queue now. */
6497 if (hold_gui_events)
6498 return 0;
6499#endif
6500
6501 row = gpm_event.y - 1;
6502 col = gpm_event.x - 1;
6503
6504 string[0] = ESC; /* Our termcode */
6505 string[1] = 'M';
6506 string[2] = 'G';
6507 switch (GPM_BARE_EVENTS(gpm_event.type))
6508 {
6509 case GPM_DRAG:
6510 string[3] = MOUSE_DRAG;
6511 break;
6512 case GPM_DOWN:
6513 buttons_mask = gpm_event.buttons & ~old_buttons;
6514 old_buttons = gpm_event.buttons;
6515 switch (buttons_mask)
6516 {
6517 case GPM_B_LEFT:
6518 button = MOUSE_LEFT;
6519 break;
6520 case GPM_B_MIDDLE:
6521 button = MOUSE_MIDDLE;
6522 break;
6523 case GPM_B_RIGHT:
6524 button = MOUSE_RIGHT;
6525 break;
6526 default:
6527 return 0;
6528 /*Don't know what to do. Can more than one button be
6529 * reported in one event? */
6530 }
6531 string[3] = (char_u)(button | 0x20);
6532 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
6533 break;
6534 case GPM_UP:
6535 string[3] = MOUSE_RELEASE;
6536 old_buttons &= ~gpm_event.buttons;
6537 break;
6538 default:
6539 return 0;
6540 }
6541 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
6542 gpm_modifiers = gpm_event.modifiers;
6543 vim_modifiers = 0x0;
6544 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
6545 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
6546 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
6547 */
6548 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
6549 vim_modifiers |= MOUSE_SHIFT;
6550
6551 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
6552 vim_modifiers |= MOUSE_CTRL;
6553 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
6554 vim_modifiers |= MOUSE_ALT;
6555 string[3] |= vim_modifiers;
6556 string[4] = (char_u)(col + ' ' + 1);
6557 string[5] = (char_u)(row + ' ' + 1);
6558 add_to_input_buf(string, 6);
6559 return 6;
6560}
6561#endif /* FEAT_MOUSE_GPM */
6562
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006563#ifdef FEAT_SYSMOUSE
6564/*
6565 * Initialize connection with sysmouse.
6566 * Let virtual console inform us with SIGUSR2 for pending sysmouse
6567 * output, any sysmouse output than will be processed via sig_sysmouse().
6568 * Return OK if succeeded, FAIL if failed.
6569 */
6570 static int
6571sysmouse_open()
6572{
6573 struct mouse_info mouse;
6574
6575 mouse.operation = MOUSE_MODE;
6576 mouse.u.mode.mode = 0;
6577 mouse.u.mode.signal = SIGUSR2;
6578 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
6579 {
6580 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
6581 mouse.operation = MOUSE_SHOW;
6582 ioctl(1, CONS_MOUSECTL, &mouse);
6583 return OK;
6584 }
6585 return FAIL;
6586}
6587
6588/*
6589 * Stop processing SIGUSR2 signals, and also make sure that
6590 * virtual console do not send us any sysmouse related signal.
6591 */
6592 static void
6593sysmouse_close()
6594{
6595 struct mouse_info mouse;
6596
6597 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
6598 mouse.operation = MOUSE_MODE;
6599 mouse.u.mode.mode = 0;
6600 mouse.u.mode.signal = 0;
6601 ioctl(1, CONS_MOUSECTL, &mouse);
6602}
6603
6604/*
6605 * Gets info from sysmouse and adds special keys to input buf.
6606 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006607 static RETSIGTYPE
6608sig_sysmouse SIGDEFARG(sigarg)
6609{
6610 struct mouse_info mouse;
6611 struct video_info video;
6612 char_u string[6];
6613 int row, col;
6614 int button;
6615 int buttons;
6616 static int oldbuttons = 0;
6617
6618#ifdef FEAT_GUI
6619 /* Don't put events in the input queue now. */
6620 if (hold_gui_events)
6621 return;
6622#endif
6623
6624 mouse.operation = MOUSE_GETINFO;
6625 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
6626 && ioctl(1, FBIO_MODEINFO, &video) != -1
6627 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
6628 && video.vi_cheight > 0 && video.vi_cwidth > 0)
6629 {
6630 row = mouse.u.data.y / video.vi_cheight;
6631 col = mouse.u.data.x / video.vi_cwidth;
6632 buttons = mouse.u.data.buttons;
6633 string[0] = ESC; /* Our termcode */
6634 string[1] = 'M';
6635 string[2] = 'S';
6636 if (oldbuttons == buttons && buttons != 0)
6637 {
6638 button = MOUSE_DRAG;
6639 }
6640 else
6641 {
6642 switch (buttons)
6643 {
6644 case 0:
6645 button = MOUSE_RELEASE;
6646 break;
6647 case 1:
6648 button = MOUSE_LEFT;
6649 break;
6650 case 2:
6651 button = MOUSE_MIDDLE;
6652 break;
6653 case 4:
6654 button = MOUSE_RIGHT;
6655 break;
6656 default:
6657 return;
6658 }
6659 oldbuttons = buttons;
6660 }
6661 string[3] = (char_u)(button);
6662 string[4] = (char_u)(col + ' ' + 1);
6663 string[5] = (char_u)(row + ' ' + 1);
6664 add_to_input_buf(string, 6);
6665 }
6666 return;
6667}
6668#endif /* FEAT_SYSMOUSE */
6669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670#if defined(FEAT_LIBCALL) || defined(PROTO)
6671typedef char_u * (*STRPROCSTR)__ARGS((char_u *));
6672typedef char_u * (*INTPROCSTR)__ARGS((int));
6673typedef int (*STRPROCINT)__ARGS((char_u *));
6674typedef int (*INTPROCINT)__ARGS((int));
6675
6676/*
6677 * Call a DLL routine which takes either a string or int param
6678 * and returns an allocated string.
6679 */
6680 int
6681mch_libcall(libname, funcname, argstring, argint, string_result, number_result)
6682 char_u *libname;
6683 char_u *funcname;
6684 char_u *argstring; /* NULL when using a argint */
6685 int argint;
6686 char_u **string_result;/* NULL when using number_result */
6687 int *number_result;
6688{
6689# if defined(USE_DLOPEN)
6690 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006691 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692# else
6693 shl_t hinstLib;
6694# endif
6695 STRPROCSTR ProcAdd;
6696 INTPROCSTR ProcAddI;
6697 char_u *retval_str = NULL;
6698 int retval_int = 0;
6699 int success = FALSE;
6700
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006701 /*
6702 * Get a handle to the DLL module.
6703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006705 /* First clear any error, it's not cleared by the dlopen() call. */
6706 (void)dlerror();
6707
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 hinstLib = dlopen((char *)libname, RTLD_LAZY
6709# ifdef RTLD_LOCAL
6710 | RTLD_LOCAL
6711# endif
6712 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006713 if (hinstLib == NULL)
6714 {
6715 /* "dlerr" must be used before dlclose() */
6716 dlerr = (char *)dlerror();
6717 if (dlerr != NULL)
6718 EMSG2(_("dlerror = \"%s\""), dlerr);
6719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720# else
6721 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
6722# endif
6723
6724 /* If the handle is valid, try to get the function address. */
6725 if (hinstLib != NULL)
6726 {
6727# ifdef HAVE_SETJMP_H
6728 /*
6729 * Catch a crash when calling the library function. For example when
6730 * using a number where a string pointer is expected.
6731 */
6732 mch_startjmp();
6733 if (SETJMP(lc_jump_env) != 0)
6734 {
6735 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006736# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006737 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006738# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 mch_didjmp();
6740 }
6741 else
6742# endif
6743 {
6744 retval_str = NULL;
6745 retval_int = 0;
6746
6747 if (argstring != NULL)
6748 {
6749# if defined(USE_DLOPEN)
6750 ProcAdd = (STRPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006751 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752# else
6753 if (shl_findsym(&hinstLib, (const char *)funcname,
6754 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
6755 ProcAdd = NULL;
6756# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006757 if ((success = (ProcAdd != NULL
6758# if defined(USE_DLOPEN)
6759 && dlerr == NULL
6760# endif
6761 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
6763 if (string_result == NULL)
6764 retval_int = ((STRPROCINT)ProcAdd)(argstring);
6765 else
6766 retval_str = (ProcAdd)(argstring);
6767 }
6768 }
6769 else
6770 {
6771# if defined(USE_DLOPEN)
6772 ProcAddI = (INTPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006773 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774# else
6775 if (shl_findsym(&hinstLib, (const char *)funcname,
6776 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
6777 ProcAddI = NULL;
6778# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006779 if ((success = (ProcAddI != NULL
6780# if defined(USE_DLOPEN)
6781 && dlerr == NULL
6782# endif
6783 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {
6785 if (string_result == NULL)
6786 retval_int = ((INTPROCINT)ProcAddI)(argint);
6787 else
6788 retval_str = (ProcAddI)(argint);
6789 }
6790 }
6791
6792 /* Save the string before we free the library. */
6793 /* Assume that a "1" or "-1" result is an illegal pointer. */
6794 if (string_result == NULL)
6795 *number_result = retval_int;
6796 else if (retval_str != NULL
6797 && retval_str != (char_u *)1
6798 && retval_str != (char_u *)-1)
6799 *string_result = vim_strsave(retval_str);
6800 }
6801
6802# ifdef HAVE_SETJMP_H
6803 mch_endjmp();
6804# ifdef SIGHASARG
6805 if (lc_signal != 0)
6806 {
6807 int i;
6808
6809 /* try to find the name of this signal */
6810 for (i = 0; signal_info[i].sig != -1; i++)
6811 if (lc_signal == signal_info[i].sig)
6812 break;
6813 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
6814 }
6815# endif
6816# endif
6817
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006819 /* "dlerr" must be used before dlclose() */
6820 if (dlerr != NULL)
6821 EMSG2(_("dlerror = \"%s\""), dlerr);
6822
6823 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 (void)dlclose(hinstLib);
6825# else
6826 (void)shl_unload(hinstLib);
6827# endif
6828 }
6829
6830 if (!success)
6831 {
6832 EMSG2(_(e_libcall), funcname);
6833 return FAIL;
6834 }
6835
6836 return OK;
6837}
6838#endif
6839
6840#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
6841static int xterm_trace = -1; /* default: disabled */
6842static int xterm_button;
6843
6844/*
6845 * Setup a dummy window for X selections in a terminal.
6846 */
6847 void
6848setup_term_clip()
6849{
6850 int z = 0;
6851 char *strp = "";
6852 Widget AppShell;
6853
6854 if (!x_connect_to_server())
6855 return;
6856
6857 open_app_context();
6858 if (app_context != NULL && xterm_Shell == (Widget)0)
6859 {
6860 int (*oldhandler)();
6861#if defined(HAVE_SETJMP_H)
6862 int (*oldIOhandler)();
6863#endif
6864# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
6865 struct timeval start_tv;
6866
6867 if (p_verbose > 0)
6868 gettimeofday(&start_tv, NULL);
6869# endif
6870
6871 /* Ignore X errors while opening the display */
6872 oldhandler = XSetErrorHandler(x_error_check);
6873
6874#if defined(HAVE_SETJMP_H)
6875 /* Ignore X IO errors while opening the display */
6876 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
6877 mch_startjmp();
6878 if (SETJMP(lc_jump_env) != 0)
6879 {
6880 mch_didjmp();
6881 xterm_dpy = NULL;
6882 }
6883 else
6884#endif
6885 {
6886 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
6887 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
6888#if defined(HAVE_SETJMP_H)
6889 mch_endjmp();
6890#endif
6891 }
6892
6893#if defined(HAVE_SETJMP_H)
6894 /* Now handle X IO errors normally. */
6895 (void)XSetIOErrorHandler(oldIOhandler);
6896#endif
6897 /* Now handle X errors normally. */
6898 (void)XSetErrorHandler(oldhandler);
6899
6900 if (xterm_dpy == NULL)
6901 {
6902 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006903 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 return;
6905 }
6906
6907 /* Catch terminating error of the X server connection. */
6908 (void)XSetIOErrorHandler(x_IOerror_handler);
6909
6910# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
6911 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006912 {
6913 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 xopen_message(&start_tv);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006915 verbose_leave();
6916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917# endif
6918
6919 /* Create a Shell to make converters work. */
6920 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
6921 applicationShellWidgetClass, xterm_dpy,
6922 NULL);
6923 if (AppShell == (Widget)0)
6924 return;
6925 xterm_Shell = XtVaCreatePopupShell("VIM",
6926 topLevelShellWidgetClass, AppShell,
6927 XtNmappedWhenManaged, 0,
6928 XtNwidth, 1,
6929 XtNheight, 1,
6930 NULL);
6931 if (xterm_Shell == (Widget)0)
6932 return;
6933
6934 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02006935 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (x11_display == NULL)
6937 x11_display = xterm_dpy;
6938
6939 XtRealizeWidget(xterm_Shell);
6940 XSync(xterm_dpy, False);
6941 xterm_update();
6942 }
6943 if (xterm_Shell != (Widget)0)
6944 {
6945 clip_init(TRUE);
6946 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
6947 x11_window = (Window)atol(strp);
6948 /* Check if $WINDOWID is valid. */
6949 if (test_x11_window(xterm_dpy) == FAIL)
6950 x11_window = 0;
6951 if (x11_window != 0)
6952 xterm_trace = 0;
6953 }
6954}
6955
6956 void
6957start_xterm_trace(button)
6958 int button;
6959{
6960 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
6961 return;
6962 xterm_trace = 1;
6963 xterm_button = button;
6964 do_xterm_trace();
6965}
6966
6967
6968 void
6969stop_xterm_trace()
6970{
6971 if (xterm_trace < 0)
6972 return;
6973 xterm_trace = 0;
6974}
6975
6976/*
6977 * Query the xterm pointer and generate mouse termcodes if necessary
6978 * return TRUE if dragging is active, else FALSE
6979 */
6980 static int
6981do_xterm_trace()
6982{
6983 Window root, child;
6984 int root_x, root_y;
6985 int win_x, win_y;
6986 int row, col;
6987 int_u mask_return;
6988 char_u buf[50];
6989 char_u *strp;
6990 long got_hints;
6991 static char_u *mouse_code;
6992 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
6993 static int prev_row = 0, prev_col = 0;
6994 static XSizeHints xterm_hints;
6995
6996 if (xterm_trace <= 0)
6997 return FALSE;
6998
6999 if (xterm_trace == 1)
7000 {
7001 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007002 * have changed recently. */
7003 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7004 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 || xterm_hints.width_inc <= 1
7006 || xterm_hints.height_inc <= 1)
7007 {
7008 xterm_trace = -1; /* Not enough data -- disable tracing */
7009 return FALSE;
7010 }
7011
7012 /* Rely on the same mouse code for the duration of this */
7013 mouse_code = find_termcode(mouse_name);
7014 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007015 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 xterm_trace = 2;
7017
7018 /* Find the offset of the chars, there might be a scrollbar on the
7019 * left of the window and/or a menu on the top (eterm etc.) */
7020 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7021 &win_x, &win_y, &mask_return);
7022 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7023 - (xterm_hints.height_inc / 2);
7024 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7025 xterm_hints.y = 2;
7026 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7027 - (xterm_hints.width_inc / 2);
7028 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7029 xterm_hints.x = 2;
7030 return TRUE;
7031 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007032 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {
7034 xterm_trace = 0;
7035 return FALSE;
7036 }
7037
7038 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7039 &win_x, &win_y, &mask_return);
7040
7041 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7042 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7043 if (row == prev_row && col == prev_col)
7044 return TRUE;
7045
7046 STRCPY(buf, mouse_code);
7047 strp = buf + STRLEN(buf);
7048 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7049 *strp++ = (char_u)(col + ' ' + 1);
7050 *strp++ = (char_u)(row + ' ' + 1);
7051 *strp = 0;
7052 add_to_input_buf(buf, STRLEN(buf));
7053
7054 prev_row = row;
7055 prev_col = col;
7056 return TRUE;
7057}
7058
7059# if defined(FEAT_GUI) || defined(PROTO)
7060/*
7061 * Destroy the display, window and app_context. Required for GTK.
7062 */
7063 void
7064clear_xterm_clip()
7065{
7066 if (xterm_Shell != (Widget)0)
7067 {
7068 XtDestroyWidget(xterm_Shell);
7069 xterm_Shell = (Widget)0;
7070 }
7071 if (xterm_dpy != NULL)
7072 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007073# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 /* Lesstif and Solaris crash here, lose some memory */
7075 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 if (x11_display == xterm_dpy)
7078 x11_display = NULL;
7079 xterm_dpy = NULL;
7080 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007081# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 if (app_context != (XtAppContext)NULL)
7083 {
7084 /* Lesstif and Solaris crash here, lose some memory */
7085 XtDestroyApplicationContext(app_context);
7086 app_context = (XtAppContext)NULL;
7087 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089}
7090# endif
7091
7092/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007093 * Catch up with GUI or X events.
7094 */
7095 static void
7096clip_update()
7097{
7098# ifdef FEAT_GUI
7099 if (gui.in_use)
7100 gui_mch_update();
7101 else
7102# endif
7103 if (xterm_Shell != (Widget)0)
7104 xterm_update();
7105}
7106
7107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 * Catch up with any queued X events. This may put keyboard input into the
7109 * input buffer, call resize call-backs, trigger timers etc. If there is
7110 * nothing in the X event queue (& no timers pending), then we return
7111 * immediately.
7112 */
7113 static void
7114xterm_update()
7115{
7116 XEvent event;
7117
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007118 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007120 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007122 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007123 break;
7124
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007125 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007126 {
7127 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007128 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007129#ifdef FEAT_CLIENTSERVER
7130 {
7131 XPropertyEvent *e = (XPropertyEvent *)&event;
7132
7133 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007135 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007138 XtDispatchEvent(&event);
7139 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007140 else
7141 {
7142 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007143 XtAppProcessEvent(app_context, mask);
7144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 }
7146}
7147
7148 int
7149clip_xterm_own_selection(cbd)
7150 VimClipboard *cbd;
7151{
7152 if (xterm_Shell != (Widget)0)
7153 return clip_x11_own_selection(xterm_Shell, cbd);
7154 return FAIL;
7155}
7156
7157 void
7158clip_xterm_lose_selection(cbd)
7159 VimClipboard *cbd;
7160{
7161 if (xterm_Shell != (Widget)0)
7162 clip_x11_lose_selection(xterm_Shell, cbd);
7163}
7164
7165 void
7166clip_xterm_request_selection(cbd)
7167 VimClipboard *cbd;
7168{
7169 if (xterm_Shell != (Widget)0)
7170 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7171}
7172
7173 void
7174clip_xterm_set_selection(cbd)
7175 VimClipboard *cbd;
7176{
7177 clip_x11_set_selection(cbd);
7178}
7179#endif
7180
7181
7182#if defined(USE_XSMP) || defined(PROTO)
7183/*
7184 * Code for X Session Management Protocol.
7185 */
7186static void xsmp_handle_save_yourself __ARGS((SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast));
7187static void xsmp_die __ARGS((SmcConn smc_conn, SmPointer client_data));
7188static void xsmp_save_complete __ARGS((SmcConn smc_conn, SmPointer client_data));
7189static void xsmp_shutdown_cancelled __ARGS((SmcConn smc_conn, SmPointer client_data));
7190static void xsmp_ice_connection __ARGS((IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData));
7191
7192
7193# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7194static void xsmp_handle_interaction __ARGS((SmcConn smc_conn, SmPointer client_data));
7195
7196/*
7197 * This is our chance to ask the user if they want to save,
7198 * or abort the logout
7199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 static void
7201xsmp_handle_interaction(smc_conn, client_data)
7202 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007203 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
7205 cmdmod_T save_cmdmod;
7206 int cancel_shutdown = False;
7207
7208 save_cmdmod = cmdmod;
7209 cmdmod.confirm = TRUE;
7210 if (check_changed_any(FALSE))
7211 /* Mustn't logout */
7212 cancel_shutdown = True;
7213 cmdmod = save_cmdmod;
7214 setcursor(); /* position cursor */
7215 out_flush();
7216
7217 /* Done interaction */
7218 SmcInteractDone(smc_conn, cancel_shutdown);
7219
7220 /* Finish off
7221 * Only end save-yourself here if we're not cancelling shutdown;
7222 * we'll get a cancelled callback later in which we'll end it.
7223 * Hopefully get around glitchy SMs (like GNOME-1)
7224 */
7225 if (!cancel_shutdown)
7226 {
7227 xsmp.save_yourself = False;
7228 SmcSaveYourselfDone(smc_conn, True);
7229 }
7230}
7231# endif
7232
7233/*
7234 * Callback that starts save-yourself.
7235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 static void
7237xsmp_handle_save_yourself(smc_conn, client_data, save_type,
7238 shutdown, interact_style, fast)
7239 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007240 SmPointer client_data UNUSED;
7241 int save_type UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 Bool shutdown;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007243 int interact_style UNUSED;
7244 Bool fast UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
7246 /* Handle already being in saveyourself */
7247 if (xsmp.save_yourself)
7248 SmcSaveYourselfDone(smc_conn, True);
7249 xsmp.save_yourself = True;
7250 xsmp.shutdown = shutdown;
7251
7252 /* First up, preserve all files */
7253 out_flush();
7254 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7255
7256 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007257 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
7259# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7260 /* Now see if we can ask about unsaved files */
7261 if (shutdown && !fast && gui.in_use)
7262 /* Need to interact with user, but need SM's permission */
7263 SmcInteractRequest(smc_conn, SmDialogError,
7264 xsmp_handle_interaction, client_data);
7265 else
7266# endif
7267 {
7268 /* Can stop the cycle here */
7269 SmcSaveYourselfDone(smc_conn, True);
7270 xsmp.save_yourself = False;
7271 }
7272}
7273
7274
7275/*
7276 * Callback to warn us of imminent death.
7277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 static void
7279xsmp_die(smc_conn, client_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007280 SmcConn smc_conn UNUSED;
7281 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282{
7283 xsmp_close();
7284
7285 /* quit quickly leaving swapfiles for modified buffers behind */
7286 getout_preserve_modified(0);
7287}
7288
7289
7290/*
7291 * Callback to tell us that save-yourself has completed.
7292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 static void
7294xsmp_save_complete(smc_conn, client_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007295 SmcConn smc_conn UNUSED;
7296 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297{
7298 xsmp.save_yourself = False;
7299}
7300
7301
7302/*
7303 * Callback to tell us that an instigated shutdown was cancelled
7304 * (maybe even by us)
7305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 static void
7307xsmp_shutdown_cancelled(smc_conn, client_data)
7308 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007309 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
7311 if (xsmp.save_yourself)
7312 SmcSaveYourselfDone(smc_conn, True);
7313 xsmp.save_yourself = False;
7314 xsmp.shutdown = False;
7315}
7316
7317
7318/*
7319 * Callback to tell us that a new ICE connection has been established.
7320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 static void
7322xsmp_ice_connection(iceConn, clientData, opening, watchData)
7323 IceConn iceConn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007324 IcePointer clientData UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 Bool opening;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007326 IcePointer *watchData UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
7328 /* Intercept creation of ICE connection fd */
7329 if (opening)
7330 {
7331 xsmp_icefd = IceConnectionNumber(iceConn);
7332 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7333 }
7334}
7335
7336
7337/* Handle any ICE processing that's required; return FAIL if SM lost */
7338 int
7339xsmp_handle_requests()
7340{
7341 Bool rep;
7342
7343 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7344 == IceProcessMessagesIOError)
7345 {
7346 /* Lost ICE */
7347 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007348 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 xsmp_close();
7350 return FAIL;
7351 }
7352 else
7353 return OK;
7354}
7355
7356static int dummy;
7357
7358/* Set up X Session Management Protocol */
7359 void
7360xsmp_init(void)
7361{
7362 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 SmcCallbacks smcallbacks;
7364#if 0
7365 SmPropValue smname;
7366 SmProp smnameprop;
7367 SmProp *smprops[1];
7368#endif
7369
7370 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007371 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372
7373 xsmp.save_yourself = xsmp.shutdown = False;
7374
7375 /* Set up SM callbacks - must have all, even if they're not used */
7376 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7377 smcallbacks.save_yourself.client_data = NULL;
7378 smcallbacks.die.callback = xsmp_die;
7379 smcallbacks.die.client_data = NULL;
7380 smcallbacks.save_complete.callback = xsmp_save_complete;
7381 smcallbacks.save_complete.client_data = NULL;
7382 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7383 smcallbacks.shutdown_cancelled.client_data = NULL;
7384
7385 /* Set up a watch on ICE connection creations. The "dummy" argument is
7386 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7387 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7388 {
7389 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007390 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 return;
7392 }
7393
7394 /* Create an SM connection */
7395 xsmp.smcconn = SmcOpenConnection(
7396 NULL,
7397 NULL,
7398 SmProtoMajor,
7399 SmProtoMinor,
7400 SmcSaveYourselfProcMask | SmcDieProcMask
7401 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7402 &smcallbacks,
7403 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007404 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 sizeof(errorstring),
7406 errorstring);
7407 if (xsmp.smcconn == NULL)
7408 {
7409 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007410
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007412 {
7413 vim_snprintf(errorreport, sizeof(errorreport),
7414 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7415 verb_msg((char_u *)errorreport);
7416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 return;
7418 }
7419 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7420
7421#if 0
7422 /* ID ourselves */
7423 smname.value = "vim";
7424 smname.length = 3;
7425 smnameprop.name = "SmProgram";
7426 smnameprop.type = "SmARRAY8";
7427 smnameprop.num_vals = 1;
7428 smnameprop.vals = &smname;
7429
7430 smprops[0] = &smnameprop;
7431 SmcSetProperties(xsmp.smcconn, 1, smprops);
7432#endif
7433}
7434
7435
7436/* Shut down XSMP comms. */
7437 void
7438xsmp_close()
7439{
7440 if (xsmp_icefd != -1)
7441 {
7442 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007443 if (xsmp.clientid != NULL)
7444 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007445 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 xsmp_icefd = -1;
7447 }
7448}
7449#endif /* USE_XSMP */
7450
7451
7452#ifdef EBCDIC
7453/* Translate character to its CTRL- value */
7454char CtrlTable[] =
7455{
7456/* 00 - 5E */
7457 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7458 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7459 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7460 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7461 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7462 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7463/* ^ */ 0x1E,
7464/* - */ 0x1F,
7465/* 61 - 6C */
7466 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7467/* _ */ 0x1F,
7468/* 6E - 80 */
7469 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7470/* a */ 0x01,
7471/* b */ 0x02,
7472/* c */ 0x03,
7473/* d */ 0x37,
7474/* e */ 0x2D,
7475/* f */ 0x2E,
7476/* g */ 0x2F,
7477/* h */ 0x16,
7478/* i */ 0x05,
7479/* 8A - 90 */
7480 0, 0, 0, 0, 0, 0, 0,
7481/* j */ 0x15,
7482/* k */ 0x0B,
7483/* l */ 0x0C,
7484/* m */ 0x0D,
7485/* n */ 0x0E,
7486/* o */ 0x0F,
7487/* p */ 0x10,
7488/* q */ 0x11,
7489/* r */ 0x12,
7490/* 9A - A1 */
7491 0, 0, 0, 0, 0, 0, 0, 0,
7492/* s */ 0x13,
7493/* t */ 0x3C,
7494/* u */ 0x3D,
7495/* v */ 0x32,
7496/* w */ 0x26,
7497/* x */ 0x18,
7498/* y */ 0x19,
7499/* z */ 0x3F,
7500/* AA - AC */
7501 0, 0, 0,
7502/* [ */ 0x27,
7503/* AE - BC */
7504 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7505/* ] */ 0x1D,
7506/* BE - C0 */ 0, 0, 0,
7507/* A */ 0x01,
7508/* B */ 0x02,
7509/* C */ 0x03,
7510/* D */ 0x37,
7511/* E */ 0x2D,
7512/* F */ 0x2E,
7513/* G */ 0x2F,
7514/* H */ 0x16,
7515/* I */ 0x05,
7516/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
7517/* J */ 0x15,
7518/* K */ 0x0B,
7519/* L */ 0x0C,
7520/* M */ 0x0D,
7521/* N */ 0x0E,
7522/* O */ 0x0F,
7523/* P */ 0x10,
7524/* Q */ 0x11,
7525/* R */ 0x12,
7526/* DA - DF */ 0, 0, 0, 0, 0, 0,
7527/* \ */ 0x1C,
7528/* E1 */ 0,
7529/* S */ 0x13,
7530/* T */ 0x3C,
7531/* U */ 0x3D,
7532/* V */ 0x32,
7533/* W */ 0x26,
7534/* X */ 0x18,
7535/* Y */ 0x19,
7536/* Z */ 0x3F,
7537/* EA - FF*/ 0, 0, 0, 0, 0, 0,
7538 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7539};
7540
7541char MetaCharTable[]=
7542{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7543 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
7544 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
7545 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
7546 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
7547};
7548
7549
7550/* TODO: Use characters NOT numbers!!! */
7551char CtrlCharTable[]=
7552{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7553 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
7554 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
7555 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
7556 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
7557};
7558
7559
7560#endif