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