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