7
|
1 /* 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 */
|
|
28 #ifndef __APPLE__
|
|
29 # define select select_declared_wrong
|
|
30 #endif
|
|
31
|
|
32 #include "vim.h"
|
|
33
|
14
|
34 #ifdef FEAT_MZSCHEME
|
|
35 # include "if_mzsch.h"
|
|
36 #endif
|
|
37
|
7
|
38 #ifdef HAVE_FCNTL_H
|
|
39 # include <fcntl.h>
|
|
40 #endif
|
|
41
|
|
42 #include "os_unixx.h" /* unix includes for os_unix.c only */
|
|
43
|
|
44 #ifdef USE_XSMP
|
|
45 # include <X11/SM/SMlib.h>
|
|
46 #endif
|
|
47
|
|
48 /*
|
|
49 * Use this prototype for select, some include files have a wrong prototype
|
|
50 */
|
|
51 #undef select
|
|
52 #ifdef __BEOS__
|
|
53 # define select beos_select
|
|
54 #endif
|
|
55
|
|
56 #if defined(HAVE_SELECT)
|
|
57 extern int select __ARGS((int, fd_set *, fd_set *, fd_set *, struct timeval *));
|
|
58 #endif
|
|
59
|
|
60 #ifdef FEAT_MOUSE_GPM
|
|
61 # include <gpm.h>
|
|
62 /* <linux/keyboard.h> contains defines conflicting with "keymap.h",
|
|
63 * I just copied relevant defines here. A cleaner solution would be to put gpm
|
|
64 * code into separate file and include there linux/keyboard.h
|
|
65 */
|
|
66 /* #include <linux/keyboard.h> */
|
|
67 # define KG_SHIFT 0
|
|
68 # define KG_CTRL 2
|
|
69 # define KG_ALT 3
|
|
70 # define KG_ALTGR 1
|
|
71 # define KG_SHIFTL 4
|
|
72 # define KG_SHIFTR 5
|
|
73 # define KG_CTRLL 6
|
|
74 # define KG_CTRLR 7
|
|
75 # define KG_CAPSSHIFT 8
|
|
76
|
|
77 static void gpm_close __ARGS((void));
|
|
78 static int gpm_open __ARGS((void));
|
|
79 static int mch_gpm_process __ARGS((void));
|
|
80 #endif
|
|
81
|
|
82 /*
|
|
83 * end of autoconf section. To be extended...
|
|
84 */
|
|
85
|
|
86 /* Are the following #ifdefs still required? And why? Is that for X11? */
|
|
87
|
|
88 #if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
|
|
89 # ifdef SIGWINCH
|
|
90 # undef SIGWINCH
|
|
91 # endif
|
|
92 # ifdef TIOCGWINSZ
|
|
93 # undef TIOCGWINSZ
|
|
94 # endif
|
|
95 #endif
|
|
96
|
|
97 #if defined(SIGWINDOW) && !defined(SIGWINCH) /* hpux 9.01 has it */
|
|
98 # define SIGWINCH SIGWINDOW
|
|
99 #endif
|
|
100
|
|
101 #ifdef FEAT_X11
|
|
102 # include <X11/Xlib.h>
|
|
103 # include <X11/Xutil.h>
|
|
104 # include <X11/Xatom.h>
|
|
105 # ifdef FEAT_XCLIPBOARD
|
|
106 # include <X11/Intrinsic.h>
|
|
107 # include <X11/Shell.h>
|
|
108 # include <X11/StringDefs.h>
|
|
109 static Widget xterm_Shell = (Widget)0;
|
|
110 static void xterm_update __ARGS((void));
|
|
111 # endif
|
|
112
|
|
113 # if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
|
|
114 Window x11_window = 0;
|
|
115 # endif
|
|
116 Display *x11_display = NULL;
|
|
117
|
|
118 # ifdef FEAT_TITLE
|
|
119 static int get_x11_windis __ARGS((void));
|
|
120 static void set_x11_title __ARGS((char_u *));
|
|
121 static void set_x11_icon __ARGS((char_u *));
|
|
122 # endif
|
|
123 #endif
|
|
124
|
|
125 #ifdef FEAT_TITLE
|
|
126 static int get_x11_title __ARGS((int));
|
|
127 static int get_x11_icon __ARGS((int));
|
|
128
|
|
129 static char_u *oldtitle = NULL;
|
|
130 static int did_set_title = FALSE;
|
|
131 static char_u *oldicon = NULL;
|
|
132 static int did_set_icon = FALSE;
|
|
133 #endif
|
|
134
|
|
135 static void may_core_dump __ARGS((void));
|
|
136
|
|
137 static int WaitForChar __ARGS((long));
|
|
138 #if defined(__BEOS__)
|
|
139 int RealWaitForChar __ARGS((int, long, int *));
|
|
140 #else
|
|
141 static int RealWaitForChar __ARGS((int, long, int *));
|
|
142 #endif
|
|
143
|
|
144 #ifdef FEAT_XCLIPBOARD
|
|
145 static int do_xterm_trace __ARGS((void));
|
331
|
146 # define XT_TRACE_DELAY 50 /* delay for xterm tracing */
|
7
|
147 #endif
|
|
148
|
|
149 static void handle_resize __ARGS((void));
|
|
150
|
|
151 #if defined(SIGWINCH)
|
|
152 static RETSIGTYPE sig_winch __ARGS(SIGPROTOARG);
|
|
153 #endif
|
|
154 #if defined(SIGINT)
|
|
155 static RETSIGTYPE catch_sigint __ARGS(SIGPROTOARG);
|
|
156 #endif
|
|
157 #if defined(SIGPWR)
|
|
158 static RETSIGTYPE catch_sigpwr __ARGS(SIGPROTOARG);
|
|
159 #endif
|
|
160 #if defined(SIGALRM) && defined(FEAT_X11) \
|
|
161 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
|
|
162 # define SET_SIG_ALARM
|
|
163 static RETSIGTYPE sig_alarm __ARGS(SIGPROTOARG);
|
|
164 static int sig_alarm_called;
|
|
165 #endif
|
|
166 static RETSIGTYPE deathtrap __ARGS(SIGPROTOARG);
|
|
167
|
167
|
168 static void catch_int_signal __ARGS((void));
|
7
|
169 static void set_signals __ARGS((void));
|
|
170 static void catch_signals __ARGS((RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)()));
|
|
171 #ifndef __EMX__
|
|
172 static int have_wildcard __ARGS((int, char_u **));
|
|
173 static int have_dollars __ARGS((int, char_u **));
|
|
174 #endif
|
|
175
|
|
176 #ifndef __EMX__
|
|
177 static int save_patterns __ARGS((int num_pat, char_u **pat, int *num_file, char_u ***file));
|
|
178 #endif
|
|
179
|
|
180 #ifndef SIG_ERR
|
|
181 # define SIG_ERR ((RETSIGTYPE (*)())-1)
|
|
182 #endif
|
|
183
|
|
184 static int do_resize = FALSE;
|
|
185 #ifndef __EMX__
|
|
186 static char_u *extra_shell_arg = NULL;
|
|
187 static int show_shell_mess = TRUE;
|
|
188 #endif
|
|
189 static int deadly_signal = 0; /* The signal we caught */
|
|
190
|
|
191 static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
|
|
192
|
|
193 #ifdef USE_XSMP
|
|
194 typedef struct
|
|
195 {
|
|
196 SmcConn smcconn; /* The SM connection ID */
|
|
197 IceConn iceconn; /* The ICE connection ID */
|
|
198 Bool save_yourself; /* If we're in the middle of a save_yourself */
|
|
199 Bool shutdown; /* If we're in shutdown mode */
|
|
200 } xsmp_config_T;
|
|
201
|
|
202 static xsmp_config_T xsmp;
|
|
203 #endif
|
|
204
|
|
205 #ifdef SYS_SIGLIST_DECLARED
|
|
206 /*
|
|
207 * I have seen
|
|
208 * extern char *_sys_siglist[NSIG];
|
|
209 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
|
|
210 * that describe the signals. That is nearly what we want here. But
|
|
211 * autoconf does only check for sys_siglist (without the underscore), I
|
|
212 * do not want to change everything today.... jw.
|
|
213 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.in
|
|
214 */
|
|
215 #endif
|
|
216
|
|
217 static struct signalinfo
|
|
218 {
|
|
219 int sig; /* Signal number, eg. SIGSEGV etc */
|
|
220 char *name; /* Signal name (not char_u!). */
|
|
221 char deadly; /* Catch as a deadly signal? */
|
|
222 } signal_info[] =
|
|
223 {
|
|
224 #ifdef SIGHUP
|
|
225 {SIGHUP, "HUP", TRUE},
|
|
226 #endif
|
|
227 #ifdef SIGQUIT
|
|
228 {SIGQUIT, "QUIT", TRUE},
|
|
229 #endif
|
|
230 #ifdef SIGILL
|
|
231 {SIGILL, "ILL", TRUE},
|
|
232 #endif
|
|
233 #ifdef SIGTRAP
|
|
234 {SIGTRAP, "TRAP", TRUE},
|
|
235 #endif
|
|
236 #ifdef SIGABRT
|
|
237 {SIGABRT, "ABRT", TRUE},
|
|
238 #endif
|
|
239 #ifdef SIGEMT
|
|
240 {SIGEMT, "EMT", TRUE},
|
|
241 #endif
|
|
242 #ifdef SIGFPE
|
|
243 {SIGFPE, "FPE", TRUE},
|
|
244 #endif
|
|
245 #ifdef SIGBUS
|
|
246 {SIGBUS, "BUS", TRUE},
|
|
247 #endif
|
|
248 #ifdef SIGSEGV
|
|
249 {SIGSEGV, "SEGV", TRUE},
|
|
250 #endif
|
|
251 #ifdef SIGSYS
|
|
252 {SIGSYS, "SYS", TRUE},
|
|
253 #endif
|
|
254 #ifdef SIGALRM
|
|
255 {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
|
|
256 #endif
|
|
257 #ifdef SIGTERM
|
|
258 {SIGTERM, "TERM", TRUE},
|
|
259 #endif
|
|
260 #ifdef SIGVTALRM
|
|
261 {SIGVTALRM, "VTALRM", TRUE},
|
|
262 #endif
|
14
|
263 #if defined(SIGPROF) && !defined(FEAT_MZSCHEME)
|
|
264 /* MzScheme uses SIGPROF for its own needs */
|
7
|
265 {SIGPROF, "PROF", TRUE},
|
|
266 #endif
|
|
267 #ifdef SIGXCPU
|
|
268 {SIGXCPU, "XCPU", TRUE},
|
|
269 #endif
|
|
270 #ifdef SIGXFSZ
|
|
271 {SIGXFSZ, "XFSZ", TRUE},
|
|
272 #endif
|
|
273 #ifdef SIGUSR1
|
|
274 {SIGUSR1, "USR1", TRUE},
|
|
275 #endif
|
|
276 #ifdef SIGUSR2
|
|
277 {SIGUSR2, "USR2", TRUE},
|
|
278 #endif
|
|
279 #ifdef SIGINT
|
|
280 {SIGINT, "INT", FALSE},
|
|
281 #endif
|
|
282 #ifdef SIGWINCH
|
|
283 {SIGWINCH, "WINCH", FALSE},
|
|
284 #endif
|
|
285 #ifdef SIGTSTP
|
|
286 {SIGTSTP, "TSTP", FALSE},
|
|
287 #endif
|
|
288 #ifdef SIGPIPE
|
|
289 {SIGPIPE, "PIPE", FALSE},
|
|
290 #endif
|
|
291 {-1, "Unknown!", FALSE}
|
|
292 };
|
|
293
|
|
294 void
|
|
295 mch_write(s, len)
|
|
296 char_u *s;
|
|
297 int len;
|
|
298 {
|
|
299 write(1, (char *)s, len);
|
|
300 if (p_wd) /* Unix is too fast, slow down a bit more */
|
|
301 RealWaitForChar(read_cmd_fd, p_wd, NULL);
|
|
302 }
|
|
303
|
|
304 /*
|
|
305 * mch_inchar(): low level input funcion.
|
|
306 * Get a characters from the keyboard.
|
|
307 * Return the number of characters that are available.
|
|
308 * If wtime == 0 do not wait for characters.
|
|
309 * If wtime == n wait a short time for characters.
|
|
310 * If wtime == -1 wait forever for characters.
|
|
311 */
|
|
312 int
|
|
313 mch_inchar(buf, maxlen, wtime, tb_change_cnt)
|
|
314 char_u *buf;
|
|
315 int maxlen;
|
|
316 long wtime; /* don't use "time", MIPS cannot handle it */
|
|
317 int tb_change_cnt;
|
|
318 {
|
|
319 int len;
|
|
320
|
|
321 /* Check if window changed size while we were busy, perhaps the ":set
|
|
322 * columns=99" command was used. */
|
|
323 while (do_resize)
|
|
324 handle_resize();
|
|
325
|
|
326 if (wtime >= 0)
|
|
327 {
|
|
328 while (WaitForChar(wtime) == 0) /* no character available */
|
|
329 {
|
|
330 if (!do_resize) /* return if not interrupted by resize */
|
|
331 return 0;
|
|
332 handle_resize();
|
|
333 }
|
|
334 }
|
|
335 else /* wtime == -1 */
|
|
336 {
|
|
337 /*
|
|
338 * If there is no character available within 'updatetime' seconds
|
216
|
339 * flush all the swap files to disk.
|
7
|
340 * Also done when interrupted by SIGWINCH.
|
|
341 */
|
|
342 if (WaitForChar(p_ut) == 0)
|
|
343 {
|
|
344 #ifdef FEAT_AUTOCMD
|
609
|
345 if (trigger_cursorhold() && maxlen >= 3
|
|
346 && !typebuf_changed(tb_change_cnt))
|
7
|
347 {
|
216
|
348 buf[0] = K_SPECIAL;
|
|
349 buf[1] = KS_EXTRA;
|
|
350 buf[2] = (int)KE_CURSORHOLD;
|
|
351 return 3;
|
7
|
352 }
|
216
|
353 #endif
|
369
|
354 before_blocking();
|
7
|
355 }
|
|
356 }
|
|
357
|
|
358 for (;;) /* repeat until we got a character */
|
|
359 {
|
|
360 while (do_resize) /* window changed size */
|
|
361 handle_resize();
|
|
362 /*
|
|
363 * we want to be interrupted by the winch signal
|
|
364 */
|
|
365 WaitForChar(-1L);
|
|
366 if (do_resize) /* interrupted by SIGWINCH signal */
|
|
367 continue;
|
|
368
|
|
369 /* If input was put directly in typeahead buffer bail out here. */
|
|
370 if (typebuf_changed(tb_change_cnt))
|
|
371 return 0;
|
|
372
|
|
373 /*
|
|
374 * For some terminals we only get one character at a time.
|
|
375 * We want the get all available characters, so we could keep on
|
|
376 * trying until none is available
|
|
377 * For some other terminals this is quite slow, that's why we don't do
|
|
378 * it.
|
|
379 */
|
|
380 len = read_from_input_buf(buf, (long)maxlen);
|
|
381 if (len > 0)
|
|
382 {
|
|
383 #ifdef OS2
|
|
384 int i;
|
|
385
|
|
386 for (i = 0; i < len; i++)
|
|
387 if (buf[i] == 0)
|
|
388 buf[i] = K_NUL;
|
|
389 #endif
|
|
390 return len;
|
|
391 }
|
|
392 }
|
|
393 }
|
|
394
|
|
395 static void
|
|
396 handle_resize()
|
|
397 {
|
|
398 do_resize = FALSE;
|
|
399 shell_resized();
|
|
400 }
|
|
401
|
|
402 /*
|
|
403 * return non-zero if a character is available
|
|
404 */
|
|
405 int
|
|
406 mch_char_avail()
|
|
407 {
|
|
408 return WaitForChar(0L);
|
|
409 }
|
|
410
|
|
411 #if defined(HAVE_TOTAL_MEM) || defined(PROTO)
|
|
412 # ifdef HAVE_SYS_RESOURCE_H
|
|
413 # include <sys/resource.h>
|
|
414 # endif
|
|
415 # if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
|
|
416 # include <sys/sysctl.h>
|
|
417 # endif
|
|
418 # if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
|
|
419 # include <sys/sysinfo.h>
|
|
420 # endif
|
|
421
|
|
422 /*
|
|
423 * Return total amount of memory available. Doesn't change when memory has
|
|
424 * been allocated.
|
|
425 */
|
|
426 /* ARGSUSED */
|
|
427 long_u
|
|
428 mch_total_mem(special)
|
|
429 int special;
|
|
430 {
|
|
431 # ifdef __EMX__
|
|
432 return ulimit(3, 0L); /* always 32MB? */
|
|
433 # else
|
|
434 long_u mem = 0;
|
|
435
|
|
436 # ifdef HAVE_SYSCTL
|
|
437 int mib[2], physmem;
|
|
438 size_t len;
|
|
439
|
|
440 /* BSD way of getting the amount of RAM available. */
|
|
441 mib[0] = CTL_HW;
|
|
442 mib[1] = HW_USERMEM;
|
|
443 len = sizeof(physmem);
|
|
444 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
|
|
445 mem = (long_u)physmem;
|
|
446 # endif
|
|
447
|
|
448 # if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
|
|
449 if (mem == 0)
|
|
450 {
|
|
451 struct sysinfo sinfo;
|
|
452
|
|
453 /* Linux way of getting amount of RAM available */
|
|
454 if (sysinfo(&sinfo) == 0)
|
|
455 mem = sinfo.totalram;
|
|
456 }
|
|
457 # endif
|
|
458
|
|
459 # ifdef HAVE_SYSCONF
|
|
460 if (mem == 0)
|
|
461 {
|
|
462 long pagesize, pagecount;
|
|
463
|
|
464 /* Solaris way of getting amount of RAM available */
|
|
465 pagesize = sysconf(_SC_PAGESIZE);
|
|
466 pagecount = sysconf(_SC_PHYS_PAGES);
|
|
467 if (pagesize > 0 && pagecount > 0)
|
|
468 mem = (long_u)pagesize * pagecount;
|
|
469 }
|
|
470 # endif
|
|
471
|
|
472 /* Return the minimum of the physical memory and the user limit, because
|
|
473 * using more than the user limit may cause Vim to be terminated. */
|
|
474 # if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
|
|
475 {
|
|
476 struct rlimit rlp;
|
|
477
|
|
478 if (getrlimit(RLIMIT_DATA, &rlp) == 0
|
|
479 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
|
|
480 # ifdef RLIM_INFINITY
|
|
481 && rlp.rlim_cur != RLIM_INFINITY
|
|
482 # endif
|
|
483 && (long_u)rlp.rlim_cur < mem
|
|
484 )
|
|
485 return (long_u)rlp.rlim_cur;
|
|
486 }
|
|
487 # endif
|
|
488
|
|
489 if (mem > 0)
|
|
490 return mem;
|
|
491 return (long_u)0x7fffffff;
|
|
492 # endif
|
|
493 }
|
|
494 #endif
|
|
495
|
|
496 void
|
|
497 mch_delay(msec, ignoreinput)
|
|
498 long msec;
|
|
499 int ignoreinput;
|
|
500 {
|
|
501 int old_tmode;
|
14
|
502 #ifdef FEAT_MZSCHEME
|
|
503 long total = msec; /* remember original value */
|
|
504 #endif
|
7
|
505
|
|
506 if (ignoreinput)
|
|
507 {
|
|
508 /* Go to cooked mode without echo, to allow SIGINT interrupting us
|
|
509 * here */
|
|
510 old_tmode = curr_tmode;
|
|
511 if (curr_tmode == TMODE_RAW)
|
|
512 settmode(TMODE_SLEEP);
|
|
513
|
|
514 /*
|
|
515 * Everybody sleeps in a different way...
|
|
516 * Prefer nanosleep(), some versions of usleep() can only sleep up to
|
|
517 * one second.
|
|
518 */
|
14
|
519 #ifdef FEAT_MZSCHEME
|
|
520 do
|
|
521 {
|
|
522 /* if total is large enough, wait by portions in p_mzq */
|
|
523 if (total > p_mzq)
|
|
524 msec = p_mzq;
|
|
525 else
|
|
526 msec = total;
|
|
527 total -= msec;
|
|
528 #endif
|
7
|
529 #ifdef HAVE_NANOSLEEP
|
|
530 {
|
|
531 struct timespec ts;
|
|
532
|
|
533 ts.tv_sec = msec / 1000;
|
|
534 ts.tv_nsec = (msec % 1000) * 1000000;
|
|
535 (void)nanosleep(&ts, NULL);
|
|
536 }
|
|
537 #else
|
|
538 # ifdef HAVE_USLEEP
|
|
539 while (msec >= 1000)
|
|
540 {
|
|
541 usleep((unsigned int)(999 * 1000));
|
|
542 msec -= 999;
|
|
543 }
|
|
544 usleep((unsigned int)(msec * 1000));
|
|
545 # else
|
|
546 # ifndef HAVE_SELECT
|
|
547 poll(NULL, 0, (int)msec);
|
|
548 # else
|
|
549 # ifdef __EMX__
|
|
550 _sleep2(msec);
|
|
551 # else
|
|
552 {
|
|
553 struct timeval tv;
|
|
554
|
|
555 tv.tv_sec = msec / 1000;
|
|
556 tv.tv_usec = (msec % 1000) * 1000;
|
|
557 /*
|
|
558 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
|
|
559 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
|
|
560 */
|
|
561 select(0, NULL, NULL, NULL, &tv);
|
|
562 }
|
|
563 # endif /* __EMX__ */
|
|
564 # endif /* HAVE_SELECT */
|
|
565 # endif /* HAVE_NANOSLEEP */
|
|
566 #endif /* HAVE_USLEEP */
|
14
|
567 #ifdef FEAT_MZSCHEME
|
|
568 }
|
|
569 while (total > 0);
|
|
570 #endif
|
7
|
571
|
|
572 settmode(old_tmode);
|
|
573 }
|
|
574 else
|
|
575 WaitForChar(msec);
|
|
576 }
|
|
577
|
180
|
578 #if 0 /* disabled, no longer needed now that regmatch() is not recursive */
|
|
579 # if defined(HAVE_GETRLIMIT)
|
|
580 # define HAVE_STACK_LIMIT
|
|
581 # endif
|
|
582 #endif
|
|
583
|
|
584 #if defined(HAVE_STACK_LIMIT) \
|
7
|
585 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
|
|
586 # define HAVE_CHECK_STACK_GROWTH
|
|
587 /*
|
|
588 * Support for checking for an almost-out-of-stack-space situation.
|
|
589 */
|
|
590
|
|
591 /*
|
|
592 * Return a pointer to an item on the stack. Used to find out if the stack
|
|
593 * grows up or down.
|
|
594 */
|
|
595 static void check_stack_growth __ARGS((char *p));
|
|
596 static int stack_grows_downwards;
|
|
597
|
|
598 /*
|
|
599 * Find out if the stack grows upwards or downwards.
|
|
600 * "p" points to a variable on the stack of the caller.
|
|
601 */
|
|
602 static void
|
|
603 check_stack_growth(p)
|
|
604 char *p;
|
|
605 {
|
|
606 int i;
|
|
607
|
|
608 stack_grows_downwards = (p > (char *)&i);
|
|
609 }
|
|
610 #endif
|
|
611
|
180
|
612 #if defined(HAVE_STACK_LIMIT) || defined(PROTO)
|
7
|
613 static char *stack_limit = NULL;
|
|
614
|
|
615 #if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
|
|
616 # include <pthread.h>
|
|
617 # include <pthread_np.h>
|
|
618 #endif
|
|
619
|
|
620 /*
|
|
621 * Find out until how var the stack can grow without getting into trouble.
|
|
622 * Called when starting up and when switching to the signal stack in
|
|
623 * deathtrap().
|
|
624 */
|
|
625 static void
|
|
626 get_stack_limit()
|
|
627 {
|
|
628 struct rlimit rlp;
|
|
629 int i;
|
|
630 long lim;
|
|
631
|
|
632 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
|
|
633 * limit doesn't fit in a long (rlim_cur might be "long long"). */
|
|
634 if (getrlimit(RLIMIT_STACK, &rlp) == 0
|
|
635 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
|
|
636 # ifdef RLIM_INFINITY
|
|
637 && rlp.rlim_cur != RLIM_INFINITY
|
|
638 # endif
|
|
639 )
|
|
640 {
|
|
641 lim = (long)rlp.rlim_cur;
|
|
642 #if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
|
|
643 {
|
|
644 pthread_attr_t attr;
|
|
645 size_t size;
|
|
646
|
|
647 /* On FreeBSD the initial thread always has a fixed stack size, no
|
|
648 * matter what the limits are set to. Normally it's 1 Mbyte. */
|
|
649 pthread_attr_init(&attr);
|
|
650 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
|
|
651 {
|
|
652 pthread_attr_getstacksize(&attr, &size);
|
|
653 if (lim > (long)size)
|
|
654 lim = (long)size;
|
|
655 }
|
|
656 pthread_attr_destroy(&attr);
|
|
657 }
|
|
658 #endif
|
|
659 if (stack_grows_downwards)
|
|
660 {
|
|
661 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
|
|
662 if (stack_limit >= (char *)&i)
|
|
663 /* overflow, set to 1/16 of current stack position */
|
|
664 stack_limit = (char *)((long)&i / 16L);
|
|
665 }
|
|
666 else
|
|
667 {
|
|
668 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
|
|
669 if (stack_limit <= (char *)&i)
|
|
670 stack_limit = NULL; /* overflow */
|
|
671 }
|
|
672 }
|
|
673 }
|
|
674
|
|
675 /*
|
|
676 * Return FAIL when running out of stack space.
|
|
677 * "p" must point to any variable local to the caller that's on the stack.
|
|
678 */
|
|
679 int
|
|
680 mch_stackcheck(p)
|
|
681 char *p;
|
|
682 {
|
|
683 if (stack_limit != NULL)
|
|
684 {
|
|
685 if (stack_grows_downwards)
|
|
686 {
|
|
687 if (p < stack_limit)
|
|
688 return FAIL;
|
|
689 }
|
|
690 else if (p > stack_limit)
|
|
691 return FAIL;
|
|
692 }
|
|
693 return OK;
|
|
694 }
|
|
695 #endif
|
|
696
|
|
697 #if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
|
|
698 /*
|
|
699 * Support for using the signal stack.
|
|
700 * This helps when we run out of stack space, which causes a SIGSEGV. The
|
|
701 * signal handler then must run on another stack, since the normal stack is
|
|
702 * completely full.
|
|
703 */
|
|
704
|
|
705 #ifndef SIGSTKSZ
|
|
706 # define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
|
|
707 #endif
|
|
708
|
|
709 # ifdef HAVE_SIGALTSTACK
|
|
710 static stack_t sigstk; /* for sigaltstack() */
|
|
711 # else
|
|
712 static struct sigstack sigstk; /* for sigstack() */
|
|
713 # endif
|
|
714
|
|
715 static void init_signal_stack __ARGS((void));
|
|
716 static char *signal_stack;
|
|
717
|
|
718 static void
|
|
719 init_signal_stack()
|
|
720 {
|
|
721 if (signal_stack != NULL)
|
|
722 {
|
|
723 # ifdef HAVE_SIGALTSTACK
|
|
724 # ifdef __APPLE__
|
|
725 /* missing prototype. Adding it to osdef?.h.in doesn't work, because
|
|
726 * "struct sigaltstack" needs to be declared. */
|
|
727 extern int sigaltstack __ARGS((const struct sigaltstack *ss, struct sigaltstack *oss));
|
|
728 # endif
|
|
729
|
|
730 # ifdef HAVE_SS_BASE
|
|
731 sigstk.ss_base = signal_stack;
|
|
732 # else
|
|
733 sigstk.ss_sp = signal_stack;
|
|
734 # endif
|
|
735 sigstk.ss_size = SIGSTKSZ;
|
|
736 sigstk.ss_flags = 0;
|
|
737 (void)sigaltstack(&sigstk, NULL);
|
|
738 # else
|
|
739 sigstk.ss_sp = signal_stack;
|
|
740 if (stack_grows_downwards)
|
|
741 sigstk.ss_sp += SIGSTKSZ - 1;
|
|
742 sigstk.ss_onstack = 0;
|
|
743 (void)sigstack(&sigstk, NULL);
|
|
744 # endif
|
|
745 }
|
|
746 }
|
|
747 #endif
|
|
748
|
|
749 /*
|
|
750 * We need correct potatotypes for a signal function, otherwise mean compilers
|
|
751 * will barf when the second argument to signal() is ``wrong''.
|
|
752 * Let me try it with a few tricky defines from my own osdef.h (jw).
|
|
753 */
|
|
754 #if defined(SIGWINCH)
|
|
755 /* ARGSUSED */
|
|
756 static RETSIGTYPE
|
|
757 sig_winch SIGDEFARG(sigarg)
|
|
758 {
|
|
759 /* this is not required on all systems, but it doesn't hurt anybody */
|
|
760 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
|
|
761 do_resize = TRUE;
|
|
762 SIGRETURN;
|
|
763 }
|
|
764 #endif
|
|
765
|
|
766 #if defined(SIGINT)
|
|
767 /* ARGSUSED */
|
|
768 static RETSIGTYPE
|
|
769 catch_sigint SIGDEFARG(sigarg)
|
|
770 {
|
|
771 /* this is not required on all systems, but it doesn't hurt anybody */
|
|
772 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
|
|
773 got_int = TRUE;
|
|
774 SIGRETURN;
|
|
775 }
|
|
776 #endif
|
|
777
|
|
778 #if defined(SIGPWR)
|
|
779 /* ARGSUSED */
|
|
780 static RETSIGTYPE
|
|
781 catch_sigpwr SIGDEFARG(sigarg)
|
|
782 {
|
37
|
783 /* this is not required on all systems, but it doesn't hurt anybody */
|
|
784 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
|
7
|
785 /*
|
|
786 * I'm not sure we get the SIGPWR signal when the system is really going
|
|
787 * down or when the batteries are almost empty. Just preserve the swap
|
|
788 * files and don't exit, that can't do any harm.
|
|
789 */
|
|
790 ml_sync_all(FALSE, FALSE);
|
|
791 SIGRETURN;
|
|
792 }
|
|
793 #endif
|
|
794
|
|
795 #ifdef SET_SIG_ALARM
|
|
796 /*
|
|
797 * signal function for alarm().
|
|
798 */
|
|
799 /* ARGSUSED */
|
|
800 static RETSIGTYPE
|
|
801 sig_alarm SIGDEFARG(sigarg)
|
|
802 {
|
|
803 /* doesn't do anything, just to break a system call */
|
|
804 sig_alarm_called = TRUE;
|
|
805 SIGRETURN;
|
|
806 }
|
|
807 #endif
|
|
808
|
188
|
809 #if (defined(HAVE_SETJMP_H) \
|
|
810 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
|
|
811 || defined(FEAT_LIBCALL))) \
|
|
812 || defined(PROTO)
|
7
|
813 /*
|
|
814 * A simplistic version of setjmp() that only allows one level of using.
|
|
815 * Don't call twice before calling mch_endjmp()!.
|
|
816 * Usage:
|
|
817 * mch_startjmp();
|
|
818 * if (SETJMP(lc_jump_env) != 0)
|
|
819 * {
|
|
820 * mch_didjmp();
|
|
821 * EMSG("crash!");
|
|
822 * }
|
|
823 * else
|
|
824 * {
|
|
825 * do_the_work;
|
|
826 * mch_endjmp();
|
|
827 * }
|
|
828 * Note: Can't move SETJMP() here, because a function calling setjmp() must
|
|
829 * not return before the saved environment is used.
|
|
830 * Returns OK for normal return, FAIL when the protected code caused a
|
|
831 * problem and LONGJMP() was used.
|
|
832 */
|
|
833 void
|
|
834 mch_startjmp()
|
|
835 {
|
|
836 #ifdef SIGHASARG
|
|
837 lc_signal = 0;
|
|
838 #endif
|
|
839 lc_active = TRUE;
|
|
840 }
|
|
841
|
|
842 void
|
|
843 mch_endjmp()
|
|
844 {
|
|
845 lc_active = FALSE;
|
|
846 }
|
|
847
|
|
848 void
|
|
849 mch_didjmp()
|
|
850 {
|
|
851 # if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
|
|
852 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
|
|
853 * otherwise catching the signal only works once. */
|
|
854 init_signal_stack();
|
|
855 # endif
|
|
856 }
|
|
857 #endif
|
|
858
|
|
859 /*
|
|
860 * This function handles deadly signals.
|
|
861 * It tries to preserve any swap file and exit properly.
|
|
862 * (partly from Elvis).
|
|
863 */
|
|
864 static RETSIGTYPE
|
|
865 deathtrap SIGDEFARG(sigarg)
|
|
866 {
|
|
867 static int entered = 0; /* count the number of times we got here.
|
|
868 Note: when memory has been corrupted
|
|
869 this may get an arbitrary value! */
|
|
870 #ifdef SIGHASARG
|
|
871 int i;
|
|
872 #endif
|
|
873
|
|
874 #if defined(HAVE_SETJMP_H)
|
|
875 /*
|
|
876 * Catch a crash in protected code.
|
|
877 * Restores the environment saved in lc_jump_env, which looks like
|
|
878 * SETJMP() returns 1.
|
|
879 */
|
|
880 if (lc_active)
|
|
881 {
|
|
882 # if defined(SIGHASARG)
|
|
883 lc_signal = sigarg;
|
|
884 # endif
|
|
885 lc_active = FALSE; /* don't jump again */
|
|
886 LONGJMP(lc_jump_env, 1);
|
|
887 /* NOTREACHED */
|
|
888 }
|
|
889 #endif
|
|
890
|
36
|
891 #ifdef SIGHASARG
|
37
|
892 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
|
|
893 * here. This avoids that a non-reentrant function is interrupted, e.g.,
|
|
894 * free(). Calling free() again may then cause a crash. */
|
|
895 if (entered == 0
|
|
896 && (0
|
|
897 # ifdef SIGHUP
|
|
898 || sigarg == SIGHUP
|
|
899 # endif
|
|
900 # ifdef SIGQUIT
|
|
901 || sigarg == SIGQUIT
|
|
902 # endif
|
|
903 # ifdef SIGTERM
|
|
904 || sigarg == SIGTERM
|
|
905 # endif
|
|
906 # ifdef SIGPWR
|
|
907 || sigarg == SIGPWR
|
|
908 # endif
|
|
909 # ifdef SIGUSR1
|
|
910 || sigarg == SIGUSR1
|
|
911 # endif
|
|
912 # ifdef SIGUSR2
|
|
913 || sigarg == SIGUSR2
|
|
914 # endif
|
|
915 )
|
413
|
916 && !vim_handle_signal(sigarg))
|
36
|
917 SIGRETURN;
|
|
918 #endif
|
|
919
|
7
|
920 /* Remember how often we have been called. */
|
|
921 ++entered;
|
|
922
|
|
923 #ifdef FEAT_EVAL
|
|
924 /* Set the v:dying variable. */
|
|
925 set_vim_var_nr(VV_DYING, (long)entered);
|
|
926 #endif
|
|
927
|
180
|
928 #ifdef HAVE_STACK_LIMIT
|
7
|
929 /* Since we are now using the signal stack, need to reset the stack
|
|
930 * limit. Otherwise using a regexp will fail. */
|
|
931 get_stack_limit();
|
|
932 #endif
|
|
933
|
758
|
934 #if 0
|
|
935 /* This is for opening gdb the moment Vim crashes.
|
|
936 * You need to manually adjust the file name and Vim executable name.
|
|
937 * Suggested by SungHyun Nam. */
|
|
938 {
|
|
939 # define VI_GDB_FILE "/tmp/vimgdb"
|
|
940 # define VIM_NAME "/usr/bin/vim"
|
|
941 FILE *fp = fopen(VI_GDB_FILE, "w");
|
|
942 if (fp)
|
|
943 {
|
|
944 fprintf(fp,
|
|
945 "file %s\n"
|
|
946 "attach %d\n"
|
|
947 "set height 1000\n"
|
|
948 "bt full\n"
|
|
949 , VIM_NAME, getpid());
|
|
950 fclose(fp);
|
|
951 system("xterm -e gdb -x "VI_GDB_FILE);
|
|
952 unlink(VI_GDB_FILE);
|
|
953 }
|
|
954 }
|
|
955 #endif
|
|
956
|
7
|
957 #ifdef SIGHASARG
|
|
958 /* try to find the name of this signal */
|
|
959 for (i = 0; signal_info[i].sig != -1; i++)
|
|
960 if (sigarg == signal_info[i].sig)
|
|
961 break;
|
|
962 deadly_signal = sigarg;
|
|
963 #endif
|
|
964
|
|
965 full_screen = FALSE; /* don't write message to the GUI, it might be
|
|
966 * part of the problem... */
|
|
967 /*
|
|
968 * If something goes wrong after entering here, we may get here again.
|
|
969 * When this happens, give a message and try to exit nicely (resetting the
|
|
970 * terminal mode, etc.)
|
|
971 * When this happens twice, just exit, don't even try to give a message,
|
|
972 * stack may be corrupt or something weird.
|
|
973 * When this still happens again (or memory was corrupted in such a way
|
|
974 * that "entered" was clobbered) use _exit(), don't try freeing resources.
|
|
975 */
|
|
976 if (entered >= 3)
|
|
977 {
|
|
978 reset_signals(); /* don't catch any signals anymore */
|
|
979 may_core_dump();
|
|
980 if (entered >= 4)
|
|
981 _exit(8);
|
|
982 exit(7);
|
|
983 }
|
|
984 if (entered == 2)
|
|
985 {
|
|
986 OUT_STR(_("Vim: Double signal, exiting\n"));
|
|
987 out_flush();
|
|
988 getout(1);
|
|
989 }
|
|
990
|
|
991 #ifdef SIGHASARG
|
|
992 sprintf((char *)IObuff, _("Vim: Caught deadly signal %s\n"),
|
|
993 signal_info[i].name);
|
|
994 #else
|
|
995 sprintf((char *)IObuff, _("Vim: Caught deadly signal\n"));
|
|
996 #endif
|
|
997 preserve_exit(); /* preserve files and exit */
|
|
998
|
33
|
999 #ifdef NBDEBUG
|
|
1000 reset_signals();
|
|
1001 may_core_dump();
|
|
1002 abort();
|
|
1003 #endif
|
|
1004
|
7
|
1005 SIGRETURN;
|
|
1006 }
|
|
1007
|
|
1008 #ifdef _REENTRANT
|
|
1009 /*
|
|
1010 * On Solaris with multi-threading, suspending might not work immediately.
|
|
1011 * Catch the SIGCONT signal, which will be used as an indication whether the
|
|
1012 * suspending has been done or not.
|
|
1013 */
|
|
1014 static int sigcont_received;
|
|
1015 static RETSIGTYPE sigcont_handler __ARGS(SIGPROTOARG);
|
|
1016
|
|
1017 /*
|
|
1018 * signal handler for SIGCONT
|
|
1019 */
|
|
1020 /* ARGSUSED */
|
|
1021 static RETSIGTYPE
|
|
1022 sigcont_handler SIGDEFARG(sigarg)
|
|
1023 {
|
|
1024 sigcont_received = TRUE;
|
|
1025 SIGRETURN;
|
|
1026 }
|
|
1027 #endif
|
|
1028
|
|
1029 /*
|
|
1030 * If the machine has job control, use it to suspend the program,
|
|
1031 * otherwise fake it by starting a new shell.
|
|
1032 */
|
|
1033 void
|
|
1034 mch_suspend()
|
|
1035 {
|
|
1036 /* BeOS does have SIGTSTP, but it doesn't work. */
|
|
1037 #if defined(SIGTSTP) && !defined(__BEOS__)
|
|
1038 out_flush(); /* needed to make cursor visible on some systems */
|
|
1039 settmode(TMODE_COOK);
|
|
1040 out_flush(); /* needed to disable mouse on some systems */
|
|
1041
|
|
1042 # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
|
|
1043 /* Since we are going to sleep, we can't respond to requests for the X
|
|
1044 * selections. Lose them, otherwise other applications will hang. But
|
|
1045 * first copy the text to cut buffer 0. */
|
|
1046 if (clip_star.owned || clip_plus.owned)
|
|
1047 {
|
|
1048 x11_export_final_selection();
|
|
1049 if (clip_star.owned)
|
|
1050 clip_lose_selection(&clip_star);
|
|
1051 if (clip_plus.owned)
|
|
1052 clip_lose_selection(&clip_plus);
|
|
1053 if (x11_display != NULL)
|
|
1054 XFlush(x11_display);
|
|
1055 }
|
|
1056 # endif
|
|
1057
|
|
1058 # ifdef _REENTRANT
|
|
1059 sigcont_received = FALSE;
|
|
1060 # endif
|
|
1061 kill(0, SIGTSTP); /* send ourselves a STOP signal */
|
|
1062 # ifdef _REENTRANT
|
|
1063 /* When we didn't suspend immediately in the kill(), do it now. Happens
|
|
1064 * on multi-threaded Solaris. */
|
|
1065 if (!sigcont_received)
|
|
1066 pause();
|
|
1067 # endif
|
|
1068
|
|
1069 # ifdef FEAT_TITLE
|
|
1070 /*
|
|
1071 * Set oldtitle to NULL, so the current title is obtained again.
|
|
1072 */
|
|
1073 vim_free(oldtitle);
|
|
1074 oldtitle = NULL;
|
|
1075 # endif
|
|
1076 settmode(TMODE_RAW);
|
|
1077 need_check_timestamps = TRUE;
|
|
1078 did_check_timestamps = FALSE;
|
|
1079 #else
|
|
1080 suspend_shell();
|
|
1081 #endif
|
|
1082 }
|
|
1083
|
|
1084 void
|
|
1085 mch_init()
|
|
1086 {
|
|
1087 Columns = 80;
|
|
1088 Rows = 24;
|
|
1089
|
|
1090 out_flush();
|
|
1091 set_signals();
|
167
|
1092
|
765
|
1093 #ifdef MACOS_CONVERT
|
167
|
1094 mac_conv_init();
|
|
1095 #endif
|
7
|
1096 }
|
|
1097
|
|
1098 static void
|
|
1099 set_signals()
|
|
1100 {
|
|
1101 #if defined(SIGWINCH)
|
|
1102 /*
|
|
1103 * WINDOW CHANGE signal is handled with sig_winch().
|
|
1104 */
|
|
1105 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
|
|
1106 #endif
|
|
1107
|
|
1108 /*
|
|
1109 * We want the STOP signal to work, to make mch_suspend() work.
|
|
1110 * For "rvim" the STOP signal is ignored.
|
|
1111 */
|
|
1112 #ifdef SIGTSTP
|
|
1113 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
|
|
1114 #endif
|
|
1115 #ifdef _REENTRANT
|
|
1116 signal(SIGCONT, sigcont_handler);
|
|
1117 #endif
|
|
1118
|
|
1119 /*
|
|
1120 * We want to ignore breaking of PIPEs.
|
|
1121 */
|
|
1122 #ifdef SIGPIPE
|
|
1123 signal(SIGPIPE, SIG_IGN);
|
|
1124 #endif
|
|
1125
|
|
1126 #ifdef SIGINT
|
167
|
1127 catch_int_signal();
|
7
|
1128 #endif
|
|
1129
|
|
1130 /*
|
|
1131 * Ignore alarm signals (Perl's alarm() generates it).
|
|
1132 */
|
|
1133 #ifdef SIGALRM
|
|
1134 signal(SIGALRM, SIG_IGN);
|
|
1135 #endif
|
|
1136
|
|
1137 /*
|
|
1138 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
|
|
1139 * work will be lost.
|
|
1140 */
|
|
1141 #ifdef SIGPWR
|
|
1142 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
|
|
1143 #endif
|
|
1144
|
|
1145 /*
|
|
1146 * Arrange for other signals to gracefully shutdown Vim.
|
|
1147 */
|
|
1148 catch_signals(deathtrap, SIG_ERR);
|
|
1149
|
|
1150 #if defined(FEAT_GUI) && defined(SIGHUP)
|
|
1151 /*
|
|
1152 * When the GUI is running, ignore the hangup signal.
|
|
1153 */
|
|
1154 if (gui.in_use)
|
|
1155 signal(SIGHUP, SIG_IGN);
|
|
1156 #endif
|
|
1157 }
|
|
1158
|
167
|
1159 #if defined(SIGINT) || defined(PROTO)
|
|
1160 /*
|
|
1161 * Catch CTRL-C (only works while in Cooked mode).
|
|
1162 */
|
|
1163 static void
|
|
1164 catch_int_signal()
|
|
1165 {
|
|
1166 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
|
|
1167 }
|
|
1168 #endif
|
|
1169
|
7
|
1170 void
|
|
1171 reset_signals()
|
|
1172 {
|
|
1173 catch_signals(SIG_DFL, SIG_DFL);
|
|
1174 #ifdef _REENTRANT
|
|
1175 /* SIGCONT isn't in the list, because its default action is ignore */
|
|
1176 signal(SIGCONT, SIG_DFL);
|
|
1177 #endif
|
|
1178 }
|
|
1179
|
|
1180 static void
|
|
1181 catch_signals(func_deadly, func_other)
|
|
1182 RETSIGTYPE (*func_deadly)();
|
|
1183 RETSIGTYPE (*func_other)();
|
|
1184 {
|
|
1185 int i;
|
|
1186
|
|
1187 for (i = 0; signal_info[i].sig != -1; i++)
|
|
1188 if (signal_info[i].deadly)
|
|
1189 {
|
|
1190 #if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
|
|
1191 struct sigaction sa;
|
|
1192
|
|
1193 /* Setup to use the alternate stack for the signal function. */
|
|
1194 sa.sa_handler = func_deadly;
|
|
1195 sigemptyset(&sa.sa_mask);
|
|
1196 # if defined(__linux__) && defined(_REENTRANT)
|
|
1197 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
|
|
1198 * thread handling in combination with using the alternate stack:
|
|
1199 * pthread library functions try to use the stack pointer to
|
|
1200 * identify the current thread, causing a SEGV signal, which
|
|
1201 * recursively calls deathtrap() and hangs. */
|
|
1202 sa.sa_flags = 0;
|
|
1203 # else
|
|
1204 sa.sa_flags = SA_ONSTACK;
|
|
1205 # endif
|
|
1206 sigaction(signal_info[i].sig, &sa, NULL);
|
|
1207 #else
|
|
1208 # if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
|
|
1209 struct sigvec sv;
|
|
1210
|
|
1211 /* Setup to use the alternate stack for the signal function. */
|
|
1212 sv.sv_handler = func_deadly;
|
|
1213 sv.sv_mask = 0;
|
|
1214 sv.sv_flags = SV_ONSTACK;
|
|
1215 sigvec(signal_info[i].sig, &sv, NULL);
|
|
1216 # else
|
|
1217 signal(signal_info[i].sig, func_deadly);
|
|
1218 # endif
|
|
1219 #endif
|
|
1220 }
|
|
1221 else if (func_other != SIG_ERR)
|
|
1222 signal(signal_info[i].sig, func_other);
|
|
1223 }
|
|
1224
|
|
1225 /*
|
37
|
1226 * Handling of SIGHUP, SIGQUIT and SIGTERM:
|
|
1227 * "when" == a signal: when busy, postpone, otherwise return TRUE
|
|
1228 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
|
|
1229 * "when" == SIGNAL_UNBLOCK: Going wait, unblock signals
|
36
|
1230 * Returns TRUE when Vim should exit.
|
|
1231 */
|
|
1232 int
|
413
|
1233 vim_handle_signal(sig)
|
37
|
1234 int sig;
|
36
|
1235 {
|
37
|
1236 static int got_signal = 0;
|
|
1237 static int blocked = TRUE;
|
|
1238
|
|
1239 switch (sig)
|
36
|
1240 {
|
37
|
1241 case SIGNAL_BLOCK: blocked = TRUE;
|
|
1242 break;
|
|
1243
|
|
1244 case SIGNAL_UNBLOCK: blocked = FALSE;
|
|
1245 if (got_signal != 0)
|
|
1246 {
|
|
1247 kill(getpid(), got_signal);
|
|
1248 got_signal = 0;
|
|
1249 }
|
|
1250 break;
|
|
1251
|
|
1252 default: if (!blocked)
|
36
|
1253 return TRUE; /* exit! */
|
37
|
1254 got_signal = sig;
|
|
1255 #ifdef SIGPWR
|
|
1256 if (sig != SIGPWR)
|
|
1257 #endif
|
|
1258 got_int = TRUE; /* break any loops */
|
36
|
1259 break;
|
|
1260 }
|
|
1261 return FALSE;
|
|
1262 }
|
|
1263
|
|
1264 /*
|
7
|
1265 * Check_win checks whether we have an interactive stdout.
|
|
1266 */
|
|
1267 /* ARGSUSED */
|
|
1268 int
|
|
1269 mch_check_win(argc, argv)
|
|
1270 int argc;
|
|
1271 char **argv;
|
|
1272 {
|
|
1273 #ifdef OS2
|
|
1274 /*
|
|
1275 * Store argv[0], may be used for $VIM. Only use it if it is an absolute
|
|
1276 * name, mostly it's just "vim" and found in the path, which is unusable.
|
|
1277 */
|
|
1278 if (mch_isFullName(argv[0]))
|
|
1279 exe_name = vim_strsave((char_u *)argv[0]);
|
|
1280 #endif
|
|
1281 if (isatty(1))
|
|
1282 return OK;
|
|
1283 return FAIL;
|
|
1284 }
|
|
1285
|
|
1286 /*
|
|
1287 * Return TRUE if the input comes from a terminal, FALSE otherwise.
|
|
1288 */
|
|
1289 int
|
|
1290 mch_input_isatty()
|
|
1291 {
|
|
1292 if (isatty(read_cmd_fd))
|
|
1293 return TRUE;
|
|
1294 return FALSE;
|
|
1295 }
|
|
1296
|
|
1297 #ifdef FEAT_X11
|
|
1298
|
|
1299 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) \
|
|
1300 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
|
|
1301
|
|
1302 static void xopen_message __ARGS((struct timeval *tvp));
|
|
1303
|
|
1304 /*
|
|
1305 * Give a message about the elapsed time for opening the X window.
|
|
1306 */
|
|
1307 static void
|
|
1308 xopen_message(tvp)
|
|
1309 struct timeval *tvp; /* must contain start time */
|
|
1310 {
|
|
1311 struct timeval end_tv;
|
|
1312
|
|
1313 /* Compute elapsed time. */
|
|
1314 gettimeofday(&end_tv, NULL);
|
|
1315 smsg((char_u *)_("Opening the X display took %ld msec"),
|
|
1316 (end_tv.tv_sec - tvp->tv_sec) * 1000L
|
273
|
1317 + (end_tv.tv_usec - tvp->tv_usec) / 1000L);
|
7
|
1318 }
|
|
1319 # endif
|
|
1320 #endif
|
|
1321
|
|
1322 #if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
|
|
1323 /*
|
|
1324 * A few functions shared by X11 title and clipboard code.
|
|
1325 */
|
|
1326 static int x_error_handler __ARGS((Display *dpy, XErrorEvent *error_event));
|
|
1327 static int x_error_check __ARGS((Display *dpy, XErrorEvent *error_event));
|
|
1328 static int x_connect_to_server __ARGS((void));
|
|
1329 static int test_x11_window __ARGS((Display *dpy));
|
|
1330
|
|
1331 static int got_x_error = FALSE;
|
|
1332
|
|
1333 /*
|
|
1334 * X Error handler, otherwise X just exits! (very rude) -- webb
|
|
1335 */
|
|
1336 static int
|
|
1337 x_error_handler(dpy, error_event)
|
|
1338 Display *dpy;
|
|
1339 XErrorEvent *error_event;
|
|
1340 {
|
42
|
1341 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
|
7
|
1342 STRCAT(IObuff, _("\nVim: Got X error\n"));
|
|
1343
|
|
1344 /* We cannot print a message and continue, because no X calls are allowed
|
|
1345 * here (causes my system to hang). Silently continuing might be an
|
|
1346 * alternative... */
|
|
1347 preserve_exit(); /* preserve files and exit */
|
|
1348
|
|
1349 return 0; /* NOTREACHED */
|
|
1350 }
|
|
1351
|
|
1352 /*
|
|
1353 * Another X Error handler, just used to check for errors.
|
|
1354 */
|
|
1355 /* ARGSUSED */
|
|
1356 static int
|
|
1357 x_error_check(dpy, error_event)
|
|
1358 Display *dpy;
|
|
1359 XErrorEvent *error_event;
|
|
1360 {
|
|
1361 got_x_error = TRUE;
|
|
1362 return 0;
|
|
1363 }
|
|
1364
|
|
1365 #if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
|
|
1366 # if defined(HAVE_SETJMP_H)
|
|
1367 /*
|
|
1368 * An X IO Error handler, used to catch error while opening the display.
|
|
1369 */
|
|
1370 static int x_IOerror_check __ARGS((Display *dpy));
|
|
1371
|
|
1372 /* ARGSUSED */
|
|
1373 static int
|
|
1374 x_IOerror_check(dpy)
|
|
1375 Display *dpy;
|
|
1376 {
|
|
1377 /* This function should not return, it causes exit(). Longjump instead. */
|
|
1378 LONGJMP(lc_jump_env, 1);
|
|
1379 /*NOTREACHED*/
|
|
1380 return 0;
|
|
1381 }
|
|
1382 # endif
|
|
1383
|
|
1384 /*
|
|
1385 * An X IO Error handler, used to catch terminal errors.
|
|
1386 */
|
|
1387 static int x_IOerror_handler __ARGS((Display *dpy));
|
|
1388
|
|
1389 /* ARGSUSED */
|
|
1390 static int
|
|
1391 x_IOerror_handler(dpy)
|
|
1392 Display *dpy;
|
|
1393 {
|
|
1394 xterm_dpy = NULL;
|
|
1395 x11_window = 0;
|
|
1396 x11_display = NULL;
|
|
1397 xterm_Shell = (Widget)0;
|
|
1398
|
|
1399 /* This function should not return, it causes exit(). Longjump instead. */
|
|
1400 LONGJMP(x_jump_env, 1);
|
|
1401 /*NOTREACHED*/
|
|
1402 return 0;
|
|
1403 }
|
|
1404 #endif
|
|
1405
|
|
1406 /*
|
|
1407 * Return TRUE when connection to the X server is desired.
|
|
1408 */
|
|
1409 static int
|
|
1410 x_connect_to_server()
|
|
1411 {
|
|
1412 regmatch_T regmatch;
|
|
1413
|
|
1414 #if defined(FEAT_CLIENTSERVER)
|
|
1415 if (x_force_connect)
|
|
1416 return TRUE;
|
|
1417 #endif
|
|
1418 if (x_no_connect)
|
|
1419 return FALSE;
|
|
1420
|
|
1421 /* Check for a match with "exclude:" from 'clipboard'. */
|
|
1422 if (clip_exclude_prog != NULL)
|
|
1423 {
|
|
1424 regmatch.rm_ic = FALSE; /* Don't ignore case */
|
|
1425 regmatch.regprog = clip_exclude_prog;
|
|
1426 if (vim_regexec(®match, T_NAME, (colnr_T)0))
|
|
1427 return FALSE;
|
|
1428 }
|
|
1429 return TRUE;
|
|
1430 }
|
|
1431
|
|
1432 /*
|
|
1433 * Test if "dpy" and x11_window are valid by getting the window title.
|
|
1434 * I don't actually want it yet, so there may be a simpler call to use, but
|
|
1435 * this will cause the error handler x_error_check() to be called if anything
|
|
1436 * is wrong, such as the window pointer being invalid (as can happen when the
|
|
1437 * user changes his DISPLAY, but not his WINDOWID) -- webb
|
|
1438 */
|
|
1439 static int
|
|
1440 test_x11_window(dpy)
|
|
1441 Display *dpy;
|
|
1442 {
|
|
1443 int (*old_handler)();
|
|
1444 XTextProperty text_prop;
|
|
1445
|
|
1446 old_handler = XSetErrorHandler(x_error_check);
|
|
1447 got_x_error = FALSE;
|
|
1448 if (XGetWMName(dpy, x11_window, &text_prop))
|
|
1449 XFree((void *)text_prop.value);
|
|
1450 XSync(dpy, False);
|
|
1451 (void)XSetErrorHandler(old_handler);
|
|
1452
|
|
1453 if (p_verbose > 0 && got_x_error)
|
292
|
1454 verb_msg((char_u *)_("Testing the X display failed"));
|
7
|
1455
|
|
1456 return (got_x_error ? FAIL : OK);
|
|
1457 }
|
|
1458 #endif
|
|
1459
|
|
1460 #ifdef FEAT_TITLE
|
|
1461
|
|
1462 #ifdef FEAT_X11
|
|
1463
|
|
1464 static int get_x11_thing __ARGS((int get_title, int test_only));
|
|
1465
|
|
1466 /*
|
|
1467 * try to get x11 window and display
|
|
1468 *
|
|
1469 * return FAIL for failure, OK otherwise
|
|
1470 */
|
|
1471 static int
|
|
1472 get_x11_windis()
|
|
1473 {
|
|
1474 char *winid;
|
|
1475 static int result = -1;
|
|
1476 #define XD_NONE 0 /* x11_display not set here */
|
|
1477 #define XD_HERE 1 /* x11_display opened here */
|
|
1478 #define XD_GUI 2 /* x11_display used from gui.dpy */
|
|
1479 #define XD_XTERM 3 /* x11_display used from xterm_dpy */
|
|
1480 static int x11_display_from = XD_NONE;
|
|
1481 static int did_set_error_handler = FALSE;
|
|
1482
|
|
1483 if (!did_set_error_handler)
|
|
1484 {
|
|
1485 /* X just exits if it finds an error otherwise! */
|
|
1486 (void)XSetErrorHandler(x_error_handler);
|
|
1487 did_set_error_handler = TRUE;
|
|
1488 }
|
|
1489
|
574
|
1490 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
|
7
|
1491 if (gui.in_use)
|
|
1492 {
|
|
1493 /*
|
|
1494 * If the X11 display was opened here before, for the window where Vim
|
|
1495 * was started, close that one now to avoid a memory leak.
|
|
1496 */
|
|
1497 if (x11_display_from == XD_HERE && x11_display != NULL)
|
|
1498 {
|
|
1499 XCloseDisplay(x11_display);
|
|
1500 x11_display_from = XD_NONE;
|
|
1501 }
|
|
1502 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
|
|
1503 {
|
|
1504 x11_display_from = XD_GUI;
|
|
1505 return OK;
|
|
1506 }
|
|
1507 x11_display = NULL;
|
|
1508 return FAIL;
|
|
1509 }
|
|
1510 else if (x11_display_from == XD_GUI)
|
|
1511 {
|
|
1512 /* GUI must have stopped somehow, clear x11_display */
|
|
1513 x11_window = 0;
|
|
1514 x11_display = NULL;
|
|
1515 x11_display_from = XD_NONE;
|
|
1516 }
|
|
1517 #endif
|
|
1518
|
|
1519 /* When started with the "-X" argument, don't try connecting. */
|
|
1520 if (!x_connect_to_server())
|
|
1521 return FAIL;
|
|
1522
|
|
1523 /*
|
|
1524 * If WINDOWID not set, should try another method to find out
|
|
1525 * what the current window number is. The only code I know for
|
|
1526 * this is very complicated.
|
|
1527 * We assume that zero is invalid for WINDOWID.
|
|
1528 */
|
|
1529 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
|
|
1530 x11_window = (Window)atol(winid);
|
|
1531
|
|
1532 #ifdef FEAT_XCLIPBOARD
|
|
1533 if (xterm_dpy != NULL && x11_window != 0)
|
|
1534 {
|
|
1535 /* Checked it already. */
|
|
1536 if (x11_display_from == XD_XTERM)
|
|
1537 return OK;
|
|
1538
|
|
1539 /*
|
|
1540 * If the X11 display was opened here before, for the window where Vim
|
|
1541 * was started, close that one now to avoid a memory leak.
|
|
1542 */
|
|
1543 if (x11_display_from == XD_HERE && x11_display != NULL)
|
|
1544 XCloseDisplay(x11_display);
|
|
1545 x11_display = xterm_dpy;
|
|
1546 x11_display_from = XD_XTERM;
|
|
1547 if (test_x11_window(x11_display) == FAIL)
|
|
1548 {
|
|
1549 /* probably bad $WINDOWID */
|
|
1550 x11_window = 0;
|
|
1551 x11_display = NULL;
|
|
1552 x11_display_from = XD_NONE;
|
|
1553 return FAIL;
|
|
1554 }
|
|
1555 return OK;
|
|
1556 }
|
|
1557 #endif
|
|
1558
|
|
1559 if (x11_window == 0 || x11_display == NULL)
|
|
1560 result = -1;
|
|
1561
|
|
1562 if (result != -1) /* Have already been here and set this */
|
|
1563 return result; /* Don't do all these X calls again */
|
|
1564
|
|
1565 if (x11_window != 0 && x11_display == NULL)
|
|
1566 {
|
|
1567 #ifdef SET_SIG_ALARM
|
|
1568 RETSIGTYPE (*sig_save)();
|
|
1569 #endif
|
|
1570 #if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
|
|
1571 struct timeval start_tv;
|
|
1572
|
|
1573 if (p_verbose > 0)
|
|
1574 gettimeofday(&start_tv, NULL);
|
|
1575 #endif
|
|
1576
|
|
1577 #ifdef SET_SIG_ALARM
|
|
1578 /*
|
|
1579 * Opening the Display may hang if the DISPLAY setting is wrong, or
|
|
1580 * the network connection is bad. Set an alarm timer to get out.
|
|
1581 */
|
|
1582 sig_alarm_called = FALSE;
|
|
1583 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
|
|
1584 (RETSIGTYPE (*)())sig_alarm);
|
|
1585 alarm(2);
|
|
1586 #endif
|
|
1587 x11_display = XOpenDisplay(NULL);
|
|
1588
|
|
1589 #ifdef SET_SIG_ALARM
|
|
1590 alarm(0);
|
|
1591 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
|
|
1592 if (p_verbose > 0 && sig_alarm_called)
|
292
|
1593 verb_msg((char_u *)_("Opening the X display timed out"));
|
7
|
1594 #endif
|
|
1595 if (x11_display != NULL)
|
|
1596 {
|
|
1597 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
|
|
1598 if (p_verbose > 0)
|
292
|
1599 {
|
|
1600 verbose_enter();
|
7
|
1601 xopen_message(&start_tv);
|
292
|
1602 verbose_leave();
|
|
1603 }
|
7
|
1604 # endif
|
|
1605 if (test_x11_window(x11_display) == FAIL)
|
|
1606 {
|
|
1607 /* Maybe window id is bad */
|
|
1608 x11_window = 0;
|
|
1609 XCloseDisplay(x11_display);
|
|
1610 x11_display = NULL;
|
|
1611 }
|
|
1612 else
|
|
1613 x11_display_from = XD_HERE;
|
|
1614 }
|
|
1615 }
|
|
1616 if (x11_window == 0 || x11_display == NULL)
|
|
1617 return (result = FAIL);
|
|
1618 return (result = OK);
|
|
1619 }
|
|
1620
|
|
1621 /*
|
|
1622 * Determine original x11 Window Title
|
|
1623 */
|
|
1624 static int
|
|
1625 get_x11_title(test_only)
|
|
1626 int test_only;
|
|
1627 {
|
32
|
1628 return get_x11_thing(TRUE, test_only);
|
7
|
1629 }
|
|
1630
|
|
1631 /*
|
|
1632 * Determine original x11 Window icon
|
|
1633 */
|
|
1634 static int
|
|
1635 get_x11_icon(test_only)
|
|
1636 int test_only;
|
|
1637 {
|
|
1638 int retval = FALSE;
|
|
1639
|
|
1640 retval = get_x11_thing(FALSE, test_only);
|
|
1641
|
|
1642 /* could not get old icon, use terminal name */
|
|
1643 if (oldicon == NULL && !test_only)
|
|
1644 {
|
|
1645 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
|
|
1646 oldicon = T_NAME + 8;
|
|
1647 else
|
|
1648 oldicon = T_NAME;
|
|
1649 }
|
|
1650
|
|
1651 return retval;
|
|
1652 }
|
|
1653
|
|
1654 static int
|
|
1655 get_x11_thing(get_title, test_only)
|
|
1656 int get_title; /* get title string */
|
|
1657 int test_only;
|
|
1658 {
|
|
1659 XTextProperty text_prop;
|
|
1660 int retval = FALSE;
|
|
1661 Status status;
|
|
1662
|
|
1663 if (get_x11_windis() == OK)
|
|
1664 {
|
|
1665 /* Get window/icon name if any */
|
|
1666 if (get_title)
|
|
1667 status = XGetWMName(x11_display, x11_window, &text_prop);
|
|
1668 else
|
|
1669 status = XGetWMIconName(x11_display, x11_window, &text_prop);
|
|
1670
|
|
1671 /*
|
|
1672 * If terminal is xterm, then x11_window may be a child window of the
|
|
1673 * outer xterm window that actually contains the window/icon name, so
|
|
1674 * keep traversing up the tree until a window with a title/icon is
|
|
1675 * found.
|
|
1676 */
|
|
1677 /* Previously this was only done for xterm and alikes. I don't see a
|
|
1678 * reason why it would fail for other terminal emulators.
|
|
1679 * if (term_is_xterm) */
|
|
1680 {
|
|
1681 Window root;
|
|
1682 Window parent;
|
|
1683 Window win = x11_window;
|
|
1684 Window *children;
|
|
1685 unsigned int num_children;
|
|
1686
|
|
1687 while (!status || text_prop.value == NULL)
|
|
1688 {
|
|
1689 if (!XQueryTree(x11_display, win, &root, &parent, &children,
|
|
1690 &num_children))
|
|
1691 break;
|
|
1692 if (children)
|
|
1693 XFree((void *)children);
|
|
1694 if (parent == root || parent == 0)
|
|
1695 break;
|
|
1696
|
|
1697 win = parent;
|
|
1698 if (get_title)
|
|
1699 status = XGetWMName(x11_display, win, &text_prop);
|
|
1700 else
|
|
1701 status = XGetWMIconName(x11_display, win, &text_prop);
|
|
1702 }
|
|
1703 }
|
|
1704 if (status && text_prop.value != NULL)
|
|
1705 {
|
|
1706 retval = TRUE;
|
|
1707 if (!test_only)
|
|
1708 {
|
|
1709 #ifdef FEAT_XFONTSET
|
|
1710 if (text_prop.encoding == XA_STRING)
|
|
1711 {
|
|
1712 #endif
|
|
1713 if (get_title)
|
|
1714 oldtitle = vim_strsave((char_u *)text_prop.value);
|
|
1715 else
|
|
1716 oldicon = vim_strsave((char_u *)text_prop.value);
|
|
1717 #ifdef FEAT_XFONTSET
|
|
1718 }
|
|
1719 else
|
|
1720 {
|
|
1721 char **cl;
|
|
1722 Status transform_status;
|
|
1723 int n = 0;
|
|
1724
|
|
1725 transform_status = XmbTextPropertyToTextList(x11_display,
|
|
1726 &text_prop,
|
|
1727 &cl, &n);
|
|
1728 if (transform_status >= Success && n > 0 && cl[0])
|
|
1729 {
|
|
1730 if (get_title)
|
|
1731 oldtitle = vim_strsave((char_u *) cl[0]);
|
|
1732 else
|
|
1733 oldicon = vim_strsave((char_u *) cl[0]);
|
|
1734 XFreeStringList(cl);
|
|
1735 }
|
|
1736 else
|
|
1737 {
|
|
1738 if (get_title)
|
|
1739 oldtitle = vim_strsave((char_u *)text_prop.value);
|
|
1740 else
|
|
1741 oldicon = vim_strsave((char_u *)text_prop.value);
|
|
1742 }
|
|
1743 }
|
|
1744 #endif
|
|
1745 }
|
|
1746 XFree((void *)text_prop.value);
|
|
1747 }
|
|
1748 }
|
|
1749 return retval;
|
|
1750 }
|
|
1751
|
|
1752 /* Are Xutf8 functions available? Avoid error from old compilers. */
|
|
1753 #if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
|
|
1754 # if X_HAVE_UTF8_STRING
|
|
1755 # define USE_UTF8_STRING
|
|
1756 # endif
|
|
1757 #endif
|
|
1758
|
|
1759 /*
|
|
1760 * Set x11 Window Title
|
|
1761 *
|
|
1762 * get_x11_windis() must be called before this and have returned OK
|
|
1763 */
|
|
1764 static void
|
|
1765 set_x11_title(title)
|
|
1766 char_u *title;
|
|
1767 {
|
|
1768 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
|
|
1769 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
|
|
1770 * supported everywhere and STRING doesn't work for multi-byte titles.
|
|
1771 */
|
|
1772 #ifdef USE_UTF8_STRING
|
|
1773 if (enc_utf8)
|
|
1774 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
|
|
1775 NULL, NULL, 0, NULL, NULL, NULL);
|
|
1776 else
|
|
1777 #endif
|
|
1778 {
|
|
1779 #if XtSpecificationRelease >= 4
|
|
1780 # ifdef FEAT_XFONTSET
|
|
1781 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
|
|
1782 NULL, NULL, 0, NULL, NULL, NULL);
|
|
1783 # else
|
|
1784 XTextProperty text_prop;
|
129
|
1785 char *c_title = (char *)title;
|
7
|
1786
|
|
1787 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
|
129
|
1788 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
|
7
|
1789 XSetWMProperties(x11_display, x11_window, &text_prop,
|
|
1790 NULL, NULL, 0, NULL, NULL, NULL);
|
|
1791 # endif
|
|
1792 #else
|
|
1793 XStoreName(x11_display, x11_window, (char *)title);
|
|
1794 #endif
|
|
1795 }
|
|
1796 XFlush(x11_display);
|
|
1797 }
|
|
1798
|
|
1799 /*
|
|
1800 * Set x11 Window icon
|
|
1801 *
|
|
1802 * get_x11_windis() must be called before this and have returned OK
|
|
1803 */
|
|
1804 static void
|
|
1805 set_x11_icon(icon)
|
|
1806 char_u *icon;
|
|
1807 {
|
|
1808 /* See above for comments about using X*SetWMProperties(). */
|
|
1809 #ifdef USE_UTF8_STRING
|
|
1810 if (enc_utf8)
|
|
1811 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
|
|
1812 NULL, 0, NULL, NULL, NULL);
|
|
1813 else
|
|
1814 #endif
|
|
1815 {
|
|
1816 #if XtSpecificationRelease >= 4
|
|
1817 # ifdef FEAT_XFONTSET
|
|
1818 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
|
|
1819 NULL, 0, NULL, NULL, NULL);
|
|
1820 # else
|
|
1821 XTextProperty text_prop;
|
129
|
1822 char *c_icon = (char *)icon;
|
|
1823
|
|
1824 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
|
7
|
1825 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
|
|
1826 NULL, 0, NULL, NULL, NULL);
|
|
1827 # endif
|
|
1828 #else
|
|
1829 XSetIconName(x11_display, x11_window, (char *)icon);
|
|
1830 #endif
|
|
1831 }
|
|
1832 XFlush(x11_display);
|
|
1833 }
|
|
1834
|
|
1835 #else /* FEAT_X11 */
|
|
1836
|
|
1837 /*ARGSUSED*/
|
|
1838 static int
|
|
1839 get_x11_title(test_only)
|
|
1840 int test_only;
|
|
1841 {
|
|
1842 return FALSE;
|
|
1843 }
|
|
1844
|
|
1845 static int
|
|
1846 get_x11_icon(test_only)
|
|
1847 int test_only;
|
|
1848 {
|
|
1849 if (!test_only)
|
|
1850 {
|
|
1851 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
|
|
1852 oldicon = T_NAME + 8;
|
|
1853 else
|
|
1854 oldicon = T_NAME;
|
|
1855 }
|
|
1856 return FALSE;
|
|
1857 }
|
|
1858
|
|
1859 #endif /* FEAT_X11 */
|
|
1860
|
|
1861 int
|
|
1862 mch_can_restore_title()
|
|
1863 {
|
|
1864 return get_x11_title(TRUE);
|
|
1865 }
|
|
1866
|
|
1867 int
|
|
1868 mch_can_restore_icon()
|
|
1869 {
|
|
1870 return get_x11_icon(TRUE);
|
|
1871 }
|
|
1872
|
|
1873 /*
|
|
1874 * Set the window title and icon.
|
|
1875 */
|
|
1876 void
|
|
1877 mch_settitle(title, icon)
|
|
1878 char_u *title;
|
|
1879 char_u *icon;
|
|
1880 {
|
|
1881 int type = 0;
|
|
1882 static int recursive = 0;
|
|
1883
|
|
1884 if (T_NAME == NULL) /* no terminal name (yet) */
|
|
1885 return;
|
|
1886 if (title == NULL && icon == NULL) /* nothing to do */
|
|
1887 return;
|
|
1888
|
|
1889 /* When one of the X11 functions causes a deadly signal, we get here again
|
|
1890 * recursively. Avoid hanging then (something is probably locked). */
|
|
1891 if (recursive)
|
|
1892 return;
|
|
1893 ++recursive;
|
|
1894
|
|
1895 /*
|
|
1896 * if the window ID and the display is known, we may use X11 calls
|
|
1897 */
|
|
1898 #ifdef FEAT_X11
|
|
1899 if (get_x11_windis() == OK)
|
|
1900 type = 1;
|
|
1901 #else
|
|
1902 # if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
|
|
1903 if (gui.in_use)
|
|
1904 type = 1;
|
|
1905 # endif
|
|
1906 #endif
|
|
1907
|
|
1908 /*
|
|
1909 * Note: if "t_TS" is set, title is set with escape sequence rather
|
|
1910 * than x11 calls, because the x11 calls don't always work
|
|
1911 */
|
|
1912 if ((type || *T_TS != NUL) && title != NULL)
|
|
1913 {
|
|
1914 if (oldtitle == NULL
|
|
1915 #ifdef FEAT_GUI
|
|
1916 && !gui.in_use
|
|
1917 #endif
|
|
1918 ) /* first call but not in GUI, save title */
|
|
1919 (void)get_x11_title(FALSE);
|
|
1920
|
|
1921 if (*T_TS != NUL) /* it's OK if t_fs is empty */
|
|
1922 term_settitle(title);
|
|
1923 #ifdef FEAT_X11
|
|
1924 else
|
|
1925 # ifdef FEAT_GUI_GTK
|
|
1926 if (!gui.in_use) /* don't do this if GTK+ is running */
|
|
1927 # endif
|
|
1928 set_x11_title(title); /* x11 */
|
|
1929 #endif
|
64
|
1930 #if defined(FEAT_GUI_GTK) \
|
7
|
1931 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
|
|
1932 else
|
|
1933 gui_mch_settitle(title, icon);
|
|
1934 #endif
|
|
1935 did_set_title = TRUE;
|
|
1936 }
|
|
1937
|
|
1938 if ((type || *T_CIS != NUL) && icon != NULL)
|
|
1939 {
|
|
1940 if (oldicon == NULL
|
|
1941 #ifdef FEAT_GUI
|
|
1942 && !gui.in_use
|
|
1943 #endif
|
|
1944 ) /* first call, save icon */
|
|
1945 get_x11_icon(FALSE);
|
|
1946
|
|
1947 if (*T_CIS != NUL)
|
|
1948 {
|
|
1949 out_str(T_CIS); /* set icon start */
|
|
1950 out_str_nf(icon);
|
|
1951 out_str(T_CIE); /* set icon end */
|
|
1952 out_flush();
|
|
1953 }
|
|
1954 #ifdef FEAT_X11
|
|
1955 else
|
|
1956 # ifdef FEAT_GUI_GTK
|
|
1957 if (!gui.in_use) /* don't do this if GTK+ is running */
|
|
1958 # endif
|
|
1959 set_x11_icon(icon); /* x11 */
|
|
1960 #endif
|
|
1961 did_set_icon = TRUE;
|
|
1962 }
|
|
1963 --recursive;
|
|
1964 }
|
|
1965
|
|
1966 /*
|
|
1967 * Restore the window/icon title.
|
|
1968 * "which" is one of:
|
|
1969 * 1 only restore title
|
|
1970 * 2 only restore icon
|
|
1971 * 3 restore title and icon
|
|
1972 */
|
|
1973 void
|
|
1974 mch_restore_title(which)
|
|
1975 int which;
|
|
1976 {
|
|
1977 /* only restore the title or icon when it has been set */
|
|
1978 mch_settitle(((which & 1) && did_set_title) ?
|
|
1979 (oldtitle ? oldtitle : p_titleold) : NULL,
|
|
1980 ((which & 2) && did_set_icon) ? oldicon : NULL);
|
|
1981 }
|
|
1982
|
|
1983 #endif /* FEAT_TITLE */
|
|
1984
|
|
1985 /*
|
|
1986 * Return TRUE if "name" looks like some xterm name.
|
158
|
1987 * Seiichi Sato mentioned that "mlterm" works like xterm.
|
7
|
1988 */
|
|
1989 int
|
|
1990 vim_is_xterm(name)
|
|
1991 char_u *name;
|
|
1992 {
|
|
1993 if (name == NULL)
|
|
1994 return FALSE;
|
|
1995 return (STRNICMP(name, "xterm", 5) == 0
|
|
1996 || STRNICMP(name, "nxterm", 6) == 0
|
|
1997 || STRNICMP(name, "kterm", 5) == 0
|
158
|
1998 || STRNICMP(name, "mlterm", 6) == 0
|
7
|
1999 || STRNICMP(name, "rxvt", 4) == 0
|
|
2000 || STRCMP(name, "builtin_xterm") == 0);
|
|
2001 }
|
|
2002
|
|
2003 #if defined(FEAT_MOUSE_TTY) || defined(PROTO)
|
|
2004 /*
|
|
2005 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
|
|
2006 * Return 1 for "xterm".
|
|
2007 * Return 2 for "xterm2".
|
|
2008 */
|
|
2009 int
|
|
2010 use_xterm_mouse()
|
|
2011 {
|
|
2012 if (ttym_flags == TTYM_XTERM2)
|
|
2013 return 2;
|
|
2014 if (ttym_flags == TTYM_XTERM)
|
|
2015 return 1;
|
|
2016 return 0;
|
|
2017 }
|
|
2018 #endif
|
|
2019
|
|
2020 int
|
|
2021 vim_is_iris(name)
|
|
2022 char_u *name;
|
|
2023 {
|
|
2024 if (name == NULL)
|
|
2025 return FALSE;
|
|
2026 return (STRNICMP(name, "iris-ansi", 9) == 0
|
|
2027 || STRCMP(name, "builtin_iris-ansi") == 0);
|
|
2028 }
|
|
2029
|
|
2030 int
|
|
2031 vim_is_vt300(name)
|
|
2032 char_u *name;
|
|
2033 {
|
|
2034 if (name == NULL)
|
|
2035 return FALSE; /* actually all ANSI comp. terminals should be here */
|
22
|
2036 /* catch VT100 - VT5xx */
|
|
2037 return ((STRNICMP(name, "vt", 2) == 0
|
|
2038 && vim_strchr((char_u *)"12345", name[2]) != NULL)
|
7
|
2039 || STRCMP(name, "builtin_vt320") == 0);
|
|
2040 }
|
|
2041
|
|
2042 /*
|
|
2043 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
|
|
2044 * This should include all windowed terminal emulators.
|
|
2045 */
|
|
2046 int
|
|
2047 vim_is_fastterm(name)
|
|
2048 char_u *name;
|
|
2049 {
|
|
2050 if (name == NULL)
|
|
2051 return FALSE;
|
|
2052 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
|
|
2053 return TRUE;
|
|
2054 return ( STRNICMP(name, "hpterm", 6) == 0
|
|
2055 || STRNICMP(name, "sun-cmd", 7) == 0
|
|
2056 || STRNICMP(name, "screen", 6) == 0
|
|
2057 || STRNICMP(name, "dtterm", 6) == 0);
|
|
2058 }
|
|
2059
|
|
2060 /*
|
|
2061 * Insert user name in s[len].
|
|
2062 * Return OK if a name found.
|
|
2063 */
|
|
2064 int
|
|
2065 mch_get_user_name(s, len)
|
|
2066 char_u *s;
|
|
2067 int len;
|
|
2068 {
|
|
2069 #ifdef VMS
|
509
|
2070 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
|
7
|
2071 return OK;
|
|
2072 #else
|
|
2073 return mch_get_uname(getuid(), s, len);
|
|
2074 #endif
|
|
2075 }
|
|
2076
|
|
2077 /*
|
|
2078 * Insert user name for "uid" in s[len].
|
|
2079 * Return OK if a name found.
|
|
2080 */
|
|
2081 int
|
|
2082 mch_get_uname(uid, s, len)
|
|
2083 uid_t uid;
|
|
2084 char_u *s;
|
|
2085 int len;
|
|
2086 {
|
|
2087 #if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
|
|
2088 struct passwd *pw;
|
|
2089
|
|
2090 if ((pw = getpwuid(uid)) != NULL
|
|
2091 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
|
|
2092 {
|
418
|
2093 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
|
7
|
2094 return OK;
|
|
2095 }
|
|
2096 #endif
|
|
2097 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
|
|
2098 return FAIL; /* a number is not a name */
|
|
2099 }
|
|
2100
|
|
2101 /*
|
|
2102 * Insert host name is s[len].
|
|
2103 */
|
|
2104
|
|
2105 #ifdef HAVE_SYS_UTSNAME_H
|
|
2106 void
|
|
2107 mch_get_host_name(s, len)
|
|
2108 char_u *s;
|
|
2109 int len;
|
|
2110 {
|
|
2111 struct utsname vutsname;
|
|
2112
|
|
2113 if (uname(&vutsname) < 0)
|
|
2114 *s = NUL;
|
|
2115 else
|
418
|
2116 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
|
7
|
2117 }
|
|
2118 #else /* HAVE_SYS_UTSNAME_H */
|
|
2119
|
|
2120 # ifdef HAVE_SYS_SYSTEMINFO_H
|
|
2121 # define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
|
|
2122 # endif
|
|
2123
|
|
2124 void
|
|
2125 mch_get_host_name(s, len)
|
|
2126 char_u *s;
|
|
2127 int len;
|
|
2128 {
|
|
2129 # ifdef VAXC
|
|
2130 vaxc$gethostname((char *)s, len);
|
|
2131 # else
|
|
2132 gethostname((char *)s, len);
|
|
2133 # endif
|
|
2134 s[len - 1] = NUL; /* make sure it's terminated */
|
|
2135 }
|
|
2136 #endif /* HAVE_SYS_UTSNAME_H */
|
|
2137
|
|
2138 /*
|
|
2139 * return process ID
|
|
2140 */
|
|
2141 long
|
|
2142 mch_get_pid()
|
|
2143 {
|
|
2144 return (long)getpid();
|
|
2145 }
|
|
2146
|
|
2147 #if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
|
|
2148 static char *strerror __ARGS((int));
|
|
2149
|
|
2150 static char *
|
|
2151 strerror(err)
|
|
2152 int err;
|
|
2153 {
|
|
2154 extern int sys_nerr;
|
|
2155 extern char *sys_errlist[];
|
|
2156 static char er[20];
|
|
2157
|
|
2158 if (err > 0 && err < sys_nerr)
|
|
2159 return (sys_errlist[err]);
|
|
2160 sprintf(er, "Error %d", err);
|
|
2161 return er;
|
|
2162 }
|
|
2163 #endif
|
|
2164
|
|
2165 /*
|
|
2166 * Get name of current directory into buffer 'buf' of length 'len' bytes.
|
|
2167 * Return OK for success, FAIL for failure.
|
|
2168 */
|
|
2169 int
|
|
2170 mch_dirname(buf, len)
|
|
2171 char_u *buf;
|
|
2172 int len;
|
|
2173 {
|
|
2174 #if defined(USE_GETCWD)
|
|
2175 if (getcwd((char *)buf, len) == NULL)
|
|
2176 {
|
|
2177 STRCPY(buf, strerror(errno));
|
|
2178 return FAIL;
|
|
2179 }
|
|
2180 return OK;
|
|
2181 #else
|
|
2182 return (getwd((char *)buf) != NULL ? OK : FAIL);
|
|
2183 #endif
|
|
2184 }
|
|
2185
|
|
2186 #if defined(OS2) || defined(PROTO)
|
|
2187 /*
|
|
2188 * Replace all slashes by backslashes.
|
|
2189 * When 'shellslash' set do it the other way around.
|
|
2190 */
|
|
2191 void
|
|
2192 slash_adjust(p)
|
|
2193 char_u *p;
|
|
2194 {
|
|
2195 while (*p)
|
|
2196 {
|
|
2197 if (*p == psepcN)
|
|
2198 *p = psepc;
|
39
|
2199 mb_ptr_adv(p);
|
7
|
2200 }
|
|
2201 }
|
|
2202 #endif
|
|
2203
|
|
2204 /*
|
|
2205 * Get absolute file name into buffer 'buf' of length 'len' bytes.
|
|
2206 *
|
|
2207 * return FAIL for failure, OK for success
|
|
2208 */
|
|
2209 int
|
|
2210 mch_FullName(fname, buf, len, force)
|
|
2211 char_u *fname, *buf;
|
|
2212 int len;
|
|
2213 int force; /* also expand when already absolute path */
|
|
2214 {
|
|
2215 int l;
|
|
2216 #ifdef OS2
|
|
2217 int only_drive; /* file name is only a drive letter */
|
|
2218 #endif
|
|
2219 #ifdef HAVE_FCHDIR
|
|
2220 int fd = -1;
|
|
2221 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
|
|
2222 #endif
|
|
2223 char_u olddir[MAXPATHL];
|
|
2224 char_u *p;
|
|
2225 int retval = OK;
|
|
2226
|
|
2227 #ifdef VMS
|
|
2228 fname = vms_fixfilename(fname);
|
|
2229 #endif
|
|
2230
|
|
2231 /* expand it if forced or not an absolute path */
|
|
2232 if (force || !mch_isFullName(fname))
|
|
2233 {
|
|
2234 /*
|
|
2235 * If the file name has a path, change to that directory for a moment,
|
|
2236 * and then do the getwd() (and get back to where we were).
|
|
2237 * This will get the correct path name with "../" things.
|
|
2238 */
|
|
2239 #ifdef OS2
|
|
2240 only_drive = 0;
|
|
2241 if (((p = vim_strrchr(fname, '/')) != NULL)
|
|
2242 || ((p = vim_strrchr(fname, '\\')) != NULL)
|
|
2243 || (((p = vim_strchr(fname, ':')) != NULL) && ++only_drive))
|
|
2244 #else
|
|
2245 if ((p = vim_strrchr(fname, '/')) != NULL)
|
|
2246 #endif
|
|
2247 {
|
|
2248 #ifdef HAVE_FCHDIR
|
|
2249 /*
|
|
2250 * Use fchdir() if possible, it's said to be faster and more
|
|
2251 * reliable. But on SunOS 4 it might not work. Check this by
|
|
2252 * doing a fchdir() right now.
|
|
2253 */
|
|
2254 if (!dont_fchdir)
|
|
2255 {
|
|
2256 fd = open(".", O_RDONLY | O_EXTRA, 0);
|
|
2257 if (fd >= 0 && fchdir(fd) < 0)
|
|
2258 {
|
|
2259 close(fd);
|
|
2260 fd = -1;
|
|
2261 dont_fchdir = TRUE; /* don't try again */
|
|
2262 }
|
|
2263 }
|
|
2264 #endif
|
|
2265
|
|
2266 /* Only change directory when we are sure we can return to where
|
|
2267 * we are now. After doing "su" chdir(".") might not work. */
|
|
2268 if (
|
|
2269 #ifdef HAVE_FCHDIR
|
|
2270 fd < 0 &&
|
|
2271 #endif
|
|
2272 (mch_dirname(olddir, MAXPATHL) == FAIL
|
|
2273 || mch_chdir((char *)olddir) != 0))
|
|
2274 {
|
|
2275 p = NULL; /* can't get current dir: don't chdir */
|
|
2276 retval = FAIL;
|
|
2277 }
|
|
2278 else
|
|
2279 {
|
|
2280 #ifdef OS2
|
|
2281 /*
|
|
2282 * compensate for case where ':' from "D:" was the only
|
|
2283 * path separator detected in the file name; the _next_
|
|
2284 * character has to be removed, and then restored later.
|
|
2285 */
|
|
2286 if (only_drive)
|
|
2287 p++;
|
|
2288 #endif
|
|
2289 /* The directory is copied into buf[], to be able to remove
|
|
2290 * the file name without changing it (could be a string in
|
|
2291 * read-only memory) */
|
|
2292 if (p - fname >= len)
|
|
2293 retval = FAIL;
|
|
2294 else
|
|
2295 {
|
418
|
2296 vim_strncpy(buf, fname, p - fname);
|
7
|
2297 if (mch_chdir((char *)buf))
|
|
2298 retval = FAIL;
|
|
2299 else
|
|
2300 fname = p + 1;
|
|
2301 *buf = NUL;
|
|
2302 }
|
|
2303 #ifdef OS2
|
|
2304 if (only_drive)
|
|
2305 {
|
|
2306 p--;
|
|
2307 if (retval != FAIL)
|
|
2308 fname--;
|
|
2309 }
|
|
2310 #endif
|
|
2311 }
|
|
2312 }
|
|
2313 if (mch_dirname(buf, len) == FAIL)
|
|
2314 {
|
|
2315 retval = FAIL;
|
|
2316 *buf = NUL;
|
|
2317 }
|
|
2318 if (p != NULL)
|
|
2319 {
|
|
2320 #ifdef HAVE_FCHDIR
|
|
2321 if (fd >= 0)
|
|
2322 {
|
|
2323 l = fchdir(fd);
|
|
2324 close(fd);
|
|
2325 }
|
|
2326 else
|
|
2327 #endif
|
|
2328 l = mch_chdir((char *)olddir);
|
|
2329 if (l != 0)
|
|
2330 EMSG(_(e_prev_dir));
|
|
2331 }
|
|
2332
|
|
2333 l = STRLEN(buf);
|
|
2334 if (l >= len)
|
|
2335 retval = FAIL;
|
|
2336 #ifndef VMS
|
|
2337 else
|
|
2338 {
|
|
2339 if (l > 0 && buf[l - 1] != '/' && *fname != NUL
|
|
2340 && STRCMP(fname, ".") != 0)
|
|
2341 STRCAT(buf, "/");
|
|
2342 }
|
|
2343 #endif
|
|
2344 }
|
|
2345 /* Catch file names which are too long. */
|
|
2346 if (retval == FAIL || STRLEN(buf) + STRLEN(fname) >= len)
|
|
2347 return FAIL;
|
|
2348
|
|
2349 /* Do not append ".", "/dir/." is equal to "/dir". */
|
|
2350 if (STRCMP(fname, ".") != 0)
|
|
2351 STRCAT(buf, fname);
|
|
2352
|
|
2353 return OK;
|
|
2354 }
|
|
2355
|
|
2356 /*
|
|
2357 * Return TRUE if "fname" does not depend on the current directory.
|
|
2358 */
|
|
2359 int
|
|
2360 mch_isFullName(fname)
|
|
2361 char_u *fname;
|
|
2362 {
|
|
2363 #ifdef __EMX__
|
|
2364 return _fnisabs(fname);
|
|
2365 #else
|
|
2366 # ifdef VMS
|
|
2367 return ( fname[0] == '/' || fname[0] == '.' ||
|
|
2368 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
|
|
2369 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
|
|
2370 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
|
|
2371 # else
|
|
2372 return (*fname == '/' || *fname == '~');
|
|
2373 # endif
|
|
2374 #endif
|
|
2375 }
|
|
2376
|
585
|
2377 #if defined(USE_FNAME_CASE) || defined(PROTO)
|
|
2378 /*
|
|
2379 * Set the case of the file name, if it already exists. This will cause the
|
|
2380 * file name to remain exactly the same.
|
|
2381 * Only required for file systems where case is ingored and preserved.
|
|
2382 */
|
|
2383 /*ARGSUSED*/
|
|
2384 void
|
|
2385 fname_case(name, len)
|
|
2386 char_u *name;
|
|
2387 int len; /* buffer size, only used when name gets longer */
|
|
2388 {
|
|
2389 struct stat st;
|
|
2390 char_u *slash, *tail;
|
|
2391 DIR *dirp;
|
|
2392 struct dirent *dp;
|
|
2393
|
|
2394 if (lstat((char *)name, &st) >= 0)
|
|
2395 {
|
|
2396 /* Open the directory where the file is located. */
|
|
2397 slash = vim_strrchr(name, '/');
|
|
2398 if (slash == NULL)
|
|
2399 {
|
|
2400 dirp = opendir(".");
|
|
2401 tail = name;
|
|
2402 }
|
|
2403 else
|
|
2404 {
|
|
2405 *slash = NUL;
|
|
2406 dirp = opendir((char *)name);
|
|
2407 *slash = '/';
|
|
2408 tail = slash + 1;
|
|
2409 }
|
|
2410
|
|
2411 if (dirp != NULL)
|
|
2412 {
|
|
2413 while ((dp = readdir(dirp)) != NULL)
|
|
2414 {
|
|
2415 /* Only accept names that differ in case and are the same byte
|
|
2416 * length. TODO: accept different length name. */
|
|
2417 if (STRICMP(tail, dp->d_name) == 0
|
|
2418 && STRLEN(tail) == STRLEN(dp->d_name))
|
|
2419 {
|
|
2420 char_u newname[MAXPATHL + 1];
|
|
2421 struct stat st2;
|
|
2422
|
|
2423 /* Verify the inode is equal. */
|
|
2424 vim_strncpy(newname, name, MAXPATHL);
|
|
2425 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
|
|
2426 MAXPATHL - (tail - name));
|
|
2427 if (lstat((char *)newname, &st2) >= 0
|
|
2428 && st.st_ino == st2.st_ino
|
|
2429 && st.st_dev == st2.st_dev)
|
|
2430 {
|
|
2431 STRCPY(tail, dp->d_name);
|
|
2432 break;
|
|
2433 }
|
|
2434 }
|
|
2435 }
|
|
2436
|
|
2437 closedir(dirp);
|
|
2438 }
|
|
2439 }
|
|
2440 }
|
|
2441 #endif
|
|
2442
|
7
|
2443 /*
|
|
2444 * Get file permissions for 'name'.
|
|
2445 * Returns -1 when it doesn't exist.
|
|
2446 */
|
|
2447 long
|
|
2448 mch_getperm(name)
|
|
2449 char_u *name;
|
|
2450 {
|
|
2451 struct stat statb;
|
|
2452
|
|
2453 /* Keep the #ifdef outside of stat(), it may be a macro. */
|
|
2454 #ifdef VMS
|
|
2455 if (stat((char *)vms_fixfilename(name), &statb))
|
|
2456 #else
|
|
2457 if (stat((char *)name, &statb))
|
|
2458 #endif
|
|
2459 return -1;
|
|
2460 return statb.st_mode;
|
|
2461 }
|
|
2462
|
|
2463 /*
|
|
2464 * set file permission for 'name' to 'perm'
|
|
2465 *
|
|
2466 * return FAIL for failure, OK otherwise
|
|
2467 */
|
|
2468 int
|
|
2469 mch_setperm(name, perm)
|
|
2470 char_u *name;
|
|
2471 long perm;
|
|
2472 {
|
|
2473 return (chmod((char *)
|
|
2474 #ifdef VMS
|
|
2475 vms_fixfilename(name),
|
|
2476 #else
|
|
2477 name,
|
|
2478 #endif
|
|
2479 (mode_t)perm) == 0 ? OK : FAIL);
|
|
2480 }
|
|
2481
|
|
2482 #if defined(HAVE_ACL) || defined(PROTO)
|
|
2483 # ifdef HAVE_SYS_ACL_H
|
|
2484 # include <sys/acl.h>
|
|
2485 # endif
|
|
2486 # ifdef HAVE_SYS_ACCESS_H
|
|
2487 # include <sys/access.h>
|
|
2488 # endif
|
|
2489
|
|
2490 # ifdef HAVE_SOLARIS_ACL
|
|
2491 typedef struct vim_acl_solaris_T {
|
|
2492 int acl_cnt;
|
|
2493 aclent_t *acl_entry;
|
|
2494 } vim_acl_solaris_T;
|
|
2495 # endif
|
|
2496
|
|
2497 /*
|
|
2498 * Return a pointer to the ACL of file "fname" in allocated memory.
|
|
2499 * Return NULL if the ACL is not available for whatever reason.
|
|
2500 */
|
|
2501 vim_acl_T
|
|
2502 mch_get_acl(fname)
|
|
2503 char_u *fname;
|
|
2504 {
|
|
2505 vim_acl_T ret = NULL;
|
|
2506 #ifdef HAVE_POSIX_ACL
|
|
2507 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
|
|
2508 #else
|
|
2509 #ifdef HAVE_SOLARIS_ACL
|
|
2510 vim_acl_solaris_T *aclent;
|
|
2511
|
|
2512 aclent = malloc(sizeof(vim_acl_solaris_T));
|
|
2513 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
|
|
2514 {
|
|
2515 free(aclent);
|
|
2516 return NULL;
|
|
2517 }
|
|
2518 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
|
|
2519 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
|
|
2520 {
|
|
2521 free(aclent->acl_entry);
|
|
2522 free(aclent);
|
|
2523 return NULL;
|
|
2524 }
|
|
2525 ret = (vim_acl_T)aclent;
|
|
2526 #else
|
|
2527 #if defined(HAVE_AIX_ACL)
|
|
2528 int aclsize;
|
|
2529 struct acl *aclent;
|
|
2530
|
|
2531 aclsize = sizeof(struct acl);
|
|
2532 aclent = malloc(aclsize);
|
|
2533 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
|
|
2534 {
|
|
2535 if (errno == ENOSPC)
|
|
2536 {
|
|
2537 aclsize = aclent->acl_len;
|
|
2538 aclent = realloc(aclent, aclsize);
|
|
2539 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
|
|
2540 {
|
|
2541 free(aclent);
|
|
2542 return NULL;
|
|
2543 }
|
|
2544 }
|
|
2545 else
|
|
2546 {
|
|
2547 free(aclent);
|
|
2548 return NULL;
|
|
2549 }
|
|
2550 }
|
|
2551 ret = (vim_acl_T)aclent;
|
|
2552 #endif /* HAVE_AIX_ACL */
|
|
2553 #endif /* HAVE_SOLARIS_ACL */
|
|
2554 #endif /* HAVE_POSIX_ACL */
|
|
2555 return ret;
|
|
2556 }
|
|
2557
|
|
2558 /*
|
|
2559 * Set the ACL of file "fname" to "acl" (unless it's NULL).
|
|
2560 */
|
|
2561 void
|
|
2562 mch_set_acl(fname, aclent)
|
|
2563 char_u *fname;
|
|
2564 vim_acl_T aclent;
|
|
2565 {
|
|
2566 if (aclent == NULL)
|
|
2567 return;
|
|
2568 #ifdef HAVE_POSIX_ACL
|
|
2569 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
|
|
2570 #else
|
|
2571 #ifdef HAVE_SOLARIS_ACL
|
|
2572 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
|
|
2573 ((vim_acl_solaris_T *)aclent)->acl_entry);
|
|
2574 #else
|
|
2575 #ifdef HAVE_AIX_ACL
|
|
2576 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
|
|
2577 #endif /* HAVE_AIX_ACL */
|
|
2578 #endif /* HAVE_SOLARIS_ACL */
|
|
2579 #endif /* HAVE_POSIX_ACL */
|
|
2580 }
|
|
2581
|
|
2582 void
|
|
2583 mch_free_acl(aclent)
|
|
2584 vim_acl_T aclent;
|
|
2585 {
|
|
2586 if (aclent == NULL)
|
|
2587 return;
|
|
2588 #ifdef HAVE_POSIX_ACL
|
|
2589 acl_free((acl_t)aclent);
|
|
2590 #else
|
|
2591 #ifdef HAVE_SOLARIS_ACL
|
|
2592 free(((vim_acl_solaris_T *)aclent)->acl_entry);
|
|
2593 free(aclent);
|
|
2594 #else
|
|
2595 #ifdef HAVE_AIX_ACL
|
|
2596 free(aclent);
|
|
2597 #endif /* HAVE_AIX_ACL */
|
|
2598 #endif /* HAVE_SOLARIS_ACL */
|
|
2599 #endif /* HAVE_POSIX_ACL */
|
|
2600 }
|
|
2601 #endif
|
|
2602
|
|
2603 /*
|
|
2604 * Set hidden flag for "name".
|
|
2605 */
|
|
2606 /* ARGSUSED */
|
|
2607 void
|
|
2608 mch_hide(name)
|
|
2609 char_u *name;
|
|
2610 {
|
|
2611 /* can't hide a file */
|
|
2612 }
|
|
2613
|
|
2614 /*
|
|
2615 * return TRUE if "name" is a directory
|
|
2616 * return FALSE if "name" is not a directory
|
|
2617 * return FALSE for error
|
|
2618 */
|
|
2619 int
|
|
2620 mch_isdir(name)
|
|
2621 char_u *name;
|
|
2622 {
|
|
2623 struct stat statb;
|
|
2624
|
|
2625 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
|
|
2626 return FALSE;
|
|
2627 if (stat((char *)name, &statb))
|
|
2628 return FALSE;
|
|
2629 #ifdef _POSIX_SOURCE
|
|
2630 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
|
|
2631 #else
|
|
2632 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
|
|
2633 #endif
|
|
2634 }
|
|
2635
|
|
2636 static int executable_file __ARGS((char_u *name));
|
|
2637
|
|
2638 /*
|
|
2639 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
|
|
2640 */
|
|
2641 static int
|
|
2642 executable_file(name)
|
|
2643 char_u *name;
|
|
2644 {
|
|
2645 struct stat st;
|
|
2646
|
|
2647 if (stat((char *)name, &st))
|
|
2648 return 0;
|
|
2649 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
|
|
2650 }
|
|
2651
|
|
2652 /*
|
|
2653 * Return 1 if "name" can be found in $PATH and executed, 0 if not.
|
|
2654 * Return -1 if unknown.
|
|
2655 */
|
|
2656 int
|
|
2657 mch_can_exe(name)
|
|
2658 char_u *name;
|
|
2659 {
|
|
2660 char_u *buf;
|
|
2661 char_u *p, *e;
|
|
2662 int retval;
|
|
2663
|
|
2664 /* If it's an absolute or relative path don't need to use $PATH. */
|
|
2665 if (mch_isFullName(name) || (name[0] == '.' && (name[1] == '/'
|
|
2666 || (name[1] == '.' && name[2] == '/'))))
|
|
2667 return executable_file(name);
|
|
2668
|
|
2669 p = (char_u *)getenv("PATH");
|
|
2670 if (p == NULL || *p == NUL)
|
|
2671 return -1;
|
|
2672 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
|
|
2673 if (buf == NULL)
|
|
2674 return -1;
|
|
2675
|
|
2676 /*
|
|
2677 * Walk through all entries in $PATH to check if "name" exists there and
|
|
2678 * is an executable file.
|
|
2679 */
|
|
2680 for (;;)
|
|
2681 {
|
|
2682 e = (char_u *)strchr((char *)p, ':');
|
|
2683 if (e == NULL)
|
|
2684 e = p + STRLEN(p);
|
|
2685 if (e - p <= 1) /* empty entry means current dir */
|
|
2686 STRCPY(buf, "./");
|
|
2687 else
|
|
2688 {
|
418
|
2689 vim_strncpy(buf, p, e - p);
|
7
|
2690 add_pathsep(buf);
|
|
2691 }
|
|
2692 STRCAT(buf, name);
|
|
2693 retval = executable_file(buf);
|
|
2694 if (retval == 1)
|
|
2695 break;
|
|
2696
|
|
2697 if (*e != ':')
|
|
2698 break;
|
|
2699 p = e + 1;
|
|
2700 }
|
|
2701
|
|
2702 vim_free(buf);
|
|
2703 return retval;
|
|
2704 }
|
|
2705
|
|
2706 /*
|
|
2707 * Check what "name" is:
|
|
2708 * NODE_NORMAL: file or directory (or doesn't exist)
|
|
2709 * NODE_WRITABLE: writable device, socket, fifo, etc.
|
|
2710 * NODE_OTHER: non-writable things
|
|
2711 */
|
|
2712 int
|
|
2713 mch_nodetype(name)
|
|
2714 char_u *name;
|
|
2715 {
|
|
2716 struct stat st;
|
|
2717
|
|
2718 if (stat((char *)name, &st))
|
|
2719 return NODE_NORMAL;
|
|
2720 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
|
|
2721 return NODE_NORMAL;
|
|
2722 #ifndef OS2
|
|
2723 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
|
|
2724 return NODE_OTHER;
|
|
2725 #endif
|
|
2726 /* Everything else is writable? */
|
|
2727 return NODE_WRITABLE;
|
|
2728 }
|
|
2729
|
|
2730 void
|
|
2731 mch_early_init()
|
|
2732 {
|
|
2733 #ifdef HAVE_CHECK_STACK_GROWTH
|
|
2734 int i;
|
180
|
2735
|
7
|
2736 check_stack_growth((char *)&i);
|
|
2737
|
180
|
2738 # ifdef HAVE_STACK_LIMIT
|
7
|
2739 get_stack_limit();
|
|
2740 # endif
|
|
2741
|
|
2742 #endif
|
|
2743
|
|
2744 /*
|
|
2745 * Setup an alternative stack for signals. Helps to catch signals when
|
|
2746 * running out of stack space.
|
|
2747 * Use of sigaltstack() is preferred, it's more portable.
|
|
2748 * Ignore any errors.
|
|
2749 */
|
|
2750 #if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
|
|
2751 signal_stack = malloc(SIGSTKSZ);
|
|
2752 init_signal_stack();
|
|
2753 #endif
|
|
2754 }
|
|
2755
|
355
|
2756 #if defined(EXITFREE) || defined(PROTO)
|
|
2757 void
|
|
2758 mch_free_mem()
|
|
2759 {
|
359
|
2760 # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
|
|
2761 if (clip_star.owned)
|
|
2762 clip_lose_selection(&clip_star);
|
|
2763 if (clip_plus.owned)
|
|
2764 clip_lose_selection(&clip_plus);
|
355
|
2765 # endif
|
|
2766 # if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
|
|
2767 if (xterm_Shell != (Widget)0)
|
|
2768 XtDestroyWidget(xterm_Shell);
|
|
2769 if (xterm_dpy != NULL)
|
|
2770 XtCloseDisplay(xterm_dpy);
|
|
2771 if (app_context != (XtAppContext)NULL)
|
|
2772 XtDestroyApplicationContext(app_context);
|
|
2773 # endif
|
359
|
2774 # ifdef FEAT_X11
|
|
2775 if (x11_display != NULL && x11_display != xterm_dpy)
|
|
2776 XCloseDisplay(x11_display);
|
|
2777 # endif
|
|
2778 # if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
|
|
2779 vim_free(signal_stack);
|
|
2780 signal_stack = NULL;
|
|
2781 # endif
|
|
2782 # ifdef FEAT_TITLE
|
|
2783 vim_free(oldtitle);
|
|
2784 vim_free(oldicon);
|
|
2785 # endif
|
355
|
2786 }
|
|
2787 #endif
|
|
2788
|
7
|
2789 static void exit_scroll __ARGS((void));
|
|
2790
|
|
2791 /*
|
|
2792 * Output a newline when exiting.
|
|
2793 * Make sure the newline goes to the same stream as the text.
|
|
2794 */
|
|
2795 static void
|
|
2796 exit_scroll()
|
|
2797 {
|
167
|
2798 if (silent_mode)
|
|
2799 return;
|
7
|
2800 if (newline_on_exit || msg_didout)
|
|
2801 {
|
|
2802 if (msg_use_printf())
|
|
2803 {
|
|
2804 if (info_message)
|
|
2805 mch_msg("\n");
|
|
2806 else
|
|
2807 mch_errmsg("\r\n");
|
|
2808 }
|
|
2809 else
|
|
2810 out_char('\n');
|
|
2811 }
|
|
2812 else
|
|
2813 {
|
|
2814 restore_cterm_colors(); /* get original colors back */
|
|
2815 msg_clr_eos_force(); /* clear the rest of the display */
|
|
2816 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
|
|
2817 }
|
|
2818 }
|
|
2819
|
|
2820 void
|
|
2821 mch_exit(r)
|
|
2822 int r;
|
|
2823 {
|
|
2824 exiting = TRUE;
|
|
2825
|
|
2826 #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
|
|
2827 x11_export_final_selection();
|
|
2828 #endif
|
|
2829
|
|
2830 #ifdef FEAT_GUI
|
|
2831 if (!gui.in_use)
|
|
2832 #endif
|
|
2833 {
|
|
2834 settmode(TMODE_COOK);
|
|
2835 #ifdef FEAT_TITLE
|
|
2836 mch_restore_title(3); /* restore xterm title and icon name */
|
|
2837 #endif
|
|
2838 /*
|
|
2839 * When t_ti is not empty but it doesn't cause swapping terminal
|
|
2840 * pages, need to output a newline when msg_didout is set. But when
|
|
2841 * t_ti does swap pages it should not go to the shell page. Do this
|
|
2842 * before stoptermcap().
|
|
2843 */
|
|
2844 if (swapping_screen() && !newline_on_exit)
|
|
2845 exit_scroll();
|
|
2846
|
|
2847 /* Stop termcap: May need to check for T_CRV response, which
|
|
2848 * requires RAW mode. */
|
|
2849 stoptermcap();
|
|
2850
|
|
2851 /*
|
|
2852 * A newline is only required after a message in the alternate screen.
|
|
2853 * This is set to TRUE by wait_return().
|
|
2854 */
|
|
2855 if (!swapping_screen() || newline_on_exit)
|
|
2856 exit_scroll();
|
|
2857
|
|
2858 /* Cursor may have been switched off without calling starttermcap()
|
|
2859 * when doing "vim -u vimrc" and vimrc contains ":q". */
|
|
2860 if (full_screen)
|
|
2861 cursor_on();
|
|
2862 }
|
|
2863 out_flush();
|
|
2864 ml_close_all(TRUE); /* remove all memfiles */
|
|
2865 may_core_dump();
|
|
2866 #ifdef FEAT_GUI
|
|
2867 if (gui.in_use)
|
|
2868 gui_exit(r);
|
|
2869 #endif
|
167
|
2870
|
765
|
2871 #ifdef MACOS_CONVERT
|
167
|
2872 mac_conv_cleanup();
|
|
2873 #endif
|
|
2874
|
7
|
2875 #ifdef __QNX__
|
|
2876 /* A core dump won't be created if the signal handler
|
|
2877 * doesn't return, so we can't call exit() */
|
|
2878 if (deadly_signal != 0)
|
|
2879 return;
|
|
2880 #endif
|
|
2881
|
33
|
2882 #ifdef FEAT_NETBEANS_INTG
|
|
2883 if (usingNetbeans)
|
|
2884 netbeans_send_disconnect();
|
|
2885 #endif
|
355
|
2886
|
|
2887 #ifdef EXITFREE
|
|
2888 free_all_mem();
|
|
2889 #endif
|
|
2890
|
7
|
2891 exit(r);
|
|
2892 }
|
|
2893
|
|
2894 static void
|
|
2895 may_core_dump()
|
|
2896 {
|
|
2897 if (deadly_signal != 0)
|
|
2898 {
|
|
2899 signal(deadly_signal, SIG_DFL);
|
|
2900 kill(getpid(), deadly_signal); /* Die using the signal we caught */
|
|
2901 }
|
|
2902 }
|
|
2903
|
|
2904 #ifndef VMS
|
|
2905
|
|
2906 void
|
|
2907 mch_settmode(tmode)
|
|
2908 int tmode;
|
|
2909 {
|
|
2910 static int first = TRUE;
|
|
2911
|
|
2912 /* Why is NeXT excluded here (and not in os_unixx.h)? */
|
|
2913 #if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
|
|
2914 /*
|
|
2915 * for "new" tty systems
|
|
2916 */
|
|
2917 # ifdef HAVE_TERMIOS_H
|
|
2918 static struct termios told;
|
|
2919 struct termios tnew;
|
|
2920 # else
|
|
2921 static struct termio told;
|
|
2922 struct termio tnew;
|
|
2923 # endif
|
|
2924
|
|
2925 if (first)
|
|
2926 {
|
|
2927 first = FALSE;
|
|
2928 # if defined(HAVE_TERMIOS_H)
|
|
2929 tcgetattr(read_cmd_fd, &told);
|
|
2930 # else
|
|
2931 ioctl(read_cmd_fd, TCGETA, &told);
|
|
2932 # endif
|
|
2933 }
|
|
2934
|
|
2935 tnew = told;
|
|
2936 if (tmode == TMODE_RAW)
|
|
2937 {
|
|
2938 /*
|
|
2939 * ~ICRNL enables typing ^V^M
|
|
2940 */
|
|
2941 tnew.c_iflag &= ~ICRNL;
|
|
2942 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
|
|
2943 # if defined(IEXTEN) && !defined(__MINT__)
|
|
2944 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
|
|
2945 /* but it breaks function keys on MINT */
|
|
2946 # endif
|
|
2947 );
|
|
2948 # ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
|
|
2949 tnew.c_oflag &= ~ONLCR;
|
|
2950 # endif
|
|
2951 tnew.c_cc[VMIN] = 1; /* return after 1 char */
|
|
2952 tnew.c_cc[VTIME] = 0; /* don't wait */
|
|
2953 }
|
|
2954 else if (tmode == TMODE_SLEEP)
|
|
2955 tnew.c_lflag &= ~(ECHO);
|
|
2956
|
|
2957 # if defined(HAVE_TERMIOS_H)
|
|
2958 {
|
|
2959 int n = 10;
|
|
2960
|
|
2961 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
|
|
2962 * few times. */
|
|
2963 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
|
|
2964 && errno == EINTR && n > 0)
|
|
2965 --n;
|
|
2966 }
|
|
2967 # else
|
|
2968 ioctl(read_cmd_fd, TCSETA, &tnew);
|
|
2969 # endif
|
|
2970
|
|
2971 #else
|
|
2972
|
|
2973 /*
|
|
2974 * for "old" tty systems
|
|
2975 */
|
|
2976 # ifndef TIOCSETN
|
|
2977 # define TIOCSETN TIOCSETP /* for hpux 9.0 */
|
|
2978 # endif
|
|
2979 static struct sgttyb ttybold;
|
|
2980 struct sgttyb ttybnew;
|
|
2981
|
|
2982 if (first)
|
|
2983 {
|
|
2984 first = FALSE;
|
|
2985 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
|
|
2986 }
|
|
2987
|
|
2988 ttybnew = ttybold;
|
|
2989 if (tmode == TMODE_RAW)
|
|
2990 {
|
|
2991 ttybnew.sg_flags &= ~(CRMOD | ECHO);
|
|
2992 ttybnew.sg_flags |= RAW;
|
|
2993 }
|
|
2994 else if (tmode == TMODE_SLEEP)
|
|
2995 ttybnew.sg_flags &= ~(ECHO);
|
|
2996 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
|
|
2997 #endif
|
|
2998 curr_tmode = tmode;
|
|
2999 }
|
|
3000
|
|
3001 /*
|
|
3002 * Try to get the code for "t_kb" from the stty setting
|
|
3003 *
|
|
3004 * Even if termcap claims a backspace key, the user's setting *should*
|
|
3005 * prevail. stty knows more about reality than termcap does, and if
|
|
3006 * somebody's usual erase key is DEL (which, for most BSD users, it will
|
|
3007 * be), they're going to get really annoyed if their erase key starts
|
|
3008 * doing forward deletes for no reason. (Eric Fischer)
|
|
3009 */
|
|
3010 void
|
|
3011 get_stty()
|
|
3012 {
|
|
3013 char_u buf[2];
|
|
3014 char_u *p;
|
|
3015
|
|
3016 /* Why is NeXT excluded here (and not in os_unixx.h)? */
|
|
3017 #if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
|
|
3018 /* for "new" tty systems */
|
|
3019 # ifdef HAVE_TERMIOS_H
|
|
3020 struct termios keys;
|
|
3021 # else
|
|
3022 struct termio keys;
|
|
3023 # endif
|
|
3024
|
|
3025 # if defined(HAVE_TERMIOS_H)
|
|
3026 if (tcgetattr(read_cmd_fd, &keys) != -1)
|
|
3027 # else
|
|
3028 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1)
|
|
3029 # endif
|
|
3030 {
|
|
3031 buf[0] = keys.c_cc[VERASE];
|
|
3032 intr_char = keys.c_cc[VINTR];
|
|
3033 #else
|
|
3034 /* for "old" tty systems */
|
|
3035 struct sgttyb keys;
|
|
3036
|
|
3037 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1)
|
|
3038 {
|
|
3039 buf[0] = keys.sg_erase;
|
|
3040 intr_char = keys.sg_kill;
|
|
3041 #endif
|
|
3042 buf[1] = NUL;
|
|
3043 add_termcode((char_u *)"kb", buf, FALSE);
|
|
3044
|
|
3045 /*
|
|
3046 * If <BS> and <DEL> are now the same, redefine <DEL>.
|
|
3047 */
|
|
3048 p = find_termcode((char_u *)"kD");
|
|
3049 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
|
|
3050 do_fixdel(NULL);
|
|
3051 }
|
|
3052 #if 0
|
|
3053 } /* to keep cindent happy */
|
|
3054 #endif
|
|
3055 }
|
|
3056
|
|
3057 #endif /* VMS */
|
|
3058
|
|
3059 #if defined(FEAT_MOUSE_TTY) || defined(PROTO)
|
|
3060 /*
|
|
3061 * Set mouse clicks on or off.
|
|
3062 */
|
|
3063 void
|
|
3064 mch_setmouse(on)
|
|
3065 int on;
|
|
3066 {
|
|
3067 static int ison = FALSE;
|
|
3068 int xterm_mouse_vers;
|
|
3069
|
|
3070 if (on == ison) /* return quickly if nothing to do */
|
|
3071 return;
|
|
3072
|
|
3073 xterm_mouse_vers = use_xterm_mouse();
|
|
3074 if (xterm_mouse_vers > 0)
|
|
3075 {
|
|
3076 if (on) /* enable mouse events, use mouse tracking if available */
|
|
3077 out_str_nf((char_u *)
|
|
3078 (xterm_mouse_vers > 1
|
|
3079 ? IF_EB("\033[?1002h", ESC_STR "[?1002h")
|
|
3080 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
|
|
3081 else /* disable mouse events, could probably always send the same */
|
|
3082 out_str_nf((char_u *)
|
|
3083 (xterm_mouse_vers > 1
|
|
3084 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
|
|
3085 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
|
|
3086 ison = on;
|
|
3087 }
|
|
3088
|
|
3089 # ifdef FEAT_MOUSE_DEC
|
|
3090 else if (ttym_flags == TTYM_DEC)
|
|
3091 {
|
|
3092 if (on) /* enable mouse events */
|
|
3093 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
|
|
3094 else /* disable mouse events */
|
|
3095 out_str_nf((char_u *)"\033['z");
|
|
3096 ison = on;
|
|
3097 }
|
|
3098 # endif
|
|
3099
|
|
3100 # ifdef FEAT_MOUSE_GPM
|
|
3101 else
|
|
3102 {
|
|
3103 if (on)
|
|
3104 {
|
|
3105 if (gpm_open())
|
|
3106 ison = TRUE;
|
|
3107 }
|
|
3108 else
|
|
3109 {
|
|
3110 gpm_close();
|
|
3111 ison = FALSE;
|
|
3112 }
|
|
3113 }
|
|
3114 # endif
|
|
3115
|
|
3116 # ifdef FEAT_MOUSE_JSB
|
|
3117 else
|
|
3118 {
|
|
3119 if (on)
|
|
3120 {
|
|
3121 /* D - Enable Mouse up/down messages
|
|
3122 * L - Enable Left Button Reporting
|
|
3123 * M - Enable Middle Button Reporting
|
|
3124 * R - Enable Right Button Reporting
|
|
3125 * K - Enable SHIFT and CTRL key Reporting
|
|
3126 * + - Enable Advanced messaging of mouse moves and up/down messages
|
|
3127 * Q - Quiet No Ack
|
|
3128 * # - Numeric value of mouse pointer required
|
|
3129 * 0 = Multiview 2000 cursor, used as standard
|
|
3130 * 1 = Windows Arrow
|
|
3131 * 2 = Windows I Beam
|
|
3132 * 3 = Windows Hour Glass
|
|
3133 * 4 = Windows Cross Hair
|
|
3134 * 5 = Windows UP Arrow
|
|
3135 */
|
|
3136 #ifdef JSBTERM_MOUSE_NONADVANCED /* Disables full feedback of pointer movements */
|
|
3137 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
|
|
3138 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
|
|
3139 #else
|
|
3140 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
|
|
3141 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
|
|
3142 #endif
|
|
3143 ison = TRUE;
|
|
3144 }
|
|
3145 else
|
|
3146 {
|
|
3147 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
|
|
3148 ESC_STR "[0~ZwQ" ESC_STR "\\"));
|
|
3149 ison = FALSE;
|
|
3150 }
|
|
3151 }
|
|
3152 # endif
|
|
3153 # ifdef FEAT_MOUSE_PTERM
|
|
3154 else
|
|
3155 {
|
|
3156 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
|
|
3157 if (on)
|
|
3158 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
|
|
3159 else
|
|
3160 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
|
|
3161 ison = on;
|
|
3162 }
|
|
3163 # endif
|
|
3164 }
|
|
3165
|
|
3166 /*
|
|
3167 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
|
|
3168 */
|
|
3169 void
|
|
3170 check_mouse_termcode()
|
|
3171 {
|
|
3172 # ifdef FEAT_MOUSE_XTERM
|
|
3173 if (use_xterm_mouse()
|
|
3174 # ifdef FEAT_GUI
|
|
3175 && !gui.in_use
|
|
3176 # endif
|
|
3177 )
|
|
3178 {
|
|
3179 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
|
180
|
3180 ? IF_EB("\233M", CSI_STR "M")
|
|
3181 : IF_EB("\033[M", ESC_STR "[M")));
|
7
|
3182 if (*p_mouse != NUL)
|
|
3183 {
|
|
3184 /* force mouse off and maybe on to send possibly new mouse
|
|
3185 * activation sequence to the xterm, with(out) drag tracing. */
|
|
3186 mch_setmouse(FALSE);
|
|
3187 setmouse();
|
|
3188 }
|
|
3189 }
|
|
3190 else
|
|
3191 del_mouse_termcode(KS_MOUSE);
|
|
3192 # endif
|
|
3193
|
|
3194 # ifdef FEAT_MOUSE_GPM
|
|
3195 if (!use_xterm_mouse()
|
|
3196 # ifdef FEAT_GUI
|
|
3197 && !gui.in_use
|
|
3198 # endif
|
|
3199 )
|
|
3200 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
|
|
3201 # endif
|
|
3202
|
|
3203 # ifdef FEAT_MOUSE_JSB
|
|
3204 /* conflicts with xterm mouse: "\033[" and "\033[M" ??? */
|
|
3205 if (!use_xterm_mouse()
|
|
3206 # ifdef FEAT_GUI
|
|
3207 && !gui.in_use
|
|
3208 # endif
|
|
3209 )
|
|
3210 set_mouse_termcode(KS_JSBTERM_MOUSE,
|
|
3211 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
|
|
3212 else
|
|
3213 del_mouse_termcode(KS_JSBTERM_MOUSE);
|
|
3214 # endif
|
|
3215
|
|
3216 # ifdef FEAT_MOUSE_NET
|
180
|
3217 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
|
7
|
3218 * define it in the GUI or when using an xterm. */
|
|
3219 if (!use_xterm_mouse()
|
|
3220 # ifdef FEAT_GUI
|
|
3221 && !gui.in_use
|
|
3222 # endif
|
|
3223 )
|
|
3224 set_mouse_termcode(KS_NETTERM_MOUSE,
|
|
3225 (char_u *)IF_EB("\033}", ESC_STR "}"));
|
|
3226 else
|
|
3227 del_mouse_termcode(KS_NETTERM_MOUSE);
|
|
3228 # endif
|
|
3229
|
|
3230 # ifdef FEAT_MOUSE_DEC
|
|
3231 /* conflicts with xterm mouse: "\033[" and "\033[M" */
|
|
3232 if (!use_xterm_mouse()
|
|
3233 # ifdef FEAT_GUI
|
|
3234 && !gui.in_use
|
|
3235 # endif
|
|
3236 )
|
180
|
3237 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
|
|
3238 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
|
7
|
3239 else
|
|
3240 del_mouse_termcode(KS_DEC_MOUSE);
|
|
3241 # endif
|
|
3242 # ifdef FEAT_MOUSE_PTERM
|
|
3243 /* same as the dec mouse */
|
|
3244 if (!use_xterm_mouse()
|
|
3245 # ifdef FEAT_GUI
|
|
3246 && !gui.in_use
|
|
3247 # endif
|
|
3248 )
|
|
3249 set_mouse_termcode(KS_PTERM_MOUSE,
|
|
3250 (char_u *) IF_EB("\033[", ESC_STR "["));
|
|
3251 else
|
|
3252 del_mouse_termcode(KS_PTERM_MOUSE);
|
|
3253 # endif
|
|
3254 }
|
|
3255 #endif
|
|
3256
|
|
3257 /*
|
|
3258 * set screen mode, always fails.
|
|
3259 */
|
|
3260 /* ARGSUSED */
|
|
3261 int
|
|
3262 mch_screenmode(arg)
|
|
3263 char_u *arg;
|
|
3264 {
|
|
3265 EMSG(_(e_screenmode));
|
|
3266 return FAIL;
|
|
3267 }
|
|
3268
|
|
3269 #ifndef VMS
|
|
3270
|
|
3271 /*
|
|
3272 * Try to get the current window size:
|
|
3273 * 1. with an ioctl(), most accurate method
|
|
3274 * 2. from the environment variables LINES and COLUMNS
|
|
3275 * 3. from the termcap
|
|
3276 * 4. keep using the old values
|
|
3277 * Return OK when size could be determined, FAIL otherwise.
|
|
3278 */
|
|
3279 int
|
|
3280 mch_get_shellsize()
|
|
3281 {
|
|
3282 long rows = 0;
|
|
3283 long columns = 0;
|
|
3284 char_u *p;
|
|
3285
|
|
3286 /*
|
|
3287 * For OS/2 use _scrsize().
|
|
3288 */
|
|
3289 # ifdef __EMX__
|
|
3290 {
|
|
3291 int s[2];
|
|
3292
|
|
3293 _scrsize(s);
|
|
3294 columns = s[0];
|
|
3295 rows = s[1];
|
|
3296 }
|
|
3297 # endif
|
|
3298
|
|
3299 /*
|
|
3300 * 1. try using an ioctl. It is the most accurate method.
|
|
3301 *
|
|
3302 * Try using TIOCGWINSZ first, some systems that have it also define
|
|
3303 * TIOCGSIZE but don't have a struct ttysize.
|
|
3304 */
|
|
3305 # ifdef TIOCGWINSZ
|
|
3306 {
|
|
3307 struct winsize ws;
|
|
3308 int fd = 1;
|
|
3309
|
|
3310 /* When stdout is not a tty, use stdin for the ioctl(). */
|
|
3311 if (!isatty(fd) && isatty(read_cmd_fd))
|
|
3312 fd = read_cmd_fd;
|
|
3313 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
|
|
3314 {
|
|
3315 columns = ws.ws_col;
|
|
3316 rows = ws.ws_row;
|
|
3317 }
|
|
3318 }
|
|
3319 # else /* TIOCGWINSZ */
|
|
3320 # ifdef TIOCGSIZE
|
|
3321 {
|
|
3322 struct ttysize ts;
|
|
3323 int fd = 1;
|
|
3324
|
|
3325 /* When stdout is not a tty, use stdin for the ioctl(). */
|
|
3326 if (!isatty(fd) && isatty(read_cmd_fd))
|
|
3327 fd = read_cmd_fd;
|
|
3328 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
|
|
3329 {
|
|
3330 columns = ts.ts_cols;
|
|
3331 rows = ts.ts_lines;
|
|
3332 }
|
|
3333 }
|
|
3334 # endif /* TIOCGSIZE */
|
|
3335 # endif /* TIOCGWINSZ */
|
|
3336
|
|
3337 /*
|
|
3338 * 2. get size from environment
|
164
|
3339 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
|
|
3340 * the ioctl() values!
|
7
|
3341 */
|
164
|
3342 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
|
7
|
3343 {
|
|
3344 if ((p = (char_u *)getenv("LINES")))
|
|
3345 rows = atoi((char *)p);
|
|
3346 if ((p = (char_u *)getenv("COLUMNS")))
|
|
3347 columns = atoi((char *)p);
|
|
3348 }
|
|
3349
|
|
3350 #ifdef HAVE_TGETENT
|
|
3351 /*
|
|
3352 * 3. try reading "co" and "li" entries from termcap
|
|
3353 */
|
|
3354 if (columns == 0 || rows == 0)
|
|
3355 getlinecol(&columns, &rows);
|
|
3356 #endif
|
|
3357
|
|
3358 /*
|
|
3359 * 4. If everything fails, use the old values
|
|
3360 */
|
|
3361 if (columns <= 0 || rows <= 0)
|
|
3362 return FAIL;
|
|
3363
|
|
3364 Rows = rows;
|
|
3365 Columns = columns;
|
|
3366 return OK;
|
|
3367 }
|
|
3368
|
|
3369 /*
|
|
3370 * Try to set the window size to Rows and Columns.
|
|
3371 */
|
|
3372 void
|
|
3373 mch_set_shellsize()
|
|
3374 {
|
|
3375 if (*T_CWS)
|
|
3376 {
|
|
3377 /*
|
|
3378 * NOTE: if you get an error here that term_set_winsize() is
|
|
3379 * undefined, check the output of configure. It could probably not
|
|
3380 * find a ncurses, termcap or termlib library.
|
|
3381 */
|
|
3382 term_set_winsize((int)Rows, (int)Columns);
|
|
3383 out_flush();
|
|
3384 screen_start(); /* don't know where cursor is now */
|
|
3385 }
|
|
3386 }
|
|
3387
|
|
3388 #endif /* VMS */
|
|
3389
|
|
3390 /*
|
|
3391 * Rows and/or Columns has changed.
|
|
3392 */
|
|
3393 void
|
|
3394 mch_new_shellsize()
|
|
3395 {
|
|
3396 /* Nothing to do. */
|
|
3397 }
|
|
3398
|
167
|
3399 #ifndef USE_SYSTEM
|
|
3400 static void append_ga_line __ARGS((garray_T *gap));
|
|
3401
|
|
3402 /*
|
|
3403 * Append the text in "gap" below the cursor line and clear "gap".
|
|
3404 */
|
|
3405 static void
|
|
3406 append_ga_line(gap)
|
|
3407 garray_T *gap;
|
|
3408 {
|
|
3409 /* Remove trailing CR. */
|
|
3410 if (gap->ga_len > 0
|
|
3411 && !curbuf->b_p_bin
|
|
3412 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
|
|
3413 --gap->ga_len;
|
|
3414 ga_append(gap, NUL);
|
|
3415 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
|
|
3416 gap->ga_len = 0;
|
|
3417 }
|
|
3418 #endif
|
|
3419
|
7
|
3420 int
|
|
3421 mch_call_shell(cmd, options)
|
|
3422 char_u *cmd;
|
|
3423 int options; /* SHELL_*, see vim.h */
|
|
3424 {
|
|
3425 #ifdef VMS
|
|
3426 char *ifn = NULL;
|
|
3427 char *ofn = NULL;
|
|
3428 #endif
|
|
3429 int tmode = cur_tmode;
|
|
3430 #ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
|
|
3431 int x;
|
|
3432 # ifndef __EMX__
|
|
3433 char_u *newcmd; /* only needed for unix */
|
|
3434 # else
|
|
3435 /*
|
|
3436 * Set the preferred shell in the EMXSHELL environment variable (but
|
|
3437 * only if it is different from what is already in the environment).
|
|
3438 * Emx then takes care of whether to use "/c" or "-c" in an
|
|
3439 * intelligent way. Simply pass the whole thing to emx's system() call.
|
|
3440 * Emx also starts an interactive shell if system() is passed an empty
|
|
3441 * string.
|
|
3442 */
|
|
3443 char_u *p, *old;
|
|
3444
|
|
3445 if (((old = (char_u *)getenv("EMXSHELL")) == NULL) || STRCMP(old, p_sh))
|
|
3446 {
|
|
3447 /* should check HAVE_SETENV, but I know we don't have it. */
|
|
3448 p = alloc(10 + strlen(p_sh));
|
|
3449 if (p)
|
|
3450 {
|
|
3451 sprintf((char *)p, "EMXSHELL=%s", p_sh);
|
|
3452 putenv((char *)p); /* don't free the pointer! */
|
|
3453 }
|
|
3454 }
|
|
3455 # endif
|
|
3456
|
|
3457 out_flush();
|
|
3458
|
|
3459 if (options & SHELL_COOKED)
|
|
3460 settmode(TMODE_COOK); /* set to normal mode */
|
|
3461
|
|
3462 # ifdef __EMX__
|
|
3463 if (cmd == NULL)
|
|
3464 x = system(""); /* this starts an interactive shell in emx */
|
|
3465 else
|
|
3466 x = system((char *)cmd);
|
|
3467 /* system() returns -1 when error occurs in starting shell */
|
|
3468 if (x == -1 && !emsg_silent)
|
|
3469 {
|
|
3470 MSG_PUTS(_("\nCannot execute shell "));
|
|
3471 msg_outtrans(p_sh);
|
|
3472 msg_putchar('\n');
|
|
3473 }
|
|
3474 # else /* not __EMX__ */
|
|
3475 if (cmd == NULL)
|
|
3476 x = system((char *)p_sh);
|
|
3477 else
|
|
3478 {
|
|
3479 # ifdef VMS
|
|
3480 if (ofn = strchr((char *)cmd, '>'))
|
|
3481 *ofn++ = '\0';
|
|
3482 if (ifn = strchr((char *)cmd, '<'))
|
|
3483 {
|
|
3484 char *p;
|
|
3485
|
|
3486 *ifn++ = '\0';
|
|
3487 p = strchr(ifn,' '); /* chop off any trailing spaces */
|
|
3488 if (p)
|
|
3489 *p = '\0';
|
|
3490 }
|
|
3491 if (ofn)
|
|
3492 x = vms_sys((char *)cmd, ofn, ifn);
|
|
3493 else
|
|
3494 x = system((char *)cmd);
|
|
3495 # else
|
|
3496 newcmd = lalloc(STRLEN(p_sh)
|
|
3497 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
|
|
3498 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
|
|
3499 if (newcmd == NULL)
|
|
3500 x = 0;
|
|
3501 else
|
|
3502 {
|
|
3503 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
|
|
3504 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
|
|
3505 (char *)p_shcf,
|
|
3506 (char *)cmd);
|
|
3507 x = system((char *)newcmd);
|
|
3508 vim_free(newcmd);
|
|
3509 }
|
|
3510 # endif
|
|
3511 }
|
|
3512 # ifdef VMS
|
|
3513 x = vms_sys_status(x);
|
|
3514 # endif
|
|
3515 if (emsg_silent)
|
|
3516 ;
|
|
3517 else if (x == 127)
|
|
3518 MSG_PUTS(_("\nCannot execute shell sh\n"));
|
|
3519 # endif /* __EMX__ */
|
|
3520 else if (x && !(options & SHELL_SILENT))
|
|
3521 {
|
|
3522 MSG_PUTS(_("\nshell returned "));
|
|
3523 msg_outnum((long)x);
|
|
3524 msg_putchar('\n');
|
|
3525 }
|
|
3526
|
|
3527 if (tmode == TMODE_RAW)
|
|
3528 settmode(TMODE_RAW); /* set to raw mode */
|
|
3529 # ifdef FEAT_TITLE
|
|
3530 resettitle();
|
|
3531 # endif
|
|
3532 return x;
|
|
3533
|
|
3534 #else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
|
|
3535
|
167
|
3536 # define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
|
|
3537 127, some shells use that already */
|
7
|
3538
|
|
3539 char_u *newcmd = NULL;
|
|
3540 pid_t pid;
|
167
|
3541 pid_t wpid = 0;
|
7
|
3542 pid_t wait_pid = 0;
|
|
3543 # ifdef HAVE_UNION_WAIT
|
|
3544 union wait status;
|
|
3545 # else
|
|
3546 int status = -1;
|
|
3547 # endif
|
|
3548 int retval = -1;
|
|
3549 char **argv = NULL;
|
|
3550 int argc;
|
|
3551 int i;
|
|
3552 char_u *p;
|
|
3553 int inquote;
|
167
|
3554 int pty_master_fd = -1; /* for pty's */
|
7
|
3555 # ifdef FEAT_GUI
|
|
3556 int pty_slave_fd = -1;
|
|
3557 char *tty_name;
|
167
|
3558 # endif
|
602
|
3559 int fd_toshell[2]; /* for pipes */
|
7
|
3560 int fd_fromshell[2];
|
|
3561 int pipe_error = FALSE;
|
167
|
3562 # ifdef HAVE_SETENV
|
7
|
3563 char envbuf[50];
|
167
|
3564 # else
|
7
|
3565 static char envbuf_Rows[20];
|
|
3566 static char envbuf_Columns[20];
|
|
3567 # endif
|
602
|
3568 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
|
7
|
3569
|
|
3570 out_flush();
|
|
3571 if (options & SHELL_COOKED)
|
|
3572 settmode(TMODE_COOK); /* set to normal mode */
|
|
3573
|
|
3574 newcmd = vim_strsave(p_sh);
|
|
3575 if (newcmd == NULL) /* out of memory */
|
|
3576 goto error;
|
602
|
3577
|
|
3578 /*
|
|
3579 * Do this loop twice:
|
|
3580 * 1: find number of arguments
|
|
3581 * 2: separate them and build argv[]
|
|
3582 */
|
7
|
3583 for (i = 0; i < 2; ++i)
|
|
3584 {
|
|
3585 p = newcmd;
|
|
3586 inquote = FALSE;
|
|
3587 argc = 0;
|
|
3588 for (;;)
|
|
3589 {
|
|
3590 if (i == 1)
|
|
3591 argv[argc] = (char *)p;
|
|
3592 ++argc;
|
|
3593 while (*p && (inquote || (*p != ' ' && *p != TAB)))
|
|
3594 {
|
|
3595 if (*p == '"')
|
|
3596 inquote = !inquote;
|
|
3597 ++p;
|
|
3598 }
|
|
3599 if (*p == NUL)
|
|
3600 break;
|
|
3601 if (i == 1)
|
|
3602 *p++ = NUL;
|
|
3603 p = skipwhite(p);
|
|
3604 }
|
|
3605 if (i == 0)
|
|
3606 {
|
|
3607 argv = (char **)alloc((unsigned)((argc + 4) * sizeof(char *)));
|
|
3608 if (argv == NULL) /* out of memory */
|
|
3609 goto error;
|
|
3610 }
|
|
3611 }
|
|
3612 if (cmd != NULL)
|
|
3613 {
|
|
3614 if (extra_shell_arg != NULL)
|
|
3615 argv[argc++] = (char *)extra_shell_arg;
|
|
3616 argv[argc++] = (char *)p_shcf;
|
|
3617 argv[argc++] = (char *)cmd;
|
|
3618 }
|
|
3619 argv[argc] = NULL;
|
|
3620
|
167
|
3621 /*
|
|
3622 * For the GUI, when writing the output into the buffer and when reading
|
|
3623 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
|
|
3624 * of the executed command into the Vim window. Or use a pipe.
|
|
3625 */
|
|
3626 if ((options & (SHELL_READ|SHELL_WRITE))
|
7
|
3627 # ifdef FEAT_GUI
|
167
|
3628 || (gui.in_use && show_shell_mess)
|
|
3629 # endif
|
|
3630 )
|
7
|
3631 {
|
167
|
3632 # ifdef FEAT_GUI
|
7
|
3633 /*
|
|
3634 * Try to open a master pty.
|
|
3635 * If this works, open the slave pty.
|
|
3636 * If the slave can't be opened, close the master pty.
|
|
3637 */
|
167
|
3638 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
|
7
|
3639 {
|
|
3640 pty_master_fd = OpenPTY(&tty_name); /* open pty */
|
|
3641 if (pty_master_fd >= 0 && ((pty_slave_fd =
|
|
3642 open(tty_name, O_RDWR | O_EXTRA, 0)) < 0))
|
|
3643 {
|
|
3644 close(pty_master_fd);
|
|
3645 pty_master_fd = -1;
|
|
3646 }
|
|
3647 }
|
|
3648 /*
|
|
3649 * If not opening a pty or it didn't work, try using pipes.
|
|
3650 */
|
|
3651 if (pty_master_fd < 0)
|
167
|
3652 # endif
|
7
|
3653 {
|
|
3654 pipe_error = (pipe(fd_toshell) < 0);
|
|
3655 if (!pipe_error) /* pipe create OK */
|
|
3656 {
|
|
3657 pipe_error = (pipe(fd_fromshell) < 0);
|
|
3658 if (pipe_error) /* pipe create failed */
|
|
3659 {
|
|
3660 close(fd_toshell[0]);
|
|
3661 close(fd_toshell[1]);
|
|
3662 }
|
|
3663 }
|
|
3664 if (pipe_error)
|
|
3665 {
|
|
3666 MSG_PUTS(_("\nCannot create pipes\n"));
|
|
3667 out_flush();
|
|
3668 }
|
|
3669 }
|
|
3670 }
|
|
3671
|
|
3672 if (!pipe_error) /* pty or pipe opened or not used */
|
|
3673 {
|
|
3674 # ifdef __BEOS__
|
|
3675 beos_cleanup_read_thread();
|
|
3676 # endif
|
602
|
3677
|
7
|
3678 if ((pid = fork()) == -1) /* maybe we should use vfork() */
|
|
3679 {
|
|
3680 MSG_PUTS(_("\nCannot fork\n"));
|
167
|
3681 if ((options & (SHELL_READ|SHELL_WRITE))
|
7
|
3682 # ifdef FEAT_GUI
|
167
|
3683 || (gui.in_use && show_shell_mess)
|
|
3684 # endif
|
|
3685 )
|
7
|
3686 {
|
167
|
3687 # ifdef FEAT_GUI
|
7
|
3688 if (pty_master_fd >= 0) /* close the pseudo tty */
|
|
3689 {
|
|
3690 close(pty_master_fd);
|
|
3691 close(pty_slave_fd);
|
|
3692 }
|
|
3693 else /* close the pipes */
|
167
|
3694 # endif
|
7
|
3695 {
|
|
3696 close(fd_toshell[0]);
|
|
3697 close(fd_toshell[1]);
|
|
3698 close(fd_fromshell[0]);
|
|
3699 close(fd_fromshell[1]);
|
|
3700 }
|
|
3701 }
|
|
3702 }
|
|
3703 else if (pid == 0) /* child */
|
|
3704 {
|
|
3705 reset_signals(); /* handle signals normally */
|
|
3706
|
|
3707 if (!show_shell_mess || (options & SHELL_EXPAND))
|
|
3708 {
|
|
3709 int fd;
|
|
3710
|
|
3711 /*
|
|
3712 * Don't want to show any message from the shell. Can't just
|
|
3713 * close stdout and stderr though, because some systems will
|
|
3714 * break if you try to write to them after that, so we must
|
|
3715 * use dup() to replace them with something else -- webb
|
|
3716 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
|
|
3717 * waiting for input.
|
|
3718 */
|
|
3719 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
|
|
3720 fclose(stdin);
|
|
3721 fclose(stdout);
|
|
3722 fclose(stderr);
|
|
3723
|
|
3724 /*
|
|
3725 * If any of these open()'s and dup()'s fail, we just continue
|
|
3726 * anyway. It's not fatal, and on most systems it will make
|
|
3727 * no difference at all. On a few it will cause the execvp()
|
|
3728 * to exit with a non-zero status even when the completion
|
|
3729 * could be done, which is nothing too serious. If the open()
|
|
3730 * or dup() failed we'd just do the same thing ourselves
|
|
3731 * anyway -- webb
|
|
3732 */
|
|
3733 if (fd >= 0)
|
|
3734 {
|
|
3735 dup(fd); /* To replace stdin (file descriptor 0) */
|
|
3736 dup(fd); /* To replace stdout (file descriptor 1) */
|
|
3737 dup(fd); /* To replace stderr (file descriptor 2) */
|
|
3738
|
|
3739 /* Don't need this now that we've duplicated it */
|
|
3740 close(fd);
|
|
3741 }
|
|
3742 }
|
167
|
3743 else if ((options & (SHELL_READ|SHELL_WRITE))
|
7
|
3744 # ifdef FEAT_GUI
|
167
|
3745 || gui.in_use
|
|
3746 # endif
|
|
3747 )
|
7
|
3748 {
|
|
3749
|
167
|
3750 # ifdef HAVE_SETSID
|
602
|
3751 /* Create our own process group, so that the child and all its
|
|
3752 * children can be kill()ed. Don't do this when using pipes,
|
|
3753 * because stdin is not a tty, we would loose /dev/tty. */
|
|
3754 if (p_stmp)
|
|
3755 (void)setsid();
|
167
|
3756 # endif
|
|
3757 # ifdef FEAT_GUI
|
602
|
3758 if (pty_slave_fd >= 0)
|
|
3759 {
|
|
3760 /* push stream discipline modules */
|
|
3761 if (options & SHELL_COOKED)
|
|
3762 SetupSlavePTY(pty_slave_fd);
|
7
|
3763 # ifdef TIOCSCTTY
|
602
|
3764 /* Try to become controlling tty (probably doesn't work,
|
|
3765 * unless run by root) */
|
|
3766 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
|
7
|
3767 # endif
|
602
|
3768 }
|
167
|
3769 # endif
|
7
|
3770 /* Simulate to have a dumb terminal (for now) */
|
167
|
3771 # ifdef HAVE_SETENV
|
7
|
3772 setenv("TERM", "dumb", 1);
|
|
3773 sprintf((char *)envbuf, "%ld", Rows);
|
|
3774 setenv("ROWS", (char *)envbuf, 1);
|
|
3775 sprintf((char *)envbuf, "%ld", Rows);
|
|
3776 setenv("LINES", (char *)envbuf, 1);
|
|
3777 sprintf((char *)envbuf, "%ld", Columns);
|
|
3778 setenv("COLUMNS", (char *)envbuf, 1);
|
167
|
3779 # else
|
7
|
3780 /*
|
|
3781 * Putenv does not copy the string, it has to remain valid.
|
|
3782 * Use a static array to avoid loosing allocated memory.
|
|
3783 */
|
|
3784 putenv("TERM=dumb");
|
|
3785 sprintf(envbuf_Rows, "ROWS=%ld", Rows);
|
|
3786 putenv(envbuf_Rows);
|
|
3787 sprintf(envbuf_Rows, "LINES=%ld", Rows);
|
|
3788 putenv(envbuf_Rows);
|
|
3789 sprintf(envbuf_Columns, "COLUMNS=%ld", Columns);
|
|
3790 putenv(envbuf_Columns);
|
167
|
3791 # endif
|
|
3792
|
557
|
3793 /*
|
|
3794 * stderr is only redirected when using the GUI, so that a
|
|
3795 * program like gpg can still access the terminal to get a
|
|
3796 * passphrase using stderr.
|
|
3797 */
|
167
|
3798 # ifdef FEAT_GUI
|
7
|
3799 if (pty_master_fd >= 0)
|
|
3800 {
|
|
3801 close(pty_master_fd); /* close master side of pty */
|
|
3802
|
|
3803 /* set up stdin/stdout/stderr for the child */
|
|
3804 close(0);
|
|
3805 dup(pty_slave_fd);
|
|
3806 close(1);
|
|
3807 dup(pty_slave_fd);
|
557
|
3808 if (gui.in_use)
|
|
3809 {
|
|
3810 close(2);
|
|
3811 dup(pty_slave_fd);
|
|
3812 }
|
7
|
3813
|
|
3814 close(pty_slave_fd); /* has been dupped, close it now */
|
|
3815 }
|
|
3816 else
|
167
|
3817 # endif
|
7
|
3818 {
|
|
3819 /* set up stdin for the child */
|
|
3820 close(fd_toshell[1]);
|
|
3821 close(0);
|
|
3822 dup(fd_toshell[0]);
|
|
3823 close(fd_toshell[0]);
|
|
3824
|
|
3825 /* set up stdout for the child */
|
|
3826 close(fd_fromshell[0]);
|
|
3827 close(1);
|
|
3828 dup(fd_fromshell[1]);
|
|
3829 close(fd_fromshell[1]);
|
|
3830
|
557
|
3831 # ifdef FEAT_GUI
|
|
3832 if (gui.in_use)
|
|
3833 {
|
|
3834 /* set up stderr for the child */
|
|
3835 close(2);
|
|
3836 dup(1);
|
|
3837 }
|
|
3838 # endif
|
7
|
3839 }
|
|
3840 }
|
167
|
3841
|
7
|
3842 /*
|
|
3843 * There is no type cast for the argv, because the type may be
|
|
3844 * different on different machines. This may cause a warning
|
|
3845 * message with strict compilers, don't worry about it.
|
|
3846 * Call _exit() instead of exit() to avoid closing the connection
|
|
3847 * to the X server (esp. with GTK, which uses atexit()).
|
|
3848 */
|
|
3849 execvp(argv[0], argv);
|
|
3850 _exit(EXEC_FAILED); /* exec failed, return failure code */
|
|
3851 }
|
|
3852 else /* parent */
|
|
3853 {
|
|
3854 /*
|
|
3855 * While child is running, ignore terminating signals.
|
167
|
3856 * Do catch CTRL-C, so that "got_int" is set.
|
7
|
3857 */
|
|
3858 catch_signals(SIG_IGN, SIG_ERR);
|
167
|
3859 catch_int_signal();
|
7
|
3860
|
|
3861 /*
|
|
3862 * For the GUI we redirect stdin, stdout and stderr to our window.
|
167
|
3863 * This is also used to pipe stdin/stdout to/from the external
|
|
3864 * command.
|
7
|
3865 */
|
167
|
3866 if ((options & (SHELL_READ|SHELL_WRITE))
|
|
3867 # ifdef FEAT_GUI
|
|
3868 || (gui.in_use && show_shell_mess)
|
|
3869 # endif
|
|
3870 )
|
7
|
3871 {
|
167
|
3872 # define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
|
7
|
3873 char_u buffer[BUFLEN + 1];
|
167
|
3874 # ifdef FEAT_MBYTE
|
7
|
3875 int buffer_off = 0; /* valid bytes in buffer[] */
|
167
|
3876 # endif
|
7
|
3877 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
|
|
3878 int ta_len = 0; /* valid bytes in ta_buf[] */
|
|
3879 int len;
|
|
3880 int p_more_save;
|
|
3881 int old_State;
|
|
3882 int c;
|
|
3883 int toshell_fd;
|
|
3884 int fromshell_fd;
|
167
|
3885 garray_T ga;
|
|
3886 int noread_cnt;
|
|
3887
|
|
3888 # ifdef FEAT_GUI
|
7
|
3889 if (pty_master_fd >= 0)
|
|
3890 {
|
|
3891 close(pty_slave_fd); /* close slave side of pty */
|
|
3892 fromshell_fd = pty_master_fd;
|
|
3893 toshell_fd = dup(pty_master_fd);
|
|
3894 }
|
|
3895 else
|
167
|
3896 # endif
|
7
|
3897 {
|
|
3898 close(fd_toshell[0]);
|
|
3899 close(fd_fromshell[1]);
|
|
3900 toshell_fd = fd_toshell[1];
|
|
3901 fromshell_fd = fd_fromshell[0];
|
|
3902 }
|
|
3903
|
|
3904 /*
|
|
3905 * Write to the child if there are typed characters.
|
|
3906 * Read from the child if there are characters available.
|
|
3907 * Repeat the reading a few times if more characters are
|
|
3908 * available. Need to check for typed keys now and then, but
|
|
3909 * not too often (delays when no chars are available).
|
|
3910 * This loop is quit if no characters can be read from the pty
|
|
3911 * (WaitForChar detected special condition), or there are no
|
|
3912 * characters available and the child has exited.
|
|
3913 * Only check if the child has exited when there is no more
|
|
3914 * output. The child may exit before all the output has
|
|
3915 * been printed.
|
|
3916 *
|
|
3917 * Currently this busy loops!
|
|
3918 * This can probably dead-lock when the write blocks!
|
|
3919 */
|
|
3920 p_more_save = p_more;
|
|
3921 p_more = FALSE;
|
|
3922 old_State = State;
|
|
3923 State = EXTERNCMD; /* don't redraw at window resize */
|
|
3924
|
602
|
3925 if ((options & SHELL_WRITE) && toshell_fd >= 0)
|
167
|
3926 {
|
|
3927 /* Fork a process that will write the lines to the
|
|
3928 * external program. */
|
|
3929 if ((wpid = fork()) == -1)
|
|
3930 {
|
|
3931 MSG_PUTS(_("\nCannot fork\n"));
|
|
3932 }
|
|
3933 else if (wpid == 0)
|
|
3934 {
|
|
3935 linenr_T lnum = curbuf->b_op_start.lnum;
|
|
3936 int written = 0;
|
|
3937 char_u *p = ml_get(lnum);
|
|
3938 char_u *s;
|
|
3939 size_t l;
|
|
3940
|
|
3941 /* child */
|
177
|
3942 close(fromshell_fd);
|
167
|
3943 for (;;)
|
|
3944 {
|
|
3945 l = STRLEN(p + written);
|
|
3946 if (l == 0)
|
|
3947 len = 0;
|
|
3948 else if (p[written] == NL)
|
|
3949 /* NL -> NUL translation */
|
|
3950 len = write(toshell_fd, "", (size_t)1);
|
|
3951 else
|
|
3952 {
|
|
3953 s = vim_strchr(p + written, NL);
|
|
3954 len = write(toshell_fd, (char *)p + written,
|
|
3955 s == NULL ? l : s - (p + written));
|
|
3956 }
|
|
3957 if (len == l)
|
|
3958 {
|
|
3959 /* Finished a line, add a NL, unless this line
|
|
3960 * should not have one. */
|
|
3961 if (lnum != curbuf->b_op_end.lnum
|
|
3962 || !curbuf->b_p_bin
|
|
3963 || (lnum != write_no_eol_lnum
|
|
3964 && (lnum !=
|
|
3965 curbuf->b_ml.ml_line_count
|
|
3966 || curbuf->b_p_eol)))
|
|
3967 write(toshell_fd, "\n", (size_t)1);
|
|
3968 ++lnum;
|
|
3969 if (lnum > curbuf->b_op_end.lnum)
|
|
3970 {
|
|
3971 /* finished all the lines, close pipe */
|
|
3972 close(toshell_fd);
|
|
3973 toshell_fd = -1;
|
|
3974 break;
|
|
3975 }
|
|
3976 p = ml_get(lnum);
|
|
3977 written = 0;
|
|
3978 }
|
|
3979 else if (len > 0)
|
|
3980 written += len;
|
|
3981 }
|
|
3982 _exit(0);
|
|
3983 }
|
|
3984 else
|
|
3985 {
|
|
3986 close(toshell_fd);
|
|
3987 toshell_fd = -1;
|
|
3988 }
|
|
3989 }
|
|
3990
|
|
3991 if (options & SHELL_READ)
|
|
3992 ga_init2(&ga, 1, BUFLEN);
|
|
3993
|
|
3994 noread_cnt = 0;
|
|
3995
|
7
|
3996 for (;;)
|
|
3997 {
|
|
3998 /*
|
|
3999 * Check if keys have been typed, write them to the child
|
592
|
4000 * if there are any.
|
|
4001 * Don't do this if we are expanding wild cards (would eat
|
|
4002 * typeahead).
|
|
4003 * Don't do this when filtering and terminal is in cooked
|
|
4004 * mode, the shell command will handle the I/O. Avoids
|
|
4005 * that a typed password is echoed for ssh or gpg command.
|
602
|
4006 * Don't get characters when the child has already
|
|
4007 * finished (wait_pid == 0).
|
592
|
4008 * Don't get extra characters when we already have one.
|
167
|
4009 * Don't read characters unless we didn't get output for a
|
|
4010 * while, avoids that ":r !ls" eats typeahead.
|
7
|
4011 */
|
|
4012 len = 0;
|
|
4013 if (!(options & SHELL_EXPAND)
|
592
|
4014 && ((options &
|
|
4015 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
|
|
4016 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
|
|
4017 #ifdef FEAT_GUI
|
|
4018 || gui.in_use
|
|
4019 #endif
|
|
4020 )
|
602
|
4021 && wait_pid == 0
|
7
|
4022 && (ta_len > 0
|
167
|
4023 || (noread_cnt > 4
|
|
4024 && (len = ui_inchar(ta_buf,
|
|
4025 BUFLEN, 10L, 0)) > 0)))
|
7
|
4026 {
|
|
4027 /*
|
|
4028 * For pipes:
|
|
4029 * Check for CTRL-C: send interrupt signal to child.
|
|
4030 * Check for CTRL-D: EOF, close pipe to child.
|
|
4031 */
|
|
4032 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
|
|
4033 {
|
167
|
4034 # ifdef SIGINT
|
7
|
4035 /*
|
|
4036 * Send SIGINT to the child's group or all
|
|
4037 * processes in our group.
|
|
4038 */
|
|
4039 if (ta_buf[ta_len] == Ctrl_C
|
|
4040 || ta_buf[ta_len] == intr_char)
|
167
|
4041 {
|
|
4042 # ifdef HAVE_SETSID
|
7
|
4043 kill(-pid, SIGINT);
|
167
|
4044 # else
|
7
|
4045 kill(0, SIGINT);
|
|
4046 # endif
|
167
|
4047 if (wpid > 0)
|
|
4048 kill(wpid, SIGINT);
|
|
4049 }
|
|
4050 # endif
|
7
|
4051 if (pty_master_fd < 0 && toshell_fd >= 0
|
|
4052 && ta_buf[ta_len] == Ctrl_D)
|
|
4053 {
|
|
4054 close(toshell_fd);
|
|
4055 toshell_fd = -1;
|
|
4056 }
|
|
4057 }
|
|
4058
|
|
4059 /* replace K_BS by <BS> and K_DEL by <DEL> */
|
|
4060 for (i = ta_len; i < ta_len + len; ++i)
|
|
4061 {
|
|
4062 if (ta_buf[i] == CSI && len - i > 2)
|
|
4063 {
|
|
4064 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
|
|
4065 if (c == K_DEL || c == K_KDEL || c == K_BS)
|
|
4066 {
|
|
4067 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
|
|
4068 (size_t)(len - i - 2));
|
|
4069 if (c == K_DEL || c == K_KDEL)
|
|
4070 ta_buf[i] = DEL;
|
|
4071 else
|
|
4072 ta_buf[i] = Ctrl_H;
|
|
4073 len -= 2;
|
|
4074 }
|
|
4075 }
|
|
4076 else if (ta_buf[i] == '\r')
|
|
4077 ta_buf[i] = '\n';
|
167
|
4078 # ifdef FEAT_MBYTE
|
7
|
4079 if (has_mbyte)
|
474
|
4080 i += (*mb_ptr2len)(ta_buf + i) - 1;
|
167
|
4081 # endif
|
7
|
4082 }
|
|
4083
|
|
4084 /*
|
|
4085 * For pipes: echo the typed characters.
|
|
4086 * For a pty this does not seem to work.
|
|
4087 */
|
|
4088 if (pty_master_fd < 0)
|
|
4089 {
|
|
4090 for (i = ta_len; i < ta_len + len; ++i)
|
|
4091 {
|
|
4092 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
|
|
4093 msg_putchar(ta_buf[i]);
|
167
|
4094 # ifdef FEAT_MBYTE
|
7
|
4095 else if (has_mbyte)
|
|
4096 {
|
474
|
4097 int l = (*mb_ptr2len)(ta_buf + i);
|
7
|
4098
|
|
4099 msg_outtrans_len(ta_buf + i, l);
|
|
4100 i += l - 1;
|
|
4101 }
|
167
|
4102 # endif
|
7
|
4103 else
|
|
4104 msg_outtrans_len(ta_buf + i, 1);
|
|
4105 }
|
|
4106 windgoto(msg_row, msg_col);
|
|
4107 out_flush();
|
|
4108 }
|
|
4109
|
|
4110 ta_len += len;
|
|
4111
|
|
4112 /*
|
|
4113 * Write the characters to the child, unless EOF has
|
|
4114 * been typed for pipes. Write one character at a
|
|
4115 * time, to avoid loosing too much typeahead.
|
167
|
4116 * When writing buffer lines, drop the typed
|
|
4117 * characters (only check for CTRL-C).
|
7
|
4118 */
|
167
|
4119 if (options & SHELL_WRITE)
|
|
4120 ta_len = 0;
|
|
4121 else if (toshell_fd >= 0)
|
7
|
4122 {
|
|
4123 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
|
|
4124 if (len > 0)
|
|
4125 {
|
|
4126 ta_len -= len;
|
|
4127 mch_memmove(ta_buf, ta_buf + len, ta_len);
|
167
|
4128 noread_cnt = 0;
|
7
|
4129 }
|
|
4130 }
|
|
4131 }
|
|
4132
|
167
|
4133 if (got_int)
|
|
4134 {
|
|
4135 /* CTRL-C sends a signal to the child, we ignore it
|
|
4136 * ourselves */
|
|
4137 # ifdef HAVE_SETSID
|
|
4138 kill(-pid, SIGINT);
|
|
4139 # else
|
|
4140 kill(0, SIGINT);
|
|
4141 # endif
|
|
4142 if (wpid > 0)
|
|
4143 kill(wpid, SIGINT);
|
|
4144 got_int = FALSE;
|
|
4145 }
|
|
4146
|
7
|
4147 /*
|
|
4148 * Check if the child has any characters to be printed.
|
|
4149 * Read them and write them to our window. Repeat this as
|
|
4150 * long as there is something to do, avoid the 10ms wait
|
|
4151 * for mch_inchar(), or sending typeahead characters to
|
|
4152 * the external process.
|
|
4153 * TODO: This should handle escape sequences, compatible
|
|
4154 * to some terminal (vt52?).
|
|
4155 */
|
167
|
4156 ++noread_cnt;
|
7
|
4157 while (RealWaitForChar(fromshell_fd, 10L, NULL))
|
|
4158 {
|
|
4159 len = read(fromshell_fd, (char *)buffer
|
167
|
4160 # ifdef FEAT_MBYTE
|
7
|
4161 + buffer_off, (size_t)(BUFLEN - buffer_off)
|
167
|
4162 # else
|
7
|
4163 , (size_t)BUFLEN
|
167
|
4164 # endif
|
7
|
4165 );
|
|
4166 if (len <= 0) /* end of file or error */
|
|
4167 goto finished;
|
167
|
4168
|
|
4169 noread_cnt = 0;
|
|
4170 if (options & SHELL_READ)
|
|
4171 {
|
|
4172 /* Do NUL -> NL translation, append NL separated
|
|
4173 * lines to the current buffer. */
|
|
4174 for (i = 0; i < len; ++i)
|
|
4175 {
|
|
4176 if (buffer[i] == NL)
|
|
4177 append_ga_line(&ga);
|
|
4178 else if (buffer[i] == NUL)
|
|
4179 ga_append(&ga, NL);
|
|
4180 else
|
|
4181 ga_append(&ga, buffer[i]);
|
|
4182 }
|
|
4183 }
|
|
4184 # ifdef FEAT_MBYTE
|
|
4185 else if (has_mbyte)
|
7
|
4186 {
|
|
4187 int l;
|
|
4188
|
167
|
4189 len += buffer_off;
|
|
4190 buffer[len] = NUL;
|
|
4191
|
7
|
4192 /* Check if the last character in buffer[] is
|
|
4193 * incomplete, keep these bytes for the next
|
|
4194 * round. */
|
|
4195 for (p = buffer; p < buffer + len; p += l)
|
|
4196 {
|
474
|
4197 l = mb_cptr2len(p);
|
7
|
4198 if (l == 0)
|
|
4199 l = 1; /* NUL byte? */
|
|
4200 else if (MB_BYTE2LEN(*p) != l)
|
|
4201 break;
|
|
4202 }
|
|
4203 if (p == buffer) /* no complete character */
|
|
4204 {
|
|
4205 /* avoid getting stuck at an illegal byte */
|
|
4206 if (len >= 12)
|
|
4207 ++p;
|
|
4208 else
|
|
4209 {
|
|
4210 buffer_off = len;
|
|
4211 continue;
|
|
4212 }
|
|
4213 }
|
|
4214 c = *p;
|
|
4215 *p = NUL;
|
|
4216 msg_puts(buffer);
|
|
4217 if (p < buffer + len)
|
|
4218 {
|
|
4219 *p = c;
|
|
4220 buffer_off = (buffer + len) - p;
|
|
4221 mch_memmove(buffer, p, buffer_off);
|
|
4222 continue;
|
|
4223 }
|
|
4224 buffer_off = 0;
|
|
4225 }
|
167
|
4226 # endif /* FEAT_MBYTE */
|
7
|
4227 else
|
|
4228 {
|
|
4229 buffer[len] = NUL;
|
|
4230 msg_puts(buffer);
|
|
4231 }
|
|
4232
|
|
4233 windgoto(msg_row, msg_col);
|
|
4234 cursor_on();
|
|
4235 out_flush();
|
|
4236 if (got_int)
|
|
4237 break;
|
|
4238 }
|
|
4239
|
602
|
4240 /* If we already detected the child has finished break the
|
|
4241 * loop now. */
|
|
4242 if (wait_pid == pid)
|
|
4243 break;
|
|
4244
|
7
|
4245 /*
|
|
4246 * Check if the child still exists, before checking for
|
|
4247 * typed characters (otherwise we would loose typeahead).
|
|
4248 */
|
167
|
4249 # ifdef __NeXT__
|
7
|
4250 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *) 0);
|
167
|
4251 # else
|
7
|
4252 wait_pid = waitpid(pid, &status, WNOHANG);
|
167
|
4253 # endif
|
7
|
4254 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
|
|
4255 || (wait_pid == pid && WIFEXITED(status)))
|
|
4256 {
|
602
|
4257 /* Don't break the loop yet, try reading more
|
|
4258 * characters from "fromshell_fd" first. When using
|
|
4259 * pipes there might still be something to read and
|
|
4260 * then we'll break the loop at the "break" above. */
|
7
|
4261 wait_pid = pid;
|
|
4262 }
|
602
|
4263 else
|
|
4264 wait_pid = 0;
|
7
|
4265 }
|
|
4266 finished:
|
|
4267 p_more = p_more_save;
|
167
|
4268 if (options & SHELL_READ)
|
|
4269 {
|
|
4270 if (ga.ga_len > 0)
|
|
4271 {
|
|
4272 append_ga_line(&ga);
|
|
4273 /* remember that the NL was missing */
|
|
4274 write_no_eol_lnum = curwin->w_cursor.lnum;
|
|
4275 }
|
|
4276 else
|
|
4277 write_no_eol_lnum = 0;
|
|
4278 ga_clear(&ga);
|
|
4279 }
|
|
4280
|
7
|
4281 /*
|
|
4282 * Give all typeahead that wasn't used back to ui_inchar().
|
|
4283 */
|
|
4284 if (ta_len)
|
|
4285 ui_inchar_undo(ta_buf, ta_len);
|
|
4286 State = old_State;
|
|
4287 if (toshell_fd >= 0)
|
|
4288 close(toshell_fd);
|
|
4289 close(fromshell_fd);
|
|
4290 }
|
|
4291
|
|
4292 /*
|
|
4293 * Wait until our child has exited.
|
|
4294 * Ignore wait() returning pids of other children and returning
|
|
4295 * because of some signal like SIGWINCH.
|
|
4296 * Don't wait if wait_pid was already set above, indicating the
|
|
4297 * child already exited.
|
|
4298 */
|
|
4299 while (wait_pid != pid)
|
|
4300 {
|
167
|
4301 # ifdef _THREAD_SAFE
|
7
|
4302 /* Ugly hack: when compiled with Python threads are probably
|
|
4303 * used, in which case wait() sometimes hangs for no obvious
|
|
4304 * reason. Use waitpid() instead and loop (like the GUI). */
|
|
4305 # ifdef __NeXT__
|
|
4306 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
|
|
4307 # else
|
|
4308 wait_pid = waitpid(pid, &status, WNOHANG);
|
|
4309 # endif
|
|
4310 if (wait_pid == 0)
|
|
4311 {
|
|
4312 /* Wait for 1/100 sec before trying again. */
|
|
4313 mch_delay(10L, TRUE);
|
|
4314 continue;
|
|
4315 }
|
167
|
4316 # else
|
7
|
4317 wait_pid = wait(&status);
|
167
|
4318 # endif
|
7
|
4319 if (wait_pid <= 0
|
|
4320 # ifdef ECHILD
|
|
4321 && errno == ECHILD
|
|
4322 # endif
|
|
4323 )
|
|
4324 break;
|
|
4325 }
|
|
4326
|
167
|
4327 /* Make sure the child that writes to the external program is
|
|
4328 * dead. */
|
|
4329 if (wpid > 0)
|
|
4330 kill(wpid, SIGKILL);
|
|
4331
|
7
|
4332 /*
|
|
4333 * Set to raw mode right now, otherwise a CTRL-C after
|
|
4334 * catch_signals() will kill Vim.
|
|
4335 */
|
|
4336 if (tmode == TMODE_RAW)
|
|
4337 settmode(TMODE_RAW);
|
|
4338 did_settmode = TRUE;
|
|
4339 set_signals();
|
|
4340
|
|
4341 if (WIFEXITED(status))
|
|
4342 {
|
129
|
4343 /* LINTED avoid "bitwise operation on signed value" */
|
7
|
4344 retval = WEXITSTATUS(status);
|
|
4345 if (retval && !emsg_silent)
|
|
4346 {
|
|
4347 if (retval == EXEC_FAILED)
|
|
4348 {
|
|
4349 MSG_PUTS(_("\nCannot execute shell "));
|
|
4350 msg_outtrans(p_sh);
|
|
4351 msg_putchar('\n');
|
|
4352 }
|
|
4353 else if (!(options & SHELL_SILENT))
|
|
4354 {
|
|
4355 MSG_PUTS(_("\nshell returned "));
|
|
4356 msg_outnum((long)retval);
|
|
4357 msg_putchar('\n');
|
|
4358 }
|
|
4359 }
|
|
4360 }
|
|
4361 else
|
|
4362 MSG_PUTS(_("\nCommand terminated\n"));
|
|
4363 }
|
|
4364 }
|
|
4365 vim_free(argv);
|
|
4366
|
|
4367 error:
|
|
4368 if (!did_settmode)
|
|
4369 if (tmode == TMODE_RAW)
|
|
4370 settmode(TMODE_RAW); /* set to raw mode */
|
|
4371 # ifdef FEAT_TITLE
|
|
4372 resettitle();
|
|
4373 # endif
|
|
4374 vim_free(newcmd);
|
|
4375
|
|
4376 return retval;
|
|
4377
|
|
4378 #endif /* USE_SYSTEM */
|
|
4379 }
|
|
4380
|
|
4381 /*
|
|
4382 * Check for CTRL-C typed by reading all available characters.
|
|
4383 * In cooked mode we should get SIGINT, no need to check.
|
|
4384 */
|
|
4385 void
|
|
4386 mch_breakcheck()
|
|
4387 {
|
|
4388 if (curr_tmode == TMODE_RAW && RealWaitForChar(read_cmd_fd, 0L, NULL))
|
|
4389 fill_input_buf(FALSE);
|
|
4390 }
|
|
4391
|
|
4392 /*
|
|
4393 * Wait "msec" msec until a character is available from the keyboard or from
|
|
4394 * inbuf[]. msec == -1 will block forever.
|
|
4395 * When a GUI is being used, this will never get called -- webb
|
|
4396 */
|
|
4397 static int
|
|
4398 WaitForChar(msec)
|
|
4399 long msec;
|
|
4400 {
|
|
4401 #ifdef FEAT_MOUSE_GPM
|
|
4402 int gpm_process_wanted;
|
|
4403 #endif
|
|
4404 #ifdef FEAT_XCLIPBOARD
|
|
4405 int rest;
|
|
4406 #endif
|
|
4407 int avail;
|
|
4408
|
|
4409 if (input_available()) /* something in inbuf[] */
|
|
4410 return 1;
|
|
4411
|
|
4412 #if defined(FEAT_MOUSE_DEC)
|
|
4413 /* May need to query the mouse position. */
|
|
4414 if (WantQueryMouse)
|
|
4415 {
|
227
|
4416 WantQueryMouse = FALSE;
|
7
|
4417 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
|
|
4418 }
|
|
4419 #endif
|
|
4420
|
|
4421 /*
|
|
4422 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
|
|
4423 * events. This is a bit complicated, because they might both be defined.
|
|
4424 */
|
|
4425 #if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
|
|
4426 # ifdef FEAT_XCLIPBOARD
|
|
4427 rest = 0;
|
|
4428 if (do_xterm_trace())
|
|
4429 rest = msec;
|
|
4430 # endif
|
|
4431 do
|
|
4432 {
|
|
4433 # ifdef FEAT_XCLIPBOARD
|
|
4434 if (rest != 0)
|
|
4435 {
|
|
4436 msec = XT_TRACE_DELAY;
|
|
4437 if (rest >= 0 && rest < XT_TRACE_DELAY)
|
|
4438 msec = rest;
|
|
4439 if (rest >= 0)
|
|
4440 rest -= msec;
|
|
4441 }
|
|
4442 # endif
|
|
4443 # ifdef FEAT_MOUSE_GPM
|
|
4444 gpm_process_wanted = 0;
|
|
4445 avail = RealWaitForChar(read_cmd_fd, msec, &gpm_process_wanted);
|
|
4446 # else
|
|
4447 avail = RealWaitForChar(read_cmd_fd, msec, NULL);
|
|
4448 # endif
|
|
4449 if (!avail)
|
|
4450 {
|
|
4451 if (input_available())
|
|
4452 return 1;
|
|
4453 # ifdef FEAT_XCLIPBOARD
|
|
4454 if (rest == 0 || !do_xterm_trace())
|
|
4455 # endif
|
|
4456 break;
|
|
4457 }
|
|
4458 }
|
|
4459 while (FALSE
|
|
4460 # ifdef FEAT_MOUSE_GPM
|
|
4461 || (gpm_process_wanted && mch_gpm_process() == 0)
|
|
4462 # endif
|
|
4463 # ifdef FEAT_XCLIPBOARD
|
|
4464 || (!avail && rest != 0)
|
|
4465 # endif
|
|
4466 );
|
|
4467
|
|
4468 #else
|
|
4469 avail = RealWaitForChar(read_cmd_fd, msec, NULL);
|
|
4470 #endif
|
|
4471 return avail;
|
|
4472 }
|
|
4473
|
|
4474 /*
|
|
4475 * Wait "msec" msec until a character is available from file descriptor "fd".
|
|
4476 * Time == -1 will block forever.
|
|
4477 * When a GUI is being used, this will not be used for input -- webb
|
|
4478 * Returns also, when a request from Sniff is waiting -- toni.
|
|
4479 * Or when a Linux GPM mouse event is waiting.
|
|
4480 */
|
|
4481 /* ARGSUSED */
|
|
4482 #if defined(__BEOS__)
|
|
4483 int
|
|
4484 #else
|
|
4485 static int
|
|
4486 #endif
|
|
4487 RealWaitForChar(fd, msec, check_for_gpm)
|
|
4488 int fd;
|
|
4489 long msec;
|
|
4490 int *check_for_gpm;
|
|
4491 {
|
|
4492 int ret;
|
14
|
4493 #if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
|
7
|
4494 static int busy = FALSE;
|
|
4495
|
|
4496 /* May retry getting characters after an event was handled. */
|
|
4497 # define MAY_LOOP
|
|
4498
|
|
4499 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
|
|
4500 /* Remember at what time we started, so that we know how much longer we
|
|
4501 * should wait after being interrupted. */
|
|
4502 # define USE_START_TV
|
|
4503 struct timeval start_tv;
|
|
4504
|
|
4505 if (msec > 0 && (
|
|
4506 # ifdef FEAT_XCLIPBOARD
|
|
4507 xterm_Shell != (Widget)0
|
14
|
4508 # if defined(USE_XSMP) || defined(FEAT_MZSCHEME)
|
7
|
4509 ||
|
|
4510 # endif
|
|
4511 # endif
|
|
4512 # ifdef USE_XSMP
|
|
4513 xsmp_icefd != -1
|
14
|
4514 # ifdef FEAT_MZSCHEME
|
|
4515 ||
|
|
4516 # endif
|
|
4517 # endif
|
|
4518 # ifdef FEAT_MZSCHEME
|
|
4519 (mzthreads_allowed() && p_mzq > 0)
|
7
|
4520 # endif
|
|
4521 ))
|
|
4522 gettimeofday(&start_tv, NULL);
|
|
4523 # endif
|
|
4524
|
|
4525 /* Handle being called recursively. This may happen for the session
|
|
4526 * manager stuff, it may save the file, which does a breakcheck. */
|
|
4527 if (busy)
|
|
4528 return 0;
|
|
4529 #endif
|
|
4530
|
|
4531 #ifdef MAY_LOOP
|
407
|
4532 for (;;)
|
7
|
4533 #endif
|
|
4534 {
|
|
4535 #ifdef MAY_LOOP
|
|
4536 int finished = TRUE; /* default is to 'loop' just once */
|
14
|
4537 # ifdef FEAT_MZSCHEME
|
|
4538 int mzquantum_used = FALSE;
|
|
4539 # endif
|
7
|
4540 #endif
|
|
4541 #ifndef HAVE_SELECT
|
|
4542 struct pollfd fds[5];
|
|
4543 int nfd;
|
|
4544 # ifdef FEAT_XCLIPBOARD
|
|
4545 int xterm_idx = -1;
|
|
4546 # endif
|
|
4547 # ifdef FEAT_MOUSE_GPM
|
|
4548 int gpm_idx = -1;
|
|
4549 # endif
|
|
4550 # ifdef USE_XSMP
|
|
4551 int xsmp_idx = -1;
|
|
4552 # endif
|
14
|
4553 int towait = (int)msec;
|
|
4554
|
|
4555 # ifdef FEAT_MZSCHEME
|
|
4556 mzvim_check_threads();
|
|
4557 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
|
|
4558 {
|
|
4559 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
|
|
4560 mzquantum_used = TRUE;
|
|
4561 }
|
|
4562 # endif
|
7
|
4563 fds[0].fd = fd;
|
|
4564 fds[0].events = POLLIN;
|
|
4565 nfd = 1;
|
|
4566
|
|
4567 # ifdef FEAT_SNIFF
|
|
4568 # define SNIFF_IDX 1
|
|
4569 if (want_sniff_request)
|
|
4570 {
|
|
4571 fds[SNIFF_IDX].fd = fd_from_sniff;
|
|
4572 fds[SNIFF_IDX].events = POLLIN;
|
|
4573 nfd++;
|
|
4574 }
|
|
4575 # endif
|
|
4576 # ifdef FEAT_XCLIPBOARD
|
|
4577 if (xterm_Shell != (Widget)0)
|
|
4578 {
|
|
4579 xterm_idx = nfd;
|
|
4580 fds[nfd].fd = ConnectionNumber(xterm_dpy);
|
|
4581 fds[nfd].events = POLLIN;
|
|
4582 nfd++;
|
|
4583 }
|
|
4584 # endif
|
|
4585 # ifdef FEAT_MOUSE_GPM
|
|
4586 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
|
|
4587 {
|
|
4588 gpm_idx = nfd;
|
|
4589 fds[nfd].fd = gpm_fd;
|
|
4590 fds[nfd].events = POLLIN;
|
|
4591 nfd++;
|
|
4592 }
|
|
4593 # endif
|
|
4594 # ifdef USE_XSMP
|
|
4595 if (xsmp_icefd != -1)
|
|
4596 {
|
|
4597 xsmp_idx = nfd;
|
|
4598 fds[nfd].fd = xsmp_icefd;
|
|
4599 fds[nfd].events = POLLIN;
|
|
4600 nfd++;
|
|
4601 }
|
|
4602 # endif
|
|
4603
|
14
|
4604 ret = poll(fds, nfd, towait);
|
|
4605 # ifdef FEAT_MZSCHEME
|
|
4606 if (ret == 0 && mzquantum_used)
|
|
4607 /* MzThreads scheduling is required and timeout occured */
|
|
4608 finished = FALSE;
|
|
4609 # endif
|
7
|
4610
|
|
4611 # ifdef FEAT_SNIFF
|
|
4612 if (ret < 0)
|
|
4613 sniff_disconnect(1);
|
|
4614 else if (want_sniff_request)
|
|
4615 {
|
|
4616 if (fds[SNIFF_IDX].revents & POLLHUP)
|
|
4617 sniff_disconnect(1);
|
|
4618 if (fds[SNIFF_IDX].revents & POLLIN)
|
|
4619 sniff_request_waiting = 1;
|
|
4620 }
|
|
4621 # endif
|
|
4622 # ifdef FEAT_XCLIPBOARD
|
|
4623 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
|
|
4624 {
|
|
4625 xterm_update(); /* Maybe we should hand out clipboard */
|
|
4626 if (--ret == 0 && !input_available())
|
|
4627 /* Try again */
|
|
4628 finished = FALSE;
|
|
4629 }
|
|
4630 # endif
|
|
4631 # ifdef FEAT_MOUSE_GPM
|
|
4632 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
|
|
4633 {
|
|
4634 *check_for_gpm = 1;
|
|
4635 }
|
|
4636 # endif
|
|
4637 # ifdef USE_XSMP
|
|
4638 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
|
|
4639 {
|
|
4640 if (fds[xsmp_idx].revents & POLLIN)
|
|
4641 {
|
|
4642 busy = TRUE;
|
|
4643 xsmp_handle_requests();
|
|
4644 busy = FALSE;
|
|
4645 }
|
|
4646 else if (fds[xsmp_idx].revents & POLLHUP)
|
|
4647 {
|
|
4648 if (p_verbose > 0)
|
292
|
4649 verb_msg((char_u *)_("XSMP lost ICE connection"));
|
7
|
4650 xsmp_close();
|
|
4651 }
|
|
4652 if (--ret == 0)
|
14
|
4653 finished = FALSE; /* Try again */
|
7
|
4654 }
|
|
4655 # endif
|
|
4656
|
|
4657
|
|
4658 #else /* HAVE_SELECT */
|
|
4659
|
|
4660 struct timeval tv;
|
14
|
4661 struct timeval *tvp;
|
7
|
4662 fd_set rfds, efds;
|
|
4663 int maxfd;
|
14
|
4664 long towait = msec;
|
|
4665
|
|
4666 # ifdef FEAT_MZSCHEME
|
|
4667 mzvim_check_threads();
|
|
4668 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
|
|
4669 {
|
|
4670 towait = p_mzq; /* don't wait longer than 'mzquantum' */
|
|
4671 mzquantum_used = TRUE;
|
|
4672 }
|
|
4673 # endif
|
7
|
4674 # ifdef __EMX__
|
|
4675 /* don't check for incoming chars if not in raw mode, because select()
|
|
4676 * always returns TRUE then (in some version of emx.dll) */
|
|
4677 if (curr_tmode != TMODE_RAW)
|
|
4678 return 0;
|
|
4679 # endif
|
|
4680
|
14
|
4681 if (towait >= 0)
|
7
|
4682 {
|
14
|
4683 tv.tv_sec = towait / 1000;
|
|
4684 tv.tv_usec = (towait % 1000) * (1000000/1000);
|
|
4685 tvp = &tv;
|
7
|
4686 }
|
14
|
4687 else
|
|
4688 tvp = NULL;
|
7
|
4689
|
|
4690 /*
|
|
4691 * Select on ready for reading and exceptional condition (end of file).
|
|
4692 */
|
|
4693 FD_ZERO(&rfds); /* calls bzero() on a sun */
|
|
4694 FD_ZERO(&efds);
|
|
4695 FD_SET(fd, &rfds);
|
|
4696 # if !defined(__QNX__) && !defined(__CYGWIN32__)
|
|
4697 /* For QNX select() always returns 1 if this is set. Why? */
|
|
4698 FD_SET(fd, &efds);
|
|
4699 # endif
|
|
4700 maxfd = fd;
|
|
4701
|
|
4702 # ifdef FEAT_SNIFF
|
|
4703 if (want_sniff_request)
|
|
4704 {
|
|
4705 FD_SET(fd_from_sniff, &rfds);
|
|
4706 FD_SET(fd_from_sniff, &efds);
|
|
4707 if (maxfd < fd_from_sniff)
|
|
4708 maxfd = fd_from_sniff;
|
|
4709 }
|
|
4710 # endif
|
|
4711 # ifdef FEAT_XCLIPBOARD
|
|
4712 if (xterm_Shell != (Widget)0)
|
|
4713 {
|
|
4714 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
|
|
4715 if (maxfd < ConnectionNumber(xterm_dpy))
|
|
4716 maxfd = ConnectionNumber(xterm_dpy);
|
|
4717 }
|
|
4718 # endif
|
|
4719 # ifdef FEAT_MOUSE_GPM
|
|
4720 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
|
|
4721 {
|
|
4722 FD_SET(gpm_fd, &rfds);
|
|
4723 FD_SET(gpm_fd, &efds);
|
|
4724 if (maxfd < gpm_fd)
|
|
4725 maxfd = gpm_fd;
|
|
4726 }
|
|
4727 # endif
|
|
4728 # ifdef USE_XSMP
|
|
4729 if (xsmp_icefd != -1)
|
|
4730 {
|
|
4731 FD_SET(xsmp_icefd, &rfds);
|
|
4732 FD_SET(xsmp_icefd, &efds);
|
|
4733 if (maxfd < xsmp_icefd)
|
|
4734 maxfd = xsmp_icefd;
|
|
4735 }
|
|
4736 # endif
|
|
4737
|
|
4738 # ifdef OLD_VMS
|
|
4739 /* Old VMS as v6.2 and older have broken select(). It waits more than
|
|
4740 * required. Should not be used */
|
|
4741 ret = 0;
|
|
4742 # else
|
14
|
4743 ret = select(maxfd + 1, &rfds, NULL, &efds, tvp);
|
|
4744 # endif
|
|
4745 # ifdef FEAT_MZSCHEME
|
|
4746 if (ret == 0 && mzquantum_used)
|
|
4747 /* loop if MzThreads must be scheduled and timeout occured */
|
|
4748 finished = FALSE;
|
7
|
4749 # endif
|
|
4750
|
|
4751 # ifdef FEAT_SNIFF
|
|
4752 if (ret < 0 )
|
|
4753 sniff_disconnect(1);
|
|
4754 else if (ret > 0 && want_sniff_request)
|
|
4755 {
|
|
4756 if (FD_ISSET(fd_from_sniff, &efds))
|
|
4757 sniff_disconnect(1);
|
|
4758 if (FD_ISSET(fd_from_sniff, &rfds))
|
|
4759 sniff_request_waiting = 1;
|
|
4760 }
|
|
4761 # endif
|
|
4762 # ifdef FEAT_XCLIPBOARD
|
|
4763 if (ret > 0 && xterm_Shell != (Widget)0
|
|
4764 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
|
|
4765 {
|
|
4766 xterm_update(); /* Maybe we should hand out clipboard */
|
|
4767 /* continue looping when we only got the X event and the input
|
|
4768 * buffer is empty */
|
|
4769 if (--ret == 0 && !input_available())
|
|
4770 {
|
|
4771 /* Try again */
|
|
4772 finished = FALSE;
|
|
4773 }
|
|
4774 }
|
|
4775 # endif
|
|
4776 # ifdef FEAT_MOUSE_GPM
|
|
4777 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
|
|
4778 {
|
|
4779 if (FD_ISSET(gpm_fd, &efds))
|
|
4780 gpm_close();
|
|
4781 else if (FD_ISSET(gpm_fd, &rfds))
|
|
4782 *check_for_gpm = 1;
|
|
4783 }
|
|
4784 # endif
|
|
4785 # ifdef USE_XSMP
|
|
4786 if (ret > 0 && xsmp_icefd != -1)
|
|
4787 {
|
|
4788 if (FD_ISSET(xsmp_icefd, &efds))
|
|
4789 {
|
|
4790 if (p_verbose > 0)
|
292
|
4791 verb_msg((char_u *)_("XSMP lost ICE connection"));
|
7
|
4792 xsmp_close();
|
|
4793 if (--ret == 0)
|
|
4794 finished = FALSE; /* keep going if event was only one */
|
|
4795 }
|
|
4796 else if (FD_ISSET(xsmp_icefd, &rfds))
|
|
4797 {
|
|
4798 busy = TRUE;
|
|
4799 xsmp_handle_requests();
|
|
4800 busy = FALSE;
|
|
4801 if (--ret == 0)
|
|
4802 finished = FALSE; /* keep going if event was only one */
|
|
4803 }
|
|
4804 }
|
|
4805 # endif
|
|
4806
|
|
4807 #endif /* HAVE_SELECT */
|
|
4808
|
|
4809 #ifdef MAY_LOOP
|
|
4810 if (finished || msec == 0)
|
|
4811 break;
|
|
4812
|
|
4813 /* We're going to loop around again, find out for how long */
|
|
4814 if (msec > 0)
|
|
4815 {
|
|
4816 # ifdef USE_START_TV
|
|
4817 struct timeval mtv;
|
|
4818
|
|
4819 /* Compute remaining wait time. */
|
|
4820 gettimeofday(&mtv, NULL);
|
|
4821 msec -= (mtv.tv_sec - start_tv.tv_sec) * 1000L
|
|
4822 + (mtv.tv_usec - start_tv.tv_usec) / 1000L;
|
|
4823 # else
|
|
4824 /* Guess we got interrupted halfway. */
|
|
4825 msec = msec / 2;
|
|
4826 # endif
|
|
4827 if (msec <= 0)
|
|
4828 break; /* waited long enough */
|
|
4829 }
|
|
4830 #endif
|
|
4831 }
|
|
4832
|
|
4833 return (ret > 0);
|
|
4834 }
|
|
4835
|
|
4836 #ifndef VMS
|
|
4837
|
|
4838 #ifndef NO_EXPANDPATH
|
|
4839 /*
|
444
|
4840 * Expand a path into all matching files and/or directories. Handles "*",
|
|
4841 * "?", "[a-z]", "**", etc.
|
|
4842 * "path" has backslashes before chars that are not to be expanded.
|
|
4843 * Returns the number of matches found.
|
7
|
4844 */
|
|
4845 int
|
|
4846 mch_expandpath(gap, path, flags)
|
|
4847 garray_T *gap;
|
|
4848 char_u *path;
|
|
4849 int flags; /* EW_* flags */
|
|
4850 {
|
444
|
4851 return unix_expandpath(gap, path, 0, flags, FALSE);
|
7
|
4852 }
|
|
4853 #endif
|
|
4854
|
|
4855 /*
|
|
4856 * mch_expand_wildcards() - this code does wild-card pattern matching using
|
|
4857 * the shell
|
|
4858 *
|
|
4859 * return OK for success, FAIL for error (you may lose some memory) and put
|
|
4860 * an error message in *file.
|
|
4861 *
|
|
4862 * num_pat is number of input patterns
|
|
4863 * pat is array of pointers to input patterns
|
|
4864 * num_file is pointer to number of matched file names
|
|
4865 * file is pointer to array of pointers to matched file names
|
|
4866 */
|
|
4867
|
|
4868 #ifndef SEEK_SET
|
|
4869 # define SEEK_SET 0
|
|
4870 #endif
|
|
4871 #ifndef SEEK_END
|
|
4872 # define SEEK_END 2
|
|
4873 #endif
|
|
4874
|
644
|
4875 #define SHELL_SPECIAL (char_u *)"\t \"&';<>()\\|"
|
628
|
4876
|
7
|
4877 /* ARGSUSED */
|
|
4878 int
|
|
4879 mch_expand_wildcards(num_pat, pat, num_file, file, flags)
|
|
4880 int num_pat;
|
|
4881 char_u **pat;
|
|
4882 int *num_file;
|
|
4883 char_u ***file;
|
|
4884 int flags; /* EW_* flags */
|
|
4885 {
|
|
4886 int i;
|
|
4887 size_t len;
|
|
4888 char_u *p;
|
|
4889 int dir;
|
|
4890 #ifdef __EMX__
|
|
4891 # define EXPL_ALLOC_INC 16
|
|
4892 char_u **expl_files;
|
|
4893 size_t files_alloced, files_free;
|
|
4894 char_u *buf;
|
|
4895 int has_wildcard;
|
|
4896
|
|
4897 *num_file = 0; /* default: no files found */
|
|
4898 files_alloced = EXPL_ALLOC_INC; /* how much space is allocated */
|
|
4899 files_free = EXPL_ALLOC_INC; /* how much space is not used */
|
|
4900 *file = (char_u **)alloc(sizeof(char_u **) * files_alloced);
|
|
4901 if (*file == NULL)
|
|
4902 return FAIL;
|
|
4903
|
|
4904 for (; num_pat > 0; num_pat--, pat++)
|
|
4905 {
|
|
4906 expl_files = NULL;
|
|
4907 if (vim_strchr(*pat, '$') || vim_strchr(*pat, '~'))
|
|
4908 /* expand environment var or home dir */
|
|
4909 buf = expand_env_save(*pat);
|
|
4910 else
|
|
4911 buf = vim_strsave(*pat);
|
|
4912 expl_files = NULL;
|
99
|
4913 has_wildcard = mch_has_exp_wildcard(buf); /* (still) wildcards? */
|
7
|
4914 if (has_wildcard) /* yes, so expand them */
|
|
4915 expl_files = (char_u **)_fnexplode(buf);
|
|
4916
|
|
4917 /*
|
|
4918 * return value of buf if no wildcards left,
|
|
4919 * OR if no match AND EW_NOTFOUND is set.
|
|
4920 */
|
|
4921 if ((!has_wildcard && ((flags & EW_NOTFOUND) || mch_getperm(buf) >= 0))
|
|
4922 || (expl_files == NULL && (flags & EW_NOTFOUND)))
|
|
4923 { /* simply save the current contents of *buf */
|
|
4924 expl_files = (char_u **)alloc(sizeof(char_u **) * 2);
|
|
4925 if (expl_files != NULL)
|
|
4926 {
|
|
4927 expl_files[0] = vim_strsave(buf);
|
|
4928 expl_files[1] = NULL;
|
|
4929 }
|
|
4930 }
|
|
4931 vim_free(buf);
|
|
4932
|
|
4933 /*
|
|
4934 * Count number of names resulting from expansion,
|
|
4935 * At the same time add a backslash to the end of names that happen to
|
|
4936 * be directories, and replace slashes with backslashes.
|
|
4937 */
|
|
4938 if (expl_files)
|
|
4939 {
|
|
4940 for (i = 0; (p = expl_files[i]) != NULL; i++)
|
|
4941 {
|
|
4942 dir = mch_isdir(p);
|
|
4943 /* If we don't want dirs and this is one, skip it */
|
|
4944 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
|
|
4945 continue;
|
|
4946
|
715
|
4947 /* Skip files that are not executable if we check for that. */
|
|
4948 if (!dir && (flags & EW_EXEC) && !mch_can_exe(p))
|
|
4949 continue;
|
|
4950
|
7
|
4951 if (--files_free == 0)
|
|
4952 {
|
|
4953 /* need more room in table of pointers */
|
|
4954 files_alloced += EXPL_ALLOC_INC;
|
|
4955 *file = (char_u **)vim_realloc(*file,
|
|
4956 sizeof(char_u **) * files_alloced);
|
|
4957 if (*file == NULL)
|
|
4958 {
|
|
4959 EMSG(_(e_outofmem));
|
|
4960 *num_file = 0;
|
|
4961 return FAIL;
|
|
4962 }
|
|
4963 files_free = EXPL_ALLOC_INC;
|
|
4964 }
|
|
4965 slash_adjust(p);
|
|
4966 if (dir)
|
|
4967 {
|
|
4968 /* For a directory we add a '/', unless it's already
|
|
4969 * there. */
|
|
4970 len = STRLEN(p);
|
|
4971 if (((*file)[*num_file] = alloc(len + 2)) != NULL)
|
|
4972 {
|
|
4973 STRCPY((*file)[*num_file], p);
|
39
|
4974 if (!after_pathsep((*file)[*num_file] + len))
|
7
|
4975 {
|
|
4976 (*file)[*num_file][len] = psepc;
|
39
|
4977 (*file)[*num_file][len + 1] = NUL;
|
7
|
4978 }
|
|
4979 }
|
|
4980 }
|
|
4981 else
|
|
4982 {
|
|
4983 (*file)[*num_file] = vim_strsave(p);
|
|
4984 }
|
|
4985
|
|
4986 /*
|
|
4987 * Error message already given by either alloc or vim_strsave.
|
|
4988 * Should return FAIL, but returning OK works also.
|
|
4989 */
|
|
4990 if ((*file)[*num_file] == NULL)
|
|
4991 break;
|
|
4992 (*num_file)++;
|
|
4993 }
|
|
4994 _fnexplodefree((char **)expl_files);
|
|
4995 }
|
|
4996 }
|
|
4997 return OK;
|
|
4998
|
|
4999 #else /* __EMX__ */
|
|
5000
|
|
5001 int j;
|
|
5002 char_u *tempname;
|
|
5003 char_u *command;
|
|
5004 FILE *fd;
|
|
5005 char_u *buffer;
|
|
5006 #define STYLE_ECHO 0 /* use "echo" to expand */
|
|
5007 #define STYLE_GLOB 1 /* use "glob" to expand, for csh */
|
|
5008 #define STYLE_PRINT 2 /* use "print -N" to expand, for zsh */
|
|
5009 #define STYLE_BT 3 /* `cmd` expansion, execute the pattern directly */
|
|
5010 int shell_style = STYLE_ECHO;
|
|
5011 int check_spaces;
|
|
5012 static int did_find_nul = FALSE;
|
|
5013 int ampersent = FALSE;
|
|
5014
|
|
5015 *num_file = 0; /* default: no files found */
|
|
5016 *file = NULL;
|
|
5017
|
|
5018 /*
|
|
5019 * If there are no wildcards, just copy the names to allocated memory.
|
|
5020 * Saves a lot of time, because we don't have to start a new shell.
|
|
5021 */
|
|
5022 if (!have_wildcard(num_pat, pat))
|
|
5023 return save_patterns(num_pat, pat, num_file, file);
|
|
5024
|
428
|
5025 # ifdef HAVE_SANDBOX
|
|
5026 /* Don't allow any shell command in the sandbox. */
|
|
5027 if (sandbox != 0 && check_secure())
|
|
5028 return FAIL;
|
|
5029 # endif
|
|
5030
|
7
|
5031 /*
|
|
5032 * Don't allow the use of backticks in secure and restricted mode.
|
|
5033 */
|
428
|
5034 if (secure || restricted)
|
7
|
5035 for (i = 0; i < num_pat; ++i)
|
|
5036 if (vim_strchr(pat[i], '`') != NULL
|
|
5037 && (check_restricted() || check_secure()))
|
|
5038 return FAIL;
|
|
5039
|
|
5040 /*
|
|
5041 * get a name for the temp file
|
|
5042 */
|
|
5043 if ((tempname = vim_tempname('o')) == NULL)
|
|
5044 {
|
|
5045 EMSG(_(e_notmp));
|
|
5046 return FAIL;
|
|
5047 }
|
|
5048
|
|
5049 /*
|
|
5050 * Let the shell expand the patterns and write the result into the temp
|
|
5051 * file. if expanding `cmd` execute it directly.
|
|
5052 * If we use csh, glob will work better than echo.
|
|
5053 * If we use zsh, print -N will work better than glob.
|
|
5054 */
|
|
5055 if (num_pat == 1 && *pat[0] == '`'
|
|
5056 && (len = STRLEN(pat[0])) > 2
|
|
5057 && *(pat[0] + len - 1) == '`')
|
|
5058 shell_style = STYLE_BT;
|
|
5059 else if ((len = STRLEN(p_sh)) >= 3)
|
|
5060 {
|
|
5061 if (STRCMP(p_sh + len - 3, "csh") == 0)
|
|
5062 shell_style = STYLE_GLOB;
|
|
5063 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
|
|
5064 shell_style = STYLE_PRINT;
|
|
5065 }
|
|
5066
|
|
5067 /* "unset nonomatch; print -N >" plus two is 29 */
|
|
5068 len = STRLEN(tempname) + 29;
|
147
|
5069 for (i = 0; i < num_pat; ++i)
|
|
5070 {
|
|
5071 /* Count the length of the patterns in the same way as they are put in
|
|
5072 * "command" below. */
|
|
5073 #ifdef USE_SYSTEM
|
7
|
5074 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
|
147
|
5075 #else
|
|
5076 ++len; /* add space */
|
628
|
5077 for (j = 0; pat[i][j] != NUL; ++j)
|
|
5078 {
|
|
5079 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
|
|
5080 ++len; /* may add a backslash */
|
|
5081 ++len;
|
|
5082 }
|
147
|
5083 #endif
|
|
5084 }
|
7
|
5085 command = alloc(len);
|
|
5086 if (command == NULL)
|
|
5087 {
|
|
5088 /* out of memory */
|
|
5089 vim_free(tempname);
|
|
5090 return FAIL;
|
|
5091 }
|
|
5092
|
|
5093 /*
|
|
5094 * Build the shell command:
|
|
5095 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
|
|
5096 * recognizes this).
|
|
5097 * - Add the shell command to print the expanded names.
|
|
5098 * - Add the temp file name.
|
|
5099 * - Add the file name patterns.
|
|
5100 */
|
|
5101 if (shell_style == STYLE_BT)
|
|
5102 {
|
628
|
5103 /* change `command; command& ` to (command; command ) */
|
|
5104 STRCPY(command, "(");
|
|
5105 STRCAT(command, pat[0] + 1); /* exclude first backtick */
|
7
|
5106 p = command + STRLEN(command) - 1;
|
628
|
5107 *p-- = ')'; /* remove last backtick */
|
7
|
5108 while (p > command && vim_iswhite(*p))
|
|
5109 --p;
|
|
5110 if (*p == '&') /* remove trailing '&' */
|
|
5111 {
|
|
5112 ampersent = TRUE;
|
|
5113 *p = ' ';
|
|
5114 }
|
|
5115 STRCAT(command, ">");
|
|
5116 }
|
|
5117 else
|
|
5118 {
|
|
5119 if (flags & EW_NOTFOUND)
|
|
5120 STRCPY(command, "set nonomatch; ");
|
|
5121 else
|
|
5122 STRCPY(command, "unset nonomatch; ");
|
|
5123 if (shell_style == STYLE_GLOB)
|
|
5124 STRCAT(command, "glob >");
|
|
5125 else if (shell_style == STYLE_PRINT)
|
|
5126 STRCAT(command, "print -N >");
|
|
5127 else
|
|
5128 STRCAT(command, "echo >");
|
|
5129 }
|
|
5130 STRCAT(command, tempname);
|
|
5131 if (shell_style != STYLE_BT)
|
|
5132 for (i = 0; i < num_pat; ++i)
|
|
5133 {
|
|
5134 /* When using system() always add extra quotes, because the shell
|
628
|
5135 * is started twice. Otherwise put a backslash before special
|
|
5136 * characters, except insice ``. */
|
7
|
5137 #ifdef USE_SYSTEM
|
|
5138 STRCAT(command, " \"");
|
|
5139 STRCAT(command, pat[i]);
|
|
5140 STRCAT(command, "\"");
|
|
5141 #else
|
233
|
5142 int intick = FALSE;
|
|
5143
|
7
|
5144 p = command + STRLEN(command);
|
|
5145 *p++ = ' ';
|
628
|
5146 for (j = 0; pat[i][j] != NUL; ++j)
|
233
|
5147 {
|
|
5148 if (pat[i][j] == '`')
|
|
5149 intick = !intick;
|
628
|
5150 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
|
|
5151 {
|
644
|
5152 /* Remove a backslash, take char literally. But keep
|
652
|
5153 * backslash inside backticks, before a special character
|
|
5154 * and before a backtick. */
|
644
|
5155 if (intick
|
652
|
5156 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
|
|
5157 || pat[i][j + 1] == '`')
|
644
|
5158 *p++ = '\\';
|
648
|
5159 ++j;
|
628
|
5160 }
|
|
5161 else if (!intick && vim_strchr(SHELL_SPECIAL,
|
233
|
5162 pat[i][j]) != NULL)
|
628
|
5163 /* Put a backslash before a special character, but not
|
|
5164 * when inside ``. */
|
|
5165 *p++ = '\\';
|
648
|
5166
|
|
5167 /* Copy one character. */
|
|
5168 *p++ = pat[i][j];
|
233
|
5169 }
|
7
|
5170 *p = NUL;
|
|
5171 #endif
|
|
5172 }
|
|
5173 if (flags & EW_SILENT)
|
|
5174 show_shell_mess = FALSE;
|
|
5175 if (ampersent)
|
|
5176 STRCAT(command, "&"); /* put the '&' back after the
|
|
5177 redirection */
|
|
5178
|
|
5179 /*
|
|
5180 * Using zsh -G: If a pattern has no matches, it is just deleted from
|
|
5181 * the argument list, otherwise zsh gives an error message and doesn't
|
|
5182 * expand any other pattern.
|
|
5183 */
|
|
5184 if (shell_style == STYLE_PRINT)
|
|
5185 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
|
|
5186
|
|
5187 /*
|
|
5188 * If we use -f then shell variables set in .cshrc won't get expanded.
|
|
5189 * vi can do it, so we will too, but it is only necessary if there is a "$"
|
|
5190 * in one of the patterns, otherwise we can still use the fast option.
|
|
5191 */
|
|
5192 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
|
|
5193 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
|
|
5194
|
|
5195 /*
|
|
5196 * execute the shell command
|
|
5197 */
|
|
5198 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
|
|
5199
|
|
5200 /* When running in the background, give it some time to create the temp
|
|
5201 * file, but don't wait for it to finish. */
|
|
5202 if (ampersent)
|
|
5203 mch_delay(10L, TRUE);
|
|
5204
|
|
5205 extra_shell_arg = NULL; /* cleanup */
|
|
5206 show_shell_mess = TRUE;
|
|
5207 vim_free(command);
|
|
5208
|
|
5209 if (i) /* mch_call_shell() failed */
|
|
5210 {
|
|
5211 mch_remove(tempname);
|
|
5212 vim_free(tempname);
|
|
5213 /*
|
|
5214 * With interactive completion, the error message is not printed.
|
|
5215 * However with USE_SYSTEM, I don't know how to turn off error messages
|
|
5216 * from the shell, so screen may still get messed up -- webb.
|
|
5217 */
|
|
5218 #ifndef USE_SYSTEM
|
|
5219 if (!(flags & EW_SILENT))
|
|
5220 #endif
|
|
5221 {
|
|
5222 redraw_later_clear(); /* probably messed up screen */
|
|
5223 msg_putchar('\n'); /* clear bottom line quickly */
|
|
5224 cmdline_row = Rows - 1; /* continue on last line */
|
|
5225 #ifdef USE_SYSTEM
|
|
5226 if (!(flags & EW_SILENT))
|
|
5227 #endif
|
|
5228 {
|
|
5229 MSG(_(e_wildexpand));
|
|
5230 msg_start(); /* don't overwrite this message */
|
|
5231 }
|
|
5232 }
|
|
5233 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
|
|
5234 * EW_NOTFOUND is given */
|
|
5235 if (shell_style == STYLE_BT)
|
|
5236 return FAIL;
|
|
5237 goto notfound;
|
|
5238 }
|
|
5239
|
|
5240 /*
|
|
5241 * read the names from the file into memory
|
|
5242 */
|
|
5243 fd = fopen((char *)tempname, READBIN);
|
|
5244 if (fd == NULL)
|
|
5245 {
|
|
5246 /* Something went wrong, perhaps a file name with a special char. */
|
|
5247 if (!(flags & EW_SILENT))
|
|
5248 {
|
|
5249 MSG(_(e_wildexpand));
|
|
5250 msg_start(); /* don't overwrite this message */
|
|
5251 }
|
|
5252 vim_free(tempname);
|
|
5253 goto notfound;
|
|
5254 }
|
|
5255 fseek(fd, 0L, SEEK_END);
|
|
5256 len = ftell(fd); /* get size of temp file */
|
|
5257 fseek(fd, 0L, SEEK_SET);
|
|
5258 buffer = alloc(len + 1);
|
|
5259 if (buffer == NULL)
|
|
5260 {
|
|
5261 /* out of memory */
|
|
5262 mch_remove(tempname);
|
|
5263 vim_free(tempname);
|
|
5264 fclose(fd);
|
|
5265 return FAIL;
|
|
5266 }
|
|
5267 i = fread((char *)buffer, 1, len, fd);
|
|
5268 fclose(fd);
|
|
5269 mch_remove(tempname);
|
|
5270 if (i != len)
|
|
5271 {
|
|
5272 /* unexpected read error */
|
|
5273 EMSG2(_(e_notread), tempname);
|
|
5274 vim_free(tempname);
|
|
5275 vim_free(buffer);
|
|
5276 return FAIL;
|
|
5277 }
|
|
5278 vim_free(tempname);
|
|
5279
|
|
5280 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
|
|
5281 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
|
|
5282 p = buffer;
|
|
5283 for (i = 0; i < len; ++i)
|
|
5284 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
|
|
5285 *p++ = buffer[i];
|
|
5286 len = p - buffer;
|
|
5287 # endif
|
|
5288
|
|
5289
|
|
5290 /* file names are separated with Space */
|
|
5291 if (shell_style == STYLE_ECHO)
|
|
5292 {
|
|
5293 buffer[len] = '\n'; /* make sure the buffer ends in NL */
|
|
5294 p = buffer;
|
|
5295 for (i = 0; *p != '\n'; ++i) /* count number of entries */
|
|
5296 {
|
|
5297 while (*p != ' ' && *p != '\n')
|
|
5298 ++p;
|
|
5299 p = skipwhite(p); /* skip to next entry */
|
|
5300 }
|
|
5301 }
|
|
5302 /* file names are separated with NL */
|
|
5303 else if (shell_style == STYLE_BT)
|
|
5304 {
|
|
5305 buffer[len] = NUL; /* make sure the buffer ends in NUL */
|
|
5306 p = buffer;
|
|
5307 for (i = 0; *p != NUL; ++i) /* count number of entries */
|
|
5308 {
|
|
5309 while (*p != '\n' && *p != NUL)
|
|
5310 ++p;
|
|
5311 if (*p != NUL)
|
|
5312 ++p;
|
|
5313 p = skipwhite(p); /* skip leading white space */
|
|
5314 }
|
|
5315 }
|
|
5316 /* file names are separated with NUL */
|
|
5317 else
|
|
5318 {
|
|
5319 /*
|
|
5320 * Some versions of zsh use spaces instead of NULs to separate
|
|
5321 * results. Only do this when there is no NUL before the end of the
|
|
5322 * buffer, otherwise we would never be able to use file names with
|
|
5323 * embedded spaces when zsh does use NULs.
|
|
5324 * When we found a NUL once, we know zsh is OK, set did_find_nul and
|
|
5325 * don't check for spaces again.
|
|
5326 */
|
|
5327 check_spaces = FALSE;
|
|
5328 if (shell_style == STYLE_PRINT && !did_find_nul)
|
|
5329 {
|
|
5330 /* If there is a NUL, set did_find_nul, else set check_spaces */
|
|
5331 if (len && (int)STRLEN(buffer) < len - 1)
|
|
5332 did_find_nul = TRUE;
|
|
5333 else
|
|
5334 check_spaces = TRUE;
|
|
5335 }
|
|
5336
|
|
5337 /*
|
|
5338 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
|
|
5339 * already is one, for STYLE_GLOB it needs to be added.
|
|
5340 */
|
|
5341 if (len && buffer[len - 1] == NUL)
|
|
5342 --len;
|
|
5343 else
|
|
5344 buffer[len] = NUL;
|
|
5345 i = 0;
|
|
5346 for (p = buffer; p < buffer + len; ++p)
|
|
5347 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
|
|
5348 {
|
|
5349 ++i;
|
|
5350 *p = NUL;
|
|
5351 }
|
|
5352 if (len)
|
|
5353 ++i; /* count last entry */
|
|
5354 }
|
|
5355 if (i == 0)
|
|
5356 {
|
|
5357 /*
|
|
5358 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
|
|
5359 * /bin/sh will happily expand it to nothing rather than returning an
|
|
5360 * error; and hey, it's good to check anyway -- webb.
|
|
5361 */
|
|
5362 vim_free(buffer);
|
|
5363 goto notfound;
|
|
5364 }
|
|
5365 *num_file = i;
|
|
5366 *file = (char_u **)alloc(sizeof(char_u *) * i);
|
|
5367 if (*file == NULL)
|
|
5368 {
|
|
5369 /* out of memory */
|
|
5370 vim_free(buffer);
|
|
5371 return FAIL;
|
|
5372 }
|
|
5373
|
|
5374 /*
|
|
5375 * Isolate the individual file names.
|
|
5376 */
|
|
5377 p = buffer;
|
|
5378 for (i = 0; i < *num_file; ++i)
|
|
5379 {
|
|
5380 (*file)[i] = p;
|
|
5381 /* Space or NL separates */
|
|
5382 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT)
|
|
5383 {
|
652
|
5384 while (!(shell_style == STYLE_ECHO && *p == ' ')
|
|
5385 && *p != '\n' && *p != NUL)
|
7
|
5386 ++p;
|
|
5387 if (p == buffer + len) /* last entry */
|
|
5388 *p = NUL;
|
|
5389 else
|
|
5390 {
|
|
5391 *p++ = NUL;
|
|
5392 p = skipwhite(p); /* skip to next entry */
|
|
5393 }
|
|
5394 }
|
|
5395 else /* NUL separates */
|
|
5396 {
|
|
5397 while (*p && p < buffer + len) /* skip entry */
|
|
5398 ++p;
|
|
5399 ++p; /* skip NUL */
|
|
5400 }
|
|
5401 }
|
|
5402
|
|
5403 /*
|
|
5404 * Move the file names to allocated memory.
|
|
5405 */
|
|
5406 for (j = 0, i = 0; i < *num_file; ++i)
|
|
5407 {
|
|
5408 /* Require the files to exist. Helps when using /bin/sh */
|
|
5409 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
|
|
5410 continue;
|
|
5411
|
|
5412 /* check if this entry should be included */
|
|
5413 dir = (mch_isdir((*file)[i]));
|
|
5414 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
|
|
5415 continue;
|
|
5416
|
715
|
5417 /* Skip files that are not executable if we check for that. */
|
|
5418 if (!dir && (flags & EW_EXEC) && !mch_can_exe((*file)[i]))
|
|
5419 continue;
|
|
5420
|
7
|
5421 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
|
|
5422 if (p)
|
|
5423 {
|
|
5424 STRCPY(p, (*file)[i]);
|
|
5425 if (dir)
|
|
5426 STRCAT(p, "/"); /* add '/' to a directory name */
|
|
5427 (*file)[j++] = p;
|
|
5428 }
|
|
5429 }
|
|
5430 vim_free(buffer);
|
|
5431 *num_file = j;
|
|
5432
|
|
5433 if (*num_file == 0) /* rejected all entries */
|
|
5434 {
|
|
5435 vim_free(*file);
|
|
5436 *file = NULL;
|
|
5437 goto notfound;
|
|
5438 }
|
|
5439
|
|
5440 return OK;
|
|
5441
|
|
5442 notfound:
|
|
5443 if (flags & EW_NOTFOUND)
|
|
5444 return save_patterns(num_pat, pat, num_file, file);
|
|
5445 return FAIL;
|
|
5446
|
|
5447 #endif /* __EMX__ */
|
|
5448 }
|
|
5449
|
|
5450 #endif /* VMS */
|
|
5451
|
|
5452 #ifndef __EMX__
|
|
5453 static int
|
|
5454 save_patterns(num_pat, pat, num_file, file)
|
|
5455 int num_pat;
|
|
5456 char_u **pat;
|
|
5457 int *num_file;
|
|
5458 char_u ***file;
|
|
5459 {
|
|
5460 int i;
|
99
|
5461 char_u *s;
|
7
|
5462
|
|
5463 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
|
|
5464 if (*file == NULL)
|
|
5465 return FAIL;
|
|
5466 for (i = 0; i < num_pat; i++)
|
99
|
5467 {
|
|
5468 s = vim_strsave(pat[i]);
|
|
5469 if (s != NULL)
|
|
5470 /* Be compatible with expand_filename(): halve the number of
|
|
5471 * backslashes. */
|
|
5472 backslash_halve(s);
|
|
5473 (*file)[i] = s;
|
|
5474 }
|
7
|
5475 *num_file = num_pat;
|
|
5476 return OK;
|
|
5477 }
|
|
5478 #endif
|
|
5479
|
|
5480
|
|
5481 /*
|
|
5482 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
|
|
5483 * expand.
|
|
5484 */
|
|
5485 int
|
|
5486 mch_has_exp_wildcard(p)
|
|
5487 char_u *p;
|
|
5488 {
|
39
|
5489 for ( ; *p; mb_ptr_adv(p))
|
7
|
5490 {
|
|
5491 #ifndef OS2
|
|
5492 if (*p == '\\' && p[1] != NUL)
|
|
5493 ++p;
|
|
5494 else
|
|
5495 #endif
|
|
5496 if (vim_strchr((char_u *)
|
|
5497 #ifdef VMS
|
|
5498 "*?%"
|
|
5499 #else
|
|
5500 # ifdef OS2
|
|
5501 "*?"
|
|
5502 # else
|
|
5503 "*?[{'"
|
|
5504 # endif
|
|
5505 #endif
|
|
5506 , *p) != NULL)
|
|
5507 return TRUE;
|
|
5508 }
|
|
5509 return FALSE;
|
|
5510 }
|
|
5511
|
|
5512 /*
|
|
5513 * Return TRUE if the string "p" contains a wildcard.
|
|
5514 * Don't recognize '~' at the end as a wildcard.
|
|
5515 */
|
|
5516 int
|
|
5517 mch_has_wildcard(p)
|
|
5518 char_u *p;
|
|
5519 {
|
39
|
5520 for ( ; *p; mb_ptr_adv(p))
|
7
|
5521 {
|
|
5522 #ifndef OS2
|
|
5523 if (*p == '\\' && p[1] != NUL)
|
|
5524 ++p;
|
|
5525 else
|
|
5526 #endif
|
|
5527 if (vim_strchr((char_u *)
|
|
5528 #ifdef VMS
|
|
5529 "*?%$"
|
|
5530 #else
|
|
5531 # ifdef OS2
|
|
5532 # ifdef VIM_BACKTICK
|
|
5533 "*?$`"
|
|
5534 # else
|
|
5535 "*?$"
|
|
5536 # endif
|
|
5537 # else
|
|
5538 "*?[{`'$"
|
|
5539 # endif
|
|
5540 #endif
|
|
5541 , *p) != NULL
|
|
5542 || (*p == '~' && p[1] != NUL))
|
|
5543 return TRUE;
|
|
5544 }
|
|
5545 return FALSE;
|
|
5546 }
|
|
5547
|
|
5548 #ifndef __EMX__
|
|
5549 static int
|
|
5550 have_wildcard(num, file)
|
|
5551 int num;
|
|
5552 char_u **file;
|
|
5553 {
|
|
5554 int i;
|
|
5555
|
|
5556 for (i = 0; i < num; i++)
|
|
5557 if (mch_has_wildcard(file[i]))
|
|
5558 return 1;
|
|
5559 return 0;
|
|
5560 }
|
|
5561
|
|
5562 static int
|
|
5563 have_dollars(num, file)
|
|
5564 int num;
|
|
5565 char_u **file;
|
|
5566 {
|
|
5567 int i;
|
|
5568
|
|
5569 for (i = 0; i < num; i++)
|
|
5570 if (vim_strchr(file[i], '$') != NULL)
|
|
5571 return TRUE;
|
|
5572 return FALSE;
|
|
5573 }
|
|
5574 #endif /* ifndef __EMX__ */
|
|
5575
|
|
5576 #ifndef HAVE_RENAME
|
|
5577 /*
|
|
5578 * Scaled-down version of rename(), which is missing in Xenix.
|
|
5579 * This version can only move regular files and will fail if the
|
|
5580 * destination exists.
|
|
5581 */
|
|
5582 int
|
|
5583 mch_rename(src, dest)
|
|
5584 const char *src, *dest;
|
|
5585 {
|
|
5586 struct stat st;
|
|
5587
|
|
5588 if (stat(dest, &st) >= 0) /* fail if destination exists */
|
|
5589 return -1;
|
|
5590 if (link(src, dest) != 0) /* link file to new name */
|
|
5591 return -1;
|
|
5592 if (mch_remove(src) == 0) /* delete link to old name */
|
|
5593 return 0;
|
|
5594 return -1;
|
|
5595 }
|
|
5596 #endif /* !HAVE_RENAME */
|
|
5597
|
|
5598 #ifdef FEAT_MOUSE_GPM
|
|
5599 /*
|
|
5600 * Initializes connection with gpm (if it isn't already opened)
|
|
5601 * Return 1 if succeeded (or connection already opened), 0 if failed
|
|
5602 */
|
|
5603 static int
|
|
5604 gpm_open()
|
|
5605 {
|
|
5606 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
|
|
5607
|
|
5608 if (!gpm_flag)
|
|
5609 {
|
|
5610 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
|
|
5611 gpm_connect.defaultMask = ~GPM_HARD;
|
|
5612 /* Default handling for mouse move*/
|
|
5613 gpm_connect.minMod = 0; /* Handle any modifier keys */
|
|
5614 gpm_connect.maxMod = 0xffff;
|
|
5615 if (Gpm_Open(&gpm_connect, 0) > 0)
|
|
5616 {
|
|
5617 /* gpm library tries to handling TSTP causes
|
|
5618 * problems. Anyways, we close connection to Gpm whenever
|
|
5619 * we are going to suspend or starting an external process
|
|
5620 * so we should'nt have problem with this
|
|
5621 */
|
|
5622 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
|
|
5623 return 1; /* succeed */
|
|
5624 }
|
|
5625 if (gpm_fd == -2)
|
|
5626 Gpm_Close(); /* We don't want to talk to xterm via gpm */
|
|
5627 return 0;
|
|
5628 }
|
|
5629 return 1; /* already open */
|
|
5630 }
|
|
5631
|
|
5632 /*
|
|
5633 * Closes connection to gpm
|
|
5634 * returns non-zero if connection succesfully closed
|
|
5635 */
|
|
5636 static void
|
|
5637 gpm_close()
|
|
5638 {
|
|
5639 if (gpm_flag && gpm_fd >= 0) /* if Open */
|
|
5640 Gpm_Close();
|
|
5641 }
|
|
5642
|
|
5643 /* Reads gpm event and adds special keys to input buf. Returns length of
|
|
5644 * generated key sequence.
|
|
5645 * This function is made after gui_send_mouse_event
|
|
5646 */
|
|
5647 static int
|
|
5648 mch_gpm_process()
|
|
5649 {
|
|
5650 int button;
|
|
5651 static Gpm_Event gpm_event;
|
|
5652 char_u string[6];
|
|
5653 int_u vim_modifiers;
|
|
5654 int row,col;
|
|
5655 unsigned char buttons_mask;
|
|
5656 unsigned char gpm_modifiers;
|
|
5657 static unsigned char old_buttons = 0;
|
|
5658
|
|
5659 Gpm_GetEvent(&gpm_event);
|
|
5660
|
|
5661 #ifdef FEAT_GUI
|
|
5662 /* Don't put events in the input queue now. */
|
|
5663 if (hold_gui_events)
|
|
5664 return 0;
|
|
5665 #endif
|
|
5666
|
|
5667 row = gpm_event.y - 1;
|
|
5668 col = gpm_event.x - 1;
|
|
5669
|
|
5670 string[0] = ESC; /* Our termcode */
|
|
5671 string[1] = 'M';
|
|
5672 string[2] = 'G';
|
|
5673 switch (GPM_BARE_EVENTS(gpm_event.type))
|
|
5674 {
|
|
5675 case GPM_DRAG:
|
|
5676 string[3] = MOUSE_DRAG;
|
|
5677 break;
|
|
5678 case GPM_DOWN:
|
|
5679 buttons_mask = gpm_event.buttons & ~old_buttons;
|
|
5680 old_buttons = gpm_event.buttons;
|
|
5681 switch (buttons_mask)
|
|
5682 {
|
|
5683 case GPM_B_LEFT:
|
|
5684 button = MOUSE_LEFT;
|
|
5685 break;
|
|
5686 case GPM_B_MIDDLE:
|
|
5687 button = MOUSE_MIDDLE;
|
|
5688 break;
|
|
5689 case GPM_B_RIGHT:
|
|
5690 button = MOUSE_RIGHT;
|
|
5691 break;
|
|
5692 default:
|
|
5693 return 0;
|
|
5694 /*Don't know what to do. Can more than one button be
|
|
5695 * reported in one event? */
|
|
5696 }
|
|
5697 string[3] = (char_u)(button | 0x20);
|
|
5698 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
|
|
5699 break;
|
|
5700 case GPM_UP:
|
|
5701 string[3] = MOUSE_RELEASE;
|
|
5702 old_buttons &= ~gpm_event.buttons;
|
|
5703 break;
|
|
5704 default:
|
|
5705 return 0;
|
|
5706 }
|
|
5707 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
|
|
5708 gpm_modifiers = gpm_event.modifiers;
|
|
5709 vim_modifiers = 0x0;
|
|
5710 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
|
|
5711 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
|
|
5712 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
|
|
5713 */
|
|
5714 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
|
|
5715 vim_modifiers |= MOUSE_SHIFT;
|
|
5716
|
|
5717 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
|
|
5718 vim_modifiers |= MOUSE_CTRL;
|
|
5719 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
|
|
5720 vim_modifiers |= MOUSE_ALT;
|
|
5721 string[3] |= vim_modifiers;
|
|
5722 string[4] = (char_u)(col + ' ' + 1);
|
|
5723 string[5] = (char_u)(row + ' ' + 1);
|
|
5724 add_to_input_buf(string, 6);
|
|
5725 return 6;
|
|
5726 }
|
|
5727 #endif /* FEAT_MOUSE_GPM */
|
|
5728
|
|
5729 #if defined(FEAT_LIBCALL) || defined(PROTO)
|
|
5730 typedef char_u * (*STRPROCSTR)__ARGS((char_u *));
|
|
5731 typedef char_u * (*INTPROCSTR)__ARGS((int));
|
|
5732 typedef int (*STRPROCINT)__ARGS((char_u *));
|
|
5733 typedef int (*INTPROCINT)__ARGS((int));
|
|
5734
|
|
5735 /*
|
|
5736 * Call a DLL routine which takes either a string or int param
|
|
5737 * and returns an allocated string.
|
|
5738 */
|
|
5739 int
|
|
5740 mch_libcall(libname, funcname, argstring, argint, string_result, number_result)
|
|
5741 char_u *libname;
|
|
5742 char_u *funcname;
|
|
5743 char_u *argstring; /* NULL when using a argint */
|
|
5744 int argint;
|
|
5745 char_u **string_result;/* NULL when using number_result */
|
|
5746 int *number_result;
|
|
5747 {
|
|
5748 # if defined(USE_DLOPEN)
|
|
5749 void *hinstLib;
|
12
|
5750 char *dlerr = NULL;
|
7
|
5751 # else
|
|
5752 shl_t hinstLib;
|
|
5753 # endif
|
|
5754 STRPROCSTR ProcAdd;
|
|
5755 INTPROCSTR ProcAddI;
|
|
5756 char_u *retval_str = NULL;
|
|
5757 int retval_int = 0;
|
|
5758 int success = FALSE;
|
|
5759
|
|
5760 /* Get a handle to the DLL module. */
|
|
5761 # if defined(USE_DLOPEN)
|
|
5762 hinstLib = dlopen((char *)libname, RTLD_LAZY
|
|
5763 # ifdef RTLD_LOCAL
|
|
5764 | RTLD_LOCAL
|
|
5765 # endif
|
|
5766 );
|
12
|
5767 if (hinstLib == NULL)
|
|
5768 {
|
|
5769 /* "dlerr" must be used before dlclose() */
|
|
5770 dlerr = (char *)dlerror();
|
|
5771 if (dlerr != NULL)
|
|
5772 EMSG2(_("dlerror = \"%s\""), dlerr);
|
|
5773 }
|
7
|
5774 # else
|
|
5775 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
|
|
5776 # endif
|
|
5777
|
|
5778 /* If the handle is valid, try to get the function address. */
|
|
5779 if (hinstLib != NULL)
|
|
5780 {
|
|
5781 # ifdef HAVE_SETJMP_H
|
|
5782 /*
|
|
5783 * Catch a crash when calling the library function. For example when
|
|
5784 * using a number where a string pointer is expected.
|
|
5785 */
|
|
5786 mch_startjmp();
|
|
5787 if (SETJMP(lc_jump_env) != 0)
|
|
5788 {
|
|
5789 success = FALSE;
|
12
|
5790 dlerr = NULL;
|
7
|
5791 mch_didjmp();
|
|
5792 }
|
|
5793 else
|
|
5794 # endif
|
|
5795 {
|
|
5796 retval_str = NULL;
|
|
5797 retval_int = 0;
|
|
5798
|
|
5799 if (argstring != NULL)
|
|
5800 {
|
|
5801 # if defined(USE_DLOPEN)
|
|
5802 ProcAdd = (STRPROCSTR)dlsym(hinstLib, (const char *)funcname);
|
12
|
5803 dlerr = (char *)dlerror();
|
7
|
5804 # else
|
|
5805 if (shl_findsym(&hinstLib, (const char *)funcname,
|
|
5806 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
|
|
5807 ProcAdd = NULL;
|
|
5808 # endif
|
12
|
5809 if ((success = (ProcAdd != NULL
|
|
5810 # if defined(USE_DLOPEN)
|
|
5811 && dlerr == NULL
|
|
5812 # endif
|
|
5813 )))
|
7
|
5814 {
|
|
5815 if (string_result == NULL)
|
|
5816 retval_int = ((STRPROCINT)ProcAdd)(argstring);
|
|
5817 else
|
|
5818 retval_str = (ProcAdd)(argstring);
|
|
5819 }
|
|
5820 }
|
|
5821 else
|
|
5822 {
|
|
5823 # if defined(USE_DLOPEN)
|
|
5824 ProcAddI = (INTPROCSTR)dlsym(hinstLib, (const char *)funcname);
|
12
|
5825 dlerr = (char *)dlerror();
|
7
|
5826 # else
|
|
5827 if (shl_findsym(&hinstLib, (const char *)funcname,
|
|
5828 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
|
|
5829 ProcAddI = NULL;
|
|
5830 # endif
|
12
|
5831 if ((success = (ProcAddI != NULL
|
|
5832 # if defined(USE_DLOPEN)
|
|
5833 && dlerr == NULL
|
|
5834 # endif
|
|
5835 )))
|
7
|
5836 {
|
|
5837 if (string_result == NULL)
|
|
5838 retval_int = ((INTPROCINT)ProcAddI)(argint);
|
|
5839 else
|
|
5840 retval_str = (ProcAddI)(argint);
|
|
5841 }
|
|
5842 }
|
|
5843
|
|
5844 /* Save the string before we free the library. */
|
|
5845 /* Assume that a "1" or "-1" result is an illegal pointer. */
|
|
5846 if (string_result == NULL)
|
|
5847 *number_result = retval_int;
|
|
5848 else if (retval_str != NULL
|
|
5849 && retval_str != (char_u *)1
|
|
5850 && retval_str != (char_u *)-1)
|
|
5851 *string_result = vim_strsave(retval_str);
|
|
5852 }
|
|
5853
|
|
5854 # ifdef HAVE_SETJMP_H
|
|
5855 mch_endjmp();
|
|
5856 # ifdef SIGHASARG
|
|
5857 if (lc_signal != 0)
|
|
5858 {
|
|
5859 int i;
|
|
5860
|
|
5861 /* try to find the name of this signal */
|
|
5862 for (i = 0; signal_info[i].sig != -1; i++)
|
|
5863 if (lc_signal == signal_info[i].sig)
|
|
5864 break;
|
|
5865 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
|
|
5866 }
|
|
5867 # endif
|
|
5868 # endif
|
|
5869
|
12
|
5870 # if defined(USE_DLOPEN)
|
|
5871 /* "dlerr" must be used before dlclose() */
|
|
5872 if (dlerr != NULL)
|
|
5873 EMSG2(_("dlerror = \"%s\""), dlerr);
|
|
5874
|
7
|
5875 /* Free the DLL module. */
|
|
5876 (void)dlclose(hinstLib);
|
|
5877 # else
|
|
5878 (void)shl_unload(hinstLib);
|
|
5879 # endif
|
|
5880 }
|
|
5881
|
|
5882 if (!success)
|
|
5883 {
|
|
5884 EMSG2(_(e_libcall), funcname);
|
|
5885 return FAIL;
|
|
5886 }
|
|
5887
|
|
5888 return OK;
|
|
5889 }
|
|
5890 #endif
|
|
5891
|
|
5892 #if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
|
|
5893 static int xterm_trace = -1; /* default: disabled */
|
|
5894 static int xterm_button;
|
|
5895
|
|
5896 /*
|
|
5897 * Setup a dummy window for X selections in a terminal.
|
|
5898 */
|
|
5899 void
|
|
5900 setup_term_clip()
|
|
5901 {
|
|
5902 int z = 0;
|
|
5903 char *strp = "";
|
|
5904 Widget AppShell;
|
|
5905
|
|
5906 if (!x_connect_to_server())
|
|
5907 return;
|
|
5908
|
|
5909 open_app_context();
|
|
5910 if (app_context != NULL && xterm_Shell == (Widget)0)
|
|
5911 {
|
|
5912 int (*oldhandler)();
|
|
5913 #if defined(HAVE_SETJMP_H)
|
|
5914 int (*oldIOhandler)();
|
|
5915 #endif
|
|
5916 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
|
|
5917 struct timeval start_tv;
|
|
5918
|
|
5919 if (p_verbose > 0)
|
|
5920 gettimeofday(&start_tv, NULL);
|
|
5921 # endif
|
|
5922
|
|
5923 /* Ignore X errors while opening the display */
|
|
5924 oldhandler = XSetErrorHandler(x_error_check);
|
|
5925
|
|
5926 #if defined(HAVE_SETJMP_H)
|
|
5927 /* Ignore X IO errors while opening the display */
|
|
5928 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
|
|
5929 mch_startjmp();
|
|
5930 if (SETJMP(lc_jump_env) != 0)
|
|
5931 {
|
|
5932 mch_didjmp();
|
|
5933 xterm_dpy = NULL;
|
|
5934 }
|
|
5935 else
|
|
5936 #endif
|
|
5937 {
|
|
5938 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
|
|
5939 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
|
|
5940 #if defined(HAVE_SETJMP_H)
|
|
5941 mch_endjmp();
|
|
5942 #endif
|
|
5943 }
|
|
5944
|
|
5945 #if defined(HAVE_SETJMP_H)
|
|
5946 /* Now handle X IO errors normally. */
|
|
5947 (void)XSetIOErrorHandler(oldIOhandler);
|
|
5948 #endif
|
|
5949 /* Now handle X errors normally. */
|
|
5950 (void)XSetErrorHandler(oldhandler);
|
|
5951
|
|
5952 if (xterm_dpy == NULL)
|
|
5953 {
|
|
5954 if (p_verbose > 0)
|
292
|
5955 verb_msg((char_u *)_("Opening the X display failed"));
|
7
|
5956 return;
|
|
5957 }
|
|
5958
|
|
5959 /* Catch terminating error of the X server connection. */
|
|
5960 (void)XSetIOErrorHandler(x_IOerror_handler);
|
|
5961
|
|
5962 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
|
|
5963 if (p_verbose > 0)
|
292
|
5964 {
|
|
5965 verbose_enter();
|
7
|
5966 xopen_message(&start_tv);
|
292
|
5967 verbose_leave();
|
|
5968 }
|
7
|
5969 # endif
|
|
5970
|
|
5971 /* Create a Shell to make converters work. */
|
|
5972 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
|
|
5973 applicationShellWidgetClass, xterm_dpy,
|
|
5974 NULL);
|
|
5975 if (AppShell == (Widget)0)
|
|
5976 return;
|
|
5977 xterm_Shell = XtVaCreatePopupShell("VIM",
|
|
5978 topLevelShellWidgetClass, AppShell,
|
|
5979 XtNmappedWhenManaged, 0,
|
|
5980 XtNwidth, 1,
|
|
5981 XtNheight, 1,
|
|
5982 NULL);
|
|
5983 if (xterm_Shell == (Widget)0)
|
|
5984 return;
|
|
5985
|
|
5986 x11_setup_atoms(xterm_dpy);
|
|
5987 if (x11_display == NULL)
|
|
5988 x11_display = xterm_dpy;
|
|
5989
|
|
5990 XtRealizeWidget(xterm_Shell);
|
|
5991 XSync(xterm_dpy, False);
|
|
5992 xterm_update();
|
|
5993 }
|
|
5994 if (xterm_Shell != (Widget)0)
|
|
5995 {
|
|
5996 clip_init(TRUE);
|
|
5997 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
|
|
5998 x11_window = (Window)atol(strp);
|
|
5999 /* Check if $WINDOWID is valid. */
|
|
6000 if (test_x11_window(xterm_dpy) == FAIL)
|
|
6001 x11_window = 0;
|
|
6002 if (x11_window != 0)
|
|
6003 xterm_trace = 0;
|
|
6004 }
|
|
6005 }
|
|
6006
|
|
6007 void
|
|
6008 start_xterm_trace(button)
|
|
6009 int button;
|
|
6010 {
|
|
6011 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
|
|
6012 return;
|
|
6013 xterm_trace = 1;
|
|
6014 xterm_button = button;
|
|
6015 do_xterm_trace();
|
|
6016 }
|
|
6017
|
|
6018
|
|
6019 void
|
|
6020 stop_xterm_trace()
|
|
6021 {
|
|
6022 if (xterm_trace < 0)
|
|
6023 return;
|
|
6024 xterm_trace = 0;
|
|
6025 }
|
|
6026
|
|
6027 /*
|
|
6028 * Query the xterm pointer and generate mouse termcodes if necessary
|
|
6029 * return TRUE if dragging is active, else FALSE
|
|
6030 */
|
|
6031 static int
|
|
6032 do_xterm_trace()
|
|
6033 {
|
|
6034 Window root, child;
|
|
6035 int root_x, root_y;
|
|
6036 int win_x, win_y;
|
|
6037 int row, col;
|
|
6038 int_u mask_return;
|
|
6039 char_u buf[50];
|
|
6040 char_u *strp;
|
|
6041 long got_hints;
|
|
6042 static char_u *mouse_code;
|
|
6043 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
|
|
6044 static int prev_row = 0, prev_col = 0;
|
|
6045 static XSizeHints xterm_hints;
|
|
6046
|
|
6047 if (xterm_trace <= 0)
|
|
6048 return FALSE;
|
|
6049
|
|
6050 if (xterm_trace == 1)
|
|
6051 {
|
|
6052 /* Get the hints just before tracking starts. The font size might
|
|
6053 * have changed recently */
|
|
6054 XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints);
|
|
6055 if (!(got_hints & PResizeInc)
|
|
6056 || xterm_hints.width_inc <= 1
|
|
6057 || xterm_hints.height_inc <= 1)
|
|
6058 {
|
|
6059 xterm_trace = -1; /* Not enough data -- disable tracing */
|
|
6060 return FALSE;
|
|
6061 }
|
|
6062
|
|
6063 /* Rely on the same mouse code for the duration of this */
|
|
6064 mouse_code = find_termcode(mouse_name);
|
|
6065 prev_row = mouse_row;
|
|
6066 prev_row = mouse_col;
|
|
6067 xterm_trace = 2;
|
|
6068
|
|
6069 /* Find the offset of the chars, there might be a scrollbar on the
|
|
6070 * left of the window and/or a menu on the top (eterm etc.) */
|
|
6071 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
|
|
6072 &win_x, &win_y, &mask_return);
|
|
6073 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
|
|
6074 - (xterm_hints.height_inc / 2);
|
|
6075 if (xterm_hints.y <= xterm_hints.height_inc / 2)
|
|
6076 xterm_hints.y = 2;
|
|
6077 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
|
|
6078 - (xterm_hints.width_inc / 2);
|
|
6079 if (xterm_hints.x <= xterm_hints.width_inc / 2)
|
|
6080 xterm_hints.x = 2;
|
|
6081 return TRUE;
|
|
6082 }
|
|
6083 if (mouse_code == NULL)
|
|
6084 {
|
|
6085 xterm_trace = 0;
|
|
6086 return FALSE;
|
|
6087 }
|
|
6088
|
|
6089 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
|
|
6090 &win_x, &win_y, &mask_return);
|
|
6091
|
|
6092 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
|
|
6093 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
|
|
6094 if (row == prev_row && col == prev_col)
|
|
6095 return TRUE;
|
|
6096
|
|
6097 STRCPY(buf, mouse_code);
|
|
6098 strp = buf + STRLEN(buf);
|
|
6099 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
|
|
6100 *strp++ = (char_u)(col + ' ' + 1);
|
|
6101 *strp++ = (char_u)(row + ' ' + 1);
|
|
6102 *strp = 0;
|
|
6103 add_to_input_buf(buf, STRLEN(buf));
|
|
6104
|
|
6105 prev_row = row;
|
|
6106 prev_col = col;
|
|
6107 return TRUE;
|
|
6108 }
|
|
6109
|
|
6110 # if defined(FEAT_GUI) || defined(PROTO)
|
|
6111 /*
|
|
6112 * Destroy the display, window and app_context. Required for GTK.
|
|
6113 */
|
|
6114 void
|
|
6115 clear_xterm_clip()
|
|
6116 {
|
|
6117 if (xterm_Shell != (Widget)0)
|
|
6118 {
|
|
6119 XtDestroyWidget(xterm_Shell);
|
|
6120 xterm_Shell = (Widget)0;
|
|
6121 }
|
|
6122 if (xterm_dpy != NULL)
|
|
6123 {
|
|
6124 #if 0
|
|
6125 /* Lesstif and Solaris crash here, lose some memory */
|
|
6126 XtCloseDisplay(xterm_dpy);
|
|
6127 #endif
|
|
6128 if (x11_display == xterm_dpy)
|
|
6129 x11_display = NULL;
|
|
6130 xterm_dpy = NULL;
|
|
6131 }
|
|
6132 #if 0
|
|
6133 if (app_context != (XtAppContext)NULL)
|
|
6134 {
|
|
6135 /* Lesstif and Solaris crash here, lose some memory */
|
|
6136 XtDestroyApplicationContext(app_context);
|
|
6137 app_context = (XtAppContext)NULL;
|
|
6138 }
|
|
6139 #endif
|
|
6140 }
|
|
6141 # endif
|
|
6142
|
|
6143 /*
|
|
6144 * Catch up with any queued X events. This may put keyboard input into the
|
|
6145 * input buffer, call resize call-backs, trigger timers etc. If there is
|
|
6146 * nothing in the X event queue (& no timers pending), then we return
|
|
6147 * immediately.
|
|
6148 */
|
|
6149 static void
|
|
6150 xterm_update()
|
|
6151 {
|
|
6152 XEvent event;
|
|
6153
|
|
6154 while (XtAppPending(app_context) && !vim_is_input_buf_full())
|
|
6155 {
|
|
6156 XtAppNextEvent(app_context, &event);
|
|
6157 #ifdef FEAT_CLIENTSERVER
|
|
6158 {
|
|
6159 XPropertyEvent *e = (XPropertyEvent *)&event;
|
|
6160
|
|
6161 if (e->type == PropertyNotify && e->window == commWindow
|
|
6162 && e->atom == commProperty && e->state == PropertyNewValue)
|
|
6163 serverEventProc(xterm_dpy, &event);
|
|
6164 }
|
|
6165 #endif
|
|
6166 XtDispatchEvent(&event);
|
|
6167 }
|
|
6168 }
|
|
6169
|
|
6170 int
|
|
6171 clip_xterm_own_selection(cbd)
|
|
6172 VimClipboard *cbd;
|
|
6173 {
|
|
6174 if (xterm_Shell != (Widget)0)
|
|
6175 return clip_x11_own_selection(xterm_Shell, cbd);
|
|
6176 return FAIL;
|
|
6177 }
|
|
6178
|
|
6179 void
|
|
6180 clip_xterm_lose_selection(cbd)
|
|
6181 VimClipboard *cbd;
|
|
6182 {
|
|
6183 if (xterm_Shell != (Widget)0)
|
|
6184 clip_x11_lose_selection(xterm_Shell, cbd);
|
|
6185 }
|
|
6186
|
|
6187 void
|
|
6188 clip_xterm_request_selection(cbd)
|
|
6189 VimClipboard *cbd;
|
|
6190 {
|
|
6191 if (xterm_Shell != (Widget)0)
|
|
6192 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
|
|
6193 }
|
|
6194
|
|
6195 void
|
|
6196 clip_xterm_set_selection(cbd)
|
|
6197 VimClipboard *cbd;
|
|
6198 {
|
|
6199 clip_x11_set_selection(cbd);
|
|
6200 }
|
|
6201 #endif
|
|
6202
|
|
6203
|
|
6204 #if defined(USE_XSMP) || defined(PROTO)
|
|
6205 /*
|
|
6206 * Code for X Session Management Protocol.
|
|
6207 */
|
|
6208 static void xsmp_handle_save_yourself __ARGS((SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast));
|
|
6209 static void xsmp_die __ARGS((SmcConn smc_conn, SmPointer client_data));
|
|
6210 static void xsmp_save_complete __ARGS((SmcConn smc_conn, SmPointer client_data));
|
|
6211 static void xsmp_shutdown_cancelled __ARGS((SmcConn smc_conn, SmPointer client_data));
|
|
6212 static void xsmp_ice_connection __ARGS((IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData));
|
|
6213
|
|
6214
|
|
6215 # if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
|
|
6216 static void xsmp_handle_interaction __ARGS((SmcConn smc_conn, SmPointer client_data));
|
|
6217
|
|
6218 /*
|
|
6219 * This is our chance to ask the user if they want to save,
|
|
6220 * or abort the logout
|
|
6221 */
|
|
6222 /*ARGSUSED*/
|
|
6223 static void
|
|
6224 xsmp_handle_interaction(smc_conn, client_data)
|
|
6225 SmcConn smc_conn;
|
|
6226 SmPointer client_data;
|
|
6227 {
|
|
6228 cmdmod_T save_cmdmod;
|
|
6229 int cancel_shutdown = False;
|
|
6230
|
|
6231 save_cmdmod = cmdmod;
|
|
6232 cmdmod.confirm = TRUE;
|
|
6233 if (check_changed_any(FALSE))
|
|
6234 /* Mustn't logout */
|
|
6235 cancel_shutdown = True;
|
|
6236 cmdmod = save_cmdmod;
|
|
6237 setcursor(); /* position cursor */
|
|
6238 out_flush();
|
|
6239
|
|
6240 /* Done interaction */
|
|
6241 SmcInteractDone(smc_conn, cancel_shutdown);
|
|
6242
|
|
6243 /* Finish off
|
|
6244 * Only end save-yourself here if we're not cancelling shutdown;
|
|
6245 * we'll get a cancelled callback later in which we'll end it.
|
|
6246 * Hopefully get around glitchy SMs (like GNOME-1)
|
|
6247 */
|
|
6248 if (!cancel_shutdown)
|
|
6249 {
|
|
6250 xsmp.save_yourself = False;
|
|
6251 SmcSaveYourselfDone(smc_conn, True);
|
|
6252 }
|
|
6253 }
|
|
6254 # endif
|
|
6255
|
|
6256 /*
|
|
6257 * Callback that starts save-yourself.
|
|
6258 */
|
|
6259 /*ARGSUSED*/
|
|
6260 static void
|
|
6261 xsmp_handle_save_yourself(smc_conn, client_data, save_type,
|
|
6262 shutdown, interact_style, fast)
|
|
6263 SmcConn smc_conn;
|
|
6264 SmPointer client_data;
|
|
6265 int save_type;
|
|
6266 Bool shutdown;
|
|
6267 int interact_style;
|
|
6268 Bool fast;
|
|
6269 {
|
|
6270 /* Handle already being in saveyourself */
|
|
6271 if (xsmp.save_yourself)
|
|
6272 SmcSaveYourselfDone(smc_conn, True);
|
|
6273 xsmp.save_yourself = True;
|
|
6274 xsmp.shutdown = shutdown;
|
|
6275
|
|
6276 /* First up, preserve all files */
|
|
6277 out_flush();
|
|
6278 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
|
|
6279
|
|
6280 if (p_verbose > 0)
|
292
|
6281 verb_msg((char_u *)_("XSMP handling save-yourself request"));
|
7
|
6282
|
|
6283 # if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
|
|
6284 /* Now see if we can ask about unsaved files */
|
|
6285 if (shutdown && !fast && gui.in_use)
|
|
6286 /* Need to interact with user, but need SM's permission */
|
|
6287 SmcInteractRequest(smc_conn, SmDialogError,
|
|
6288 xsmp_handle_interaction, client_data);
|
|
6289 else
|
|
6290 # endif
|
|
6291 {
|
|
6292 /* Can stop the cycle here */
|
|
6293 SmcSaveYourselfDone(smc_conn, True);
|
|
6294 xsmp.save_yourself = False;
|
|
6295 }
|
|
6296 }
|
|
6297
|
|
6298
|
|
6299 /*
|
|
6300 * Callback to warn us of imminent death.
|
|
6301 */
|
|
6302 /*ARGSUSED*/
|
|
6303 static void
|
|
6304 xsmp_die(smc_conn, client_data)
|
|
6305 SmcConn smc_conn;
|
|
6306 SmPointer client_data;
|
|
6307 {
|
|
6308 xsmp_close();
|
|
6309
|
|
6310 /* quit quickly leaving swapfiles for modified buffers behind */
|
|
6311 getout_preserve_modified(0);
|
|
6312 }
|
|
6313
|
|
6314
|
|
6315 /*
|
|
6316 * Callback to tell us that save-yourself has completed.
|
|
6317 */
|
|
6318 /*ARGSUSED*/
|
|
6319 static void
|
|
6320 xsmp_save_complete(smc_conn, client_data)
|
|
6321 SmcConn smc_conn;
|
|
6322 SmPointer client_data;
|
|
6323 {
|
|
6324 xsmp.save_yourself = False;
|
|
6325 }
|
|
6326
|
|
6327
|
|
6328 /*
|
|
6329 * Callback to tell us that an instigated shutdown was cancelled
|
|
6330 * (maybe even by us)
|
|
6331 */
|
|
6332 /*ARGSUSED*/
|
|
6333 static void
|
|
6334 xsmp_shutdown_cancelled(smc_conn, client_data)
|
|
6335 SmcConn smc_conn;
|
|
6336 SmPointer client_data;
|
|
6337 {
|
|
6338 if (xsmp.save_yourself)
|
|
6339 SmcSaveYourselfDone(smc_conn, True);
|
|
6340 xsmp.save_yourself = False;
|
|
6341 xsmp.shutdown = False;
|
|
6342 }
|
|
6343
|
|
6344
|
|
6345 /*
|
|
6346 * Callback to tell us that a new ICE connection has been established.
|
|
6347 */
|
|
6348 /*ARGSUSED*/
|
|
6349 static void
|
|
6350 xsmp_ice_connection(iceConn, clientData, opening, watchData)
|
|
6351 IceConn iceConn;
|
|
6352 IcePointer clientData;
|
|
6353 Bool opening;
|
|
6354 IcePointer *watchData;
|
|
6355 {
|
|
6356 /* Intercept creation of ICE connection fd */
|
|
6357 if (opening)
|
|
6358 {
|
|
6359 xsmp_icefd = IceConnectionNumber(iceConn);
|
|
6360 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
|
|
6361 }
|
|
6362 }
|
|
6363
|
|
6364
|
|
6365 /* Handle any ICE processing that's required; return FAIL if SM lost */
|
|
6366 int
|
|
6367 xsmp_handle_requests()
|
|
6368 {
|
|
6369 Bool rep;
|
|
6370
|
|
6371 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
|
|
6372 == IceProcessMessagesIOError)
|
|
6373 {
|
|
6374 /* Lost ICE */
|
|
6375 if (p_verbose > 0)
|
292
|
6376 verb_msg((char_u *)_("XSMP lost ICE connection"));
|
7
|
6377 xsmp_close();
|
|
6378 return FAIL;
|
|
6379 }
|
|
6380 else
|
|
6381 return OK;
|
|
6382 }
|
|
6383
|
|
6384 static int dummy;
|
|
6385
|
|
6386 /* Set up X Session Management Protocol */
|
|
6387 void
|
|
6388 xsmp_init(void)
|
|
6389 {
|
|
6390 char errorstring[80];
|
|
6391 char *clientid;
|
|
6392 SmcCallbacks smcallbacks;
|
|
6393 #if 0
|
|
6394 SmPropValue smname;
|
|
6395 SmProp smnameprop;
|
|
6396 SmProp *smprops[1];
|
|
6397 #endif
|
|
6398
|
|
6399 if (p_verbose > 0)
|
292
|
6400 verb_msg((char_u *)_("XSMP opening connection"));
|
7
|
6401
|
|
6402 xsmp.save_yourself = xsmp.shutdown = False;
|
|
6403
|
|
6404 /* Set up SM callbacks - must have all, even if they're not used */
|
|
6405 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
|
|
6406 smcallbacks.save_yourself.client_data = NULL;
|
|
6407 smcallbacks.die.callback = xsmp_die;
|
|
6408 smcallbacks.die.client_data = NULL;
|
|
6409 smcallbacks.save_complete.callback = xsmp_save_complete;
|
|
6410 smcallbacks.save_complete.client_data = NULL;
|
|
6411 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
|
|
6412 smcallbacks.shutdown_cancelled.client_data = NULL;
|
|
6413
|
|
6414 /* Set up a watch on ICE connection creations. The "dummy" argument is
|
|
6415 * apparently required for FreeBSD (we get a BUS error when using NULL). */
|
|
6416 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
|
|
6417 {
|
|
6418 if (p_verbose > 0)
|
292
|
6419 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
|
7
|
6420 return;
|
|
6421 }
|
|
6422
|
|
6423 /* Create an SM connection */
|
|
6424 xsmp.smcconn = SmcOpenConnection(
|
|
6425 NULL,
|
|
6426 NULL,
|
|
6427 SmProtoMajor,
|
|
6428 SmProtoMinor,
|
|
6429 SmcSaveYourselfProcMask | SmcDieProcMask
|
|
6430 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
|
|
6431 &smcallbacks,
|
|
6432 NULL,
|
|
6433 &clientid,
|
|
6434 sizeof(errorstring),
|
|
6435 errorstring);
|
|
6436 if (xsmp.smcconn == NULL)
|
|
6437 {
|
|
6438 char errorreport[132];
|
273
|
6439
|
292
|
6440 if (p_verbose > 0)
|
|
6441 {
|
|
6442 vim_snprintf(errorreport, sizeof(errorreport),
|
273
|
6443 _("XSMP SmcOpenConnection failed: %s"), errorstring);
|
292
|
6444 verb_msg((char_u *)errorreport);
|
|
6445 }
|
7
|
6446 return;
|
|
6447 }
|
|
6448 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
|
|
6449
|
|
6450 #if 0
|
|
6451 /* ID ourselves */
|
|
6452 smname.value = "vim";
|
|
6453 smname.length = 3;
|
|
6454 smnameprop.name = "SmProgram";
|
|
6455 smnameprop.type = "SmARRAY8";
|
|
6456 smnameprop.num_vals = 1;
|
|
6457 smnameprop.vals = &smname;
|
|
6458
|
|
6459 smprops[0] = &smnameprop;
|
|
6460 SmcSetProperties(xsmp.smcconn, 1, smprops);
|
|
6461 #endif
|
|
6462 }
|
|
6463
|
|
6464
|
|
6465 /* Shut down XSMP comms. */
|
|
6466 void
|
|
6467 xsmp_close()
|
|
6468 {
|
|
6469 if (xsmp_icefd != -1)
|
|
6470 {
|
|
6471 SmcCloseConnection(xsmp.smcconn, 0, NULL);
|
|
6472 xsmp_icefd = -1;
|
|
6473 }
|
|
6474 }
|
|
6475 #endif /* USE_XSMP */
|
|
6476
|
|
6477
|
|
6478 #ifdef EBCDIC
|
|
6479 /* Translate character to its CTRL- value */
|
|
6480 char CtrlTable[] =
|
|
6481 {
|
|
6482 /* 00 - 5E */
|
|
6483 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6484 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6485 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6486 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6487 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6488 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6489 /* ^ */ 0x1E,
|
|
6490 /* - */ 0x1F,
|
|
6491 /* 61 - 6C */
|
|
6492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6493 /* _ */ 0x1F,
|
|
6494 /* 6E - 80 */
|
|
6495 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6496 /* a */ 0x01,
|
|
6497 /* b */ 0x02,
|
|
6498 /* c */ 0x03,
|
|
6499 /* d */ 0x37,
|
|
6500 /* e */ 0x2D,
|
|
6501 /* f */ 0x2E,
|
|
6502 /* g */ 0x2F,
|
|
6503 /* h */ 0x16,
|
|
6504 /* i */ 0x05,
|
|
6505 /* 8A - 90 */
|
|
6506 0, 0, 0, 0, 0, 0, 0,
|
|
6507 /* j */ 0x15,
|
|
6508 /* k */ 0x0B,
|
|
6509 /* l */ 0x0C,
|
|
6510 /* m */ 0x0D,
|
|
6511 /* n */ 0x0E,
|
|
6512 /* o */ 0x0F,
|
|
6513 /* p */ 0x10,
|
|
6514 /* q */ 0x11,
|
|
6515 /* r */ 0x12,
|
|
6516 /* 9A - A1 */
|
|
6517 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6518 /* s */ 0x13,
|
|
6519 /* t */ 0x3C,
|
|
6520 /* u */ 0x3D,
|
|
6521 /* v */ 0x32,
|
|
6522 /* w */ 0x26,
|
|
6523 /* x */ 0x18,
|
|
6524 /* y */ 0x19,
|
|
6525 /* z */ 0x3F,
|
|
6526 /* AA - AC */
|
|
6527 0, 0, 0,
|
|
6528 /* [ */ 0x27,
|
|
6529 /* AE - BC */
|
|
6530 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6531 /* ] */ 0x1D,
|
|
6532 /* BE - C0 */ 0, 0, 0,
|
|
6533 /* A */ 0x01,
|
|
6534 /* B */ 0x02,
|
|
6535 /* C */ 0x03,
|
|
6536 /* D */ 0x37,
|
|
6537 /* E */ 0x2D,
|
|
6538 /* F */ 0x2E,
|
|
6539 /* G */ 0x2F,
|
|
6540 /* H */ 0x16,
|
|
6541 /* I */ 0x05,
|
|
6542 /* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
|
|
6543 /* J */ 0x15,
|
|
6544 /* K */ 0x0B,
|
|
6545 /* L */ 0x0C,
|
|
6546 /* M */ 0x0D,
|
|
6547 /* N */ 0x0E,
|
|
6548 /* O */ 0x0F,
|
|
6549 /* P */ 0x10,
|
|
6550 /* Q */ 0x11,
|
|
6551 /* R */ 0x12,
|
|
6552 /* DA - DF */ 0, 0, 0, 0, 0, 0,
|
|
6553 /* \ */ 0x1C,
|
|
6554 /* E1 */ 0,
|
|
6555 /* S */ 0x13,
|
|
6556 /* T */ 0x3C,
|
|
6557 /* U */ 0x3D,
|
|
6558 /* V */ 0x32,
|
|
6559 /* W */ 0x26,
|
|
6560 /* X */ 0x18,
|
|
6561 /* Y */ 0x19,
|
|
6562 /* Z */ 0x3F,
|
|
6563 /* EA - FF*/ 0, 0, 0, 0, 0, 0,
|
|
6564 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
6565 };
|
|
6566
|
|
6567 char MetaCharTable[]=
|
|
6568 {/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
|
6569 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
|
|
6570 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
|
|
6571 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
|
|
6572 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
|
|
6573 };
|
|
6574
|
|
6575
|
|
6576 /* TODO: Use characters NOT numbers!!! */
|
|
6577 char CtrlCharTable[]=
|
|
6578 {/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
|
6579 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
|
|
6580 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
|
|
6581 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
|
|
6582 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
|
|
6583 };
|
|
6584
|
|
6585
|
|
6586 #endif
|