Mercurial > vim
annotate src/os_unix.c @ 7631:ad3e9ea845e7 v7.4.1115
commit https://github.com/vim/vim/commit/d0232917ced39ff4838665fbcf379d5116a91aa3
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jan 17 16:15:32 2016 +0100
patch 7.4.1115
Problem: MS-Windows: make clean in testdir doesn't clean everything.
Solution: Add command to delete X* directories. (Ken Takata)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 17 Jan 2016 16:30:04 +0100 |
parents | befbed72da87 |
children | 6069f43cea4e |
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 /* | |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
2997 * return TRUE if "name" is a directory or a symlink to a directory |
7 | 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 | |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3018 /* |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3019 * return TRUE if "name" is a directory, NOT a symlink to a directory |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3020 * return FALSE if "name" is not a directory |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3021 * return FALSE for error |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3022 */ |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3023 int |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3024 mch_isrealdir(name) |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3025 char_u *name; |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3026 { |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3027 struct stat statb; |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3028 |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3029 if (*name == NUL) /* Some stat()s don't flag "" as an error. */ |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3030 return FALSE; |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3031 if (lstat((char *)name, &statb)) |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3032 return FALSE; |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3033 #ifdef _POSIX_SOURCE |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3034 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE); |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3035 #else |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3036 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE); |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3037 #endif |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3038 } |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7469
diff
changeset
|
3039 |
7 | 3040 static int executable_file __ARGS((char_u *name)); |
3041 | |
3042 /* | |
3043 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist. | |
3044 */ | |
3045 static int | |
3046 executable_file(name) | |
3047 char_u *name; | |
3048 { | |
3049 struct stat st; | |
3050 | |
3051 if (stat((char *)name, &st)) | |
3052 return 0; | |
5704 | 3053 #ifdef VMS |
3054 /* Like on Unix system file can have executable rights but not necessarily | |
3055 * be an executable, but on Unix is not a default for an ordianry file to | |
3056 * have an executable flag - on VMS it is in most cases. | |
3057 * Therefore, this check does not have any sense - let keep us to the | |
3058 * conventions instead: | |
3059 * *.COM and *.EXE files are the executables - the rest are not. This is | |
3060 * not ideal but better then it was. | |
3061 */ | |
3062 int vms_executable = 0; | |
3063 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0) | |
3064 { | |
3065 if (strstr(vms_tolower((char*)name),".exe") != NULL | |
3066 || strstr(vms_tolower((char*)name),".com")!= NULL) | |
3067 vms_executable = 1; | |
3068 } | |
3069 return vms_executable; | |
3070 #else | |
7 | 3071 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0; |
5704 | 3072 #endif |
7 | 3073 } |
3074 | |
3075 /* | |
3076 * Return 1 if "name" can be found in $PATH and executed, 0 if not. | |
6695 | 3077 * If "use_path" is FALSE only check if "name" is executable. |
7 | 3078 * Return -1 if unknown. |
3079 */ | |
3080 int | |
6695 | 3081 mch_can_exe(name, path, use_path) |
7 | 3082 char_u *name; |
5782 | 3083 char_u **path; |
6695 | 3084 int use_path; |
7 | 3085 { |
3086 char_u *buf; | |
3087 char_u *p, *e; | |
3088 int retval; | |
3089 | |
6695 | 3090 /* When "use_path" is false and if it's an absolute or relative path don't |
3091 * need to use $PATH. */ | |
3092 if (!use_path || mch_isFullName(name) || (name[0] == '.' | |
3093 && (name[1] == '/' || (name[1] == '.' && name[2] == '/')))) | |
5704 | 3094 { |
6695 | 3095 /* There must be a path separator, files in the current directory |
3096 * can't be executed. */ | |
3097 if (gettail(name) != name && executable_file(name)) | |
5782 | 3098 { |
3099 if (path != NULL) | |
3100 { | |
3101 if (name[0] == '.') | |
3102 *path = FullName_save(name, TRUE); | |
3103 else | |
3104 *path = vim_strsave(name); | |
3105 } | |
3106 return TRUE; | |
3107 } | |
3108 return FALSE; | |
5704 | 3109 } |
7 | 3110 |
3111 p = (char_u *)getenv("PATH"); | |
3112 if (p == NULL || *p == NUL) | |
3113 return -1; | |
3114 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2)); | |
3115 if (buf == NULL) | |
3116 return -1; | |
3117 | |
3118 /* | |
3119 * Walk through all entries in $PATH to check if "name" exists there and | |
3120 * is an executable file. | |
3121 */ | |
3122 for (;;) | |
3123 { | |
3124 e = (char_u *)strchr((char *)p, ':'); | |
3125 if (e == NULL) | |
3126 e = p + STRLEN(p); | |
3127 if (e - p <= 1) /* empty entry means current dir */ | |
3128 STRCPY(buf, "./"); | |
3129 else | |
3130 { | |
418 | 3131 vim_strncpy(buf, p, e - p); |
7 | 3132 add_pathsep(buf); |
3133 } | |
3134 STRCAT(buf, name); | |
3135 retval = executable_file(buf); | |
3136 if (retval == 1) | |
5782 | 3137 { |
3138 if (path != NULL) | |
3139 { | |
3140 if (buf[0] == '.') | |
3141 *path = FullName_save(buf, TRUE); | |
3142 else | |
3143 *path = vim_strsave(buf); | |
3144 } | |
7 | 3145 break; |
5782 | 3146 } |
7 | 3147 |
3148 if (*e != ':') | |
3149 break; | |
3150 p = e + 1; | |
3151 } | |
3152 | |
3153 vim_free(buf); | |
3154 return retval; | |
3155 } | |
3156 | |
3157 /* | |
3158 * Check what "name" is: | |
3159 * NODE_NORMAL: file or directory (or doesn't exist) | |
3160 * NODE_WRITABLE: writable device, socket, fifo, etc. | |
3161 * NODE_OTHER: non-writable things | |
3162 */ | |
3163 int | |
3164 mch_nodetype(name) | |
3165 char_u *name; | |
3166 { | |
3167 struct stat st; | |
3168 | |
3169 if (stat((char *)name, &st)) | |
3170 return NODE_NORMAL; | |
3171 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) | |
3172 return NODE_NORMAL; | |
3173 if (S_ISBLK(st.st_mode)) /* block device isn't writable */ | |
3174 return NODE_OTHER; | |
3175 /* Everything else is writable? */ | |
3176 return NODE_WRITABLE; | |
3177 } | |
3178 | |
3179 void | |
3180 mch_early_init() | |
3181 { | |
3182 #ifdef HAVE_CHECK_STACK_GROWTH | |
3183 int i; | |
180 | 3184 |
7 | 3185 check_stack_growth((char *)&i); |
3186 | |
180 | 3187 # ifdef HAVE_STACK_LIMIT |
7 | 3188 get_stack_limit(); |
3189 # endif | |
3190 | |
3191 #endif | |
3192 | |
3193 /* | |
3194 * Setup an alternative stack for signals. Helps to catch signals when | |
3195 * running out of stack space. | |
3196 * Use of sigaltstack() is preferred, it's more portable. | |
3197 * Ignore any errors. | |
3198 */ | |
3199 #if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) | |
1737 | 3200 signal_stack = (char *)alloc(SIGSTKSZ); |
7 | 3201 init_signal_stack(); |
3202 #endif | |
3203 } | |
3204 | |
355 | 3205 #if defined(EXITFREE) || defined(PROTO) |
3206 void | |
3207 mch_free_mem() | |
3208 { | |
359 | 3209 # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
3210 if (clip_star.owned) | |
3211 clip_lose_selection(&clip_star); | |
3212 if (clip_plus.owned) | |
3213 clip_lose_selection(&clip_plus); | |
355 | 3214 # endif |
1605 | 3215 # if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) |
355 | 3216 if (xterm_Shell != (Widget)0) |
3217 XtDestroyWidget(xterm_Shell); | |
1605 | 3218 # ifndef LESSTIF_VERSION |
3219 /* Lesstif crashes here, lose some memory */ | |
355 | 3220 if (xterm_dpy != NULL) |
3221 XtCloseDisplay(xterm_dpy); | |
3222 if (app_context != (XtAppContext)NULL) | |
1605 | 3223 { |
355 | 3224 XtDestroyApplicationContext(app_context); |
1605 | 3225 # ifdef FEAT_X11 |
3226 x11_display = NULL; /* freed by XtDestroyApplicationContext() */ | |
3227 # endif | |
3228 } | |
3229 # endif | |
355 | 3230 # endif |
2277
f42e0b5ff9e9
Change remaining HAVE_GTK2 to FEAT_GUI_GTK.
Bram Moolenaar <bram@vim.org>
parents:
2275
diff
changeset
|
3231 # if defined(FEAT_X11) |
1605 | 3232 if (x11_display != NULL |
3233 # ifdef FEAT_XCLIPBOARD | |
3234 && x11_display != xterm_dpy | |
3235 # endif | |
3236 ) | |
359 | 3237 XCloseDisplay(x11_display); |
3238 # endif | |
3239 # if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) | |
3240 vim_free(signal_stack); | |
3241 signal_stack = NULL; | |
3242 # endif | |
3243 # ifdef FEAT_TITLE | |
3244 vim_free(oldtitle); | |
3245 vim_free(oldicon); | |
3246 # endif | |
355 | 3247 } |
3248 #endif | |
3249 | |
7 | 3250 static void exit_scroll __ARGS((void)); |
3251 | |
3252 /* | |
3253 * Output a newline when exiting. | |
3254 * Make sure the newline goes to the same stream as the text. | |
3255 */ | |
3256 static void | |
3257 exit_scroll() | |
3258 { | |
167 | 3259 if (silent_mode) |
3260 return; | |
7 | 3261 if (newline_on_exit || msg_didout) |
3262 { | |
3263 if (msg_use_printf()) | |
3264 { | |
3265 if (info_message) | |
3266 mch_msg("\n"); | |
3267 else | |
3268 mch_errmsg("\r\n"); | |
3269 } | |
3270 else | |
3271 out_char('\n'); | |
3272 } | |
3273 else | |
3274 { | |
3275 restore_cterm_colors(); /* get original colors back */ | |
3276 msg_clr_eos_force(); /* clear the rest of the display */ | |
3277 windgoto((int)Rows - 1, 0); /* may have moved the cursor */ | |
3278 } | |
3279 } | |
3280 | |
3281 void | |
3282 mch_exit(r) | |
3283 int r; | |
3284 { | |
3285 exiting = TRUE; | |
3286 | |
3287 #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD) | |
3288 x11_export_final_selection(); | |
3289 #endif | |
3290 | |
3291 #ifdef FEAT_GUI | |
3292 if (!gui.in_use) | |
3293 #endif | |
3294 { | |
3295 settmode(TMODE_COOK); | |
3296 #ifdef FEAT_TITLE | |
3297 mch_restore_title(3); /* restore xterm title and icon name */ | |
3298 #endif | |
3299 /* | |
3300 * When t_ti is not empty but it doesn't cause swapping terminal | |
3301 * pages, need to output a newline when msg_didout is set. But when | |
3302 * t_ti does swap pages it should not go to the shell page. Do this | |
3303 * before stoptermcap(). | |
3304 */ | |
3305 if (swapping_screen() && !newline_on_exit) | |
3306 exit_scroll(); | |
3307 | |
3308 /* Stop termcap: May need to check for T_CRV response, which | |
3309 * requires RAW mode. */ | |
3310 stoptermcap(); | |
3311 | |
3312 /* | |
3313 * A newline is only required after a message in the alternate screen. | |
3314 * This is set to TRUE by wait_return(). | |
3315 */ | |
3316 if (!swapping_screen() || newline_on_exit) | |
3317 exit_scroll(); | |
3318 | |
3319 /* Cursor may have been switched off without calling starttermcap() | |
3320 * when doing "vim -u vimrc" and vimrc contains ":q". */ | |
3321 if (full_screen) | |
3322 cursor_on(); | |
3323 } | |
3324 out_flush(); | |
3325 ml_close_all(TRUE); /* remove all memfiles */ | |
3326 may_core_dump(); | |
3327 #ifdef FEAT_GUI | |
3328 if (gui.in_use) | |
3329 gui_exit(r); | |
3330 #endif | |
167 | 3331 |
765 | 3332 #ifdef MACOS_CONVERT |
167 | 3333 mac_conv_cleanup(); |
3334 #endif | |
3335 | |
7 | 3336 #ifdef __QNX__ |
3337 /* A core dump won't be created if the signal handler | |
3338 * doesn't return, so we can't call exit() */ | |
3339 if (deadly_signal != 0) | |
3340 return; | |
3341 #endif | |
3342 | |
33 | 3343 #ifdef FEAT_NETBEANS_INTG |
2210 | 3344 netbeans_send_disconnect(); |
33 | 3345 #endif |
355 | 3346 |
3347 #ifdef EXITFREE | |
3348 free_all_mem(); | |
3349 #endif | |
3350 | |
7 | 3351 exit(r); |
3352 } | |
3353 | |
3354 static void | |
3355 may_core_dump() | |
3356 { | |
3357 if (deadly_signal != 0) | |
3358 { | |
3359 signal(deadly_signal, SIG_DFL); | |
3360 kill(getpid(), deadly_signal); /* Die using the signal we caught */ | |
3361 } | |
3362 } | |
3363 | |
3364 #ifndef VMS | |
3365 | |
3366 void | |
3367 mch_settmode(tmode) | |
3368 int tmode; | |
3369 { | |
3370 static int first = TRUE; | |
3371 | |
3372 /* Why is NeXT excluded here (and not in os_unixx.h)? */ | |
3373 #if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__) | |
3374 /* | |
3375 * for "new" tty systems | |
3376 */ | |
3377 # ifdef HAVE_TERMIOS_H | |
3378 static struct termios told; | |
3379 struct termios tnew; | |
3380 # else | |
3381 static struct termio told; | |
3382 struct termio tnew; | |
3383 # endif | |
3384 | |
3385 if (first) | |
3386 { | |
3387 first = FALSE; | |
3388 # if defined(HAVE_TERMIOS_H) | |
3389 tcgetattr(read_cmd_fd, &told); | |
3390 # else | |
3391 ioctl(read_cmd_fd, TCGETA, &told); | |
3392 # endif | |
3393 } | |
3394 | |
3395 tnew = told; | |
3396 if (tmode == TMODE_RAW) | |
3397 { | |
3398 /* | |
3399 * ~ICRNL enables typing ^V^M | |
3400 */ | |
3401 tnew.c_iflag &= ~ICRNL; | |
3402 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE | |
3403 # if defined(IEXTEN) && !defined(__MINT__) | |
3404 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */ | |
3405 /* but it breaks function keys on MINT */ | |
3406 # endif | |
3407 ); | |
3408 # ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */ | |
3409 tnew.c_oflag &= ~ONLCR; | |
3410 # endif | |
3411 tnew.c_cc[VMIN] = 1; /* return after 1 char */ | |
3412 tnew.c_cc[VTIME] = 0; /* don't wait */ | |
3413 } | |
3414 else if (tmode == TMODE_SLEEP) | |
3415 tnew.c_lflag &= ~(ECHO); | |
3416 | |
3417 # if defined(HAVE_TERMIOS_H) | |
3418 { | |
3419 int n = 10; | |
3420 | |
3421 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a | |
3422 * few times. */ | |
3423 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1 | |
3424 && errno == EINTR && n > 0) | |
3425 --n; | |
3426 } | |
3427 # else | |
3428 ioctl(read_cmd_fd, TCSETA, &tnew); | |
3429 # endif | |
3430 | |
3431 #else | |
3432 | |
3433 /* | |
3434 * for "old" tty systems | |
3435 */ | |
3436 # ifndef TIOCSETN | |
3437 # define TIOCSETN TIOCSETP /* for hpux 9.0 */ | |
3438 # endif | |
3439 static struct sgttyb ttybold; | |
3440 struct sgttyb ttybnew; | |
3441 | |
3442 if (first) | |
3443 { | |
3444 first = FALSE; | |
3445 ioctl(read_cmd_fd, TIOCGETP, &ttybold); | |
3446 } | |
3447 | |
3448 ttybnew = ttybold; | |
3449 if (tmode == TMODE_RAW) | |
3450 { | |
3451 ttybnew.sg_flags &= ~(CRMOD | ECHO); | |
3452 ttybnew.sg_flags |= RAW; | |
3453 } | |
3454 else if (tmode == TMODE_SLEEP) | |
3455 ttybnew.sg_flags &= ~(ECHO); | |
3456 ioctl(read_cmd_fd, TIOCSETN, &ttybnew); | |
3457 #endif | |
3458 curr_tmode = tmode; | |
3459 } | |
3460 | |
3461 /* | |
3462 * Try to get the code for "t_kb" from the stty setting | |
3463 * | |
3464 * Even if termcap claims a backspace key, the user's setting *should* | |
3465 * prevail. stty knows more about reality than termcap does, and if | |
3466 * somebody's usual erase key is DEL (which, for most BSD users, it will | |
3467 * be), they're going to get really annoyed if their erase key starts | |
3468 * doing forward deletes for no reason. (Eric Fischer) | |
3469 */ | |
3470 void | |
3471 get_stty() | |
3472 { | |
3473 char_u buf[2]; | |
3474 char_u *p; | |
3475 | |
3476 /* Why is NeXT excluded here (and not in os_unixx.h)? */ | |
3477 #if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__) | |
3478 /* for "new" tty systems */ | |
3479 # ifdef HAVE_TERMIOS_H | |
3480 struct termios keys; | |
3481 # else | |
3482 struct termio keys; | |
3483 # endif | |
3484 | |
3485 # if defined(HAVE_TERMIOS_H) | |
3486 if (tcgetattr(read_cmd_fd, &keys) != -1) | |
3487 # else | |
3488 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1) | |
3489 # endif | |
3490 { | |
3491 buf[0] = keys.c_cc[VERASE]; | |
3492 intr_char = keys.c_cc[VINTR]; | |
3493 #else | |
3494 /* for "old" tty systems */ | |
3495 struct sgttyb keys; | |
3496 | |
3497 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1) | |
3498 { | |
3499 buf[0] = keys.sg_erase; | |
3500 intr_char = keys.sg_kill; | |
3501 #endif | |
3502 buf[1] = NUL; | |
3503 add_termcode((char_u *)"kb", buf, FALSE); | |
3504 | |
3505 /* | |
3506 * If <BS> and <DEL> are now the same, redefine <DEL>. | |
3507 */ | |
3508 p = find_termcode((char_u *)"kD"); | |
3509 if (p != NULL && p[0] == buf[0] && p[1] == buf[1]) | |
3510 do_fixdel(NULL); | |
3511 } | |
3512 #if 0 | |
3513 } /* to keep cindent happy */ | |
3514 #endif | |
3515 } | |
3516 | |
3517 #endif /* VMS */ | |
3518 | |
3519 #if defined(FEAT_MOUSE_TTY) || defined(PROTO) | |
3520 /* | |
3521 * Set mouse clicks on or off. | |
3522 */ | |
3523 void | |
3524 mch_setmouse(on) | |
3525 int on; | |
3526 { | |
3527 static int ison = FALSE; | |
3528 int xterm_mouse_vers; | |
3529 | |
3530 if (on == ison) /* return quickly if nothing to do */ | |
3531 return; | |
3532 | |
3533 xterm_mouse_vers = use_xterm_mouse(); | |
3145 | 3534 |
3535 # ifdef FEAT_MOUSE_URXVT | |
3746 | 3536 if (ttym_flags == TTYM_URXVT) |
3537 { | |
3145 | 3538 out_str_nf((char_u *) |
3539 (on | |
3540 ? IF_EB("\033[?1015h", ESC_STR "[?1015h") | |
3541 : IF_EB("\033[?1015l", ESC_STR "[?1015l"))); | |
3542 ison = on; | |
3543 } | |
3544 # endif | |
3545 | |
3746 | 3546 # ifdef FEAT_MOUSE_SGR |
3547 if (ttym_flags == TTYM_SGR) | |
3548 { | |
3549 out_str_nf((char_u *) | |
3550 (on | |
3551 ? IF_EB("\033[?1006h", ESC_STR "[?1006h") | |
3552 : IF_EB("\033[?1006l", ESC_STR "[?1006l"))); | |
3553 ison = on; | |
3554 } | |
3555 # endif | |
3556 | |
7 | 3557 if (xterm_mouse_vers > 0) |
3558 { | |
3559 if (on) /* enable mouse events, use mouse tracking if available */ | |
3560 out_str_nf((char_u *) | |
3561 (xterm_mouse_vers > 1 | |
3562 ? IF_EB("\033[?1002h", ESC_STR "[?1002h") | |
3563 : IF_EB("\033[?1000h", ESC_STR "[?1000h"))); | |
3564 else /* disable mouse events, could probably always send the same */ | |
3565 out_str_nf((char_u *) | |
3566 (xterm_mouse_vers > 1 | |
3567 ? IF_EB("\033[?1002l", ESC_STR "[?1002l") | |
3568 : IF_EB("\033[?1000l", ESC_STR "[?1000l"))); | |
3569 ison = on; | |
3570 } | |
3571 | |
3572 # ifdef FEAT_MOUSE_DEC | |
3573 else if (ttym_flags == TTYM_DEC) | |
3574 { | |
3575 if (on) /* enable mouse events */ | |
3576 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{"); | |
3577 else /* disable mouse events */ | |
3578 out_str_nf((char_u *)"\033['z"); | |
3579 ison = on; | |
3580 } | |
3581 # endif | |
3582 | |
3583 # ifdef FEAT_MOUSE_GPM | |
3584 else | |
3585 { | |
3586 if (on) | |
3587 { | |
3588 if (gpm_open()) | |
3589 ison = TRUE; | |
3590 } | |
3591 else | |
3592 { | |
3593 gpm_close(); | |
3594 ison = FALSE; | |
3595 } | |
3596 } | |
3597 # endif | |
3598 | |
1620 | 3599 # ifdef FEAT_SYSMOUSE |
3600 else | |
3601 { | |
3602 if (on) | |
3603 { | |
3604 if (sysmouse_open() == OK) | |
3605 ison = TRUE; | |
3606 } | |
3607 else | |
3608 { | |
3609 sysmouse_close(); | |
3610 ison = FALSE; | |
3611 } | |
3612 } | |
3613 # endif | |
3614 | |
7 | 3615 # ifdef FEAT_MOUSE_JSB |
3616 else | |
3617 { | |
3618 if (on) | |
3619 { | |
3620 /* D - Enable Mouse up/down messages | |
3621 * L - Enable Left Button Reporting | |
3622 * M - Enable Middle Button Reporting | |
3623 * R - Enable Right Button Reporting | |
3624 * K - Enable SHIFT and CTRL key Reporting | |
3625 * + - Enable Advanced messaging of mouse moves and up/down messages | |
3626 * Q - Quiet No Ack | |
3627 * # - Numeric value of mouse pointer required | |
3628 * 0 = Multiview 2000 cursor, used as standard | |
3629 * 1 = Windows Arrow | |
3630 * 2 = Windows I Beam | |
3631 * 3 = Windows Hour Glass | |
3632 * 4 = Windows Cross Hair | |
3633 * 5 = Windows UP Arrow | |
3634 */ | |
4299 | 3635 # ifdef JSBTERM_MOUSE_NONADVANCED |
3636 /* Disables full feedback of pointer movements */ | |
7 | 3637 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\", |
3638 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\")); | |
4299 | 3639 # else |
7 | 3640 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\", |
3641 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\")); | |
4299 | 3642 # endif |
7 | 3643 ison = TRUE; |
3644 } | |
3645 else | |
3646 { | |
3647 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\", | |
3648 ESC_STR "[0~ZwQ" ESC_STR "\\")); | |
3649 ison = FALSE; | |
3650 } | |
3651 } | |
3652 # endif | |
3653 # ifdef FEAT_MOUSE_PTERM | |
3654 else | |
3655 { | |
3656 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */ | |
3657 if (on) | |
3658 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l"); | |
3659 else | |
3660 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h"); | |
3661 ison = on; | |
3662 } | |
3663 # endif | |
3664 } | |
3665 | |
3666 /* | |
3667 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options. | |
3668 */ | |
3669 void | |
3670 check_mouse_termcode() | |
3671 { | |
3672 # ifdef FEAT_MOUSE_XTERM | |
3673 if (use_xterm_mouse() | |
3145 | 3674 # ifdef FEAT_MOUSE_URXVT |
3675 && use_xterm_mouse() != 3 | |
3676 # endif | |
7 | 3677 # ifdef FEAT_GUI |
3678 && !gui.in_use | |
3679 # endif | |
3680 ) | |
3681 { | |
3682 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME) | |
180 | 3683 ? IF_EB("\233M", CSI_STR "M") |
3684 : IF_EB("\033[M", ESC_STR "[M"))); | |
7 | 3685 if (*p_mouse != NUL) |
3686 { | |
3687 /* force mouse off and maybe on to send possibly new mouse | |
3688 * activation sequence to the xterm, with(out) drag tracing. */ | |
3689 mch_setmouse(FALSE); | |
3690 setmouse(); | |
3691 } | |
3692 } | |
3693 else | |
3694 del_mouse_termcode(KS_MOUSE); | |
3695 # endif | |
3696 | |
3697 # ifdef FEAT_MOUSE_GPM | |
3698 if (!use_xterm_mouse() | |
3699 # ifdef FEAT_GUI | |
3700 && !gui.in_use | |
3701 # endif | |
3702 ) | |
3703 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG")); | |
3704 # endif | |
3705 | |
1620 | 3706 # ifdef FEAT_SYSMOUSE |
3707 if (!use_xterm_mouse() | |
3708 # ifdef FEAT_GUI | |
3709 && !gui.in_use | |
3710 # endif | |
3711 ) | |
3712 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS")); | |
3713 # endif | |
3714 | |
7 | 3715 # ifdef FEAT_MOUSE_JSB |
6102 | 3716 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */ |
7 | 3717 if (!use_xterm_mouse() |
3718 # ifdef FEAT_GUI | |
3719 && !gui.in_use | |
3720 # endif | |
3721 ) | |
3722 set_mouse_termcode(KS_JSBTERM_MOUSE, | |
3723 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw")); | |
3724 else | |
3725 del_mouse_termcode(KS_JSBTERM_MOUSE); | |
3726 # endif | |
3727 | |
3728 # ifdef FEAT_MOUSE_NET | |
180 | 3729 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't |
7 | 3730 * define it in the GUI or when using an xterm. */ |
3731 if (!use_xterm_mouse() | |
3732 # ifdef FEAT_GUI | |
3733 && !gui.in_use | |
3734 # endif | |
3735 ) | |
3736 set_mouse_termcode(KS_NETTERM_MOUSE, | |
3737 (char_u *)IF_EB("\033}", ESC_STR "}")); | |
3738 else | |
3739 del_mouse_termcode(KS_NETTERM_MOUSE); | |
3740 # endif | |
3741 | |
3742 # ifdef FEAT_MOUSE_DEC | |
6102 | 3743 /* Conflicts with xterm mouse: "\033[" and "\033[M" */ |
5932 | 3744 if (!use_xterm_mouse() |
7 | 3745 # ifdef FEAT_GUI |
3746 && !gui.in_use | |
3747 # endif | |
3748 ) | |
180 | 3749 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
3750 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "["))); | |
7 | 3751 else |
3752 del_mouse_termcode(KS_DEC_MOUSE); | |
3753 # endif | |
3754 # ifdef FEAT_MOUSE_PTERM | |
6102 | 3755 /* same conflict as the dec mouse */ |
5932 | 3756 if (!use_xterm_mouse() |
7 | 3757 # ifdef FEAT_GUI |
3758 && !gui.in_use | |
3759 # endif | |
3760 ) | |
3761 set_mouse_termcode(KS_PTERM_MOUSE, | |
3762 (char_u *) IF_EB("\033[", ESC_STR "[")); | |
3763 else | |
3764 del_mouse_termcode(KS_PTERM_MOUSE); | |
3765 # endif | |
3145 | 3766 # ifdef FEAT_MOUSE_URXVT |
6102 | 3767 /* same conflict as the dec mouse */ |
5932 | 3768 if (use_xterm_mouse() == 3 |
3145 | 3769 # ifdef FEAT_GUI |
3770 && !gui.in_use | |
3771 # endif | |
3772 ) | |
3773 { | |
3774 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME) | |
3775 ? IF_EB("\233", CSI_STR) | |
3776 : IF_EB("\033[", ESC_STR "["))); | |
3777 | |
3778 if (*p_mouse != NUL) | |
3779 { | |
3780 mch_setmouse(FALSE); | |
3781 setmouse(); | |
3782 } | |
3783 } | |
3784 else | |
3785 del_mouse_termcode(KS_URXVT_MOUSE); | |
3786 # endif | |
3746 | 3787 # ifdef FEAT_MOUSE_SGR |
5905 | 3788 /* There is no conflict with xterm mouse */ |
3746 | 3789 if (use_xterm_mouse() == 4 |
3790 # ifdef FEAT_GUI | |
3791 && !gui.in_use | |
3792 # endif | |
3793 ) | |
3794 { | |
3795 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME) | |
3796 ? IF_EB("\233<", CSI_STR "<") | |
3797 : IF_EB("\033[<", ESC_STR "[<"))); | |
3798 | |
3799 if (*p_mouse != NUL) | |
3800 { | |
3801 mch_setmouse(FALSE); | |
3802 setmouse(); | |
3803 } | |
3804 } | |
3805 else | |
3806 del_mouse_termcode(KS_SGR_MOUSE); | |
3807 # endif | |
7 | 3808 } |
3809 #endif | |
3810 | |
3811 /* | |
3812 * set screen mode, always fails. | |
3813 */ | |
3814 int | |
3815 mch_screenmode(arg) | |
1877 | 3816 char_u *arg UNUSED; |
7 | 3817 { |
3818 EMSG(_(e_screenmode)); | |
3819 return FAIL; | |
3820 } | |
3821 | |
3822 #ifndef VMS | |
3823 | |
3824 /* | |
3825 * Try to get the current window size: | |
3826 * 1. with an ioctl(), most accurate method | |
3827 * 2. from the environment variables LINES and COLUMNS | |
3828 * 3. from the termcap | |
3829 * 4. keep using the old values | |
3830 * Return OK when size could be determined, FAIL otherwise. | |
3831 */ | |
3832 int | |
3833 mch_get_shellsize() | |
3834 { | |
3835 long rows = 0; | |
3836 long columns = 0; | |
3837 char_u *p; | |
3838 | |
3839 /* | |
3840 * For OS/2 use _scrsize(). | |
3841 */ | |
3842 # ifdef __EMX__ | |
3843 { | |
3844 int s[2]; | |
3845 | |
3846 _scrsize(s); | |
3847 columns = s[0]; | |
3848 rows = s[1]; | |
3849 } | |
3850 # endif | |
3851 | |
3852 /* | |
3853 * 1. try using an ioctl. It is the most accurate method. | |
3854 * | |
3855 * Try using TIOCGWINSZ first, some systems that have it also define | |
3856 * TIOCGSIZE but don't have a struct ttysize. | |
3857 */ | |
3858 # ifdef TIOCGWINSZ | |
3859 { | |
3860 struct winsize ws; | |
3861 int fd = 1; | |
3862 | |
3863 /* When stdout is not a tty, use stdin for the ioctl(). */ | |
3864 if (!isatty(fd) && isatty(read_cmd_fd)) | |
3865 fd = read_cmd_fd; | |
3866 if (ioctl(fd, TIOCGWINSZ, &ws) == 0) | |
3867 { | |
3868 columns = ws.ws_col; | |
3869 rows = ws.ws_row; | |
3870 } | |
3871 } | |
3872 # else /* TIOCGWINSZ */ | |
3873 # ifdef TIOCGSIZE | |
3874 { | |
3875 struct ttysize ts; | |
3876 int fd = 1; | |
3877 | |
3878 /* When stdout is not a tty, use stdin for the ioctl(). */ | |
3879 if (!isatty(fd) && isatty(read_cmd_fd)) | |
3880 fd = read_cmd_fd; | |
3881 if (ioctl(fd, TIOCGSIZE, &ts) == 0) | |
3882 { | |
3883 columns = ts.ts_cols; | |
3884 rows = ts.ts_lines; | |
3885 } | |
3886 } | |
3887 # endif /* TIOCGSIZE */ | |
3888 # endif /* TIOCGWINSZ */ | |
3889 | |
3890 /* | |
3891 * 2. get size from environment | |
164 | 3892 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules |
3893 * the ioctl() values! | |
7 | 3894 */ |
164 | 3895 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL) |
7 | 3896 { |
3897 if ((p = (char_u *)getenv("LINES"))) | |
3898 rows = atoi((char *)p); | |
3899 if ((p = (char_u *)getenv("COLUMNS"))) | |
3900 columns = atoi((char *)p); | |
3901 } | |
3902 | |
3903 #ifdef HAVE_TGETENT | |
3904 /* | |
3905 * 3. try reading "co" and "li" entries from termcap | |
3906 */ | |
3907 if (columns == 0 || rows == 0) | |
3908 getlinecol(&columns, &rows); | |
3909 #endif | |
3910 | |
3911 /* | |
3912 * 4. If everything fails, use the old values | |
3913 */ | |
3914 if (columns <= 0 || rows <= 0) | |
3915 return FAIL; | |
3916 | |
3917 Rows = rows; | |
3918 Columns = columns; | |
5070
cf52d2a8c05c
updated for version 7.3.1278
Bram Moolenaar <bram@vim.org>
parents:
5037
diff
changeset
|
3919 limit_screen_size(); |
7 | 3920 return OK; |
3921 } | |
3922 | |
3923 /* | |
3924 * Try to set the window size to Rows and Columns. | |
3925 */ | |
3926 void | |
3927 mch_set_shellsize() | |
3928 { | |
3929 if (*T_CWS) | |
3930 { | |
3931 /* | |
3932 * NOTE: if you get an error here that term_set_winsize() is | |
3933 * undefined, check the output of configure. It could probably not | |
3934 * find a ncurses, termcap or termlib library. | |
3935 */ | |
3936 term_set_winsize((int)Rows, (int)Columns); | |
3937 out_flush(); | |
3938 screen_start(); /* don't know where cursor is now */ | |
3939 } | |
3940 } | |
3941 | |
3942 #endif /* VMS */ | |
3943 | |
3944 /* | |
3945 * Rows and/or Columns has changed. | |
3946 */ | |
3947 void | |
3948 mch_new_shellsize() | |
3949 { | |
3950 /* Nothing to do. */ | |
3951 } | |
3952 | |
3048 | 3953 /* |
3954 * Wait for process "child" to end. | |
3955 * Return "child" if it exited properly, <= 0 on error. | |
3956 */ | |
3957 static pid_t | |
3958 wait4pid(child, status) | |
3959 pid_t child; | |
3960 waitstatus *status; | |
3961 { | |
3962 pid_t wait_pid = 0; | |
3963 | |
3964 while (wait_pid != child) | |
3965 { | |
3470 | 3966 /* When compiled with Python threads are probably used, in which case |
3967 * wait() sometimes hangs for no obvious reason. Use waitpid() | |
3968 * instead and loop (like the GUI). Also needed for other interfaces, | |
3969 * they might call system(). */ | |
3970 # ifdef __NeXT__ | |
3048 | 3971 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0); |
3470 | 3972 # else |
3048 | 3973 wait_pid = waitpid(child, status, WNOHANG); |
3470 | 3974 # endif |
3048 | 3975 if (wait_pid == 0) |
3976 { | |
4074 | 3977 /* Wait for 10 msec before trying again. */ |
3048 | 3978 mch_delay(10L, TRUE); |
3979 continue; | |
3980 } | |
3981 if (wait_pid <= 0 | |
3982 # ifdef ECHILD | |
3983 && errno == ECHILD | |
3984 # endif | |
3985 ) | |
3986 break; | |
3987 } | |
3988 return wait_pid; | |
3989 } | |
3990 | |
7 | 3991 int |
3992 mch_call_shell(cmd, options) | |
3993 char_u *cmd; | |
3994 int options; /* SHELL_*, see vim.h */ | |
3995 { | |
3996 #ifdef VMS | |
3997 char *ifn = NULL; | |
3998 char *ofn = NULL; | |
3999 #endif | |
4000 int tmode = cur_tmode; | |
4001 #ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */ | |
4002 int x; | |
4003 # ifndef __EMX__ | |
4004 char_u *newcmd; /* only needed for unix */ | |
4005 # else | |
4006 /* | |
4007 * Set the preferred shell in the EMXSHELL environment variable (but | |
4008 * only if it is different from what is already in the environment). | |
4009 * Emx then takes care of whether to use "/c" or "-c" in an | |
4010 * intelligent way. Simply pass the whole thing to emx's system() call. | |
4011 * Emx also starts an interactive shell if system() is passed an empty | |
4012 * string. | |
4013 */ | |
4014 char_u *p, *old; | |
4015 | |
4016 if (((old = (char_u *)getenv("EMXSHELL")) == NULL) || STRCMP(old, p_sh)) | |
4017 { | |
4018 /* should check HAVE_SETENV, but I know we don't have it. */ | |
4019 p = alloc(10 + strlen(p_sh)); | |
4020 if (p) | |
4021 { | |
4022 sprintf((char *)p, "EMXSHELL=%s", p_sh); | |
4023 putenv((char *)p); /* don't free the pointer! */ | |
4024 } | |
4025 } | |
4026 # endif | |
4027 | |
4028 out_flush(); | |
4029 | |
4030 if (options & SHELL_COOKED) | |
4031 settmode(TMODE_COOK); /* set to normal mode */ | |
4032 | |
2586 | 4033 # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
4209 | 4034 save_clipboard(); |
2586 | 4035 loose_clipboard(); |
4036 # endif | |
4037 | |
7 | 4038 # ifdef __EMX__ |
4039 if (cmd == NULL) | |
4040 x = system(""); /* this starts an interactive shell in emx */ | |
4041 else | |
4042 x = system((char *)cmd); | |
4043 /* system() returns -1 when error occurs in starting shell */ | |
4044 if (x == -1 && !emsg_silent) | |
4045 { | |
4046 MSG_PUTS(_("\nCannot execute shell ")); | |
4047 msg_outtrans(p_sh); | |
4048 msg_putchar('\n'); | |
4049 } | |
4050 # else /* not __EMX__ */ | |
4051 if (cmd == NULL) | |
4052 x = system((char *)p_sh); | |
4053 else | |
4054 { | |
4055 # ifdef VMS | |
4056 if (ofn = strchr((char *)cmd, '>')) | |
4057 *ofn++ = '\0'; | |
4058 if (ifn = strchr((char *)cmd, '<')) | |
4059 { | |
4060 char *p; | |
4061 | |
4062 *ifn++ = '\0'; | |
4063 p = strchr(ifn,' '); /* chop off any trailing spaces */ | |
4064 if (p) | |
4065 *p = '\0'; | |
4066 } | |
4067 if (ofn) | |
4068 x = vms_sys((char *)cmd, ofn, ifn); | |
4069 else | |
4070 x = system((char *)cmd); | |
4071 # else | |
4072 newcmd = lalloc(STRLEN(p_sh) | |
4073 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg)) | |
4074 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE); | |
4075 if (newcmd == NULL) | |
4076 x = 0; | |
4077 else | |
4078 { | |
4079 sprintf((char *)newcmd, "%s %s %s %s", p_sh, | |
4080 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg, | |
4081 (char *)p_shcf, | |
4082 (char *)cmd); | |
4083 x = system((char *)newcmd); | |
4084 vim_free(newcmd); | |
4085 } | |
4086 # endif | |
4087 } | |
4088 # ifdef VMS | |
4089 x = vms_sys_status(x); | |
4090 # endif | |
4091 if (emsg_silent) | |
4092 ; | |
4093 else if (x == 127) | |
4094 MSG_PUTS(_("\nCannot execute shell sh\n")); | |
4095 # endif /* __EMX__ */ | |
4096 else if (x && !(options & SHELL_SILENT)) | |
4097 { | |
4098 MSG_PUTS(_("\nshell returned ")); | |
4099 msg_outnum((long)x); | |
4100 msg_putchar('\n'); | |
4101 } | |
4102 | |
4103 if (tmode == TMODE_RAW) | |
4104 settmode(TMODE_RAW); /* set to raw mode */ | |
4105 # ifdef FEAT_TITLE | |
4106 resettitle(); | |
4107 # endif | |
4209 | 4108 # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
4109 restore_clipboard(); | |
4110 # endif | |
7 | 4111 return x; |
4112 | |
4113 #else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */ | |
4114 | |
167 | 4115 # define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use |
4116 127, some shells use that already */ | |
7 | 4117 |
4118 char_u *newcmd = NULL; | |
4119 pid_t pid; | |
167 | 4120 pid_t wpid = 0; |
7 | 4121 pid_t wait_pid = 0; |
4122 # ifdef HAVE_UNION_WAIT | |
4123 union wait status; | |
4124 # else | |
4125 int status = -1; | |
4126 # endif | |
4127 int retval = -1; | |
4128 char **argv = NULL; | |
4129 int argc; | |
2994 | 4130 char_u *p_shcf_copy = NULL; |
7 | 4131 int i; |
4132 char_u *p; | |
4133 int inquote; | |
167 | 4134 int pty_master_fd = -1; /* for pty's */ |
7 | 4135 # ifdef FEAT_GUI |
4136 int pty_slave_fd = -1; | |
4137 char *tty_name; | |
167 | 4138 # endif |
602 | 4139 int fd_toshell[2]; /* for pipes */ |
7 | 4140 int fd_fromshell[2]; |
4141 int pipe_error = FALSE; | |
167 | 4142 # ifdef HAVE_SETENV |
7 | 4143 char envbuf[50]; |
167 | 4144 # else |
7 | 4145 static char envbuf_Rows[20]; |
4146 static char envbuf_Columns[20]; | |
4147 # endif | |
602 | 4148 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */ |
7 | 4149 |
2586 | 4150 newcmd = vim_strsave(p_sh); |
4151 if (newcmd == NULL) /* out of memory */ | |
4152 goto error; | |
4153 | |
7 | 4154 out_flush(); |
4155 if (options & SHELL_COOKED) | |
4156 settmode(TMODE_COOK); /* set to normal mode */ | |
4157 | |
602 | 4158 /* |
4159 * Do this loop twice: | |
4160 * 1: find number of arguments | |
4161 * 2: separate them and build argv[] | |
4162 */ | |
7 | 4163 for (i = 0; i < 2; ++i) |
4164 { | |
4165 p = newcmd; | |
4166 inquote = FALSE; | |
4167 argc = 0; | |
4168 for (;;) | |
4169 { | |
4170 if (i == 1) | |
4171 argv[argc] = (char *)p; | |
4172 ++argc; | |
4173 while (*p && (inquote || (*p != ' ' && *p != TAB))) | |
4174 { | |
4175 if (*p == '"') | |
4176 inquote = !inquote; | |
4177 ++p; | |
4178 } | |
4179 if (*p == NUL) | |
4180 break; | |
4181 if (i == 1) | |
4182 *p++ = NUL; | |
4183 p = skipwhite(p); | |
4184 } | |
840 | 4185 if (argv == NULL) |
7 | 4186 { |
2994 | 4187 /* |
4188 * Account for possible multiple args in p_shcf. | |
4189 */ | |
4190 p = p_shcf; | |
4191 for (;;) | |
4192 { | |
4193 p = skiptowhite(p); | |
4194 if (*p == NUL) | |
4195 break; | |
4196 ++argc; | |
4197 p = skipwhite(p); | |
4198 } | |
4199 | |
7 | 4200 argv = (char **)alloc((unsigned)((argc + 4) * sizeof(char *))); |
4201 if (argv == NULL) /* out of memory */ | |
4202 goto error; | |
4203 } | |
4204 } | |
4205 if (cmd != NULL) | |
4206 { | |
3263 | 4207 char_u *s; |
4208 | |
7 | 4209 if (extra_shell_arg != NULL) |
4210 argv[argc++] = (char *)extra_shell_arg; | |
2994 | 4211 |
4212 /* Break 'shellcmdflag' into white separated parts. This doesn't | |
4213 * handle quoted strings, they are very unlikely to appear. */ | |
4214 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1); | |
4215 if (p_shcf_copy == NULL) /* out of memory */ | |
4216 goto error; | |
4217 s = p_shcf_copy; | |
4218 p = p_shcf; | |
4219 while (*p != NUL) | |
4220 { | |
4221 argv[argc++] = (char *)s; | |
4222 while (*p && *p != ' ' && *p != TAB) | |
4223 *s++ = *p++; | |
4224 *s++ = NUL; | |
4225 p = skipwhite(p); | |
4226 } | |
4227 | |
7 | 4228 argv[argc++] = (char *)cmd; |
4229 } | |
4230 argv[argc] = NULL; | |
4231 | |
167 | 4232 /* |
4233 * For the GUI, when writing the output into the buffer and when reading | |
4234 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout | |
4235 * of the executed command into the Vim window. Or use a pipe. | |
4236 */ | |
4237 if ((options & (SHELL_READ|SHELL_WRITE)) | |
7 | 4238 # ifdef FEAT_GUI |
167 | 4239 || (gui.in_use && show_shell_mess) |
4240 # endif | |
4241 ) | |
7 | 4242 { |
167 | 4243 # ifdef FEAT_GUI |
7 | 4244 /* |
4245 * Try to open a master pty. | |
4246 * If this works, open the slave pty. | |
4247 * If the slave can't be opened, close the master pty. | |
4248 */ | |
167 | 4249 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE))) |
7 | 4250 { |
4251 pty_master_fd = OpenPTY(&tty_name); /* open pty */ | |
2992 | 4252 if (pty_master_fd >= 0) |
7 | 4253 { |
2992 | 4254 /* Leaving out O_NOCTTY may lead to waitpid() always returning |
4255 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume | |
4256 * adding O_NOCTTY always works when defined. */ | |
4257 #ifdef O_NOCTTY | |
4258 pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0); | |
4259 #else | |
4260 pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0); | |
4261 #endif | |
4262 if (pty_slave_fd < 0) | |
4263 { | |
4264 close(pty_master_fd); | |
4265 pty_master_fd = -1; | |
4266 } | |
7 | 4267 } |
4268 } | |
4269 /* | |
4270 * If not opening a pty or it didn't work, try using pipes. | |
4271 */ | |
4272 if (pty_master_fd < 0) | |
167 | 4273 # endif |
7 | 4274 { |
4275 pipe_error = (pipe(fd_toshell) < 0); | |
4276 if (!pipe_error) /* pipe create OK */ | |
4277 { | |
4278 pipe_error = (pipe(fd_fromshell) < 0); | |
4279 if (pipe_error) /* pipe create failed */ | |
4280 { | |
4281 close(fd_toshell[0]); | |
4282 close(fd_toshell[1]); | |
4283 } | |
4284 } | |
4285 if (pipe_error) | |
4286 { | |
4287 MSG_PUTS(_("\nCannot create pipes\n")); | |
4288 out_flush(); | |
4289 } | |
4290 } | |
4291 } | |
4292 | |
4293 if (!pipe_error) /* pty or pipe opened or not used */ | |
4294 { | |
4295 # ifdef __BEOS__ | |
4296 beos_cleanup_read_thread(); | |
4297 # endif | |
602 | 4298 |
7 | 4299 if ((pid = fork()) == -1) /* maybe we should use vfork() */ |
4300 { | |
4301 MSG_PUTS(_("\nCannot fork\n")); | |
167 | 4302 if ((options & (SHELL_READ|SHELL_WRITE)) |
7 | 4303 # ifdef FEAT_GUI |
167 | 4304 || (gui.in_use && show_shell_mess) |
4305 # endif | |
4306 ) | |
7 | 4307 { |
167 | 4308 # ifdef FEAT_GUI |
7 | 4309 if (pty_master_fd >= 0) /* close the pseudo tty */ |
4310 { | |
4311 close(pty_master_fd); | |
4312 close(pty_slave_fd); | |
4313 } | |
4314 else /* close the pipes */ | |
167 | 4315 # endif |
7 | 4316 { |
4317 close(fd_toshell[0]); | |
4318 close(fd_toshell[1]); | |
4319 close(fd_fromshell[0]); | |
4320 close(fd_fromshell[1]); | |
4321 } | |
4322 } | |
4323 } | |
4324 else if (pid == 0) /* child */ | |
4325 { | |
4326 reset_signals(); /* handle signals normally */ | |
4327 | |
4328 if (!show_shell_mess || (options & SHELL_EXPAND)) | |
4329 { | |
4330 int fd; | |
4331 | |
4332 /* | |
4333 * Don't want to show any message from the shell. Can't just | |
4334 * close stdout and stderr though, because some systems will | |
4335 * break if you try to write to them after that, so we must | |
4336 * use dup() to replace them with something else -- webb | |
4337 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang, | |
4338 * waiting for input. | |
4339 */ | |
4340 fd = open("/dev/null", O_RDWR | O_EXTRA, 0); | |
4341 fclose(stdin); | |
4342 fclose(stdout); | |
4343 fclose(stderr); | |
4344 | |
4345 /* | |
4346 * If any of these open()'s and dup()'s fail, we just continue | |
4347 * anyway. It's not fatal, and on most systems it will make | |
4348 * no difference at all. On a few it will cause the execvp() | |
4349 * to exit with a non-zero status even when the completion | |
4350 * could be done, which is nothing too serious. If the open() | |
4351 * or dup() failed we'd just do the same thing ourselves | |
4352 * anyway -- webb | |
4353 */ | |
4354 if (fd >= 0) | |
4355 { | |
1757 | 4356 ignored = dup(fd); /* To replace stdin (fd 0) */ |
4357 ignored = dup(fd); /* To replace stdout (fd 1) */ | |
4358 ignored = dup(fd); /* To replace stderr (fd 2) */ | |
7 | 4359 |
4360 /* Don't need this now that we've duplicated it */ | |
4361 close(fd); | |
4362 } | |
4363 } | |
167 | 4364 else if ((options & (SHELL_READ|SHELL_WRITE)) |
7 | 4365 # ifdef FEAT_GUI |
167 | 4366 || gui.in_use |
4367 # endif | |
4368 ) | |
7 | 4369 { |
4370 | |
167 | 4371 # ifdef HAVE_SETSID |
602 | 4372 /* Create our own process group, so that the child and all its |
4373 * children can be kill()ed. Don't do this when using pipes, | |
1698 | 4374 * because stdin is not a tty, we would lose /dev/tty. */ |
602 | 4375 if (p_stmp) |
1799 | 4376 { |
602 | 4377 (void)setsid(); |
1799 | 4378 # if defined(SIGHUP) |
4379 /* When doing "!xterm&" and 'shell' is bash: the shell | |
4380 * will exit and send SIGHUP to all processes in its | |
4381 * group, killing the just started process. Ignore SIGHUP | |
4382 * to avoid that. (suggested by Simon Schubert) | |
4383 */ | |
4384 signal(SIGHUP, SIG_IGN); | |
4385 # endif | |
4386 } | |
167 | 4387 # endif |
4388 # ifdef FEAT_GUI | |
602 | 4389 if (pty_slave_fd >= 0) |
4390 { | |
4391 /* push stream discipline modules */ | |
4392 if (options & SHELL_COOKED) | |
4393 SetupSlavePTY(pty_slave_fd); | |
7 | 4394 # ifdef TIOCSCTTY |
602 | 4395 /* Try to become controlling tty (probably doesn't work, |
4396 * unless run by root) */ | |
4397 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL); | |
7 | 4398 # endif |
602 | 4399 } |
167 | 4400 # endif |
7 | 4401 /* Simulate to have a dumb terminal (for now) */ |
167 | 4402 # ifdef HAVE_SETENV |
7 | 4403 setenv("TERM", "dumb", 1); |
4404 sprintf((char *)envbuf, "%ld", Rows); | |
4405 setenv("ROWS", (char *)envbuf, 1); | |
4406 sprintf((char *)envbuf, "%ld", Rows); | |
4407 setenv("LINES", (char *)envbuf, 1); | |
4408 sprintf((char *)envbuf, "%ld", Columns); | |
4409 setenv("COLUMNS", (char *)envbuf, 1); | |
167 | 4410 # else |
7 | 4411 /* |
4412 * Putenv does not copy the string, it has to remain valid. | |
1698 | 4413 * Use a static array to avoid losing allocated memory. |
7 | 4414 */ |
4415 putenv("TERM=dumb"); | |
4416 sprintf(envbuf_Rows, "ROWS=%ld", Rows); | |
4417 putenv(envbuf_Rows); | |
4418 sprintf(envbuf_Rows, "LINES=%ld", Rows); | |
4419 putenv(envbuf_Rows); | |
4420 sprintf(envbuf_Columns, "COLUMNS=%ld", Columns); | |
4421 putenv(envbuf_Columns); | |
167 | 4422 # endif |
4423 | |
557 | 4424 /* |
4425 * stderr is only redirected when using the GUI, so that a | |
4426 * program like gpg can still access the terminal to get a | |
4427 * passphrase using stderr. | |
4428 */ | |
167 | 4429 # ifdef FEAT_GUI |
7 | 4430 if (pty_master_fd >= 0) |
4431 { | |
4432 close(pty_master_fd); /* close master side of pty */ | |
4433 | |
4434 /* set up stdin/stdout/stderr for the child */ | |
4435 close(0); | |
1757 | 4436 ignored = dup(pty_slave_fd); |
7 | 4437 close(1); |
1757 | 4438 ignored = dup(pty_slave_fd); |
557 | 4439 if (gui.in_use) |
4440 { | |
4441 close(2); | |
1757 | 4442 ignored = dup(pty_slave_fd); |
557 | 4443 } |
7 | 4444 |
4445 close(pty_slave_fd); /* has been dupped, close it now */ | |
4446 } | |
4447 else | |
167 | 4448 # endif |
7 | 4449 { |
4450 /* set up stdin for the child */ | |
4451 close(fd_toshell[1]); | |
4452 close(0); | |
1757 | 4453 ignored = dup(fd_toshell[0]); |
7 | 4454 close(fd_toshell[0]); |
4455 | |
4456 /* set up stdout for the child */ | |
4457 close(fd_fromshell[0]); | |
4458 close(1); | |
1757 | 4459 ignored = dup(fd_fromshell[1]); |
7 | 4460 close(fd_fromshell[1]); |
4461 | |
557 | 4462 # ifdef FEAT_GUI |
4463 if (gui.in_use) | |
4464 { | |
4465 /* set up stderr for the child */ | |
4466 close(2); | |
1757 | 4467 ignored = dup(1); |
557 | 4468 } |
4469 # endif | |
7 | 4470 } |
4471 } | |
167 | 4472 |
7 | 4473 /* |
4474 * There is no type cast for the argv, because the type may be | |
4475 * different on different machines. This may cause a warning | |
4476 * message with strict compilers, don't worry about it. | |
4477 * Call _exit() instead of exit() to avoid closing the connection | |
4478 * to the X server (esp. with GTK, which uses atexit()). | |
4479 */ | |
4480 execvp(argv[0], argv); | |
4481 _exit(EXEC_FAILED); /* exec failed, return failure code */ | |
4482 } | |
4483 else /* parent */ | |
4484 { | |
4485 /* | |
4486 * While child is running, ignore terminating signals. | |
167 | 4487 * Do catch CTRL-C, so that "got_int" is set. |
7 | 4488 */ |
4489 catch_signals(SIG_IGN, SIG_ERR); | |
167 | 4490 catch_int_signal(); |
7 | 4491 |
4492 /* | |
4493 * For the GUI we redirect stdin, stdout and stderr to our window. | |
167 | 4494 * This is also used to pipe stdin/stdout to/from the external |
4495 * command. | |
7 | 4496 */ |
167 | 4497 if ((options & (SHELL_READ|SHELL_WRITE)) |
4498 # ifdef FEAT_GUI | |
4499 || (gui.in_use && show_shell_mess) | |
4500 # endif | |
4501 ) | |
7 | 4502 { |
167 | 4503 # define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */ |
7 | 4504 char_u buffer[BUFLEN + 1]; |
167 | 4505 # ifdef FEAT_MBYTE |
7 | 4506 int buffer_off = 0; /* valid bytes in buffer[] */ |
167 | 4507 # endif |
7 | 4508 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */ |
4509 int ta_len = 0; /* valid bytes in ta_buf[] */ | |
4510 int len; | |
4511 int p_more_save; | |
4512 int old_State; | |
4513 int c; | |
4514 int toshell_fd; | |
4515 int fromshell_fd; | |
167 | 4516 garray_T ga; |
4517 int noread_cnt; | |
1823 | 4518 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
4519 struct timeval start_tv; | |
4520 # endif | |
167 | 4521 |
4522 # ifdef FEAT_GUI | |
7 | 4523 if (pty_master_fd >= 0) |
4524 { | |
4525 fromshell_fd = pty_master_fd; | |
4526 toshell_fd = dup(pty_master_fd); | |
4527 } | |
4528 else | |
167 | 4529 # endif |
7 | 4530 { |
4531 close(fd_toshell[0]); | |
4532 close(fd_fromshell[1]); | |
4533 toshell_fd = fd_toshell[1]; | |
4534 fromshell_fd = fd_fromshell[0]; | |
4535 } | |
4536 | |
4537 /* | |
4538 * Write to the child if there are typed characters. | |
4539 * Read from the child if there are characters available. | |
4540 * Repeat the reading a few times if more characters are | |
4541 * available. Need to check for typed keys now and then, but | |
4542 * not too often (delays when no chars are available). | |
4543 * This loop is quit if no characters can be read from the pty | |
4544 * (WaitForChar detected special condition), or there are no | |
4545 * characters available and the child has exited. | |
4546 * Only check if the child has exited when there is no more | |
4547 * output. The child may exit before all the output has | |
4548 * been printed. | |
4549 * | |
4550 * Currently this busy loops! | |
4551 * This can probably dead-lock when the write blocks! | |
4552 */ | |
4553 p_more_save = p_more; | |
4554 p_more = FALSE; | |
4555 old_State = State; | |
4556 State = EXTERNCMD; /* don't redraw at window resize */ | |
4557 | |
602 | 4558 if ((options & SHELL_WRITE) && toshell_fd >= 0) |
167 | 4559 { |
4560 /* Fork a process that will write the lines to the | |
4561 * external program. */ | |
4562 if ((wpid = fork()) == -1) | |
4563 { | |
4564 MSG_PUTS(_("\nCannot fork\n")); | |
4565 } | |
3048 | 4566 else if (wpid == 0) /* child */ |
167 | 4567 { |
4568 linenr_T lnum = curbuf->b_op_start.lnum; | |
4569 int written = 0; | |
944 | 4570 char_u *lp = ml_get(lnum); |
167 | 4571 size_t l; |
4572 | |
177 | 4573 close(fromshell_fd); |
167 | 4574 for (;;) |
4575 { | |
944 | 4576 l = STRLEN(lp + written); |
167 | 4577 if (l == 0) |
4578 len = 0; | |
944 | 4579 else if (lp[written] == NL) |
167 | 4580 /* NL -> NUL translation */ |
4581 len = write(toshell_fd, "", (size_t)1); | |
4582 else | |
4583 { | |
3263 | 4584 char_u *s = vim_strchr(lp + written, NL); |
4585 | |
944 | 4586 len = write(toshell_fd, (char *)lp + written, |
1877 | 4587 s == NULL ? l |
4588 : (size_t)(s - (lp + written))); | |
167 | 4589 } |
1877 | 4590 if (len == (int)l) |
167 | 4591 { |
4592 /* Finished a line, add a NL, unless this line | |
4593 * should not have one. */ | |
4594 if (lnum != curbuf->b_op_end.lnum | |
6933 | 4595 || (!curbuf->b_p_bin |
4596 && curbuf->b_p_fixeol) | |
2707 | 4597 || (lnum != curbuf->b_no_eol_lnum |
167 | 4598 && (lnum != |
4599 curbuf->b_ml.ml_line_count | |
4600 || curbuf->b_p_eol))) | |
1757 | 4601 ignored = write(toshell_fd, "\n", |
4602 (size_t)1); | |
167 | 4603 ++lnum; |
4604 if (lnum > curbuf->b_op_end.lnum) | |
4605 { | |
4606 /* finished all the lines, close pipe */ | |
4607 close(toshell_fd); | |
4608 toshell_fd = -1; | |
4609 break; | |
4610 } | |
944 | 4611 lp = ml_get(lnum); |
167 | 4612 written = 0; |
4613 } | |
4614 else if (len > 0) | |
4615 written += len; | |
4616 } | |
4617 _exit(0); | |
4618 } | |
3048 | 4619 else /* parent */ |
167 | 4620 { |
4621 close(toshell_fd); | |
4622 toshell_fd = -1; | |
4623 } | |
4624 } | |
4625 | |
4626 if (options & SHELL_READ) | |
4627 ga_init2(&ga, 1, BUFLEN); | |
4628 | |
4629 noread_cnt = 0; | |
1823 | 4630 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
4631 gettimeofday(&start_tv, NULL); | |
4632 # endif | |
7 | 4633 for (;;) |
4634 { | |
4635 /* | |
4636 * Check if keys have been typed, write them to the child | |
592 | 4637 * if there are any. |
4638 * Don't do this if we are expanding wild cards (would eat | |
4639 * typeahead). | |
4640 * Don't do this when filtering and terminal is in cooked | |
4641 * mode, the shell command will handle the I/O. Avoids | |
4642 * that a typed password is echoed for ssh or gpg command. | |
602 | 4643 * Don't get characters when the child has already |
4644 * finished (wait_pid == 0). | |
167 | 4645 * Don't read characters unless we didn't get output for a |
1823 | 4646 * while (noread_cnt > 4), avoids that ":r !ls" eats |
4647 * typeahead. | |
7 | 4648 */ |
4649 len = 0; | |
4650 if (!(options & SHELL_EXPAND) | |
592 | 4651 && ((options & |
4652 (SHELL_READ|SHELL_WRITE|SHELL_COOKED)) | |
4653 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED) | |
1823 | 4654 # ifdef FEAT_GUI |
592 | 4655 || gui.in_use |
1823 | 4656 # endif |
592 | 4657 ) |
602 | 4658 && wait_pid == 0 |
1823 | 4659 && (ta_len > 0 || noread_cnt > 4)) |
7 | 4660 { |
1823 | 4661 if (ta_len == 0) |
4662 { | |
4663 /* Get extra characters when we don't have any. | |
4664 * Reset the counter and timer. */ | |
4665 noread_cnt = 0; | |
4666 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) | |
4667 gettimeofday(&start_tv, NULL); | |
4668 # endif | |
4669 len = ui_inchar(ta_buf, BUFLEN, 10L, 0); | |
4670 } | |
4671 if (ta_len > 0 || len > 0) | |
4672 { | |
7 | 4673 /* |
4674 * For pipes: | |
4675 * Check for CTRL-C: send interrupt signal to child. | |
4676 * Check for CTRL-D: EOF, close pipe to child. | |
4677 */ | |
4678 if (len == 1 && (pty_master_fd < 0 || cmd != NULL)) | |
4679 { | |
167 | 4680 # ifdef SIGINT |
7 | 4681 /* |
4682 * Send SIGINT to the child's group or all | |
4683 * processes in our group. | |
4684 */ | |
4685 if (ta_buf[ta_len] == Ctrl_C | |
4686 || ta_buf[ta_len] == intr_char) | |
167 | 4687 { |
4688 # ifdef HAVE_SETSID | |
7 | 4689 kill(-pid, SIGINT); |
167 | 4690 # else |
7 | 4691 kill(0, SIGINT); |
4692 # endif | |
167 | 4693 if (wpid > 0) |
4694 kill(wpid, SIGINT); | |
4695 } | |
4696 # endif | |
7 | 4697 if (pty_master_fd < 0 && toshell_fd >= 0 |
4698 && ta_buf[ta_len] == Ctrl_D) | |
4699 { | |
4700 close(toshell_fd); | |
4701 toshell_fd = -1; | |
4702 } | |
4703 } | |
4704 | |
4705 /* replace K_BS by <BS> and K_DEL by <DEL> */ | |
4706 for (i = ta_len; i < ta_len + len; ++i) | |
4707 { | |
4708 if (ta_buf[i] == CSI && len - i > 2) | |
4709 { | |
4710 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]); | |
4711 if (c == K_DEL || c == K_KDEL || c == K_BS) | |
4712 { | |
4713 mch_memmove(ta_buf + i + 1, ta_buf + i + 3, | |
4714 (size_t)(len - i - 2)); | |
4715 if (c == K_DEL || c == K_KDEL) | |
4716 ta_buf[i] = DEL; | |
4717 else | |
4718 ta_buf[i] = Ctrl_H; | |
4719 len -= 2; | |
4720 } | |
4721 } | |
4722 else if (ta_buf[i] == '\r') | |
4723 ta_buf[i] = '\n'; | |
167 | 4724 # ifdef FEAT_MBYTE |
7 | 4725 if (has_mbyte) |
1903 | 4726 i += (*mb_ptr2len_len)(ta_buf + i, |
4727 ta_len + len - i) - 1; | |
167 | 4728 # endif |
7 | 4729 } |
4730 | |
4731 /* | |
4732 * For pipes: echo the typed characters. | |
4733 * For a pty this does not seem to work. | |
4734 */ | |
4735 if (pty_master_fd < 0) | |
4736 { | |
4737 for (i = ta_len; i < ta_len + len; ++i) | |
4738 { | |
4739 if (ta_buf[i] == '\n' || ta_buf[i] == '\b') | |
4740 msg_putchar(ta_buf[i]); | |
167 | 4741 # ifdef FEAT_MBYTE |
7 | 4742 else if (has_mbyte) |
4743 { | |
474 | 4744 int l = (*mb_ptr2len)(ta_buf + i); |
7 | 4745 |
4746 msg_outtrans_len(ta_buf + i, l); | |
4747 i += l - 1; | |
4748 } | |
167 | 4749 # endif |
7 | 4750 else |
4751 msg_outtrans_len(ta_buf + i, 1); | |
4752 } | |
4753 windgoto(msg_row, msg_col); | |
4754 out_flush(); | |
4755 } | |
4756 | |
4757 ta_len += len; | |
4758 | |
4759 /* | |
4760 * Write the characters to the child, unless EOF has | |
4761 * been typed for pipes. Write one character at a | |
1698 | 4762 * time, to avoid losing too much typeahead. |
167 | 4763 * When writing buffer lines, drop the typed |
4764 * characters (only check for CTRL-C). | |
7 | 4765 */ |
167 | 4766 if (options & SHELL_WRITE) |
4767 ta_len = 0; | |
4768 else if (toshell_fd >= 0) | |
7 | 4769 { |
4770 len = write(toshell_fd, (char *)ta_buf, (size_t)1); | |
4771 if (len > 0) | |
4772 { | |
4773 ta_len -= len; | |
4774 mch_memmove(ta_buf, ta_buf + len, ta_len); | |
4775 } | |
4776 } | |
1823 | 4777 } |
7 | 4778 } |
4779 | |
167 | 4780 if (got_int) |
4781 { | |
4782 /* CTRL-C sends a signal to the child, we ignore it | |
4783 * ourselves */ | |
4784 # ifdef HAVE_SETSID | |
4785 kill(-pid, SIGINT); | |
4786 # else | |
4787 kill(0, SIGINT); | |
4788 # endif | |
4789 if (wpid > 0) | |
4790 kill(wpid, SIGINT); | |
4791 got_int = FALSE; | |
4792 } | |
4793 | |
7 | 4794 /* |
4795 * Check if the child has any characters to be printed. | |
4796 * Read them and write them to our window. Repeat this as | |
4797 * long as there is something to do, avoid the 10ms wait | |
4798 * for mch_inchar(), or sending typeahead characters to | |
4799 * the external process. | |
4800 * TODO: This should handle escape sequences, compatible | |
4801 * to some terminal (vt52?). | |
4802 */ | |
167 | 4803 ++noread_cnt; |
7 | 4804 while (RealWaitForChar(fromshell_fd, 10L, NULL)) |
4805 { | |
2664 | 4806 len = read_eintr(fromshell_fd, buffer |
167 | 4807 # ifdef FEAT_MBYTE |
7 | 4808 + buffer_off, (size_t)(BUFLEN - buffer_off) |
167 | 4809 # else |
7 | 4810 , (size_t)BUFLEN |
167 | 4811 # endif |
7 | 4812 ); |
4813 if (len <= 0) /* end of file or error */ | |
4814 goto finished; | |
167 | 4815 |
4816 noread_cnt = 0; | |
4817 if (options & SHELL_READ) | |
4818 { | |
4819 /* Do NUL -> NL translation, append NL separated | |
4820 * lines to the current buffer. */ | |
4821 for (i = 0; i < len; ++i) | |
4822 { | |
4823 if (buffer[i] == NL) | |
4824 append_ga_line(&ga); | |
4825 else if (buffer[i] == NUL) | |
4826 ga_append(&ga, NL); | |
4827 else | |
4828 ga_append(&ga, buffer[i]); | |
4829 } | |
4830 } | |
4831 # ifdef FEAT_MBYTE | |
4832 else if (has_mbyte) | |
7 | 4833 { |
4834 int l; | |
4835 | |
167 | 4836 len += buffer_off; |
4837 buffer[len] = NUL; | |
4838 | |
7 | 4839 /* Check if the last character in buffer[] is |
4840 * incomplete, keep these bytes for the next | |
4841 * round. */ | |
4842 for (p = buffer; p < buffer + len; p += l) | |
4843 { | |
474 | 4844 l = mb_cptr2len(p); |
7 | 4845 if (l == 0) |
4846 l = 1; /* NUL byte? */ | |
4847 else if (MB_BYTE2LEN(*p) != l) | |
4848 break; | |
4849 } | |
4850 if (p == buffer) /* no complete character */ | |
4851 { | |
4852 /* avoid getting stuck at an illegal byte */ | |
4853 if (len >= 12) | |
4854 ++p; | |
4855 else | |
4856 { | |
4857 buffer_off = len; | |
4858 continue; | |
4859 } | |
4860 } | |
4861 c = *p; | |
4862 *p = NUL; | |
4863 msg_puts(buffer); | |
4864 if (p < buffer + len) | |
4865 { | |
4866 *p = c; | |
4867 buffer_off = (buffer + len) - p; | |
4868 mch_memmove(buffer, p, buffer_off); | |
4869 continue; | |
4870 } | |
4871 buffer_off = 0; | |
4872 } | |
167 | 4873 # endif /* FEAT_MBYTE */ |
7 | 4874 else |
4875 { | |
4876 buffer[len] = NUL; | |
4877 msg_puts(buffer); | |
4878 } | |
4879 | |
4880 windgoto(msg_row, msg_col); | |
4881 cursor_on(); | |
4882 out_flush(); | |
4883 if (got_int) | |
4884 break; | |
1823 | 4885 |
4886 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) | |
4887 { | |
4888 struct timeval now_tv; | |
4889 long msec; | |
4890 | |
4891 /* Avoid that we keep looping here without | |
4892 * checking for a CTRL-C for a long time. Don't | |
4893 * break out too often to avoid losing typeahead. */ | |
4894 gettimeofday(&now_tv, NULL); | |
4895 msec = (now_tv.tv_sec - start_tv.tv_sec) * 1000L | |
4896 + (now_tv.tv_usec - start_tv.tv_usec) / 1000L; | |
4897 if (msec > 2000) | |
4898 { | |
4899 noread_cnt = 5; | |
4900 break; | |
4901 } | |
4902 } | |
4903 # endif | |
7 | 4904 } |
4905 | |
602 | 4906 /* If we already detected the child has finished break the |
4907 * loop now. */ | |
4908 if (wait_pid == pid) | |
4909 break; | |
4910 | |
7 | 4911 /* |
4912 * Check if the child still exists, before checking for | |
1698 | 4913 * typed characters (otherwise we would lose typeahead). |
7 | 4914 */ |
167 | 4915 # ifdef __NeXT__ |
3048 | 4916 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0); |
167 | 4917 # else |
7 | 4918 wait_pid = waitpid(pid, &status, WNOHANG); |
167 | 4919 # endif |
7 | 4920 if ((wait_pid == (pid_t)-1 && errno == ECHILD) |
4921 || (wait_pid == pid && WIFEXITED(status))) | |
4922 { | |
602 | 4923 /* Don't break the loop yet, try reading more |
4924 * characters from "fromshell_fd" first. When using | |
4925 * pipes there might still be something to read and | |
4926 * then we'll break the loop at the "break" above. */ | |
7 | 4927 wait_pid = pid; |
4928 } | |
602 | 4929 else |
4930 wait_pid = 0; | |
4230 | 4931 |
4248 | 4932 # if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11) |
4230 | 4933 /* Handle any X events, e.g. serving the clipboard. */ |
4934 clip_update(); | |
4935 # endif | |
7 | 4936 } |
4937 finished: | |
4938 p_more = p_more_save; | |
167 | 4939 if (options & SHELL_READ) |
4940 { | |
4941 if (ga.ga_len > 0) | |
4942 { | |
4943 append_ga_line(&ga); | |
4944 /* remember that the NL was missing */ | |
2707 | 4945 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum; |
167 | 4946 } |
4947 else | |
2707 | 4948 curbuf->b_no_eol_lnum = 0; |
167 | 4949 ga_clear(&ga); |
4950 } | |
4951 | |
7 | 4952 /* |
4953 * Give all typeahead that wasn't used back to ui_inchar(). | |
4954 */ | |
4955 if (ta_len) | |
4956 ui_inchar_undo(ta_buf, ta_len); | |
4957 State = old_State; | |
4958 if (toshell_fd >= 0) | |
4959 close(toshell_fd); | |
4960 close(fromshell_fd); | |
4961 } | |
4248 | 4962 # if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11) |
4230 | 4963 else |
4964 { | |
4965 /* | |
4966 * Similar to the loop above, but only handle X events, no | |
4967 * I/O. | |
4968 */ | |
4969 for (;;) | |
4970 { | |
4971 if (got_int) | |
4972 { | |
4973 /* CTRL-C sends a signal to the child, we ignore it | |
4974 * ourselves */ | |
4975 # ifdef HAVE_SETSID | |
4976 kill(-pid, SIGINT); | |
4977 # else | |
4978 kill(0, SIGINT); | |
4979 # endif | |
4980 got_int = FALSE; | |
4981 } | |
4982 # ifdef __NeXT__ | |
4983 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0); | |
4984 # else | |
4985 wait_pid = waitpid(pid, &status, WNOHANG); | |
4986 # endif | |
4987 if ((wait_pid == (pid_t)-1 && errno == ECHILD) | |
4988 || (wait_pid == pid && WIFEXITED(status))) | |
4989 { | |
4990 wait_pid = pid; | |
4991 break; | |
4992 } | |
4993 | |
4994 /* Handle any X events, e.g. serving the clipboard. */ | |
4995 clip_update(); | |
4996 | |
4997 mch_delay(10L, TRUE); | |
4998 } | |
4999 } | |
5000 # endif | |
7 | 5001 |
5002 /* | |
5003 * Wait until our child has exited. | |
5004 * Ignore wait() returning pids of other children and returning | |
5005 * because of some signal like SIGWINCH. | |
5006 * Don't wait if wait_pid was already set above, indicating the | |
5007 * child already exited. | |
5008 */ | |
3048 | 5009 if (wait_pid != pid) |
5010 wait_pid = wait4pid(pid, &status); | |
7 | 5011 |
2600 | 5012 # ifdef FEAT_GUI |
5013 /* Close slave side of pty. Only do this after the child has | |
5014 * exited, otherwise the child may hang when it tries to write on | |
5015 * the pty. */ | |
5016 if (pty_master_fd >= 0) | |
5017 close(pty_slave_fd); | |
5018 # endif | |
5019 | |
167 | 5020 /* Make sure the child that writes to the external program is |
5021 * dead. */ | |
5022 if (wpid > 0) | |
3048 | 5023 { |
167 | 5024 kill(wpid, SIGKILL); |
3048 | 5025 wait4pid(wpid, NULL); |
5026 } | |
167 | 5027 |
7 | 5028 /* |
5029 * Set to raw mode right now, otherwise a CTRL-C after | |
5030 * catch_signals() will kill Vim. | |
5031 */ | |
5032 if (tmode == TMODE_RAW) | |
5033 settmode(TMODE_RAW); | |
5034 did_settmode = TRUE; | |
5035 set_signals(); | |
5036 | |
5037 if (WIFEXITED(status)) | |
5038 { | |
129 | 5039 /* LINTED avoid "bitwise operation on signed value" */ |
7 | 5040 retval = WEXITSTATUS(status); |
4074 | 5041 if (retval != 0 && !emsg_silent) |
7 | 5042 { |
5043 if (retval == EXEC_FAILED) | |
5044 { | |
5045 MSG_PUTS(_("\nCannot execute shell ")); | |
5046 msg_outtrans(p_sh); | |
5047 msg_putchar('\n'); | |
5048 } | |
5049 else if (!(options & SHELL_SILENT)) | |
5050 { | |
5051 MSG_PUTS(_("\nshell returned ")); | |
5052 msg_outnum((long)retval); | |
5053 msg_putchar('\n'); | |
5054 } | |
5055 } | |
5056 } | |
5057 else | |
5058 MSG_PUTS(_("\nCommand terminated\n")); | |
5059 } | |
5060 } | |
5061 vim_free(argv); | |
2994 | 5062 vim_free(p_shcf_copy); |
7 | 5063 |
5064 error: | |
5065 if (!did_settmode) | |
5066 if (tmode == TMODE_RAW) | |
5067 settmode(TMODE_RAW); /* set to raw mode */ | |
5068 # ifdef FEAT_TITLE | |
5069 resettitle(); | |
5070 # endif | |
5071 vim_free(newcmd); | |
5072 | |
5073 return retval; | |
5074 | |
5075 #endif /* USE_SYSTEM */ | |
5076 } | |
5077 | |
5078 /* | |
5079 * Check for CTRL-C typed by reading all available characters. | |
5080 * In cooked mode we should get SIGINT, no need to check. | |
5081 */ | |
5082 void | |
5083 mch_breakcheck() | |
5084 { | |
5085 if (curr_tmode == TMODE_RAW && RealWaitForChar(read_cmd_fd, 0L, NULL)) | |
5086 fill_input_buf(FALSE); | |
5087 } | |
5088 | |
5089 /* | |
5090 * Wait "msec" msec until a character is available from the keyboard or from | |
5091 * inbuf[]. msec == -1 will block forever. | |
5092 * When a GUI is being used, this will never get called -- webb | |
5093 */ | |
5094 static int | |
5095 WaitForChar(msec) | |
5096 long msec; | |
5097 { | |
5098 #ifdef FEAT_MOUSE_GPM | |
5099 int gpm_process_wanted; | |
5100 #endif | |
5101 #ifdef FEAT_XCLIPBOARD | |
5102 int rest; | |
5103 #endif | |
5104 int avail; | |
5105 | |
5106 if (input_available()) /* something in inbuf[] */ | |
5107 return 1; | |
5108 | |
5109 #if defined(FEAT_MOUSE_DEC) | |
5110 /* May need to query the mouse position. */ | |
5111 if (WantQueryMouse) | |
5112 { | |
227 | 5113 WantQueryMouse = FALSE; |
7 | 5114 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5); |
5115 } | |
5116 #endif | |
5117 | |
5118 /* | |
5119 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse | |
5120 * events. This is a bit complicated, because they might both be defined. | |
5121 */ | |
5122 #if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD) | |
5123 # ifdef FEAT_XCLIPBOARD | |
5124 rest = 0; | |
5125 if (do_xterm_trace()) | |
5126 rest = msec; | |
5127 # endif | |
5128 do | |
5129 { | |
5130 # ifdef FEAT_XCLIPBOARD | |
5131 if (rest != 0) | |
5132 { | |
5133 msec = XT_TRACE_DELAY; | |
5134 if (rest >= 0 && rest < XT_TRACE_DELAY) | |
5135 msec = rest; | |
5136 if (rest >= 0) | |
5137 rest -= msec; | |
5138 } | |
5139 # endif | |
5140 # ifdef FEAT_MOUSE_GPM | |
5141 gpm_process_wanted = 0; | |
5142 avail = RealWaitForChar(read_cmd_fd, msec, &gpm_process_wanted); | |
5143 # else | |
5144 avail = RealWaitForChar(read_cmd_fd, msec, NULL); | |
5145 # endif | |
5146 if (!avail) | |
5147 { | |
5148 if (input_available()) | |
5149 return 1; | |
5150 # ifdef FEAT_XCLIPBOARD | |
5151 if (rest == 0 || !do_xterm_trace()) | |
5152 # endif | |
5153 break; | |
5154 } | |
5155 } | |
5156 while (FALSE | |
5157 # ifdef FEAT_MOUSE_GPM | |
5158 || (gpm_process_wanted && mch_gpm_process() == 0) | |
5159 # endif | |
5160 # ifdef FEAT_XCLIPBOARD | |
5161 || (!avail && rest != 0) | |
5162 # endif | |
5163 ); | |
5164 | |
5165 #else | |
5166 avail = RealWaitForChar(read_cmd_fd, msec, NULL); | |
5167 #endif | |
5168 return avail; | |
5169 } | |
5170 | |
5541 | 5171 #ifndef VMS |
7 | 5172 /* |
5173 * Wait "msec" msec until a character is available from file descriptor "fd". | |
3046 | 5174 * "msec" == 0 will check for characters once. |
5175 * "msec" == -1 will block until a character is available. | |
7 | 5176 * When a GUI is being used, this will not be used for input -- webb |
5177 * Returns also, when a request from Sniff is waiting -- toni. | |
5178 * 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
|
5179 * Or when a clientserver message is on the queue. |
7 | 5180 */ |
5181 #if defined(__BEOS__) | |
5182 int | |
5183 #else | |
5184 static int | |
5185 #endif | |
5186 RealWaitForChar(fd, msec, check_for_gpm) | |
5187 int fd; | |
5188 long msec; | |
1877 | 5189 int *check_for_gpm UNUSED; |
7 | 5190 { |
5191 int ret; | |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5192 #ifdef FEAT_NETBEANS_INTG |
2210 | 5193 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
|
5194 #endif |
14 | 5195 #if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME) |
7 | 5196 static int busy = FALSE; |
5197 | |
5198 /* May retry getting characters after an event was handled. */ | |
5199 # define MAY_LOOP | |
5200 | |
5201 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) | |
5202 /* Remember at what time we started, so that we know how much longer we | |
5203 * should wait after being interrupted. */ | |
5204 # define USE_START_TV | |
5205 struct timeval start_tv; | |
5206 | |
5207 if (msec > 0 && ( | |
5208 # ifdef FEAT_XCLIPBOARD | |
5209 xterm_Shell != (Widget)0 | |
14 | 5210 # if defined(USE_XSMP) || defined(FEAT_MZSCHEME) |
7 | 5211 || |
5212 # endif | |
5213 # endif | |
5214 # ifdef USE_XSMP | |
5215 xsmp_icefd != -1 | |
14 | 5216 # ifdef FEAT_MZSCHEME |
5217 || | |
5218 # endif | |
5219 # endif | |
5220 # ifdef FEAT_MZSCHEME | |
5221 (mzthreads_allowed() && p_mzq > 0) | |
7 | 5222 # endif |
5223 )) | |
5224 gettimeofday(&start_tv, NULL); | |
5225 # endif | |
5226 | |
5227 /* Handle being called recursively. This may happen for the session | |
5228 * manager stuff, it may save the file, which does a breakcheck. */ | |
5229 if (busy) | |
5230 return 0; | |
5231 #endif | |
5232 | |
5233 #ifdef MAY_LOOP | |
407 | 5234 for (;;) |
7 | 5235 #endif |
5236 { | |
5237 #ifdef MAY_LOOP | |
5238 int finished = TRUE; /* default is to 'loop' just once */ | |
14 | 5239 # ifdef FEAT_MZSCHEME |
5240 int mzquantum_used = FALSE; | |
5241 # endif | |
7 | 5242 #endif |
5243 #ifndef HAVE_SELECT | |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5244 struct pollfd fds[6]; |
7 | 5245 int nfd; |
5246 # ifdef FEAT_XCLIPBOARD | |
5247 int xterm_idx = -1; | |
5248 # endif | |
5249 # ifdef FEAT_MOUSE_GPM | |
5250 int gpm_idx = -1; | |
5251 # endif | |
5252 # ifdef USE_XSMP | |
5253 int xsmp_idx = -1; | |
5254 # endif | |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5255 # ifdef FEAT_NETBEANS_INTG |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5256 int nb_idx = -1; |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5257 # endif |
14 | 5258 int towait = (int)msec; |
5259 | |
5260 # ifdef FEAT_MZSCHEME | |
5261 mzvim_check_threads(); | |
5262 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq)) | |
5263 { | |
5264 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */ | |
5265 mzquantum_used = TRUE; | |
5266 } | |
5267 # endif | |
7 | 5268 fds[0].fd = fd; |
5269 fds[0].events = POLLIN; | |
5270 nfd = 1; | |
5271 | |
5272 # ifdef FEAT_SNIFF | |
5273 # define SNIFF_IDX 1 | |
5274 if (want_sniff_request) | |
5275 { | |
5276 fds[SNIFF_IDX].fd = fd_from_sniff; | |
5277 fds[SNIFF_IDX].events = POLLIN; | |
5278 nfd++; | |
5279 } | |
5280 # endif | |
5281 # ifdef FEAT_XCLIPBOARD | |
6383 | 5282 may_restore_clipboard(); |
7 | 5283 if (xterm_Shell != (Widget)0) |
5284 { | |
5285 xterm_idx = nfd; | |
5286 fds[nfd].fd = ConnectionNumber(xterm_dpy); | |
5287 fds[nfd].events = POLLIN; | |
5288 nfd++; | |
5289 } | |
5290 # endif | |
5291 # ifdef FEAT_MOUSE_GPM | |
5292 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0) | |
5293 { | |
5294 gpm_idx = nfd; | |
5295 fds[nfd].fd = gpm_fd; | |
5296 fds[nfd].events = POLLIN; | |
5297 nfd++; | |
5298 } | |
5299 # endif | |
5300 # ifdef USE_XSMP | |
5301 if (xsmp_icefd != -1) | |
5302 { | |
5303 xsmp_idx = nfd; | |
5304 fds[nfd].fd = xsmp_icefd; | |
5305 fds[nfd].events = POLLIN; | |
5306 nfd++; | |
5307 } | |
5308 # endif | |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5309 #ifdef FEAT_NETBEANS_INTG |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5310 if (nb_fd != -1) |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5311 { |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5312 nb_idx = nfd; |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5313 fds[nfd].fd = nb_fd; |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5314 fds[nfd].events = POLLIN; |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5315 nfd++; |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5316 } |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5317 #endif |
7 | 5318 |
14 | 5319 ret = poll(fds, nfd, towait); |
5320 # ifdef FEAT_MZSCHEME | |
5321 if (ret == 0 && mzquantum_used) | |
1450 | 5322 /* MzThreads scheduling is required and timeout occurred */ |
14 | 5323 finished = FALSE; |
5324 # endif | |
7 | 5325 |
5326 # ifdef FEAT_SNIFF | |
5327 if (ret < 0) | |
5328 sniff_disconnect(1); | |
5329 else if (want_sniff_request) | |
5330 { | |
5331 if (fds[SNIFF_IDX].revents & POLLHUP) | |
5332 sniff_disconnect(1); | |
5333 if (fds[SNIFF_IDX].revents & POLLIN) | |
5334 sniff_request_waiting = 1; | |
5335 } | |
5336 # endif | |
5337 # ifdef FEAT_XCLIPBOARD | |
5338 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN)) | |
5339 { | |
5340 xterm_update(); /* Maybe we should hand out clipboard */ | |
5341 if (--ret == 0 && !input_available()) | |
5342 /* Try again */ | |
5343 finished = FALSE; | |
5344 } | |
5345 # endif | |
5346 # ifdef FEAT_MOUSE_GPM | |
5347 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN)) | |
5348 { | |
5349 *check_for_gpm = 1; | |
5350 } | |
5351 # endif | |
5352 # ifdef USE_XSMP | |
5353 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP))) | |
5354 { | |
5355 if (fds[xsmp_idx].revents & POLLIN) | |
5356 { | |
5357 busy = TRUE; | |
5358 xsmp_handle_requests(); | |
5359 busy = FALSE; | |
5360 } | |
5361 else if (fds[xsmp_idx].revents & POLLHUP) | |
5362 { | |
5363 if (p_verbose > 0) | |
292 | 5364 verb_msg((char_u *)_("XSMP lost ICE connection")); |
7 | 5365 xsmp_close(); |
5366 } | |
5367 if (--ret == 0) | |
14 | 5368 finished = FALSE; /* Try again */ |
7 | 5369 } |
5370 # endif | |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5371 #ifdef FEAT_NETBEANS_INTG |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5372 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
|
5373 { |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5374 netbeans_read(); |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5375 --ret; |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5376 } |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5377 #endif |
7 | 5378 |
5379 | |
5380 #else /* HAVE_SELECT */ | |
5381 | |
5382 struct timeval tv; | |
14 | 5383 struct timeval *tvp; |
7 | 5384 fd_set rfds, efds; |
5385 int maxfd; | |
14 | 5386 long towait = msec; |
5387 | |
5388 # ifdef FEAT_MZSCHEME | |
5389 mzvim_check_threads(); | |
5390 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq)) | |
5391 { | |
5392 towait = p_mzq; /* don't wait longer than 'mzquantum' */ | |
5393 mzquantum_used = TRUE; | |
5394 } | |
5395 # endif | |
7 | 5396 # ifdef __EMX__ |
5397 /* don't check for incoming chars if not in raw mode, because select() | |
5398 * always returns TRUE then (in some version of emx.dll) */ | |
5399 if (curr_tmode != TMODE_RAW) | |
5400 return 0; | |
5401 # endif | |
5402 | |
14 | 5403 if (towait >= 0) |
7 | 5404 { |
14 | 5405 tv.tv_sec = towait / 1000; |
5406 tv.tv_usec = (towait % 1000) * (1000000/1000); | |
5407 tvp = &tv; | |
7 | 5408 } |
14 | 5409 else |
5410 tvp = NULL; | |
7 | 5411 |
5412 /* | |
5413 * Select on ready for reading and exceptional condition (end of file). | |
5414 */ | |
3046 | 5415 select_eintr: |
5416 FD_ZERO(&rfds); | |
7 | 5417 FD_ZERO(&efds); |
5418 FD_SET(fd, &rfds); | |
5419 # if !defined(__QNX__) && !defined(__CYGWIN32__) | |
5420 /* For QNX select() always returns 1 if this is set. Why? */ | |
5421 FD_SET(fd, &efds); | |
5422 # endif | |
5423 maxfd = fd; | |
5424 | |
5425 # ifdef FEAT_SNIFF | |
5426 if (want_sniff_request) | |
5427 { | |
5428 FD_SET(fd_from_sniff, &rfds); | |
5429 FD_SET(fd_from_sniff, &efds); | |
5430 if (maxfd < fd_from_sniff) | |
5431 maxfd = fd_from_sniff; | |
5432 } | |
5433 # endif | |
5434 # ifdef FEAT_XCLIPBOARD | |
6383 | 5435 may_restore_clipboard(); |
7 | 5436 if (xterm_Shell != (Widget)0) |
5437 { | |
5438 FD_SET(ConnectionNumber(xterm_dpy), &rfds); | |
5439 if (maxfd < ConnectionNumber(xterm_dpy)) | |
5440 maxfd = ConnectionNumber(xterm_dpy); | |
3748 | 5441 |
5442 /* An event may have already been read but not handled. In | |
5443 * particulary, XFlush may cause this. */ | |
5444 xterm_update(); | |
7 | 5445 } |
5446 # endif | |
5447 # ifdef FEAT_MOUSE_GPM | |
5448 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0) | |
5449 { | |
5450 FD_SET(gpm_fd, &rfds); | |
5451 FD_SET(gpm_fd, &efds); | |
5452 if (maxfd < gpm_fd) | |
5453 maxfd = gpm_fd; | |
5454 } | |
5455 # endif | |
5456 # ifdef USE_XSMP | |
5457 if (xsmp_icefd != -1) | |
5458 { | |
5459 FD_SET(xsmp_icefd, &rfds); | |
5460 FD_SET(xsmp_icefd, &efds); | |
5461 if (maxfd < xsmp_icefd) | |
5462 maxfd = xsmp_icefd; | |
5463 } | |
5464 # endif | |
3748 | 5465 # ifdef FEAT_NETBEANS_INTG |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5466 if (nb_fd != -1) |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5467 { |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5468 FD_SET(nb_fd, &rfds); |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5469 if (maxfd < nb_fd) |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5470 maxfd = nb_fd; |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5471 } |
3748 | 5472 # endif |
7 | 5473 |
14 | 5474 ret = select(maxfd + 1, &rfds, NULL, &efds, tvp); |
3046 | 5475 # ifdef EINTR |
5476 if (ret == -1 && errno == EINTR) | |
3133 | 5477 { |
5478 /* Check whether window has been resized, EINTR may be caused by | |
5479 * SIGWINCH. */ | |
5480 if (do_resize) | |
5481 handle_resize(); | |
5482 | |
3046 | 5483 /* Interrupted by a signal, need to try again. We ignore msec |
5484 * here, because we do want to check even after a timeout if | |
5485 * characters are available. Needed for reading output of an | |
5486 * external command after the process has finished. */ | |
5487 goto select_eintr; | |
3133 | 5488 } |
3046 | 5489 # endif |
1076 | 5490 # ifdef __TANDEM |
5491 if (ret == -1 && errno == ENOTSUP) | |
5492 { | |
5493 FD_ZERO(&rfds); | |
5494 FD_ZERO(&efds); | |
5495 ret = 0; | |
5496 } | |
3046 | 5497 # endif |
14 | 5498 # ifdef FEAT_MZSCHEME |
5499 if (ret == 0 && mzquantum_used) | |
1450 | 5500 /* loop if MzThreads must be scheduled and timeout occurred */ |
14 | 5501 finished = FALSE; |
7 | 5502 # endif |
5503 | |
5504 # ifdef FEAT_SNIFF | |
5505 if (ret < 0 ) | |
5506 sniff_disconnect(1); | |
5507 else if (ret > 0 && want_sniff_request) | |
5508 { | |
5509 if (FD_ISSET(fd_from_sniff, &efds)) | |
5510 sniff_disconnect(1); | |
5511 if (FD_ISSET(fd_from_sniff, &rfds)) | |
5512 sniff_request_waiting = 1; | |
5513 } | |
5514 # endif | |
5515 # ifdef FEAT_XCLIPBOARD | |
5516 if (ret > 0 && xterm_Shell != (Widget)0 | |
5517 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds)) | |
5518 { | |
5519 xterm_update(); /* Maybe we should hand out clipboard */ | |
5520 /* continue looping when we only got the X event and the input | |
5521 * buffer is empty */ | |
5522 if (--ret == 0 && !input_available()) | |
5523 { | |
5524 /* Try again */ | |
5525 finished = FALSE; | |
5526 } | |
5527 } | |
5528 # endif | |
5529 # ifdef FEAT_MOUSE_GPM | |
5530 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0) | |
5531 { | |
5532 if (FD_ISSET(gpm_fd, &efds)) | |
5533 gpm_close(); | |
5534 else if (FD_ISSET(gpm_fd, &rfds)) | |
5535 *check_for_gpm = 1; | |
5536 } | |
5537 # endif | |
5538 # ifdef USE_XSMP | |
5539 if (ret > 0 && xsmp_icefd != -1) | |
5540 { | |
5541 if (FD_ISSET(xsmp_icefd, &efds)) | |
5542 { | |
5543 if (p_verbose > 0) | |
292 | 5544 verb_msg((char_u *)_("XSMP lost ICE connection")); |
7 | 5545 xsmp_close(); |
5546 if (--ret == 0) | |
5547 finished = FALSE; /* keep going if event was only one */ | |
5548 } | |
5549 else if (FD_ISSET(xsmp_icefd, &rfds)) | |
5550 { | |
5551 busy = TRUE; | |
5552 xsmp_handle_requests(); | |
5553 busy = FALSE; | |
5554 if (--ret == 0) | |
5555 finished = FALSE; /* keep going if event was only one */ | |
5556 } | |
5557 } | |
5558 # endif | |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5559 #ifdef FEAT_NETBEANS_INTG |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5560 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
|
5561 { |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5562 netbeans_read(); |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5563 --ret; |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5564 } |
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2074
diff
changeset
|
5565 #endif |
7 | 5566 |
5567 #endif /* HAVE_SELECT */ | |
5568 | |
5569 #ifdef MAY_LOOP | |
5570 if (finished || msec == 0) | |
5571 break; | |
5572 | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
5573 # ifdef FEAT_CLIENTSERVER |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
5574 if (server_waiting()) |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
5575 break; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
5576 # endif |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
5577 |
7 | 5578 /* We're going to loop around again, find out for how long */ |
5579 if (msec > 0) | |
5580 { | |
5581 # ifdef USE_START_TV | |
5582 struct timeval mtv; | |
5583 | |
5584 /* Compute remaining wait time. */ | |
5585 gettimeofday(&mtv, NULL); | |
5586 msec -= (mtv.tv_sec - start_tv.tv_sec) * 1000L | |
5587 + (mtv.tv_usec - start_tv.tv_usec) / 1000L; | |
5588 # else | |
5589 /* Guess we got interrupted halfway. */ | |
5590 msec = msec / 2; | |
5591 # endif | |
5592 if (msec <= 0) | |
5593 break; /* waited long enough */ | |
5594 } | |
5595 #endif | |
5596 } | |
5597 | |
5598 return (ret > 0); | |
5599 } | |
5600 | |
5601 #ifndef NO_EXPANDPATH | |
5602 /* | |
444 | 5603 * Expand a path into all matching files and/or directories. Handles "*", |
5604 * "?", "[a-z]", "**", etc. | |
5605 * "path" has backslashes before chars that are not to be expanded. | |
5606 * Returns the number of matches found. | |
7 | 5607 */ |
5608 int | |
5609 mch_expandpath(gap, path, flags) | |
5610 garray_T *gap; | |
5611 char_u *path; | |
5612 int flags; /* EW_* flags */ | |
5613 { | |
444 | 5614 return unix_expandpath(gap, path, 0, flags, FALSE); |
7 | 5615 } |
5616 #endif | |
5617 | |
5618 /* | |
5619 * mch_expand_wildcards() - this code does wild-card pattern matching using | |
5620 * the shell | |
5621 * | |
5622 * return OK for success, FAIL for error (you may lose some memory) and put | |
5623 * an error message in *file. | |
5624 * | |
5625 * num_pat is number of input patterns | |
5626 * pat is array of pointers to input patterns | |
5627 * num_file is pointer to number of matched file names | |
5628 * file is pointer to array of pointers to matched file names | |
5629 */ | |
5630 | |
5631 #ifndef SEEK_SET | |
5632 # define SEEK_SET 0 | |
5633 #endif | |
5634 #ifndef SEEK_END | |
5635 # define SEEK_END 2 | |
5636 #endif | |
5637 | |
822 | 5638 #define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|" |
628 | 5639 |
7 | 5640 int |
5641 mch_expand_wildcards(num_pat, pat, num_file, file, flags) | |
5642 int num_pat; | |
5643 char_u **pat; | |
5644 int *num_file; | |
5645 char_u ***file; | |
5646 int flags; /* EW_* flags */ | |
5647 { | |
5648 int i; | |
5649 size_t len; | |
5650 char_u *p; | |
5651 int dir; | |
5652 #ifdef __EMX__ | |
1508 | 5653 /* |
5654 * This is the OS/2 implementation. | |
5655 */ | |
7 | 5656 # define EXPL_ALLOC_INC 16 |
5657 char_u **expl_files; | |
5658 size_t files_alloced, files_free; | |
5659 char_u *buf; | |
5660 int has_wildcard; | |
5661 | |
5662 *num_file = 0; /* default: no files found */ | |
5663 files_alloced = EXPL_ALLOC_INC; /* how much space is allocated */ | |
5664 files_free = EXPL_ALLOC_INC; /* how much space is not used */ | |
5665 *file = (char_u **)alloc(sizeof(char_u **) * files_alloced); | |
5666 if (*file == NULL) | |
5667 return FAIL; | |
5668 | |
5669 for (; num_pat > 0; num_pat--, pat++) | |
5670 { | |
5671 expl_files = NULL; | |
5672 if (vim_strchr(*pat, '$') || vim_strchr(*pat, '~')) | |
5673 /* expand environment var or home dir */ | |
5674 buf = expand_env_save(*pat); | |
5675 else | |
5676 buf = vim_strsave(*pat); | |
5677 expl_files = NULL; | |
99 | 5678 has_wildcard = mch_has_exp_wildcard(buf); /* (still) wildcards? */ |
7 | 5679 if (has_wildcard) /* yes, so expand them */ |
5680 expl_files = (char_u **)_fnexplode(buf); | |
5681 | |
5682 /* | |
5683 * return value of buf if no wildcards left, | |
5684 * OR if no match AND EW_NOTFOUND is set. | |
5685 */ | |
5686 if ((!has_wildcard && ((flags & EW_NOTFOUND) || mch_getperm(buf) >= 0)) | |
5687 || (expl_files == NULL && (flags & EW_NOTFOUND))) | |
5688 { /* simply save the current contents of *buf */ | |
5689 expl_files = (char_u **)alloc(sizeof(char_u **) * 2); | |
5690 if (expl_files != NULL) | |
5691 { | |
5692 expl_files[0] = vim_strsave(buf); | |
5693 expl_files[1] = NULL; | |
5694 } | |
5695 } | |
5696 vim_free(buf); | |
5697 | |
5698 /* | |
5699 * Count number of names resulting from expansion, | |
5700 * At the same time add a backslash to the end of names that happen to | |
5701 * be directories, and replace slashes with backslashes. | |
5702 */ | |
5703 if (expl_files) | |
5704 { | |
5705 for (i = 0; (p = expl_files[i]) != NULL; i++) | |
5706 { | |
5707 dir = mch_isdir(p); | |
5708 /* If we don't want dirs and this is one, skip it */ | |
5709 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE))) | |
5710 continue; | |
5711 | |
715 | 5712 /* Skip files that are not executable if we check for that. */ |
6695 | 5713 if (!dir && (flags & EW_EXEC) |
5714 && !mch_can_exe(p, NULL, !(flags & EW_SHELLCMD))) | |
715 | 5715 continue; |
5716 | |
7 | 5717 if (--files_free == 0) |
5718 { | |
5719 /* need more room in table of pointers */ | |
5720 files_alloced += EXPL_ALLOC_INC; | |
5721 *file = (char_u **)vim_realloc(*file, | |
5722 sizeof(char_u **) * files_alloced); | |
5723 if (*file == NULL) | |
5724 { | |
5725 EMSG(_(e_outofmem)); | |
5726 *num_file = 0; | |
5727 return FAIL; | |
5728 } | |
5729 files_free = EXPL_ALLOC_INC; | |
5730 } | |
5731 slash_adjust(p); | |
5732 if (dir) | |
5733 { | |
5734 /* For a directory we add a '/', unless it's already | |
5735 * there. */ | |
5736 len = STRLEN(p); | |
5737 if (((*file)[*num_file] = alloc(len + 2)) != NULL) | |
5738 { | |
5739 STRCPY((*file)[*num_file], p); | |
902 | 5740 if (!after_pathsep((*file)[*num_file], |
5741 (*file)[*num_file] + len)) | |
7 | 5742 { |
5743 (*file)[*num_file][len] = psepc; | |
39 | 5744 (*file)[*num_file][len + 1] = NUL; |
7 | 5745 } |
5746 } | |
5747 } | |
5748 else | |
5749 { | |
5750 (*file)[*num_file] = vim_strsave(p); | |
5751 } | |
5752 | |
5753 /* | |
5754 * Error message already given by either alloc or vim_strsave. | |
5755 * Should return FAIL, but returning OK works also. | |
5756 */ | |
5757 if ((*file)[*num_file] == NULL) | |
5758 break; | |
5759 (*num_file)++; | |
5760 } | |
5761 _fnexplodefree((char **)expl_files); | |
5762 } | |
5763 } | |
5764 return OK; | |
5765 | |
5766 #else /* __EMX__ */ | |
1508 | 5767 /* |
5768 * This is the non-OS/2 implementation (really Unix). | |
5769 */ | |
7 | 5770 int j; |
5771 char_u *tempname; | |
5772 char_u *command; | |
5773 FILE *fd; | |
5774 char_u *buffer; | |
1508 | 5775 #define STYLE_ECHO 0 /* use "echo", the default */ |
5776 #define STYLE_GLOB 1 /* use "glob", for csh */ | |
5777 #define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */ | |
5778 #define STYLE_PRINT 3 /* use "print -N", for zsh */ | |
5779 #define STYLE_BT 4 /* `cmd` expansion, execute the pattern | |
5780 * directly */ | |
7 | 5781 int shell_style = STYLE_ECHO; |
5782 int check_spaces; | |
5783 static int did_find_nul = FALSE; | |
5784 int ampersent = FALSE; | |
1508 | 5785 /* vimglob() function to define for Posix shell */ |
1620 | 5786 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >"; |
7 | 5787 |
5788 *num_file = 0; /* default: no files found */ | |
5789 *file = NULL; | |
5790 | |
5791 /* | |
5792 * If there are no wildcards, just copy the names to allocated memory. | |
5793 * Saves a lot of time, because we don't have to start a new shell. | |
5794 */ | |
5795 if (!have_wildcard(num_pat, pat)) | |
5796 return save_patterns(num_pat, pat, num_file, file); | |
5797 | |
428 | 5798 # ifdef HAVE_SANDBOX |
5799 /* Don't allow any shell command in the sandbox. */ | |
5800 if (sandbox != 0 && check_secure()) | |
5801 return FAIL; | |
5802 # endif | |
5803 | |
7 | 5804 /* |
5805 * Don't allow the use of backticks in secure and restricted mode. | |
5806 */ | |
428 | 5807 if (secure || restricted) |
7 | 5808 for (i = 0; i < num_pat; ++i) |
5809 if (vim_strchr(pat[i], '`') != NULL | |
5810 && (check_restricted() || check_secure())) | |
5811 return FAIL; | |
5812 | |
5813 /* | |
5814 * get a name for the temp file | |
5815 */ | |
6721 | 5816 if ((tempname = vim_tempname('o', FALSE)) == NULL) |
7 | 5817 { |
5818 EMSG(_(e_notmp)); | |
5819 return FAIL; | |
5820 } | |
5821 | |
5822 /* | |
5823 * Let the shell expand the patterns and write the result into the temp | |
1508 | 5824 * file. |
5825 * STYLE_BT: NL separated | |
5826 * If expanding `cmd` execute it directly. | |
5827 * STYLE_GLOB: NUL separated | |
5828 * If we use *csh, "glob" will work better than "echo". | |
5829 * STYLE_PRINT: NL or NUL separated | |
5830 * If we use *zsh, "print -N" will work better than "glob". | |
5831 * STYLE_VIMGLOB: NL separated | |
5832 * If we use *sh*, we define "vimglob()". | |
5833 * STYLE_ECHO: space separated. | |
5834 * A shell we don't know, stay safe and use "echo". | |
7 | 5835 */ |
5836 if (num_pat == 1 && *pat[0] == '`' | |
5837 && (len = STRLEN(pat[0])) > 2 | |
5838 && *(pat[0] + len - 1) == '`') | |
5839 shell_style = STYLE_BT; | |
5840 else if ((len = STRLEN(p_sh)) >= 3) | |
5841 { | |
5842 if (STRCMP(p_sh + len - 3, "csh") == 0) | |
5843 shell_style = STYLE_GLOB; | |
5844 else if (STRCMP(p_sh + len - 3, "zsh") == 0) | |
5845 shell_style = STYLE_PRINT; | |
5846 } | |
1508 | 5847 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh), |
5848 "sh") != NULL) | |
5849 shell_style = STYLE_VIMGLOB; | |
5850 | |
5851 /* Compute the length of the command. We need 2 extra bytes: for the | |
5852 * optional '&' and for the NUL. | |
5853 * Worst case: "unset nonomatch; print -N >" plus two is 29 */ | |
7 | 5854 len = STRLEN(tempname) + 29; |
1508 | 5855 if (shell_style == STYLE_VIMGLOB) |
5856 len += STRLEN(sh_vimglob_func); | |
5857 | |
147 | 5858 for (i = 0; i < num_pat; ++i) |
5859 { | |
5860 /* Count the length of the patterns in the same way as they are put in | |
5861 * "command" below. */ | |
5862 #ifdef USE_SYSTEM | |
7 | 5863 len += STRLEN(pat[i]) + 3; /* add space and two quotes */ |
147 | 5864 #else |
5865 ++len; /* add space */ | |
628 | 5866 for (j = 0; pat[i][j] != NUL; ++j) |
5867 { | |
5868 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL) | |
5869 ++len; /* may add a backslash */ | |
5870 ++len; | |
5871 } | |
147 | 5872 #endif |
5873 } | |
7 | 5874 command = alloc(len); |
5875 if (command == NULL) | |
5876 { | |
5877 /* out of memory */ | |
5878 vim_free(tempname); | |
5879 return FAIL; | |
5880 } | |
5881 | |
5882 /* | |
5883 * Build the shell command: | |
5884 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell | |
5885 * recognizes this). | |
5886 * - Add the shell command to print the expanded names. | |
5887 * - Add the temp file name. | |
5888 * - Add the file name patterns. | |
5889 */ | |
5890 if (shell_style == STYLE_BT) | |
5891 { | |
628 | 5892 /* change `command; command& ` to (command; command ) */ |
5893 STRCPY(command, "("); | |
5894 STRCAT(command, pat[0] + 1); /* exclude first backtick */ | |
7 | 5895 p = command + STRLEN(command) - 1; |
628 | 5896 *p-- = ')'; /* remove last backtick */ |
7 | 5897 while (p > command && vim_iswhite(*p)) |
5898 --p; | |
5899 if (*p == '&') /* remove trailing '&' */ | |
5900 { | |
5901 ampersent = TRUE; | |
5902 *p = ' '; | |
5903 } | |
5904 STRCAT(command, ">"); | |
5905 } | |
5906 else | |
5907 { | |
5908 if (flags & EW_NOTFOUND) | |
5909 STRCPY(command, "set nonomatch; "); | |
5910 else | |
5911 STRCPY(command, "unset nonomatch; "); | |
5912 if (shell_style == STYLE_GLOB) | |
5913 STRCAT(command, "glob >"); | |
5914 else if (shell_style == STYLE_PRINT) | |
5915 STRCAT(command, "print -N >"); | |
1508 | 5916 else if (shell_style == STYLE_VIMGLOB) |
5917 STRCAT(command, sh_vimglob_func); | |
7 | 5918 else |
5919 STRCAT(command, "echo >"); | |
5920 } | |
1508 | 5921 |
7 | 5922 STRCAT(command, tempname); |
1508 | 5923 |
7 | 5924 if (shell_style != STYLE_BT) |
5925 for (i = 0; i < num_pat; ++i) | |
5926 { | |
5927 /* When using system() always add extra quotes, because the shell | |
628 | 5928 * is started twice. Otherwise put a backslash before special |
1450 | 5929 * characters, except inside ``. */ |
7 | 5930 #ifdef USE_SYSTEM |
5931 STRCAT(command, " \""); | |
5932 STRCAT(command, pat[i]); | |
5933 STRCAT(command, "\""); | |
5934 #else | |
233 | 5935 int intick = FALSE; |
5936 | |
7 | 5937 p = command + STRLEN(command); |
5938 *p++ = ' '; | |
628 | 5939 for (j = 0; pat[i][j] != NUL; ++j) |
233 | 5940 { |
5941 if (pat[i][j] == '`') | |
5942 intick = !intick; | |
628 | 5943 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL) |
5944 { | |
644 | 5945 /* Remove a backslash, take char literally. But keep |
652 | 5946 * backslash inside backticks, before a special character |
5947 * and before a backtick. */ | |
644 | 5948 if (intick |
652 | 5949 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL |
5950 || pat[i][j + 1] == '`') | |
644 | 5951 *p++ = '\\'; |
648 | 5952 ++j; |
628 | 5953 } |
6174 | 5954 else if (!intick |
5955 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$') | |
5956 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL) | |
628 | 5957 /* Put a backslash before a special character, but not |
6174 | 5958 * when inside ``. And not for $var when EW_KEEPDOLLAR is |
5959 * set. */ | |
628 | 5960 *p++ = '\\'; |
648 | 5961 |
5962 /* Copy one character. */ | |
5963 *p++ = pat[i][j]; | |
233 | 5964 } |
7 | 5965 *p = NUL; |
5966 #endif | |
5967 } | |
5968 if (flags & EW_SILENT) | |
5969 show_shell_mess = FALSE; | |
5970 if (ampersent) | |
1508 | 5971 STRCAT(command, "&"); /* put the '&' after the redirection */ |
7 | 5972 |
5973 /* | |
5974 * Using zsh -G: If a pattern has no matches, it is just deleted from | |
5975 * the argument list, otherwise zsh gives an error message and doesn't | |
5976 * expand any other pattern. | |
5977 */ | |
5978 if (shell_style == STYLE_PRINT) | |
5979 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */ | |
5980 | |
5981 /* | |
5982 * If we use -f then shell variables set in .cshrc won't get expanded. | |
5983 * vi can do it, so we will too, but it is only necessary if there is a "$" | |
5984 * in one of the patterns, otherwise we can still use the fast option. | |
5985 */ | |
5986 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat)) | |
5987 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */ | |
5988 | |
5989 /* | |
5990 * execute the shell command | |
5991 */ | |
5992 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT); | |
5993 | |
5994 /* When running in the background, give it some time to create the temp | |
5995 * file, but don't wait for it to finish. */ | |
5996 if (ampersent) | |
5997 mch_delay(10L, TRUE); | |
5998 | |
5999 extra_shell_arg = NULL; /* cleanup */ | |
6000 show_shell_mess = TRUE; | |
6001 vim_free(command); | |
6002 | |
1508 | 6003 if (i != 0) /* mch_call_shell() failed */ |
7 | 6004 { |
6005 mch_remove(tempname); | |
6006 vim_free(tempname); | |
6007 /* | |
6008 * With interactive completion, the error message is not printed. | |
6009 * However with USE_SYSTEM, I don't know how to turn off error messages | |
6010 * from the shell, so screen may still get messed up -- webb. | |
6011 */ | |
6012 #ifndef USE_SYSTEM | |
6013 if (!(flags & EW_SILENT)) | |
6014 #endif | |
6015 { | |
6016 redraw_later_clear(); /* probably messed up screen */ | |
6017 msg_putchar('\n'); /* clear bottom line quickly */ | |
6018 cmdline_row = Rows - 1; /* continue on last line */ | |
6019 #ifdef USE_SYSTEM | |
6020 if (!(flags & EW_SILENT)) | |
6021 #endif | |
6022 { | |
6023 MSG(_(e_wildexpand)); | |
6024 msg_start(); /* don't overwrite this message */ | |
6025 } | |
6026 } | |
6027 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when | |
6028 * EW_NOTFOUND is given */ | |
6029 if (shell_style == STYLE_BT) | |
6030 return FAIL; | |
6031 goto notfound; | |
6032 } | |
6033 | |
6034 /* | |
6035 * read the names from the file into memory | |
6036 */ | |
6037 fd = fopen((char *)tempname, READBIN); | |
6038 if (fd == NULL) | |
6039 { | |
6040 /* Something went wrong, perhaps a file name with a special char. */ | |
6041 if (!(flags & EW_SILENT)) | |
6042 { | |
6043 MSG(_(e_wildexpand)); | |
6044 msg_start(); /* don't overwrite this message */ | |
6045 } | |
6046 vim_free(tempname); | |
6047 goto notfound; | |
6048 } | |
6049 fseek(fd, 0L, SEEK_END); | |
6050 len = ftell(fd); /* get size of temp file */ | |
6051 fseek(fd, 0L, SEEK_SET); | |
6052 buffer = alloc(len + 1); | |
6053 if (buffer == NULL) | |
6054 { | |
6055 /* out of memory */ | |
6056 mch_remove(tempname); | |
6057 vim_free(tempname); | |
6058 fclose(fd); | |
6059 return FAIL; | |
6060 } | |
6061 i = fread((char *)buffer, 1, len, fd); | |
6062 fclose(fd); | |
6063 mch_remove(tempname); | |
1877 | 6064 if (i != (int)len) |
7 | 6065 { |
6066 /* unexpected read error */ | |
6067 EMSG2(_(e_notread), tempname); | |
6068 vim_free(tempname); | |
6069 vim_free(buffer); | |
6070 return FAIL; | |
6071 } | |
6072 vim_free(tempname); | |
6073 | |
1508 | 6074 # if defined(__CYGWIN__) || defined(__CYGWIN32__) |
7 | 6075 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */ |
6076 p = buffer; | |
5037
5e0b6a9282df
updated for version 7.3.1262
Bram Moolenaar <bram@vim.org>
parents:
4299
diff
changeset
|
6077 for (i = 0; i < (int)len; ++i) |
7 | 6078 if (!(buffer[i] == CAR && buffer[i + 1] == NL)) |
6079 *p++ = buffer[i]; | |
6080 len = p - buffer; | |
6081 # endif | |
6082 | |
6083 | |
6084 /* file names are separated with Space */ | |
6085 if (shell_style == STYLE_ECHO) | |
6086 { | |
6087 buffer[len] = '\n'; /* make sure the buffer ends in NL */ | |
6088 p = buffer; | |
6089 for (i = 0; *p != '\n'; ++i) /* count number of entries */ | |
6090 { | |
6091 while (*p != ' ' && *p != '\n') | |
6092 ++p; | |
6093 p = skipwhite(p); /* skip to next entry */ | |
6094 } | |
6095 } | |
6096 /* file names are separated with NL */ | |
1508 | 6097 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB) |
7 | 6098 { |
6099 buffer[len] = NUL; /* make sure the buffer ends in NUL */ | |
6100 p = buffer; | |
6101 for (i = 0; *p != NUL; ++i) /* count number of entries */ | |
6102 { | |
6103 while (*p != '\n' && *p != NUL) | |
6104 ++p; | |
6105 if (*p != NUL) | |
6106 ++p; | |
6107 p = skipwhite(p); /* skip leading white space */ | |
6108 } | |
6109 } | |
6110 /* file names are separated with NUL */ | |
6111 else | |
6112 { | |
6113 /* | |
6114 * Some versions of zsh use spaces instead of NULs to separate | |
6115 * results. Only do this when there is no NUL before the end of the | |
6116 * buffer, otherwise we would never be able to use file names with | |
6117 * embedded spaces when zsh does use NULs. | |
6118 * When we found a NUL once, we know zsh is OK, set did_find_nul and | |
6119 * don't check for spaces again. | |
6120 */ | |
6121 check_spaces = FALSE; | |
6122 if (shell_style == STYLE_PRINT && !did_find_nul) | |
6123 { | |
6124 /* If there is a NUL, set did_find_nul, else set check_spaces */ | |
2768 | 6125 buffer[len] = NUL; |
5533 | 6126 if (len && (int)STRLEN(buffer) < (int)len) |
7 | 6127 did_find_nul = TRUE; |
6128 else | |
6129 check_spaces = TRUE; | |
6130 } | |
6131 | |
6132 /* | |
6133 * Make sure the buffer ends with a NUL. For STYLE_PRINT there | |
6134 * already is one, for STYLE_GLOB it needs to be added. | |
6135 */ | |
6136 if (len && buffer[len - 1] == NUL) | |
6137 --len; | |
6138 else | |
6139 buffer[len] = NUL; | |
6140 i = 0; | |
6141 for (p = buffer; p < buffer + len; ++p) | |
6142 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */ | |
6143 { | |
6144 ++i; | |
6145 *p = NUL; | |
6146 } | |
6147 if (len) | |
6148 ++i; /* count last entry */ | |
6149 } | |
6150 if (i == 0) | |
6151 { | |
6152 /* | |
6153 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I". | |
6154 * /bin/sh will happily expand it to nothing rather than returning an | |
6155 * error; and hey, it's good to check anyway -- webb. | |
6156 */ | |
6157 vim_free(buffer); | |
6158 goto notfound; | |
6159 } | |
6160 *num_file = i; | |
6161 *file = (char_u **)alloc(sizeof(char_u *) * i); | |
6162 if (*file == NULL) | |
6163 { | |
6164 /* out of memory */ | |
6165 vim_free(buffer); | |
6166 return FAIL; | |
6167 } | |
6168 | |
6169 /* | |
6170 * Isolate the individual file names. | |
6171 */ | |
6172 p = buffer; | |
6173 for (i = 0; i < *num_file; ++i) | |
6174 { | |
6175 (*file)[i] = p; | |
6176 /* Space or NL separates */ | |
1508 | 6177 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT |
6178 || shell_style == STYLE_VIMGLOB) | |
7 | 6179 { |
652 | 6180 while (!(shell_style == STYLE_ECHO && *p == ' ') |
6181 && *p != '\n' && *p != NUL) | |
7 | 6182 ++p; |
6183 if (p == buffer + len) /* last entry */ | |
6184 *p = NUL; | |
6185 else | |
6186 { | |
6187 *p++ = NUL; | |
6188 p = skipwhite(p); /* skip to next entry */ | |
6189 } | |
6190 } | |
6191 else /* NUL separates */ | |
6192 { | |
6193 while (*p && p < buffer + len) /* skip entry */ | |
6194 ++p; | |
6195 ++p; /* skip NUL */ | |
6196 } | |
6197 } | |
6198 | |
6199 /* | |
6200 * Move the file names to allocated memory. | |
6201 */ | |
6202 for (j = 0, i = 0; i < *num_file; ++i) | |
6203 { | |
6204 /* Require the files to exist. Helps when using /bin/sh */ | |
6205 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0) | |
6206 continue; | |
6207 | |
6208 /* check if this entry should be included */ | |
6209 dir = (mch_isdir((*file)[i])); | |
6210 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE))) | |
6211 continue; | |
6212 | |
715 | 6213 /* Skip files that are not executable if we check for that. */ |
6695 | 6214 if (!dir && (flags & EW_EXEC) |
6215 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD))) | |
715 | 6216 continue; |
6217 | |
7 | 6218 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir)); |
6219 if (p) | |
6220 { | |
6221 STRCPY(p, (*file)[i]); | |
6222 if (dir) | |
1479 | 6223 add_pathsep(p); /* add '/' to a directory name */ |
7 | 6224 (*file)[j++] = p; |
6225 } | |
6226 } | |
6227 vim_free(buffer); | |
6228 *num_file = j; | |
6229 | |
6230 if (*num_file == 0) /* rejected all entries */ | |
6231 { | |
6232 vim_free(*file); | |
6233 *file = NULL; | |
6234 goto notfound; | |
6235 } | |
6236 | |
6237 return OK; | |
6238 | |
6239 notfound: | |
6240 if (flags & EW_NOTFOUND) | |
6241 return save_patterns(num_pat, pat, num_file, file); | |
6242 return FAIL; | |
6243 | |
6244 #endif /* __EMX__ */ | |
6245 } | |
6246 | |
6247 #endif /* VMS */ | |
6248 | |
6249 #ifndef __EMX__ | |
6250 static int | |
6251 save_patterns(num_pat, pat, num_file, file) | |
6252 int num_pat; | |
6253 char_u **pat; | |
6254 int *num_file; | |
6255 char_u ***file; | |
6256 { | |
6257 int i; | |
99 | 6258 char_u *s; |
7 | 6259 |
6260 *file = (char_u **)alloc(num_pat * sizeof(char_u *)); | |
6261 if (*file == NULL) | |
6262 return FAIL; | |
6263 for (i = 0; i < num_pat; i++) | |
99 | 6264 { |
6265 s = vim_strsave(pat[i]); | |
6266 if (s != NULL) | |
6267 /* Be compatible with expand_filename(): halve the number of | |
6268 * backslashes. */ | |
6269 backslash_halve(s); | |
6270 (*file)[i] = s; | |
6271 } | |
7 | 6272 *num_file = num_pat; |
6273 return OK; | |
6274 } | |
6275 #endif | |
6276 | |
6277 /* | |
6278 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can | |
6279 * expand. | |
6280 */ | |
6281 int | |
6282 mch_has_exp_wildcard(p) | |
6283 char_u *p; | |
6284 { | |
39 | 6285 for ( ; *p; mb_ptr_adv(p)) |
7 | 6286 { |
6287 if (*p == '\\' && p[1] != NUL) | |
6288 ++p; | |
6289 else | |
6290 if (vim_strchr((char_u *) | |
6291 #ifdef VMS | |
6292 "*?%" | |
6293 #else | |
6294 "*?[{'" | |
6295 #endif | |
6296 , *p) != NULL) | |
6297 return TRUE; | |
6298 } | |
6299 return FALSE; | |
6300 } | |
6301 | |
6302 /* | |
6303 * Return TRUE if the string "p" contains a wildcard. | |
6304 * Don't recognize '~' at the end as a wildcard. | |
6305 */ | |
6306 int | |
6307 mch_has_wildcard(p) | |
6308 char_u *p; | |
6309 { | |
39 | 6310 for ( ; *p; mb_ptr_adv(p)) |
7 | 6311 { |
6312 if (*p == '\\' && p[1] != NUL) | |
6313 ++p; | |
6314 else | |
6315 if (vim_strchr((char_u *) | |
6316 #ifdef VMS | |
6317 "*?%$" | |
6318 #else | |
6319 "*?[{`'$" | |
6320 #endif | |
6321 , *p) != NULL | |
6322 || (*p == '~' && p[1] != NUL)) | |
6323 return TRUE; | |
6324 } | |
6325 return FALSE; | |
6326 } | |
6327 | |
6328 #ifndef __EMX__ | |
6329 static int | |
6330 have_wildcard(num, file) | |
6331 int num; | |
6332 char_u **file; | |
6333 { | |
6334 int i; | |
6335 | |
6336 for (i = 0; i < num; i++) | |
6337 if (mch_has_wildcard(file[i])) | |
6338 return 1; | |
6339 return 0; | |
6340 } | |
6341 | |
6342 static int | |
6343 have_dollars(num, file) | |
6344 int num; | |
6345 char_u **file; | |
6346 { | |
6347 int i; | |
6348 | |
6349 for (i = 0; i < num; i++) | |
6350 if (vim_strchr(file[i], '$') != NULL) | |
6351 return TRUE; | |
6352 return FALSE; | |
6353 } | |
6354 #endif /* ifndef __EMX__ */ | |
6355 | |
6356 #ifndef HAVE_RENAME | |
6357 /* | |
6358 * Scaled-down version of rename(), which is missing in Xenix. | |
6359 * This version can only move regular files and will fail if the | |
6360 * destination exists. | |
6361 */ | |
6362 int | |
6363 mch_rename(src, dest) | |
6364 const char *src, *dest; | |
6365 { | |
6366 struct stat st; | |
6367 | |
6368 if (stat(dest, &st) >= 0) /* fail if destination exists */ | |
6369 return -1; | |
6370 if (link(src, dest) != 0) /* link file to new name */ | |
6371 return -1; | |
6372 if (mch_remove(src) == 0) /* delete link to old name */ | |
6373 return 0; | |
6374 return -1; | |
6375 } | |
6376 #endif /* !HAVE_RENAME */ | |
6377 | |
6378 #ifdef FEAT_MOUSE_GPM | |
6379 /* | |
6380 * Initializes connection with gpm (if it isn't already opened) | |
6381 * Return 1 if succeeded (or connection already opened), 0 if failed | |
6382 */ | |
6383 static int | |
6384 gpm_open() | |
6385 { | |
6386 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */ | |
6387 | |
6388 if (!gpm_flag) | |
6389 { | |
6390 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN); | |
6391 gpm_connect.defaultMask = ~GPM_HARD; | |
6392 /* Default handling for mouse move*/ | |
6393 gpm_connect.minMod = 0; /* Handle any modifier keys */ | |
6394 gpm_connect.maxMod = 0xffff; | |
6395 if (Gpm_Open(&gpm_connect, 0) > 0) | |
6396 { | |
6397 /* gpm library tries to handling TSTP causes | |
6398 * problems. Anyways, we close connection to Gpm whenever | |
6399 * we are going to suspend or starting an external process | |
1450 | 6400 * so we shouldn't have problem with this |
7 | 6401 */ |
1832 | 6402 # ifdef SIGTSTP |
7 | 6403 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL); |
1832 | 6404 # endif |
7 | 6405 return 1; /* succeed */ |
6406 } | |
6407 if (gpm_fd == -2) | |
6408 Gpm_Close(); /* We don't want to talk to xterm via gpm */ | |
6409 return 0; | |
6410 } | |
6411 return 1; /* already open */ | |
6412 } | |
6413 | |
6414 /* | |
6415 * Closes connection to gpm | |
6416 */ | |
6417 static void | |
6418 gpm_close() | |
6419 { | |
6420 if (gpm_flag && gpm_fd >= 0) /* if Open */ | |
6421 Gpm_Close(); | |
6422 } | |
6423 | |
6424 /* Reads gpm event and adds special keys to input buf. Returns length of | |
6425 * generated key sequence. | |
5782 | 6426 * This function is styled after gui_send_mouse_event(). |
7 | 6427 */ |
6428 static int | |
6429 mch_gpm_process() | |
6430 { | |
6431 int button; | |
6432 static Gpm_Event gpm_event; | |
6433 char_u string[6]; | |
6434 int_u vim_modifiers; | |
6435 int row,col; | |
6436 unsigned char buttons_mask; | |
6437 unsigned char gpm_modifiers; | |
6438 static unsigned char old_buttons = 0; | |
6439 | |
6440 Gpm_GetEvent(&gpm_event); | |
6441 | |
6442 #ifdef FEAT_GUI | |
6443 /* Don't put events in the input queue now. */ | |
6444 if (hold_gui_events) | |
6445 return 0; | |
6446 #endif | |
6447 | |
6448 row = gpm_event.y - 1; | |
6449 col = gpm_event.x - 1; | |
6450 | |
6451 string[0] = ESC; /* Our termcode */ | |
6452 string[1] = 'M'; | |
6453 string[2] = 'G'; | |
6454 switch (GPM_BARE_EVENTS(gpm_event.type)) | |
6455 { | |
6456 case GPM_DRAG: | |
6457 string[3] = MOUSE_DRAG; | |
6458 break; | |
6459 case GPM_DOWN: | |
6460 buttons_mask = gpm_event.buttons & ~old_buttons; | |
6461 old_buttons = gpm_event.buttons; | |
6462 switch (buttons_mask) | |
6463 { | |
6464 case GPM_B_LEFT: | |
6465 button = MOUSE_LEFT; | |
6466 break; | |
6467 case GPM_B_MIDDLE: | |
6468 button = MOUSE_MIDDLE; | |
6469 break; | |
6470 case GPM_B_RIGHT: | |
6471 button = MOUSE_RIGHT; | |
6472 break; | |
6473 default: | |
6474 return 0; | |
6475 /*Don't know what to do. Can more than one button be | |
6476 * reported in one event? */ | |
6477 } | |
6478 string[3] = (char_u)(button | 0x20); | |
6479 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1); | |
6480 break; | |
6481 case GPM_UP: | |
6482 string[3] = MOUSE_RELEASE; | |
6483 old_buttons &= ~gpm_event.buttons; | |
6484 break; | |
6485 default: | |
6486 return 0; | |
6487 } | |
6488 /*This code is based on gui_x11_mouse_cb in gui_x11.c */ | |
6489 gpm_modifiers = gpm_event.modifiers; | |
6490 vim_modifiers = 0x0; | |
6491 /* I ignore capslock stats. Aren't we all just hate capslock mixing with | |
6492 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and | |
6493 * K_CAPSSHIFT is defined 8, so it probably isn't even reported | |
6494 */ | |
6495 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL))) | |
6496 vim_modifiers |= MOUSE_SHIFT; | |
6497 | |
6498 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL))) | |
6499 vim_modifiers |= MOUSE_CTRL; | |
6500 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR))) | |
6501 vim_modifiers |= MOUSE_ALT; | |
6502 string[3] |= vim_modifiers; | |
6503 string[4] = (char_u)(col + ' ' + 1); | |
6504 string[5] = (char_u)(row + ' ' + 1); | |
6505 add_to_input_buf(string, 6); | |
6506 return 6; | |
6507 } | |
6508 #endif /* FEAT_MOUSE_GPM */ | |
6509 | |
1620 | 6510 #ifdef FEAT_SYSMOUSE |
6511 /* | |
6512 * Initialize connection with sysmouse. | |
6513 * Let virtual console inform us with SIGUSR2 for pending sysmouse | |
6514 * output, any sysmouse output than will be processed via sig_sysmouse(). | |
6515 * Return OK if succeeded, FAIL if failed. | |
6516 */ | |
6517 static int | |
6518 sysmouse_open() | |
6519 { | |
6520 struct mouse_info mouse; | |
6521 | |
6522 mouse.operation = MOUSE_MODE; | |
6523 mouse.u.mode.mode = 0; | |
6524 mouse.u.mode.signal = SIGUSR2; | |
6525 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1) | |
6526 { | |
6527 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse); | |
6528 mouse.operation = MOUSE_SHOW; | |
6529 ioctl(1, CONS_MOUSECTL, &mouse); | |
6530 return OK; | |
6531 } | |
6532 return FAIL; | |
6533 } | |
6534 | |
6535 /* | |
6536 * Stop processing SIGUSR2 signals, and also make sure that | |
6537 * virtual console do not send us any sysmouse related signal. | |
6538 */ | |
6539 static void | |
6540 sysmouse_close() | |
6541 { | |
6542 struct mouse_info mouse; | |
6543 | |
6544 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL); | |
6545 mouse.operation = MOUSE_MODE; | |
6546 mouse.u.mode.mode = 0; | |
6547 mouse.u.mode.signal = 0; | |
6548 ioctl(1, CONS_MOUSECTL, &mouse); | |
6549 } | |
6550 | |
6551 /* | |
6552 * Gets info from sysmouse and adds special keys to input buf. | |
6553 */ | |
6554 static RETSIGTYPE | |
6555 sig_sysmouse SIGDEFARG(sigarg) | |
6556 { | |
6557 struct mouse_info mouse; | |
6558 struct video_info video; | |
6559 char_u string[6]; | |
6560 int row, col; | |
6561 int button; | |
6562 int buttons; | |
6563 static int oldbuttons = 0; | |
6564 | |
6565 #ifdef FEAT_GUI | |
6566 /* Don't put events in the input queue now. */ | |
6567 if (hold_gui_events) | |
6568 return; | |
6569 #endif | |
6570 | |
6571 mouse.operation = MOUSE_GETINFO; | |
6572 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1 | |
6573 && ioctl(1, FBIO_MODEINFO, &video) != -1 | |
6574 && ioctl(1, CONS_MOUSECTL, &mouse) != -1 | |
6575 && video.vi_cheight > 0 && video.vi_cwidth > 0) | |
6576 { | |
6577 row = mouse.u.data.y / video.vi_cheight; | |
6578 col = mouse.u.data.x / video.vi_cwidth; | |
6579 buttons = mouse.u.data.buttons; | |
6580 string[0] = ESC; /* Our termcode */ | |
6581 string[1] = 'M'; | |
6582 string[2] = 'S'; | |
6583 if (oldbuttons == buttons && buttons != 0) | |
6584 { | |
6585 button = MOUSE_DRAG; | |
6586 } | |
6587 else | |
6588 { | |
6589 switch (buttons) | |
6590 { | |
6591 case 0: | |
6592 button = MOUSE_RELEASE; | |
6593 break; | |
6594 case 1: | |
6595 button = MOUSE_LEFT; | |
6596 break; | |
6597 case 2: | |
6598 button = MOUSE_MIDDLE; | |
6599 break; | |
6600 case 4: | |
6601 button = MOUSE_RIGHT; | |
6602 break; | |
6603 default: | |
6604 return; | |
6605 } | |
6606 oldbuttons = buttons; | |
6607 } | |
6608 string[3] = (char_u)(button); | |
6609 string[4] = (char_u)(col + ' ' + 1); | |
6610 string[5] = (char_u)(row + ' ' + 1); | |
6611 add_to_input_buf(string, 6); | |
6612 } | |
6613 return; | |
6614 } | |
6615 #endif /* FEAT_SYSMOUSE */ | |
6616 | |
7 | 6617 #if defined(FEAT_LIBCALL) || defined(PROTO) |
6618 typedef char_u * (*STRPROCSTR)__ARGS((char_u *)); | |
6619 typedef char_u * (*INTPROCSTR)__ARGS((int)); | |
6620 typedef int (*STRPROCINT)__ARGS((char_u *)); | |
6621 typedef int (*INTPROCINT)__ARGS((int)); | |
6622 | |
6623 /* | |
6624 * Call a DLL routine which takes either a string or int param | |
6625 * and returns an allocated string. | |
6626 */ | |
6627 int | |
6628 mch_libcall(libname, funcname, argstring, argint, string_result, number_result) | |
6629 char_u *libname; | |
6630 char_u *funcname; | |
6631 char_u *argstring; /* NULL when using a argint */ | |
6632 int argint; | |
6633 char_u **string_result;/* NULL when using number_result */ | |
6634 int *number_result; | |
6635 { | |
6636 # if defined(USE_DLOPEN) | |
6637 void *hinstLib; | |
12 | 6638 char *dlerr = NULL; |
7 | 6639 # else |
6640 shl_t hinstLib; | |
6641 # endif | |
6642 STRPROCSTR ProcAdd; | |
6643 INTPROCSTR ProcAddI; | |
6644 char_u *retval_str = NULL; | |
6645 int retval_int = 0; | |
6646 int success = FALSE; | |
6647 | |
900 | 6648 /* |
6649 * Get a handle to the DLL module. | |
6650 */ | |
7 | 6651 # if defined(USE_DLOPEN) |
900 | 6652 /* First clear any error, it's not cleared by the dlopen() call. */ |
6653 (void)dlerror(); | |
6654 | |
7 | 6655 hinstLib = dlopen((char *)libname, RTLD_LAZY |
6656 # ifdef RTLD_LOCAL | |
6657 | RTLD_LOCAL | |
6658 # endif | |
6659 ); | |
12 | 6660 if (hinstLib == NULL) |
6661 { | |
6662 /* "dlerr" must be used before dlclose() */ | |
6663 dlerr = (char *)dlerror(); | |
6664 if (dlerr != NULL) | |
6665 EMSG2(_("dlerror = \"%s\""), dlerr); | |
6666 } | |
7 | 6667 # else |
6668 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L); | |
6669 # endif | |
6670 | |
6671 /* If the handle is valid, try to get the function address. */ | |
6672 if (hinstLib != NULL) | |
6673 { | |
6674 # ifdef HAVE_SETJMP_H | |
6675 /* | |
6676 * Catch a crash when calling the library function. For example when | |
6677 * using a number where a string pointer is expected. | |
6678 */ | |
6679 mch_startjmp(); | |
6680 if (SETJMP(lc_jump_env) != 0) | |
6681 { | |
6682 success = FALSE; | |
857 | 6683 # if defined(USE_DLOPEN) |
12 | 6684 dlerr = NULL; |
857 | 6685 # endif |
7 | 6686 mch_didjmp(); |
6687 } | |
6688 else | |
6689 # endif | |
6690 { | |
6691 retval_str = NULL; | |
6692 retval_int = 0; | |
6693 | |
6694 if (argstring != NULL) | |
6695 { | |
6696 # if defined(USE_DLOPEN) | |
6697 ProcAdd = (STRPROCSTR)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 *)&ProcAdd) < 0) | |
6702 ProcAdd = NULL; | |
6703 # endif | |
12 | 6704 if ((success = (ProcAdd != NULL |
6705 # if defined(USE_DLOPEN) | |
6706 && dlerr == NULL | |
6707 # endif | |
6708 ))) | |
7 | 6709 { |
6710 if (string_result == NULL) | |
6711 retval_int = ((STRPROCINT)ProcAdd)(argstring); | |
6712 else | |
6713 retval_str = (ProcAdd)(argstring); | |
6714 } | |
6715 } | |
6716 else | |
6717 { | |
6718 # if defined(USE_DLOPEN) | |
6719 ProcAddI = (INTPROCSTR)dlsym(hinstLib, (const char *)funcname); | |
12 | 6720 dlerr = (char *)dlerror(); |
7 | 6721 # else |
6722 if (shl_findsym(&hinstLib, (const char *)funcname, | |
6723 TYPE_PROCEDURE, (void *)&ProcAddI) < 0) | |
6724 ProcAddI = NULL; | |
6725 # endif | |
12 | 6726 if ((success = (ProcAddI != NULL |
6727 # if defined(USE_DLOPEN) | |
6728 && dlerr == NULL | |
6729 # endif | |
6730 ))) | |
7 | 6731 { |
6732 if (string_result == NULL) | |
6733 retval_int = ((INTPROCINT)ProcAddI)(argint); | |
6734 else | |
6735 retval_str = (ProcAddI)(argint); | |
6736 } | |
6737 } | |
6738 | |
6739 /* Save the string before we free the library. */ | |
6740 /* Assume that a "1" or "-1" result is an illegal pointer. */ | |
6741 if (string_result == NULL) | |
6742 *number_result = retval_int; | |
6743 else if (retval_str != NULL | |
6744 && retval_str != (char_u *)1 | |
6745 && retval_str != (char_u *)-1) | |
6746 *string_result = vim_strsave(retval_str); | |
6747 } | |
6748 | |
6749 # ifdef HAVE_SETJMP_H | |
6750 mch_endjmp(); | |
6751 # ifdef SIGHASARG | |
6752 if (lc_signal != 0) | |
6753 { | |
6754 int i; | |
6755 | |
6756 /* try to find the name of this signal */ | |
6757 for (i = 0; signal_info[i].sig != -1; i++) | |
6758 if (lc_signal == signal_info[i].sig) | |
6759 break; | |
6760 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name); | |
6761 } | |
6762 # endif | |
6763 # endif | |
6764 | |
12 | 6765 # if defined(USE_DLOPEN) |
6766 /* "dlerr" must be used before dlclose() */ | |
6767 if (dlerr != NULL) | |
6768 EMSG2(_("dlerror = \"%s\""), dlerr); | |
6769 | |
7 | 6770 /* Free the DLL module. */ |
6771 (void)dlclose(hinstLib); | |
6772 # else | |
6773 (void)shl_unload(hinstLib); | |
6774 # endif | |
6775 } | |
6776 | |
6777 if (!success) | |
6778 { | |
6779 EMSG2(_(e_libcall), funcname); | |
6780 return FAIL; | |
6781 } | |
6782 | |
6783 return OK; | |
6784 } | |
6785 #endif | |
6786 | |
6787 #if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO) | |
6788 static int xterm_trace = -1; /* default: disabled */ | |
6789 static int xterm_button; | |
6790 | |
6791 /* | |
6792 * Setup a dummy window for X selections in a terminal. | |
6793 */ | |
6794 void | |
6795 setup_term_clip() | |
6796 { | |
6797 int z = 0; | |
6798 char *strp = ""; | |
6799 Widget AppShell; | |
6800 | |
6801 if (!x_connect_to_server()) | |
6802 return; | |
6803 | |
6804 open_app_context(); | |
6805 if (app_context != NULL && xterm_Shell == (Widget)0) | |
6806 { | |
6807 int (*oldhandler)(); | |
6808 #if defined(HAVE_SETJMP_H) | |
6809 int (*oldIOhandler)(); | |
6810 #endif | |
6811 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) | |
6812 struct timeval start_tv; | |
6813 | |
6814 if (p_verbose > 0) | |
6815 gettimeofday(&start_tv, NULL); | |
6816 # endif | |
6817 | |
6818 /* Ignore X errors while opening the display */ | |
6819 oldhandler = XSetErrorHandler(x_error_check); | |
6820 | |
6821 #if defined(HAVE_SETJMP_H) | |
6822 /* Ignore X IO errors while opening the display */ | |
6823 oldIOhandler = XSetIOErrorHandler(x_IOerror_check); | |
6824 mch_startjmp(); | |
6825 if (SETJMP(lc_jump_env) != 0) | |
6826 { | |
6827 mch_didjmp(); | |
6828 xterm_dpy = NULL; | |
6829 } | |
6830 else | |
6831 #endif | |
6832 { | |
6833 xterm_dpy = XtOpenDisplay(app_context, xterm_display, | |
6834 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp); | |
6835 #if defined(HAVE_SETJMP_H) | |
6836 mch_endjmp(); | |
6837 #endif | |
6838 } | |
6839 | |
6840 #if defined(HAVE_SETJMP_H) | |
6841 /* Now handle X IO errors normally. */ | |
6842 (void)XSetIOErrorHandler(oldIOhandler); | |
6843 #endif | |
6844 /* Now handle X errors normally. */ | |
6845 (void)XSetErrorHandler(oldhandler); | |
6846 | |
6847 if (xterm_dpy == NULL) | |
6848 { | |
6849 if (p_verbose > 0) | |
292 | 6850 verb_msg((char_u *)_("Opening the X display failed")); |
7 | 6851 return; |
6852 } | |
6853 | |
6854 /* Catch terminating error of the X server connection. */ | |
6855 (void)XSetIOErrorHandler(x_IOerror_handler); | |
6856 | |
6857 # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) | |
6858 if (p_verbose > 0) | |
292 | 6859 { |
6860 verbose_enter(); | |
7 | 6861 xopen_message(&start_tv); |
292 | 6862 verbose_leave(); |
6863 } | |
7 | 6864 # endif |
6865 | |
6866 /* Create a Shell to make converters work. */ | |
6867 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm", | |
6868 applicationShellWidgetClass, xterm_dpy, | |
6869 NULL); | |
6870 if (AppShell == (Widget)0) | |
6871 return; | |
6872 xterm_Shell = XtVaCreatePopupShell("VIM", | |
6873 topLevelShellWidgetClass, AppShell, | |
6874 XtNmappedWhenManaged, 0, | |
6875 XtNwidth, 1, | |
6876 XtNheight, 1, | |
6877 NULL); | |
6878 if (xterm_Shell == (Widget)0) | |
6879 return; | |
6880 | |
6881 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
|
6882 x11_setup_selection(xterm_Shell); |
7 | 6883 if (x11_display == NULL) |
6884 x11_display = xterm_dpy; | |
6885 | |
6886 XtRealizeWidget(xterm_Shell); | |
6887 XSync(xterm_dpy, False); | |
6888 xterm_update(); | |
6889 } | |
6890 if (xterm_Shell != (Widget)0) | |
6891 { | |
6892 clip_init(TRUE); | |
6893 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL) | |
6894 x11_window = (Window)atol(strp); | |
6895 /* Check if $WINDOWID is valid. */ | |
6896 if (test_x11_window(xterm_dpy) == FAIL) | |
6897 x11_window = 0; | |
6898 if (x11_window != 0) | |
6899 xterm_trace = 0; | |
6900 } | |
6901 } | |
6902 | |
6903 void | |
6904 start_xterm_trace(button) | |
6905 int button; | |
6906 { | |
6907 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0) | |
6908 return; | |
6909 xterm_trace = 1; | |
6910 xterm_button = button; | |
6911 do_xterm_trace(); | |
6912 } | |
6913 | |
6914 | |
6915 void | |
6916 stop_xterm_trace() | |
6917 { | |
6918 if (xterm_trace < 0) | |
6919 return; | |
6920 xterm_trace = 0; | |
6921 } | |
6922 | |
6923 /* | |
6924 * Query the xterm pointer and generate mouse termcodes if necessary | |
6925 * return TRUE if dragging is active, else FALSE | |
6926 */ | |
6927 static int | |
6928 do_xterm_trace() | |
6929 { | |
6930 Window root, child; | |
6931 int root_x, root_y; | |
6932 int win_x, win_y; | |
6933 int row, col; | |
6934 int_u mask_return; | |
6935 char_u buf[50]; | |
6936 char_u *strp; | |
6937 long got_hints; | |
6938 static char_u *mouse_code; | |
6939 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER}; | |
6940 static int prev_row = 0, prev_col = 0; | |
6941 static XSizeHints xterm_hints; | |
6942 | |
6943 if (xterm_trace <= 0) | |
6944 return FALSE; | |
6945 | |
6946 if (xterm_trace == 1) | |
6947 { | |
6948 /* Get the hints just before tracking starts. The font size might | |
1510 | 6949 * have changed recently. */ |
6950 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints) | |
6951 || !(got_hints & PResizeInc) | |
7 | 6952 || xterm_hints.width_inc <= 1 |
6953 || xterm_hints.height_inc <= 1) | |
6954 { | |
6955 xterm_trace = -1; /* Not enough data -- disable tracing */ | |
6956 return FALSE; | |
6957 } | |
6958 | |
6959 /* Rely on the same mouse code for the duration of this */ | |
6960 mouse_code = find_termcode(mouse_name); | |
6961 prev_row = mouse_row; | |
7009 | 6962 prev_col = mouse_col; |
7 | 6963 xterm_trace = 2; |
6964 | |
6965 /* Find the offset of the chars, there might be a scrollbar on the | |
6966 * left of the window and/or a menu on the top (eterm etc.) */ | |
6967 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y, | |
6968 &win_x, &win_y, &mask_return); | |
6969 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row) | |
6970 - (xterm_hints.height_inc / 2); | |
6971 if (xterm_hints.y <= xterm_hints.height_inc / 2) | |
6972 xterm_hints.y = 2; | |
6973 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col) | |
6974 - (xterm_hints.width_inc / 2); | |
6975 if (xterm_hints.x <= xterm_hints.width_inc / 2) | |
6976 xterm_hints.x = 2; | |
6977 return TRUE; | |
6978 } | |
2768 | 6979 if (mouse_code == NULL || STRLEN(mouse_code) > 45) |
7 | 6980 { |
6981 xterm_trace = 0; | |
6982 return FALSE; | |
6983 } | |
6984 | |
6985 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y, | |
6986 &win_x, &win_y, &mask_return); | |
6987 | |
6988 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc); | |
6989 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc); | |
6990 if (row == prev_row && col == prev_col) | |
6991 return TRUE; | |
6992 | |
6993 STRCPY(buf, mouse_code); | |
6994 strp = buf + STRLEN(buf); | |
6995 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20; | |
6996 *strp++ = (char_u)(col + ' ' + 1); | |
6997 *strp++ = (char_u)(row + ' ' + 1); | |
6998 *strp = 0; | |
6999 add_to_input_buf(buf, STRLEN(buf)); | |
7000 | |
7001 prev_row = row; | |
7002 prev_col = col; | |
7003 return TRUE; | |
7004 } | |
7005 | |
7006 # if defined(FEAT_GUI) || defined(PROTO) | |
7007 /* | |
7008 * Destroy the display, window and app_context. Required for GTK. | |
7009 */ | |
7010 void | |
7011 clear_xterm_clip() | |
7012 { | |
7013 if (xterm_Shell != (Widget)0) | |
7014 { | |
7015 XtDestroyWidget(xterm_Shell); | |
7016 xterm_Shell = (Widget)0; | |
7017 } | |
7018 if (xterm_dpy != NULL) | |
7019 { | |
1605 | 7020 # if 0 |
7 | 7021 /* Lesstif and Solaris crash here, lose some memory */ |
7022 XtCloseDisplay(xterm_dpy); | |
1605 | 7023 # endif |
7 | 7024 if (x11_display == xterm_dpy) |
7025 x11_display = NULL; | |
7026 xterm_dpy = NULL; | |
7027 } | |
1605 | 7028 # if 0 |
7 | 7029 if (app_context != (XtAppContext)NULL) |
7030 { | |
7031 /* Lesstif and Solaris crash here, lose some memory */ | |
7032 XtDestroyApplicationContext(app_context); | |
7033 app_context = (XtAppContext)NULL; | |
7034 } | |
1605 | 7035 # endif |
7 | 7036 } |
7037 # endif | |
7038 | |
7039 /* | |
4230 | 7040 * Catch up with GUI or X events. |
7041 */ | |
7042 static void | |
7043 clip_update() | |
7044 { | |
7045 # ifdef FEAT_GUI | |
7046 if (gui.in_use) | |
7047 gui_mch_update(); | |
7048 else | |
7049 # endif | |
7050 if (xterm_Shell != (Widget)0) | |
7051 xterm_update(); | |
7052 } | |
7053 | |
7054 /* | |
7 | 7055 * Catch up with any queued X events. This may put keyboard input into the |
7056 * input buffer, call resize call-backs, trigger timers etc. If there is | |
7057 * nothing in the X event queue (& no timers pending), then we return | |
7058 * immediately. | |
7059 */ | |
7060 static void | |
7061 xterm_update() | |
7062 { | |
7063 XEvent event; | |
7064 | |
6683 | 7065 for (;;) |
7 | 7066 { |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7067 XtInputMask mask = XtAppPending(app_context); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7068 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7069 if (mask == 0 || vim_is_input_buf_full()) |
6683 | 7070 break; |
7071 | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7072 if (mask & XtIMXEvent) |
7 | 7073 { |
6683 | 7074 /* 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
|
7075 XtAppNextEvent(app_context, &event); |
6683 | 7076 #ifdef FEAT_CLIENTSERVER |
7077 { | |
7078 XPropertyEvent *e = (XPropertyEvent *)&event; | |
7079 | |
7080 if (e->type == PropertyNotify && e->window == commWindow | |
7 | 7081 && 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
|
7082 serverEventProc(xterm_dpy, &event, 0); |
6683 | 7083 } |
7084 #endif | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7085 XtDispatchEvent(&event); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7086 } |
6683 | 7087 else |
7088 { | |
7089 /* 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
|
7090 XtAppProcessEvent(app_context, mask); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7091 } |
7 | 7092 } |
7093 } | |
7094 | |
7095 int | |
7096 clip_xterm_own_selection(cbd) | |
7097 VimClipboard *cbd; | |
7098 { | |
7099 if (xterm_Shell != (Widget)0) | |
7100 return clip_x11_own_selection(xterm_Shell, cbd); | |
7101 return FAIL; | |
7102 } | |
7103 | |
7104 void | |
7105 clip_xterm_lose_selection(cbd) | |
7106 VimClipboard *cbd; | |
7107 { | |
7108 if (xterm_Shell != (Widget)0) | |
7109 clip_x11_lose_selection(xterm_Shell, cbd); | |
7110 } | |
7111 | |
7112 void | |
7113 clip_xterm_request_selection(cbd) | |
7114 VimClipboard *cbd; | |
7115 { | |
7116 if (xterm_Shell != (Widget)0) | |
7117 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd); | |
7118 } | |
7119 | |
7120 void | |
7121 clip_xterm_set_selection(cbd) | |
7122 VimClipboard *cbd; | |
7123 { | |
7124 clip_x11_set_selection(cbd); | |
7125 } | |
7126 #endif | |
7127 | |
7128 | |
7129 #if defined(USE_XSMP) || defined(PROTO) | |
7130 /* | |
7131 * Code for X Session Management Protocol. | |
7132 */ | |
7133 static void xsmp_handle_save_yourself __ARGS((SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast)); | |
7134 static void xsmp_die __ARGS((SmcConn smc_conn, SmPointer client_data)); | |
7135 static void xsmp_save_complete __ARGS((SmcConn smc_conn, SmPointer client_data)); | |
7136 static void xsmp_shutdown_cancelled __ARGS((SmcConn smc_conn, SmPointer client_data)); | |
7137 static void xsmp_ice_connection __ARGS((IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData)); | |
7138 | |
7139 | |
7140 # if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT) | |
7141 static void xsmp_handle_interaction __ARGS((SmcConn smc_conn, SmPointer client_data)); | |
7142 | |
7143 /* | |
7144 * This is our chance to ask the user if they want to save, | |
7145 * or abort the logout | |
7146 */ | |
7147 static void | |
7148 xsmp_handle_interaction(smc_conn, client_data) | |
7149 SmcConn smc_conn; | |
1883 | 7150 SmPointer client_data UNUSED; |
7 | 7151 { |
7152 cmdmod_T save_cmdmod; | |
7153 int cancel_shutdown = False; | |
7154 | |
7155 save_cmdmod = cmdmod; | |
7156 cmdmod.confirm = TRUE; | |
7469
15eefe1b0dad
commit https://github.com/vim/vim/commit/027387f70c671f62e3e08e0bdd09ec05b0232735
Christian Brabandt <cb@256bit.org>
parents:
7420
diff
changeset
|
7157 if (check_changed_any(FALSE, FALSE)) |
7 | 7158 /* Mustn't logout */ |
7159 cancel_shutdown = True; | |
7160 cmdmod = save_cmdmod; | |
7161 setcursor(); /* position cursor */ | |
7162 out_flush(); | |
7163 | |
7164 /* Done interaction */ | |
7165 SmcInteractDone(smc_conn, cancel_shutdown); | |
7166 | |
7167 /* Finish off | |
7168 * Only end save-yourself here if we're not cancelling shutdown; | |
7169 * we'll get a cancelled callback later in which we'll end it. | |
7170 * Hopefully get around glitchy SMs (like GNOME-1) | |
7171 */ | |
7172 if (!cancel_shutdown) | |
7173 { | |
7174 xsmp.save_yourself = False; | |
7175 SmcSaveYourselfDone(smc_conn, True); | |
7176 } | |
7177 } | |
7178 # endif | |
7179 | |
7180 /* | |
7181 * Callback that starts save-yourself. | |
7182 */ | |
7183 static void | |
7184 xsmp_handle_save_yourself(smc_conn, client_data, save_type, | |
7185 shutdown, interact_style, fast) | |
7186 SmcConn smc_conn; | |
1883 | 7187 SmPointer client_data UNUSED; |
7188 int save_type UNUSED; | |
7 | 7189 Bool shutdown; |
1883 | 7190 int interact_style UNUSED; |
7191 Bool fast UNUSED; | |
7 | 7192 { |
7193 /* Handle already being in saveyourself */ | |
7194 if (xsmp.save_yourself) | |
7195 SmcSaveYourselfDone(smc_conn, True); | |
7196 xsmp.save_yourself = True; | |
7197 xsmp.shutdown = shutdown; | |
7198 | |
7199 /* First up, preserve all files */ | |
7200 out_flush(); | |
7201 ml_sync_all(FALSE, FALSE); /* preserve all swap files */ | |
7202 | |
7203 if (p_verbose > 0) | |
292 | 7204 verb_msg((char_u *)_("XSMP handling save-yourself request")); |
7 | 7205 |
7206 # if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT) | |
7207 /* Now see if we can ask about unsaved files */ | |
7208 if (shutdown && !fast && gui.in_use) | |
7209 /* Need to interact with user, but need SM's permission */ | |
7210 SmcInteractRequest(smc_conn, SmDialogError, | |
7211 xsmp_handle_interaction, client_data); | |
7212 else | |
7213 # endif | |
7214 { | |
7215 /* Can stop the cycle here */ | |
7216 SmcSaveYourselfDone(smc_conn, True); | |
7217 xsmp.save_yourself = False; | |
7218 } | |
7219 } | |
7220 | |
7221 | |
7222 /* | |
7223 * Callback to warn us of imminent death. | |
7224 */ | |
7225 static void | |
7226 xsmp_die(smc_conn, client_data) | |
1883 | 7227 SmcConn smc_conn UNUSED; |
7228 SmPointer client_data UNUSED; | |
7 | 7229 { |
7230 xsmp_close(); | |
7231 | |
7232 /* quit quickly leaving swapfiles for modified buffers behind */ | |
7233 getout_preserve_modified(0); | |
7234 } | |
7235 | |
7236 | |
7237 /* | |
7238 * Callback to tell us that save-yourself has completed. | |
7239 */ | |
7240 static void | |
7241 xsmp_save_complete(smc_conn, client_data) | |
1883 | 7242 SmcConn smc_conn UNUSED; |
7243 SmPointer client_data UNUSED; | |
7 | 7244 { |
7245 xsmp.save_yourself = False; | |
7246 } | |
7247 | |
7248 | |
7249 /* | |
7250 * Callback to tell us that an instigated shutdown was cancelled | |
7251 * (maybe even by us) | |
7252 */ | |
7253 static void | |
7254 xsmp_shutdown_cancelled(smc_conn, client_data) | |
7255 SmcConn smc_conn; | |
1883 | 7256 SmPointer client_data UNUSED; |
7 | 7257 { |
7258 if (xsmp.save_yourself) | |
7259 SmcSaveYourselfDone(smc_conn, True); | |
7260 xsmp.save_yourself = False; | |
7261 xsmp.shutdown = False; | |
7262 } | |
7263 | |
7264 | |
7265 /* | |
7266 * Callback to tell us that a new ICE connection has been established. | |
7267 */ | |
7268 static void | |
7269 xsmp_ice_connection(iceConn, clientData, opening, watchData) | |
7270 IceConn iceConn; | |
1883 | 7271 IcePointer clientData UNUSED; |
7 | 7272 Bool opening; |
1883 | 7273 IcePointer *watchData UNUSED; |
7 | 7274 { |
7275 /* Intercept creation of ICE connection fd */ | |
7276 if (opening) | |
7277 { | |
7278 xsmp_icefd = IceConnectionNumber(iceConn); | |
7279 IceRemoveConnectionWatch(xsmp_ice_connection, NULL); | |
7280 } | |
7281 } | |
7282 | |
7283 | |
7284 /* Handle any ICE processing that's required; return FAIL if SM lost */ | |
7285 int | |
7286 xsmp_handle_requests() | |
7287 { | |
7288 Bool rep; | |
7289 | |
7290 if (IceProcessMessages(xsmp.iceconn, NULL, &rep) | |
7291 == IceProcessMessagesIOError) | |
7292 { | |
7293 /* Lost ICE */ | |
7294 if (p_verbose > 0) | |
292 | 7295 verb_msg((char_u *)_("XSMP lost ICE connection")); |
7 | 7296 xsmp_close(); |
7297 return FAIL; | |
7298 } | |
7299 else | |
7300 return OK; | |
7301 } | |
7302 | |
7303 static int dummy; | |
7304 | |
7305 /* Set up X Session Management Protocol */ | |
7306 void | |
7307 xsmp_init(void) | |
7308 { | |
7309 char errorstring[80]; | |
7310 SmcCallbacks smcallbacks; | |
7311 #if 0 | |
7312 SmPropValue smname; | |
7313 SmProp smnameprop; | |
7314 SmProp *smprops[1]; | |
7315 #endif | |
7316 | |
7317 if (p_verbose > 0) | |
292 | 7318 verb_msg((char_u *)_("XSMP opening connection")); |
7 | 7319 |
7320 xsmp.save_yourself = xsmp.shutdown = False; | |
7321 | |
7322 /* Set up SM callbacks - must have all, even if they're not used */ | |
7323 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself; | |
7324 smcallbacks.save_yourself.client_data = NULL; | |
7325 smcallbacks.die.callback = xsmp_die; | |
7326 smcallbacks.die.client_data = NULL; | |
7327 smcallbacks.save_complete.callback = xsmp_save_complete; | |
7328 smcallbacks.save_complete.client_data = NULL; | |
7329 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled; | |
7330 smcallbacks.shutdown_cancelled.client_data = NULL; | |
7331 | |
7332 /* Set up a watch on ICE connection creations. The "dummy" argument is | |
7333 * apparently required for FreeBSD (we get a BUS error when using NULL). */ | |
7334 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0) | |
7335 { | |
7336 if (p_verbose > 0) | |
292 | 7337 verb_msg((char_u *)_("XSMP ICE connection watch failed")); |
7 | 7338 return; |
7339 } | |
7340 | |
7341 /* Create an SM connection */ | |
7342 xsmp.smcconn = SmcOpenConnection( | |
7343 NULL, | |
7344 NULL, | |
7345 SmProtoMajor, | |
7346 SmProtoMinor, | |
7347 SmcSaveYourselfProcMask | SmcDieProcMask | |
7348 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask, | |
7349 &smcallbacks, | |
7350 NULL, | |
1605 | 7351 &xsmp.clientid, |
7 | 7352 sizeof(errorstring), |
7353 errorstring); | |
7354 if (xsmp.smcconn == NULL) | |
7355 { | |
7356 char errorreport[132]; | |
273 | 7357 |
292 | 7358 if (p_verbose > 0) |
7359 { | |
7360 vim_snprintf(errorreport, sizeof(errorreport), | |
273 | 7361 _("XSMP SmcOpenConnection failed: %s"), errorstring); |
292 | 7362 verb_msg((char_u *)errorreport); |
7363 } | |
7 | 7364 return; |
7365 } | |
7366 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn); | |
7367 | |
7368 #if 0 | |
7369 /* ID ourselves */ | |
7370 smname.value = "vim"; | |
7371 smname.length = 3; | |
7372 smnameprop.name = "SmProgram"; | |
7373 smnameprop.type = "SmARRAY8"; | |
7374 smnameprop.num_vals = 1; | |
7375 smnameprop.vals = &smname; | |
7376 | |
7377 smprops[0] = &smnameprop; | |
7378 SmcSetProperties(xsmp.smcconn, 1, smprops); | |
7379 #endif | |
7380 } | |
7381 | |
7382 | |
7383 /* Shut down XSMP comms. */ | |
7384 void | |
7385 xsmp_close() | |
7386 { | |
7387 if (xsmp_icefd != -1) | |
7388 { | |
7389 SmcCloseConnection(xsmp.smcconn, 0, NULL); | |
1737 | 7390 if (xsmp.clientid != NULL) |
7391 free(xsmp.clientid); | |
1605 | 7392 xsmp.clientid = NULL; |
7 | 7393 xsmp_icefd = -1; |
7394 } | |
7395 } | |
7396 #endif /* USE_XSMP */ | |
7397 | |
7398 | |
7399 #ifdef EBCDIC | |
7400 /* Translate character to its CTRL- value */ | |
7401 char CtrlTable[] = | |
7402 { | |
7403 /* 00 - 5E */ | |
7404 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
7405 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
7406 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
7407 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
7408 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
7409 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
7410 /* ^ */ 0x1E, | |
7411 /* - */ 0x1F, | |
7412 /* 61 - 6C */ | |
7413 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
7414 /* _ */ 0x1F, | |
7415 /* 6E - 80 */ | |
7416 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
7417 /* a */ 0x01, | |
7418 /* b */ 0x02, | |
7419 /* c */ 0x03, | |
7420 /* d */ 0x37, | |
7421 /* e */ 0x2D, | |
7422 /* f */ 0x2E, | |
7423 /* g */ 0x2F, | |
7424 /* h */ 0x16, | |
7425 /* i */ 0x05, | |
7426 /* 8A - 90 */ | |
7427 0, 0, 0, 0, 0, 0, 0, | |
7428 /* j */ 0x15, | |
7429 /* k */ 0x0B, | |
7430 /* l */ 0x0C, | |
7431 /* m */ 0x0D, | |
7432 /* n */ 0x0E, | |
7433 /* o */ 0x0F, | |
7434 /* p */ 0x10, | |
7435 /* q */ 0x11, | |
7436 /* r */ 0x12, | |
7437 /* 9A - A1 */ | |
7438 0, 0, 0, 0, 0, 0, 0, 0, | |
7439 /* s */ 0x13, | |
7440 /* t */ 0x3C, | |
7441 /* u */ 0x3D, | |
7442 /* v */ 0x32, | |
7443 /* w */ 0x26, | |
7444 /* x */ 0x18, | |
7445 /* y */ 0x19, | |
7446 /* z */ 0x3F, | |
7447 /* AA - AC */ | |
7448 0, 0, 0, | |
7449 /* [ */ 0x27, | |
7450 /* AE - BC */ | |
7451 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
7452 /* ] */ 0x1D, | |
7453 /* BE - C0 */ 0, 0, 0, | |
7454 /* A */ 0x01, | |
7455 /* B */ 0x02, | |
7456 /* C */ 0x03, | |
7457 /* D */ 0x37, | |
7458 /* E */ 0x2D, | |
7459 /* F */ 0x2E, | |
7460 /* G */ 0x2F, | |
7461 /* H */ 0x16, | |
7462 /* I */ 0x05, | |
7463 /* CA - D0 */ 0, 0, 0, 0, 0, 0, 0, | |
7464 /* J */ 0x15, | |
7465 /* K */ 0x0B, | |
7466 /* L */ 0x0C, | |
7467 /* M */ 0x0D, | |
7468 /* N */ 0x0E, | |
7469 /* O */ 0x0F, | |
7470 /* P */ 0x10, | |
7471 /* Q */ 0x11, | |
7472 /* R */ 0x12, | |
7473 /* DA - DF */ 0, 0, 0, 0, 0, 0, | |
7474 /* \ */ 0x1C, | |
7475 /* E1 */ 0, | |
7476 /* S */ 0x13, | |
7477 /* T */ 0x3C, | |
7478 /* U */ 0x3D, | |
7479 /* V */ 0x32, | |
7480 /* W */ 0x26, | |
7481 /* X */ 0x18, | |
7482 /* Y */ 0x19, | |
7483 /* Z */ 0x3F, | |
7484 /* EA - FF*/ 0, 0, 0, 0, 0, 0, | |
7485 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
7486 }; | |
7487 | |
7488 char MetaCharTable[]= | |
7489 {/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ | |
7490 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0, | |
7491 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0, | |
7492 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0, | |
7493 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0 | |
7494 }; | |
7495 | |
7496 | |
7497 /* TODO: Use characters NOT numbers!!! */ | |
7498 char CtrlCharTable[]= | |
7499 {/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ | |
7500 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214, | |
7501 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109, | |
7502 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199, | |
7503 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233, | |
7504 }; | |
7505 | |
7506 | |
7507 #endif |