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