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