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