blob: df045a50f697c382fa570fbd1c4fae1d0de98f40 [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 Moolenaar67c53842010-05-22 18:28:27 +0200391#ifdef FEAT_NETBEANS_INTG
392 /* Process the queued netbeans messages. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200393 netbeans_parse_messages();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200394#endif
395
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 /* Check if window changed size while we were busy, perhaps the ":set
397 * columns=99" command was used. */
398 while (do_resize)
399 handle_resize();
400
401 if (wtime >= 0)
402 {
403 while (WaitForChar(wtime) == 0) /* no character available */
404 {
405 if (!do_resize) /* return if not interrupted by resize */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200408#ifdef FEAT_NETBEANS_INTG
409 /* Process the queued netbeans messages. */
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200410 netbeans_parse_messages();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 }
413 }
414 else /* wtime == -1 */
415 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 /*
417 * If there is no character available within 'updatetime' seconds
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000418 * flush all the swap files to disk.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 * Also done when interrupted by SIGWINCH.
420 */
421 if (WaitForChar(p_ut) == 0)
422 {
423#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +0000424 if (trigger_cursorhold() && maxlen >= 3
425 && !typebuf_changed(tb_change_cnt))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 {
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000427 buf[0] = K_SPECIAL;
428 buf[1] = KS_EXTRA;
429 buf[2] = (int)KE_CURSORHOLD;
430 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#endif
Bram Moolenaard4098f52005-06-27 22:37:13 +0000433 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 }
435 }
436
437 for (;;) /* repeat until we got a character */
438 {
439 while (do_resize) /* window changed size */
440 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200441
442#ifdef FEAT_NETBEANS_INTG
443 /* Process the queued netbeans messages. */
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200444 netbeans_parse_messages();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200447 * We want to be interrupted by the winch signal
448 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200450 if (WaitForChar(-1L) == 0)
451 {
452 if (do_resize) /* interrupted by SIGWINCH signal */
453 handle_resize();
454 return 0;
455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456
457 /* If input was put directly in typeahead buffer bail out here. */
458 if (typebuf_changed(tb_change_cnt))
459 return 0;
460
461 /*
462 * For some terminals we only get one character at a time.
463 * We want the get all available characters, so we could keep on
464 * trying until none is available
465 * For some other terminals this is quite slow, that's why we don't do
466 * it.
467 */
468 len = read_from_input_buf(buf, (long)maxlen);
469 if (len > 0)
470 {
471#ifdef OS2
472 int i;
473
474 for (i = 0; i < len; i++)
475 if (buf[i] == 0)
476 buf[i] = K_NUL;
477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 return len;
479 }
480 }
481}
482
483 static void
484handle_resize()
485{
486 do_resize = FALSE;
487 shell_resized();
488}
489
490/*
491 * return non-zero if a character is available
492 */
493 int
494mch_char_avail()
495{
496 return WaitForChar(0L);
497}
498
499#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
500# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000501# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502# endif
503# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
504# include <sys/sysctl.h>
505# endif
506# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
507# include <sys/sysinfo.h>
508# endif
509
510/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000511 * Return total amount of memory available in Kbyte.
512 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 long_u
515mch_total_mem(special)
Bram Moolenaar78a15312009-05-15 19:33:18 +0000516 int special UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
518# ifdef __EMX__
Bram Moolenaar914572a2007-05-01 11:37:47 +0000519 return ulimit(3, 0L) >> 10; /* always 32MB? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520# else
521 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000522 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523
524# ifdef HAVE_SYSCTL
525 int mib[2], physmem;
526 size_t len;
527
528 /* BSD way of getting the amount of RAM available. */
529 mib[0] = CTL_HW;
530 mib[1] = HW_USERMEM;
531 len = sizeof(physmem);
532 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
533 mem = (long_u)physmem;
534# endif
535
536# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
537 if (mem == 0)
538 {
539 struct sysinfo sinfo;
540
541 /* Linux way of getting amount of RAM available */
542 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000543 {
544# ifdef HAVE_SYSINFO_MEM_UNIT
545 /* avoid overflow as much as possible */
546 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
547 {
548 sinfo.mem_unit = sinfo.mem_unit >> 1;
549 --shiftright;
550 }
551 mem = sinfo.totalram * sinfo.mem_unit;
552# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 mem = sinfo.totalram;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000554# endif
555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 }
557# endif
558
559# ifdef HAVE_SYSCONF
560 if (mem == 0)
561 {
562 long pagesize, pagecount;
563
564 /* Solaris way of getting amount of RAM available */
565 pagesize = sysconf(_SC_PAGESIZE);
566 pagecount = sysconf(_SC_PHYS_PAGES);
567 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000568 {
569 /* avoid overflow as much as possible */
570 while (shiftright > 0 && (pagesize & 1) == 0)
571 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000572 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000573 --shiftright;
574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
578# endif
579
580 /* Return the minimum of the physical memory and the user limit, because
581 * using more than the user limit may cause Vim to be terminated. */
582# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
583 {
584 struct rlimit rlp;
585
586 if (getrlimit(RLIMIT_DATA, &rlp) == 0
587 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
588# ifdef RLIM_INFINITY
589 && rlp.rlim_cur != RLIM_INFINITY
590# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000591 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000593 {
594 mem = (long_u)rlp.rlim_cur;
595 shiftright = 10;
596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 }
598# endif
599
600 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000601 return mem >> shiftright;
602 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603# endif
604}
605#endif
606
607 void
608mch_delay(msec, ignoreinput)
609 long msec;
610 int ignoreinput;
611{
612 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000613#ifdef FEAT_MZSCHEME
614 long total = msec; /* remember original value */
615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617 if (ignoreinput)
618 {
619 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000620 * here. But we don't want QUIT to kill us (CTRL-\ used in a
621 * shell may produce SIGQUIT). */
622 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 old_tmode = curr_tmode;
624 if (curr_tmode == TMODE_RAW)
625 settmode(TMODE_SLEEP);
626
627 /*
628 * Everybody sleeps in a different way...
629 * Prefer nanosleep(), some versions of usleep() can only sleep up to
630 * one second.
631 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000632#ifdef FEAT_MZSCHEME
633 do
634 {
635 /* if total is large enough, wait by portions in p_mzq */
636 if (total > p_mzq)
637 msec = p_mzq;
638 else
639 msec = total;
640 total -= msec;
641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#ifdef HAVE_NANOSLEEP
643 {
644 struct timespec ts;
645
646 ts.tv_sec = msec / 1000;
647 ts.tv_nsec = (msec % 1000) * 1000000;
648 (void)nanosleep(&ts, NULL);
649 }
650#else
651# ifdef HAVE_USLEEP
652 while (msec >= 1000)
653 {
654 usleep((unsigned int)(999 * 1000));
655 msec -= 999;
656 }
657 usleep((unsigned int)(msec * 1000));
658# else
659# ifndef HAVE_SELECT
660 poll(NULL, 0, (int)msec);
661# else
662# ifdef __EMX__
663 _sleep2(msec);
664# else
665 {
666 struct timeval tv;
667
668 tv.tv_sec = msec / 1000;
669 tv.tv_usec = (msec % 1000) * 1000;
670 /*
671 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
672 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
673 */
674 select(0, NULL, NULL, NULL, &tv);
675 }
676# endif /* __EMX__ */
677# endif /* HAVE_SELECT */
678# endif /* HAVE_NANOSLEEP */
679#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000680#ifdef FEAT_MZSCHEME
681 }
682 while (total > 0);
683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
685 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000686 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688 else
689 WaitForChar(msec);
690}
691
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000692#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
694# define HAVE_CHECK_STACK_GROWTH
695/*
696 * Support for checking for an almost-out-of-stack-space situation.
697 */
698
699/*
700 * Return a pointer to an item on the stack. Used to find out if the stack
701 * grows up or down.
702 */
703static void check_stack_growth __ARGS((char *p));
704static int stack_grows_downwards;
705
706/*
707 * Find out if the stack grows upwards or downwards.
708 * "p" points to a variable on the stack of the caller.
709 */
710 static void
711check_stack_growth(p)
712 char *p;
713{
714 int i;
715
716 stack_grows_downwards = (p > (char *)&i);
717}
718#endif
719
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000720#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721static char *stack_limit = NULL;
722
723#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
724# include <pthread.h>
725# include <pthread_np.h>
726#endif
727
728/*
729 * Find out until how var the stack can grow without getting into trouble.
730 * Called when starting up and when switching to the signal stack in
731 * deathtrap().
732 */
733 static void
734get_stack_limit()
735{
736 struct rlimit rlp;
737 int i;
738 long lim;
739
740 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
741 * limit doesn't fit in a long (rlim_cur might be "long long"). */
742 if (getrlimit(RLIMIT_STACK, &rlp) == 0
743 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
744# ifdef RLIM_INFINITY
745 && rlp.rlim_cur != RLIM_INFINITY
746# endif
747 )
748 {
749 lim = (long)rlp.rlim_cur;
750#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
751 {
752 pthread_attr_t attr;
753 size_t size;
754
755 /* On FreeBSD the initial thread always has a fixed stack size, no
756 * matter what the limits are set to. Normally it's 1 Mbyte. */
757 pthread_attr_init(&attr);
758 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
759 {
760 pthread_attr_getstacksize(&attr, &size);
761 if (lim > (long)size)
762 lim = (long)size;
763 }
764 pthread_attr_destroy(&attr);
765 }
766#endif
767 if (stack_grows_downwards)
768 {
769 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
770 if (stack_limit >= (char *)&i)
771 /* overflow, set to 1/16 of current stack position */
772 stack_limit = (char *)((long)&i / 16L);
773 }
774 else
775 {
776 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
777 if (stack_limit <= (char *)&i)
778 stack_limit = NULL; /* overflow */
779 }
780 }
781}
782
783/*
784 * Return FAIL when running out of stack space.
785 * "p" must point to any variable local to the caller that's on the stack.
786 */
787 int
788mch_stackcheck(p)
789 char *p;
790{
791 if (stack_limit != NULL)
792 {
793 if (stack_grows_downwards)
794 {
795 if (p < stack_limit)
796 return FAIL;
797 }
798 else if (p > stack_limit)
799 return FAIL;
800 }
801 return OK;
802}
803#endif
804
805#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
806/*
807 * Support for using the signal stack.
808 * This helps when we run out of stack space, which causes a SIGSEGV. The
809 * signal handler then must run on another stack, since the normal stack is
810 * completely full.
811 */
812
Bram Moolenaar39766a72013-11-03 00:41:00 +0100813#if defined(HAVE_AVAILABILITYMACROS_H)
Bram Moolenaar4cc95d12013-11-02 21:49:32 +0100814# include <AvailabilityMacros.h>
815#endif
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817#ifndef SIGSTKSZ
818# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
819#endif
820
821# ifdef HAVE_SIGALTSTACK
822static stack_t sigstk; /* for sigaltstack() */
823# else
824static struct sigstack sigstk; /* for sigstack() */
825# endif
826
827static void init_signal_stack __ARGS((void));
828static char *signal_stack;
829
830 static void
831init_signal_stack()
832{
833 if (signal_stack != NULL)
834 {
835# ifdef HAVE_SIGALTSTACK
Bram Moolenaar1a3d0862007-08-30 09:47:38 +0000836# if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \
837 || MAC_OS_X_VERSION_MAX_ALLOWED <= 1040)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 /* missing prototype. Adding it to osdef?.h.in doesn't work, because
839 * "struct sigaltstack" needs to be declared. */
840 extern int sigaltstack __ARGS((const struct sigaltstack *ss, struct sigaltstack *oss));
841# endif
842
843# ifdef HAVE_SS_BASE
844 sigstk.ss_base = signal_stack;
845# else
846 sigstk.ss_sp = signal_stack;
847# endif
848 sigstk.ss_size = SIGSTKSZ;
849 sigstk.ss_flags = 0;
850 (void)sigaltstack(&sigstk, NULL);
851# else
852 sigstk.ss_sp = signal_stack;
853 if (stack_grows_downwards)
854 sigstk.ss_sp += SIGSTKSZ - 1;
855 sigstk.ss_onstack = 0;
856 (void)sigstack(&sigstk, NULL);
857# endif
858 }
859}
860#endif
861
862/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000863 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * will barf when the second argument to signal() is ``wrong''.
865 * Let me try it with a few tricky defines from my own osdef.h (jw).
866 */
867#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 static RETSIGTYPE
869sig_winch SIGDEFARG(sigarg)
870{
871 /* this is not required on all systems, but it doesn't hurt anybody */
872 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
873 do_resize = TRUE;
874 SIGRETURN;
875}
876#endif
877
878#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 static RETSIGTYPE
880catch_sigint SIGDEFARG(sigarg)
881{
882 /* this is not required on all systems, but it doesn't hurt anybody */
883 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
884 got_int = TRUE;
885 SIGRETURN;
886}
887#endif
888
889#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 static RETSIGTYPE
891catch_sigpwr SIGDEFARG(sigarg)
892{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000893 /* this is not required on all systems, but it doesn't hurt anybody */
894 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 /*
896 * I'm not sure we get the SIGPWR signal when the system is really going
897 * down or when the batteries are almost empty. Just preserve the swap
898 * files and don't exit, that can't do any harm.
899 */
900 ml_sync_all(FALSE, FALSE);
901 SIGRETURN;
902}
903#endif
904
905#ifdef SET_SIG_ALARM
906/*
907 * signal function for alarm().
908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 static RETSIGTYPE
910sig_alarm SIGDEFARG(sigarg)
911{
912 /* doesn't do anything, just to break a system call */
913 sig_alarm_called = TRUE;
914 SIGRETURN;
915}
916#endif
917
Bram Moolenaar44ecf652005-03-07 23:09:59 +0000918#if (defined(HAVE_SETJMP_H) \
919 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
920 || defined(FEAT_LIBCALL))) \
921 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922/*
923 * A simplistic version of setjmp() that only allows one level of using.
924 * Don't call twice before calling mch_endjmp()!.
925 * Usage:
926 * mch_startjmp();
927 * if (SETJMP(lc_jump_env) != 0)
928 * {
929 * mch_didjmp();
930 * EMSG("crash!");
931 * }
932 * else
933 * {
934 * do_the_work;
935 * mch_endjmp();
936 * }
937 * Note: Can't move SETJMP() here, because a function calling setjmp() must
938 * not return before the saved environment is used.
939 * Returns OK for normal return, FAIL when the protected code caused a
940 * problem and LONGJMP() was used.
941 */
942 void
943mch_startjmp()
944{
945#ifdef SIGHASARG
946 lc_signal = 0;
947#endif
948 lc_active = TRUE;
949}
950
951 void
952mch_endjmp()
953{
954 lc_active = FALSE;
955}
956
957 void
958mch_didjmp()
959{
960# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
961 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
962 * otherwise catching the signal only works once. */
963 init_signal_stack();
964# endif
965}
966#endif
967
968/*
969 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200970 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200972 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
973 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 */
975 static RETSIGTYPE
976deathtrap SIGDEFARG(sigarg)
977{
978 static int entered = 0; /* count the number of times we got here.
979 Note: when memory has been corrupted
980 this may get an arbitrary value! */
981#ifdef SIGHASARG
982 int i;
983#endif
984
985#if defined(HAVE_SETJMP_H)
986 /*
987 * Catch a crash in protected code.
988 * Restores the environment saved in lc_jump_env, which looks like
989 * SETJMP() returns 1.
990 */
991 if (lc_active)
992 {
993# if defined(SIGHASARG)
994 lc_signal = sigarg;
995# endif
996 lc_active = FALSE; /* don't jump again */
997 LONGJMP(lc_jump_env, 1);
998 /* NOTREACHED */
999 }
1000#endif
1001
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001002#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001003# ifdef SIGQUIT
1004 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1005 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1006 * pressing CTRL-\, but we don't want Vim to exit then. */
1007 if (in_mch_delay && sigarg == SIGQUIT)
1008 SIGRETURN;
1009# endif
1010
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001011 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1012 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1013 * free(). Calling free() again may then cause a crash. */
1014 if (entered == 0
1015 && (0
1016# ifdef SIGHUP
1017 || sigarg == SIGHUP
1018# endif
1019# ifdef SIGQUIT
1020 || sigarg == SIGQUIT
1021# endif
1022# ifdef SIGTERM
1023 || sigarg == SIGTERM
1024# endif
1025# ifdef SIGPWR
1026 || sigarg == SIGPWR
1027# endif
1028# ifdef SIGUSR1
1029 || sigarg == SIGUSR1
1030# endif
1031# ifdef SIGUSR2
1032 || sigarg == SIGUSR2
1033# endif
1034 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001035 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001036 SIGRETURN;
1037#endif
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 /* Remember how often we have been called. */
1040 ++entered;
1041
1042#ifdef FEAT_EVAL
1043 /* Set the v:dying variable. */
1044 set_vim_var_nr(VV_DYING, (long)entered);
1045#endif
1046
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001047#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 /* Since we are now using the signal stack, need to reset the stack
1049 * limit. Otherwise using a regexp will fail. */
1050 get_stack_limit();
1051#endif
1052
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001053#if 0
1054 /* This is for opening gdb the moment Vim crashes.
1055 * You need to manually adjust the file name and Vim executable name.
1056 * Suggested by SungHyun Nam. */
1057 {
1058# define VI_GDB_FILE "/tmp/vimgdb"
1059# define VIM_NAME "/usr/bin/vim"
1060 FILE *fp = fopen(VI_GDB_FILE, "w");
1061 if (fp)
1062 {
1063 fprintf(fp,
1064 "file %s\n"
1065 "attach %d\n"
1066 "set height 1000\n"
1067 "bt full\n"
1068 , VIM_NAME, getpid());
1069 fclose(fp);
1070 system("xterm -e gdb -x "VI_GDB_FILE);
1071 unlink(VI_GDB_FILE);
1072 }
1073 }
1074#endif
1075
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#ifdef SIGHASARG
1077 /* try to find the name of this signal */
1078 for (i = 0; signal_info[i].sig != -1; i++)
1079 if (sigarg == signal_info[i].sig)
1080 break;
1081 deadly_signal = sigarg;
1082#endif
1083
1084 full_screen = FALSE; /* don't write message to the GUI, it might be
1085 * part of the problem... */
1086 /*
1087 * If something goes wrong after entering here, we may get here again.
1088 * When this happens, give a message and try to exit nicely (resetting the
1089 * terminal mode, etc.)
1090 * When this happens twice, just exit, don't even try to give a message,
1091 * stack may be corrupt or something weird.
1092 * When this still happens again (or memory was corrupted in such a way
1093 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1094 */
1095 if (entered >= 3)
1096 {
1097 reset_signals(); /* don't catch any signals anymore */
1098 may_core_dump();
1099 if (entered >= 4)
1100 _exit(8);
1101 exit(7);
1102 }
1103 if (entered == 2)
1104 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001105 /* No translation, it may call malloc(). */
1106 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 out_flush();
1108 getout(1);
1109 }
1110
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001111 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001113 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 signal_info[i].name);
1115#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001116 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001118
1119 /* Preserve files and exit. This sets the really_exiting flag to prevent
1120 * calling free(). */
1121 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122
Bram Moolenaar009b2592004-10-24 19:18:58 +00001123#ifdef NBDEBUG
1124 reset_signals();
1125 may_core_dump();
1126 abort();
1127#endif
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 SIGRETURN;
1130}
1131
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001132#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133/*
1134 * On Solaris with multi-threading, suspending might not work immediately.
1135 * Catch the SIGCONT signal, which will be used as an indication whether the
1136 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001137 *
1138 * On Linux, signal is not always handled immediately either.
1139 * See https://bugs.launchpad.net/bugs/291373
1140 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001141 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001143static volatile int sigcont_received;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144static RETSIGTYPE sigcont_handler __ARGS(SIGPROTOARG);
1145
1146/*
1147 * signal handler for SIGCONT
1148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 static RETSIGTYPE
1150sigcont_handler SIGDEFARG(sigarg)
1151{
1152 sigcont_received = TRUE;
1153 SIGRETURN;
1154}
1155#endif
1156
Bram Moolenaar62b42182010-09-21 22:09:37 +02001157# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
1158static void loose_clipboard __ARGS((void));
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001159# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001160static void save_clipboard __ARGS((void));
1161static void restore_clipboard __ARGS((void));
1162
1163static void *clip_star_save = NULL;
1164static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001165# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001166
1167/*
1168 * Called when Vim is going to sleep or execute a shell command.
1169 * We can't respond to requests for the X selections. Lose them, otherwise
1170 * other applications will hang. But first copy the text to cut buffer 0.
1171 */
1172 static void
1173loose_clipboard()
1174{
1175 if (clip_star.owned || clip_plus.owned)
1176 {
1177 x11_export_final_selection();
1178 if (clip_star.owned)
1179 clip_lose_selection(&clip_star);
1180 if (clip_plus.owned)
1181 clip_lose_selection(&clip_plus);
1182 if (x11_display != NULL)
1183 XFlush(x11_display);
1184 }
1185}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001186
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001187# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001188/*
1189 * Save clipboard text to restore later.
1190 */
1191 static void
1192save_clipboard()
1193{
1194 if (clip_star.owned)
1195 clip_star_save = get_register('*', TRUE);
1196 if (clip_plus.owned)
1197 clip_plus_save = get_register('+', TRUE);
1198}
1199
1200/*
1201 * Restore clipboard text if no one own the X selection.
1202 */
1203 static void
1204restore_clipboard()
1205{
1206 if (clip_star_save != NULL)
1207 {
1208 if (!clip_gen_owner_exists(&clip_star))
1209 put_register('*', clip_star_save);
1210 else
1211 free_register(clip_star_save);
1212 clip_star_save = NULL;
1213 }
1214 if (clip_plus_save != NULL)
1215 {
1216 if (!clip_gen_owner_exists(&clip_plus))
1217 put_register('+', clip_plus_save);
1218 else
1219 free_register(clip_plus_save);
1220 clip_plus_save = NULL;
1221 }
1222}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001223# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001224#endif
1225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226/*
1227 * If the machine has job control, use it to suspend the program,
1228 * otherwise fake it by starting a new shell.
1229 */
1230 void
1231mch_suspend()
1232{
1233 /* BeOS does have SIGTSTP, but it doesn't work. */
1234#if defined(SIGTSTP) && !defined(__BEOS__)
1235 out_flush(); /* needed to make cursor visible on some systems */
1236 settmode(TMODE_COOK);
1237 out_flush(); /* needed to disable mouse on some systems */
1238
1239# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001240 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241# endif
1242
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001243# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 sigcont_received = FALSE;
1245# endif
1246 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001247# if defined(_REENTRANT) && defined(SIGCONT)
1248 /*
1249 * Wait for the SIGCONT signal to be handled. It generally happens
1250 * immediately, but somehow not all the time. Do not call pause()
1251 * because there would be race condition which would hang Vim if
1252 * signal happened in between the test of sigcont_received and the
1253 * call to pause(). If signal is not yet received, call sleep(0)
1254 * to just yield CPU. Signal should then be received. If somehow
1255 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1256 * further if signal is not received after 1+2+3+4 ms (not expected
1257 * to happen).
1258 */
1259 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001260 long wait_time;
1261 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001262 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001263 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265# endif
1266
1267# ifdef FEAT_TITLE
1268 /*
1269 * Set oldtitle to NULL, so the current title is obtained again.
1270 */
1271 vim_free(oldtitle);
1272 oldtitle = NULL;
1273# endif
1274 settmode(TMODE_RAW);
1275 need_check_timestamps = TRUE;
1276 did_check_timestamps = FALSE;
1277#else
1278 suspend_shell();
1279#endif
1280}
1281
1282 void
1283mch_init()
1284{
1285 Columns = 80;
1286 Rows = 24;
1287
1288 out_flush();
1289 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001290
Bram Moolenaar56718732006-03-15 22:53:57 +00001291#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001292 mac_conv_init();
1293#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001294#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1295 win_clip_init();
1296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297}
1298
1299 static void
1300set_signals()
1301{
1302#if defined(SIGWINCH)
1303 /*
1304 * WINDOW CHANGE signal is handled with sig_winch().
1305 */
1306 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1307#endif
1308
1309 /*
1310 * We want the STOP signal to work, to make mch_suspend() work.
1311 * For "rvim" the STOP signal is ignored.
1312 */
1313#ifdef SIGTSTP
1314 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1315#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001316#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 signal(SIGCONT, sigcont_handler);
1318#endif
1319
1320 /*
1321 * We want to ignore breaking of PIPEs.
1322 */
1323#ifdef SIGPIPE
1324 signal(SIGPIPE, SIG_IGN);
1325#endif
1326
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001328 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329#endif
1330
1331 /*
1332 * Ignore alarm signals (Perl's alarm() generates it).
1333 */
1334#ifdef SIGALRM
1335 signal(SIGALRM, SIG_IGN);
1336#endif
1337
1338 /*
1339 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1340 * work will be lost.
1341 */
1342#ifdef SIGPWR
1343 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1344#endif
1345
1346 /*
1347 * Arrange for other signals to gracefully shutdown Vim.
1348 */
1349 catch_signals(deathtrap, SIG_ERR);
1350
1351#if defined(FEAT_GUI) && defined(SIGHUP)
1352 /*
1353 * When the GUI is running, ignore the hangup signal.
1354 */
1355 if (gui.in_use)
1356 signal(SIGHUP, SIG_IGN);
1357#endif
1358}
1359
Bram Moolenaardf177f62005-02-22 08:39:57 +00001360#if defined(SIGINT) || defined(PROTO)
1361/*
1362 * Catch CTRL-C (only works while in Cooked mode).
1363 */
1364 static void
1365catch_int_signal()
1366{
1367 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1368}
1369#endif
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 void
1372reset_signals()
1373{
1374 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001375#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 /* SIGCONT isn't in the list, because its default action is ignore */
1377 signal(SIGCONT, SIG_DFL);
1378#endif
1379}
1380
1381 static void
1382catch_signals(func_deadly, func_other)
1383 RETSIGTYPE (*func_deadly)();
1384 RETSIGTYPE (*func_other)();
1385{
1386 int i;
1387
1388 for (i = 0; signal_info[i].sig != -1; i++)
1389 if (signal_info[i].deadly)
1390 {
1391#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1392 struct sigaction sa;
1393
1394 /* Setup to use the alternate stack for the signal function. */
1395 sa.sa_handler = func_deadly;
1396 sigemptyset(&sa.sa_mask);
1397# if defined(__linux__) && defined(_REENTRANT)
1398 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1399 * thread handling in combination with using the alternate stack:
1400 * pthread library functions try to use the stack pointer to
1401 * identify the current thread, causing a SEGV signal, which
1402 * recursively calls deathtrap() and hangs. */
1403 sa.sa_flags = 0;
1404# else
1405 sa.sa_flags = SA_ONSTACK;
1406# endif
1407 sigaction(signal_info[i].sig, &sa, NULL);
1408#else
1409# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1410 struct sigvec sv;
1411
1412 /* Setup to use the alternate stack for the signal function. */
1413 sv.sv_handler = func_deadly;
1414 sv.sv_mask = 0;
1415 sv.sv_flags = SV_ONSTACK;
1416 sigvec(signal_info[i].sig, &sv, NULL);
1417# else
1418 signal(signal_info[i].sig, func_deadly);
1419# endif
1420#endif
1421 }
1422 else if (func_other != SIG_ERR)
1423 signal(signal_info[i].sig, func_other);
1424}
1425
1426/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001427 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001428 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1429 * return TRUE
1430 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1431 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001432 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001433 * Returns TRUE when Vim should exit.
1434 */
1435 int
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001436vim_handle_signal(sig)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001437 int sig;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001438{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001439 static int got_signal = 0;
1440 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001441
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001442 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001443 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001444 case SIGNAL_BLOCK: blocked = TRUE;
1445 break;
1446
1447 case SIGNAL_UNBLOCK: blocked = FALSE;
1448 if (got_signal != 0)
1449 {
1450 kill(getpid(), got_signal);
1451 got_signal = 0;
1452 }
1453 break;
1454
1455 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001456 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001457 got_signal = sig;
1458#ifdef SIGPWR
1459 if (sig != SIGPWR)
1460#endif
1461 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001462 break;
1463 }
1464 return FALSE;
1465}
1466
1467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 * Check_win checks whether we have an interactive stdout.
1469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 int
1471mch_check_win(argc, argv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001472 int argc UNUSED;
1473 char **argv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
1475#ifdef OS2
1476 /*
1477 * Store argv[0], may be used for $VIM. Only use it if it is an absolute
1478 * name, mostly it's just "vim" and found in the path, which is unusable.
1479 */
1480 if (mch_isFullName(argv[0]))
1481 exe_name = vim_strsave((char_u *)argv[0]);
1482#endif
1483 if (isatty(1))
1484 return OK;
1485 return FAIL;
1486}
1487
1488/*
1489 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1490 */
1491 int
1492mch_input_isatty()
1493{
1494 if (isatty(read_cmd_fd))
1495 return TRUE;
1496 return FALSE;
1497}
1498
1499#ifdef FEAT_X11
1500
1501# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) \
1502 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1503
1504static void xopen_message __ARGS((struct timeval *tvp));
1505
1506/*
1507 * Give a message about the elapsed time for opening the X window.
1508 */
1509 static void
1510xopen_message(tvp)
1511 struct timeval *tvp; /* must contain start time */
1512{
1513 struct timeval end_tv;
1514
1515 /* Compute elapsed time. */
1516 gettimeofday(&end_tv, NULL);
1517 smsg((char_u *)_("Opening the X display took %ld msec"),
1518 (end_tv.tv_sec - tvp->tv_sec) * 1000L
Bram Moolenaar051b7822005-05-19 21:00:46 +00001519 + (end_tv.tv_usec - tvp->tv_usec) / 1000L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520}
1521# endif
1522#endif
1523
1524#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1525/*
1526 * A few functions shared by X11 title and clipboard code.
1527 */
1528static int x_error_handler __ARGS((Display *dpy, XErrorEvent *error_event));
1529static int x_error_check __ARGS((Display *dpy, XErrorEvent *error_event));
1530static int x_connect_to_server __ARGS((void));
1531static int test_x11_window __ARGS((Display *dpy));
1532
1533static int got_x_error = FALSE;
1534
1535/*
1536 * X Error handler, otherwise X just exits! (very rude) -- webb
1537 */
1538 static int
1539x_error_handler(dpy, error_event)
1540 Display *dpy;
1541 XErrorEvent *error_event;
1542{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001543 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 STRCAT(IObuff, _("\nVim: Got X error\n"));
1545
1546 /* We cannot print a message and continue, because no X calls are allowed
1547 * here (causes my system to hang). Silently continuing might be an
1548 * alternative... */
1549 preserve_exit(); /* preserve files and exit */
1550
1551 return 0; /* NOTREACHED */
1552}
1553
1554/*
1555 * Another X Error handler, just used to check for errors.
1556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 static int
1558x_error_check(dpy, error_event)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001559 Display *dpy UNUSED;
1560 XErrorEvent *error_event UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562 got_x_error = TRUE;
1563 return 0;
1564}
1565
1566#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1567# if defined(HAVE_SETJMP_H)
1568/*
1569 * An X IO Error handler, used to catch error while opening the display.
1570 */
1571static int x_IOerror_check __ARGS((Display *dpy));
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 static int
1574x_IOerror_check(dpy)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001575 Display *dpy UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
1577 /* This function should not return, it causes exit(). Longjump instead. */
1578 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001579# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001580 return 0; /* avoid the compiler complains about missing return value */
1581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582}
1583# endif
1584
1585/*
1586 * An X IO Error handler, used to catch terminal errors.
1587 */
1588static int x_IOerror_handler __ARGS((Display *dpy));
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001589static void may_restore_clipboard __ARGS((void));
1590static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 static int
1593x_IOerror_handler(dpy)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001594 Display *dpy UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001597 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 x11_window = 0;
1599 x11_display = NULL;
1600 xterm_Shell = (Widget)0;
1601
1602 /* This function should not return, it causes exit(). Longjump instead. */
1603 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001604# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001605 return 0; /* avoid the compiler complains about missing return value */
1606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001608
1609/*
1610 * If the X11 connection was lost try to restore it.
1611 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001612 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001613 */
1614 static void
1615may_restore_clipboard()
1616{
1617 if (xterm_dpy_was_reset)
1618 {
1619 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001620
1621# ifndef LESSTIF_VERSION
1622 /* This has been reported to avoid Vim getting stuck. */
1623 if (app_context != (XtAppContext)NULL)
1624 {
1625 XtDestroyApplicationContext(app_context);
1626 app_context = (XtAppContext)NULL;
1627 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1628 }
1629# endif
1630
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001631 setup_term_clip();
1632 get_x11_title(FALSE);
1633 }
1634}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#endif
1636
1637/*
1638 * Return TRUE when connection to the X server is desired.
1639 */
1640 static int
1641x_connect_to_server()
1642{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643#if defined(FEAT_CLIENTSERVER)
1644 if (x_force_connect)
1645 return TRUE;
1646#endif
1647 if (x_no_connect)
1648 return FALSE;
1649
1650 /* Check for a match with "exclude:" from 'clipboard'. */
1651 if (clip_exclude_prog != NULL)
1652 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001653 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 return FALSE;
1655 }
1656 return TRUE;
1657}
1658
1659/*
1660 * Test if "dpy" and x11_window are valid by getting the window title.
1661 * I don't actually want it yet, so there may be a simpler call to use, but
1662 * this will cause the error handler x_error_check() to be called if anything
1663 * is wrong, such as the window pointer being invalid (as can happen when the
1664 * user changes his DISPLAY, but not his WINDOWID) -- webb
1665 */
1666 static int
1667test_x11_window(dpy)
1668 Display *dpy;
1669{
1670 int (*old_handler)();
1671 XTextProperty text_prop;
1672
1673 old_handler = XSetErrorHandler(x_error_check);
1674 got_x_error = FALSE;
1675 if (XGetWMName(dpy, x11_window, &text_prop))
1676 XFree((void *)text_prop.value);
1677 XSync(dpy, False);
1678 (void)XSetErrorHandler(old_handler);
1679
1680 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001681 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 return (got_x_error ? FAIL : OK);
1684}
1685#endif
1686
1687#ifdef FEAT_TITLE
1688
1689#ifdef FEAT_X11
1690
1691static int get_x11_thing __ARGS((int get_title, int test_only));
1692
1693/*
1694 * try to get x11 window and display
1695 *
1696 * return FAIL for failure, OK otherwise
1697 */
1698 static int
1699get_x11_windis()
1700{
1701 char *winid;
1702 static int result = -1;
1703#define XD_NONE 0 /* x11_display not set here */
1704#define XD_HERE 1 /* x11_display opened here */
1705#define XD_GUI 2 /* x11_display used from gui.dpy */
1706#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1707 static int x11_display_from = XD_NONE;
1708 static int did_set_error_handler = FALSE;
1709
1710 if (!did_set_error_handler)
1711 {
1712 /* X just exits if it finds an error otherwise! */
1713 (void)XSetErrorHandler(x_error_handler);
1714 did_set_error_handler = TRUE;
1715 }
1716
Bram Moolenaar9372a112005-12-06 19:59:18 +00001717#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (gui.in_use)
1719 {
1720 /*
1721 * If the X11 display was opened here before, for the window where Vim
1722 * was started, close that one now to avoid a memory leak.
1723 */
1724 if (x11_display_from == XD_HERE && x11_display != NULL)
1725 {
1726 XCloseDisplay(x11_display);
1727 x11_display_from = XD_NONE;
1728 }
1729 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1730 {
1731 x11_display_from = XD_GUI;
1732 return OK;
1733 }
1734 x11_display = NULL;
1735 return FAIL;
1736 }
1737 else if (x11_display_from == XD_GUI)
1738 {
1739 /* GUI must have stopped somehow, clear x11_display */
1740 x11_window = 0;
1741 x11_display = NULL;
1742 x11_display_from = XD_NONE;
1743 }
1744#endif
1745
1746 /* When started with the "-X" argument, don't try connecting. */
1747 if (!x_connect_to_server())
1748 return FAIL;
1749
1750 /*
1751 * If WINDOWID not set, should try another method to find out
1752 * what the current window number is. The only code I know for
1753 * this is very complicated.
1754 * We assume that zero is invalid for WINDOWID.
1755 */
1756 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1757 x11_window = (Window)atol(winid);
1758
1759#ifdef FEAT_XCLIPBOARD
1760 if (xterm_dpy != NULL && x11_window != 0)
1761 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001762 /* We may have checked it already, but Gnome terminal can move us to
1763 * another window, so we need to check every time. */
1764 if (x11_display_from != XD_XTERM)
1765 {
1766 /*
1767 * If the X11 display was opened here before, for the window where
1768 * Vim was started, close that one now to avoid a memory leak.
1769 */
1770 if (x11_display_from == XD_HERE && x11_display != NULL)
1771 XCloseDisplay(x11_display);
1772 x11_display = xterm_dpy;
1773 x11_display_from = XD_XTERM;
1774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (test_x11_window(x11_display) == FAIL)
1776 {
1777 /* probably bad $WINDOWID */
1778 x11_window = 0;
1779 x11_display = NULL;
1780 x11_display_from = XD_NONE;
1781 return FAIL;
1782 }
1783 return OK;
1784 }
1785#endif
1786
1787 if (x11_window == 0 || x11_display == NULL)
1788 result = -1;
1789
1790 if (result != -1) /* Have already been here and set this */
1791 return result; /* Don't do all these X calls again */
1792
1793 if (x11_window != 0 && x11_display == NULL)
1794 {
1795#ifdef SET_SIG_ALARM
1796 RETSIGTYPE (*sig_save)();
1797#endif
1798#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1799 struct timeval start_tv;
1800
1801 if (p_verbose > 0)
1802 gettimeofday(&start_tv, NULL);
1803#endif
1804
1805#ifdef SET_SIG_ALARM
1806 /*
1807 * Opening the Display may hang if the DISPLAY setting is wrong, or
1808 * the network connection is bad. Set an alarm timer to get out.
1809 */
1810 sig_alarm_called = FALSE;
1811 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1812 (RETSIGTYPE (*)())sig_alarm);
1813 alarm(2);
1814#endif
1815 x11_display = XOpenDisplay(NULL);
1816
1817#ifdef SET_SIG_ALARM
1818 alarm(0);
1819 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1820 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001821 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#endif
1823 if (x11_display != NULL)
1824 {
1825# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1826 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001827 {
1828 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 xopen_message(&start_tv);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001830 verbose_leave();
1831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832# endif
1833 if (test_x11_window(x11_display) == FAIL)
1834 {
1835 /* Maybe window id is bad */
1836 x11_window = 0;
1837 XCloseDisplay(x11_display);
1838 x11_display = NULL;
1839 }
1840 else
1841 x11_display_from = XD_HERE;
1842 }
1843 }
1844 if (x11_window == 0 || x11_display == NULL)
1845 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001846
1847# ifdef FEAT_EVAL
1848 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1849# endif
1850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 return (result = OK);
1852}
1853
1854/*
1855 * Determine original x11 Window Title
1856 */
1857 static int
1858get_x11_title(test_only)
1859 int test_only;
1860{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001861 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862}
1863
1864/*
1865 * Determine original x11 Window icon
1866 */
1867 static int
1868get_x11_icon(test_only)
1869 int test_only;
1870{
1871 int retval = FALSE;
1872
1873 retval = get_x11_thing(FALSE, test_only);
1874
1875 /* could not get old icon, use terminal name */
1876 if (oldicon == NULL && !test_only)
1877 {
1878 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001879 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001881 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
1883
1884 return retval;
1885}
1886
1887 static int
1888get_x11_thing(get_title, test_only)
1889 int get_title; /* get title string */
1890 int test_only;
1891{
1892 XTextProperty text_prop;
1893 int retval = FALSE;
1894 Status status;
1895
1896 if (get_x11_windis() == OK)
1897 {
1898 /* Get window/icon name if any */
1899 if (get_title)
1900 status = XGetWMName(x11_display, x11_window, &text_prop);
1901 else
1902 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1903
1904 /*
1905 * If terminal is xterm, then x11_window may be a child window of the
1906 * outer xterm window that actually contains the window/icon name, so
1907 * keep traversing up the tree until a window with a title/icon is
1908 * found.
1909 */
1910 /* Previously this was only done for xterm and alikes. I don't see a
1911 * reason why it would fail for other terminal emulators.
1912 * if (term_is_xterm) */
1913 {
1914 Window root;
1915 Window parent;
1916 Window win = x11_window;
1917 Window *children;
1918 unsigned int num_children;
1919
1920 while (!status || text_prop.value == NULL)
1921 {
1922 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1923 &num_children))
1924 break;
1925 if (children)
1926 XFree((void *)children);
1927 if (parent == root || parent == 0)
1928 break;
1929
1930 win = parent;
1931 if (get_title)
1932 status = XGetWMName(x11_display, win, &text_prop);
1933 else
1934 status = XGetWMIconName(x11_display, win, &text_prop);
1935 }
1936 }
1937 if (status && text_prop.value != NULL)
1938 {
1939 retval = TRUE;
1940 if (!test_only)
1941 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001942#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
1943 if (text_prop.encoding == XA_STRING
1944# ifdef FEAT_MBYTE
1945 && !has_mbyte
1946# endif
1947 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
1949#endif
1950 if (get_title)
1951 oldtitle = vim_strsave((char_u *)text_prop.value);
1952 else
1953 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001954#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
1956 else
1957 {
1958 char **cl;
1959 Status transform_status;
1960 int n = 0;
1961
1962 transform_status = XmbTextPropertyToTextList(x11_display,
1963 &text_prop,
1964 &cl, &n);
1965 if (transform_status >= Success && n > 0 && cl[0])
1966 {
1967 if (get_title)
1968 oldtitle = vim_strsave((char_u *) cl[0]);
1969 else
1970 oldicon = vim_strsave((char_u *) cl[0]);
1971 XFreeStringList(cl);
1972 }
1973 else
1974 {
1975 if (get_title)
1976 oldtitle = vim_strsave((char_u *)text_prop.value);
1977 else
1978 oldicon = vim_strsave((char_u *)text_prop.value);
1979 }
1980 }
1981#endif
1982 }
1983 XFree((void *)text_prop.value);
1984 }
1985 }
1986 return retval;
1987}
1988
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02001989/* Xutf8 functions are not avaialble on older systems. Note that on some
1990 * systems X_HAVE_UTF8_STRING may be defined in a header file but
1991 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
1992 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02001994# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995# define USE_UTF8_STRING
1996# endif
1997#endif
1998
1999/*
2000 * Set x11 Window Title
2001 *
2002 * get_x11_windis() must be called before this and have returned OK
2003 */
2004 static void
2005set_x11_title(title)
2006 char_u *title;
2007{
2008 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2009 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2010 * supported everywhere and STRING doesn't work for multi-byte titles.
2011 */
2012#ifdef USE_UTF8_STRING
2013 if (enc_utf8)
2014 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2015 NULL, NULL, 0, NULL, NULL, NULL);
2016 else
2017#endif
2018 {
2019#if XtSpecificationRelease >= 4
2020# ifdef FEAT_XFONTSET
2021 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2022 NULL, NULL, 0, NULL, NULL, NULL);
2023# else
2024 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002025 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002028 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 XSetWMProperties(x11_display, x11_window, &text_prop,
2030 NULL, NULL, 0, NULL, NULL, NULL);
2031# endif
2032#else
2033 XStoreName(x11_display, x11_window, (char *)title);
2034#endif
2035 }
2036 XFlush(x11_display);
2037}
2038
2039/*
2040 * Set x11 Window icon
2041 *
2042 * get_x11_windis() must be called before this and have returned OK
2043 */
2044 static void
2045set_x11_icon(icon)
2046 char_u *icon;
2047{
2048 /* See above for comments about using X*SetWMProperties(). */
2049#ifdef USE_UTF8_STRING
2050 if (enc_utf8)
2051 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2052 NULL, 0, NULL, NULL, NULL);
2053 else
2054#endif
2055 {
2056#if XtSpecificationRelease >= 4
2057# ifdef FEAT_XFONTSET
2058 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2059 NULL, 0, NULL, NULL, NULL);
2060# else
2061 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002062 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002064 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2066 NULL, 0, NULL, NULL, NULL);
2067# endif
2068#else
2069 XSetIconName(x11_display, x11_window, (char *)icon);
2070#endif
2071 }
2072 XFlush(x11_display);
2073}
2074
2075#else /* FEAT_X11 */
2076
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 static int
2078get_x11_title(test_only)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002079 int test_only UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081 return FALSE;
2082}
2083
2084 static int
2085get_x11_icon(test_only)
2086 int test_only;
2087{
2088 if (!test_only)
2089 {
2090 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002091 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002093 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095 return FALSE;
2096}
2097
2098#endif /* FEAT_X11 */
2099
2100 int
2101mch_can_restore_title()
2102{
2103 return get_x11_title(TRUE);
2104}
2105
2106 int
2107mch_can_restore_icon()
2108{
2109 return get_x11_icon(TRUE);
2110}
2111
2112/*
2113 * Set the window title and icon.
2114 */
2115 void
2116mch_settitle(title, icon)
2117 char_u *title;
2118 char_u *icon;
2119{
2120 int type = 0;
2121 static int recursive = 0;
2122
2123 if (T_NAME == NULL) /* no terminal name (yet) */
2124 return;
2125 if (title == NULL && icon == NULL) /* nothing to do */
2126 return;
2127
2128 /* When one of the X11 functions causes a deadly signal, we get here again
2129 * recursively. Avoid hanging then (something is probably locked). */
2130 if (recursive)
2131 return;
2132 ++recursive;
2133
2134 /*
2135 * if the window ID and the display is known, we may use X11 calls
2136 */
2137#ifdef FEAT_X11
2138 if (get_x11_windis() == OK)
2139 type = 1;
2140#else
2141# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2142 if (gui.in_use)
2143 type = 1;
2144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145#endif
2146
2147 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002148 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 * than x11 calls, because the x11 calls don't always work
2150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if ((type || *T_TS != NUL) && title != NULL)
2152 {
2153 if (oldtitle == NULL
2154#ifdef FEAT_GUI
2155 && !gui.in_use
2156#endif
2157 ) /* first call but not in GUI, save title */
2158 (void)get_x11_title(FALSE);
2159
2160 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2161 term_settitle(title);
2162#ifdef FEAT_X11
2163 else
2164# ifdef FEAT_GUI_GTK
2165 if (!gui.in_use) /* don't do this if GTK+ is running */
2166# endif
2167 set_x11_title(title); /* x11 */
2168#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002169#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2171 else
2172 gui_mch_settitle(title, icon);
2173#endif
2174 did_set_title = TRUE;
2175 }
2176
2177 if ((type || *T_CIS != NUL) && icon != NULL)
2178 {
2179 if (oldicon == NULL
2180#ifdef FEAT_GUI
2181 && !gui.in_use
2182#endif
2183 ) /* first call, save icon */
2184 get_x11_icon(FALSE);
2185
2186 if (*T_CIS != NUL)
2187 {
2188 out_str(T_CIS); /* set icon start */
2189 out_str_nf(icon);
2190 out_str(T_CIE); /* set icon end */
2191 out_flush();
2192 }
2193#ifdef FEAT_X11
2194 else
2195# ifdef FEAT_GUI_GTK
2196 if (!gui.in_use) /* don't do this if GTK+ is running */
2197# endif
2198 set_x11_icon(icon); /* x11 */
2199#endif
2200 did_set_icon = TRUE;
2201 }
2202 --recursive;
2203}
2204
2205/*
2206 * Restore the window/icon title.
2207 * "which" is one of:
2208 * 1 only restore title
2209 * 2 only restore icon
2210 * 3 restore title and icon
2211 */
2212 void
2213mch_restore_title(which)
2214 int which;
2215{
2216 /* only restore the title or icon when it has been set */
2217 mch_settitle(((which & 1) && did_set_title) ?
2218 (oldtitle ? oldtitle : p_titleold) : NULL,
2219 ((which & 2) && did_set_icon) ? oldicon : NULL);
2220}
2221
2222#endif /* FEAT_TITLE */
2223
2224/*
2225 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002226 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 */
2228 int
2229vim_is_xterm(name)
2230 char_u *name;
2231{
2232 if (name == NULL)
2233 return FALSE;
2234 return (STRNICMP(name, "xterm", 5) == 0
2235 || STRNICMP(name, "nxterm", 6) == 0
2236 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002237 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 || STRNICMP(name, "rxvt", 4) == 0
2239 || STRCMP(name, "builtin_xterm") == 0);
2240}
2241
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002242#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2243/*
2244 * Return TRUE if "name" appears to be that of a terminal
2245 * known to support the xterm-style mouse protocol.
2246 * Relies on term_is_xterm having been set to its correct value.
2247 */
2248 int
2249use_xterm_like_mouse(name)
2250 char_u *name;
2251{
2252 return (name != NULL
2253 && (term_is_xterm || STRNICMP(name, "screen", 6) == 0));
2254}
2255#endif
2256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2258/*
2259 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2260 * Return 1 for "xterm".
2261 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002262 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002263 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 */
2265 int
2266use_xterm_mouse()
2267{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002268 if (ttym_flags == TTYM_SGR)
2269 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002270 if (ttym_flags == TTYM_URXVT)
2271 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (ttym_flags == TTYM_XTERM2)
2273 return 2;
2274 if (ttym_flags == TTYM_XTERM)
2275 return 1;
2276 return 0;
2277}
2278#endif
2279
2280 int
2281vim_is_iris(name)
2282 char_u *name;
2283{
2284 if (name == NULL)
2285 return FALSE;
2286 return (STRNICMP(name, "iris-ansi", 9) == 0
2287 || STRCMP(name, "builtin_iris-ansi") == 0);
2288}
2289
2290 int
2291vim_is_vt300(name)
2292 char_u *name;
2293{
2294 if (name == NULL)
2295 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002296 /* catch VT100 - VT5xx */
2297 return ((STRNICMP(name, "vt", 2) == 0
2298 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 || STRCMP(name, "builtin_vt320") == 0);
2300}
2301
2302/*
2303 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2304 * This should include all windowed terminal emulators.
2305 */
2306 int
2307vim_is_fastterm(name)
2308 char_u *name;
2309{
2310 if (name == NULL)
2311 return FALSE;
2312 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2313 return TRUE;
2314 return ( STRNICMP(name, "hpterm", 6) == 0
2315 || STRNICMP(name, "sun-cmd", 7) == 0
2316 || STRNICMP(name, "screen", 6) == 0
2317 || STRNICMP(name, "dtterm", 6) == 0);
2318}
2319
2320/*
2321 * Insert user name in s[len].
2322 * Return OK if a name found.
2323 */
2324 int
2325mch_get_user_name(s, len)
2326 char_u *s;
2327 int len;
2328{
2329#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002330 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 return OK;
2332#else
2333 return mch_get_uname(getuid(), s, len);
2334#endif
2335}
2336
2337/*
2338 * Insert user name for "uid" in s[len].
2339 * Return OK if a name found.
2340 */
2341 int
2342mch_get_uname(uid, s, len)
2343 uid_t uid;
2344 char_u *s;
2345 int len;
2346{
2347#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2348 struct passwd *pw;
2349
2350 if ((pw = getpwuid(uid)) != NULL
2351 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2352 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002353 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 return OK;
2355 }
2356#endif
2357 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2358 return FAIL; /* a number is not a name */
2359}
2360
2361/*
2362 * Insert host name is s[len].
2363 */
2364
2365#ifdef HAVE_SYS_UTSNAME_H
2366 void
2367mch_get_host_name(s, len)
2368 char_u *s;
2369 int len;
2370{
2371 struct utsname vutsname;
2372
2373 if (uname(&vutsname) < 0)
2374 *s = NUL;
2375 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002376 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377}
2378#else /* HAVE_SYS_UTSNAME_H */
2379
2380# ifdef HAVE_SYS_SYSTEMINFO_H
2381# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2382# endif
2383
2384 void
2385mch_get_host_name(s, len)
2386 char_u *s;
2387 int len;
2388{
2389# ifdef VAXC
2390 vaxc$gethostname((char *)s, len);
2391# else
2392 gethostname((char *)s, len);
2393# endif
2394 s[len - 1] = NUL; /* make sure it's terminated */
2395}
2396#endif /* HAVE_SYS_UTSNAME_H */
2397
2398/*
2399 * return process ID
2400 */
2401 long
2402mch_get_pid()
2403{
2404 return (long)getpid();
2405}
2406
2407#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
2408static char *strerror __ARGS((int));
2409
2410 static char *
2411strerror(err)
2412 int err;
2413{
2414 extern int sys_nerr;
2415 extern char *sys_errlist[];
2416 static char er[20];
2417
2418 if (err > 0 && err < sys_nerr)
2419 return (sys_errlist[err]);
2420 sprintf(er, "Error %d", err);
2421 return er;
2422}
2423#endif
2424
2425/*
2426 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2427 * Return OK for success, FAIL for failure.
2428 */
2429 int
2430mch_dirname(buf, len)
2431 char_u *buf;
2432 int len;
2433{
2434#if defined(USE_GETCWD)
2435 if (getcwd((char *)buf, len) == NULL)
2436 {
2437 STRCPY(buf, strerror(errno));
2438 return FAIL;
2439 }
2440 return OK;
2441#else
2442 return (getwd((char *)buf) != NULL ? OK : FAIL);
2443#endif
2444}
2445
2446#if defined(OS2) || defined(PROTO)
2447/*
2448 * Replace all slashes by backslashes.
2449 * When 'shellslash' set do it the other way around.
2450 */
2451 void
2452slash_adjust(p)
2453 char_u *p;
2454{
2455 while (*p)
2456 {
2457 if (*p == psepcN)
2458 *p = psepc;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002459 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461}
2462#endif
2463
2464/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002465 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 *
2467 * return FAIL for failure, OK for success
2468 */
2469 int
2470mch_FullName(fname, buf, len, force)
2471 char_u *fname, *buf;
2472 int len;
2473 int force; /* also expand when already absolute path */
2474{
2475 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002476#ifdef OS2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 int only_drive; /* file name is only a drive letter */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002478#endif
2479#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 int fd = -1;
2481 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 char_u olddir[MAXPATHL];
2484 char_u *p;
2485 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002486#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002487 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2488 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002489#endif
2490
Bram Moolenaar38323e42007-03-06 19:22:53 +00002491#ifdef VMS
2492 fname = vms_fixfilename(fname);
2493#endif
2494
Bram Moolenaara2442432007-04-26 14:26:37 +00002495#ifdef __CYGWIN__
2496 /*
2497 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2498 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002499# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2500 cygwin_conv_path(CCP_WIN_A_TO_POSIX, fname, posix_fname, MAXPATHL);
2501# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002502 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002503# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002504 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002505#endif
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 /* expand it if forced or not an absolute path */
2508 if (force || !mch_isFullName(fname))
2509 {
2510 /*
2511 * If the file name has a path, change to that directory for a moment,
2512 * and then do the getwd() (and get back to where we were).
2513 * This will get the correct path name with "../" things.
2514 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002515#ifdef OS2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 only_drive = 0;
2517 if (((p = vim_strrchr(fname, '/')) != NULL)
2518 || ((p = vim_strrchr(fname, '\\')) != NULL)
2519 || (((p = vim_strchr(fname, ':')) != NULL) && ++only_drive))
Bram Moolenaar38323e42007-03-06 19:22:53 +00002520#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if ((p = vim_strrchr(fname, '/')) != NULL)
Bram Moolenaar38323e42007-03-06 19:22:53 +00002522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002524#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 /*
2526 * Use fchdir() if possible, it's said to be faster and more
2527 * reliable. But on SunOS 4 it might not work. Check this by
2528 * doing a fchdir() right now.
2529 */
2530 if (!dont_fchdir)
2531 {
2532 fd = open(".", O_RDONLY | O_EXTRA, 0);
2533 if (fd >= 0 && fchdir(fd) < 0)
2534 {
2535 close(fd);
2536 fd = -1;
2537 dont_fchdir = TRUE; /* don't try again */
2538 }
2539 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
2542 /* Only change directory when we are sure we can return to where
2543 * we are now. After doing "su" chdir(".") might not work. */
2544 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002545#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 (mch_dirname(olddir, MAXPATHL) == FAIL
2549 || mch_chdir((char *)olddir) != 0))
2550 {
2551 p = NULL; /* can't get current dir: don't chdir */
2552 retval = FAIL;
2553 }
2554 else
2555 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002556#ifdef OS2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 /*
2558 * compensate for case where ':' from "D:" was the only
2559 * path separator detected in the file name; the _next_
2560 * character has to be removed, and then restored later.
2561 */
2562 if (only_drive)
2563 p++;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 /* The directory is copied into buf[], to be able to remove
2566 * the file name without changing it (could be a string in
2567 * read-only memory) */
2568 if (p - fname >= len)
2569 retval = FAIL;
2570 else
2571 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002572 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (mch_chdir((char *)buf))
2574 retval = FAIL;
2575 else
2576 fname = p + 1;
2577 *buf = NUL;
2578 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002579#ifdef OS2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (only_drive)
2581 {
2582 p--;
2583 if (retval != FAIL)
2584 fname--;
2585 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588 }
2589 if (mch_dirname(buf, len) == FAIL)
2590 {
2591 retval = FAIL;
2592 *buf = NUL;
2593 }
2594 if (p != NULL)
2595 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002596#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (fd >= 0)
2598 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002599 if (p_verbose >= 5)
2600 {
2601 verbose_enter();
2602 MSG("fchdir() to previous dir");
2603 verbose_leave();
2604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 l = fchdir(fd);
2606 close(fd);
2607 }
2608 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 l = mch_chdir((char *)olddir);
2611 if (l != 0)
2612 EMSG(_(e_prev_dir));
2613 }
2614
2615 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002616 if (l >= len - 1)
2617 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002618#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002619 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002621 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002626 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 return FAIL;
2628
2629 /* Do not append ".", "/dir/." is equal to "/dir". */
2630 if (STRCMP(fname, ".") != 0)
2631 STRCAT(buf, fname);
2632
2633 return OK;
2634}
2635
2636/*
2637 * Return TRUE if "fname" does not depend on the current directory.
2638 */
2639 int
2640mch_isFullName(fname)
2641 char_u *fname;
2642{
2643#ifdef __EMX__
2644 return _fnisabs(fname);
2645#else
2646# ifdef VMS
2647 return ( fname[0] == '/' || fname[0] == '.' ||
2648 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2649 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2650 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
2651# else
2652 return (*fname == '/' || *fname == '~');
2653# endif
2654#endif
2655}
2656
Bram Moolenaar24552be2005-12-10 20:17:30 +00002657#if defined(USE_FNAME_CASE) || defined(PROTO)
2658/*
2659 * Set the case of the file name, if it already exists. This will cause the
2660 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002661 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002662 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002663 void
2664fname_case(name, len)
2665 char_u *name;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002666 int len UNUSED; /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002667{
2668 struct stat st;
2669 char_u *slash, *tail;
2670 DIR *dirp;
2671 struct dirent *dp;
2672
2673 if (lstat((char *)name, &st) >= 0)
2674 {
2675 /* Open the directory where the file is located. */
2676 slash = vim_strrchr(name, '/');
2677 if (slash == NULL)
2678 {
2679 dirp = opendir(".");
2680 tail = name;
2681 }
2682 else
2683 {
2684 *slash = NUL;
2685 dirp = opendir((char *)name);
2686 *slash = '/';
2687 tail = slash + 1;
2688 }
2689
2690 if (dirp != NULL)
2691 {
2692 while ((dp = readdir(dirp)) != NULL)
2693 {
2694 /* Only accept names that differ in case and are the same byte
2695 * length. TODO: accept different length name. */
2696 if (STRICMP(tail, dp->d_name) == 0
2697 && STRLEN(tail) == STRLEN(dp->d_name))
2698 {
2699 char_u newname[MAXPATHL + 1];
2700 struct stat st2;
2701
2702 /* Verify the inode is equal. */
2703 vim_strncpy(newname, name, MAXPATHL);
2704 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2705 MAXPATHL - (tail - name));
2706 if (lstat((char *)newname, &st2) >= 0
2707 && st.st_ino == st2.st_ino
2708 && st.st_dev == st2.st_dev)
2709 {
2710 STRCPY(tail, dp->d_name);
2711 break;
2712 }
2713 }
2714 }
2715
2716 closedir(dirp);
2717 }
2718 }
2719}
2720#endif
2721
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722/*
2723 * Get file permissions for 'name'.
2724 * Returns -1 when it doesn't exist.
2725 */
2726 long
2727mch_getperm(name)
2728 char_u *name;
2729{
2730 struct stat statb;
2731
2732 /* Keep the #ifdef outside of stat(), it may be a macro. */
2733#ifdef VMS
2734 if (stat((char *)vms_fixfilename(name), &statb))
2735#else
2736 if (stat((char *)name, &statb))
2737#endif
2738 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002739#ifdef __INTERIX
2740 /* The top bit makes the value negative, which means the file doesn't
2741 * exist. Remove the bit, we don't use it. */
2742 return statb.st_mode & ~S_ADDACE;
2743#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746}
2747
2748/*
2749 * set file permission for 'name' to 'perm'
2750 *
2751 * return FAIL for failure, OK otherwise
2752 */
2753 int
2754mch_setperm(name, perm)
2755 char_u *name;
2756 long perm;
2757{
2758 return (chmod((char *)
2759#ifdef VMS
2760 vms_fixfilename(name),
2761#else
2762 name,
2763#endif
2764 (mode_t)perm) == 0 ? OK : FAIL);
2765}
2766
2767#if defined(HAVE_ACL) || defined(PROTO)
2768# ifdef HAVE_SYS_ACL_H
2769# include <sys/acl.h>
2770# endif
2771# ifdef HAVE_SYS_ACCESS_H
2772# include <sys/access.h>
2773# endif
2774
2775# ifdef HAVE_SOLARIS_ACL
2776typedef struct vim_acl_solaris_T {
2777 int acl_cnt;
2778 aclent_t *acl_entry;
2779} vim_acl_solaris_T;
2780# endif
2781
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002782#if defined(HAVE_SELINUX) || defined(PROTO)
2783/*
2784 * Copy security info from "from_file" to "to_file".
2785 */
2786 void
2787mch_copy_sec(from_file, to_file)
2788 char_u *from_file;
2789 char_u *to_file;
2790{
2791 if (from_file == NULL)
2792 return;
2793
2794 if (selinux_enabled == -1)
2795 selinux_enabled = is_selinux_enabled();
2796
2797 if (selinux_enabled > 0)
2798 {
2799 security_context_t from_context = NULL;
2800 security_context_t to_context = NULL;
2801
2802 if (getfilecon((char *)from_file, &from_context) < 0)
2803 {
2804 /* If the filesystem doesn't support extended attributes,
2805 the original had no special security context and the
2806 target cannot have one either. */
2807 if (errno == EOPNOTSUPP)
2808 return;
2809
2810 MSG_PUTS(_("\nCould not get security context for "));
2811 msg_outtrans(from_file);
2812 msg_putchar('\n');
2813 return;
2814 }
2815 if (getfilecon((char *)to_file, &to_context) < 0)
2816 {
2817 MSG_PUTS(_("\nCould not get security context for "));
2818 msg_outtrans(to_file);
2819 msg_putchar('\n');
2820 freecon (from_context);
2821 return ;
2822 }
2823 if (strcmp(from_context, to_context) != 0)
2824 {
2825 if (setfilecon((char *)to_file, from_context) < 0)
2826 {
2827 MSG_PUTS(_("\nCould not set security context for "));
2828 msg_outtrans(to_file);
2829 msg_putchar('\n');
2830 }
2831 }
2832 freecon(to_context);
2833 freecon(from_context);
2834 }
2835}
2836#endif /* HAVE_SELINUX */
2837
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002838#if defined(HAVE_SMACK) && !defined(PROTO)
2839/*
2840 * Copy security info from "from_file" to "to_file".
2841 */
2842 void
2843mch_copy_sec(from_file, to_file)
2844 char_u *from_file;
2845 char_u *to_file;
2846{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002847 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002848 {
2849 XATTR_NAME_SMACK,
2850 XATTR_NAME_SMACKEXEC,
2851 XATTR_NAME_SMACKMMAP
2852 };
2853
2854 char buffer[SMACK_LABEL_LEN];
2855 const char *name;
2856 int index;
2857 int ret;
2858 ssize_t size;
2859
2860 if (from_file == NULL)
2861 return;
2862
2863 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2864 / sizeof(smack_copied_attributes)[0]) ; index++)
2865 {
2866 /* get the name of the attribute to copy */
2867 name = smack_copied_attributes[index];
2868
2869 /* get the value of the attribute in buffer */
2870 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2871 if (size >= 0)
2872 {
2873 /* copy the attribute value of buffer */
2874 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2875 if (ret < 0)
2876 {
2877 MSG_PUTS(_("Could not set security context "));
2878 MSG_PUTS(name);
2879 MSG_PUTS(_(" for "));
2880 msg_outtrans(to_file);
2881 msg_putchar('\n');
2882 }
2883 }
2884 else
2885 {
2886 /* what reason of not having the attribute value? */
2887 switch (errno)
2888 {
2889 case ENOTSUP:
2890 /* extended attributes aren't supported or enabled */
2891 /* should a message be echoed? not sure... */
2892 return; /* leave because it isn't usefull to continue */
2893
2894 case ERANGE:
2895 default:
2896 /* no enough size OR unexpected error */
2897 MSG_PUTS(_("Could not get security context "));
2898 MSG_PUTS(name);
2899 MSG_PUTS(_(" for "));
2900 msg_outtrans(from_file);
2901 MSG_PUTS(_(". Removing it!\n"));
2902 /* FALLTHROUGH to remove the attribute */
2903
2904 case ENODATA:
2905 /* no attribute of this name */
2906 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002907 /* Silently ignore errors, apparently this happens when
2908 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002909 break;
2910 }
2911 }
2912 }
2913}
2914#endif /* HAVE_SMACK */
2915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916/*
2917 * Return a pointer to the ACL of file "fname" in allocated memory.
2918 * Return NULL if the ACL is not available for whatever reason.
2919 */
2920 vim_acl_T
2921mch_get_acl(fname)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002922 char_u *fname UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
2924 vim_acl_T ret = NULL;
2925#ifdef HAVE_POSIX_ACL
2926 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2927#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002928#ifdef HAVE_SOLARIS_ZFS_ACL
2929 acl_t *aclent;
2930
2931 if (acl_get((char *)fname, 0, &aclent) < 0)
2932 return NULL;
2933 ret = (vim_acl_T)aclent;
2934#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935#ifdef HAVE_SOLARIS_ACL
2936 vim_acl_solaris_T *aclent;
2937
2938 aclent = malloc(sizeof(vim_acl_solaris_T));
2939 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2940 {
2941 free(aclent);
2942 return NULL;
2943 }
2944 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2945 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2946 {
2947 free(aclent->acl_entry);
2948 free(aclent);
2949 return NULL;
2950 }
2951 ret = (vim_acl_T)aclent;
2952#else
2953#if defined(HAVE_AIX_ACL)
2954 int aclsize;
2955 struct acl *aclent;
2956
2957 aclsize = sizeof(struct acl);
2958 aclent = malloc(aclsize);
2959 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2960 {
2961 if (errno == ENOSPC)
2962 {
2963 aclsize = aclent->acl_len;
2964 aclent = realloc(aclent, aclsize);
2965 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2966 {
2967 free(aclent);
2968 return NULL;
2969 }
2970 }
2971 else
2972 {
2973 free(aclent);
2974 return NULL;
2975 }
2976 }
2977 ret = (vim_acl_T)aclent;
2978#endif /* HAVE_AIX_ACL */
2979#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002980#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981#endif /* HAVE_POSIX_ACL */
2982 return ret;
2983}
2984
2985/*
2986 * Set the ACL of file "fname" to "acl" (unless it's NULL).
2987 */
2988 void
2989mch_set_acl(fname, aclent)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002990 char_u *fname UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 vim_acl_T aclent;
2992{
2993 if (aclent == NULL)
2994 return;
2995#ifdef HAVE_POSIX_ACL
2996 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
2997#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002998#ifdef HAVE_SOLARIS_ZFS_ACL
2999 acl_set((char *)fname, (acl_t *)aclent);
3000#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001#ifdef HAVE_SOLARIS_ACL
3002 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3003 ((vim_acl_solaris_T *)aclent)->acl_entry);
3004#else
3005#ifdef HAVE_AIX_ACL
3006 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3007#endif /* HAVE_AIX_ACL */
3008#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003009#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#endif /* HAVE_POSIX_ACL */
3011}
3012
3013 void
3014mch_free_acl(aclent)
3015 vim_acl_T aclent;
3016{
3017 if (aclent == NULL)
3018 return;
3019#ifdef HAVE_POSIX_ACL
3020 acl_free((acl_t)aclent);
3021#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003022#ifdef HAVE_SOLARIS_ZFS_ACL
3023 acl_free((acl_t *)aclent);
3024#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025#ifdef HAVE_SOLARIS_ACL
3026 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3027 free(aclent);
3028#else
3029#ifdef HAVE_AIX_ACL
3030 free(aclent);
3031#endif /* HAVE_AIX_ACL */
3032#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003033#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034#endif /* HAVE_POSIX_ACL */
3035}
3036#endif
3037
3038/*
3039 * Set hidden flag for "name".
3040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 void
3042mch_hide(name)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003043 char_u *name UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 /* can't hide a file */
3046}
3047
3048/*
3049 * return TRUE if "name" is a directory
3050 * return FALSE if "name" is not a directory
3051 * return FALSE for error
3052 */
3053 int
3054mch_isdir(name)
3055 char_u *name;
3056{
3057 struct stat statb;
3058
3059 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3060 return FALSE;
3061 if (stat((char *)name, &statb))
3062 return FALSE;
3063#ifdef _POSIX_SOURCE
3064 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3065#else
3066 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3067#endif
3068}
3069
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070static int executable_file __ARGS((char_u *name));
3071
3072/*
3073 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3074 */
3075 static int
3076executable_file(name)
3077 char_u *name;
3078{
3079 struct stat st;
3080
3081 if (stat((char *)name, &st))
3082 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003083#ifdef VMS
3084 /* Like on Unix system file can have executable rights but not necessarily
3085 * be an executable, but on Unix is not a default for an ordianry file to
3086 * have an executable flag - on VMS it is in most cases.
3087 * Therefore, this check does not have any sense - let keep us to the
3088 * conventions instead:
3089 * *.COM and *.EXE files are the executables - the rest are not. This is
3090 * not ideal but better then it was.
3091 */
3092 int vms_executable = 0;
3093 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3094 {
3095 if (strstr(vms_tolower((char*)name),".exe") != NULL
3096 || strstr(vms_tolower((char*)name),".com")!= NULL)
3097 vms_executable = 1;
3098 }
3099 return vms_executable;
3100#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
3105/*
3106 * Return 1 if "name" can be found in $PATH and executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003107 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 * Return -1 if unknown.
3109 */
3110 int
Bram Moolenaarb5971142015-03-21 17:32:19 +01003111mch_can_exe(name, path, use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 char_u *name;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003113 char_u **path;
Bram Moolenaarb5971142015-03-21 17:32:19 +01003114 int use_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 char_u *buf;
3117 char_u *p, *e;
3118 int retval;
3119
Bram Moolenaarb5971142015-03-21 17:32:19 +01003120 /* When "use_path" is false and if it's an absolute or relative path don't
3121 * need to use $PATH. */
3122 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3123 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003124 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003125 /* There must be a path separator, files in the current directory
3126 * can't be executed. */
3127 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003128 {
3129 if (path != NULL)
3130 {
3131 if (name[0] == '.')
3132 *path = FullName_save(name, TRUE);
3133 else
3134 *path = vim_strsave(name);
3135 }
3136 return TRUE;
3137 }
3138 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
3141 p = (char_u *)getenv("PATH");
3142 if (p == NULL || *p == NUL)
3143 return -1;
3144 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3145 if (buf == NULL)
3146 return -1;
3147
3148 /*
3149 * Walk through all entries in $PATH to check if "name" exists there and
3150 * is an executable file.
3151 */
3152 for (;;)
3153 {
3154 e = (char_u *)strchr((char *)p, ':');
3155 if (e == NULL)
3156 e = p + STRLEN(p);
3157 if (e - p <= 1) /* empty entry means current dir */
3158 STRCPY(buf, "./");
3159 else
3160 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003161 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 add_pathsep(buf);
3163 }
3164 STRCAT(buf, name);
3165 retval = executable_file(buf);
3166 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003167 {
3168 if (path != NULL)
3169 {
3170 if (buf[0] == '.')
3171 *path = FullName_save(buf, TRUE);
3172 else
3173 *path = vim_strsave(buf);
3174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
3178 if (*e != ':')
3179 break;
3180 p = e + 1;
3181 }
3182
3183 vim_free(buf);
3184 return retval;
3185}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187/*
3188 * Check what "name" is:
3189 * NODE_NORMAL: file or directory (or doesn't exist)
3190 * NODE_WRITABLE: writable device, socket, fifo, etc.
3191 * NODE_OTHER: non-writable things
3192 */
3193 int
3194mch_nodetype(name)
3195 char_u *name;
3196{
3197 struct stat st;
3198
3199 if (stat((char *)name, &st))
3200 return NODE_NORMAL;
3201 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3202 return NODE_NORMAL;
3203#ifndef OS2
3204 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3205 return NODE_OTHER;
3206#endif
3207 /* Everything else is writable? */
3208 return NODE_WRITABLE;
3209}
3210
3211 void
3212mch_early_init()
3213{
3214#ifdef HAVE_CHECK_STACK_GROWTH
3215 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 check_stack_growth((char *)&i);
3218
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003219# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 get_stack_limit();
3221# endif
3222
3223#endif
3224
3225 /*
3226 * Setup an alternative stack for signals. Helps to catch signals when
3227 * running out of stack space.
3228 * Use of sigaltstack() is preferred, it's more portable.
3229 * Ignore any errors.
3230 */
3231#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003232 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 init_signal_stack();
3234#endif
3235}
3236
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003237#if defined(EXITFREE) || defined(PROTO)
3238 void
3239mch_free_mem()
3240{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003241# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3242 if (clip_star.owned)
3243 clip_lose_selection(&clip_star);
3244 if (clip_plus.owned)
3245 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003246# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003247# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003248 if (xterm_Shell != (Widget)0)
3249 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003250# ifndef LESSTIF_VERSION
3251 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003252 if (xterm_dpy != NULL)
3253 XtCloseDisplay(xterm_dpy);
3254 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003255 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003256 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003257# ifdef FEAT_X11
3258 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3259# endif
3260 }
3261# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003262# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003263# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003264 if (x11_display != NULL
3265# ifdef FEAT_XCLIPBOARD
3266 && x11_display != xterm_dpy
3267# endif
3268 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 XCloseDisplay(x11_display);
3270# endif
3271# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
3272 vim_free(signal_stack);
3273 signal_stack = NULL;
3274# endif
3275# ifdef FEAT_TITLE
3276 vim_free(oldtitle);
3277 vim_free(oldicon);
3278# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003279}
3280#endif
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282static void exit_scroll __ARGS((void));
3283
3284/*
3285 * Output a newline when exiting.
3286 * Make sure the newline goes to the same stream as the text.
3287 */
3288 static void
3289exit_scroll()
3290{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003291 if (silent_mode)
3292 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (newline_on_exit || msg_didout)
3294 {
3295 if (msg_use_printf())
3296 {
3297 if (info_message)
3298 mch_msg("\n");
3299 else
3300 mch_errmsg("\r\n");
3301 }
3302 else
3303 out_char('\n');
3304 }
3305 else
3306 {
3307 restore_cterm_colors(); /* get original colors back */
3308 msg_clr_eos_force(); /* clear the rest of the display */
3309 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3310 }
3311}
3312
3313 void
3314mch_exit(r)
3315 int r;
3316{
3317 exiting = TRUE;
3318
3319#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3320 x11_export_final_selection();
3321#endif
3322
3323#ifdef FEAT_GUI
3324 if (!gui.in_use)
3325#endif
3326 {
3327 settmode(TMODE_COOK);
3328#ifdef FEAT_TITLE
3329 mch_restore_title(3); /* restore xterm title and icon name */
3330#endif
3331 /*
3332 * When t_ti is not empty but it doesn't cause swapping terminal
3333 * pages, need to output a newline when msg_didout is set. But when
3334 * t_ti does swap pages it should not go to the shell page. Do this
3335 * before stoptermcap().
3336 */
3337 if (swapping_screen() && !newline_on_exit)
3338 exit_scroll();
3339
3340 /* Stop termcap: May need to check for T_CRV response, which
3341 * requires RAW mode. */
3342 stoptermcap();
3343
3344 /*
3345 * A newline is only required after a message in the alternate screen.
3346 * This is set to TRUE by wait_return().
3347 */
3348 if (!swapping_screen() || newline_on_exit)
3349 exit_scroll();
3350
3351 /* Cursor may have been switched off without calling starttermcap()
3352 * when doing "vim -u vimrc" and vimrc contains ":q". */
3353 if (full_screen)
3354 cursor_on();
3355 }
3356 out_flush();
3357 ml_close_all(TRUE); /* remove all memfiles */
3358 may_core_dump();
3359#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 gui_exit(r);
3362#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003363
Bram Moolenaar56718732006-03-15 22:53:57 +00003364#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003365 mac_conv_cleanup();
3366#endif
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#ifdef __QNX__
3369 /* A core dump won't be created if the signal handler
3370 * doesn't return, so we can't call exit() */
3371 if (deadly_signal != 0)
3372 return;
3373#endif
3374
Bram Moolenaar009b2592004-10-24 19:18:58 +00003375#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003376 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003377#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003378
3379#ifdef EXITFREE
3380 free_all_mem();
3381#endif
3382
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 exit(r);
3384}
3385
3386 static void
3387may_core_dump()
3388{
3389 if (deadly_signal != 0)
3390 {
3391 signal(deadly_signal, SIG_DFL);
3392 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3393 }
3394}
3395
3396#ifndef VMS
3397
3398 void
3399mch_settmode(tmode)
3400 int tmode;
3401{
3402 static int first = TRUE;
3403
3404 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3405#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3406 /*
3407 * for "new" tty systems
3408 */
3409# ifdef HAVE_TERMIOS_H
3410 static struct termios told;
3411 struct termios tnew;
3412# else
3413 static struct termio told;
3414 struct termio tnew;
3415# endif
3416
3417 if (first)
3418 {
3419 first = FALSE;
3420# if defined(HAVE_TERMIOS_H)
3421 tcgetattr(read_cmd_fd, &told);
3422# else
3423 ioctl(read_cmd_fd, TCGETA, &told);
3424# endif
3425 }
3426
3427 tnew = told;
3428 if (tmode == TMODE_RAW)
3429 {
3430 /*
3431 * ~ICRNL enables typing ^V^M
3432 */
3433 tnew.c_iflag &= ~ICRNL;
3434 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3435# if defined(IEXTEN) && !defined(__MINT__)
3436 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3437 /* but it breaks function keys on MINT */
3438# endif
3439 );
3440# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3441 tnew.c_oflag &= ~ONLCR;
3442# endif
3443 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3444 tnew.c_cc[VTIME] = 0; /* don't wait */
3445 }
3446 else if (tmode == TMODE_SLEEP)
3447 tnew.c_lflag &= ~(ECHO);
3448
3449# if defined(HAVE_TERMIOS_H)
3450 {
3451 int n = 10;
3452
3453 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3454 * few times. */
3455 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3456 && errno == EINTR && n > 0)
3457 --n;
3458 }
3459# else
3460 ioctl(read_cmd_fd, TCSETA, &tnew);
3461# endif
3462
3463#else
3464
3465 /*
3466 * for "old" tty systems
3467 */
3468# ifndef TIOCSETN
3469# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3470# endif
3471 static struct sgttyb ttybold;
3472 struct sgttyb ttybnew;
3473
3474 if (first)
3475 {
3476 first = FALSE;
3477 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3478 }
3479
3480 ttybnew = ttybold;
3481 if (tmode == TMODE_RAW)
3482 {
3483 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3484 ttybnew.sg_flags |= RAW;
3485 }
3486 else if (tmode == TMODE_SLEEP)
3487 ttybnew.sg_flags &= ~(ECHO);
3488 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3489#endif
3490 curr_tmode = tmode;
3491}
3492
3493/*
3494 * Try to get the code for "t_kb" from the stty setting
3495 *
3496 * Even if termcap claims a backspace key, the user's setting *should*
3497 * prevail. stty knows more about reality than termcap does, and if
3498 * somebody's usual erase key is DEL (which, for most BSD users, it will
3499 * be), they're going to get really annoyed if their erase key starts
3500 * doing forward deletes for no reason. (Eric Fischer)
3501 */
3502 void
3503get_stty()
3504{
3505 char_u buf[2];
3506 char_u *p;
3507
3508 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3509#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3510 /* for "new" tty systems */
3511# ifdef HAVE_TERMIOS_H
3512 struct termios keys;
3513# else
3514 struct termio keys;
3515# endif
3516
3517# if defined(HAVE_TERMIOS_H)
3518 if (tcgetattr(read_cmd_fd, &keys) != -1)
3519# else
3520 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1)
3521# endif
3522 {
3523 buf[0] = keys.c_cc[VERASE];
3524 intr_char = keys.c_cc[VINTR];
3525#else
3526 /* for "old" tty systems */
3527 struct sgttyb keys;
3528
3529 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1)
3530 {
3531 buf[0] = keys.sg_erase;
3532 intr_char = keys.sg_kill;
3533#endif
3534 buf[1] = NUL;
3535 add_termcode((char_u *)"kb", buf, FALSE);
3536
3537 /*
3538 * If <BS> and <DEL> are now the same, redefine <DEL>.
3539 */
3540 p = find_termcode((char_u *)"kD");
3541 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3542 do_fixdel(NULL);
3543 }
3544#if 0
3545 } /* to keep cindent happy */
3546#endif
3547}
3548
3549#endif /* VMS */
3550
3551#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
3552/*
3553 * Set mouse clicks on or off.
3554 */
3555 void
3556mch_setmouse(on)
3557 int on;
3558{
3559 static int ison = FALSE;
3560 int xterm_mouse_vers;
3561
3562 if (on == ison) /* return quickly if nothing to do */
3563 return;
3564
3565 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003566
3567# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003568 if (ttym_flags == TTYM_URXVT)
3569 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003570 out_str_nf((char_u *)
3571 (on
3572 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3573 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
3574 ison = on;
3575 }
3576# endif
3577
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003578# ifdef FEAT_MOUSE_SGR
3579 if (ttym_flags == TTYM_SGR)
3580 {
3581 out_str_nf((char_u *)
3582 (on
3583 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3584 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
3585 ison = on;
3586 }
3587# endif
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 if (xterm_mouse_vers > 0)
3590 {
3591 if (on) /* enable mouse events, use mouse tracking if available */
3592 out_str_nf((char_u *)
3593 (xterm_mouse_vers > 1
3594 ? IF_EB("\033[?1002h", ESC_STR "[?1002h")
3595 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3596 else /* disable mouse events, could probably always send the same */
3597 out_str_nf((char_u *)
3598 (xterm_mouse_vers > 1
3599 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3600 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
3601 ison = on;
3602 }
3603
3604# ifdef FEAT_MOUSE_DEC
3605 else if (ttym_flags == TTYM_DEC)
3606 {
3607 if (on) /* enable mouse events */
3608 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3609 else /* disable mouse events */
3610 out_str_nf((char_u *)"\033['z");
3611 ison = on;
3612 }
3613# endif
3614
3615# ifdef FEAT_MOUSE_GPM
3616 else
3617 {
3618 if (on)
3619 {
3620 if (gpm_open())
3621 ison = TRUE;
3622 }
3623 else
3624 {
3625 gpm_close();
3626 ison = FALSE;
3627 }
3628 }
3629# endif
3630
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003631# ifdef FEAT_SYSMOUSE
3632 else
3633 {
3634 if (on)
3635 {
3636 if (sysmouse_open() == OK)
3637 ison = TRUE;
3638 }
3639 else
3640 {
3641 sysmouse_close();
3642 ison = FALSE;
3643 }
3644 }
3645# endif
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647# ifdef FEAT_MOUSE_JSB
3648 else
3649 {
3650 if (on)
3651 {
3652 /* D - Enable Mouse up/down messages
3653 * L - Enable Left Button Reporting
3654 * M - Enable Middle Button Reporting
3655 * R - Enable Right Button Reporting
3656 * K - Enable SHIFT and CTRL key Reporting
3657 * + - Enable Advanced messaging of mouse moves and up/down messages
3658 * Q - Quiet No Ack
3659 * # - Numeric value of mouse pointer required
3660 * 0 = Multiview 2000 cursor, used as standard
3661 * 1 = Windows Arrow
3662 * 2 = Windows I Beam
3663 * 3 = Windows Hour Glass
3664 * 4 = Windows Cross Hair
3665 * 5 = Windows UP Arrow
3666 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003667# ifdef JSBTERM_MOUSE_NONADVANCED
3668 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3670 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003671# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3673 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 ison = TRUE;
3676 }
3677 else
3678 {
3679 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3680 ESC_STR "[0~ZwQ" ESC_STR "\\"));
3681 ison = FALSE;
3682 }
3683 }
3684# endif
3685# ifdef FEAT_MOUSE_PTERM
3686 else
3687 {
3688 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3689 if (on)
3690 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3691 else
3692 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
3693 ison = on;
3694 }
3695# endif
3696}
3697
3698/*
3699 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3700 */
3701 void
3702check_mouse_termcode()
3703{
3704# ifdef FEAT_MOUSE_XTERM
3705 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003706# ifdef FEAT_MOUSE_URXVT
3707 && use_xterm_mouse() != 3
3708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709# ifdef FEAT_GUI
3710 && !gui.in_use
3711# endif
3712 )
3713 {
3714 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003715 ? IF_EB("\233M", CSI_STR "M")
3716 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 if (*p_mouse != NUL)
3718 {
3719 /* force mouse off and maybe on to send possibly new mouse
3720 * activation sequence to the xterm, with(out) drag tracing. */
3721 mch_setmouse(FALSE);
3722 setmouse();
3723 }
3724 }
3725 else
3726 del_mouse_termcode(KS_MOUSE);
3727# endif
3728
3729# ifdef FEAT_MOUSE_GPM
3730 if (!use_xterm_mouse()
3731# ifdef FEAT_GUI
3732 && !gui.in_use
3733# endif
3734 )
3735 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3736# endif
3737
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003738# ifdef FEAT_SYSMOUSE
3739 if (!use_xterm_mouse()
3740# ifdef FEAT_GUI
3741 && !gui.in_use
3742# endif
3743 )
3744 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3745# endif
3746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003748 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (!use_xterm_mouse()
3750# ifdef FEAT_GUI
3751 && !gui.in_use
3752# endif
3753 )
3754 set_mouse_termcode(KS_JSBTERM_MOUSE,
3755 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3756 else
3757 del_mouse_termcode(KS_JSBTERM_MOUSE);
3758# endif
3759
3760# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003761 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 * define it in the GUI or when using an xterm. */
3763 if (!use_xterm_mouse()
3764# ifdef FEAT_GUI
3765 && !gui.in_use
3766# endif
3767 )
3768 set_mouse_termcode(KS_NETTERM_MOUSE,
3769 (char_u *)IF_EB("\033}", ESC_STR "}"));
3770 else
3771 del_mouse_termcode(KS_NETTERM_MOUSE);
3772# endif
3773
3774# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003775 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003776 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777# ifdef FEAT_GUI
3778 && !gui.in_use
3779# endif
3780 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003781 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3782 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 else
3784 del_mouse_termcode(KS_DEC_MOUSE);
3785# endif
3786# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003787 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003788 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789# ifdef FEAT_GUI
3790 && !gui.in_use
3791# endif
3792 )
3793 set_mouse_termcode(KS_PTERM_MOUSE,
3794 (char_u *) IF_EB("\033[", ESC_STR "["));
3795 else
3796 del_mouse_termcode(KS_PTERM_MOUSE);
3797# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003798# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003799 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003800 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003801# ifdef FEAT_GUI
3802 && !gui.in_use
3803# endif
3804 )
3805 {
3806 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3807 ? IF_EB("\233", CSI_STR)
3808 : IF_EB("\033[", ESC_STR "[")));
3809
3810 if (*p_mouse != NUL)
3811 {
3812 mch_setmouse(FALSE);
3813 setmouse();
3814 }
3815 }
3816 else
3817 del_mouse_termcode(KS_URXVT_MOUSE);
3818# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003819# ifdef FEAT_MOUSE_SGR
Bram Moolenaar24dc2302014-05-13 20:19:58 +02003820 /* There is no conflict with xterm mouse */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003821 if (use_xterm_mouse() == 4
3822# ifdef FEAT_GUI
3823 && !gui.in_use
3824# endif
3825 )
3826 {
3827 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3828 ? IF_EB("\233<", CSI_STR "<")
3829 : IF_EB("\033[<", ESC_STR "[<")));
3830
3831 if (*p_mouse != NUL)
3832 {
3833 mch_setmouse(FALSE);
3834 setmouse();
3835 }
3836 }
3837 else
3838 del_mouse_termcode(KS_SGR_MOUSE);
3839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840}
3841#endif
3842
3843/*
3844 * set screen mode, always fails.
3845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 int
3847mch_screenmode(arg)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003848 char_u *arg UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849{
3850 EMSG(_(e_screenmode));
3851 return FAIL;
3852}
3853
3854#ifndef VMS
3855
3856/*
3857 * Try to get the current window size:
3858 * 1. with an ioctl(), most accurate method
3859 * 2. from the environment variables LINES and COLUMNS
3860 * 3. from the termcap
3861 * 4. keep using the old values
3862 * Return OK when size could be determined, FAIL otherwise.
3863 */
3864 int
3865mch_get_shellsize()
3866{
3867 long rows = 0;
3868 long columns = 0;
3869 char_u *p;
3870
3871 /*
3872 * For OS/2 use _scrsize().
3873 */
3874# ifdef __EMX__
3875 {
3876 int s[2];
3877
3878 _scrsize(s);
3879 columns = s[0];
3880 rows = s[1];
3881 }
3882# endif
3883
3884 /*
3885 * 1. try using an ioctl. It is the most accurate method.
3886 *
3887 * Try using TIOCGWINSZ first, some systems that have it also define
3888 * TIOCGSIZE but don't have a struct ttysize.
3889 */
3890# ifdef TIOCGWINSZ
3891 {
3892 struct winsize ws;
3893 int fd = 1;
3894
3895 /* When stdout is not a tty, use stdin for the ioctl(). */
3896 if (!isatty(fd) && isatty(read_cmd_fd))
3897 fd = read_cmd_fd;
3898 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3899 {
3900 columns = ws.ws_col;
3901 rows = ws.ws_row;
3902 }
3903 }
3904# else /* TIOCGWINSZ */
3905# ifdef TIOCGSIZE
3906 {
3907 struct ttysize ts;
3908 int fd = 1;
3909
3910 /* When stdout is not a tty, use stdin for the ioctl(). */
3911 if (!isatty(fd) && isatty(read_cmd_fd))
3912 fd = read_cmd_fd;
3913 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
3914 {
3915 columns = ts.ts_cols;
3916 rows = ts.ts_lines;
3917 }
3918 }
3919# endif /* TIOCGSIZE */
3920# endif /* TIOCGWINSZ */
3921
3922 /*
3923 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003924 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
3925 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003927 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
3929 if ((p = (char_u *)getenv("LINES")))
3930 rows = atoi((char *)p);
3931 if ((p = (char_u *)getenv("COLUMNS")))
3932 columns = atoi((char *)p);
3933 }
3934
3935#ifdef HAVE_TGETENT
3936 /*
3937 * 3. try reading "co" and "li" entries from termcap
3938 */
3939 if (columns == 0 || rows == 0)
3940 getlinecol(&columns, &rows);
3941#endif
3942
3943 /*
3944 * 4. If everything fails, use the old values
3945 */
3946 if (columns <= 0 || rows <= 0)
3947 return FAIL;
3948
3949 Rows = rows;
3950 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02003951 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 return OK;
3953}
3954
3955/*
3956 * Try to set the window size to Rows and Columns.
3957 */
3958 void
3959mch_set_shellsize()
3960{
3961 if (*T_CWS)
3962 {
3963 /*
3964 * NOTE: if you get an error here that term_set_winsize() is
3965 * undefined, check the output of configure. It could probably not
3966 * find a ncurses, termcap or termlib library.
3967 */
3968 term_set_winsize((int)Rows, (int)Columns);
3969 out_flush();
3970 screen_start(); /* don't know where cursor is now */
3971 }
3972}
3973
3974#endif /* VMS */
3975
3976/*
3977 * Rows and/or Columns has changed.
3978 */
3979 void
3980mch_new_shellsize()
3981{
3982 /* Nothing to do. */
3983}
3984
Bram Moolenaar205b8862011-09-07 15:04:31 +02003985/*
3986 * Wait for process "child" to end.
3987 * Return "child" if it exited properly, <= 0 on error.
3988 */
3989 static pid_t
3990wait4pid(child, status)
3991 pid_t child;
3992 waitstatus *status;
3993{
3994 pid_t wait_pid = 0;
3995
3996 while (wait_pid != child)
3997 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003998 /* When compiled with Python threads are probably used, in which case
3999 * wait() sometimes hangs for no obvious reason. Use waitpid()
4000 * instead and loop (like the GUI). Also needed for other interfaces,
4001 * they might call system(). */
4002# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004003 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004004# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004005 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004006# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004007 if (wait_pid == 0)
4008 {
Bram Moolenaar75676462013-01-30 14:55:42 +01004009 /* Wait for 10 msec before trying again. */
Bram Moolenaar205b8862011-09-07 15:04:31 +02004010 mch_delay(10L, TRUE);
4011 continue;
4012 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004013 if (wait_pid <= 0
4014# ifdef ECHILD
4015 && errno == ECHILD
4016# endif
4017 )
4018 break;
4019 }
4020 return wait_pid;
4021}
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 int
4024mch_call_shell(cmd, options)
4025 char_u *cmd;
4026 int options; /* SHELL_*, see vim.h */
4027{
4028#ifdef VMS
4029 char *ifn = NULL;
4030 char *ofn = NULL;
4031#endif
4032 int tmode = cur_tmode;
4033#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
4034 int x;
4035# ifndef __EMX__
4036 char_u *newcmd; /* only needed for unix */
4037# else
4038 /*
4039 * Set the preferred shell in the EMXSHELL environment variable (but
4040 * only if it is different from what is already in the environment).
4041 * Emx then takes care of whether to use "/c" or "-c" in an
4042 * intelligent way. Simply pass the whole thing to emx's system() call.
4043 * Emx also starts an interactive shell if system() is passed an empty
4044 * string.
4045 */
4046 char_u *p, *old;
4047
4048 if (((old = (char_u *)getenv("EMXSHELL")) == NULL) || STRCMP(old, p_sh))
4049 {
4050 /* should check HAVE_SETENV, but I know we don't have it. */
4051 p = alloc(10 + strlen(p_sh));
4052 if (p)
4053 {
4054 sprintf((char *)p, "EMXSHELL=%s", p_sh);
4055 putenv((char *)p); /* don't free the pointer! */
4056 }
4057 }
4058# endif
4059
4060 out_flush();
4061
4062 if (options & SHELL_COOKED)
4063 settmode(TMODE_COOK); /* set to normal mode */
4064
Bram Moolenaar62b42182010-09-21 22:09:37 +02004065# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004066 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004067 loose_clipboard();
4068# endif
4069
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070# ifdef __EMX__
4071 if (cmd == NULL)
4072 x = system(""); /* this starts an interactive shell in emx */
4073 else
4074 x = system((char *)cmd);
4075 /* system() returns -1 when error occurs in starting shell */
4076 if (x == -1 && !emsg_silent)
4077 {
4078 MSG_PUTS(_("\nCannot execute shell "));
4079 msg_outtrans(p_sh);
4080 msg_putchar('\n');
4081 }
4082# else /* not __EMX__ */
4083 if (cmd == NULL)
4084 x = system((char *)p_sh);
4085 else
4086 {
4087# ifdef VMS
4088 if (ofn = strchr((char *)cmd, '>'))
4089 *ofn++ = '\0';
4090 if (ifn = strchr((char *)cmd, '<'))
4091 {
4092 char *p;
4093
4094 *ifn++ = '\0';
4095 p = strchr(ifn,' '); /* chop off any trailing spaces */
4096 if (p)
4097 *p = '\0';
4098 }
4099 if (ofn)
4100 x = vms_sys((char *)cmd, ofn, ifn);
4101 else
4102 x = system((char *)cmd);
4103# else
4104 newcmd = lalloc(STRLEN(p_sh)
4105 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4106 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4107 if (newcmd == NULL)
4108 x = 0;
4109 else
4110 {
4111 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4112 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4113 (char *)p_shcf,
4114 (char *)cmd);
4115 x = system((char *)newcmd);
4116 vim_free(newcmd);
4117 }
4118# endif
4119 }
4120# ifdef VMS
4121 x = vms_sys_status(x);
4122# endif
4123 if (emsg_silent)
4124 ;
4125 else if (x == 127)
4126 MSG_PUTS(_("\nCannot execute shell sh\n"));
4127# endif /* __EMX__ */
4128 else if (x && !(options & SHELL_SILENT))
4129 {
4130 MSG_PUTS(_("\nshell returned "));
4131 msg_outnum((long)x);
4132 msg_putchar('\n');
4133 }
4134
4135 if (tmode == TMODE_RAW)
4136 settmode(TMODE_RAW); /* set to raw mode */
4137# ifdef FEAT_TITLE
4138 resettitle();
4139# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004140# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4141 restore_clipboard();
4142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 return x;
4144
4145#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4146
Bram Moolenaardf177f62005-02-22 08:39:57 +00004147# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4148 127, some shells use that already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
4150 char_u *newcmd = NULL;
4151 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004152 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 pid_t wait_pid = 0;
4154# ifdef HAVE_UNION_WAIT
4155 union wait status;
4156# else
4157 int status = -1;
4158# endif
4159 int retval = -1;
4160 char **argv = NULL;
4161 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004162 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 int i;
4164 char_u *p;
4165 int inquote;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004167# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 int pty_slave_fd = -1;
4169 char *tty_name;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004170# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004171 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 int fd_fromshell[2];
4173 int pipe_error = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004174# ifdef HAVE_SETENV
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 char envbuf[50];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004176# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 static char envbuf_Rows[20];
4178 static char envbuf_Columns[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004180 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
Bram Moolenaar62b42182010-09-21 22:09:37 +02004182 newcmd = vim_strsave(p_sh);
4183 if (newcmd == NULL) /* out of memory */
4184 goto error;
4185
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 out_flush();
4187 if (options & SHELL_COOKED)
4188 settmode(TMODE_COOK); /* set to normal mode */
4189
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004190 /*
4191 * Do this loop twice:
4192 * 1: find number of arguments
4193 * 2: separate them and build argv[]
4194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 for (i = 0; i < 2; ++i)
4196 {
4197 p = newcmd;
4198 inquote = FALSE;
4199 argc = 0;
4200 for (;;)
4201 {
4202 if (i == 1)
4203 argv[argc] = (char *)p;
4204 ++argc;
4205 while (*p && (inquote || (*p != ' ' && *p != TAB)))
4206 {
4207 if (*p == '"')
4208 inquote = !inquote;
4209 ++p;
4210 }
4211 if (*p == NUL)
4212 break;
4213 if (i == 1)
4214 *p++ = NUL;
4215 p = skipwhite(p);
4216 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004217 if (argv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004219 /*
4220 * Account for possible multiple args in p_shcf.
4221 */
4222 p = p_shcf;
4223 for (;;)
4224 {
4225 p = skiptowhite(p);
4226 if (*p == NUL)
4227 break;
4228 ++argc;
4229 p = skipwhite(p);
4230 }
4231
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 argv = (char **)alloc((unsigned)((argc + 4) * sizeof(char *)));
4233 if (argv == NULL) /* out of memory */
4234 goto error;
4235 }
4236 }
4237 if (cmd != NULL)
4238 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004239 char_u *s;
4240
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 if (extra_shell_arg != NULL)
4242 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004243
4244 /* Break 'shellcmdflag' into white separated parts. This doesn't
4245 * handle quoted strings, they are very unlikely to appear. */
4246 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4247 if (p_shcf_copy == NULL) /* out of memory */
4248 goto error;
4249 s = p_shcf_copy;
4250 p = p_shcf;
4251 while (*p != NUL)
4252 {
4253 argv[argc++] = (char *)s;
4254 while (*p && *p != ' ' && *p != TAB)
4255 *s++ = *p++;
4256 *s++ = NUL;
4257 p = skipwhite(p);
4258 }
4259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 argv[argc++] = (char *)cmd;
4261 }
4262 argv[argc] = NULL;
4263
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004265 * For the GUI, when writing the output into the buffer and when reading
4266 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4267 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004269 if ((options & (SHELL_READ|SHELL_WRITE))
4270# ifdef FEAT_GUI
4271 || (gui.in_use && show_shell_mess)
4272# endif
4273 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004275# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 /*
4277 * Try to open a master pty.
4278 * If this works, open the slave pty.
4279 * If the slave can't be opened, close the master pty.
4280 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004281 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 {
4283 pty_master_fd = OpenPTY(&tty_name); /* open pty */
Bram Moolenaare70172e2011-08-04 19:36:52 +02004284 if (pty_master_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaare70172e2011-08-04 19:36:52 +02004286 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4287 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4288 * adding O_NOCTTY always works when defined. */
4289#ifdef O_NOCTTY
4290 pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4291#else
4292 pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4293#endif
4294 if (pty_slave_fd < 0)
4295 {
4296 close(pty_master_fd);
4297 pty_master_fd = -1;
4298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 }
4301 /*
4302 * If not opening a pty or it didn't work, try using pipes.
4303 */
4304 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004305# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
4307 pipe_error = (pipe(fd_toshell) < 0);
4308 if (!pipe_error) /* pipe create OK */
4309 {
4310 pipe_error = (pipe(fd_fromshell) < 0);
4311 if (pipe_error) /* pipe create failed */
4312 {
4313 close(fd_toshell[0]);
4314 close(fd_toshell[1]);
4315 }
4316 }
4317 if (pipe_error)
4318 {
4319 MSG_PUTS(_("\nCannot create pipes\n"));
4320 out_flush();
4321 }
4322 }
4323 }
4324
4325 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 {
4327# ifdef __BEOS__
4328 beos_cleanup_read_thread();
4329# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004330
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 if ((pid = fork()) == -1) /* maybe we should use vfork() */
4332 {
4333 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004334 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004336 || (gui.in_use && show_shell_mess)
4337# endif
4338 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004340# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 if (pty_master_fd >= 0) /* close the pseudo tty */
4342 {
4343 close(pty_master_fd);
4344 close(pty_slave_fd);
4345 }
4346 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 close(fd_toshell[0]);
4350 close(fd_toshell[1]);
4351 close(fd_fromshell[0]);
4352 close(fd_fromshell[1]);
4353 }
4354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 else if (pid == 0) /* child */
4357 {
4358 reset_signals(); /* handle signals normally */
4359
4360 if (!show_shell_mess || (options & SHELL_EXPAND))
4361 {
4362 int fd;
4363
4364 /*
4365 * Don't want to show any message from the shell. Can't just
4366 * close stdout and stderr though, because some systems will
4367 * break if you try to write to them after that, so we must
4368 * use dup() to replace them with something else -- webb
4369 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4370 * waiting for input.
4371 */
4372 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4373 fclose(stdin);
4374 fclose(stdout);
4375 fclose(stderr);
4376
4377 /*
4378 * If any of these open()'s and dup()'s fail, we just continue
4379 * anyway. It's not fatal, and on most systems it will make
4380 * no difference at all. On a few it will cause the execvp()
4381 * to exit with a non-zero status even when the completion
4382 * could be done, which is nothing too serious. If the open()
4383 * or dup() failed we'd just do the same thing ourselves
4384 * anyway -- webb
4385 */
4386 if (fd >= 0)
4387 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004388 ignored = dup(fd); /* To replace stdin (fd 0) */
4389 ignored = dup(fd); /* To replace stdout (fd 1) */
4390 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
4392 /* Don't need this now that we've duplicated it */
4393 close(fd);
4394 }
4395 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004396 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004398 || gui.in_use
4399# endif
4400 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
4402
Bram Moolenaardf177f62005-02-22 08:39:57 +00004403# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004404 /* Create our own process group, so that the child and all its
4405 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004406 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004407 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004408 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004409 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004410# if defined(SIGHUP)
4411 /* When doing "!xterm&" and 'shell' is bash: the shell
4412 * will exit and send SIGHUP to all processes in its
4413 * group, killing the just started process. Ignore SIGHUP
4414 * to avoid that. (suggested by Simon Schubert)
4415 */
4416 signal(SIGHUP, SIG_IGN);
4417# endif
4418 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004419# endif
4420# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004421 if (pty_slave_fd >= 0)
4422 {
4423 /* push stream discipline modules */
4424 if (options & SHELL_COOKED)
4425 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004427 /* Try to become controlling tty (probably doesn't work,
4428 * unless run by root) */
4429 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004431 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 /* Simulate to have a dumb terminal (for now) */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004434# ifdef HAVE_SETENV
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 setenv("TERM", "dumb", 1);
4436 sprintf((char *)envbuf, "%ld", Rows);
4437 setenv("ROWS", (char *)envbuf, 1);
4438 sprintf((char *)envbuf, "%ld", Rows);
4439 setenv("LINES", (char *)envbuf, 1);
4440 sprintf((char *)envbuf, "%ld", Columns);
4441 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004442# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 /*
4444 * Putenv does not copy the string, it has to remain valid.
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004445 * Use a static array to avoid losing allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 */
4447 putenv("TERM=dumb");
4448 sprintf(envbuf_Rows, "ROWS=%ld", Rows);
4449 putenv(envbuf_Rows);
4450 sprintf(envbuf_Rows, "LINES=%ld", Rows);
4451 putenv(envbuf_Rows);
4452 sprintf(envbuf_Columns, "COLUMNS=%ld", Columns);
4453 putenv(envbuf_Columns);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
Bram Moolenaara5792f52005-11-23 21:25:05 +00004456 /*
4457 * stderr is only redirected when using the GUI, so that a
4458 * program like gpg can still access the terminal to get a
4459 * passphrase using stderr.
4460 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004461# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 if (pty_master_fd >= 0)
4463 {
4464 close(pty_master_fd); /* close master side of pty */
4465
4466 /* set up stdin/stdout/stderr for the child */
4467 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004468 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004470 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004471 if (gui.in_use)
4472 {
4473 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004474 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477 close(pty_slave_fd); /* has been dupped, close it now */
4478 }
4479 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
4482 /* set up stdin for the child */
4483 close(fd_toshell[1]);
4484 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004485 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 close(fd_toshell[0]);
4487
4488 /* set up stdout for the child */
4489 close(fd_fromshell[0]);
4490 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004491 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 close(fd_fromshell[1]);
4493
Bram Moolenaara5792f52005-11-23 21:25:05 +00004494# ifdef FEAT_GUI
4495 if (gui.in_use)
4496 {
4497 /* set up stderr for the child */
4498 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004499 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004500 }
4501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
4503 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 /*
4506 * There is no type cast for the argv, because the type may be
4507 * different on different machines. This may cause a warning
4508 * message with strict compilers, don't worry about it.
4509 * Call _exit() instead of exit() to avoid closing the connection
4510 * to the X server (esp. with GTK, which uses atexit()).
4511 */
4512 execvp(argv[0], argv);
4513 _exit(EXEC_FAILED); /* exec failed, return failure code */
4514 }
4515 else /* parent */
4516 {
4517 /*
4518 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004519 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 */
4521 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004522 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524 /*
4525 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004526 * This is also used to pipe stdin/stdout to/from the external
4527 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004529 if ((options & (SHELL_READ|SHELL_WRITE))
4530# ifdef FEAT_GUI
4531 || (gui.in_use && show_shell_mess)
4532# endif
4533 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004535# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004537# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4541 int ta_len = 0; /* valid bytes in ta_buf[] */
4542 int len;
4543 int p_more_save;
4544 int old_State;
4545 int c;
4546 int toshell_fd;
4547 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004548 garray_T ga;
4549 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004550# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4551 struct timeval start_tv;
4552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Bram Moolenaardf177f62005-02-22 08:39:57 +00004554# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (pty_master_fd >= 0)
4556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 fromshell_fd = pty_master_fd;
4558 toshell_fd = dup(pty_master_fd);
4559 }
4560 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 close(fd_toshell[0]);
4564 close(fd_fromshell[1]);
4565 toshell_fd = fd_toshell[1];
4566 fromshell_fd = fd_fromshell[0];
4567 }
4568
4569 /*
4570 * Write to the child if there are typed characters.
4571 * Read from the child if there are characters available.
4572 * Repeat the reading a few times if more characters are
4573 * available. Need to check for typed keys now and then, but
4574 * not too often (delays when no chars are available).
4575 * This loop is quit if no characters can be read from the pty
4576 * (WaitForChar detected special condition), or there are no
4577 * characters available and the child has exited.
4578 * Only check if the child has exited when there is no more
4579 * output. The child may exit before all the output has
4580 * been printed.
4581 *
4582 * Currently this busy loops!
4583 * This can probably dead-lock when the write blocks!
4584 */
4585 p_more_save = p_more;
4586 p_more = FALSE;
4587 old_State = State;
4588 State = EXTERNCMD; /* don't redraw at window resize */
4589
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004590 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004591 {
4592 /* Fork a process that will write the lines to the
4593 * external program. */
4594 if ((wpid = fork()) == -1)
4595 {
4596 MSG_PUTS(_("\nCannot fork\n"));
4597 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004598 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004599 {
4600 linenr_T lnum = curbuf->b_op_start.lnum;
4601 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004602 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004603 size_t l;
4604
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004605 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004606 for (;;)
4607 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004608 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004609 if (l == 0)
4610 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004611 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004612 /* NL -> NUL translation */
4613 len = write(toshell_fd, "", (size_t)1);
4614 else
4615 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004616 char_u *s = vim_strchr(lp + written, NL);
4617
Bram Moolenaar89d40322006-08-29 15:30:07 +00004618 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004619 s == NULL ? l
4620 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004621 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004622 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004623 {
4624 /* Finished a line, add a NL, unless this line
4625 * should not have one. */
4626 if (lnum != curbuf->b_op_end.lnum
4627 || !curbuf->b_p_bin
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004628 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004629 && (lnum !=
4630 curbuf->b_ml.ml_line_count
4631 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004632 ignored = write(toshell_fd, "\n",
4633 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004634 ++lnum;
4635 if (lnum > curbuf->b_op_end.lnum)
4636 {
4637 /* finished all the lines, close pipe */
4638 close(toshell_fd);
4639 toshell_fd = -1;
4640 break;
4641 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004642 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004643 written = 0;
4644 }
4645 else if (len > 0)
4646 written += len;
4647 }
4648 _exit(0);
4649 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004650 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004651 {
4652 close(toshell_fd);
4653 toshell_fd = -1;
4654 }
4655 }
4656
4657 if (options & SHELL_READ)
4658 ga_init2(&ga, 1, BUFLEN);
4659
4660 noread_cnt = 0;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004661# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4662 gettimeofday(&start_tv, NULL);
4663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 for (;;)
4665 {
4666 /*
4667 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004668 * if there are any.
4669 * Don't do this if we are expanding wild cards (would eat
4670 * typeahead).
4671 * Don't do this when filtering and terminal is in cooked
4672 * mode, the shell command will handle the I/O. Avoids
4673 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004674 * Don't get characters when the child has already
4675 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004676 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004677 * while (noread_cnt > 4), avoids that ":r !ls" eats
4678 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 */
4680 len = 0;
4681 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004682 && ((options &
4683 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4684 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004685# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004686 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004687# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004688 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004689 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004690 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004692 if (ta_len == 0)
4693 {
4694 /* Get extra characters when we don't have any.
4695 * Reset the counter and timer. */
4696 noread_cnt = 0;
4697# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4698 gettimeofday(&start_tv, NULL);
4699# endif
4700 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4701 }
4702 if (ta_len > 0 || len > 0)
4703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 /*
4705 * For pipes:
4706 * Check for CTRL-C: send interrupt signal to child.
4707 * Check for CTRL-D: EOF, close pipe to child.
4708 */
4709 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4710 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004711# ifdef SIGINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 /*
4713 * Send SIGINT to the child's group or all
4714 * processes in our group.
4715 */
4716 if (ta_buf[ta_len] == Ctrl_C
4717 || ta_buf[ta_len] == intr_char)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004718 {
4719# ifdef HAVE_SETSID
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 kill(-pid, SIGINT);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004721# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 kill(0, SIGINT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004724 if (wpid > 0)
4725 kill(wpid, SIGINT);
4726 }
4727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 if (pty_master_fd < 0 && toshell_fd >= 0
4729 && ta_buf[ta_len] == Ctrl_D)
4730 {
4731 close(toshell_fd);
4732 toshell_fd = -1;
4733 }
4734 }
4735
4736 /* replace K_BS by <BS> and K_DEL by <DEL> */
4737 for (i = ta_len; i < ta_len + len; ++i)
4738 {
4739 if (ta_buf[i] == CSI && len - i > 2)
4740 {
4741 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4742 if (c == K_DEL || c == K_KDEL || c == K_BS)
4743 {
4744 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4745 (size_t)(len - i - 2));
4746 if (c == K_DEL || c == K_KDEL)
4747 ta_buf[i] = DEL;
4748 else
4749 ta_buf[i] = Ctrl_H;
4750 len -= 2;
4751 }
4752 }
4753 else if (ta_buf[i] == '\r')
4754 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004755# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004757 i += (*mb_ptr2len_len)(ta_buf + i,
4758 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761
4762 /*
4763 * For pipes: echo the typed characters.
4764 * For a pty this does not seem to work.
4765 */
4766 if (pty_master_fd < 0)
4767 {
4768 for (i = ta_len; i < ta_len + len; ++i)
4769 {
4770 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4771 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004772# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 else if (has_mbyte)
4774 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004775 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777 msg_outtrans_len(ta_buf + i, l);
4778 i += l - 1;
4779 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 else
4782 msg_outtrans_len(ta_buf + i, 1);
4783 }
4784 windgoto(msg_row, msg_col);
4785 out_flush();
4786 }
4787
4788 ta_len += len;
4789
4790 /*
4791 * Write the characters to the child, unless EOF has
4792 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004793 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004794 * When writing buffer lines, drop the typed
4795 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004797 if (options & SHELL_WRITE)
4798 ta_len = 0;
4799 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
4801 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
4802 if (len > 0)
4803 {
4804 ta_len -= len;
4805 mch_memmove(ta_buf, ta_buf + len, ta_len);
4806 }
4807 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 }
4810
Bram Moolenaardf177f62005-02-22 08:39:57 +00004811 if (got_int)
4812 {
4813 /* CTRL-C sends a signal to the child, we ignore it
4814 * ourselves */
4815# ifdef HAVE_SETSID
4816 kill(-pid, SIGINT);
4817# else
4818 kill(0, SIGINT);
4819# endif
4820 if (wpid > 0)
4821 kill(wpid, SIGINT);
4822 got_int = FALSE;
4823 }
4824
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 /*
4826 * Check if the child has any characters to be printed.
4827 * Read them and write them to our window. Repeat this as
4828 * long as there is something to do, avoid the 10ms wait
4829 * for mch_inchar(), or sending typeahead characters to
4830 * the external process.
4831 * TODO: This should handle escape sequences, compatible
4832 * to some terminal (vt52?).
4833 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004834 ++noread_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 while (RealWaitForChar(fromshell_fd, 10L, NULL))
4836 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004837 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00004838# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004840# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00004842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 );
4844 if (len <= 0) /* end of file or error */
4845 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004846
4847 noread_cnt = 0;
4848 if (options & SHELL_READ)
4849 {
4850 /* Do NUL -> NL translation, append NL separated
4851 * lines to the current buffer. */
4852 for (i = 0; i < len; ++i)
4853 {
4854 if (buffer[i] == NL)
4855 append_ga_line(&ga);
4856 else if (buffer[i] == NUL)
4857 ga_append(&ga, NL);
4858 else
4859 ga_append(&ga, buffer[i]);
4860 }
4861 }
4862# ifdef FEAT_MBYTE
4863 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
4865 int l;
4866
Bram Moolenaardf177f62005-02-22 08:39:57 +00004867 len += buffer_off;
4868 buffer[len] = NUL;
4869
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 /* Check if the last character in buffer[] is
4871 * incomplete, keep these bytes for the next
4872 * round. */
4873 for (p = buffer; p < buffer + len; p += l)
4874 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004875 l = mb_cptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (l == 0)
4877 l = 1; /* NUL byte? */
4878 else if (MB_BYTE2LEN(*p) != l)
4879 break;
4880 }
4881 if (p == buffer) /* no complete character */
4882 {
4883 /* avoid getting stuck at an illegal byte */
4884 if (len >= 12)
4885 ++p;
4886 else
4887 {
4888 buffer_off = len;
4889 continue;
4890 }
4891 }
4892 c = *p;
4893 *p = NUL;
4894 msg_puts(buffer);
4895 if (p < buffer + len)
4896 {
4897 *p = c;
4898 buffer_off = (buffer + len) - p;
4899 mch_memmove(buffer, p, buffer_off);
4900 continue;
4901 }
4902 buffer_off = 0;
4903 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004904# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
4907 buffer[len] = NUL;
4908 msg_puts(buffer);
4909 }
4910
4911 windgoto(msg_row, msg_col);
4912 cursor_on();
4913 out_flush();
4914 if (got_int)
4915 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004916
4917# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4918 {
4919 struct timeval now_tv;
4920 long msec;
4921
4922 /* Avoid that we keep looping here without
4923 * checking for a CTRL-C for a long time. Don't
4924 * break out too often to avoid losing typeahead. */
4925 gettimeofday(&now_tv, NULL);
4926 msec = (now_tv.tv_sec - start_tv.tv_sec) * 1000L
4927 + (now_tv.tv_usec - start_tv.tv_usec) / 1000L;
4928 if (msec > 2000)
4929 {
4930 noread_cnt = 5;
4931 break;
4932 }
4933 }
4934# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004937 /* If we already detected the child has finished break the
4938 * loop now. */
4939 if (wait_pid == pid)
4940 break;
4941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 /*
4943 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004944 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004946# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004947 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004948# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4952 || (wait_pid == pid && WIFEXITED(status)))
4953 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004954 /* Don't break the loop yet, try reading more
4955 * characters from "fromshell_fd" first. When using
4956 * pipes there might still be something to read and
4957 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004960 else
4961 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004962
Bram Moolenaar95a51352013-03-21 22:53:50 +01004963# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004964 /* Handle any X events, e.g. serving the clipboard. */
4965 clip_update();
4966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968finished:
4969 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004970 if (options & SHELL_READ)
4971 {
4972 if (ga.ga_len > 0)
4973 {
4974 append_ga_line(&ga);
4975 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004976 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004977 }
4978 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004979 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004980 ga_clear(&ga);
4981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 /*
4984 * Give all typeahead that wasn't used back to ui_inchar().
4985 */
4986 if (ta_len)
4987 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 State = old_State;
4989 if (toshell_fd >= 0)
4990 close(toshell_fd);
4991 close(fromshell_fd);
4992 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01004993# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004994 else
4995 {
4996 /*
4997 * Similar to the loop above, but only handle X events, no
4998 * I/O.
4999 */
5000 for (;;)
5001 {
5002 if (got_int)
5003 {
5004 /* CTRL-C sends a signal to the child, we ignore it
5005 * ourselves */
5006# ifdef HAVE_SETSID
5007 kill(-pid, SIGINT);
5008# else
5009 kill(0, SIGINT);
5010# endif
5011 got_int = FALSE;
5012 }
5013# ifdef __NeXT__
5014 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5015# else
5016 wait_pid = waitpid(pid, &status, WNOHANG);
5017# endif
5018 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5019 || (wait_pid == pid && WIFEXITED(status)))
5020 {
5021 wait_pid = pid;
5022 break;
5023 }
5024
5025 /* Handle any X events, e.g. serving the clipboard. */
5026 clip_update();
5027
5028 mch_delay(10L, TRUE);
5029 }
5030 }
5031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
5033 /*
5034 * Wait until our child has exited.
5035 * Ignore wait() returning pids of other children and returning
5036 * because of some signal like SIGWINCH.
5037 * Don't wait if wait_pid was already set above, indicating the
5038 * child already exited.
5039 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005040 if (wait_pid != pid)
5041 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
Bram Moolenaar624891f2010-10-13 16:22:09 +02005043# ifdef FEAT_GUI
5044 /* Close slave side of pty. Only do this after the child has
5045 * exited, otherwise the child may hang when it tries to write on
5046 * the pty. */
5047 if (pty_master_fd >= 0)
5048 close(pty_slave_fd);
5049# endif
5050
Bram Moolenaardf177f62005-02-22 08:39:57 +00005051 /* Make sure the child that writes to the external program is
5052 * dead. */
5053 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005054 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005055 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005056 wait4pid(wpid, NULL);
5057 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005058
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 /*
5060 * Set to raw mode right now, otherwise a CTRL-C after
5061 * catch_signals() will kill Vim.
5062 */
5063 if (tmode == TMODE_RAW)
5064 settmode(TMODE_RAW);
5065 did_settmode = TRUE;
5066 set_signals();
5067
5068 if (WIFEXITED(status))
5069 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005070 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005072 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
5074 if (retval == EXEC_FAILED)
5075 {
5076 MSG_PUTS(_("\nCannot execute shell "));
5077 msg_outtrans(p_sh);
5078 msg_putchar('\n');
5079 }
5080 else if (!(options & SHELL_SILENT))
5081 {
5082 MSG_PUTS(_("\nshell returned "));
5083 msg_outnum((long)retval);
5084 msg_putchar('\n');
5085 }
5086 }
5087 }
5088 else
5089 MSG_PUTS(_("\nCommand terminated\n"));
5090 }
5091 }
5092 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005093 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095error:
5096 if (!did_settmode)
5097 if (tmode == TMODE_RAW)
5098 settmode(TMODE_RAW); /* set to raw mode */
5099# ifdef FEAT_TITLE
5100 resettitle();
5101# endif
5102 vim_free(newcmd);
5103
5104 return retval;
5105
5106#endif /* USE_SYSTEM */
5107}
5108
5109/*
5110 * Check for CTRL-C typed by reading all available characters.
5111 * In cooked mode we should get SIGINT, no need to check.
5112 */
5113 void
5114mch_breakcheck()
5115{
5116 if (curr_tmode == TMODE_RAW && RealWaitForChar(read_cmd_fd, 0L, NULL))
5117 fill_input_buf(FALSE);
5118}
5119
5120/*
5121 * Wait "msec" msec until a character is available from the keyboard or from
5122 * inbuf[]. msec == -1 will block forever.
5123 * When a GUI is being used, this will never get called -- webb
5124 */
5125 static int
5126WaitForChar(msec)
5127 long msec;
5128{
5129#ifdef FEAT_MOUSE_GPM
5130 int gpm_process_wanted;
5131#endif
5132#ifdef FEAT_XCLIPBOARD
5133 int rest;
5134#endif
5135 int avail;
5136
5137 if (input_available()) /* something in inbuf[] */
5138 return 1;
5139
5140#if defined(FEAT_MOUSE_DEC)
5141 /* May need to query the mouse position. */
5142 if (WantQueryMouse)
5143 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005144 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5146 }
5147#endif
5148
5149 /*
5150 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5151 * events. This is a bit complicated, because they might both be defined.
5152 */
5153#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5154# ifdef FEAT_XCLIPBOARD
5155 rest = 0;
5156 if (do_xterm_trace())
5157 rest = msec;
5158# endif
5159 do
5160 {
5161# ifdef FEAT_XCLIPBOARD
5162 if (rest != 0)
5163 {
5164 msec = XT_TRACE_DELAY;
5165 if (rest >= 0 && rest < XT_TRACE_DELAY)
5166 msec = rest;
5167 if (rest >= 0)
5168 rest -= msec;
5169 }
5170# endif
5171# ifdef FEAT_MOUSE_GPM
5172 gpm_process_wanted = 0;
5173 avail = RealWaitForChar(read_cmd_fd, msec, &gpm_process_wanted);
5174# else
5175 avail = RealWaitForChar(read_cmd_fd, msec, NULL);
5176# endif
5177 if (!avail)
5178 {
5179 if (input_available())
5180 return 1;
5181# ifdef FEAT_XCLIPBOARD
5182 if (rest == 0 || !do_xterm_trace())
5183# endif
5184 break;
5185 }
5186 }
5187 while (FALSE
5188# ifdef FEAT_MOUSE_GPM
5189 || (gpm_process_wanted && mch_gpm_process() == 0)
5190# endif
5191# ifdef FEAT_XCLIPBOARD
5192 || (!avail && rest != 0)
5193# endif
5194 );
5195
5196#else
5197 avail = RealWaitForChar(read_cmd_fd, msec, NULL);
5198#endif
5199 return avail;
5200}
5201
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005202#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203/*
5204 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005205 * "msec" == 0 will check for characters once.
5206 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 * When a GUI is being used, this will not be used for input -- webb
5208 * Returns also, when a request from Sniff is waiting -- toni.
5209 * Or when a Linux GPM mouse event is waiting.
5210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#if defined(__BEOS__)
5212 int
5213#else
5214 static int
5215#endif
5216RealWaitForChar(fd, msec, check_for_gpm)
5217 int fd;
5218 long msec;
Bram Moolenaar78a15312009-05-15 19:33:18 +00005219 int *check_for_gpm UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220{
5221 int ret;
Bram Moolenaar67c53842010-05-22 18:28:27 +02005222#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005223 int nb_fd = netbeans_filedesc();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005224#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005225#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 static int busy = FALSE;
5227
5228 /* May retry getting characters after an event was handled. */
5229# define MAY_LOOP
5230
5231# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
5232 /* Remember at what time we started, so that we know how much longer we
5233 * should wait after being interrupted. */
5234# define USE_START_TV
5235 struct timeval start_tv;
5236
5237 if (msec > 0 && (
5238# ifdef FEAT_XCLIPBOARD
5239 xterm_Shell != (Widget)0
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005240# if defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 ||
5242# endif
5243# endif
5244# ifdef USE_XSMP
5245 xsmp_icefd != -1
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005246# ifdef FEAT_MZSCHEME
5247 ||
5248# endif
5249# endif
5250# ifdef FEAT_MZSCHEME
5251 (mzthreads_allowed() && p_mzq > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252# endif
5253 ))
5254 gettimeofday(&start_tv, NULL);
5255# endif
5256
5257 /* Handle being called recursively. This may happen for the session
5258 * manager stuff, it may save the file, which does a breakcheck. */
5259 if (busy)
5260 return 0;
5261#endif
5262
5263#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005264 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265#endif
5266 {
5267#ifdef MAY_LOOP
5268 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005269# ifdef FEAT_MZSCHEME
5270 int mzquantum_used = FALSE;
5271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272#endif
5273#ifndef HAVE_SELECT
Bram Moolenaar67c53842010-05-22 18:28:27 +02005274 struct pollfd fds[6];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 int nfd;
5276# ifdef FEAT_XCLIPBOARD
5277 int xterm_idx = -1;
5278# endif
5279# ifdef FEAT_MOUSE_GPM
5280 int gpm_idx = -1;
5281# endif
5282# ifdef USE_XSMP
5283 int xsmp_idx = -1;
5284# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005285# ifdef FEAT_NETBEANS_INTG
5286 int nb_idx = -1;
5287# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005288 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005290# ifdef FEAT_MZSCHEME
5291 mzvim_check_threads();
5292 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5293 {
5294 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
5295 mzquantum_used = TRUE;
5296 }
5297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 fds[0].fd = fd;
5299 fds[0].events = POLLIN;
5300 nfd = 1;
5301
5302# ifdef FEAT_SNIFF
5303# define SNIFF_IDX 1
5304 if (want_sniff_request)
5305 {
5306 fds[SNIFF_IDX].fd = fd_from_sniff;
5307 fds[SNIFF_IDX].events = POLLIN;
5308 nfd++;
5309 }
5310# endif
5311# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005312 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 if (xterm_Shell != (Widget)0)
5314 {
5315 xterm_idx = nfd;
5316 fds[nfd].fd = ConnectionNumber(xterm_dpy);
5317 fds[nfd].events = POLLIN;
5318 nfd++;
5319 }
5320# endif
5321# ifdef FEAT_MOUSE_GPM
5322 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5323 {
5324 gpm_idx = nfd;
5325 fds[nfd].fd = gpm_fd;
5326 fds[nfd].events = POLLIN;
5327 nfd++;
5328 }
5329# endif
5330# ifdef USE_XSMP
5331 if (xsmp_icefd != -1)
5332 {
5333 xsmp_idx = nfd;
5334 fds[nfd].fd = xsmp_icefd;
5335 fds[nfd].events = POLLIN;
5336 nfd++;
5337 }
5338# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005339#ifdef FEAT_NETBEANS_INTG
5340 if (nb_fd != -1)
5341 {
5342 nb_idx = nfd;
5343 fds[nfd].fd = nb_fd;
5344 fds[nfd].events = POLLIN;
5345 nfd++;
5346 }
5347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005349 ret = poll(fds, nfd, towait);
5350# ifdef FEAT_MZSCHEME
5351 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005352 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005353 finished = FALSE;
5354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
5356# ifdef FEAT_SNIFF
5357 if (ret < 0)
5358 sniff_disconnect(1);
5359 else if (want_sniff_request)
5360 {
5361 if (fds[SNIFF_IDX].revents & POLLHUP)
5362 sniff_disconnect(1);
5363 if (fds[SNIFF_IDX].revents & POLLIN)
5364 sniff_request_waiting = 1;
5365 }
5366# endif
5367# ifdef FEAT_XCLIPBOARD
5368 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
5369 {
5370 xterm_update(); /* Maybe we should hand out clipboard */
5371 if (--ret == 0 && !input_available())
5372 /* Try again */
5373 finished = FALSE;
5374 }
5375# endif
5376# ifdef FEAT_MOUSE_GPM
5377 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
5378 {
5379 *check_for_gpm = 1;
5380 }
5381# endif
5382# ifdef USE_XSMP
5383 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
5384 {
5385 if (fds[xsmp_idx].revents & POLLIN)
5386 {
5387 busy = TRUE;
5388 xsmp_handle_requests();
5389 busy = FALSE;
5390 }
5391 else if (fds[xsmp_idx].revents & POLLHUP)
5392 {
5393 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005394 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 xsmp_close();
5396 }
5397 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005398 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
5400# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005401#ifdef FEAT_NETBEANS_INTG
5402 if (ret > 0 && nb_idx != -1 && fds[nb_idx].revents & POLLIN)
5403 {
5404 netbeans_read();
5405 --ret;
5406 }
5407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
5409
5410#else /* HAVE_SELECT */
5411
5412 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005413 struct timeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 fd_set rfds, efds;
5415 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005416 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005418# ifdef FEAT_MZSCHEME
5419 mzvim_check_threads();
5420 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5421 {
5422 towait = p_mzq; /* don't wait longer than 'mzquantum' */
5423 mzquantum_used = TRUE;
5424 }
5425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426# ifdef __EMX__
5427 /* don't check for incoming chars if not in raw mode, because select()
5428 * always returns TRUE then (in some version of emx.dll) */
5429 if (curr_tmode != TMODE_RAW)
5430 return 0;
5431# endif
5432
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005433 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005435 tv.tv_sec = towait / 1000;
5436 tv.tv_usec = (towait % 1000) * (1000000/1000);
5437 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005439 else
5440 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
5442 /*
5443 * Select on ready for reading and exceptional condition (end of file).
5444 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005445select_eintr:
5446 FD_ZERO(&rfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 FD_ZERO(&efds);
5448 FD_SET(fd, &rfds);
5449# if !defined(__QNX__) && !defined(__CYGWIN32__)
5450 /* For QNX select() always returns 1 if this is set. Why? */
5451 FD_SET(fd, &efds);
5452# endif
5453 maxfd = fd;
5454
5455# ifdef FEAT_SNIFF
5456 if (want_sniff_request)
5457 {
5458 FD_SET(fd_from_sniff, &rfds);
5459 FD_SET(fd_from_sniff, &efds);
5460 if (maxfd < fd_from_sniff)
5461 maxfd = fd_from_sniff;
5462 }
5463# endif
5464# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005465 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 if (xterm_Shell != (Widget)0)
5467 {
5468 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
5469 if (maxfd < ConnectionNumber(xterm_dpy))
5470 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005471
5472 /* An event may have already been read but not handled. In
5473 * particulary, XFlush may cause this. */
5474 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 }
5476# endif
5477# ifdef FEAT_MOUSE_GPM
5478 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5479 {
5480 FD_SET(gpm_fd, &rfds);
5481 FD_SET(gpm_fd, &efds);
5482 if (maxfd < gpm_fd)
5483 maxfd = gpm_fd;
5484 }
5485# endif
5486# ifdef USE_XSMP
5487 if (xsmp_icefd != -1)
5488 {
5489 FD_SET(xsmp_icefd, &rfds);
5490 FD_SET(xsmp_icefd, &efds);
5491 if (maxfd < xsmp_icefd)
5492 maxfd = xsmp_icefd;
5493 }
5494# endif
Bram Moolenaardd82d692012-08-15 17:26:57 +02005495# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar67c53842010-05-22 18:28:27 +02005496 if (nb_fd != -1)
5497 {
5498 FD_SET(nb_fd, &rfds);
5499 if (maxfd < nb_fd)
5500 maxfd = nb_fd;
5501 }
Bram Moolenaardd82d692012-08-15 17:26:57 +02005502# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005504 ret = select(maxfd + 1, &rfds, NULL, &efds, tvp);
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005505# ifdef EINTR
5506 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005507 {
5508 /* Check whether window has been resized, EINTR may be caused by
5509 * SIGWINCH. */
5510 if (do_resize)
5511 handle_resize();
5512
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005513 /* Interrupted by a signal, need to try again. We ignore msec
5514 * here, because we do want to check even after a timeout if
5515 * characters are available. Needed for reading output of an
5516 * external command after the process has finished. */
5517 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005518 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005519# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00005520# ifdef __TANDEM
5521 if (ret == -1 && errno == ENOTSUP)
5522 {
5523 FD_ZERO(&rfds);
5524 FD_ZERO(&efds);
5525 ret = 0;
5526 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005527# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005528# ifdef FEAT_MZSCHEME
5529 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005530 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005531 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532# endif
5533
5534# ifdef FEAT_SNIFF
5535 if (ret < 0 )
5536 sniff_disconnect(1);
5537 else if (ret > 0 && want_sniff_request)
5538 {
5539 if (FD_ISSET(fd_from_sniff, &efds))
5540 sniff_disconnect(1);
5541 if (FD_ISSET(fd_from_sniff, &rfds))
5542 sniff_request_waiting = 1;
5543 }
5544# endif
5545# ifdef FEAT_XCLIPBOARD
5546 if (ret > 0 && xterm_Shell != (Widget)0
5547 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
5548 {
5549 xterm_update(); /* Maybe we should hand out clipboard */
5550 /* continue looping when we only got the X event and the input
5551 * buffer is empty */
5552 if (--ret == 0 && !input_available())
5553 {
5554 /* Try again */
5555 finished = FALSE;
5556 }
5557 }
5558# endif
5559# ifdef FEAT_MOUSE_GPM
5560 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
5561 {
5562 if (FD_ISSET(gpm_fd, &efds))
5563 gpm_close();
5564 else if (FD_ISSET(gpm_fd, &rfds))
5565 *check_for_gpm = 1;
5566 }
5567# endif
5568# ifdef USE_XSMP
5569 if (ret > 0 && xsmp_icefd != -1)
5570 {
5571 if (FD_ISSET(xsmp_icefd, &efds))
5572 {
5573 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005574 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 xsmp_close();
5576 if (--ret == 0)
5577 finished = FALSE; /* keep going if event was only one */
5578 }
5579 else if (FD_ISSET(xsmp_icefd, &rfds))
5580 {
5581 busy = TRUE;
5582 xsmp_handle_requests();
5583 busy = FALSE;
5584 if (--ret == 0)
5585 finished = FALSE; /* keep going if event was only one */
5586 }
5587 }
5588# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005589#ifdef FEAT_NETBEANS_INTG
5590 if (ret > 0 && nb_fd != -1 && FD_ISSET(nb_fd, &rfds))
5591 {
5592 netbeans_read();
5593 --ret;
5594 }
5595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
5597#endif /* HAVE_SELECT */
5598
5599#ifdef MAY_LOOP
5600 if (finished || msec == 0)
5601 break;
5602
5603 /* We're going to loop around again, find out for how long */
5604 if (msec > 0)
5605 {
5606# ifdef USE_START_TV
5607 struct timeval mtv;
5608
5609 /* Compute remaining wait time. */
5610 gettimeofday(&mtv, NULL);
5611 msec -= (mtv.tv_sec - start_tv.tv_sec) * 1000L
5612 + (mtv.tv_usec - start_tv.tv_usec) / 1000L;
5613# else
5614 /* Guess we got interrupted halfway. */
5615 msec = msec / 2;
5616# endif
5617 if (msec <= 0)
5618 break; /* waited long enough */
5619 }
5620#endif
5621 }
5622
5623 return (ret > 0);
5624}
5625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627/*
Bram Moolenaar02743632005-07-25 20:42:36 +00005628 * Expand a path into all matching files and/or directories. Handles "*",
5629 * "?", "[a-z]", "**", etc.
5630 * "path" has backslashes before chars that are not to be expanded.
5631 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 */
5633 int
5634mch_expandpath(gap, path, flags)
5635 garray_T *gap;
5636 char_u *path;
5637 int flags; /* EW_* flags */
5638{
Bram Moolenaar02743632005-07-25 20:42:36 +00005639 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
5641#endif
5642
5643/*
5644 * mch_expand_wildcards() - this code does wild-card pattern matching using
5645 * the shell
5646 *
5647 * return OK for success, FAIL for error (you may lose some memory) and put
5648 * an error message in *file.
5649 *
5650 * num_pat is number of input patterns
5651 * pat is array of pointers to input patterns
5652 * num_file is pointer to number of matched file names
5653 * file is pointer to array of pointers to matched file names
5654 */
5655
5656#ifndef SEEK_SET
5657# define SEEK_SET 0
5658#endif
5659#ifndef SEEK_END
5660# define SEEK_END 2
5661#endif
5662
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005663#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00005664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 int
5666mch_expand_wildcards(num_pat, pat, num_file, file, flags)
5667 int num_pat;
5668 char_u **pat;
5669 int *num_file;
5670 char_u ***file;
5671 int flags; /* EW_* flags */
5672{
5673 int i;
5674 size_t len;
5675 char_u *p;
5676 int dir;
5677#ifdef __EMX__
Bram Moolenaarc7247912008-01-13 12:54:11 +00005678 /*
5679 * This is the OS/2 implementation.
5680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681# define EXPL_ALLOC_INC 16
5682 char_u **expl_files;
5683 size_t files_alloced, files_free;
5684 char_u *buf;
5685 int has_wildcard;
5686
5687 *num_file = 0; /* default: no files found */
5688 files_alloced = EXPL_ALLOC_INC; /* how much space is allocated */
5689 files_free = EXPL_ALLOC_INC; /* how much space is not used */
5690 *file = (char_u **)alloc(sizeof(char_u **) * files_alloced);
5691 if (*file == NULL)
5692 return FAIL;
5693
5694 for (; num_pat > 0; num_pat--, pat++)
5695 {
5696 expl_files = NULL;
5697 if (vim_strchr(*pat, '$') || vim_strchr(*pat, '~'))
5698 /* expand environment var or home dir */
5699 buf = expand_env_save(*pat);
5700 else
5701 buf = vim_strsave(*pat);
5702 expl_files = NULL;
Bram Moolenaard8b02732005-01-14 21:48:43 +00005703 has_wildcard = mch_has_exp_wildcard(buf); /* (still) wildcards? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (has_wildcard) /* yes, so expand them */
5705 expl_files = (char_u **)_fnexplode(buf);
5706
5707 /*
5708 * return value of buf if no wildcards left,
5709 * OR if no match AND EW_NOTFOUND is set.
5710 */
5711 if ((!has_wildcard && ((flags & EW_NOTFOUND) || mch_getperm(buf) >= 0))
5712 || (expl_files == NULL && (flags & EW_NOTFOUND)))
5713 { /* simply save the current contents of *buf */
5714 expl_files = (char_u **)alloc(sizeof(char_u **) * 2);
5715 if (expl_files != NULL)
5716 {
5717 expl_files[0] = vim_strsave(buf);
5718 expl_files[1] = NULL;
5719 }
5720 }
5721 vim_free(buf);
5722
5723 /*
5724 * Count number of names resulting from expansion,
5725 * At the same time add a backslash to the end of names that happen to
5726 * be directories, and replace slashes with backslashes.
5727 */
5728 if (expl_files)
5729 {
5730 for (i = 0; (p = expl_files[i]) != NULL; i++)
5731 {
5732 dir = mch_isdir(p);
5733 /* If we don't want dirs and this is one, skip it */
5734 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
5735 continue;
5736
Bram Moolenaara2031822006-03-07 22:29:51 +00005737 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01005738 if (!dir && (flags & EW_EXEC)
5739 && !mch_can_exe(p, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00005740 continue;
5741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 if (--files_free == 0)
5743 {
5744 /* need more room in table of pointers */
5745 files_alloced += EXPL_ALLOC_INC;
5746 *file = (char_u **)vim_realloc(*file,
5747 sizeof(char_u **) * files_alloced);
5748 if (*file == NULL)
5749 {
5750 EMSG(_(e_outofmem));
5751 *num_file = 0;
5752 return FAIL;
5753 }
5754 files_free = EXPL_ALLOC_INC;
5755 }
5756 slash_adjust(p);
5757 if (dir)
5758 {
5759 /* For a directory we add a '/', unless it's already
5760 * there. */
5761 len = STRLEN(p);
5762 if (((*file)[*num_file] = alloc(len + 2)) != NULL)
5763 {
5764 STRCPY((*file)[*num_file], p);
Bram Moolenaar654b5b52006-06-22 17:47:10 +00005765 if (!after_pathsep((*file)[*num_file],
5766 (*file)[*num_file] + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 {
5768 (*file)[*num_file][len] = psepc;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005769 (*file)[*num_file][len + 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771 }
5772 }
5773 else
5774 {
5775 (*file)[*num_file] = vim_strsave(p);
5776 }
5777
5778 /*
5779 * Error message already given by either alloc or vim_strsave.
5780 * Should return FAIL, but returning OK works also.
5781 */
5782 if ((*file)[*num_file] == NULL)
5783 break;
5784 (*num_file)++;
5785 }
5786 _fnexplodefree((char **)expl_files);
5787 }
5788 }
5789 return OK;
5790
5791#else /* __EMX__ */
Bram Moolenaarc7247912008-01-13 12:54:11 +00005792 /*
5793 * This is the non-OS/2 implementation (really Unix).
5794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 int j;
5796 char_u *tempname;
5797 char_u *command;
5798 FILE *fd;
5799 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005800#define STYLE_ECHO 0 /* use "echo", the default */
5801#define STYLE_GLOB 1 /* use "glob", for csh */
5802#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
5803#define STYLE_PRINT 3 /* use "print -N", for zsh */
5804#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
5805 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 int shell_style = STYLE_ECHO;
5807 int check_spaces;
5808 static int did_find_nul = FALSE;
5809 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005810 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005811 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812
5813 *num_file = 0; /* default: no files found */
5814 *file = NULL;
5815
5816 /*
5817 * If there are no wildcards, just copy the names to allocated memory.
5818 * Saves a lot of time, because we don't have to start a new shell.
5819 */
5820 if (!have_wildcard(num_pat, pat))
5821 return save_patterns(num_pat, pat, num_file, file);
5822
Bram Moolenaar0e634da2005-07-20 21:57:28 +00005823# ifdef HAVE_SANDBOX
5824 /* Don't allow any shell command in the sandbox. */
5825 if (sandbox != 0 && check_secure())
5826 return FAIL;
5827# endif
5828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 /*
5830 * Don't allow the use of backticks in secure and restricted mode.
5831 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00005832 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 for (i = 0; i < num_pat; ++i)
5834 if (vim_strchr(pat[i], '`') != NULL
5835 && (check_restricted() || check_secure()))
5836 return FAIL;
5837
5838 /*
5839 * get a name for the temp file
5840 */
5841 if ((tempname = vim_tempname('o')) == NULL)
5842 {
5843 EMSG(_(e_notmp));
5844 return FAIL;
5845 }
5846
5847 /*
5848 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00005849 * file.
5850 * STYLE_BT: NL separated
5851 * If expanding `cmd` execute it directly.
5852 * STYLE_GLOB: NUL separated
5853 * If we use *csh, "glob" will work better than "echo".
5854 * STYLE_PRINT: NL or NUL separated
5855 * If we use *zsh, "print -N" will work better than "glob".
5856 * STYLE_VIMGLOB: NL separated
5857 * If we use *sh*, we define "vimglob()".
5858 * STYLE_ECHO: space separated.
5859 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 */
5861 if (num_pat == 1 && *pat[0] == '`'
5862 && (len = STRLEN(pat[0])) > 2
5863 && *(pat[0] + len - 1) == '`')
5864 shell_style = STYLE_BT;
5865 else if ((len = STRLEN(p_sh)) >= 3)
5866 {
5867 if (STRCMP(p_sh + len - 3, "csh") == 0)
5868 shell_style = STYLE_GLOB;
5869 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
5870 shell_style = STYLE_PRINT;
5871 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00005872 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
5873 "sh") != NULL)
5874 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
Bram Moolenaarc7247912008-01-13 12:54:11 +00005876 /* Compute the length of the command. We need 2 extra bytes: for the
5877 * optional '&' and for the NUL.
5878 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005880 if (shell_style == STYLE_VIMGLOB)
5881 len += STRLEN(sh_vimglob_func);
5882
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005883 for (i = 0; i < num_pat; ++i)
5884 {
5885 /* Count the length of the patterns in the same way as they are put in
5886 * "command" below. */
5887#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005889#else
5890 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00005891 for (j = 0; pat[i][j] != NUL; ++j)
5892 {
5893 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
5894 ++len; /* may add a backslash */
5895 ++len;
5896 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005897#endif
5898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 command = alloc(len);
5900 if (command == NULL)
5901 {
5902 /* out of memory */
5903 vim_free(tempname);
5904 return FAIL;
5905 }
5906
5907 /*
5908 * Build the shell command:
5909 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
5910 * recognizes this).
5911 * - Add the shell command to print the expanded names.
5912 * - Add the temp file name.
5913 * - Add the file name patterns.
5914 */
5915 if (shell_style == STYLE_BT)
5916 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00005917 /* change `command; command& ` to (command; command ) */
5918 STRCPY(command, "(");
5919 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005921 *p-- = ')'; /* remove last backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 while (p > command && vim_iswhite(*p))
5923 --p;
5924 if (*p == '&') /* remove trailing '&' */
5925 {
5926 ampersent = TRUE;
5927 *p = ' ';
5928 }
5929 STRCAT(command, ">");
5930 }
5931 else
5932 {
5933 if (flags & EW_NOTFOUND)
5934 STRCPY(command, "set nonomatch; ");
5935 else
5936 STRCPY(command, "unset nonomatch; ");
5937 if (shell_style == STYLE_GLOB)
5938 STRCAT(command, "glob >");
5939 else if (shell_style == STYLE_PRINT)
5940 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00005941 else if (shell_style == STYLE_VIMGLOB)
5942 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 else
5944 STRCAT(command, "echo >");
5945 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00005946
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00005948
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 if (shell_style != STYLE_BT)
5950 for (i = 0; i < num_pat; ++i)
5951 {
5952 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00005953 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005954 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955#ifdef USE_SYSTEM
5956 STRCAT(command, " \"");
5957 STRCAT(command, pat[i]);
5958 STRCAT(command, "\"");
5959#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00005960 int intick = FALSE;
5961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 p = command + STRLEN(command);
5963 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00005964 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005965 {
5966 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00005967 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005968 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
5969 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005970 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00005971 * backslash inside backticks, before a special character
5972 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005973 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00005974 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
5975 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005976 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00005977 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005978 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02005979 else if (!intick
5980 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
5981 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00005982 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02005983 * when inside ``. And not for $var when EW_KEEPDOLLAR is
5984 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00005985 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00005986
5987 /* Copy one character. */
5988 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00005989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 *p = NUL;
5991#endif
5992 }
5993 if (flags & EW_SILENT)
5994 show_shell_mess = FALSE;
5995 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00005996 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
5998 /*
5999 * Using zsh -G: If a pattern has no matches, it is just deleted from
6000 * the argument list, otherwise zsh gives an error message and doesn't
6001 * expand any other pattern.
6002 */
6003 if (shell_style == STYLE_PRINT)
6004 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6005
6006 /*
6007 * If we use -f then shell variables set in .cshrc won't get expanded.
6008 * vi can do it, so we will too, but it is only necessary if there is a "$"
6009 * in one of the patterns, otherwise we can still use the fast option.
6010 */
6011 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6012 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6013
6014 /*
6015 * execute the shell command
6016 */
6017 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6018
6019 /* When running in the background, give it some time to create the temp
6020 * file, but don't wait for it to finish. */
6021 if (ampersent)
6022 mch_delay(10L, TRUE);
6023
6024 extra_shell_arg = NULL; /* cleanup */
6025 show_shell_mess = TRUE;
6026 vim_free(command);
6027
Bram Moolenaarc7247912008-01-13 12:54:11 +00006028 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 {
6030 mch_remove(tempname);
6031 vim_free(tempname);
6032 /*
6033 * With interactive completion, the error message is not printed.
6034 * However with USE_SYSTEM, I don't know how to turn off error messages
6035 * from the shell, so screen may still get messed up -- webb.
6036 */
6037#ifndef USE_SYSTEM
6038 if (!(flags & EW_SILENT))
6039#endif
6040 {
6041 redraw_later_clear(); /* probably messed up screen */
6042 msg_putchar('\n'); /* clear bottom line quickly */
6043 cmdline_row = Rows - 1; /* continue on last line */
6044#ifdef USE_SYSTEM
6045 if (!(flags & EW_SILENT))
6046#endif
6047 {
6048 MSG(_(e_wildexpand));
6049 msg_start(); /* don't overwrite this message */
6050 }
6051 }
6052 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6053 * EW_NOTFOUND is given */
6054 if (shell_style == STYLE_BT)
6055 return FAIL;
6056 goto notfound;
6057 }
6058
6059 /*
6060 * read the names from the file into memory
6061 */
6062 fd = fopen((char *)tempname, READBIN);
6063 if (fd == NULL)
6064 {
6065 /* Something went wrong, perhaps a file name with a special char. */
6066 if (!(flags & EW_SILENT))
6067 {
6068 MSG(_(e_wildexpand));
6069 msg_start(); /* don't overwrite this message */
6070 }
6071 vim_free(tempname);
6072 goto notfound;
6073 }
6074 fseek(fd, 0L, SEEK_END);
6075 len = ftell(fd); /* get size of temp file */
6076 fseek(fd, 0L, SEEK_SET);
6077 buffer = alloc(len + 1);
6078 if (buffer == NULL)
6079 {
6080 /* out of memory */
6081 mch_remove(tempname);
6082 vim_free(tempname);
6083 fclose(fd);
6084 return FAIL;
6085 }
6086 i = fread((char *)buffer, 1, len, fd);
6087 fclose(fd);
6088 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006089 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 {
6091 /* unexpected read error */
6092 EMSG2(_(e_notread), tempname);
6093 vim_free(tempname);
6094 vim_free(buffer);
6095 return FAIL;
6096 }
6097 vim_free(tempname);
6098
Bram Moolenaarc7247912008-01-13 12:54:11 +00006099# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6101 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006102 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6104 *p++ = buffer[i];
6105 len = p - buffer;
6106# endif
6107
6108
6109 /* file names are separated with Space */
6110 if (shell_style == STYLE_ECHO)
6111 {
6112 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6113 p = buffer;
6114 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6115 {
6116 while (*p != ' ' && *p != '\n')
6117 ++p;
6118 p = skipwhite(p); /* skip to next entry */
6119 }
6120 }
6121 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006122 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 {
6124 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6125 p = buffer;
6126 for (i = 0; *p != NUL; ++i) /* count number of entries */
6127 {
6128 while (*p != '\n' && *p != NUL)
6129 ++p;
6130 if (*p != NUL)
6131 ++p;
6132 p = skipwhite(p); /* skip leading white space */
6133 }
6134 }
6135 /* file names are separated with NUL */
6136 else
6137 {
6138 /*
6139 * Some versions of zsh use spaces instead of NULs to separate
6140 * results. Only do this when there is no NUL before the end of the
6141 * buffer, otherwise we would never be able to use file names with
6142 * embedded spaces when zsh does use NULs.
6143 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6144 * don't check for spaces again.
6145 */
6146 check_spaces = FALSE;
6147 if (shell_style == STYLE_PRINT && !did_find_nul)
6148 {
6149 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006150 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006151 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 did_find_nul = TRUE;
6153 else
6154 check_spaces = TRUE;
6155 }
6156
6157 /*
6158 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6159 * already is one, for STYLE_GLOB it needs to be added.
6160 */
6161 if (len && buffer[len - 1] == NUL)
6162 --len;
6163 else
6164 buffer[len] = NUL;
6165 i = 0;
6166 for (p = buffer; p < buffer + len; ++p)
6167 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6168 {
6169 ++i;
6170 *p = NUL;
6171 }
6172 if (len)
6173 ++i; /* count last entry */
6174 }
6175 if (i == 0)
6176 {
6177 /*
6178 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6179 * /bin/sh will happily expand it to nothing rather than returning an
6180 * error; and hey, it's good to check anyway -- webb.
6181 */
6182 vim_free(buffer);
6183 goto notfound;
6184 }
6185 *num_file = i;
6186 *file = (char_u **)alloc(sizeof(char_u *) * i);
6187 if (*file == NULL)
6188 {
6189 /* out of memory */
6190 vim_free(buffer);
6191 return FAIL;
6192 }
6193
6194 /*
6195 * Isolate the individual file names.
6196 */
6197 p = buffer;
6198 for (i = 0; i < *num_file; ++i)
6199 {
6200 (*file)[i] = p;
6201 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006202 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6203 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006205 while (!(shell_style == STYLE_ECHO && *p == ' ')
6206 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 ++p;
6208 if (p == buffer + len) /* last entry */
6209 *p = NUL;
6210 else
6211 {
6212 *p++ = NUL;
6213 p = skipwhite(p); /* skip to next entry */
6214 }
6215 }
6216 else /* NUL separates */
6217 {
6218 while (*p && p < buffer + len) /* skip entry */
6219 ++p;
6220 ++p; /* skip NUL */
6221 }
6222 }
6223
6224 /*
6225 * Move the file names to allocated memory.
6226 */
6227 for (j = 0, i = 0; i < *num_file; ++i)
6228 {
6229 /* Require the files to exist. Helps when using /bin/sh */
6230 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6231 continue;
6232
6233 /* check if this entry should be included */
6234 dir = (mch_isdir((*file)[i]));
6235 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6236 continue;
6237
Bram Moolenaara2031822006-03-07 22:29:51 +00006238 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006239 if (!dir && (flags & EW_EXEC)
6240 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006241 continue;
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6244 if (p)
6245 {
6246 STRCPY(p, (*file)[i]);
6247 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006248 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 (*file)[j++] = p;
6250 }
6251 }
6252 vim_free(buffer);
6253 *num_file = j;
6254
6255 if (*num_file == 0) /* rejected all entries */
6256 {
6257 vim_free(*file);
6258 *file = NULL;
6259 goto notfound;
6260 }
6261
6262 return OK;
6263
6264notfound:
6265 if (flags & EW_NOTFOUND)
6266 return save_patterns(num_pat, pat, num_file, file);
6267 return FAIL;
6268
6269#endif /* __EMX__ */
6270}
6271
6272#endif /* VMS */
6273
6274#ifndef __EMX__
6275 static int
6276save_patterns(num_pat, pat, num_file, file)
6277 int num_pat;
6278 char_u **pat;
6279 int *num_file;
6280 char_u ***file;
6281{
6282 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006283 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
6285 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6286 if (*file == NULL)
6287 return FAIL;
6288 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006289 {
6290 s = vim_strsave(pat[i]);
6291 if (s != NULL)
6292 /* Be compatible with expand_filename(): halve the number of
6293 * backslashes. */
6294 backslash_halve(s);
6295 (*file)[i] = s;
6296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 *num_file = num_pat;
6298 return OK;
6299}
6300#endif
6301
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302/*
6303 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6304 * expand.
6305 */
6306 int
6307mch_has_exp_wildcard(p)
6308 char_u *p;
6309{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006310 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 {
6312#ifndef OS2
6313 if (*p == '\\' && p[1] != NUL)
6314 ++p;
6315 else
6316#endif
6317 if (vim_strchr((char_u *)
6318#ifdef VMS
6319 "*?%"
6320#else
6321# ifdef OS2
6322 "*?"
6323# else
6324 "*?[{'"
6325# endif
6326#endif
6327 , *p) != NULL)
6328 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 }
6330 return FALSE;
6331}
6332
6333/*
6334 * Return TRUE if the string "p" contains a wildcard.
6335 * Don't recognize '~' at the end as a wildcard.
6336 */
6337 int
6338mch_has_wildcard(p)
6339 char_u *p;
6340{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006341 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
6343#ifndef OS2
6344 if (*p == '\\' && p[1] != NUL)
6345 ++p;
6346 else
6347#endif
6348 if (vim_strchr((char_u *)
6349#ifdef VMS
6350 "*?%$"
6351#else
6352# ifdef OS2
6353# ifdef VIM_BACKTICK
6354 "*?$`"
6355# else
6356 "*?$"
6357# endif
6358# else
6359 "*?[{`'$"
6360# endif
6361#endif
6362 , *p) != NULL
6363 || (*p == '~' && p[1] != NUL))
6364 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 }
6366 return FALSE;
6367}
6368
6369#ifndef __EMX__
6370 static int
6371have_wildcard(num, file)
6372 int num;
6373 char_u **file;
6374{
6375 int i;
6376
6377 for (i = 0; i < num; i++)
6378 if (mch_has_wildcard(file[i]))
6379 return 1;
6380 return 0;
6381}
6382
6383 static int
6384have_dollars(num, file)
6385 int num;
6386 char_u **file;
6387{
6388 int i;
6389
6390 for (i = 0; i < num; i++)
6391 if (vim_strchr(file[i], '$') != NULL)
6392 return TRUE;
6393 return FALSE;
6394}
6395#endif /* ifndef __EMX__ */
6396
6397#ifndef HAVE_RENAME
6398/*
6399 * Scaled-down version of rename(), which is missing in Xenix.
6400 * This version can only move regular files and will fail if the
6401 * destination exists.
6402 */
6403 int
6404mch_rename(src, dest)
6405 const char *src, *dest;
6406{
6407 struct stat st;
6408
6409 if (stat(dest, &st) >= 0) /* fail if destination exists */
6410 return -1;
6411 if (link(src, dest) != 0) /* link file to new name */
6412 return -1;
6413 if (mch_remove(src) == 0) /* delete link to old name */
6414 return 0;
6415 return -1;
6416}
6417#endif /* !HAVE_RENAME */
6418
6419#ifdef FEAT_MOUSE_GPM
6420/*
6421 * Initializes connection with gpm (if it isn't already opened)
6422 * Return 1 if succeeded (or connection already opened), 0 if failed
6423 */
6424 static int
6425gpm_open()
6426{
6427 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6428
6429 if (!gpm_flag)
6430 {
6431 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6432 gpm_connect.defaultMask = ~GPM_HARD;
6433 /* Default handling for mouse move*/
6434 gpm_connect.minMod = 0; /* Handle any modifier keys */
6435 gpm_connect.maxMod = 0xffff;
6436 if (Gpm_Open(&gpm_connect, 0) > 0)
6437 {
6438 /* gpm library tries to handling TSTP causes
6439 * problems. Anyways, we close connection to Gpm whenever
6440 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006441 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006443# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 return 1; /* succeed */
6447 }
6448 if (gpm_fd == -2)
6449 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6450 return 0;
6451 }
6452 return 1; /* already open */
6453}
6454
6455/*
6456 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 */
6458 static void
6459gpm_close()
6460{
6461 if (gpm_flag && gpm_fd >= 0) /* if Open */
6462 Gpm_Close();
6463}
6464
6465/* Reads gpm event and adds special keys to input buf. Returns length of
6466 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02006467 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 */
6469 static int
6470mch_gpm_process()
6471{
6472 int button;
6473 static Gpm_Event gpm_event;
6474 char_u string[6];
6475 int_u vim_modifiers;
6476 int row,col;
6477 unsigned char buttons_mask;
6478 unsigned char gpm_modifiers;
6479 static unsigned char old_buttons = 0;
6480
6481 Gpm_GetEvent(&gpm_event);
6482
6483#ifdef FEAT_GUI
6484 /* Don't put events in the input queue now. */
6485 if (hold_gui_events)
6486 return 0;
6487#endif
6488
6489 row = gpm_event.y - 1;
6490 col = gpm_event.x - 1;
6491
6492 string[0] = ESC; /* Our termcode */
6493 string[1] = 'M';
6494 string[2] = 'G';
6495 switch (GPM_BARE_EVENTS(gpm_event.type))
6496 {
6497 case GPM_DRAG:
6498 string[3] = MOUSE_DRAG;
6499 break;
6500 case GPM_DOWN:
6501 buttons_mask = gpm_event.buttons & ~old_buttons;
6502 old_buttons = gpm_event.buttons;
6503 switch (buttons_mask)
6504 {
6505 case GPM_B_LEFT:
6506 button = MOUSE_LEFT;
6507 break;
6508 case GPM_B_MIDDLE:
6509 button = MOUSE_MIDDLE;
6510 break;
6511 case GPM_B_RIGHT:
6512 button = MOUSE_RIGHT;
6513 break;
6514 default:
6515 return 0;
6516 /*Don't know what to do. Can more than one button be
6517 * reported in one event? */
6518 }
6519 string[3] = (char_u)(button | 0x20);
6520 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
6521 break;
6522 case GPM_UP:
6523 string[3] = MOUSE_RELEASE;
6524 old_buttons &= ~gpm_event.buttons;
6525 break;
6526 default:
6527 return 0;
6528 }
6529 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
6530 gpm_modifiers = gpm_event.modifiers;
6531 vim_modifiers = 0x0;
6532 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
6533 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
6534 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
6535 */
6536 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
6537 vim_modifiers |= MOUSE_SHIFT;
6538
6539 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
6540 vim_modifiers |= MOUSE_CTRL;
6541 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
6542 vim_modifiers |= MOUSE_ALT;
6543 string[3] |= vim_modifiers;
6544 string[4] = (char_u)(col + ' ' + 1);
6545 string[5] = (char_u)(row + ' ' + 1);
6546 add_to_input_buf(string, 6);
6547 return 6;
6548}
6549#endif /* FEAT_MOUSE_GPM */
6550
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006551#ifdef FEAT_SYSMOUSE
6552/*
6553 * Initialize connection with sysmouse.
6554 * Let virtual console inform us with SIGUSR2 for pending sysmouse
6555 * output, any sysmouse output than will be processed via sig_sysmouse().
6556 * Return OK if succeeded, FAIL if failed.
6557 */
6558 static int
6559sysmouse_open()
6560{
6561 struct mouse_info mouse;
6562
6563 mouse.operation = MOUSE_MODE;
6564 mouse.u.mode.mode = 0;
6565 mouse.u.mode.signal = SIGUSR2;
6566 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
6567 {
6568 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
6569 mouse.operation = MOUSE_SHOW;
6570 ioctl(1, CONS_MOUSECTL, &mouse);
6571 return OK;
6572 }
6573 return FAIL;
6574}
6575
6576/*
6577 * Stop processing SIGUSR2 signals, and also make sure that
6578 * virtual console do not send us any sysmouse related signal.
6579 */
6580 static void
6581sysmouse_close()
6582{
6583 struct mouse_info mouse;
6584
6585 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
6586 mouse.operation = MOUSE_MODE;
6587 mouse.u.mode.mode = 0;
6588 mouse.u.mode.signal = 0;
6589 ioctl(1, CONS_MOUSECTL, &mouse);
6590}
6591
6592/*
6593 * Gets info from sysmouse and adds special keys to input buf.
6594 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006595 static RETSIGTYPE
6596sig_sysmouse SIGDEFARG(sigarg)
6597{
6598 struct mouse_info mouse;
6599 struct video_info video;
6600 char_u string[6];
6601 int row, col;
6602 int button;
6603 int buttons;
6604 static int oldbuttons = 0;
6605
6606#ifdef FEAT_GUI
6607 /* Don't put events in the input queue now. */
6608 if (hold_gui_events)
6609 return;
6610#endif
6611
6612 mouse.operation = MOUSE_GETINFO;
6613 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
6614 && ioctl(1, FBIO_MODEINFO, &video) != -1
6615 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
6616 && video.vi_cheight > 0 && video.vi_cwidth > 0)
6617 {
6618 row = mouse.u.data.y / video.vi_cheight;
6619 col = mouse.u.data.x / video.vi_cwidth;
6620 buttons = mouse.u.data.buttons;
6621 string[0] = ESC; /* Our termcode */
6622 string[1] = 'M';
6623 string[2] = 'S';
6624 if (oldbuttons == buttons && buttons != 0)
6625 {
6626 button = MOUSE_DRAG;
6627 }
6628 else
6629 {
6630 switch (buttons)
6631 {
6632 case 0:
6633 button = MOUSE_RELEASE;
6634 break;
6635 case 1:
6636 button = MOUSE_LEFT;
6637 break;
6638 case 2:
6639 button = MOUSE_MIDDLE;
6640 break;
6641 case 4:
6642 button = MOUSE_RIGHT;
6643 break;
6644 default:
6645 return;
6646 }
6647 oldbuttons = buttons;
6648 }
6649 string[3] = (char_u)(button);
6650 string[4] = (char_u)(col + ' ' + 1);
6651 string[5] = (char_u)(row + ' ' + 1);
6652 add_to_input_buf(string, 6);
6653 }
6654 return;
6655}
6656#endif /* FEAT_SYSMOUSE */
6657
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658#if defined(FEAT_LIBCALL) || defined(PROTO)
6659typedef char_u * (*STRPROCSTR)__ARGS((char_u *));
6660typedef char_u * (*INTPROCSTR)__ARGS((int));
6661typedef int (*STRPROCINT)__ARGS((char_u *));
6662typedef int (*INTPROCINT)__ARGS((int));
6663
6664/*
6665 * Call a DLL routine which takes either a string or int param
6666 * and returns an allocated string.
6667 */
6668 int
6669mch_libcall(libname, funcname, argstring, argint, string_result, number_result)
6670 char_u *libname;
6671 char_u *funcname;
6672 char_u *argstring; /* NULL when using a argint */
6673 int argint;
6674 char_u **string_result;/* NULL when using number_result */
6675 int *number_result;
6676{
6677# if defined(USE_DLOPEN)
6678 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006679 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680# else
6681 shl_t hinstLib;
6682# endif
6683 STRPROCSTR ProcAdd;
6684 INTPROCSTR ProcAddI;
6685 char_u *retval_str = NULL;
6686 int retval_int = 0;
6687 int success = FALSE;
6688
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006689 /*
6690 * Get a handle to the DLL module.
6691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006693 /* First clear any error, it's not cleared by the dlopen() call. */
6694 (void)dlerror();
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 hinstLib = dlopen((char *)libname, RTLD_LAZY
6697# ifdef RTLD_LOCAL
6698 | RTLD_LOCAL
6699# endif
6700 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006701 if (hinstLib == NULL)
6702 {
6703 /* "dlerr" must be used before dlclose() */
6704 dlerr = (char *)dlerror();
6705 if (dlerr != NULL)
6706 EMSG2(_("dlerror = \"%s\""), dlerr);
6707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708# else
6709 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
6710# endif
6711
6712 /* If the handle is valid, try to get the function address. */
6713 if (hinstLib != NULL)
6714 {
6715# ifdef HAVE_SETJMP_H
6716 /*
6717 * Catch a crash when calling the library function. For example when
6718 * using a number where a string pointer is expected.
6719 */
6720 mch_startjmp();
6721 if (SETJMP(lc_jump_env) != 0)
6722 {
6723 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006724# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006725 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006726# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 mch_didjmp();
6728 }
6729 else
6730# endif
6731 {
6732 retval_str = NULL;
6733 retval_int = 0;
6734
6735 if (argstring != NULL)
6736 {
6737# if defined(USE_DLOPEN)
6738 ProcAdd = (STRPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006739 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740# else
6741 if (shl_findsym(&hinstLib, (const char *)funcname,
6742 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
6743 ProcAdd = NULL;
6744# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006745 if ((success = (ProcAdd != NULL
6746# if defined(USE_DLOPEN)
6747 && dlerr == NULL
6748# endif
6749 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
6751 if (string_result == NULL)
6752 retval_int = ((STRPROCINT)ProcAdd)(argstring);
6753 else
6754 retval_str = (ProcAdd)(argstring);
6755 }
6756 }
6757 else
6758 {
6759# if defined(USE_DLOPEN)
6760 ProcAddI = (INTPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006761 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762# else
6763 if (shl_findsym(&hinstLib, (const char *)funcname,
6764 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
6765 ProcAddI = NULL;
6766# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006767 if ((success = (ProcAddI != NULL
6768# if defined(USE_DLOPEN)
6769 && dlerr == NULL
6770# endif
6771 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {
6773 if (string_result == NULL)
6774 retval_int = ((INTPROCINT)ProcAddI)(argint);
6775 else
6776 retval_str = (ProcAddI)(argint);
6777 }
6778 }
6779
6780 /* Save the string before we free the library. */
6781 /* Assume that a "1" or "-1" result is an illegal pointer. */
6782 if (string_result == NULL)
6783 *number_result = retval_int;
6784 else if (retval_str != NULL
6785 && retval_str != (char_u *)1
6786 && retval_str != (char_u *)-1)
6787 *string_result = vim_strsave(retval_str);
6788 }
6789
6790# ifdef HAVE_SETJMP_H
6791 mch_endjmp();
6792# ifdef SIGHASARG
6793 if (lc_signal != 0)
6794 {
6795 int i;
6796
6797 /* try to find the name of this signal */
6798 for (i = 0; signal_info[i].sig != -1; i++)
6799 if (lc_signal == signal_info[i].sig)
6800 break;
6801 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
6802 }
6803# endif
6804# endif
6805
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006807 /* "dlerr" must be used before dlclose() */
6808 if (dlerr != NULL)
6809 EMSG2(_("dlerror = \"%s\""), dlerr);
6810
6811 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 (void)dlclose(hinstLib);
6813# else
6814 (void)shl_unload(hinstLib);
6815# endif
6816 }
6817
6818 if (!success)
6819 {
6820 EMSG2(_(e_libcall), funcname);
6821 return FAIL;
6822 }
6823
6824 return OK;
6825}
6826#endif
6827
6828#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
6829static int xterm_trace = -1; /* default: disabled */
6830static int xterm_button;
6831
6832/*
6833 * Setup a dummy window for X selections in a terminal.
6834 */
6835 void
6836setup_term_clip()
6837{
6838 int z = 0;
6839 char *strp = "";
6840 Widget AppShell;
6841
6842 if (!x_connect_to_server())
6843 return;
6844
6845 open_app_context();
6846 if (app_context != NULL && xterm_Shell == (Widget)0)
6847 {
6848 int (*oldhandler)();
6849#if defined(HAVE_SETJMP_H)
6850 int (*oldIOhandler)();
6851#endif
6852# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
6853 struct timeval start_tv;
6854
6855 if (p_verbose > 0)
6856 gettimeofday(&start_tv, NULL);
6857# endif
6858
6859 /* Ignore X errors while opening the display */
6860 oldhandler = XSetErrorHandler(x_error_check);
6861
6862#if defined(HAVE_SETJMP_H)
6863 /* Ignore X IO errors while opening the display */
6864 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
6865 mch_startjmp();
6866 if (SETJMP(lc_jump_env) != 0)
6867 {
6868 mch_didjmp();
6869 xterm_dpy = NULL;
6870 }
6871 else
6872#endif
6873 {
6874 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
6875 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
6876#if defined(HAVE_SETJMP_H)
6877 mch_endjmp();
6878#endif
6879 }
6880
6881#if defined(HAVE_SETJMP_H)
6882 /* Now handle X IO errors normally. */
6883 (void)XSetIOErrorHandler(oldIOhandler);
6884#endif
6885 /* Now handle X errors normally. */
6886 (void)XSetErrorHandler(oldhandler);
6887
6888 if (xterm_dpy == NULL)
6889 {
6890 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006891 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 return;
6893 }
6894
6895 /* Catch terminating error of the X server connection. */
6896 (void)XSetIOErrorHandler(x_IOerror_handler);
6897
6898# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
6899 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006900 {
6901 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 xopen_message(&start_tv);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006903 verbose_leave();
6904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905# endif
6906
6907 /* Create a Shell to make converters work. */
6908 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
6909 applicationShellWidgetClass, xterm_dpy,
6910 NULL);
6911 if (AppShell == (Widget)0)
6912 return;
6913 xterm_Shell = XtVaCreatePopupShell("VIM",
6914 topLevelShellWidgetClass, AppShell,
6915 XtNmappedWhenManaged, 0,
6916 XtNwidth, 1,
6917 XtNheight, 1,
6918 NULL);
6919 if (xterm_Shell == (Widget)0)
6920 return;
6921
6922 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02006923 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 if (x11_display == NULL)
6925 x11_display = xterm_dpy;
6926
6927 XtRealizeWidget(xterm_Shell);
6928 XSync(xterm_dpy, False);
6929 xterm_update();
6930 }
6931 if (xterm_Shell != (Widget)0)
6932 {
6933 clip_init(TRUE);
6934 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
6935 x11_window = (Window)atol(strp);
6936 /* Check if $WINDOWID is valid. */
6937 if (test_x11_window(xterm_dpy) == FAIL)
6938 x11_window = 0;
6939 if (x11_window != 0)
6940 xterm_trace = 0;
6941 }
6942}
6943
6944 void
6945start_xterm_trace(button)
6946 int button;
6947{
6948 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
6949 return;
6950 xterm_trace = 1;
6951 xterm_button = button;
6952 do_xterm_trace();
6953}
6954
6955
6956 void
6957stop_xterm_trace()
6958{
6959 if (xterm_trace < 0)
6960 return;
6961 xterm_trace = 0;
6962}
6963
6964/*
6965 * Query the xterm pointer and generate mouse termcodes if necessary
6966 * return TRUE if dragging is active, else FALSE
6967 */
6968 static int
6969do_xterm_trace()
6970{
6971 Window root, child;
6972 int root_x, root_y;
6973 int win_x, win_y;
6974 int row, col;
6975 int_u mask_return;
6976 char_u buf[50];
6977 char_u *strp;
6978 long got_hints;
6979 static char_u *mouse_code;
6980 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
6981 static int prev_row = 0, prev_col = 0;
6982 static XSizeHints xterm_hints;
6983
6984 if (xterm_trace <= 0)
6985 return FALSE;
6986
6987 if (xterm_trace == 1)
6988 {
6989 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00006990 * have changed recently. */
6991 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
6992 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 || xterm_hints.width_inc <= 1
6994 || xterm_hints.height_inc <= 1)
6995 {
6996 xterm_trace = -1; /* Not enough data -- disable tracing */
6997 return FALSE;
6998 }
6999
7000 /* Rely on the same mouse code for the duration of this */
7001 mouse_code = find_termcode(mouse_name);
7002 prev_row = mouse_row;
7003 prev_row = mouse_col;
7004 xterm_trace = 2;
7005
7006 /* Find the offset of the chars, there might be a scrollbar on the
7007 * left of the window and/or a menu on the top (eterm etc.) */
7008 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7009 &win_x, &win_y, &mask_return);
7010 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7011 - (xterm_hints.height_inc / 2);
7012 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7013 xterm_hints.y = 2;
7014 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7015 - (xterm_hints.width_inc / 2);
7016 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7017 xterm_hints.x = 2;
7018 return TRUE;
7019 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007020 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 {
7022 xterm_trace = 0;
7023 return FALSE;
7024 }
7025
7026 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7027 &win_x, &win_y, &mask_return);
7028
7029 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7030 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7031 if (row == prev_row && col == prev_col)
7032 return TRUE;
7033
7034 STRCPY(buf, mouse_code);
7035 strp = buf + STRLEN(buf);
7036 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7037 *strp++ = (char_u)(col + ' ' + 1);
7038 *strp++ = (char_u)(row + ' ' + 1);
7039 *strp = 0;
7040 add_to_input_buf(buf, STRLEN(buf));
7041
7042 prev_row = row;
7043 prev_col = col;
7044 return TRUE;
7045}
7046
7047# if defined(FEAT_GUI) || defined(PROTO)
7048/*
7049 * Destroy the display, window and app_context. Required for GTK.
7050 */
7051 void
7052clear_xterm_clip()
7053{
7054 if (xterm_Shell != (Widget)0)
7055 {
7056 XtDestroyWidget(xterm_Shell);
7057 xterm_Shell = (Widget)0;
7058 }
7059 if (xterm_dpy != NULL)
7060 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007061# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 /* Lesstif and Solaris crash here, lose some memory */
7063 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007064# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 if (x11_display == xterm_dpy)
7066 x11_display = NULL;
7067 xterm_dpy = NULL;
7068 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007069# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (app_context != (XtAppContext)NULL)
7071 {
7072 /* Lesstif and Solaris crash here, lose some memory */
7073 XtDestroyApplicationContext(app_context);
7074 app_context = (XtAppContext)NULL;
7075 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077}
7078# endif
7079
7080/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007081 * Catch up with GUI or X events.
7082 */
7083 static void
7084clip_update()
7085{
7086# ifdef FEAT_GUI
7087 if (gui.in_use)
7088 gui_mch_update();
7089 else
7090# endif
7091 if (xterm_Shell != (Widget)0)
7092 xterm_update();
7093}
7094
7095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 * Catch up with any queued X events. This may put keyboard input into the
7097 * input buffer, call resize call-backs, trigger timers etc. If there is
7098 * nothing in the X event queue (& no timers pending), then we return
7099 * immediately.
7100 */
7101 static void
7102xterm_update()
7103{
7104 XEvent event;
7105
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007106 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007108 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007110 if (mask == 0 || vim_is_input_buf_full())
7111 break;
7112
7113 if (mask & XtIMXEvent)
7114 {
7115 /* There is an event to process. */
7116 XtAppNextEvent(app_context, &event);
7117#ifdef FEAT_CLIENTSERVER
7118 {
7119 XPropertyEvent *e = (XPropertyEvent *)&event;
7120
7121 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007123 serverEventProc(xterm_dpy, &event);
7124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125#endif
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007126 XtDispatchEvent(&event);
7127 }
7128 else
7129 {
7130 /* There is something else than an event to process. */
7131 XtAppProcessEvent(app_context, mask);
7132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
7134}
7135
7136 int
7137clip_xterm_own_selection(cbd)
7138 VimClipboard *cbd;
7139{
7140 if (xterm_Shell != (Widget)0)
7141 return clip_x11_own_selection(xterm_Shell, cbd);
7142 return FAIL;
7143}
7144
7145 void
7146clip_xterm_lose_selection(cbd)
7147 VimClipboard *cbd;
7148{
7149 if (xterm_Shell != (Widget)0)
7150 clip_x11_lose_selection(xterm_Shell, cbd);
7151}
7152
7153 void
7154clip_xterm_request_selection(cbd)
7155 VimClipboard *cbd;
7156{
7157 if (xterm_Shell != (Widget)0)
7158 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7159}
7160
7161 void
7162clip_xterm_set_selection(cbd)
7163 VimClipboard *cbd;
7164{
7165 clip_x11_set_selection(cbd);
7166}
7167#endif
7168
7169
7170#if defined(USE_XSMP) || defined(PROTO)
7171/*
7172 * Code for X Session Management Protocol.
7173 */
7174static void xsmp_handle_save_yourself __ARGS((SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast));
7175static void xsmp_die __ARGS((SmcConn smc_conn, SmPointer client_data));
7176static void xsmp_save_complete __ARGS((SmcConn smc_conn, SmPointer client_data));
7177static void xsmp_shutdown_cancelled __ARGS((SmcConn smc_conn, SmPointer client_data));
7178static void xsmp_ice_connection __ARGS((IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData));
7179
7180
7181# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7182static void xsmp_handle_interaction __ARGS((SmcConn smc_conn, SmPointer client_data));
7183
7184/*
7185 * This is our chance to ask the user if they want to save,
7186 * or abort the logout
7187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 static void
7189xsmp_handle_interaction(smc_conn, client_data)
7190 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007191 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 cmdmod_T save_cmdmod;
7194 int cancel_shutdown = False;
7195
7196 save_cmdmod = cmdmod;
7197 cmdmod.confirm = TRUE;
7198 if (check_changed_any(FALSE))
7199 /* Mustn't logout */
7200 cancel_shutdown = True;
7201 cmdmod = save_cmdmod;
7202 setcursor(); /* position cursor */
7203 out_flush();
7204
7205 /* Done interaction */
7206 SmcInteractDone(smc_conn, cancel_shutdown);
7207
7208 /* Finish off
7209 * Only end save-yourself here if we're not cancelling shutdown;
7210 * we'll get a cancelled callback later in which we'll end it.
7211 * Hopefully get around glitchy SMs (like GNOME-1)
7212 */
7213 if (!cancel_shutdown)
7214 {
7215 xsmp.save_yourself = False;
7216 SmcSaveYourselfDone(smc_conn, True);
7217 }
7218}
7219# endif
7220
7221/*
7222 * Callback that starts save-yourself.
7223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 static void
7225xsmp_handle_save_yourself(smc_conn, client_data, save_type,
7226 shutdown, interact_style, fast)
7227 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007228 SmPointer client_data UNUSED;
7229 int save_type UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 Bool shutdown;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007231 int interact_style UNUSED;
7232 Bool fast UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233{
7234 /* Handle already being in saveyourself */
7235 if (xsmp.save_yourself)
7236 SmcSaveYourselfDone(smc_conn, True);
7237 xsmp.save_yourself = True;
7238 xsmp.shutdown = shutdown;
7239
7240 /* First up, preserve all files */
7241 out_flush();
7242 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7243
7244 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007245 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
7247# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7248 /* Now see if we can ask about unsaved files */
7249 if (shutdown && !fast && gui.in_use)
7250 /* Need to interact with user, but need SM's permission */
7251 SmcInteractRequest(smc_conn, SmDialogError,
7252 xsmp_handle_interaction, client_data);
7253 else
7254# endif
7255 {
7256 /* Can stop the cycle here */
7257 SmcSaveYourselfDone(smc_conn, True);
7258 xsmp.save_yourself = False;
7259 }
7260}
7261
7262
7263/*
7264 * Callback to warn us of imminent death.
7265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 static void
7267xsmp_die(smc_conn, client_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007268 SmcConn smc_conn UNUSED;
7269 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
7271 xsmp_close();
7272
7273 /* quit quickly leaving swapfiles for modified buffers behind */
7274 getout_preserve_modified(0);
7275}
7276
7277
7278/*
7279 * Callback to tell us that save-yourself has completed.
7280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 static void
7282xsmp_save_complete(smc_conn, client_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007283 SmcConn smc_conn UNUSED;
7284 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
7286 xsmp.save_yourself = False;
7287}
7288
7289
7290/*
7291 * Callback to tell us that an instigated shutdown was cancelled
7292 * (maybe even by us)
7293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 static void
7295xsmp_shutdown_cancelled(smc_conn, client_data)
7296 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007297 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298{
7299 if (xsmp.save_yourself)
7300 SmcSaveYourselfDone(smc_conn, True);
7301 xsmp.save_yourself = False;
7302 xsmp.shutdown = False;
7303}
7304
7305
7306/*
7307 * Callback to tell us that a new ICE connection has been established.
7308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 static void
7310xsmp_ice_connection(iceConn, clientData, opening, watchData)
7311 IceConn iceConn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007312 IcePointer clientData UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 Bool opening;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007314 IcePointer *watchData UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316 /* Intercept creation of ICE connection fd */
7317 if (opening)
7318 {
7319 xsmp_icefd = IceConnectionNumber(iceConn);
7320 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7321 }
7322}
7323
7324
7325/* Handle any ICE processing that's required; return FAIL if SM lost */
7326 int
7327xsmp_handle_requests()
7328{
7329 Bool rep;
7330
7331 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7332 == IceProcessMessagesIOError)
7333 {
7334 /* Lost ICE */
7335 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007336 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 xsmp_close();
7338 return FAIL;
7339 }
7340 else
7341 return OK;
7342}
7343
7344static int dummy;
7345
7346/* Set up X Session Management Protocol */
7347 void
7348xsmp_init(void)
7349{
7350 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 SmcCallbacks smcallbacks;
7352#if 0
7353 SmPropValue smname;
7354 SmProp smnameprop;
7355 SmProp *smprops[1];
7356#endif
7357
7358 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007359 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
7361 xsmp.save_yourself = xsmp.shutdown = False;
7362
7363 /* Set up SM callbacks - must have all, even if they're not used */
7364 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7365 smcallbacks.save_yourself.client_data = NULL;
7366 smcallbacks.die.callback = xsmp_die;
7367 smcallbacks.die.client_data = NULL;
7368 smcallbacks.save_complete.callback = xsmp_save_complete;
7369 smcallbacks.save_complete.client_data = NULL;
7370 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7371 smcallbacks.shutdown_cancelled.client_data = NULL;
7372
7373 /* Set up a watch on ICE connection creations. The "dummy" argument is
7374 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7375 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7376 {
7377 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007378 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 return;
7380 }
7381
7382 /* Create an SM connection */
7383 xsmp.smcconn = SmcOpenConnection(
7384 NULL,
7385 NULL,
7386 SmProtoMajor,
7387 SmProtoMinor,
7388 SmcSaveYourselfProcMask | SmcDieProcMask
7389 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7390 &smcallbacks,
7391 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007392 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 sizeof(errorstring),
7394 errorstring);
7395 if (xsmp.smcconn == NULL)
7396 {
7397 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007398
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007400 {
7401 vim_snprintf(errorreport, sizeof(errorreport),
7402 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7403 verb_msg((char_u *)errorreport);
7404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 return;
7406 }
7407 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7408
7409#if 0
7410 /* ID ourselves */
7411 smname.value = "vim";
7412 smname.length = 3;
7413 smnameprop.name = "SmProgram";
7414 smnameprop.type = "SmARRAY8";
7415 smnameprop.num_vals = 1;
7416 smnameprop.vals = &smname;
7417
7418 smprops[0] = &smnameprop;
7419 SmcSetProperties(xsmp.smcconn, 1, smprops);
7420#endif
7421}
7422
7423
7424/* Shut down XSMP comms. */
7425 void
7426xsmp_close()
7427{
7428 if (xsmp_icefd != -1)
7429 {
7430 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007431 if (xsmp.clientid != NULL)
7432 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007433 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 xsmp_icefd = -1;
7435 }
7436}
7437#endif /* USE_XSMP */
7438
7439
7440#ifdef EBCDIC
7441/* Translate character to its CTRL- value */
7442char CtrlTable[] =
7443{
7444/* 00 - 5E */
7445 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7446 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7447 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7448 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7449 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7450 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7451/* ^ */ 0x1E,
7452/* - */ 0x1F,
7453/* 61 - 6C */
7454 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7455/* _ */ 0x1F,
7456/* 6E - 80 */
7457 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7458/* a */ 0x01,
7459/* b */ 0x02,
7460/* c */ 0x03,
7461/* d */ 0x37,
7462/* e */ 0x2D,
7463/* f */ 0x2E,
7464/* g */ 0x2F,
7465/* h */ 0x16,
7466/* i */ 0x05,
7467/* 8A - 90 */
7468 0, 0, 0, 0, 0, 0, 0,
7469/* j */ 0x15,
7470/* k */ 0x0B,
7471/* l */ 0x0C,
7472/* m */ 0x0D,
7473/* n */ 0x0E,
7474/* o */ 0x0F,
7475/* p */ 0x10,
7476/* q */ 0x11,
7477/* r */ 0x12,
7478/* 9A - A1 */
7479 0, 0, 0, 0, 0, 0, 0, 0,
7480/* s */ 0x13,
7481/* t */ 0x3C,
7482/* u */ 0x3D,
7483/* v */ 0x32,
7484/* w */ 0x26,
7485/* x */ 0x18,
7486/* y */ 0x19,
7487/* z */ 0x3F,
7488/* AA - AC */
7489 0, 0, 0,
7490/* [ */ 0x27,
7491/* AE - BC */
7492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7493/* ] */ 0x1D,
7494/* BE - C0 */ 0, 0, 0,
7495/* A */ 0x01,
7496/* B */ 0x02,
7497/* C */ 0x03,
7498/* D */ 0x37,
7499/* E */ 0x2D,
7500/* F */ 0x2E,
7501/* G */ 0x2F,
7502/* H */ 0x16,
7503/* I */ 0x05,
7504/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
7505/* J */ 0x15,
7506/* K */ 0x0B,
7507/* L */ 0x0C,
7508/* M */ 0x0D,
7509/* N */ 0x0E,
7510/* O */ 0x0F,
7511/* P */ 0x10,
7512/* Q */ 0x11,
7513/* R */ 0x12,
7514/* DA - DF */ 0, 0, 0, 0, 0, 0,
7515/* \ */ 0x1C,
7516/* E1 */ 0,
7517/* S */ 0x13,
7518/* T */ 0x3C,
7519/* U */ 0x3D,
7520/* V */ 0x32,
7521/* W */ 0x26,
7522/* X */ 0x18,
7523/* Y */ 0x19,
7524/* Z */ 0x3F,
7525/* EA - FF*/ 0, 0, 0, 0, 0, 0,
7526 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7527};
7528
7529char MetaCharTable[]=
7530{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7531 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
7532 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
7533 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
7534 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
7535};
7536
7537
7538/* TODO: Use characters NOT numbers!!! */
7539char CtrlCharTable[]=
7540{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7541 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
7542 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
7543 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
7544 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
7545};
7546
7547
7548#endif