Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 81 | extern int select(int, fd_set *, fd_set *, fd_set *, struct timeval *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 101 | static void gpm_close(void); |
| 102 | static int gpm_open(void); |
| 103 | static int mch_gpm_process(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 110 | static int sysmouse_open(void); |
| 111 | static void sysmouse_close(void); |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 112 | static RETSIGTYPE sig_sysmouse SIGPROTOARG; |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 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 | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 143 | static void clip_update(void); |
| 144 | static void xterm_update(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 153 | static int get_x11_windis(void); |
| 154 | static void set_x11_title(char_u *); |
| 155 | static void set_x11_icon(char_u *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | # endif |
| 157 | #endif |
| 158 | |
| 159 | #ifdef FEAT_TITLE |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 160 | static int get_x11_title(int); |
| 161 | static int get_x11_icon(int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 169 | static void may_core_dump(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 176 | static pid_t wait4pid(pid_t, waitstatus *); |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 177 | |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 178 | static int WaitForChar(long msec, int *interrupted); |
| 179 | static int WaitForCharOrMouse(long msec, int *interrupted); |
Bram Moolenaar | 4ffa070 | 2013-12-11 17:12:37 +0100 | [diff] [blame] | 180 | #if defined(__BEOS__) || defined(VMS) |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 181 | int RealWaitForChar(int, long, int *, int *interrupted); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | #else |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 183 | static int RealWaitForChar(int, long, int *, int *interrupted); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 184 | #endif |
| 185 | |
| 186 | #ifdef FEAT_XCLIPBOARD |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 187 | static int do_xterm_trace(void); |
Bram Moolenaar | cf851ce | 2005-06-16 21:52:47 +0000 | [diff] [blame] | 188 | # define XT_TRACE_DELAY 50 /* delay for xterm tracing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 189 | #endif |
| 190 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 191 | static void handle_resize(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 192 | |
| 193 | #if defined(SIGWINCH) |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 194 | static RETSIGTYPE sig_winch SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | #endif |
| 196 | #if defined(SIGINT) |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 197 | static RETSIGTYPE catch_sigint SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 198 | #endif |
| 199 | #if defined(SIGPWR) |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 200 | static RETSIGTYPE catch_sigpwr SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 201 | #endif |
| 202 | #if defined(SIGALRM) && defined(FEAT_X11) \ |
| 203 | && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK) |
| 204 | # define SET_SIG_ALARM |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 205 | static RETSIGTYPE sig_alarm SIGPROTOARG; |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 206 | /* volatile because it is used in signal handler sig_alarm(). */ |
| 207 | static volatile int sig_alarm_called; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 208 | #endif |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 209 | static RETSIGTYPE deathtrap SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 210 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 211 | static void catch_int_signal(void); |
| 212 | static void set_signals(void); |
| 213 | static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)()); |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 214 | #ifdef HAVE_SIGPROCMASK |
| 215 | # define SIGSET_DECL(set) sigset_t set; |
| 216 | # define BLOCK_SIGNALS(set) block_signals(set) |
| 217 | # define UNBLOCK_SIGNALS(set) unblock_signals(set) |
| 218 | #else |
| 219 | # define SIGSET_DECL(set) |
| 220 | # define BLOCK_SIGNALS(set) do { /**/ } while (0) |
| 221 | # define UNBLOCK_SIGNALS(set) do { /**/ } while (0) |
| 222 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 223 | static int have_wildcard(int, char_u **); |
| 224 | static int have_dollars(int, char_u **); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 225 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 226 | static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 227 | |
| 228 | #ifndef SIG_ERR |
Bram Moolenaar | 8f0b2d4 | 2009-05-16 14:41:10 +0000 | [diff] [blame] | 229 | # define SIG_ERR ((RETSIGTYPE (*)())-1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 230 | #endif |
| 231 | |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 232 | /* volatile because it is used in signal handler sig_winch(). */ |
| 233 | static volatile int do_resize = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 234 | static char_u *extra_shell_arg = NULL; |
| 235 | static int show_shell_mess = TRUE; |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 236 | /* volatile because it is used in signal handler deathtrap(). */ |
| 237 | static volatile int deadly_signal = 0; /* The signal we caught */ |
| 238 | /* volatile because it is used in signal handler deathtrap(). */ |
| 239 | static volatile int in_mch_delay = FALSE; /* sleeping in mch_delay() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 240 | |
Bram Moolenaar | c4d4ac2 | 2016-11-07 22:42:57 +0100 | [diff] [blame] | 241 | #if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM) |
| 242 | static int dont_check_job_ended = 0; |
| 243 | #endif |
| 244 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 245 | static int curr_tmode = TMODE_COOK; /* contains current terminal mode */ |
| 246 | |
| 247 | #ifdef USE_XSMP |
| 248 | typedef struct |
| 249 | { |
| 250 | SmcConn smcconn; /* The SM connection ID */ |
| 251 | IceConn iceconn; /* The ICE connection ID */ |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 252 | char *clientid; /* The client ID for the current smc session */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 253 | Bool save_yourself; /* If we're in the middle of a save_yourself */ |
| 254 | Bool shutdown; /* If we're in shutdown mode */ |
| 255 | } xsmp_config_T; |
| 256 | |
| 257 | static xsmp_config_T xsmp; |
| 258 | #endif |
| 259 | |
| 260 | #ifdef SYS_SIGLIST_DECLARED |
| 261 | /* |
| 262 | * I have seen |
| 263 | * extern char *_sys_siglist[NSIG]; |
| 264 | * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings |
| 265 | * that describe the signals. That is nearly what we want here. But |
| 266 | * autoconf does only check for sys_siglist (without the underscore), I |
| 267 | * do not want to change everything today.... jw. |
Bram Moolenaar | 3f7d090 | 2016-11-12 21:13:42 +0100 | [diff] [blame] | 268 | * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 269 | */ |
| 270 | #endif |
| 271 | |
| 272 | static struct signalinfo |
| 273 | { |
| 274 | int sig; /* Signal number, eg. SIGSEGV etc */ |
| 275 | char *name; /* Signal name (not char_u!). */ |
| 276 | char deadly; /* Catch as a deadly signal? */ |
| 277 | } signal_info[] = |
| 278 | { |
| 279 | #ifdef SIGHUP |
| 280 | {SIGHUP, "HUP", TRUE}, |
| 281 | #endif |
| 282 | #ifdef SIGQUIT |
| 283 | {SIGQUIT, "QUIT", TRUE}, |
| 284 | #endif |
| 285 | #ifdef SIGILL |
| 286 | {SIGILL, "ILL", TRUE}, |
| 287 | #endif |
| 288 | #ifdef SIGTRAP |
| 289 | {SIGTRAP, "TRAP", TRUE}, |
| 290 | #endif |
| 291 | #ifdef SIGABRT |
| 292 | {SIGABRT, "ABRT", TRUE}, |
| 293 | #endif |
| 294 | #ifdef SIGEMT |
| 295 | {SIGEMT, "EMT", TRUE}, |
| 296 | #endif |
| 297 | #ifdef SIGFPE |
| 298 | {SIGFPE, "FPE", TRUE}, |
| 299 | #endif |
| 300 | #ifdef SIGBUS |
| 301 | {SIGBUS, "BUS", TRUE}, |
| 302 | #endif |
Bram Moolenaar | 7567646 | 2013-01-30 14:55:42 +0100 | [diff] [blame] | 303 | #if defined(SIGSEGV) && !defined(FEAT_MZSCHEME) |
| 304 | /* MzScheme uses SEGV in its garbage collector */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 305 | {SIGSEGV, "SEGV", TRUE}, |
| 306 | #endif |
| 307 | #ifdef SIGSYS |
| 308 | {SIGSYS, "SYS", TRUE}, |
| 309 | #endif |
| 310 | #ifdef SIGALRM |
| 311 | {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */ |
| 312 | #endif |
| 313 | #ifdef SIGTERM |
| 314 | {SIGTERM, "TERM", TRUE}, |
| 315 | #endif |
Bram Moolenaar | b292a2a | 2011-02-09 18:47:40 +0100 | [diff] [blame] | 316 | #if defined(SIGVTALRM) && !defined(FEAT_RUBY) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 317 | {SIGVTALRM, "VTALRM", TRUE}, |
| 318 | #endif |
Bram Moolenaar | 02f07e0 | 2008-03-12 12:17:28 +0000 | [diff] [blame] | 319 | #if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING) |
| 320 | /* MzScheme uses SIGPROF for its own needs; On Linux with profiling |
| 321 | * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 322 | {SIGPROF, "PROF", TRUE}, |
| 323 | #endif |
| 324 | #ifdef SIGXCPU |
| 325 | {SIGXCPU, "XCPU", TRUE}, |
| 326 | #endif |
| 327 | #ifdef SIGXFSZ |
| 328 | {SIGXFSZ, "XFSZ", TRUE}, |
| 329 | #endif |
| 330 | #ifdef SIGUSR1 |
| 331 | {SIGUSR1, "USR1", TRUE}, |
| 332 | #endif |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 333 | #if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE) |
| 334 | /* Used for sysmouse handling */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 335 | {SIGUSR2, "USR2", TRUE}, |
| 336 | #endif |
| 337 | #ifdef SIGINT |
| 338 | {SIGINT, "INT", FALSE}, |
| 339 | #endif |
| 340 | #ifdef SIGWINCH |
| 341 | {SIGWINCH, "WINCH", FALSE}, |
| 342 | #endif |
| 343 | #ifdef SIGTSTP |
| 344 | {SIGTSTP, "TSTP", FALSE}, |
| 345 | #endif |
| 346 | #ifdef SIGPIPE |
| 347 | {SIGPIPE, "PIPE", FALSE}, |
| 348 | #endif |
| 349 | {-1, "Unknown!", FALSE} |
| 350 | }; |
| 351 | |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 352 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 353 | mch_chdir(char *path) |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 354 | { |
| 355 | if (p_verbose >= 5) |
| 356 | { |
| 357 | verbose_enter(); |
| 358 | smsg((char_u *)"chdir(%s)", path); |
| 359 | verbose_leave(); |
| 360 | } |
| 361 | # ifdef VMS |
| 362 | return chdir(vms_fixfilename(path)); |
| 363 | # else |
| 364 | return chdir(path); |
| 365 | # endif |
| 366 | } |
| 367 | |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 368 | /* |
| 369 | * Write s[len] to the screen. |
| 370 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 371 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 372 | mch_write(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 373 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 374 | ignored = (int)write(1, (char *)s, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 375 | if (p_wd) /* Unix is too fast, slow down a bit more */ |
Bram Moolenaar | 8fdd721 | 2016-03-26 19:41:48 +0100 | [diff] [blame] | 376 | RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 380 | * mch_inchar(): low level input function. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 381 | * Get a characters from the keyboard. |
| 382 | * Return the number of characters that are available. |
| 383 | * If wtime == 0 do not wait for characters. |
| 384 | * If wtime == n wait a short time for characters. |
| 385 | * If wtime == -1 wait forever for characters. |
| 386 | */ |
| 387 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 388 | mch_inchar( |
| 389 | char_u *buf, |
| 390 | int maxlen, |
| 391 | long wtime, /* don't use "time", MIPS cannot handle it */ |
| 392 | int tb_change_cnt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 393 | { |
| 394 | int len; |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 395 | int interrupted = FALSE; |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 396 | int did_start_blocking = FALSE; |
Bram Moolenaar | e30a3d0 | 2016-06-04 14:11:20 +0200 | [diff] [blame] | 397 | long wait_time; |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 398 | long elapsed_time = 0; |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 399 | #ifdef ELAPSED_FUNC |
| 400 | ELAPSED_TYPE start_tv; |
Bram Moolenaar | e30a3d0 | 2016-06-04 14:11:20 +0200 | [diff] [blame] | 401 | |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 402 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | e30a3d0 | 2016-06-04 14:11:20 +0200 | [diff] [blame] | 403 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 404 | |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 405 | /* repeat until we got a character or waited long enough */ |
Bram Moolenaar | e30a3d0 | 2016-06-04 14:11:20 +0200 | [diff] [blame] | 406 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 407 | { |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 408 | /* Check if window changed size while we were busy, perhaps the ":set |
| 409 | * columns=99" command was used. */ |
| 410 | while (do_resize) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 411 | handle_resize(); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 412 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 413 | #ifdef MESSAGE_QUEUE |
| 414 | parse_queued_messages(); |
Bram Moolenaar | d85f271 | 2017-07-28 21:51:57 +0200 | [diff] [blame] | 415 | /* If input was put directly in typeahead buffer bail out here. */ |
| 416 | if (typebuf_changed(tb_change_cnt)) |
| 417 | return 0; |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 418 | #endif |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 419 | if (wtime < 0 && did_start_blocking) |
| 420 | /* blocking and already waited for p_ut */ |
| 421 | wait_time = -1; |
| 422 | else |
| 423 | { |
| 424 | if (wtime >= 0) |
| 425 | wait_time = wtime; |
| 426 | else |
| 427 | /* going to block after p_ut */ |
| 428 | wait_time = p_ut; |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 429 | #ifdef ELAPSED_FUNC |
| 430 | elapsed_time = ELAPSED_FUNC(start_tv); |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 431 | #endif |
| 432 | wait_time -= elapsed_time; |
| 433 | if (wait_time < 0) |
| 434 | { |
| 435 | if (wtime >= 0) |
| 436 | /* no character available within "wtime" */ |
| 437 | return 0; |
| 438 | |
| 439 | if (wtime < 0) |
| 440 | { |
| 441 | /* no character available within 'updatetime' */ |
| 442 | did_start_blocking = TRUE; |
| 443 | #ifdef FEAT_AUTOCMD |
| 444 | if (trigger_cursorhold() && maxlen >= 3 |
| 445 | && !typebuf_changed(tb_change_cnt)) |
| 446 | { |
| 447 | buf[0] = K_SPECIAL; |
| 448 | buf[1] = KS_EXTRA; |
| 449 | buf[2] = (int)KE_CURSORHOLD; |
| 450 | return 3; |
| 451 | } |
| 452 | #endif |
| 453 | /* |
| 454 | * If there is no character available within 'updatetime' |
| 455 | * seconds flush all the swap files to disk. |
| 456 | * Also done when interrupted by SIGWINCH. |
| 457 | */ |
| 458 | before_blocking(); |
| 459 | continue; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | #ifdef FEAT_JOB_CHANNEL |
| 465 | /* Checking if a job ended requires polling. Do this every 100 msec. */ |
| 466 | if (has_pending_job() && (wait_time < 0 || wait_time > 100L)) |
| 467 | wait_time = 100L; |
Bram Moolenaar | 8a8199e | 2016-11-26 15:13:33 +0100 | [diff] [blame] | 468 | /* If there is readahead then parse_queued_messages() timed out and we |
| 469 | * should call it again soon. */ |
| 470 | if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead()) |
| 471 | wait_time = 10L; |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 472 | #endif |
Bram Moolenaar | 59716a2 | 2017-03-01 20:32:44 +0100 | [diff] [blame] | 473 | #ifdef FEAT_BEVAL |
| 474 | if (p_beval && wait_time > 100L) |
| 475 | /* The 'balloonexpr' may indirectly invoke a callback while waiting |
| 476 | * for a character, need to check often. */ |
| 477 | wait_time = 100L; |
| 478 | #endif |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 479 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 480 | /* |
Bram Moolenaar | 48bae37 | 2010-07-29 23:12:15 +0200 | [diff] [blame] | 481 | * We want to be interrupted by the winch signal |
| 482 | * or by an event on the monitored file descriptors. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 483 | */ |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 484 | if (WaitForChar(wait_time, &interrupted)) |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 485 | { |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 486 | /* If input was put directly in typeahead buffer bail out here. */ |
| 487 | if (typebuf_changed(tb_change_cnt)) |
| 488 | return 0; |
| 489 | |
| 490 | /* |
| 491 | * For some terminals we only get one character at a time. |
| 492 | * We want the get all available characters, so we could keep on |
| 493 | * trying until none is available |
| 494 | * For some other terminals this is quite slow, that's why we don't |
| 495 | * do it. |
| 496 | */ |
| 497 | len = read_from_input_buf(buf, (long)maxlen); |
| 498 | if (len > 0) |
| 499 | return len; |
| 500 | continue; |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 501 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 502 | |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 503 | /* no character available */ |
| 504 | #if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)) |
| 505 | /* estimate the elapsed time */ |
Bram Moolenaar | de5e2c2 | 2016-11-04 20:35:31 +0100 | [diff] [blame] | 506 | elapsed_time += wait_time; |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 507 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 508 | |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 509 | if (do_resize /* interrupted by SIGWINCH signal */ |
| 510 | #ifdef FEAT_CLIENTSERVER |
| 511 | || server_waiting() |
| 512 | #endif |
| 513 | #ifdef MESSAGE_QUEUE |
| 514 | || interrupted |
| 515 | #endif |
| 516 | || wait_time > 0 |
Bram Moolenaar | 1572e30 | 2017-03-25 20:16:28 +0100 | [diff] [blame] | 517 | || (wtime < 0 && !did_start_blocking)) |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 518 | continue; |
| 519 | |
| 520 | /* no character available or interrupted */ |
| 521 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 522 | } |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 523 | return 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 527 | handle_resize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 528 | { |
| 529 | do_resize = FALSE; |
| 530 | shell_resized(); |
| 531 | } |
| 532 | |
| 533 | /* |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 534 | * Return non-zero if a character is available. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 535 | */ |
| 536 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 537 | mch_char_avail(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 538 | { |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 539 | return WaitForChar(0L, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | #if defined(HAVE_TOTAL_MEM) || defined(PROTO) |
| 543 | # ifdef HAVE_SYS_RESOURCE_H |
Bram Moolenaar | 8f0b2d4 | 2009-05-16 14:41:10 +0000 | [diff] [blame] | 544 | # include <sys/resource.h> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 545 | # endif |
| 546 | # if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL) |
| 547 | # include <sys/sysctl.h> |
| 548 | # endif |
| 549 | # if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO) |
| 550 | # include <sys/sysinfo.h> |
| 551 | # endif |
| 552 | |
| 553 | /* |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 554 | * Return total amount of memory available in Kbyte. |
| 555 | * Doesn't change when memory has been allocated. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 556 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 557 | long_u |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 558 | mch_total_mem(int special UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 559 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 560 | long_u mem = 0; |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 561 | long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 562 | |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 563 | # ifdef HAVE_SYSCTL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 564 | int mib[2], physmem; |
| 565 | size_t len; |
| 566 | |
| 567 | /* BSD way of getting the amount of RAM available. */ |
| 568 | mib[0] = CTL_HW; |
| 569 | mib[1] = HW_USERMEM; |
| 570 | len = sizeof(physmem); |
| 571 | if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0) |
| 572 | mem = (long_u)physmem; |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 573 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 574 | |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 575 | # if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 576 | if (mem == 0) |
| 577 | { |
| 578 | struct sysinfo sinfo; |
| 579 | |
| 580 | /* Linux way of getting amount of RAM available */ |
| 581 | if (sysinfo(&sinfo) == 0) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 582 | { |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 583 | # ifdef HAVE_SYSINFO_MEM_UNIT |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 584 | /* avoid overflow as much as possible */ |
| 585 | while (shiftright > 0 && (sinfo.mem_unit & 1) == 0) |
| 586 | { |
| 587 | sinfo.mem_unit = sinfo.mem_unit >> 1; |
| 588 | --shiftright; |
| 589 | } |
| 590 | mem = sinfo.totalram * sinfo.mem_unit; |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 591 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 592 | mem = sinfo.totalram; |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 593 | # endif |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 594 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 595 | } |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 596 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 597 | |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 598 | # ifdef HAVE_SYSCONF |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 599 | if (mem == 0) |
| 600 | { |
| 601 | long pagesize, pagecount; |
| 602 | |
| 603 | /* Solaris way of getting amount of RAM available */ |
| 604 | pagesize = sysconf(_SC_PAGESIZE); |
| 605 | pagecount = sysconf(_SC_PHYS_PAGES); |
| 606 | if (pagesize > 0 && pagecount > 0) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 607 | { |
| 608 | /* avoid overflow as much as possible */ |
| 609 | while (shiftright > 0 && (pagesize & 1) == 0) |
| 610 | { |
Bram Moolenaar | 3d27a45 | 2007-05-10 17:44:18 +0000 | [diff] [blame] | 611 | pagesize = (long_u)pagesize >> 1; |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 612 | --shiftright; |
| 613 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 614 | mem = (long_u)pagesize * pagecount; |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 615 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 616 | } |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 617 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 618 | |
| 619 | /* Return the minimum of the physical memory and the user limit, because |
| 620 | * using more than the user limit may cause Vim to be terminated. */ |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 621 | # if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 622 | { |
| 623 | struct rlimit rlp; |
| 624 | |
| 625 | if (getrlimit(RLIMIT_DATA, &rlp) == 0 |
| 626 | && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1)) |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 627 | # ifdef RLIM_INFINITY |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 628 | && rlp.rlim_cur != RLIM_INFINITY |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 629 | # endif |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 630 | && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 631 | ) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 632 | { |
| 633 | mem = (long_u)rlp.rlim_cur; |
| 634 | shiftright = 10; |
| 635 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 636 | } |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 637 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 638 | |
| 639 | if (mem > 0) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 640 | return mem >> shiftright; |
| 641 | return (long_u)0x1fffff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 642 | } |
| 643 | #endif |
| 644 | |
| 645 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 646 | mch_delay(long msec, int ignoreinput) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 647 | { |
| 648 | int old_tmode; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 649 | #ifdef FEAT_MZSCHEME |
| 650 | long total = msec; /* remember original value */ |
| 651 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 652 | |
| 653 | if (ignoreinput) |
| 654 | { |
| 655 | /* Go to cooked mode without echo, to allow SIGINT interrupting us |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 656 | * here. But we don't want QUIT to kill us (CTRL-\ used in a |
| 657 | * shell may produce SIGQUIT). */ |
| 658 | in_mch_delay = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 659 | old_tmode = curr_tmode; |
| 660 | if (curr_tmode == TMODE_RAW) |
| 661 | settmode(TMODE_SLEEP); |
| 662 | |
| 663 | /* |
| 664 | * Everybody sleeps in a different way... |
| 665 | * Prefer nanosleep(), some versions of usleep() can only sleep up to |
| 666 | * one second. |
| 667 | */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 668 | #ifdef FEAT_MZSCHEME |
| 669 | do |
| 670 | { |
| 671 | /* if total is large enough, wait by portions in p_mzq */ |
| 672 | if (total > p_mzq) |
| 673 | msec = p_mzq; |
| 674 | else |
| 675 | msec = total; |
| 676 | total -= msec; |
| 677 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 678 | #ifdef HAVE_NANOSLEEP |
| 679 | { |
| 680 | struct timespec ts; |
| 681 | |
| 682 | ts.tv_sec = msec / 1000; |
| 683 | ts.tv_nsec = (msec % 1000) * 1000000; |
| 684 | (void)nanosleep(&ts, NULL); |
| 685 | } |
| 686 | #else |
| 687 | # ifdef HAVE_USLEEP |
| 688 | while (msec >= 1000) |
| 689 | { |
| 690 | usleep((unsigned int)(999 * 1000)); |
| 691 | msec -= 999; |
| 692 | } |
| 693 | usleep((unsigned int)(msec * 1000)); |
| 694 | # else |
| 695 | # ifndef HAVE_SELECT |
| 696 | poll(NULL, 0, (int)msec); |
| 697 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 698 | { |
| 699 | struct timeval tv; |
| 700 | |
| 701 | tv.tv_sec = msec / 1000; |
| 702 | tv.tv_usec = (msec % 1000) * 1000; |
| 703 | /* |
| 704 | * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get |
| 705 | * a patch from Sun to fix this. Reported by Gunnar Pedersen. |
| 706 | */ |
| 707 | select(0, NULL, NULL, NULL, &tv); |
| 708 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 709 | # endif /* HAVE_SELECT */ |
| 710 | # endif /* HAVE_NANOSLEEP */ |
| 711 | #endif /* HAVE_USLEEP */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 712 | #ifdef FEAT_MZSCHEME |
| 713 | } |
| 714 | while (total > 0); |
| 715 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 716 | |
| 717 | settmode(old_tmode); |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 718 | in_mch_delay = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 719 | } |
| 720 | else |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 721 | WaitForChar(msec, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 724 | #if defined(HAVE_STACK_LIMIT) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 725 | || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK)) |
| 726 | # define HAVE_CHECK_STACK_GROWTH |
| 727 | /* |
| 728 | * Support for checking for an almost-out-of-stack-space situation. |
| 729 | */ |
| 730 | |
| 731 | /* |
| 732 | * Return a pointer to an item on the stack. Used to find out if the stack |
| 733 | * grows up or down. |
| 734 | */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 735 | static void check_stack_growth(char *p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 736 | static int stack_grows_downwards; |
| 737 | |
| 738 | /* |
| 739 | * Find out if the stack grows upwards or downwards. |
| 740 | * "p" points to a variable on the stack of the caller. |
| 741 | */ |
| 742 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 743 | check_stack_growth(char *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 744 | { |
| 745 | int i; |
| 746 | |
| 747 | stack_grows_downwards = (p > (char *)&i); |
| 748 | } |
| 749 | #endif |
| 750 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 751 | #if defined(HAVE_STACK_LIMIT) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 752 | static char *stack_limit = NULL; |
| 753 | |
| 754 | #if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H) |
| 755 | # include <pthread.h> |
| 756 | # include <pthread_np.h> |
| 757 | #endif |
| 758 | |
| 759 | /* |
| 760 | * Find out until how var the stack can grow without getting into trouble. |
| 761 | * Called when starting up and when switching to the signal stack in |
| 762 | * deathtrap(). |
| 763 | */ |
| 764 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 765 | get_stack_limit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 766 | { |
| 767 | struct rlimit rlp; |
| 768 | int i; |
| 769 | long lim; |
| 770 | |
| 771 | /* Set the stack limit to 15/16 of the allowable size. Skip this when the |
| 772 | * limit doesn't fit in a long (rlim_cur might be "long long"). */ |
| 773 | if (getrlimit(RLIMIT_STACK, &rlp) == 0 |
| 774 | && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1)) |
| 775 | # ifdef RLIM_INFINITY |
| 776 | && rlp.rlim_cur != RLIM_INFINITY |
| 777 | # endif |
| 778 | ) |
| 779 | { |
| 780 | lim = (long)rlp.rlim_cur; |
| 781 | #if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H) |
| 782 | { |
| 783 | pthread_attr_t attr; |
| 784 | size_t size; |
| 785 | |
| 786 | /* On FreeBSD the initial thread always has a fixed stack size, no |
| 787 | * matter what the limits are set to. Normally it's 1 Mbyte. */ |
| 788 | pthread_attr_init(&attr); |
| 789 | if (pthread_attr_get_np(pthread_self(), &attr) == 0) |
| 790 | { |
| 791 | pthread_attr_getstacksize(&attr, &size); |
| 792 | if (lim > (long)size) |
| 793 | lim = (long)size; |
| 794 | } |
| 795 | pthread_attr_destroy(&attr); |
| 796 | } |
| 797 | #endif |
| 798 | if (stack_grows_downwards) |
| 799 | { |
| 800 | stack_limit = (char *)((long)&i - (lim / 16L * 15L)); |
| 801 | if (stack_limit >= (char *)&i) |
| 802 | /* overflow, set to 1/16 of current stack position */ |
| 803 | stack_limit = (char *)((long)&i / 16L); |
| 804 | } |
| 805 | else |
| 806 | { |
| 807 | stack_limit = (char *)((long)&i + (lim / 16L * 15L)); |
| 808 | if (stack_limit <= (char *)&i) |
| 809 | stack_limit = NULL; /* overflow */ |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * Return FAIL when running out of stack space. |
| 816 | * "p" must point to any variable local to the caller that's on the stack. |
| 817 | */ |
| 818 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 819 | mch_stackcheck(char *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 820 | { |
| 821 | if (stack_limit != NULL) |
| 822 | { |
| 823 | if (stack_grows_downwards) |
| 824 | { |
| 825 | if (p < stack_limit) |
| 826 | return FAIL; |
| 827 | } |
| 828 | else if (p > stack_limit) |
| 829 | return FAIL; |
| 830 | } |
| 831 | return OK; |
| 832 | } |
| 833 | #endif |
| 834 | |
| 835 | #if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
| 836 | /* |
| 837 | * Support for using the signal stack. |
| 838 | * This helps when we run out of stack space, which causes a SIGSEGV. The |
| 839 | * signal handler then must run on another stack, since the normal stack is |
| 840 | * completely full. |
| 841 | */ |
| 842 | |
Bram Moolenaar | 39766a7 | 2013-11-03 00:41:00 +0100 | [diff] [blame] | 843 | #if defined(HAVE_AVAILABILITYMACROS_H) |
Bram Moolenaar | 4cc95d1 | 2013-11-02 21:49:32 +0100 | [diff] [blame] | 844 | # include <AvailabilityMacros.h> |
| 845 | #endif |
| 846 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 847 | #ifndef SIGSTKSZ |
| 848 | # define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */ |
| 849 | #endif |
| 850 | |
| 851 | # ifdef HAVE_SIGALTSTACK |
| 852 | static stack_t sigstk; /* for sigaltstack() */ |
| 853 | # else |
| 854 | static struct sigstack sigstk; /* for sigstack() */ |
| 855 | # endif |
| 856 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 857 | static void init_signal_stack(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 858 | static char *signal_stack; |
| 859 | |
| 860 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 861 | init_signal_stack(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 862 | { |
| 863 | if (signal_stack != NULL) |
| 864 | { |
| 865 | # ifdef HAVE_SIGALTSTACK |
Bram Moolenaar | 1a3d086 | 2007-08-30 09:47:38 +0000 | [diff] [blame] | 866 | # if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \ |
| 867 | || MAC_OS_X_VERSION_MAX_ALLOWED <= 1040) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 868 | /* missing prototype. Adding it to osdef?.h.in doesn't work, because |
| 869 | * "struct sigaltstack" needs to be declared. */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 870 | extern int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 871 | # endif |
| 872 | |
| 873 | # ifdef HAVE_SS_BASE |
| 874 | sigstk.ss_base = signal_stack; |
| 875 | # else |
| 876 | sigstk.ss_sp = signal_stack; |
| 877 | # endif |
| 878 | sigstk.ss_size = SIGSTKSZ; |
| 879 | sigstk.ss_flags = 0; |
| 880 | (void)sigaltstack(&sigstk, NULL); |
| 881 | # else |
| 882 | sigstk.ss_sp = signal_stack; |
| 883 | if (stack_grows_downwards) |
| 884 | sigstk.ss_sp += SIGSTKSZ - 1; |
| 885 | sigstk.ss_onstack = 0; |
| 886 | (void)sigstack(&sigstk, NULL); |
| 887 | # endif |
| 888 | } |
| 889 | } |
| 890 | #endif |
| 891 | |
| 892 | /* |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 893 | * We need correct prototypes for a signal function, otherwise mean compilers |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 894 | * will barf when the second argument to signal() is ``wrong''. |
| 895 | * Let me try it with a few tricky defines from my own osdef.h (jw). |
| 896 | */ |
| 897 | #if defined(SIGWINCH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 898 | static RETSIGTYPE |
| 899 | sig_winch SIGDEFARG(sigarg) |
| 900 | { |
| 901 | /* this is not required on all systems, but it doesn't hurt anybody */ |
| 902 | signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch); |
| 903 | do_resize = TRUE; |
| 904 | SIGRETURN; |
| 905 | } |
| 906 | #endif |
| 907 | |
| 908 | #if defined(SIGINT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 909 | static RETSIGTYPE |
| 910 | catch_sigint SIGDEFARG(sigarg) |
| 911 | { |
| 912 | /* this is not required on all systems, but it doesn't hurt anybody */ |
| 913 | signal(SIGINT, (RETSIGTYPE (*)())catch_sigint); |
| 914 | got_int = TRUE; |
| 915 | SIGRETURN; |
| 916 | } |
| 917 | #endif |
| 918 | |
| 919 | #if defined(SIGPWR) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 920 | static RETSIGTYPE |
| 921 | catch_sigpwr SIGDEFARG(sigarg) |
| 922 | { |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 923 | /* this is not required on all systems, but it doesn't hurt anybody */ |
| 924 | signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 925 | /* |
| 926 | * I'm not sure we get the SIGPWR signal when the system is really going |
| 927 | * down or when the batteries are almost empty. Just preserve the swap |
| 928 | * files and don't exit, that can't do any harm. |
| 929 | */ |
| 930 | ml_sync_all(FALSE, FALSE); |
| 931 | SIGRETURN; |
| 932 | } |
| 933 | #endif |
| 934 | |
| 935 | #ifdef SET_SIG_ALARM |
| 936 | /* |
| 937 | * signal function for alarm(). |
| 938 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 939 | static RETSIGTYPE |
| 940 | sig_alarm SIGDEFARG(sigarg) |
| 941 | { |
| 942 | /* doesn't do anything, just to break a system call */ |
| 943 | sig_alarm_called = TRUE; |
| 944 | SIGRETURN; |
| 945 | } |
| 946 | #endif |
| 947 | |
Bram Moolenaar | 44ecf65 | 2005-03-07 23:09:59 +0000 | [diff] [blame] | 948 | #if (defined(HAVE_SETJMP_H) \ |
| 949 | && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \ |
| 950 | || defined(FEAT_LIBCALL))) \ |
| 951 | || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 952 | /* |
| 953 | * A simplistic version of setjmp() that only allows one level of using. |
| 954 | * Don't call twice before calling mch_endjmp()!. |
| 955 | * Usage: |
| 956 | * mch_startjmp(); |
| 957 | * if (SETJMP(lc_jump_env) != 0) |
| 958 | * { |
| 959 | * mch_didjmp(); |
| 960 | * EMSG("crash!"); |
| 961 | * } |
| 962 | * else |
| 963 | * { |
| 964 | * do_the_work; |
| 965 | * mch_endjmp(); |
| 966 | * } |
| 967 | * Note: Can't move SETJMP() here, because a function calling setjmp() must |
| 968 | * not return before the saved environment is used. |
| 969 | * Returns OK for normal return, FAIL when the protected code caused a |
| 970 | * problem and LONGJMP() was used. |
| 971 | */ |
| 972 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 973 | mch_startjmp(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 974 | { |
| 975 | #ifdef SIGHASARG |
| 976 | lc_signal = 0; |
| 977 | #endif |
| 978 | lc_active = TRUE; |
| 979 | } |
| 980 | |
| 981 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 982 | mch_endjmp(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 983 | { |
| 984 | lc_active = FALSE; |
| 985 | } |
| 986 | |
| 987 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 988 | mch_didjmp(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 989 | { |
| 990 | # if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
| 991 | /* On FreeBSD the signal stack has to be reset after using siglongjmp(), |
| 992 | * otherwise catching the signal only works once. */ |
| 993 | init_signal_stack(); |
| 994 | # endif |
| 995 | } |
| 996 | #endif |
| 997 | |
| 998 | /* |
| 999 | * This function handles deadly signals. |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1000 | * It tries to preserve any swap files and exit properly. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1001 | * (partly from Elvis). |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1002 | * NOTE: Avoid unsafe functions, such as allocating memory, they can result in |
| 1003 | * a deadlock. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1004 | */ |
| 1005 | static RETSIGTYPE |
| 1006 | deathtrap SIGDEFARG(sigarg) |
| 1007 | { |
| 1008 | static int entered = 0; /* count the number of times we got here. |
| 1009 | Note: when memory has been corrupted |
| 1010 | this may get an arbitrary value! */ |
| 1011 | #ifdef SIGHASARG |
| 1012 | int i; |
| 1013 | #endif |
| 1014 | |
| 1015 | #if defined(HAVE_SETJMP_H) |
| 1016 | /* |
| 1017 | * Catch a crash in protected code. |
| 1018 | * Restores the environment saved in lc_jump_env, which looks like |
| 1019 | * SETJMP() returns 1. |
| 1020 | */ |
| 1021 | if (lc_active) |
| 1022 | { |
| 1023 | # if defined(SIGHASARG) |
| 1024 | lc_signal = sigarg; |
| 1025 | # endif |
| 1026 | lc_active = FALSE; /* don't jump again */ |
| 1027 | LONGJMP(lc_jump_env, 1); |
| 1028 | /* NOTREACHED */ |
| 1029 | } |
| 1030 | #endif |
| 1031 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1032 | #ifdef SIGHASARG |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 1033 | # ifdef SIGQUIT |
| 1034 | /* While in mch_delay() we go to cooked mode to allow a CTRL-C to |
| 1035 | * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when |
| 1036 | * pressing CTRL-\, but we don't want Vim to exit then. */ |
| 1037 | if (in_mch_delay && sigarg == SIGQUIT) |
| 1038 | SIGRETURN; |
| 1039 | # endif |
| 1040 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1041 | /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return |
| 1042 | * here. This avoids that a non-reentrant function is interrupted, e.g., |
| 1043 | * free(). Calling free() again may then cause a crash. */ |
| 1044 | if (entered == 0 |
| 1045 | && (0 |
| 1046 | # ifdef SIGHUP |
| 1047 | || sigarg == SIGHUP |
| 1048 | # endif |
| 1049 | # ifdef SIGQUIT |
| 1050 | || sigarg == SIGQUIT |
| 1051 | # endif |
| 1052 | # ifdef SIGTERM |
| 1053 | || sigarg == SIGTERM |
| 1054 | # endif |
| 1055 | # ifdef SIGPWR |
| 1056 | || sigarg == SIGPWR |
| 1057 | # endif |
| 1058 | # ifdef SIGUSR1 |
| 1059 | || sigarg == SIGUSR1 |
| 1060 | # endif |
| 1061 | # ifdef SIGUSR2 |
| 1062 | || sigarg == SIGUSR2 |
| 1063 | # endif |
| 1064 | ) |
Bram Moolenaar | 1f28b07 | 2005-07-12 22:42:41 +0000 | [diff] [blame] | 1065 | && !vim_handle_signal(sigarg)) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1066 | SIGRETURN; |
| 1067 | #endif |
| 1068 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1069 | /* Remember how often we have been called. */ |
| 1070 | ++entered; |
| 1071 | |
Bram Moolenaar | e429e70 | 2016-06-10 19:49:14 +0200 | [diff] [blame] | 1072 | #ifdef FEAT_AUTOCMD |
| 1073 | /* Executing autocommands is likely to use more stack space than we have |
| 1074 | * available in the signal stack. */ |
| 1075 | block_autocmds(); |
| 1076 | #endif |
| 1077 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1078 | #ifdef FEAT_EVAL |
| 1079 | /* Set the v:dying variable. */ |
| 1080 | set_vim_var_nr(VV_DYING, (long)entered); |
| 1081 | #endif |
| 1082 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1083 | #ifdef HAVE_STACK_LIMIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1084 | /* Since we are now using the signal stack, need to reset the stack |
| 1085 | * limit. Otherwise using a regexp will fail. */ |
| 1086 | get_stack_limit(); |
| 1087 | #endif |
| 1088 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1089 | #if 0 |
| 1090 | /* This is for opening gdb the moment Vim crashes. |
| 1091 | * You need to manually adjust the file name and Vim executable name. |
| 1092 | * Suggested by SungHyun Nam. */ |
| 1093 | { |
| 1094 | # define VI_GDB_FILE "/tmp/vimgdb" |
| 1095 | # define VIM_NAME "/usr/bin/vim" |
| 1096 | FILE *fp = fopen(VI_GDB_FILE, "w"); |
| 1097 | if (fp) |
| 1098 | { |
| 1099 | fprintf(fp, |
| 1100 | "file %s\n" |
| 1101 | "attach %d\n" |
| 1102 | "set height 1000\n" |
| 1103 | "bt full\n" |
| 1104 | , VIM_NAME, getpid()); |
| 1105 | fclose(fp); |
| 1106 | system("xterm -e gdb -x "VI_GDB_FILE); |
| 1107 | unlink(VI_GDB_FILE); |
| 1108 | } |
| 1109 | } |
| 1110 | #endif |
| 1111 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1112 | #ifdef SIGHASARG |
| 1113 | /* try to find the name of this signal */ |
| 1114 | for (i = 0; signal_info[i].sig != -1; i++) |
| 1115 | if (sigarg == signal_info[i].sig) |
| 1116 | break; |
| 1117 | deadly_signal = sigarg; |
| 1118 | #endif |
| 1119 | |
| 1120 | full_screen = FALSE; /* don't write message to the GUI, it might be |
| 1121 | * part of the problem... */ |
| 1122 | /* |
| 1123 | * If something goes wrong after entering here, we may get here again. |
| 1124 | * When this happens, give a message and try to exit nicely (resetting the |
| 1125 | * terminal mode, etc.) |
| 1126 | * When this happens twice, just exit, don't even try to give a message, |
| 1127 | * stack may be corrupt or something weird. |
| 1128 | * When this still happens again (or memory was corrupted in such a way |
| 1129 | * that "entered" was clobbered) use _exit(), don't try freeing resources. |
| 1130 | */ |
| 1131 | if (entered >= 3) |
| 1132 | { |
| 1133 | reset_signals(); /* don't catch any signals anymore */ |
| 1134 | may_core_dump(); |
| 1135 | if (entered >= 4) |
| 1136 | _exit(8); |
| 1137 | exit(7); |
| 1138 | } |
| 1139 | if (entered == 2) |
| 1140 | { |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1141 | /* No translation, it may call malloc(). */ |
| 1142 | OUT_STR("Vim: Double signal, exiting\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1143 | out_flush(); |
| 1144 | getout(1); |
| 1145 | } |
| 1146 | |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1147 | /* No translation, it may call malloc(). */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1148 | #ifdef SIGHASARG |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1149 | sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1150 | signal_info[i].name); |
| 1151 | #else |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1152 | sprintf((char *)IObuff, "Vim: Caught deadly signal\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1153 | #endif |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1154 | |
| 1155 | /* Preserve files and exit. This sets the really_exiting flag to prevent |
| 1156 | * calling free(). */ |
| 1157 | preserve_exit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | |
Bram Moolenaar | e429e70 | 2016-06-10 19:49:14 +0200 | [diff] [blame] | 1159 | /* NOTREACHED */ |
| 1160 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1161 | #ifdef NBDEBUG |
| 1162 | reset_signals(); |
| 1163 | may_core_dump(); |
| 1164 | abort(); |
| 1165 | #endif |
| 1166 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1167 | SIGRETURN; |
| 1168 | } |
| 1169 | |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1170 | #if defined(_REENTRANT) && defined(SIGCONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1171 | /* |
| 1172 | * On Solaris with multi-threading, suspending might not work immediately. |
| 1173 | * Catch the SIGCONT signal, which will be used as an indication whether the |
| 1174 | * suspending has been done or not. |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1175 | * |
| 1176 | * On Linux, signal is not always handled immediately either. |
| 1177 | * See https://bugs.launchpad.net/bugs/291373 |
| 1178 | * |
Bram Moolenaar | b292a2a | 2011-02-09 18:47:40 +0100 | [diff] [blame] | 1179 | * volatile because it is used in signal handler sigcont_handler(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1180 | */ |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1181 | static volatile int sigcont_received; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 1182 | static RETSIGTYPE sigcont_handler SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1183 | |
| 1184 | /* |
| 1185 | * signal handler for SIGCONT |
| 1186 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1187 | static RETSIGTYPE |
| 1188 | sigcont_handler SIGDEFARG(sigarg) |
| 1189 | { |
| 1190 | sigcont_received = TRUE; |
| 1191 | SIGRETURN; |
| 1192 | } |
| 1193 | #endif |
| 1194 | |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1195 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1196 | static void loose_clipboard(void); |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1197 | # ifdef USE_SYSTEM |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1198 | static void save_clipboard(void); |
| 1199 | static void restore_clipboard(void); |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1200 | |
| 1201 | static void *clip_star_save = NULL; |
| 1202 | static void *clip_plus_save = NULL; |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1203 | # endif |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1204 | |
| 1205 | /* |
| 1206 | * Called when Vim is going to sleep or execute a shell command. |
| 1207 | * We can't respond to requests for the X selections. Lose them, otherwise |
| 1208 | * other applications will hang. But first copy the text to cut buffer 0. |
| 1209 | */ |
| 1210 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1211 | loose_clipboard(void) |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1212 | { |
| 1213 | if (clip_star.owned || clip_plus.owned) |
| 1214 | { |
| 1215 | x11_export_final_selection(); |
| 1216 | if (clip_star.owned) |
| 1217 | clip_lose_selection(&clip_star); |
| 1218 | if (clip_plus.owned) |
| 1219 | clip_lose_selection(&clip_plus); |
| 1220 | if (x11_display != NULL) |
| 1221 | XFlush(x11_display); |
| 1222 | } |
| 1223 | } |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1224 | |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1225 | # ifdef USE_SYSTEM |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1226 | /* |
| 1227 | * Save clipboard text to restore later. |
| 1228 | */ |
| 1229 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1230 | save_clipboard(void) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1231 | { |
| 1232 | if (clip_star.owned) |
| 1233 | clip_star_save = get_register('*', TRUE); |
| 1234 | if (clip_plus.owned) |
| 1235 | clip_plus_save = get_register('+', TRUE); |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * Restore clipboard text if no one own the X selection. |
| 1240 | */ |
| 1241 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1242 | restore_clipboard(void) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1243 | { |
| 1244 | if (clip_star_save != NULL) |
| 1245 | { |
| 1246 | if (!clip_gen_owner_exists(&clip_star)) |
| 1247 | put_register('*', clip_star_save); |
| 1248 | else |
| 1249 | free_register(clip_star_save); |
| 1250 | clip_star_save = NULL; |
| 1251 | } |
| 1252 | if (clip_plus_save != NULL) |
| 1253 | { |
| 1254 | if (!clip_gen_owner_exists(&clip_plus)) |
| 1255 | put_register('+', clip_plus_save); |
| 1256 | else |
| 1257 | free_register(clip_plus_save); |
| 1258 | clip_plus_save = NULL; |
| 1259 | } |
| 1260 | } |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1261 | # endif |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1262 | #endif |
| 1263 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1264 | /* |
| 1265 | * If the machine has job control, use it to suspend the program, |
| 1266 | * otherwise fake it by starting a new shell. |
| 1267 | */ |
| 1268 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1269 | mch_suspend(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1270 | { |
| 1271 | /* BeOS does have SIGTSTP, but it doesn't work. */ |
| 1272 | #if defined(SIGTSTP) && !defined(__BEOS__) |
| 1273 | out_flush(); /* needed to make cursor visible on some systems */ |
| 1274 | settmode(TMODE_COOK); |
| 1275 | out_flush(); /* needed to disable mouse on some systems */ |
| 1276 | |
| 1277 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1278 | loose_clipboard(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1279 | # endif |
| 1280 | |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1281 | # if defined(_REENTRANT) && defined(SIGCONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1282 | sigcont_received = FALSE; |
| 1283 | # endif |
| 1284 | kill(0, SIGTSTP); /* send ourselves a STOP signal */ |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1285 | # if defined(_REENTRANT) && defined(SIGCONT) |
| 1286 | /* |
| 1287 | * Wait for the SIGCONT signal to be handled. It generally happens |
| 1288 | * immediately, but somehow not all the time. Do not call pause() |
| 1289 | * because there would be race condition which would hang Vim if |
| 1290 | * signal happened in between the test of sigcont_received and the |
| 1291 | * call to pause(). If signal is not yet received, call sleep(0) |
| 1292 | * to just yield CPU. Signal should then be received. If somehow |
| 1293 | * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting |
| 1294 | * further if signal is not received after 1+2+3+4 ms (not expected |
| 1295 | * to happen). |
| 1296 | */ |
| 1297 | { |
Bram Moolenaar | 262735e | 2009-07-14 10:20:22 +0000 | [diff] [blame] | 1298 | long wait_time; |
| 1299 | for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++) |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1300 | /* Loop is not entered most of the time */ |
Bram Moolenaar | 262735e | 2009-07-14 10:20:22 +0000 | [diff] [blame] | 1301 | mch_delay(wait_time, FALSE); |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1302 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | # endif |
| 1304 | |
| 1305 | # ifdef FEAT_TITLE |
| 1306 | /* |
| 1307 | * Set oldtitle to NULL, so the current title is obtained again. |
| 1308 | */ |
| 1309 | vim_free(oldtitle); |
| 1310 | oldtitle = NULL; |
| 1311 | # endif |
| 1312 | settmode(TMODE_RAW); |
| 1313 | need_check_timestamps = TRUE; |
| 1314 | did_check_timestamps = FALSE; |
| 1315 | #else |
| 1316 | suspend_shell(); |
| 1317 | #endif |
| 1318 | } |
| 1319 | |
| 1320 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1321 | mch_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1322 | { |
| 1323 | Columns = 80; |
| 1324 | Rows = 24; |
| 1325 | |
| 1326 | out_flush(); |
| 1327 | set_signals(); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1328 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 1329 | #ifdef MACOS_CONVERT |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1330 | mac_conv_init(); |
| 1331 | #endif |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 1332 | #ifdef FEAT_CYGWIN_WIN32_CLIPBOARD |
| 1333 | win_clip_init(); |
| 1334 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1338 | set_signals(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1339 | { |
| 1340 | #if defined(SIGWINCH) |
| 1341 | /* |
| 1342 | * WINDOW CHANGE signal is handled with sig_winch(). |
| 1343 | */ |
| 1344 | signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch); |
| 1345 | #endif |
| 1346 | |
| 1347 | /* |
| 1348 | * We want the STOP signal to work, to make mch_suspend() work. |
| 1349 | * For "rvim" the STOP signal is ignored. |
| 1350 | */ |
| 1351 | #ifdef SIGTSTP |
| 1352 | signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL); |
| 1353 | #endif |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1354 | #if defined(_REENTRANT) && defined(SIGCONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1355 | signal(SIGCONT, sigcont_handler); |
| 1356 | #endif |
| 1357 | |
| 1358 | /* |
| 1359 | * We want to ignore breaking of PIPEs. |
| 1360 | */ |
| 1361 | #ifdef SIGPIPE |
| 1362 | signal(SIGPIPE, SIG_IGN); |
| 1363 | #endif |
| 1364 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1365 | #ifdef SIGINT |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1366 | catch_int_signal(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1367 | #endif |
| 1368 | |
| 1369 | /* |
| 1370 | * Ignore alarm signals (Perl's alarm() generates it). |
| 1371 | */ |
| 1372 | #ifdef SIGALRM |
| 1373 | signal(SIGALRM, SIG_IGN); |
| 1374 | #endif |
| 1375 | |
| 1376 | /* |
| 1377 | * Catch SIGPWR (power failure?) to preserve the swap files, so that no |
| 1378 | * work will be lost. |
| 1379 | */ |
| 1380 | #ifdef SIGPWR |
| 1381 | signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr); |
| 1382 | #endif |
| 1383 | |
| 1384 | /* |
| 1385 | * Arrange for other signals to gracefully shutdown Vim. |
| 1386 | */ |
| 1387 | catch_signals(deathtrap, SIG_ERR); |
| 1388 | |
| 1389 | #if defined(FEAT_GUI) && defined(SIGHUP) |
| 1390 | /* |
| 1391 | * When the GUI is running, ignore the hangup signal. |
| 1392 | */ |
| 1393 | if (gui.in_use) |
| 1394 | signal(SIGHUP, SIG_IGN); |
| 1395 | #endif |
| 1396 | } |
| 1397 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1398 | #if defined(SIGINT) || defined(PROTO) |
| 1399 | /* |
| 1400 | * Catch CTRL-C (only works while in Cooked mode). |
| 1401 | */ |
| 1402 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1403 | catch_int_signal(void) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1404 | { |
| 1405 | signal(SIGINT, (RETSIGTYPE (*)())catch_sigint); |
| 1406 | } |
| 1407 | #endif |
| 1408 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1409 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1410 | reset_signals(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1411 | { |
| 1412 | catch_signals(SIG_DFL, SIG_DFL); |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1413 | #if defined(_REENTRANT) && defined(SIGCONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1414 | /* SIGCONT isn't in the list, because its default action is ignore */ |
| 1415 | signal(SIGCONT, SIG_DFL); |
| 1416 | #endif |
| 1417 | } |
| 1418 | |
| 1419 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1420 | catch_signals( |
| 1421 | RETSIGTYPE (*func_deadly)(), |
| 1422 | RETSIGTYPE (*func_other)()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1423 | { |
| 1424 | int i; |
| 1425 | |
| 1426 | for (i = 0; signal_info[i].sig != -1; i++) |
| 1427 | if (signal_info[i].deadly) |
| 1428 | { |
| 1429 | #if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION) |
| 1430 | struct sigaction sa; |
| 1431 | |
| 1432 | /* Setup to use the alternate stack for the signal function. */ |
| 1433 | sa.sa_handler = func_deadly; |
| 1434 | sigemptyset(&sa.sa_mask); |
| 1435 | # if defined(__linux__) && defined(_REENTRANT) |
| 1436 | /* On Linux, with glibc compiled for kernel 2.2, there is a bug in |
| 1437 | * thread handling in combination with using the alternate stack: |
| 1438 | * pthread library functions try to use the stack pointer to |
| 1439 | * identify the current thread, causing a SEGV signal, which |
| 1440 | * recursively calls deathtrap() and hangs. */ |
| 1441 | sa.sa_flags = 0; |
| 1442 | # else |
| 1443 | sa.sa_flags = SA_ONSTACK; |
| 1444 | # endif |
| 1445 | sigaction(signal_info[i].sig, &sa, NULL); |
| 1446 | #else |
| 1447 | # if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC) |
| 1448 | struct sigvec sv; |
| 1449 | |
| 1450 | /* Setup to use the alternate stack for the signal function. */ |
| 1451 | sv.sv_handler = func_deadly; |
| 1452 | sv.sv_mask = 0; |
| 1453 | sv.sv_flags = SV_ONSTACK; |
| 1454 | sigvec(signal_info[i].sig, &sv, NULL); |
| 1455 | # else |
| 1456 | signal(signal_info[i].sig, func_deadly); |
| 1457 | # endif |
| 1458 | #endif |
| 1459 | } |
| 1460 | else if (func_other != SIG_ERR) |
| 1461 | signal(signal_info[i].sig, func_other); |
| 1462 | } |
| 1463 | |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 1464 | #ifdef HAVE_SIGPROCMASK |
| 1465 | static void |
| 1466 | block_signals(sigset_t *set) |
| 1467 | { |
| 1468 | sigset_t newset; |
| 1469 | int i; |
| 1470 | |
| 1471 | sigemptyset(&newset); |
| 1472 | |
| 1473 | for (i = 0; signal_info[i].sig != -1; i++) |
| 1474 | sigaddset(&newset, signal_info[i].sig); |
| 1475 | |
| 1476 | # if defined(_REENTRANT) && defined(SIGCONT) |
| 1477 | /* SIGCONT isn't in the list, because its default action is ignore */ |
| 1478 | sigaddset(&newset, SIGCONT); |
| 1479 | # endif |
| 1480 | |
| 1481 | sigprocmask(SIG_BLOCK, &newset, set); |
| 1482 | } |
| 1483 | |
| 1484 | static void |
| 1485 | unblock_signals(sigset_t *set) |
| 1486 | { |
| 1487 | sigprocmask(SIG_SETMASK, set, NULL); |
| 1488 | } |
| 1489 | #endif |
| 1490 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1491 | /* |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1492 | * Handling of SIGHUP, SIGQUIT and SIGTERM: |
Bram Moolenaar | 9e1d283 | 2007-05-06 12:51:41 +0000 | [diff] [blame] | 1493 | * "when" == a signal: when busy, postpone and return FALSE, otherwise |
| 1494 | * return TRUE |
| 1495 | * "when" == SIGNAL_BLOCK: Going to be busy, block signals |
| 1496 | * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 1497 | * signal |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1498 | * Returns TRUE when Vim should exit. |
| 1499 | */ |
| 1500 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1501 | vim_handle_signal(int sig) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1502 | { |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1503 | static int got_signal = 0; |
| 1504 | static int blocked = TRUE; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1505 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1506 | switch (sig) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1507 | { |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1508 | case SIGNAL_BLOCK: blocked = TRUE; |
| 1509 | break; |
| 1510 | |
| 1511 | case SIGNAL_UNBLOCK: blocked = FALSE; |
| 1512 | if (got_signal != 0) |
| 1513 | { |
| 1514 | kill(getpid(), got_signal); |
| 1515 | got_signal = 0; |
| 1516 | } |
| 1517 | break; |
| 1518 | |
| 1519 | default: if (!blocked) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1520 | return TRUE; /* exit! */ |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1521 | got_signal = sig; |
| 1522 | #ifdef SIGPWR |
| 1523 | if (sig != SIGPWR) |
| 1524 | #endif |
| 1525 | got_int = TRUE; /* break any loops */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1526 | break; |
| 1527 | } |
| 1528 | return FALSE; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1532 | * Check_win checks whether we have an interactive stdout. |
| 1533 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1534 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1535 | mch_check_win(int argc UNUSED, char **argv UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1536 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1537 | if (isatty(1)) |
| 1538 | return OK; |
| 1539 | return FAIL; |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * Return TRUE if the input comes from a terminal, FALSE otherwise. |
| 1544 | */ |
| 1545 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1546 | mch_input_isatty(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1547 | { |
| 1548 | if (isatty(read_cmd_fd)) |
| 1549 | return TRUE; |
| 1550 | return FALSE; |
| 1551 | } |
| 1552 | |
| 1553 | #ifdef FEAT_X11 |
| 1554 | |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 1555 | # if defined(ELAPSED_TIMEVAL) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1556 | && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)) |
| 1557 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1558 | /* |
| 1559 | * Give a message about the elapsed time for opening the X window. |
| 1560 | */ |
| 1561 | static void |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 1562 | xopen_message(long elapsed_msec) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1563 | { |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 1564 | smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1565 | } |
| 1566 | # endif |
| 1567 | #endif |
| 1568 | |
| 1569 | #if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD)) |
| 1570 | /* |
| 1571 | * A few functions shared by X11 title and clipboard code. |
| 1572 | */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1573 | static int x_error_handler(Display *dpy, XErrorEvent *error_event); |
| 1574 | static int x_error_check(Display *dpy, XErrorEvent *error_event); |
| 1575 | static int x_connect_to_server(void); |
| 1576 | static int test_x11_window(Display *dpy); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1577 | |
| 1578 | static int got_x_error = FALSE; |
| 1579 | |
| 1580 | /* |
| 1581 | * X Error handler, otherwise X just exits! (very rude) -- webb |
| 1582 | */ |
| 1583 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1584 | x_error_handler(Display *dpy, XErrorEvent *error_event) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1585 | { |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 1586 | XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1587 | STRCAT(IObuff, _("\nVim: Got X error\n")); |
| 1588 | |
| 1589 | /* We cannot print a message and continue, because no X calls are allowed |
| 1590 | * here (causes my system to hang). Silently continuing might be an |
| 1591 | * alternative... */ |
| 1592 | preserve_exit(); /* preserve files and exit */ |
| 1593 | |
| 1594 | return 0; /* NOTREACHED */ |
| 1595 | } |
| 1596 | |
| 1597 | /* |
| 1598 | * Another X Error handler, just used to check for errors. |
| 1599 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1600 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1601 | x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1602 | { |
| 1603 | got_x_error = TRUE; |
| 1604 | return 0; |
| 1605 | } |
| 1606 | |
| 1607 | #if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) |
| 1608 | # if defined(HAVE_SETJMP_H) |
| 1609 | /* |
| 1610 | * An X IO Error handler, used to catch error while opening the display. |
| 1611 | */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1612 | static int x_IOerror_check(Display *dpy); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1613 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1614 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1615 | x_IOerror_check(Display *dpy UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1616 | { |
| 1617 | /* This function should not return, it causes exit(). Longjump instead. */ |
| 1618 | LONGJMP(lc_jump_env, 1); |
Bram Moolenaar | fe17e76 | 2013-06-29 14:17:02 +0200 | [diff] [blame] | 1619 | # if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
Bram Moolenaar | b4990bf | 2010-02-11 18:19:38 +0100 | [diff] [blame] | 1620 | return 0; /* avoid the compiler complains about missing return value */ |
| 1621 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1622 | } |
| 1623 | # endif |
| 1624 | |
| 1625 | /* |
| 1626 | * An X IO Error handler, used to catch terminal errors. |
| 1627 | */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1628 | static int x_IOerror_handler(Display *dpy); |
| 1629 | static void may_restore_clipboard(void); |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1630 | static int xterm_dpy_was_reset = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1631 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1632 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1633 | x_IOerror_handler(Display *dpy UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1634 | { |
| 1635 | xterm_dpy = NULL; |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1636 | xterm_dpy_was_reset = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1637 | x11_window = 0; |
| 1638 | x11_display = NULL; |
| 1639 | xterm_Shell = (Widget)0; |
| 1640 | |
| 1641 | /* This function should not return, it causes exit(). Longjump instead. */ |
| 1642 | LONGJMP(x_jump_env, 1); |
Bram Moolenaar | fe17e76 | 2013-06-29 14:17:02 +0200 | [diff] [blame] | 1643 | # if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
Bram Moolenaar | b4990bf | 2010-02-11 18:19:38 +0100 | [diff] [blame] | 1644 | return 0; /* avoid the compiler complains about missing return value */ |
| 1645 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1646 | } |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1647 | |
| 1648 | /* |
| 1649 | * If the X11 connection was lost try to restore it. |
| 1650 | * Helps when the X11 server was stopped and restarted while Vim was inactive |
Bram Moolenaar | caad4f0 | 2014-12-17 14:36:14 +0100 | [diff] [blame] | 1651 | * (e.g. through tmux). |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1652 | */ |
| 1653 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1654 | may_restore_clipboard(void) |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1655 | { |
| 1656 | if (xterm_dpy_was_reset) |
| 1657 | { |
| 1658 | xterm_dpy_was_reset = FALSE; |
Bram Moolenaar | 527a678 | 2014-12-17 17:59:31 +0100 | [diff] [blame] | 1659 | |
| 1660 | # ifndef LESSTIF_VERSION |
| 1661 | /* This has been reported to avoid Vim getting stuck. */ |
| 1662 | if (app_context != (XtAppContext)NULL) |
| 1663 | { |
| 1664 | XtDestroyApplicationContext(app_context); |
| 1665 | app_context = (XtAppContext)NULL; |
| 1666 | x11_display = NULL; /* freed by XtDestroyApplicationContext() */ |
| 1667 | } |
| 1668 | # endif |
| 1669 | |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1670 | setup_term_clip(); |
| 1671 | get_x11_title(FALSE); |
| 1672 | } |
| 1673 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1674 | #endif |
| 1675 | |
| 1676 | /* |
| 1677 | * Return TRUE when connection to the X server is desired. |
| 1678 | */ |
| 1679 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1680 | x_connect_to_server(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1681 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1682 | #if defined(FEAT_CLIENTSERVER) |
| 1683 | if (x_force_connect) |
| 1684 | return TRUE; |
| 1685 | #endif |
| 1686 | if (x_no_connect) |
| 1687 | return FALSE; |
| 1688 | |
| 1689 | /* Check for a match with "exclude:" from 'clipboard'. */ |
| 1690 | if (clip_exclude_prog != NULL) |
| 1691 | { |
Bram Moolenaar | dffa5b8 | 2014-11-19 16:38:07 +0100 | [diff] [blame] | 1692 | if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1693 | return FALSE; |
| 1694 | } |
| 1695 | return TRUE; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * Test if "dpy" and x11_window are valid by getting the window title. |
| 1700 | * I don't actually want it yet, so there may be a simpler call to use, but |
| 1701 | * this will cause the error handler x_error_check() to be called if anything |
| 1702 | * is wrong, such as the window pointer being invalid (as can happen when the |
| 1703 | * user changes his DISPLAY, but not his WINDOWID) -- webb |
| 1704 | */ |
| 1705 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1706 | test_x11_window(Display *dpy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1707 | { |
| 1708 | int (*old_handler)(); |
| 1709 | XTextProperty text_prop; |
| 1710 | |
| 1711 | old_handler = XSetErrorHandler(x_error_check); |
| 1712 | got_x_error = FALSE; |
| 1713 | if (XGetWMName(dpy, x11_window, &text_prop)) |
| 1714 | XFree((void *)text_prop.value); |
| 1715 | XSync(dpy, False); |
| 1716 | (void)XSetErrorHandler(old_handler); |
| 1717 | |
| 1718 | if (p_verbose > 0 && got_x_error) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 1719 | verb_msg((char_u *)_("Testing the X display failed")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1720 | |
| 1721 | return (got_x_error ? FAIL : OK); |
| 1722 | } |
| 1723 | #endif |
| 1724 | |
| 1725 | #ifdef FEAT_TITLE |
| 1726 | |
| 1727 | #ifdef FEAT_X11 |
| 1728 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1729 | static int get_x11_thing(int get_title, int test_only); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1730 | |
| 1731 | /* |
| 1732 | * try to get x11 window and display |
| 1733 | * |
| 1734 | * return FAIL for failure, OK otherwise |
| 1735 | */ |
| 1736 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1737 | get_x11_windis(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1738 | { |
| 1739 | char *winid; |
| 1740 | static int result = -1; |
| 1741 | #define XD_NONE 0 /* x11_display not set here */ |
| 1742 | #define XD_HERE 1 /* x11_display opened here */ |
| 1743 | #define XD_GUI 2 /* x11_display used from gui.dpy */ |
| 1744 | #define XD_XTERM 3 /* x11_display used from xterm_dpy */ |
| 1745 | static int x11_display_from = XD_NONE; |
| 1746 | static int did_set_error_handler = FALSE; |
| 1747 | |
| 1748 | if (!did_set_error_handler) |
| 1749 | { |
| 1750 | /* X just exits if it finds an error otherwise! */ |
| 1751 | (void)XSetErrorHandler(x_error_handler); |
| 1752 | did_set_error_handler = TRUE; |
| 1753 | } |
| 1754 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 1755 | #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1756 | if (gui.in_use) |
| 1757 | { |
| 1758 | /* |
| 1759 | * If the X11 display was opened here before, for the window where Vim |
| 1760 | * was started, close that one now to avoid a memory leak. |
| 1761 | */ |
| 1762 | if (x11_display_from == XD_HERE && x11_display != NULL) |
| 1763 | { |
| 1764 | XCloseDisplay(x11_display); |
| 1765 | x11_display_from = XD_NONE; |
| 1766 | } |
| 1767 | if (gui_get_x11_windis(&x11_window, &x11_display) == OK) |
| 1768 | { |
| 1769 | x11_display_from = XD_GUI; |
| 1770 | return OK; |
| 1771 | } |
| 1772 | x11_display = NULL; |
| 1773 | return FAIL; |
| 1774 | } |
| 1775 | else if (x11_display_from == XD_GUI) |
| 1776 | { |
| 1777 | /* GUI must have stopped somehow, clear x11_display */ |
| 1778 | x11_window = 0; |
| 1779 | x11_display = NULL; |
| 1780 | x11_display_from = XD_NONE; |
| 1781 | } |
| 1782 | #endif |
| 1783 | |
| 1784 | /* When started with the "-X" argument, don't try connecting. */ |
| 1785 | if (!x_connect_to_server()) |
| 1786 | return FAIL; |
| 1787 | |
| 1788 | /* |
| 1789 | * If WINDOWID not set, should try another method to find out |
| 1790 | * what the current window number is. The only code I know for |
| 1791 | * this is very complicated. |
| 1792 | * We assume that zero is invalid for WINDOWID. |
| 1793 | */ |
| 1794 | if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL) |
| 1795 | x11_window = (Window)atol(winid); |
| 1796 | |
| 1797 | #ifdef FEAT_XCLIPBOARD |
| 1798 | if (xterm_dpy != NULL && x11_window != 0) |
| 1799 | { |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 1800 | /* We may have checked it already, but Gnome terminal can move us to |
| 1801 | * another window, so we need to check every time. */ |
| 1802 | if (x11_display_from != XD_XTERM) |
| 1803 | { |
| 1804 | /* |
| 1805 | * If the X11 display was opened here before, for the window where |
| 1806 | * Vim was started, close that one now to avoid a memory leak. |
| 1807 | */ |
| 1808 | if (x11_display_from == XD_HERE && x11_display != NULL) |
| 1809 | XCloseDisplay(x11_display); |
| 1810 | x11_display = xterm_dpy; |
| 1811 | x11_display_from = XD_XTERM; |
| 1812 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1813 | if (test_x11_window(x11_display) == FAIL) |
| 1814 | { |
| 1815 | /* probably bad $WINDOWID */ |
| 1816 | x11_window = 0; |
| 1817 | x11_display = NULL; |
| 1818 | x11_display_from = XD_NONE; |
| 1819 | return FAIL; |
| 1820 | } |
| 1821 | return OK; |
| 1822 | } |
| 1823 | #endif |
| 1824 | |
| 1825 | if (x11_window == 0 || x11_display == NULL) |
| 1826 | result = -1; |
| 1827 | |
| 1828 | if (result != -1) /* Have already been here and set this */ |
| 1829 | return result; /* Don't do all these X calls again */ |
| 1830 | |
| 1831 | if (x11_window != 0 && x11_display == NULL) |
| 1832 | { |
| 1833 | #ifdef SET_SIG_ALARM |
| 1834 | RETSIGTYPE (*sig_save)(); |
| 1835 | #endif |
| 1836 | #if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 1837 | struct timeval start_tv; |
| 1838 | |
| 1839 | if (p_verbose > 0) |
| 1840 | gettimeofday(&start_tv, NULL); |
| 1841 | #endif |
| 1842 | |
| 1843 | #ifdef SET_SIG_ALARM |
| 1844 | /* |
| 1845 | * Opening the Display may hang if the DISPLAY setting is wrong, or |
| 1846 | * the network connection is bad. Set an alarm timer to get out. |
| 1847 | */ |
| 1848 | sig_alarm_called = FALSE; |
| 1849 | sig_save = (RETSIGTYPE (*)())signal(SIGALRM, |
| 1850 | (RETSIGTYPE (*)())sig_alarm); |
| 1851 | alarm(2); |
| 1852 | #endif |
| 1853 | x11_display = XOpenDisplay(NULL); |
| 1854 | |
| 1855 | #ifdef SET_SIG_ALARM |
| 1856 | alarm(0); |
| 1857 | signal(SIGALRM, (RETSIGTYPE (*)())sig_save); |
| 1858 | if (p_verbose > 0 && sig_alarm_called) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 1859 | verb_msg((char_u *)_("Opening the X display timed out")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1860 | #endif |
| 1861 | if (x11_display != NULL) |
| 1862 | { |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 1863 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1864 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 1865 | { |
| 1866 | verbose_enter(); |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 1867 | xopen_message(ELAPSED_FUNC(start_tv)); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 1868 | verbose_leave(); |
| 1869 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1870 | # endif |
| 1871 | if (test_x11_window(x11_display) == FAIL) |
| 1872 | { |
| 1873 | /* Maybe window id is bad */ |
| 1874 | x11_window = 0; |
| 1875 | XCloseDisplay(x11_display); |
| 1876 | x11_display = NULL; |
| 1877 | } |
| 1878 | else |
| 1879 | x11_display_from = XD_HERE; |
| 1880 | } |
| 1881 | } |
| 1882 | if (x11_window == 0 || x11_display == NULL) |
| 1883 | return (result = FAIL); |
Bram Moolenaar | 727c876 | 2010-10-20 19:17:48 +0200 | [diff] [blame] | 1884 | |
| 1885 | # ifdef FEAT_EVAL |
| 1886 | set_vim_var_nr(VV_WINDOWID, (long)x11_window); |
| 1887 | # endif |
| 1888 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1889 | return (result = OK); |
| 1890 | } |
| 1891 | |
| 1892 | /* |
| 1893 | * Determine original x11 Window Title |
| 1894 | */ |
| 1895 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1896 | get_x11_title(int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1897 | { |
Bram Moolenaar | 47136d7 | 2004-10-12 20:02:24 +0000 | [diff] [blame] | 1898 | return get_x11_thing(TRUE, test_only); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /* |
| 1902 | * Determine original x11 Window icon |
| 1903 | */ |
| 1904 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1905 | get_x11_icon(int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1906 | { |
| 1907 | int retval = FALSE; |
| 1908 | |
| 1909 | retval = get_x11_thing(FALSE, test_only); |
| 1910 | |
| 1911 | /* could not get old icon, use terminal name */ |
| 1912 | if (oldicon == NULL && !test_only) |
| 1913 | { |
| 1914 | if (STRNCMP(T_NAME, "builtin_", 8) == 0) |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 1915 | oldicon = vim_strsave(T_NAME + 8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1916 | else |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 1917 | oldicon = vim_strsave(T_NAME); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | return retval; |
| 1921 | } |
| 1922 | |
| 1923 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1924 | get_x11_thing( |
| 1925 | int get_title, /* get title string */ |
| 1926 | int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1927 | { |
| 1928 | XTextProperty text_prop; |
| 1929 | int retval = FALSE; |
| 1930 | Status status; |
| 1931 | |
| 1932 | if (get_x11_windis() == OK) |
| 1933 | { |
| 1934 | /* Get window/icon name if any */ |
| 1935 | if (get_title) |
| 1936 | status = XGetWMName(x11_display, x11_window, &text_prop); |
| 1937 | else |
| 1938 | status = XGetWMIconName(x11_display, x11_window, &text_prop); |
| 1939 | |
| 1940 | /* |
| 1941 | * If terminal is xterm, then x11_window may be a child window of the |
| 1942 | * outer xterm window that actually contains the window/icon name, so |
| 1943 | * keep traversing up the tree until a window with a title/icon is |
| 1944 | * found. |
| 1945 | */ |
| 1946 | /* Previously this was only done for xterm and alikes. I don't see a |
| 1947 | * reason why it would fail for other terminal emulators. |
| 1948 | * if (term_is_xterm) */ |
| 1949 | { |
| 1950 | Window root; |
| 1951 | Window parent; |
| 1952 | Window win = x11_window; |
| 1953 | Window *children; |
| 1954 | unsigned int num_children; |
| 1955 | |
| 1956 | while (!status || text_prop.value == NULL) |
| 1957 | { |
| 1958 | if (!XQueryTree(x11_display, win, &root, &parent, &children, |
| 1959 | &num_children)) |
| 1960 | break; |
| 1961 | if (children) |
| 1962 | XFree((void *)children); |
| 1963 | if (parent == root || parent == 0) |
| 1964 | break; |
| 1965 | |
| 1966 | win = parent; |
| 1967 | if (get_title) |
| 1968 | status = XGetWMName(x11_display, win, &text_prop); |
| 1969 | else |
| 1970 | status = XGetWMIconName(x11_display, win, &text_prop); |
| 1971 | } |
| 1972 | } |
| 1973 | if (status && text_prop.value != NULL) |
| 1974 | { |
| 1975 | retval = TRUE; |
| 1976 | if (!test_only) |
| 1977 | { |
Bram Moolenaar | f82bac3 | 2010-07-25 22:30:20 +0200 | [diff] [blame] | 1978 | #if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE) |
| 1979 | if (text_prop.encoding == XA_STRING |
| 1980 | # ifdef FEAT_MBYTE |
| 1981 | && !has_mbyte |
| 1982 | # endif |
| 1983 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1984 | { |
| 1985 | #endif |
| 1986 | if (get_title) |
| 1987 | oldtitle = vim_strsave((char_u *)text_prop.value); |
| 1988 | else |
| 1989 | oldicon = vim_strsave((char_u *)text_prop.value); |
Bram Moolenaar | f82bac3 | 2010-07-25 22:30:20 +0200 | [diff] [blame] | 1990 | #if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1991 | } |
| 1992 | else |
| 1993 | { |
| 1994 | char **cl; |
| 1995 | Status transform_status; |
| 1996 | int n = 0; |
| 1997 | |
| 1998 | transform_status = XmbTextPropertyToTextList(x11_display, |
| 1999 | &text_prop, |
| 2000 | &cl, &n); |
| 2001 | if (transform_status >= Success && n > 0 && cl[0]) |
| 2002 | { |
| 2003 | if (get_title) |
| 2004 | oldtitle = vim_strsave((char_u *) cl[0]); |
| 2005 | else |
| 2006 | oldicon = vim_strsave((char_u *) cl[0]); |
| 2007 | XFreeStringList(cl); |
| 2008 | } |
| 2009 | else |
| 2010 | { |
| 2011 | if (get_title) |
| 2012 | oldtitle = vim_strsave((char_u *)text_prop.value); |
| 2013 | else |
| 2014 | oldicon = vim_strsave((char_u *)text_prop.value); |
| 2015 | } |
| 2016 | } |
| 2017 | #endif |
| 2018 | } |
| 2019 | XFree((void *)text_prop.value); |
| 2020 | } |
| 2021 | } |
| 2022 | return retval; |
| 2023 | } |
| 2024 | |
Bram Moolenaar | cbc246a | 2014-10-11 14:47:26 +0200 | [diff] [blame] | 2025 | /* Xutf8 functions are not avaialble on older systems. Note that on some |
| 2026 | * systems X_HAVE_UTF8_STRING may be defined in a header file but |
| 2027 | * Xutf8SetWMProperties() is not in the X11 library. Configure checks for |
| 2028 | * that and defines HAVE_XUTF8SETWMPROPERTIES. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2029 | #if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE) |
Bram Moolenaar | cbc246a | 2014-10-11 14:47:26 +0200 | [diff] [blame] | 2030 | # if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2031 | # define USE_UTF8_STRING |
| 2032 | # endif |
| 2033 | #endif |
| 2034 | |
| 2035 | /* |
| 2036 | * Set x11 Window Title |
| 2037 | * |
| 2038 | * get_x11_windis() must be called before this and have returned OK |
| 2039 | */ |
| 2040 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2041 | set_x11_title(char_u *title) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2042 | { |
| 2043 | /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING |
| 2044 | * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't |
| 2045 | * supported everywhere and STRING doesn't work for multi-byte titles. |
| 2046 | */ |
| 2047 | #ifdef USE_UTF8_STRING |
| 2048 | if (enc_utf8) |
| 2049 | Xutf8SetWMProperties(x11_display, x11_window, (const char *)title, |
| 2050 | NULL, NULL, 0, NULL, NULL, NULL); |
| 2051 | else |
| 2052 | #endif |
| 2053 | { |
| 2054 | #if XtSpecificationRelease >= 4 |
| 2055 | # ifdef FEAT_XFONTSET |
| 2056 | XmbSetWMProperties(x11_display, x11_window, (const char *)title, |
| 2057 | NULL, NULL, 0, NULL, NULL, NULL); |
| 2058 | # else |
| 2059 | XTextProperty text_prop; |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 2060 | char *c_title = (char *)title; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2061 | |
| 2062 | /* directly from example 3-18 "basicwin" of Xlib Programming Manual */ |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 2063 | (void)XStringListToTextProperty(&c_title, 1, &text_prop); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2064 | XSetWMProperties(x11_display, x11_window, &text_prop, |
| 2065 | NULL, NULL, 0, NULL, NULL, NULL); |
| 2066 | # endif |
| 2067 | #else |
| 2068 | XStoreName(x11_display, x11_window, (char *)title); |
| 2069 | #endif |
| 2070 | } |
| 2071 | XFlush(x11_display); |
| 2072 | } |
| 2073 | |
| 2074 | /* |
| 2075 | * Set x11 Window icon |
| 2076 | * |
| 2077 | * get_x11_windis() must be called before this and have returned OK |
| 2078 | */ |
| 2079 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2080 | set_x11_icon(char_u *icon) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2081 | { |
| 2082 | /* See above for comments about using X*SetWMProperties(). */ |
| 2083 | #ifdef USE_UTF8_STRING |
| 2084 | if (enc_utf8) |
| 2085 | Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon, |
| 2086 | NULL, 0, NULL, NULL, NULL); |
| 2087 | else |
| 2088 | #endif |
| 2089 | { |
| 2090 | #if XtSpecificationRelease >= 4 |
| 2091 | # ifdef FEAT_XFONTSET |
| 2092 | XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon, |
| 2093 | NULL, 0, NULL, NULL, NULL); |
| 2094 | # else |
| 2095 | XTextProperty text_prop; |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 2096 | char *c_icon = (char *)icon; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2097 | |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 2098 | (void)XStringListToTextProperty(&c_icon, 1, &text_prop); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2099 | XSetWMProperties(x11_display, x11_window, NULL, &text_prop, |
| 2100 | NULL, 0, NULL, NULL, NULL); |
| 2101 | # endif |
| 2102 | #else |
| 2103 | XSetIconName(x11_display, x11_window, (char *)icon); |
| 2104 | #endif |
| 2105 | } |
| 2106 | XFlush(x11_display); |
| 2107 | } |
| 2108 | |
| 2109 | #else /* FEAT_X11 */ |
| 2110 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2111 | static int |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2112 | get_x11_title(int test_only UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2113 | { |
| 2114 | return FALSE; |
| 2115 | } |
| 2116 | |
| 2117 | static int |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2118 | get_x11_icon(int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2119 | { |
| 2120 | if (!test_only) |
| 2121 | { |
| 2122 | if (STRNCMP(T_NAME, "builtin_", 8) == 0) |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 2123 | oldicon = vim_strsave(T_NAME + 8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2124 | else |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 2125 | oldicon = vim_strsave(T_NAME); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2126 | } |
| 2127 | return FALSE; |
| 2128 | } |
| 2129 | |
| 2130 | #endif /* FEAT_X11 */ |
| 2131 | |
| 2132 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2133 | mch_can_restore_title(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2134 | { |
| 2135 | return get_x11_title(TRUE); |
| 2136 | } |
| 2137 | |
| 2138 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2139 | mch_can_restore_icon(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2140 | { |
| 2141 | return get_x11_icon(TRUE); |
| 2142 | } |
| 2143 | |
| 2144 | /* |
| 2145 | * Set the window title and icon. |
| 2146 | */ |
| 2147 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2148 | mch_settitle(char_u *title, char_u *icon) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2149 | { |
| 2150 | int type = 0; |
| 2151 | static int recursive = 0; |
| 2152 | |
| 2153 | if (T_NAME == NULL) /* no terminal name (yet) */ |
| 2154 | return; |
| 2155 | if (title == NULL && icon == NULL) /* nothing to do */ |
| 2156 | return; |
| 2157 | |
| 2158 | /* When one of the X11 functions causes a deadly signal, we get here again |
| 2159 | * recursively. Avoid hanging then (something is probably locked). */ |
| 2160 | if (recursive) |
| 2161 | return; |
| 2162 | ++recursive; |
| 2163 | |
| 2164 | /* |
| 2165 | * if the window ID and the display is known, we may use X11 calls |
| 2166 | */ |
| 2167 | #ifdef FEAT_X11 |
| 2168 | if (get_x11_windis() == OK) |
| 2169 | type = 1; |
| 2170 | #else |
| 2171 | # if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK) |
| 2172 | if (gui.in_use) |
| 2173 | type = 1; |
| 2174 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2175 | #endif |
| 2176 | |
| 2177 | /* |
Bram Moolenaar | f82bac3 | 2010-07-25 22:30:20 +0200 | [diff] [blame] | 2178 | * 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] | 2179 | * than x11 calls, because the x11 calls don't always work |
| 2180 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2181 | if ((type || *T_TS != NUL) && title != NULL) |
| 2182 | { |
| 2183 | if (oldtitle == NULL |
| 2184 | #ifdef FEAT_GUI |
| 2185 | && !gui.in_use |
| 2186 | #endif |
| 2187 | ) /* first call but not in GUI, save title */ |
| 2188 | (void)get_x11_title(FALSE); |
| 2189 | |
| 2190 | if (*T_TS != NUL) /* it's OK if t_fs is empty */ |
| 2191 | term_settitle(title); |
| 2192 | #ifdef FEAT_X11 |
| 2193 | else |
| 2194 | # ifdef FEAT_GUI_GTK |
| 2195 | if (!gui.in_use) /* don't do this if GTK+ is running */ |
| 2196 | # endif |
| 2197 | set_x11_title(title); /* x11 */ |
| 2198 | #endif |
Bram Moolenaar | 2fa15e6 | 2005-01-04 21:23:48 +0000 | [diff] [blame] | 2199 | #if defined(FEAT_GUI_GTK) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2200 | || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) |
| 2201 | else |
| 2202 | gui_mch_settitle(title, icon); |
| 2203 | #endif |
| 2204 | did_set_title = TRUE; |
| 2205 | } |
| 2206 | |
| 2207 | if ((type || *T_CIS != NUL) && icon != NULL) |
| 2208 | { |
| 2209 | if (oldicon == NULL |
| 2210 | #ifdef FEAT_GUI |
| 2211 | && !gui.in_use |
| 2212 | #endif |
| 2213 | ) /* first call, save icon */ |
| 2214 | get_x11_icon(FALSE); |
| 2215 | |
| 2216 | if (*T_CIS != NUL) |
| 2217 | { |
| 2218 | out_str(T_CIS); /* set icon start */ |
| 2219 | out_str_nf(icon); |
| 2220 | out_str(T_CIE); /* set icon end */ |
| 2221 | out_flush(); |
| 2222 | } |
| 2223 | #ifdef FEAT_X11 |
| 2224 | else |
| 2225 | # ifdef FEAT_GUI_GTK |
| 2226 | if (!gui.in_use) /* don't do this if GTK+ is running */ |
| 2227 | # endif |
| 2228 | set_x11_icon(icon); /* x11 */ |
| 2229 | #endif |
| 2230 | did_set_icon = TRUE; |
| 2231 | } |
| 2232 | --recursive; |
| 2233 | } |
| 2234 | |
| 2235 | /* |
| 2236 | * Restore the window/icon title. |
| 2237 | * "which" is one of: |
| 2238 | * 1 only restore title |
| 2239 | * 2 only restore icon |
| 2240 | * 3 restore title and icon |
| 2241 | */ |
| 2242 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2243 | mch_restore_title(int which) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2244 | { |
| 2245 | /* only restore the title or icon when it has been set */ |
| 2246 | mch_settitle(((which & 1) && did_set_title) ? |
| 2247 | (oldtitle ? oldtitle : p_titleold) : NULL, |
| 2248 | ((which & 2) && did_set_icon) ? oldicon : NULL); |
| 2249 | } |
| 2250 | |
| 2251 | #endif /* FEAT_TITLE */ |
| 2252 | |
| 2253 | /* |
| 2254 | * Return TRUE if "name" looks like some xterm name. |
Bram Moolenaar | 3a7c85b | 2005-02-05 21:39:53 +0000 | [diff] [blame] | 2255 | * Seiichi Sato mentioned that "mlterm" works like xterm. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2256 | */ |
| 2257 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2258 | vim_is_xterm(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2259 | { |
| 2260 | if (name == NULL) |
| 2261 | return FALSE; |
| 2262 | return (STRNICMP(name, "xterm", 5) == 0 |
| 2263 | || STRNICMP(name, "nxterm", 6) == 0 |
| 2264 | || STRNICMP(name, "kterm", 5) == 0 |
Bram Moolenaar | 3a7c85b | 2005-02-05 21:39:53 +0000 | [diff] [blame] | 2265 | || STRNICMP(name, "mlterm", 6) == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2266 | || STRNICMP(name, "rxvt", 4) == 0 |
| 2267 | || STRCMP(name, "builtin_xterm") == 0); |
| 2268 | } |
| 2269 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2270 | #if defined(FEAT_MOUSE_XTERM) || defined(PROTO) |
| 2271 | /* |
| 2272 | * Return TRUE if "name" appears to be that of a terminal |
| 2273 | * known to support the xterm-style mouse protocol. |
| 2274 | * Relies on term_is_xterm having been set to its correct value. |
| 2275 | */ |
| 2276 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2277 | use_xterm_like_mouse(char_u *name) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2278 | { |
| 2279 | return (name != NULL |
Bram Moolenaar | e56132b | 2016-08-14 18:23:21 +0200 | [diff] [blame] | 2280 | && (term_is_xterm |
| 2281 | || STRNICMP(name, "screen", 6) == 0 |
Bram Moolenaar | 0ba4070 | 2016-10-12 14:50:54 +0200 | [diff] [blame] | 2282 | || STRNICMP(name, "tmux", 4) == 0 |
Bram Moolenaar | e56132b | 2016-08-14 18:23:21 +0200 | [diff] [blame] | 2283 | || STRICMP(name, "st") == 0 |
| 2284 | || STRNICMP(name, "st-", 3) == 0 |
| 2285 | || STRNICMP(name, "stterm", 6) == 0)); |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2286 | } |
| 2287 | #endif |
| 2288 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2289 | #if defined(FEAT_MOUSE_TTY) || defined(PROTO) |
| 2290 | /* |
| 2291 | * Return non-zero when using an xterm mouse, according to 'ttymouse'. |
| 2292 | * Return 1 for "xterm". |
| 2293 | * Return 2 for "xterm2". |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 2294 | * Return 3 for "urxvt". |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 2295 | * Return 4 for "sgr". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2296 | */ |
| 2297 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2298 | use_xterm_mouse(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2299 | { |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 2300 | if (ttym_flags == TTYM_SGR) |
| 2301 | return 4; |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 2302 | if (ttym_flags == TTYM_URXVT) |
| 2303 | return 3; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2304 | if (ttym_flags == TTYM_XTERM2) |
| 2305 | return 2; |
| 2306 | if (ttym_flags == TTYM_XTERM) |
| 2307 | return 1; |
| 2308 | return 0; |
| 2309 | } |
| 2310 | #endif |
| 2311 | |
| 2312 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2313 | vim_is_iris(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2314 | { |
| 2315 | if (name == NULL) |
| 2316 | return FALSE; |
| 2317 | return (STRNICMP(name, "iris-ansi", 9) == 0 |
| 2318 | || STRCMP(name, "builtin_iris-ansi") == 0); |
| 2319 | } |
| 2320 | |
| 2321 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2322 | vim_is_vt300(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2323 | { |
| 2324 | if (name == NULL) |
| 2325 | return FALSE; /* actually all ANSI comp. terminals should be here */ |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 2326 | /* catch VT100 - VT5xx */ |
| 2327 | return ((STRNICMP(name, "vt", 2) == 0 |
| 2328 | && vim_strchr((char_u *)"12345", name[2]) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2329 | || STRCMP(name, "builtin_vt320") == 0); |
| 2330 | } |
| 2331 | |
| 2332 | /* |
| 2333 | * Return TRUE if "name" is a terminal for which 'ttyfast' should be set. |
| 2334 | * This should include all windowed terminal emulators. |
| 2335 | */ |
| 2336 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2337 | vim_is_fastterm(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2338 | { |
| 2339 | if (name == NULL) |
| 2340 | return FALSE; |
| 2341 | if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name)) |
| 2342 | return TRUE; |
| 2343 | return ( STRNICMP(name, "hpterm", 6) == 0 |
| 2344 | || STRNICMP(name, "sun-cmd", 7) == 0 |
| 2345 | || STRNICMP(name, "screen", 6) == 0 |
Bram Moolenaar | 0ba4070 | 2016-10-12 14:50:54 +0200 | [diff] [blame] | 2346 | || STRNICMP(name, "tmux", 4) == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2347 | || STRNICMP(name, "dtterm", 6) == 0); |
| 2348 | } |
| 2349 | |
| 2350 | /* |
| 2351 | * Insert user name in s[len]. |
| 2352 | * Return OK if a name found. |
| 2353 | */ |
| 2354 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2355 | mch_get_user_name(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2356 | { |
| 2357 | #ifdef VMS |
Bram Moolenaar | ffb8ab0 | 2005-09-07 21:15:32 +0000 | [diff] [blame] | 2358 | vim_strncpy(s, (char_u *)cuserid(NULL), len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2359 | return OK; |
| 2360 | #else |
| 2361 | return mch_get_uname(getuid(), s, len); |
| 2362 | #endif |
| 2363 | } |
| 2364 | |
| 2365 | /* |
| 2366 | * Insert user name for "uid" in s[len]. |
| 2367 | * Return OK if a name found. |
| 2368 | */ |
| 2369 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2370 | mch_get_uname(uid_t uid, char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2371 | { |
| 2372 | #if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID) |
| 2373 | struct passwd *pw; |
| 2374 | |
| 2375 | if ((pw = getpwuid(uid)) != NULL |
| 2376 | && pw->pw_name != NULL && *(pw->pw_name) != NUL) |
| 2377 | { |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 2378 | vim_strncpy(s, (char_u *)pw->pw_name, len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2379 | return OK; |
| 2380 | } |
| 2381 | #endif |
| 2382 | sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */ |
| 2383 | return FAIL; /* a number is not a name */ |
| 2384 | } |
| 2385 | |
| 2386 | /* |
| 2387 | * Insert host name is s[len]. |
| 2388 | */ |
| 2389 | |
| 2390 | #ifdef HAVE_SYS_UTSNAME_H |
| 2391 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2392 | mch_get_host_name(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2393 | { |
| 2394 | struct utsname vutsname; |
| 2395 | |
| 2396 | if (uname(&vutsname) < 0) |
| 2397 | *s = NUL; |
| 2398 | else |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 2399 | vim_strncpy(s, (char_u *)vutsname.nodename, len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2400 | } |
| 2401 | #else /* HAVE_SYS_UTSNAME_H */ |
| 2402 | |
| 2403 | # ifdef HAVE_SYS_SYSTEMINFO_H |
| 2404 | # define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len) |
| 2405 | # endif |
| 2406 | |
| 2407 | void |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2408 | mch_get_host_name(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2409 | { |
| 2410 | # ifdef VAXC |
| 2411 | vaxc$gethostname((char *)s, len); |
| 2412 | # else |
| 2413 | gethostname((char *)s, len); |
| 2414 | # endif |
| 2415 | s[len - 1] = NUL; /* make sure it's terminated */ |
| 2416 | } |
| 2417 | #endif /* HAVE_SYS_UTSNAME_H */ |
| 2418 | |
| 2419 | /* |
| 2420 | * return process ID |
| 2421 | */ |
| 2422 | long |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2423 | mch_get_pid(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2424 | { |
| 2425 | return (long)getpid(); |
| 2426 | } |
| 2427 | |
| 2428 | #if !defined(HAVE_STRERROR) && defined(USE_GETCWD) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 2429 | static char *strerror(int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2430 | |
| 2431 | static char * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2432 | strerror(int err) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2433 | { |
| 2434 | extern int sys_nerr; |
| 2435 | extern char *sys_errlist[]; |
| 2436 | static char er[20]; |
| 2437 | |
| 2438 | if (err > 0 && err < sys_nerr) |
| 2439 | return (sys_errlist[err]); |
| 2440 | sprintf(er, "Error %d", err); |
| 2441 | return er; |
| 2442 | } |
| 2443 | #endif |
| 2444 | |
| 2445 | /* |
| 2446 | * Get name of current directory into buffer 'buf' of length 'len' bytes. |
| 2447 | * Return OK for success, FAIL for failure. |
| 2448 | */ |
| 2449 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2450 | mch_dirname(char_u *buf, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2451 | { |
| 2452 | #if defined(USE_GETCWD) |
| 2453 | if (getcwd((char *)buf, len) == NULL) |
| 2454 | { |
| 2455 | STRCPY(buf, strerror(errno)); |
| 2456 | return FAIL; |
| 2457 | } |
| 2458 | return OK; |
| 2459 | #else |
| 2460 | return (getwd((char *)buf) != NULL ? OK : FAIL); |
| 2461 | #endif |
| 2462 | } |
| 2463 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2464 | /* |
Bram Moolenaar | 3d20ca1 | 2006-11-28 16:43:58 +0000 | [diff] [blame] | 2465 | * Get absolute file name into "buf[len]". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2466 | * |
| 2467 | * return FAIL for failure, OK for success |
| 2468 | */ |
| 2469 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2470 | mch_FullName( |
| 2471 | char_u *fname, |
| 2472 | char_u *buf, |
| 2473 | int len, |
| 2474 | int force) /* also expand when already absolute path */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2475 | { |
| 2476 | int l; |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2477 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2478 | int fd = -1; |
| 2479 | static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */ |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2480 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2481 | char_u olddir[MAXPATHL]; |
| 2482 | char_u *p; |
| 2483 | int retval = OK; |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2484 | #ifdef __CYGWIN__ |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2485 | char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but |
| 2486 | it's not always defined */ |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2487 | #endif |
| 2488 | |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2489 | #ifdef VMS |
| 2490 | fname = vms_fixfilename(fname); |
| 2491 | #endif |
| 2492 | |
Bram Moolenaar | a244243 | 2007-04-26 14:26:37 +0000 | [diff] [blame] | 2493 | #ifdef __CYGWIN__ |
| 2494 | /* |
| 2495 | * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts". |
| 2496 | */ |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2497 | # if CYGWIN_VERSION_DLL_MAJOR >= 1007 |
Bram Moolenaar | 06b0734 | 2015-12-31 22:26:28 +0100 | [diff] [blame] | 2498 | /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in |
| 2499 | * a forward slash. */ |
| 2500 | cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE, |
| 2501 | fname, posix_fname, MAXPATHL); |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2502 | # else |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2503 | cygwin_conv_to_posix_path(fname, posix_fname); |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2504 | # endif |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2505 | fname = posix_fname; |
Bram Moolenaar | a244243 | 2007-04-26 14:26:37 +0000 | [diff] [blame] | 2506 | #endif |
| 2507 | |
Bram Moolenaar | e3303cb | 2015-12-31 18:29:46 +0100 | [diff] [blame] | 2508 | /* Expand it if forced or not an absolute path. |
| 2509 | * Do not do it for "/file", the result is always "/". */ |
| 2510 | if ((force || !mch_isFullName(fname)) |
| 2511 | && ((p = vim_strrchr(fname, '/')) == NULL || p != fname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2512 | { |
| 2513 | /* |
| 2514 | * If the file name has a path, change to that directory for a moment, |
| 2515 | * and then do the getwd() (and get back to where we were). |
| 2516 | * This will get the correct path name with "../" things. |
| 2517 | */ |
Bram Moolenaar | e3303cb | 2015-12-31 18:29:46 +0100 | [diff] [blame] | 2518 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2519 | { |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2520 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2521 | /* |
| 2522 | * Use fchdir() if possible, it's said to be faster and more |
| 2523 | * reliable. But on SunOS 4 it might not work. Check this by |
| 2524 | * doing a fchdir() right now. |
| 2525 | */ |
| 2526 | if (!dont_fchdir) |
| 2527 | { |
| 2528 | fd = open(".", O_RDONLY | O_EXTRA, 0); |
| 2529 | if (fd >= 0 && fchdir(fd) < 0) |
| 2530 | { |
| 2531 | close(fd); |
| 2532 | fd = -1; |
| 2533 | dont_fchdir = TRUE; /* don't try again */ |
| 2534 | } |
| 2535 | } |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2536 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2537 | |
| 2538 | /* Only change directory when we are sure we can return to where |
| 2539 | * we are now. After doing "su" chdir(".") might not work. */ |
| 2540 | if ( |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2541 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2542 | fd < 0 && |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2543 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2544 | (mch_dirname(olddir, MAXPATHL) == FAIL |
| 2545 | || mch_chdir((char *)olddir) != 0)) |
| 2546 | { |
| 2547 | p = NULL; /* can't get current dir: don't chdir */ |
| 2548 | retval = FAIL; |
| 2549 | } |
| 2550 | else |
| 2551 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2552 | /* The directory is copied into buf[], to be able to remove |
| 2553 | * the file name without changing it (could be a string in |
| 2554 | * read-only memory) */ |
| 2555 | if (p - fname >= len) |
| 2556 | retval = FAIL; |
| 2557 | else |
| 2558 | { |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 2559 | vim_strncpy(buf, fname, p - fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2560 | if (mch_chdir((char *)buf)) |
| 2561 | retval = FAIL; |
| 2562 | else |
| 2563 | fname = p + 1; |
| 2564 | *buf = NUL; |
| 2565 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | if (mch_dirname(buf, len) == FAIL) |
| 2569 | { |
| 2570 | retval = FAIL; |
| 2571 | *buf = NUL; |
| 2572 | } |
| 2573 | if (p != NULL) |
| 2574 | { |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2575 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2576 | if (fd >= 0) |
| 2577 | { |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 2578 | if (p_verbose >= 5) |
| 2579 | { |
| 2580 | verbose_enter(); |
| 2581 | MSG("fchdir() to previous dir"); |
| 2582 | verbose_leave(); |
| 2583 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2584 | l = fchdir(fd); |
| 2585 | close(fd); |
| 2586 | } |
| 2587 | else |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2588 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2589 | l = mch_chdir((char *)olddir); |
| 2590 | if (l != 0) |
| 2591 | EMSG(_(e_prev_dir)); |
| 2592 | } |
| 2593 | |
| 2594 | l = STRLEN(buf); |
Bram Moolenaar | dac7569 | 2012-10-14 04:35:45 +0200 | [diff] [blame] | 2595 | if (l >= len - 1) |
| 2596 | retval = FAIL; /* no space for trailing "/" */ |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2597 | #ifndef VMS |
Bram Moolenaar | dac7569 | 2012-10-14 04:35:45 +0200 | [diff] [blame] | 2598 | else if (l > 0 && buf[l - 1] != '/' && *fname != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2599 | && STRCMP(fname, ".") != 0) |
Bram Moolenaar | dac7569 | 2012-10-14 04:35:45 +0200 | [diff] [blame] | 2600 | STRCAT(buf, "/"); |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2601 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2602 | } |
Bram Moolenaar | 3d20ca1 | 2006-11-28 16:43:58 +0000 | [diff] [blame] | 2603 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2604 | /* Catch file names which are too long. */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 2605 | if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2606 | return FAIL; |
| 2607 | |
| 2608 | /* Do not append ".", "/dir/." is equal to "/dir". */ |
| 2609 | if (STRCMP(fname, ".") != 0) |
| 2610 | STRCAT(buf, fname); |
| 2611 | |
| 2612 | return OK; |
| 2613 | } |
| 2614 | |
| 2615 | /* |
| 2616 | * Return TRUE if "fname" does not depend on the current directory. |
| 2617 | */ |
| 2618 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2619 | mch_isFullName(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2620 | { |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 2621 | #ifdef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2622 | return ( fname[0] == '/' || fname[0] == '.' || |
| 2623 | strchr((char *)fname,':') || strchr((char *)fname,'"') || |
| 2624 | (strchr((char *)fname,'[') && strchr((char *)fname,']'))|| |
| 2625 | (strchr((char *)fname,'<') && strchr((char *)fname,'>')) ); |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 2626 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2627 | return (*fname == '/' || *fname == '~'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2628 | #endif |
| 2629 | } |
| 2630 | |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2631 | #if defined(USE_FNAME_CASE) || defined(PROTO) |
| 2632 | /* |
| 2633 | * Set the case of the file name, if it already exists. This will cause the |
| 2634 | * file name to remain exactly the same. |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 2635 | * Only required for file systems where case is ignored and preserved. |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2636 | */ |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2637 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2638 | fname_case( |
| 2639 | char_u *name, |
| 2640 | int len UNUSED) /* buffer size, only used when name gets longer */ |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2641 | { |
| 2642 | struct stat st; |
| 2643 | char_u *slash, *tail; |
| 2644 | DIR *dirp; |
| 2645 | struct dirent *dp; |
| 2646 | |
Bram Moolenaar | de5e2c2 | 2016-11-04 20:35:31 +0100 | [diff] [blame] | 2647 | if (mch_lstat((char *)name, &st) >= 0) |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2648 | { |
| 2649 | /* Open the directory where the file is located. */ |
| 2650 | slash = vim_strrchr(name, '/'); |
| 2651 | if (slash == NULL) |
| 2652 | { |
| 2653 | dirp = opendir("."); |
| 2654 | tail = name; |
| 2655 | } |
| 2656 | else |
| 2657 | { |
| 2658 | *slash = NUL; |
| 2659 | dirp = opendir((char *)name); |
| 2660 | *slash = '/'; |
| 2661 | tail = slash + 1; |
| 2662 | } |
| 2663 | |
| 2664 | if (dirp != NULL) |
| 2665 | { |
| 2666 | while ((dp = readdir(dirp)) != NULL) |
| 2667 | { |
| 2668 | /* Only accept names that differ in case and are the same byte |
| 2669 | * length. TODO: accept different length name. */ |
| 2670 | if (STRICMP(tail, dp->d_name) == 0 |
| 2671 | && STRLEN(tail) == STRLEN(dp->d_name)) |
| 2672 | { |
| 2673 | char_u newname[MAXPATHL + 1]; |
| 2674 | struct stat st2; |
| 2675 | |
| 2676 | /* Verify the inode is equal. */ |
| 2677 | vim_strncpy(newname, name, MAXPATHL); |
| 2678 | vim_strncpy(newname + (tail - name), (char_u *)dp->d_name, |
| 2679 | MAXPATHL - (tail - name)); |
Bram Moolenaar | de5e2c2 | 2016-11-04 20:35:31 +0100 | [diff] [blame] | 2680 | if (mch_lstat((char *)newname, &st2) >= 0 |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2681 | && st.st_ino == st2.st_ino |
| 2682 | && st.st_dev == st2.st_dev) |
| 2683 | { |
| 2684 | STRCPY(tail, dp->d_name); |
| 2685 | break; |
| 2686 | } |
| 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | closedir(dirp); |
| 2691 | } |
| 2692 | } |
| 2693 | } |
| 2694 | #endif |
| 2695 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2696 | /* |
| 2697 | * Get file permissions for 'name'. |
| 2698 | * Returns -1 when it doesn't exist. |
| 2699 | */ |
| 2700 | long |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2701 | mch_getperm(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2702 | { |
| 2703 | struct stat statb; |
| 2704 | |
| 2705 | /* Keep the #ifdef outside of stat(), it may be a macro. */ |
| 2706 | #ifdef VMS |
| 2707 | if (stat((char *)vms_fixfilename(name), &statb)) |
| 2708 | #else |
| 2709 | if (stat((char *)name, &statb)) |
| 2710 | #endif |
| 2711 | return -1; |
Bram Moolenaar | 708f62c | 2007-08-11 20:24:10 +0000 | [diff] [blame] | 2712 | #ifdef __INTERIX |
| 2713 | /* The top bit makes the value negative, which means the file doesn't |
| 2714 | * exist. Remove the bit, we don't use it. */ |
| 2715 | return statb.st_mode & ~S_ADDACE; |
| 2716 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2717 | return statb.st_mode; |
Bram Moolenaar | 708f62c | 2007-08-11 20:24:10 +0000 | [diff] [blame] | 2718 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | /* |
| 2722 | * set file permission for 'name' to 'perm' |
| 2723 | * |
| 2724 | * return FAIL for failure, OK otherwise |
| 2725 | */ |
| 2726 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2727 | mch_setperm(char_u *name, long perm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2758 | mch_copy_sec(char_u *from_file, char_u *to_file) |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 2759 | { |
| 2760 | if (from_file == NULL) |
| 2761 | return; |
| 2762 | |
| 2763 | if (selinux_enabled == -1) |
| 2764 | selinux_enabled = is_selinux_enabled(); |
| 2765 | |
| 2766 | if (selinux_enabled > 0) |
| 2767 | { |
| 2768 | security_context_t from_context = NULL; |
| 2769 | security_context_t to_context = NULL; |
| 2770 | |
| 2771 | if (getfilecon((char *)from_file, &from_context) < 0) |
| 2772 | { |
| 2773 | /* If the filesystem doesn't support extended attributes, |
| 2774 | the original had no special security context and the |
| 2775 | target cannot have one either. */ |
| 2776 | if (errno == EOPNOTSUPP) |
| 2777 | return; |
| 2778 | |
| 2779 | MSG_PUTS(_("\nCould not get security context for ")); |
| 2780 | msg_outtrans(from_file); |
| 2781 | msg_putchar('\n'); |
| 2782 | return; |
| 2783 | } |
| 2784 | if (getfilecon((char *)to_file, &to_context) < 0) |
| 2785 | { |
| 2786 | MSG_PUTS(_("\nCould not get security context for ")); |
| 2787 | msg_outtrans(to_file); |
| 2788 | msg_putchar('\n'); |
| 2789 | freecon (from_context); |
| 2790 | return ; |
| 2791 | } |
| 2792 | if (strcmp(from_context, to_context) != 0) |
| 2793 | { |
| 2794 | if (setfilecon((char *)to_file, from_context) < 0) |
| 2795 | { |
| 2796 | MSG_PUTS(_("\nCould not set security context for ")); |
| 2797 | msg_outtrans(to_file); |
| 2798 | msg_putchar('\n'); |
| 2799 | } |
| 2800 | } |
| 2801 | freecon(to_context); |
| 2802 | freecon(from_context); |
| 2803 | } |
| 2804 | } |
| 2805 | #endif /* HAVE_SELINUX */ |
| 2806 | |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2807 | #if defined(HAVE_SMACK) && !defined(PROTO) |
| 2808 | /* |
| 2809 | * Copy security info from "from_file" to "to_file". |
| 2810 | */ |
| 2811 | void |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2812 | mch_copy_sec(char_u *from_file, char_u *to_file) |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2813 | { |
Bram Moolenaar | 62f167f | 2014-04-23 12:52:40 +0200 | [diff] [blame] | 2814 | static const char * const smack_copied_attributes[] = |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2815 | { |
| 2816 | XATTR_NAME_SMACK, |
| 2817 | XATTR_NAME_SMACKEXEC, |
| 2818 | XATTR_NAME_SMACKMMAP |
| 2819 | }; |
| 2820 | |
| 2821 | char buffer[SMACK_LABEL_LEN]; |
| 2822 | const char *name; |
| 2823 | int index; |
| 2824 | int ret; |
| 2825 | ssize_t size; |
| 2826 | |
| 2827 | if (from_file == NULL) |
| 2828 | return; |
| 2829 | |
| 2830 | for (index = 0 ; index < (int)(sizeof(smack_copied_attributes) |
| 2831 | / sizeof(smack_copied_attributes)[0]) ; index++) |
| 2832 | { |
| 2833 | /* get the name of the attribute to copy */ |
| 2834 | name = smack_copied_attributes[index]; |
| 2835 | |
| 2836 | /* get the value of the attribute in buffer */ |
| 2837 | size = getxattr((char*)from_file, name, buffer, sizeof(buffer)); |
| 2838 | if (size >= 0) |
| 2839 | { |
| 2840 | /* copy the attribute value of buffer */ |
| 2841 | ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0); |
| 2842 | if (ret < 0) |
| 2843 | { |
Bram Moolenaar | 4a1314c | 2016-01-27 20:47:18 +0100 | [diff] [blame] | 2844 | vim_snprintf((char *)IObuff, IOSIZE, |
| 2845 | _("Could not set security context %s for %s"), |
| 2846 | name, to_file); |
| 2847 | msg_outtrans(IObuff); |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2848 | msg_putchar('\n'); |
| 2849 | } |
| 2850 | } |
| 2851 | else |
| 2852 | { |
| 2853 | /* what reason of not having the attribute value? */ |
| 2854 | switch (errno) |
| 2855 | { |
| 2856 | case ENOTSUP: |
| 2857 | /* extended attributes aren't supported or enabled */ |
| 2858 | /* should a message be echoed? not sure... */ |
| 2859 | return; /* leave because it isn't usefull to continue */ |
| 2860 | |
| 2861 | case ERANGE: |
| 2862 | default: |
| 2863 | /* no enough size OR unexpected error */ |
Bram Moolenaar | 4a1314c | 2016-01-27 20:47:18 +0100 | [diff] [blame] | 2864 | vim_snprintf((char *)IObuff, IOSIZE, |
| 2865 | _("Could not get security context %s for %s. Removing it!"), |
| 2866 | name, from_file); |
| 2867 | msg_puts(IObuff); |
| 2868 | msg_putchar('\n'); |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2869 | /* FALLTHROUGH to remove the attribute */ |
| 2870 | |
| 2871 | case ENODATA: |
| 2872 | /* no attribute of this name */ |
| 2873 | ret = removexattr((char*)to_file, name); |
Bram Moolenaar | 57a728d | 2014-04-02 23:09:26 +0200 | [diff] [blame] | 2874 | /* Silently ignore errors, apparently this happens when |
| 2875 | * smack is not actually being used. */ |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2876 | break; |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | } |
| 2881 | #endif /* HAVE_SMACK */ |
| 2882 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2883 | /* |
| 2884 | * Return a pointer to the ACL of file "fname" in allocated memory. |
| 2885 | * Return NULL if the ACL is not available for whatever reason. |
| 2886 | */ |
| 2887 | vim_acl_T |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2888 | mch_get_acl(char_u *fname UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2889 | { |
| 2890 | vim_acl_T ret = NULL; |
| 2891 | #ifdef HAVE_POSIX_ACL |
| 2892 | ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS); |
| 2893 | #else |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2894 | #ifdef HAVE_SOLARIS_ZFS_ACL |
| 2895 | acl_t *aclent; |
| 2896 | |
| 2897 | if (acl_get((char *)fname, 0, &aclent) < 0) |
| 2898 | return NULL; |
| 2899 | ret = (vim_acl_T)aclent; |
| 2900 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2901 | #ifdef HAVE_SOLARIS_ACL |
| 2902 | vim_acl_solaris_T *aclent; |
| 2903 | |
| 2904 | aclent = malloc(sizeof(vim_acl_solaris_T)); |
| 2905 | if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0) |
| 2906 | { |
| 2907 | free(aclent); |
| 2908 | return NULL; |
| 2909 | } |
| 2910 | aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t)); |
| 2911 | if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0) |
| 2912 | { |
| 2913 | free(aclent->acl_entry); |
| 2914 | free(aclent); |
| 2915 | return NULL; |
| 2916 | } |
| 2917 | ret = (vim_acl_T)aclent; |
| 2918 | #else |
| 2919 | #if defined(HAVE_AIX_ACL) |
| 2920 | int aclsize; |
| 2921 | struct acl *aclent; |
| 2922 | |
| 2923 | aclsize = sizeof(struct acl); |
| 2924 | aclent = malloc(aclsize); |
| 2925 | if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0) |
| 2926 | { |
| 2927 | if (errno == ENOSPC) |
| 2928 | { |
| 2929 | aclsize = aclent->acl_len; |
| 2930 | aclent = realloc(aclent, aclsize); |
| 2931 | if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0) |
| 2932 | { |
| 2933 | free(aclent); |
| 2934 | return NULL; |
| 2935 | } |
| 2936 | } |
| 2937 | else |
| 2938 | { |
| 2939 | free(aclent); |
| 2940 | return NULL; |
| 2941 | } |
| 2942 | } |
| 2943 | ret = (vim_acl_T)aclent; |
| 2944 | #endif /* HAVE_AIX_ACL */ |
| 2945 | #endif /* HAVE_SOLARIS_ACL */ |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2946 | #endif /* HAVE_SOLARIS_ZFS_ACL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2947 | #endif /* HAVE_POSIX_ACL */ |
| 2948 | return ret; |
| 2949 | } |
| 2950 | |
| 2951 | /* |
| 2952 | * Set the ACL of file "fname" to "acl" (unless it's NULL). |
| 2953 | */ |
| 2954 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2955 | mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2956 | { |
| 2957 | if (aclent == NULL) |
| 2958 | return; |
| 2959 | #ifdef HAVE_POSIX_ACL |
| 2960 | acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent); |
| 2961 | #else |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2962 | #ifdef HAVE_SOLARIS_ZFS_ACL |
| 2963 | acl_set((char *)fname, (acl_t *)aclent); |
| 2964 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2965 | #ifdef HAVE_SOLARIS_ACL |
| 2966 | acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt, |
| 2967 | ((vim_acl_solaris_T *)aclent)->acl_entry); |
| 2968 | #else |
| 2969 | #ifdef HAVE_AIX_ACL |
| 2970 | chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len); |
| 2971 | #endif /* HAVE_AIX_ACL */ |
| 2972 | #endif /* HAVE_SOLARIS_ACL */ |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2973 | #endif /* HAVE_SOLARIS_ZFS_ACL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2974 | #endif /* HAVE_POSIX_ACL */ |
| 2975 | } |
| 2976 | |
| 2977 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2978 | mch_free_acl(vim_acl_T aclent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2979 | { |
| 2980 | if (aclent == NULL) |
| 2981 | return; |
| 2982 | #ifdef HAVE_POSIX_ACL |
| 2983 | acl_free((acl_t)aclent); |
| 2984 | #else |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2985 | #ifdef HAVE_SOLARIS_ZFS_ACL |
| 2986 | acl_free((acl_t *)aclent); |
| 2987 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2988 | #ifdef HAVE_SOLARIS_ACL |
| 2989 | free(((vim_acl_solaris_T *)aclent)->acl_entry); |
| 2990 | free(aclent); |
| 2991 | #else |
| 2992 | #ifdef HAVE_AIX_ACL |
| 2993 | free(aclent); |
| 2994 | #endif /* HAVE_AIX_ACL */ |
| 2995 | #endif /* HAVE_SOLARIS_ACL */ |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2996 | #endif /* HAVE_SOLARIS_ZFS_ACL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2997 | #endif /* HAVE_POSIX_ACL */ |
| 2998 | } |
| 2999 | #endif |
| 3000 | |
| 3001 | /* |
| 3002 | * Set hidden flag for "name". |
| 3003 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3004 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3005 | mch_hide(char_u *name UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3006 | { |
| 3007 | /* can't hide a file */ |
| 3008 | } |
| 3009 | |
| 3010 | /* |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3011 | * return TRUE if "name" is a directory or a symlink to a directory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3012 | * return FALSE if "name" is not a directory |
| 3013 | * return FALSE for error |
| 3014 | */ |
| 3015 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3016 | mch_isdir(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3017 | { |
| 3018 | struct stat statb; |
| 3019 | |
| 3020 | if (*name == NUL) /* Some stat()s don't flag "" as an error. */ |
| 3021 | return FALSE; |
| 3022 | if (stat((char *)name, &statb)) |
| 3023 | return FALSE; |
| 3024 | #ifdef _POSIX_SOURCE |
| 3025 | return (S_ISDIR(statb.st_mode) ? TRUE : FALSE); |
| 3026 | #else |
| 3027 | return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE); |
| 3028 | #endif |
| 3029 | } |
| 3030 | |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3031 | /* |
| 3032 | * return TRUE if "name" is a directory, NOT a symlink to a directory |
| 3033 | * return FALSE if "name" is not a directory |
| 3034 | * return FALSE for error |
| 3035 | */ |
| 3036 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3037 | mch_isrealdir(char_u *name) |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3038 | { |
| 3039 | struct stat statb; |
| 3040 | |
| 3041 | if (*name == NUL) /* Some stat()s don't flag "" as an error. */ |
| 3042 | return FALSE; |
Bram Moolenaar | de5e2c2 | 2016-11-04 20:35:31 +0100 | [diff] [blame] | 3043 | if (mch_lstat((char *)name, &statb)) |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3044 | return FALSE; |
| 3045 | #ifdef _POSIX_SOURCE |
| 3046 | return (S_ISDIR(statb.st_mode) ? TRUE : FALSE); |
| 3047 | #else |
| 3048 | return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE); |
| 3049 | #endif |
| 3050 | } |
| 3051 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 3052 | static int executable_file(char_u *name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3053 | |
| 3054 | /* |
| 3055 | * Return 1 if "name" is an executable file, 0 if not or it doesn't exist. |
| 3056 | */ |
| 3057 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3058 | executable_file(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3059 | { |
| 3060 | struct stat st; |
| 3061 | |
| 3062 | if (stat((char *)name, &st)) |
| 3063 | return 0; |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 3064 | #ifdef VMS |
| 3065 | /* Like on Unix system file can have executable rights but not necessarily |
| 3066 | * be an executable, but on Unix is not a default for an ordianry file to |
| 3067 | * have an executable flag - on VMS it is in most cases. |
| 3068 | * Therefore, this check does not have any sense - let keep us to the |
| 3069 | * conventions instead: |
| 3070 | * *.COM and *.EXE files are the executables - the rest are not. This is |
| 3071 | * not ideal but better then it was. |
| 3072 | */ |
| 3073 | int vms_executable = 0; |
| 3074 | if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0) |
| 3075 | { |
| 3076 | if (strstr(vms_tolower((char*)name),".exe") != NULL |
| 3077 | || strstr(vms_tolower((char*)name),".com")!= NULL) |
| 3078 | vms_executable = 1; |
| 3079 | } |
| 3080 | return vms_executable; |
| 3081 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3082 | 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] | 3083 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | /* |
Bram Moolenaar | 2fcf668 | 2017-03-11 20:03:42 +0100 | [diff] [blame] | 3087 | * Return TRUE if "name" can be found in $PATH and executed, FALSE if not. |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 3088 | * If "use_path" is FALSE only check if "name" is executable. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3089 | * Return -1 if unknown. |
| 3090 | */ |
| 3091 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3092 | mch_can_exe(char_u *name, char_u **path, int use_path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3093 | { |
| 3094 | char_u *buf; |
| 3095 | char_u *p, *e; |
| 3096 | int retval; |
| 3097 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 3098 | /* When "use_path" is false and if it's an absolute or relative path don't |
| 3099 | * need to use $PATH. */ |
| 3100 | if (!use_path || mch_isFullName(name) || (name[0] == '.' |
| 3101 | && (name[1] == '/' || (name[1] == '.' && name[2] == '/')))) |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 3102 | { |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 3103 | /* There must be a path separator, files in the current directory |
| 3104 | * can't be executed. */ |
| 3105 | if (gettail(name) != name && executable_file(name)) |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3106 | { |
| 3107 | if (path != NULL) |
| 3108 | { |
Bram Moolenaar | 4366319 | 2017-03-05 14:29:12 +0100 | [diff] [blame] | 3109 | if (name[0] != '/') |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3110 | *path = FullName_save(name, TRUE); |
| 3111 | else |
| 3112 | *path = vim_strsave(name); |
| 3113 | } |
| 3114 | return TRUE; |
| 3115 | } |
| 3116 | return FALSE; |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 3117 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3118 | |
| 3119 | p = (char_u *)getenv("PATH"); |
| 3120 | if (p == NULL || *p == NUL) |
| 3121 | return -1; |
| 3122 | buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2)); |
| 3123 | if (buf == NULL) |
| 3124 | return -1; |
| 3125 | |
| 3126 | /* |
| 3127 | * Walk through all entries in $PATH to check if "name" exists there and |
| 3128 | * is an executable file. |
| 3129 | */ |
| 3130 | for (;;) |
| 3131 | { |
| 3132 | e = (char_u *)strchr((char *)p, ':'); |
| 3133 | if (e == NULL) |
| 3134 | e = p + STRLEN(p); |
| 3135 | if (e - p <= 1) /* empty entry means current dir */ |
| 3136 | STRCPY(buf, "./"); |
| 3137 | else |
| 3138 | { |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 3139 | vim_strncpy(buf, p, e - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3140 | add_pathsep(buf); |
| 3141 | } |
| 3142 | STRCAT(buf, name); |
| 3143 | retval = executable_file(buf); |
| 3144 | if (retval == 1) |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3145 | { |
| 3146 | if (path != NULL) |
| 3147 | { |
Bram Moolenaar | 4366319 | 2017-03-05 14:29:12 +0100 | [diff] [blame] | 3148 | if (buf[0] != '/') |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3149 | *path = FullName_save(buf, TRUE); |
| 3150 | else |
| 3151 | *path = vim_strsave(buf); |
| 3152 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3153 | break; |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3154 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3155 | |
| 3156 | if (*e != ':') |
| 3157 | break; |
| 3158 | p = e + 1; |
| 3159 | } |
| 3160 | |
| 3161 | vim_free(buf); |
| 3162 | return retval; |
| 3163 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3164 | |
| 3165 | /* |
| 3166 | * Check what "name" is: |
| 3167 | * NODE_NORMAL: file or directory (or doesn't exist) |
| 3168 | * NODE_WRITABLE: writable device, socket, fifo, etc. |
| 3169 | * NODE_OTHER: non-writable things |
| 3170 | */ |
| 3171 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3172 | mch_nodetype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3173 | { |
| 3174 | struct stat st; |
| 3175 | |
| 3176 | if (stat((char *)name, &st)) |
| 3177 | return NODE_NORMAL; |
| 3178 | if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) |
| 3179 | return NODE_NORMAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3180 | if (S_ISBLK(st.st_mode)) /* block device isn't writable */ |
| 3181 | return NODE_OTHER; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3182 | /* Everything else is writable? */ |
| 3183 | return NODE_WRITABLE; |
| 3184 | } |
| 3185 | |
| 3186 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3187 | mch_early_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3188 | { |
| 3189 | #ifdef HAVE_CHECK_STACK_GROWTH |
| 3190 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3191 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3192 | check_stack_growth((char *)&i); |
| 3193 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3194 | # ifdef HAVE_STACK_LIMIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3195 | get_stack_limit(); |
| 3196 | # endif |
| 3197 | |
| 3198 | #endif |
| 3199 | |
| 3200 | /* |
| 3201 | * Setup an alternative stack for signals. Helps to catch signals when |
| 3202 | * running out of stack space. |
| 3203 | * Use of sigaltstack() is preferred, it's more portable. |
| 3204 | * Ignore any errors. |
| 3205 | */ |
| 3206 | #if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
Bram Moolenaar | 5a22181 | 2008-11-12 12:08:45 +0000 | [diff] [blame] | 3207 | signal_stack = (char *)alloc(SIGSTKSZ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3208 | init_signal_stack(); |
| 3209 | #endif |
| 3210 | } |
| 3211 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3212 | #if defined(EXITFREE) || defined(PROTO) |
| 3213 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3214 | mch_free_mem(void) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3215 | { |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3216 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
| 3217 | if (clip_star.owned) |
| 3218 | clip_lose_selection(&clip_star); |
| 3219 | if (clip_plus.owned) |
| 3220 | clip_lose_selection(&clip_plus); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3221 | # endif |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3222 | # if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3223 | if (xterm_Shell != (Widget)0) |
| 3224 | XtDestroyWidget(xterm_Shell); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3225 | # ifndef LESSTIF_VERSION |
| 3226 | /* Lesstif crashes here, lose some memory */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3227 | if (xterm_dpy != NULL) |
| 3228 | XtCloseDisplay(xterm_dpy); |
| 3229 | if (app_context != (XtAppContext)NULL) |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3230 | { |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3231 | XtDestroyApplicationContext(app_context); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3232 | # ifdef FEAT_X11 |
| 3233 | x11_display = NULL; /* freed by XtDestroyApplicationContext() */ |
| 3234 | # endif |
| 3235 | } |
| 3236 | # endif |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3237 | # endif |
Bram Moolenaar | 0eda7ac | 2010-06-26 05:38:18 +0200 | [diff] [blame] | 3238 | # if defined(FEAT_X11) |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3239 | if (x11_display != NULL |
| 3240 | # ifdef FEAT_XCLIPBOARD |
| 3241 | && x11_display != xterm_dpy |
| 3242 | # endif |
| 3243 | ) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3244 | XCloseDisplay(x11_display); |
| 3245 | # endif |
| 3246 | # if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
| 3247 | vim_free(signal_stack); |
| 3248 | signal_stack = NULL; |
| 3249 | # endif |
| 3250 | # ifdef FEAT_TITLE |
| 3251 | vim_free(oldtitle); |
| 3252 | vim_free(oldicon); |
| 3253 | # endif |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3254 | } |
| 3255 | #endif |
| 3256 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 3257 | static void exit_scroll(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3258 | |
| 3259 | /* |
| 3260 | * Output a newline when exiting. |
| 3261 | * Make sure the newline goes to the same stream as the text. |
| 3262 | */ |
| 3263 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3264 | exit_scroll(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3265 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3266 | if (silent_mode) |
| 3267 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3268 | if (newline_on_exit || msg_didout) |
| 3269 | { |
| 3270 | if (msg_use_printf()) |
| 3271 | { |
| 3272 | if (info_message) |
| 3273 | mch_msg("\n"); |
| 3274 | else |
| 3275 | mch_errmsg("\r\n"); |
| 3276 | } |
| 3277 | else |
| 3278 | out_char('\n'); |
| 3279 | } |
| 3280 | else |
| 3281 | { |
| 3282 | restore_cterm_colors(); /* get original colors back */ |
| 3283 | msg_clr_eos_force(); /* clear the rest of the display */ |
| 3284 | windgoto((int)Rows - 1, 0); /* may have moved the cursor */ |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3289 | mch_exit(int r) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3290 | { |
| 3291 | exiting = TRUE; |
| 3292 | |
| 3293 | #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD) |
| 3294 | x11_export_final_selection(); |
| 3295 | #endif |
| 3296 | |
| 3297 | #ifdef FEAT_GUI |
| 3298 | if (!gui.in_use) |
| 3299 | #endif |
| 3300 | { |
| 3301 | settmode(TMODE_COOK); |
| 3302 | #ifdef FEAT_TITLE |
| 3303 | mch_restore_title(3); /* restore xterm title and icon name */ |
| 3304 | #endif |
| 3305 | /* |
| 3306 | * When t_ti is not empty but it doesn't cause swapping terminal |
| 3307 | * pages, need to output a newline when msg_didout is set. But when |
| 3308 | * t_ti does swap pages it should not go to the shell page. Do this |
| 3309 | * before stoptermcap(). |
| 3310 | */ |
| 3311 | if (swapping_screen() && !newline_on_exit) |
| 3312 | exit_scroll(); |
| 3313 | |
| 3314 | /* Stop termcap: May need to check for T_CRV response, which |
| 3315 | * requires RAW mode. */ |
| 3316 | stoptermcap(); |
| 3317 | |
| 3318 | /* |
| 3319 | * A newline is only required after a message in the alternate screen. |
| 3320 | * This is set to TRUE by wait_return(). |
| 3321 | */ |
| 3322 | if (!swapping_screen() || newline_on_exit) |
| 3323 | exit_scroll(); |
| 3324 | |
| 3325 | /* Cursor may have been switched off without calling starttermcap() |
| 3326 | * when doing "vim -u vimrc" and vimrc contains ":q". */ |
| 3327 | if (full_screen) |
| 3328 | cursor_on(); |
| 3329 | } |
| 3330 | out_flush(); |
| 3331 | ml_close_all(TRUE); /* remove all memfiles */ |
| 3332 | may_core_dump(); |
| 3333 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3334 | if (gui.in_use) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3335 | gui_exit(r); |
| 3336 | #endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3337 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 3338 | #ifdef MACOS_CONVERT |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3339 | mac_conv_cleanup(); |
| 3340 | #endif |
| 3341 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3342 | #ifdef __QNX__ |
| 3343 | /* A core dump won't be created if the signal handler |
| 3344 | * doesn't return, so we can't call exit() */ |
| 3345 | if (deadly_signal != 0) |
| 3346 | return; |
| 3347 | #endif |
| 3348 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3349 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 3350 | netbeans_send_disconnect(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3351 | #endif |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3352 | |
| 3353 | #ifdef EXITFREE |
| 3354 | free_all_mem(); |
| 3355 | #endif |
| 3356 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3357 | exit(r); |
| 3358 | } |
| 3359 | |
| 3360 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3361 | may_core_dump(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3362 | { |
| 3363 | if (deadly_signal != 0) |
| 3364 | { |
| 3365 | signal(deadly_signal, SIG_DFL); |
| 3366 | kill(getpid(), deadly_signal); /* Die using the signal we caught */ |
| 3367 | } |
| 3368 | } |
| 3369 | |
| 3370 | #ifndef VMS |
| 3371 | |
| 3372 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3373 | mch_settmode(int tmode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3374 | { |
| 3375 | static int first = TRUE; |
| 3376 | |
| 3377 | /* Why is NeXT excluded here (and not in os_unixx.h)? */ |
| 3378 | #if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__) |
| 3379 | /* |
| 3380 | * for "new" tty systems |
| 3381 | */ |
| 3382 | # ifdef HAVE_TERMIOS_H |
| 3383 | static struct termios told; |
| 3384 | struct termios tnew; |
| 3385 | # else |
| 3386 | static struct termio told; |
| 3387 | struct termio tnew; |
| 3388 | # endif |
| 3389 | |
| 3390 | if (first) |
| 3391 | { |
| 3392 | first = FALSE; |
| 3393 | # if defined(HAVE_TERMIOS_H) |
| 3394 | tcgetattr(read_cmd_fd, &told); |
| 3395 | # else |
| 3396 | ioctl(read_cmd_fd, TCGETA, &told); |
| 3397 | # endif |
| 3398 | } |
| 3399 | |
| 3400 | tnew = told; |
| 3401 | if (tmode == TMODE_RAW) |
| 3402 | { |
| 3403 | /* |
| 3404 | * ~ICRNL enables typing ^V^M |
| 3405 | */ |
| 3406 | tnew.c_iflag &= ~ICRNL; |
| 3407 | tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE |
| 3408 | # if defined(IEXTEN) && !defined(__MINT__) |
| 3409 | | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */ |
| 3410 | /* but it breaks function keys on MINT */ |
| 3411 | # endif |
| 3412 | ); |
| 3413 | # ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */ |
| 3414 | tnew.c_oflag &= ~ONLCR; |
| 3415 | # endif |
| 3416 | tnew.c_cc[VMIN] = 1; /* return after 1 char */ |
| 3417 | tnew.c_cc[VTIME] = 0; /* don't wait */ |
| 3418 | } |
| 3419 | else if (tmode == TMODE_SLEEP) |
Bram Moolenaar | 40de456 | 2016-07-01 15:03:46 +0200 | [diff] [blame] | 3420 | { |
| 3421 | /* Also reset ICANON here, otherwise on Solaris select() won't see |
| 3422 | * typeahead characters. */ |
| 3423 | tnew.c_lflag &= ~(ICANON | ECHO); |
| 3424 | tnew.c_cc[VMIN] = 1; /* return after 1 char */ |
| 3425 | tnew.c_cc[VTIME] = 0; /* don't wait */ |
| 3426 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3427 | |
| 3428 | # if defined(HAVE_TERMIOS_H) |
| 3429 | { |
| 3430 | int n = 10; |
| 3431 | |
| 3432 | /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a |
| 3433 | * few times. */ |
| 3434 | while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1 |
| 3435 | && errno == EINTR && n > 0) |
| 3436 | --n; |
| 3437 | } |
| 3438 | # else |
| 3439 | ioctl(read_cmd_fd, TCSETA, &tnew); |
| 3440 | # endif |
| 3441 | |
| 3442 | #else |
| 3443 | |
| 3444 | /* |
| 3445 | * for "old" tty systems |
| 3446 | */ |
| 3447 | # ifndef TIOCSETN |
| 3448 | # define TIOCSETN TIOCSETP /* for hpux 9.0 */ |
| 3449 | # endif |
| 3450 | static struct sgttyb ttybold; |
| 3451 | struct sgttyb ttybnew; |
| 3452 | |
| 3453 | if (first) |
| 3454 | { |
| 3455 | first = FALSE; |
| 3456 | ioctl(read_cmd_fd, TIOCGETP, &ttybold); |
| 3457 | } |
| 3458 | |
| 3459 | ttybnew = ttybold; |
| 3460 | if (tmode == TMODE_RAW) |
| 3461 | { |
| 3462 | ttybnew.sg_flags &= ~(CRMOD | ECHO); |
| 3463 | ttybnew.sg_flags |= RAW; |
| 3464 | } |
| 3465 | else if (tmode == TMODE_SLEEP) |
| 3466 | ttybnew.sg_flags &= ~(ECHO); |
| 3467 | ioctl(read_cmd_fd, TIOCSETN, &ttybnew); |
| 3468 | #endif |
| 3469 | curr_tmode = tmode; |
| 3470 | } |
| 3471 | |
| 3472 | /* |
| 3473 | * Try to get the code for "t_kb" from the stty setting |
| 3474 | * |
| 3475 | * Even if termcap claims a backspace key, the user's setting *should* |
| 3476 | * prevail. stty knows more about reality than termcap does, and if |
| 3477 | * somebody's usual erase key is DEL (which, for most BSD users, it will |
| 3478 | * be), they're going to get really annoyed if their erase key starts |
| 3479 | * doing forward deletes for no reason. (Eric Fischer) |
| 3480 | */ |
| 3481 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3482 | get_stty(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3483 | { |
| 3484 | char_u buf[2]; |
| 3485 | char_u *p; |
| 3486 | |
| 3487 | /* Why is NeXT excluded here (and not in os_unixx.h)? */ |
| 3488 | #if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__) |
| 3489 | /* for "new" tty systems */ |
| 3490 | # ifdef HAVE_TERMIOS_H |
| 3491 | struct termios keys; |
| 3492 | # else |
| 3493 | struct termio keys; |
| 3494 | # endif |
| 3495 | |
| 3496 | # if defined(HAVE_TERMIOS_H) |
| 3497 | if (tcgetattr(read_cmd_fd, &keys) != -1) |
| 3498 | # else |
| 3499 | if (ioctl(read_cmd_fd, TCGETA, &keys) != -1) |
| 3500 | # endif |
| 3501 | { |
| 3502 | buf[0] = keys.c_cc[VERASE]; |
| 3503 | intr_char = keys.c_cc[VINTR]; |
| 3504 | #else |
| 3505 | /* for "old" tty systems */ |
| 3506 | struct sgttyb keys; |
| 3507 | |
| 3508 | if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1) |
| 3509 | { |
| 3510 | buf[0] = keys.sg_erase; |
| 3511 | intr_char = keys.sg_kill; |
| 3512 | #endif |
| 3513 | buf[1] = NUL; |
| 3514 | add_termcode((char_u *)"kb", buf, FALSE); |
| 3515 | |
| 3516 | /* |
| 3517 | * If <BS> and <DEL> are now the same, redefine <DEL>. |
| 3518 | */ |
| 3519 | p = find_termcode((char_u *)"kD"); |
| 3520 | if (p != NULL && p[0] == buf[0] && p[1] == buf[1]) |
| 3521 | do_fixdel(NULL); |
| 3522 | } |
| 3523 | #if 0 |
| 3524 | } /* to keep cindent happy */ |
| 3525 | #endif |
| 3526 | } |
| 3527 | |
| 3528 | #endif /* VMS */ |
| 3529 | |
| 3530 | #if defined(FEAT_MOUSE_TTY) || defined(PROTO) |
| 3531 | /* |
| 3532 | * Set mouse clicks on or off. |
| 3533 | */ |
| 3534 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3535 | mch_setmouse(int on) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3536 | { |
| 3537 | static int ison = FALSE; |
| 3538 | int xterm_mouse_vers; |
| 3539 | |
| 3540 | if (on == ison) /* return quickly if nothing to do */ |
| 3541 | return; |
| 3542 | |
| 3543 | xterm_mouse_vers = use_xterm_mouse(); |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3544 | |
| 3545 | # ifdef FEAT_MOUSE_URXVT |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3546 | if (ttym_flags == TTYM_URXVT) |
| 3547 | { |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3548 | out_str_nf((char_u *) |
| 3549 | (on |
| 3550 | ? IF_EB("\033[?1015h", ESC_STR "[?1015h") |
| 3551 | : IF_EB("\033[?1015l", ESC_STR "[?1015l"))); |
| 3552 | ison = on; |
| 3553 | } |
| 3554 | # endif |
| 3555 | |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3556 | # ifdef FEAT_MOUSE_SGR |
| 3557 | if (ttym_flags == TTYM_SGR) |
| 3558 | { |
| 3559 | out_str_nf((char_u *) |
| 3560 | (on |
| 3561 | ? IF_EB("\033[?1006h", ESC_STR "[?1006h") |
| 3562 | : IF_EB("\033[?1006l", ESC_STR "[?1006l"))); |
| 3563 | ison = on; |
| 3564 | } |
| 3565 | # endif |
| 3566 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3567 | if (xterm_mouse_vers > 0) |
| 3568 | { |
| 3569 | if (on) /* enable mouse events, use mouse tracking if available */ |
| 3570 | out_str_nf((char_u *) |
| 3571 | (xterm_mouse_vers > 1 |
| 3572 | ? IF_EB("\033[?1002h", ESC_STR "[?1002h") |
| 3573 | : IF_EB("\033[?1000h", ESC_STR "[?1000h"))); |
| 3574 | else /* disable mouse events, could probably always send the same */ |
| 3575 | out_str_nf((char_u *) |
| 3576 | (xterm_mouse_vers > 1 |
| 3577 | ? IF_EB("\033[?1002l", ESC_STR "[?1002l") |
| 3578 | : IF_EB("\033[?1000l", ESC_STR "[?1000l"))); |
| 3579 | ison = on; |
| 3580 | } |
| 3581 | |
| 3582 | # ifdef FEAT_MOUSE_DEC |
| 3583 | else if (ttym_flags == TTYM_DEC) |
| 3584 | { |
| 3585 | if (on) /* enable mouse events */ |
| 3586 | out_str_nf((char_u *)"\033[1;2'z\033[1;3'{"); |
| 3587 | else /* disable mouse events */ |
| 3588 | out_str_nf((char_u *)"\033['z"); |
| 3589 | ison = on; |
| 3590 | } |
| 3591 | # endif |
| 3592 | |
| 3593 | # ifdef FEAT_MOUSE_GPM |
| 3594 | else |
| 3595 | { |
| 3596 | if (on) |
| 3597 | { |
| 3598 | if (gpm_open()) |
| 3599 | ison = TRUE; |
| 3600 | } |
| 3601 | else |
| 3602 | { |
| 3603 | gpm_close(); |
| 3604 | ison = FALSE; |
| 3605 | } |
| 3606 | } |
| 3607 | # endif |
| 3608 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 3609 | # ifdef FEAT_SYSMOUSE |
| 3610 | else |
| 3611 | { |
| 3612 | if (on) |
| 3613 | { |
| 3614 | if (sysmouse_open() == OK) |
| 3615 | ison = TRUE; |
| 3616 | } |
| 3617 | else |
| 3618 | { |
| 3619 | sysmouse_close(); |
| 3620 | ison = FALSE; |
| 3621 | } |
| 3622 | } |
| 3623 | # endif |
| 3624 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3625 | # ifdef FEAT_MOUSE_JSB |
| 3626 | else |
| 3627 | { |
| 3628 | if (on) |
| 3629 | { |
| 3630 | /* D - Enable Mouse up/down messages |
| 3631 | * L - Enable Left Button Reporting |
| 3632 | * M - Enable Middle Button Reporting |
| 3633 | * R - Enable Right Button Reporting |
| 3634 | * K - Enable SHIFT and CTRL key Reporting |
| 3635 | * + - Enable Advanced messaging of mouse moves and up/down messages |
| 3636 | * Q - Quiet No Ack |
| 3637 | * # - Numeric value of mouse pointer required |
| 3638 | * 0 = Multiview 2000 cursor, used as standard |
| 3639 | * 1 = Windows Arrow |
| 3640 | * 2 = Windows I Beam |
| 3641 | * 3 = Windows Hour Glass |
| 3642 | * 4 = Windows Cross Hair |
| 3643 | * 5 = Windows UP Arrow |
| 3644 | */ |
Bram Moolenaar | f8de161 | 2013-04-15 15:32:25 +0200 | [diff] [blame] | 3645 | # ifdef JSBTERM_MOUSE_NONADVANCED |
| 3646 | /* Disables full feedback of pointer movements */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3647 | out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\", |
| 3648 | ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\")); |
Bram Moolenaar | f8de161 | 2013-04-15 15:32:25 +0200 | [diff] [blame] | 3649 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3650 | out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\", |
| 3651 | ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\")); |
Bram Moolenaar | f8de161 | 2013-04-15 15:32:25 +0200 | [diff] [blame] | 3652 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3653 | ison = TRUE; |
| 3654 | } |
| 3655 | else |
| 3656 | { |
| 3657 | out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\", |
| 3658 | ESC_STR "[0~ZwQ" ESC_STR "\\")); |
| 3659 | ison = FALSE; |
| 3660 | } |
| 3661 | } |
| 3662 | # endif |
| 3663 | # ifdef FEAT_MOUSE_PTERM |
| 3664 | else |
| 3665 | { |
| 3666 | /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */ |
| 3667 | if (on) |
| 3668 | out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l"); |
| 3669 | else |
| 3670 | out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h"); |
| 3671 | ison = on; |
| 3672 | } |
| 3673 | # endif |
| 3674 | } |
| 3675 | |
| 3676 | /* |
| 3677 | * Set the mouse termcode, depending on the 'term' and 'ttymouse' options. |
| 3678 | */ |
| 3679 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3680 | check_mouse_termcode(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3681 | { |
| 3682 | # ifdef FEAT_MOUSE_XTERM |
| 3683 | if (use_xterm_mouse() |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3684 | # ifdef FEAT_MOUSE_URXVT |
| 3685 | && use_xterm_mouse() != 3 |
| 3686 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3687 | # ifdef FEAT_GUI |
| 3688 | && !gui.in_use |
| 3689 | # endif |
| 3690 | ) |
| 3691 | { |
| 3692 | set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3693 | ? IF_EB("\233M", CSI_STR "M") |
| 3694 | : IF_EB("\033[M", ESC_STR "[M"))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3695 | if (*p_mouse != NUL) |
| 3696 | { |
| 3697 | /* force mouse off and maybe on to send possibly new mouse |
| 3698 | * activation sequence to the xterm, with(out) drag tracing. */ |
| 3699 | mch_setmouse(FALSE); |
| 3700 | setmouse(); |
| 3701 | } |
| 3702 | } |
| 3703 | else |
| 3704 | del_mouse_termcode(KS_MOUSE); |
| 3705 | # endif |
| 3706 | |
| 3707 | # ifdef FEAT_MOUSE_GPM |
| 3708 | if (!use_xterm_mouse() |
| 3709 | # ifdef FEAT_GUI |
| 3710 | && !gui.in_use |
| 3711 | # endif |
| 3712 | ) |
| 3713 | set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG")); |
| 3714 | # endif |
| 3715 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 3716 | # ifdef FEAT_SYSMOUSE |
| 3717 | if (!use_xterm_mouse() |
| 3718 | # ifdef FEAT_GUI |
| 3719 | && !gui.in_use |
| 3720 | # endif |
| 3721 | ) |
| 3722 | set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS")); |
| 3723 | # endif |
| 3724 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3725 | # ifdef FEAT_MOUSE_JSB |
Bram Moolenaar | 4e067c8 | 2014-07-30 17:21:58 +0200 | [diff] [blame] | 3726 | /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3727 | if (!use_xterm_mouse() |
| 3728 | # ifdef FEAT_GUI |
| 3729 | && !gui.in_use |
| 3730 | # endif |
| 3731 | ) |
| 3732 | set_mouse_termcode(KS_JSBTERM_MOUSE, |
| 3733 | (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw")); |
| 3734 | else |
| 3735 | del_mouse_termcode(KS_JSBTERM_MOUSE); |
| 3736 | # endif |
| 3737 | |
| 3738 | # ifdef FEAT_MOUSE_NET |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3739 | /* 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] | 3740 | * define it in the GUI or when using an xterm. */ |
| 3741 | if (!use_xterm_mouse() |
| 3742 | # ifdef FEAT_GUI |
| 3743 | && !gui.in_use |
| 3744 | # endif |
| 3745 | ) |
| 3746 | set_mouse_termcode(KS_NETTERM_MOUSE, |
| 3747 | (char_u *)IF_EB("\033}", ESC_STR "}")); |
| 3748 | else |
| 3749 | del_mouse_termcode(KS_NETTERM_MOUSE); |
| 3750 | # endif |
| 3751 | |
| 3752 | # ifdef FEAT_MOUSE_DEC |
Bram Moolenaar | 4e067c8 | 2014-07-30 17:21:58 +0200 | [diff] [blame] | 3753 | /* Conflicts with xterm mouse: "\033[" and "\033[M" */ |
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 | ) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3759 | set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
| 3760 | ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "["))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3761 | else |
| 3762 | del_mouse_termcode(KS_DEC_MOUSE); |
| 3763 | # endif |
| 3764 | # ifdef FEAT_MOUSE_PTERM |
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() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3767 | # ifdef FEAT_GUI |
| 3768 | && !gui.in_use |
| 3769 | # endif |
| 3770 | ) |
| 3771 | set_mouse_termcode(KS_PTERM_MOUSE, |
| 3772 | (char_u *) IF_EB("\033[", ESC_STR "[")); |
| 3773 | else |
| 3774 | del_mouse_termcode(KS_PTERM_MOUSE); |
| 3775 | # endif |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3776 | # ifdef FEAT_MOUSE_URXVT |
Bram Moolenaar | cbc17d6 | 2014-05-22 21:22:19 +0200 | [diff] [blame] | 3777 | if (use_xterm_mouse() == 3 |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3778 | # ifdef FEAT_GUI |
| 3779 | && !gui.in_use |
| 3780 | # endif |
| 3781 | ) |
| 3782 | { |
| 3783 | set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
Bram Moolenaar | a529ce0 | 2017-06-22 22:37:57 +0200 | [diff] [blame] | 3784 | ? IF_EB("\233*M", CSI_STR "*M") |
| 3785 | : IF_EB("\033[*M", ESC_STR "[*M"))); |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3786 | |
| 3787 | if (*p_mouse != NUL) |
| 3788 | { |
| 3789 | mch_setmouse(FALSE); |
| 3790 | setmouse(); |
| 3791 | } |
| 3792 | } |
| 3793 | else |
| 3794 | del_mouse_termcode(KS_URXVT_MOUSE); |
| 3795 | # endif |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3796 | # ifdef FEAT_MOUSE_SGR |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3797 | if (use_xterm_mouse() == 4 |
| 3798 | # ifdef FEAT_GUI |
| 3799 | && !gui.in_use |
| 3800 | # endif |
| 3801 | ) |
| 3802 | { |
| 3803 | set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
Bram Moolenaar | a529ce0 | 2017-06-22 22:37:57 +0200 | [diff] [blame] | 3804 | ? IF_EB("\233<*M", CSI_STR "<*M") |
| 3805 | : IF_EB("\033[<*M", ESC_STR "[<*M"))); |
| 3806 | |
| 3807 | set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME) |
| 3808 | ? IF_EB("\233<*m", CSI_STR "<*m") |
| 3809 | : IF_EB("\033[<*m", ESC_STR "[<*m"))); |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3810 | |
| 3811 | if (*p_mouse != NUL) |
| 3812 | { |
| 3813 | mch_setmouse(FALSE); |
| 3814 | setmouse(); |
| 3815 | } |
| 3816 | } |
| 3817 | else |
Bram Moolenaar | a529ce0 | 2017-06-22 22:37:57 +0200 | [diff] [blame] | 3818 | { |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3819 | del_mouse_termcode(KS_SGR_MOUSE); |
Bram Moolenaar | a529ce0 | 2017-06-22 22:37:57 +0200 | [diff] [blame] | 3820 | del_mouse_termcode(KS_SGR_MOUSE_RELEASE); |
| 3821 | } |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3822 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3823 | } |
| 3824 | #endif |
| 3825 | |
| 3826 | /* |
| 3827 | * set screen mode, always fails. |
| 3828 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3829 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3830 | mch_screenmode(char_u *arg UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3831 | { |
| 3832 | EMSG(_(e_screenmode)); |
| 3833 | return FAIL; |
| 3834 | } |
| 3835 | |
| 3836 | #ifndef VMS |
| 3837 | |
| 3838 | /* |
| 3839 | * Try to get the current window size: |
| 3840 | * 1. with an ioctl(), most accurate method |
| 3841 | * 2. from the environment variables LINES and COLUMNS |
| 3842 | * 3. from the termcap |
| 3843 | * 4. keep using the old values |
| 3844 | * Return OK when size could be determined, FAIL otherwise. |
| 3845 | */ |
| 3846 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3847 | mch_get_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3848 | { |
| 3849 | long rows = 0; |
| 3850 | long columns = 0; |
| 3851 | char_u *p; |
| 3852 | |
| 3853 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3854 | * 1. try using an ioctl. It is the most accurate method. |
| 3855 | * |
| 3856 | * Try using TIOCGWINSZ first, some systems that have it also define |
| 3857 | * TIOCGSIZE but don't have a struct ttysize. |
| 3858 | */ |
| 3859 | # ifdef TIOCGWINSZ |
| 3860 | { |
| 3861 | struct winsize ws; |
| 3862 | int fd = 1; |
| 3863 | |
| 3864 | /* When stdout is not a tty, use stdin for the ioctl(). */ |
| 3865 | if (!isatty(fd) && isatty(read_cmd_fd)) |
| 3866 | fd = read_cmd_fd; |
| 3867 | if (ioctl(fd, TIOCGWINSZ, &ws) == 0) |
| 3868 | { |
| 3869 | columns = ws.ws_col; |
| 3870 | rows = ws.ws_row; |
| 3871 | } |
| 3872 | } |
| 3873 | # else /* TIOCGWINSZ */ |
| 3874 | # ifdef TIOCGSIZE |
| 3875 | { |
| 3876 | struct ttysize ts; |
| 3877 | int fd = 1; |
| 3878 | |
| 3879 | /* When stdout is not a tty, use stdin for the ioctl(). */ |
| 3880 | if (!isatty(fd) && isatty(read_cmd_fd)) |
| 3881 | fd = read_cmd_fd; |
| 3882 | if (ioctl(fd, TIOCGSIZE, &ts) == 0) |
| 3883 | { |
| 3884 | columns = ts.ts_cols; |
| 3885 | rows = ts.ts_lines; |
| 3886 | } |
| 3887 | } |
| 3888 | # endif /* TIOCGSIZE */ |
| 3889 | # endif /* TIOCGWINSZ */ |
| 3890 | |
| 3891 | /* |
| 3892 | * 2. get size from environment |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 3893 | * When being POSIX compliant ('|' flag in 'cpoptions') this overrules |
| 3894 | * the ioctl() values! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3895 | */ |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 3896 | if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3897 | { |
| 3898 | if ((p = (char_u *)getenv("LINES"))) |
| 3899 | rows = atoi((char *)p); |
| 3900 | if ((p = (char_u *)getenv("COLUMNS"))) |
| 3901 | columns = atoi((char *)p); |
| 3902 | } |
| 3903 | |
| 3904 | #ifdef HAVE_TGETENT |
| 3905 | /* |
| 3906 | * 3. try reading "co" and "li" entries from termcap |
| 3907 | */ |
| 3908 | if (columns == 0 || rows == 0) |
| 3909 | getlinecol(&columns, &rows); |
| 3910 | #endif |
| 3911 | |
| 3912 | /* |
| 3913 | * 4. If everything fails, use the old values |
| 3914 | */ |
| 3915 | if (columns <= 0 || rows <= 0) |
| 3916 | return FAIL; |
| 3917 | |
| 3918 | Rows = rows; |
| 3919 | Columns = columns; |
Bram Moolenaar | e057d40 | 2013-06-30 17:51:51 +0200 | [diff] [blame] | 3920 | limit_screen_size(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3921 | return OK; |
| 3922 | } |
| 3923 | |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 3924 | #if defined(FEAT_TERMINAL) || defined(PROTO) |
| 3925 | /* |
| 3926 | * Report the windows size "rows" and "cols" to tty "fd". |
| 3927 | */ |
| 3928 | int |
| 3929 | mch_report_winsize(int fd, int rows, int cols) |
| 3930 | { |
| 3931 | # ifdef TIOCSWINSZ |
| 3932 | struct winsize ws; |
| 3933 | |
| 3934 | ws.ws_col = cols; |
| 3935 | ws.ws_row = rows; |
| 3936 | ws.ws_xpixel = cols * 5; |
| 3937 | ws.ws_ypixel = rows * 10; |
| 3938 | if (ioctl(fd, TIOCSWINSZ, &ws) == 0) |
| 3939 | { |
| 3940 | ch_log(NULL, "ioctl(TIOCSWINSZ) success"); |
| 3941 | return OK; |
| 3942 | } |
| 3943 | ch_log(NULL, "ioctl(TIOCSWINSZ) failed"); |
| 3944 | # else |
| 3945 | # ifdef TIOCSSIZE |
| 3946 | struct ttysize ts; |
| 3947 | |
| 3948 | ts.ts_cols = cols; |
| 3949 | ts.ts_lines = rows; |
| 3950 | if (ioctl(fd, TIOCSSIZE, &ws) == 0) |
| 3951 | { |
| 3952 | ch_log(NULL, "ioctl(TIOCSSIZE) success"); |
| 3953 | return OK; |
| 3954 | } |
| 3955 | ch_log(NULL, "ioctl(TIOCSSIZE) failed"); |
| 3956 | # endif |
| 3957 | # endif |
| 3958 | return FAIL; |
| 3959 | } |
| 3960 | #endif |
| 3961 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3962 | /* |
| 3963 | * Try to set the window size to Rows and Columns. |
| 3964 | */ |
| 3965 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3966 | mch_set_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3967 | { |
| 3968 | if (*T_CWS) |
| 3969 | { |
| 3970 | /* |
| 3971 | * NOTE: if you get an error here that term_set_winsize() is |
| 3972 | * undefined, check the output of configure. It could probably not |
| 3973 | * find a ncurses, termcap or termlib library. |
| 3974 | */ |
| 3975 | term_set_winsize((int)Rows, (int)Columns); |
| 3976 | out_flush(); |
| 3977 | screen_start(); /* don't know where cursor is now */ |
| 3978 | } |
| 3979 | } |
| 3980 | |
| 3981 | #endif /* VMS */ |
| 3982 | |
| 3983 | /* |
| 3984 | * Rows and/or Columns has changed. |
| 3985 | */ |
| 3986 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3987 | mch_new_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3988 | { |
| 3989 | /* Nothing to do. */ |
| 3990 | } |
| 3991 | |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 3992 | /* |
| 3993 | * Wait for process "child" to end. |
| 3994 | * Return "child" if it exited properly, <= 0 on error. |
| 3995 | */ |
| 3996 | static pid_t |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3997 | wait4pid(pid_t child, waitstatus *status) |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 3998 | { |
| 3999 | pid_t wait_pid = 0; |
Bram Moolenaar | 0abe052 | 2016-08-28 16:53:12 +0200 | [diff] [blame] | 4000 | long delay_msec = 1; |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4001 | |
| 4002 | while (wait_pid != child) |
| 4003 | { |
Bram Moolenaar | 6be120e | 2012-04-20 15:55:16 +0200 | [diff] [blame] | 4004 | /* When compiled with Python threads are probably used, in which case |
| 4005 | * wait() sometimes hangs for no obvious reason. Use waitpid() |
| 4006 | * instead and loop (like the GUI). Also needed for other interfaces, |
| 4007 | * they might call system(). */ |
| 4008 | # ifdef __NeXT__ |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4009 | wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0); |
Bram Moolenaar | 6be120e | 2012-04-20 15:55:16 +0200 | [diff] [blame] | 4010 | # else |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4011 | wait_pid = waitpid(child, status, WNOHANG); |
Bram Moolenaar | 6be120e | 2012-04-20 15:55:16 +0200 | [diff] [blame] | 4012 | # endif |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4013 | if (wait_pid == 0) |
| 4014 | { |
Bram Moolenaar | 0abe052 | 2016-08-28 16:53:12 +0200 | [diff] [blame] | 4015 | /* Wait for 1 to 10 msec before trying again. */ |
| 4016 | mch_delay(delay_msec, TRUE); |
| 4017 | if (++delay_msec > 10) |
| 4018 | delay_msec = 10; |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4019 | continue; |
| 4020 | } |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4021 | if (wait_pid <= 0 |
| 4022 | # ifdef ECHILD |
| 4023 | && errno == ECHILD |
| 4024 | # endif |
| 4025 | ) |
| 4026 | break; |
| 4027 | } |
| 4028 | return wait_pid; |
| 4029 | } |
| 4030 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 4031 | #if defined(FEAT_JOB_CHANNEL) || !defined(USE_SYSTEM) || defined(PROTO) |
Bram Moolenaar | 7280140 | 2016-02-09 11:37:50 +0100 | [diff] [blame] | 4032 | /* |
| 4033 | * Parse "cmd" and put the white-separated parts in "argv". |
| 4034 | * "argv" is an allocated array with "argc" entries. |
| 4035 | * Returns FAIL when out of memory. |
| 4036 | */ |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 4037 | int |
| 4038 | mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc) |
| 4039 | { |
| 4040 | int i; |
| 4041 | char_u *p; |
| 4042 | int inquote; |
| 4043 | |
| 4044 | /* |
| 4045 | * Do this loop twice: |
| 4046 | * 1: find number of arguments |
| 4047 | * 2: separate them and build argv[] |
| 4048 | */ |
| 4049 | for (i = 0; i < 2; ++i) |
| 4050 | { |
Bram Moolenaar | 6231cb8 | 2016-04-26 19:42:42 +0200 | [diff] [blame] | 4051 | p = skipwhite(cmd); |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 4052 | inquote = FALSE; |
| 4053 | *argc = 0; |
| 4054 | for (;;) |
| 4055 | { |
| 4056 | if (i == 1) |
| 4057 | (*argv)[*argc] = (char *)p; |
| 4058 | ++*argc; |
| 4059 | while (*p != NUL && (inquote || (*p != ' ' && *p != TAB))) |
| 4060 | { |
| 4061 | if (*p == '"') |
| 4062 | inquote = !inquote; |
| 4063 | ++p; |
| 4064 | } |
| 4065 | if (*p == NUL) |
| 4066 | break; |
| 4067 | if (i == 1) |
| 4068 | *p++ = NUL; |
| 4069 | p = skipwhite(p); |
| 4070 | } |
| 4071 | if (*argv == NULL) |
| 4072 | { |
| 4073 | if (use_shcf) |
| 4074 | { |
| 4075 | /* Account for possible multiple args in p_shcf. */ |
| 4076 | p = p_shcf; |
| 4077 | for (;;) |
| 4078 | { |
| 4079 | p = skiptowhite(p); |
| 4080 | if (*p == NUL) |
| 4081 | break; |
| 4082 | ++*argc; |
| 4083 | p = skipwhite(p); |
| 4084 | } |
| 4085 | } |
| 4086 | |
| 4087 | *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *))); |
| 4088 | if (*argv == NULL) /* out of memory */ |
| 4089 | return FAIL; |
| 4090 | } |
| 4091 | } |
| 4092 | return OK; |
| 4093 | } |
| 4094 | #endif |
| 4095 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 4096 | #if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL) |
Bram Moolenaar | 7da3460 | 2017-08-01 17:14:21 +0200 | [diff] [blame] | 4097 | /* |
| 4098 | * Set the environment for a child process. |
| 4099 | */ |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4100 | static void |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4101 | set_child_environment(long rows, long columns, char *term) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4102 | { |
| 4103 | # ifdef HAVE_SETENV |
| 4104 | char envbuf[50]; |
| 4105 | # else |
Bram Moolenaar | 5f7e7bd | 2017-07-22 14:08:43 +0200 | [diff] [blame] | 4106 | static char envbuf_Term[30]; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4107 | static char envbuf_Rows[20]; |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4108 | static char envbuf_Lines[20]; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4109 | static char envbuf_Columns[20]; |
Bram Moolenaar | b7a8dfe | 2017-07-22 20:33:05 +0200 | [diff] [blame] | 4110 | static char envbuf_Colors[20]; |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4111 | # ifdef FEAT_CLIENTSERVER |
Bram Moolenaar | 7da3460 | 2017-08-01 17:14:21 +0200 | [diff] [blame] | 4112 | static char envbuf_Servername[60]; |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4113 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4114 | # endif |
Bram Moolenaar | b7a8dfe | 2017-07-22 20:33:05 +0200 | [diff] [blame] | 4115 | long colors = |
| 4116 | # ifdef FEAT_GUI |
| 4117 | gui.in_use ? 256*256*256 : |
| 4118 | # endif |
| 4119 | t_colors; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4120 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4121 | # ifdef HAVE_SETENV |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4122 | setenv("TERM", term, 1); |
| 4123 | sprintf((char *)envbuf, "%ld", rows); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4124 | setenv("ROWS", (char *)envbuf, 1); |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4125 | sprintf((char *)envbuf, "%ld", rows); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4126 | setenv("LINES", (char *)envbuf, 1); |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4127 | sprintf((char *)envbuf, "%ld", columns); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4128 | setenv("COLUMNS", (char *)envbuf, 1); |
Bram Moolenaar | b7a8dfe | 2017-07-22 20:33:05 +0200 | [diff] [blame] | 4129 | sprintf((char *)envbuf, "%ld", colors); |
| 4130 | setenv("COLORS", (char *)envbuf, 1); |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4131 | # ifdef FEAT_CLIENTSERVER |
Bram Moolenaar | 7da3460 | 2017-08-01 17:14:21 +0200 | [diff] [blame] | 4132 | setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1); |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4133 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4134 | # else |
| 4135 | /* |
| 4136 | * Putenv does not copy the string, it has to remain valid. |
| 4137 | * Use a static array to avoid losing allocated memory. |
Bram Moolenaar | 7da3460 | 2017-08-01 17:14:21 +0200 | [diff] [blame] | 4138 | * This won't work well when running multiple children... |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4139 | */ |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4140 | vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term); |
| 4141 | putenv(envbuf_Term); |
| 4142 | vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4143 | putenv(envbuf_Rows); |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4144 | vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows); |
| 4145 | putenv(envbuf_Lines); |
| 4146 | vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns), |
| 4147 | "COLUMNS=%ld", columns); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4148 | putenv(envbuf_Columns); |
Bram Moolenaar | b7a8dfe | 2017-07-22 20:33:05 +0200 | [diff] [blame] | 4149 | vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors); |
| 4150 | putenv(envbuf_Colors); |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4151 | # ifdef FEAT_CLIENTSERVER |
Bram Moolenaar | 7da3460 | 2017-08-01 17:14:21 +0200 | [diff] [blame] | 4152 | vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername), |
| 4153 | "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName); |
| 4154 | putenv(envbuf_Servername); |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4155 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4156 | # endif |
| 4157 | } |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4158 | |
| 4159 | static void |
| 4160 | set_default_child_environment(void) |
| 4161 | { |
| 4162 | set_child_environment(Rows, Columns, "dumb"); |
| 4163 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4164 | #endif |
| 4165 | |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 4166 | #if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL) |
Bram Moolenaar | 979e8c5 | 2017-08-01 15:08:07 +0200 | [diff] [blame] | 4167 | /* |
| 4168 | * Open a PTY, with FD for the master and slave side. |
| 4169 | * When failing "pty_master_fd" and "pty_slave_fd" are -1. |
| 4170 | * When successful both file descriptors are stored. |
| 4171 | */ |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 4172 | static void |
| 4173 | open_pty(int *pty_master_fd, int *pty_slave_fd) |
| 4174 | { |
| 4175 | char *tty_name; |
| 4176 | |
| 4177 | *pty_master_fd = OpenPTY(&tty_name); /* open pty */ |
| 4178 | if (*pty_master_fd >= 0) |
| 4179 | { |
| 4180 | /* Leaving out O_NOCTTY may lead to waitpid() always returning |
| 4181 | * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume |
| 4182 | * adding O_NOCTTY always works when defined. */ |
| 4183 | #ifdef O_NOCTTY |
| 4184 | *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0); |
| 4185 | #else |
| 4186 | *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0); |
| 4187 | #endif |
| 4188 | if (*pty_slave_fd < 0) |
| 4189 | { |
| 4190 | close(*pty_master_fd); |
| 4191 | *pty_master_fd = -1; |
| 4192 | } |
| 4193 | } |
| 4194 | } |
| 4195 | #endif |
| 4196 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4197 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4198 | mch_call_shell( |
| 4199 | char_u *cmd, |
| 4200 | int options) /* SHELL_*, see vim.h */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4201 | { |
| 4202 | #ifdef VMS |
| 4203 | char *ifn = NULL; |
| 4204 | char *ofn = NULL; |
| 4205 | #endif |
| 4206 | int tmode = cur_tmode; |
| 4207 | #ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */ |
Bram Moolenaar | b2b050a | 2016-07-16 21:52:46 +0200 | [diff] [blame] | 4208 | char_u *newcmd; /* only needed for unix */ |
Bram Moolenaar | de5e2c2 | 2016-11-04 20:35:31 +0100 | [diff] [blame] | 4209 | int x; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4210 | |
| 4211 | out_flush(); |
| 4212 | |
| 4213 | if (options & SHELL_COOKED) |
| 4214 | settmode(TMODE_COOK); /* set to normal mode */ |
| 4215 | |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 4216 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 4217 | save_clipboard(); |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 4218 | loose_clipboard(); |
| 4219 | # endif |
| 4220 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4221 | if (cmd == NULL) |
| 4222 | x = system((char *)p_sh); |
| 4223 | else |
| 4224 | { |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 4225 | # ifdef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4226 | if (ofn = strchr((char *)cmd, '>')) |
| 4227 | *ofn++ = '\0'; |
| 4228 | if (ifn = strchr((char *)cmd, '<')) |
| 4229 | { |
| 4230 | char *p; |
| 4231 | |
| 4232 | *ifn++ = '\0'; |
| 4233 | p = strchr(ifn,' '); /* chop off any trailing spaces */ |
| 4234 | if (p) |
| 4235 | *p = '\0'; |
| 4236 | } |
| 4237 | if (ofn) |
| 4238 | x = vms_sys((char *)cmd, ofn, ifn); |
| 4239 | else |
| 4240 | x = system((char *)cmd); |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 4241 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4242 | newcmd = lalloc(STRLEN(p_sh) |
| 4243 | + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg)) |
| 4244 | + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE); |
| 4245 | if (newcmd == NULL) |
| 4246 | x = 0; |
| 4247 | else |
| 4248 | { |
| 4249 | sprintf((char *)newcmd, "%s %s %s %s", p_sh, |
| 4250 | extra_shell_arg == NULL ? "" : (char *)extra_shell_arg, |
| 4251 | (char *)p_shcf, |
| 4252 | (char *)cmd); |
| 4253 | x = system((char *)newcmd); |
| 4254 | vim_free(newcmd); |
| 4255 | } |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 4256 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4257 | } |
| 4258 | # ifdef VMS |
| 4259 | x = vms_sys_status(x); |
| 4260 | # endif |
| 4261 | if (emsg_silent) |
| 4262 | ; |
| 4263 | else if (x == 127) |
| 4264 | MSG_PUTS(_("\nCannot execute shell sh\n")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4265 | else if (x && !(options & SHELL_SILENT)) |
| 4266 | { |
| 4267 | MSG_PUTS(_("\nshell returned ")); |
| 4268 | msg_outnum((long)x); |
| 4269 | msg_putchar('\n'); |
| 4270 | } |
| 4271 | |
| 4272 | if (tmode == TMODE_RAW) |
| 4273 | settmode(TMODE_RAW); /* set to raw mode */ |
| 4274 | # ifdef FEAT_TITLE |
| 4275 | resettitle(); |
| 4276 | # endif |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 4277 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
| 4278 | restore_clipboard(); |
| 4279 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4280 | return x; |
| 4281 | |
| 4282 | #else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */ |
| 4283 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4284 | # define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use |
| 4285 | 127, some shells use that already */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4286 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 4287 | char_u *newcmd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4288 | pid_t pid; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4289 | pid_t wpid = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4290 | pid_t wait_pid = 0; |
| 4291 | # ifdef HAVE_UNION_WAIT |
| 4292 | union wait status; |
| 4293 | # else |
| 4294 | int status = -1; |
| 4295 | # endif |
| 4296 | int retval = -1; |
| 4297 | char **argv = NULL; |
| 4298 | int argc; |
Bram Moolenaar | ea35ef6 | 2011-08-04 22:59:28 +0200 | [diff] [blame] | 4299 | char_u *p_shcf_copy = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4300 | int i; |
| 4301 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4302 | int pty_master_fd = -1; /* for pty's */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4303 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4304 | int pty_slave_fd = -1; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4305 | # endif |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4306 | int fd_toshell[2]; /* for pipes */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4307 | int fd_fromshell[2]; |
| 4308 | int pipe_error = FALSE; |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4309 | int did_settmode = FALSE; /* settmode(TMODE_RAW) called */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4310 | |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 4311 | newcmd = vim_strsave(p_sh); |
| 4312 | if (newcmd == NULL) /* out of memory */ |
| 4313 | goto error; |
| 4314 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4315 | out_flush(); |
| 4316 | if (options & SHELL_COOKED) |
| 4317 | settmode(TMODE_COOK); /* set to normal mode */ |
| 4318 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 4319 | if (mch_parse_cmd(newcmd, TRUE, &argv, &argc) == FAIL) |
| 4320 | goto error; |
Bram Moolenaar | ea35ef6 | 2011-08-04 22:59:28 +0200 | [diff] [blame] | 4321 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4322 | if (cmd != NULL) |
| 4323 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4324 | char_u *s; |
| 4325 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4326 | if (extra_shell_arg != NULL) |
| 4327 | argv[argc++] = (char *)extra_shell_arg; |
Bram Moolenaar | ea35ef6 | 2011-08-04 22:59:28 +0200 | [diff] [blame] | 4328 | |
| 4329 | /* Break 'shellcmdflag' into white separated parts. This doesn't |
| 4330 | * handle quoted strings, they are very unlikely to appear. */ |
| 4331 | p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1); |
| 4332 | if (p_shcf_copy == NULL) /* out of memory */ |
| 4333 | goto error; |
| 4334 | s = p_shcf_copy; |
| 4335 | p = p_shcf; |
| 4336 | while (*p != NUL) |
| 4337 | { |
| 4338 | argv[argc++] = (char *)s; |
| 4339 | while (*p && *p != ' ' && *p != TAB) |
| 4340 | *s++ = *p++; |
| 4341 | *s++ = NUL; |
| 4342 | p = skipwhite(p); |
| 4343 | } |
| 4344 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4345 | argv[argc++] = (char *)cmd; |
| 4346 | } |
| 4347 | argv[argc] = NULL; |
| 4348 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4349 | /* |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4350 | * For the GUI, when writing the output into the buffer and when reading |
| 4351 | * input from the buffer: Try using a pseudo-tty to get the stdin/stdout |
| 4352 | * of the executed command into the Vim window. Or use a pipe. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4353 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4354 | if ((options & (SHELL_READ|SHELL_WRITE)) |
| 4355 | # ifdef FEAT_GUI |
| 4356 | || (gui.in_use && show_shell_mess) |
| 4357 | # endif |
| 4358 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4359 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4360 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4361 | /* |
| 4362 | * Try to open a master pty. |
| 4363 | * If this works, open the slave pty. |
| 4364 | * If the slave can't be opened, close the master pty. |
| 4365 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4366 | if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE))) |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 4367 | open_pty(&pty_master_fd, &pty_slave_fd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4368 | /* |
| 4369 | * If not opening a pty or it didn't work, try using pipes. |
| 4370 | */ |
| 4371 | if (pty_master_fd < 0) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4372 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4373 | { |
| 4374 | pipe_error = (pipe(fd_toshell) < 0); |
| 4375 | if (!pipe_error) /* pipe create OK */ |
| 4376 | { |
| 4377 | pipe_error = (pipe(fd_fromshell) < 0); |
| 4378 | if (pipe_error) /* pipe create failed */ |
| 4379 | { |
| 4380 | close(fd_toshell[0]); |
| 4381 | close(fd_toshell[1]); |
| 4382 | } |
| 4383 | } |
| 4384 | if (pipe_error) |
| 4385 | { |
| 4386 | MSG_PUTS(_("\nCannot create pipes\n")); |
| 4387 | out_flush(); |
| 4388 | } |
| 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | if (!pipe_error) /* pty or pipe opened or not used */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4393 | { |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 4394 | SIGSET_DECL(curset) |
| 4395 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4396 | # ifdef __BEOS__ |
| 4397 | beos_cleanup_read_thread(); |
| 4398 | # endif |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4399 | |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 4400 | BLOCK_SIGNALS(&curset); |
| 4401 | pid = fork(); /* maybe we should use vfork() */ |
| 4402 | if (pid == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4403 | { |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 4404 | UNBLOCK_SIGNALS(&curset); |
| 4405 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4406 | MSG_PUTS(_("\nCannot fork\n")); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4407 | if ((options & (SHELL_READ|SHELL_WRITE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4408 | # ifdef FEAT_GUI |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4409 | || (gui.in_use && show_shell_mess) |
| 4410 | # endif |
| 4411 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4412 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4413 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4414 | if (pty_master_fd >= 0) /* close the pseudo tty */ |
| 4415 | { |
| 4416 | close(pty_master_fd); |
| 4417 | close(pty_slave_fd); |
| 4418 | } |
| 4419 | else /* close the pipes */ |
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 | { |
| 4422 | close(fd_toshell[0]); |
| 4423 | close(fd_toshell[1]); |
| 4424 | close(fd_fromshell[0]); |
| 4425 | close(fd_fromshell[1]); |
| 4426 | } |
| 4427 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4428 | } |
| 4429 | else if (pid == 0) /* child */ |
| 4430 | { |
| 4431 | reset_signals(); /* handle signals normally */ |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 4432 | UNBLOCK_SIGNALS(&curset); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4433 | |
| 4434 | if (!show_shell_mess || (options & SHELL_EXPAND)) |
| 4435 | { |
| 4436 | int fd; |
| 4437 | |
| 4438 | /* |
| 4439 | * Don't want to show any message from the shell. Can't just |
| 4440 | * close stdout and stderr though, because some systems will |
| 4441 | * break if you try to write to them after that, so we must |
| 4442 | * use dup() to replace them with something else -- webb |
| 4443 | * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang, |
| 4444 | * waiting for input. |
| 4445 | */ |
| 4446 | fd = open("/dev/null", O_RDWR | O_EXTRA, 0); |
| 4447 | fclose(stdin); |
| 4448 | fclose(stdout); |
| 4449 | fclose(stderr); |
| 4450 | |
| 4451 | /* |
| 4452 | * If any of these open()'s and dup()'s fail, we just continue |
| 4453 | * anyway. It's not fatal, and on most systems it will make |
| 4454 | * no difference at all. On a few it will cause the execvp() |
| 4455 | * to exit with a non-zero status even when the completion |
| 4456 | * could be done, which is nothing too serious. If the open() |
| 4457 | * or dup() failed we'd just do the same thing ourselves |
| 4458 | * anyway -- webb |
| 4459 | */ |
| 4460 | if (fd >= 0) |
| 4461 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4462 | ignored = dup(fd); /* To replace stdin (fd 0) */ |
| 4463 | ignored = dup(fd); /* To replace stdout (fd 1) */ |
| 4464 | ignored = dup(fd); /* To replace stderr (fd 2) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4465 | |
| 4466 | /* Don't need this now that we've duplicated it */ |
| 4467 | close(fd); |
| 4468 | } |
| 4469 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4470 | else if ((options & (SHELL_READ|SHELL_WRITE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4471 | # ifdef FEAT_GUI |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4472 | || gui.in_use |
| 4473 | # endif |
| 4474 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4475 | { |
| 4476 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4477 | # ifdef HAVE_SETSID |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4478 | /* Create our own process group, so that the child and all its |
| 4479 | * children can be kill()ed. Don't do this when using pipes, |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 4480 | * because stdin is not a tty, we would lose /dev/tty. */ |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4481 | if (p_stmp) |
Bram Moolenaar | 0725608 | 2009-02-04 13:19:42 +0000 | [diff] [blame] | 4482 | { |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4483 | (void)setsid(); |
Bram Moolenaar | 0725608 | 2009-02-04 13:19:42 +0000 | [diff] [blame] | 4484 | # if defined(SIGHUP) |
| 4485 | /* When doing "!xterm&" and 'shell' is bash: the shell |
| 4486 | * will exit and send SIGHUP to all processes in its |
| 4487 | * group, killing the just started process. Ignore SIGHUP |
| 4488 | * to avoid that. (suggested by Simon Schubert) |
| 4489 | */ |
| 4490 | signal(SIGHUP, SIG_IGN); |
| 4491 | # endif |
| 4492 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4493 | # endif |
| 4494 | # ifdef FEAT_GUI |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4495 | if (pty_slave_fd >= 0) |
| 4496 | { |
| 4497 | /* push stream discipline modules */ |
| 4498 | if (options & SHELL_COOKED) |
| 4499 | SetupSlavePTY(pty_slave_fd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4500 | # ifdef TIOCSCTTY |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4501 | /* Try to become controlling tty (probably doesn't work, |
| 4502 | * unless run by root) */ |
| 4503 | ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4504 | # endif |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4505 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4506 | # endif |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4507 | set_default_child_environment(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4508 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4509 | /* |
| 4510 | * stderr is only redirected when using the GUI, so that a |
| 4511 | * program like gpg can still access the terminal to get a |
| 4512 | * passphrase using stderr. |
| 4513 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4514 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4515 | if (pty_master_fd >= 0) |
| 4516 | { |
| 4517 | close(pty_master_fd); /* close master side of pty */ |
| 4518 | |
| 4519 | /* set up stdin/stdout/stderr for the child */ |
| 4520 | close(0); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4521 | ignored = dup(pty_slave_fd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4522 | close(1); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4523 | ignored = dup(pty_slave_fd); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4524 | if (gui.in_use) |
| 4525 | { |
| 4526 | close(2); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4527 | ignored = dup(pty_slave_fd); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4528 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4529 | |
| 4530 | close(pty_slave_fd); /* has been dupped, close it now */ |
| 4531 | } |
| 4532 | else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4533 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4534 | { |
| 4535 | /* set up stdin for the child */ |
| 4536 | close(fd_toshell[1]); |
| 4537 | close(0); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4538 | ignored = dup(fd_toshell[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4539 | close(fd_toshell[0]); |
| 4540 | |
| 4541 | /* set up stdout for the child */ |
| 4542 | close(fd_fromshell[0]); |
| 4543 | close(1); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4544 | ignored = dup(fd_fromshell[1]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4545 | close(fd_fromshell[1]); |
| 4546 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4547 | # ifdef FEAT_GUI |
| 4548 | if (gui.in_use) |
| 4549 | { |
| 4550 | /* set up stderr for the child */ |
| 4551 | close(2); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4552 | ignored = dup(1); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4553 | } |
| 4554 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4555 | } |
| 4556 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4557 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4558 | /* |
| 4559 | * There is no type cast for the argv, because the type may be |
| 4560 | * different on different machines. This may cause a warning |
| 4561 | * message with strict compilers, don't worry about it. |
| 4562 | * Call _exit() instead of exit() to avoid closing the connection |
| 4563 | * to the X server (esp. with GTK, which uses atexit()). |
| 4564 | */ |
| 4565 | execvp(argv[0], argv); |
| 4566 | _exit(EXEC_FAILED); /* exec failed, return failure code */ |
| 4567 | } |
| 4568 | else /* parent */ |
| 4569 | { |
| 4570 | /* |
| 4571 | * While child is running, ignore terminating signals. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4572 | * Do catch CTRL-C, so that "got_int" is set. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4573 | */ |
| 4574 | catch_signals(SIG_IGN, SIG_ERR); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4575 | catch_int_signal(); |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 4576 | UNBLOCK_SIGNALS(&curset); |
Bram Moolenaar | c4d4ac2 | 2016-11-07 22:42:57 +0100 | [diff] [blame] | 4577 | # ifdef FEAT_JOB_CHANNEL |
| 4578 | ++dont_check_job_ended; |
| 4579 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4580 | /* |
| 4581 | * For the GUI we redirect stdin, stdout and stderr to our window. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4582 | * This is also used to pipe stdin/stdout to/from the external |
| 4583 | * command. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4584 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4585 | if ((options & (SHELL_READ|SHELL_WRITE)) |
| 4586 | # ifdef FEAT_GUI |
| 4587 | || (gui.in_use && show_shell_mess) |
| 4588 | # endif |
| 4589 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4590 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4591 | # define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4592 | char_u buffer[BUFLEN + 1]; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4593 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4594 | int buffer_off = 0; /* valid bytes in buffer[] */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4595 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4596 | char_u ta_buf[BUFLEN + 1]; /* TypeAHead */ |
| 4597 | int ta_len = 0; /* valid bytes in ta_buf[] */ |
| 4598 | int len; |
| 4599 | int p_more_save; |
| 4600 | int old_State; |
| 4601 | int c; |
| 4602 | int toshell_fd; |
| 4603 | int fromshell_fd; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4604 | garray_T ga; |
| 4605 | int noread_cnt; |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4606 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 4607 | struct timeval start_tv; |
| 4608 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4609 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4610 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4611 | if (pty_master_fd >= 0) |
| 4612 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4613 | fromshell_fd = pty_master_fd; |
| 4614 | toshell_fd = dup(pty_master_fd); |
| 4615 | } |
| 4616 | else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4617 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4618 | { |
| 4619 | close(fd_toshell[0]); |
| 4620 | close(fd_fromshell[1]); |
| 4621 | toshell_fd = fd_toshell[1]; |
| 4622 | fromshell_fd = fd_fromshell[0]; |
| 4623 | } |
| 4624 | |
| 4625 | /* |
| 4626 | * Write to the child if there are typed characters. |
| 4627 | * Read from the child if there are characters available. |
| 4628 | * Repeat the reading a few times if more characters are |
| 4629 | * available. Need to check for typed keys now and then, but |
| 4630 | * not too often (delays when no chars are available). |
| 4631 | * This loop is quit if no characters can be read from the pty |
| 4632 | * (WaitForChar detected special condition), or there are no |
| 4633 | * characters available and the child has exited. |
| 4634 | * Only check if the child has exited when there is no more |
| 4635 | * output. The child may exit before all the output has |
| 4636 | * been printed. |
| 4637 | * |
| 4638 | * Currently this busy loops! |
| 4639 | * This can probably dead-lock when the write blocks! |
| 4640 | */ |
| 4641 | p_more_save = p_more; |
| 4642 | p_more = FALSE; |
| 4643 | old_State = State; |
| 4644 | State = EXTERNCMD; /* don't redraw at window resize */ |
| 4645 | |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4646 | if ((options & SHELL_WRITE) && toshell_fd >= 0) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4647 | { |
| 4648 | /* Fork a process that will write the lines to the |
| 4649 | * external program. */ |
| 4650 | if ((wpid = fork()) == -1) |
| 4651 | { |
| 4652 | MSG_PUTS(_("\nCannot fork\n")); |
| 4653 | } |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4654 | else if (wpid == 0) /* child */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4655 | { |
| 4656 | linenr_T lnum = curbuf->b_op_start.lnum; |
| 4657 | int written = 0; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4658 | char_u *lp = ml_get(lnum); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4659 | size_t l; |
| 4660 | |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 4661 | close(fromshell_fd); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4662 | for (;;) |
| 4663 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4664 | l = STRLEN(lp + written); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4665 | if (l == 0) |
| 4666 | len = 0; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4667 | else if (lp[written] == NL) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4668 | /* NL -> NUL translation */ |
| 4669 | len = write(toshell_fd, "", (size_t)1); |
| 4670 | else |
| 4671 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4672 | char_u *s = vim_strchr(lp + written, NL); |
| 4673 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4674 | len = write(toshell_fd, (char *)lp + written, |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 4675 | s == NULL ? l |
| 4676 | : (size_t)(s - (lp + written))); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4677 | } |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 4678 | if (len == (int)l) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4679 | { |
| 4680 | /* Finished a line, add a NL, unless this line |
| 4681 | * should not have one. */ |
| 4682 | if (lnum != curbuf->b_op_end.lnum |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 4683 | || (!curbuf->b_p_bin |
| 4684 | && curbuf->b_p_fixeol) |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 4685 | || (lnum != curbuf->b_no_eol_lnum |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4686 | && (lnum != |
| 4687 | curbuf->b_ml.ml_line_count |
| 4688 | || curbuf->b_p_eol))) |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4689 | ignored = write(toshell_fd, "\n", |
| 4690 | (size_t)1); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4691 | ++lnum; |
| 4692 | if (lnum > curbuf->b_op_end.lnum) |
| 4693 | { |
| 4694 | /* finished all the lines, close pipe */ |
| 4695 | close(toshell_fd); |
| 4696 | toshell_fd = -1; |
| 4697 | break; |
| 4698 | } |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4699 | lp = ml_get(lnum); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4700 | written = 0; |
| 4701 | } |
| 4702 | else if (len > 0) |
| 4703 | written += len; |
| 4704 | } |
| 4705 | _exit(0); |
| 4706 | } |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4707 | else /* parent */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4708 | { |
| 4709 | close(toshell_fd); |
| 4710 | toshell_fd = -1; |
| 4711 | } |
| 4712 | } |
| 4713 | |
| 4714 | if (options & SHELL_READ) |
| 4715 | ga_init2(&ga, 1, BUFLEN); |
| 4716 | |
| 4717 | noread_cnt = 0; |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 4718 | # ifdef ELAPSED_FUNC |
| 4719 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4720 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4721 | for (;;) |
| 4722 | { |
| 4723 | /* |
| 4724 | * Check if keys have been typed, write them to the child |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 4725 | * if there are any. |
| 4726 | * Don't do this if we are expanding wild cards (would eat |
| 4727 | * typeahead). |
| 4728 | * Don't do this when filtering and terminal is in cooked |
| 4729 | * mode, the shell command will handle the I/O. Avoids |
| 4730 | * that a typed password is echoed for ssh or gpg command. |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4731 | * Don't get characters when the child has already |
| 4732 | * finished (wait_pid == 0). |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4733 | * Don't read characters unless we didn't get output for a |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4734 | * while (noread_cnt > 4), avoids that ":r !ls" eats |
| 4735 | * typeahead. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4736 | */ |
| 4737 | len = 0; |
| 4738 | if (!(options & SHELL_EXPAND) |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 4739 | && ((options & |
| 4740 | (SHELL_READ|SHELL_WRITE|SHELL_COOKED)) |
| 4741 | != (SHELL_READ|SHELL_WRITE|SHELL_COOKED) |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4742 | # ifdef FEAT_GUI |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 4743 | || gui.in_use |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4744 | # endif |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 4745 | ) |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4746 | && wait_pid == 0 |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4747 | && (ta_len > 0 || noread_cnt > 4)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4748 | { |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4749 | if (ta_len == 0) |
| 4750 | { |
| 4751 | /* Get extra characters when we don't have any. |
| 4752 | * Reset the counter and timer. */ |
| 4753 | noread_cnt = 0; |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 4754 | # ifdef ELAPSED_FUNC |
| 4755 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4756 | # endif |
| 4757 | len = ui_inchar(ta_buf, BUFLEN, 10L, 0); |
| 4758 | } |
| 4759 | if (ta_len > 0 || len > 0) |
| 4760 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4761 | /* |
| 4762 | * For pipes: |
| 4763 | * Check for CTRL-C: send interrupt signal to child. |
| 4764 | * Check for CTRL-D: EOF, close pipe to child. |
| 4765 | */ |
| 4766 | if (len == 1 && (pty_master_fd < 0 || cmd != NULL)) |
| 4767 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4768 | # ifdef SIGINT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4769 | /* |
| 4770 | * Send SIGINT to the child's group or all |
| 4771 | * processes in our group. |
| 4772 | */ |
| 4773 | if (ta_buf[ta_len] == Ctrl_C |
| 4774 | || ta_buf[ta_len] == intr_char) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4775 | { |
| 4776 | # ifdef HAVE_SETSID |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4777 | kill(-pid, SIGINT); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4778 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4779 | kill(0, SIGINT); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4780 | # endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4781 | if (wpid > 0) |
| 4782 | kill(wpid, SIGINT); |
| 4783 | } |
| 4784 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4785 | if (pty_master_fd < 0 && toshell_fd >= 0 |
| 4786 | && ta_buf[ta_len] == Ctrl_D) |
| 4787 | { |
| 4788 | close(toshell_fd); |
| 4789 | toshell_fd = -1; |
| 4790 | } |
| 4791 | } |
| 4792 | |
| 4793 | /* replace K_BS by <BS> and K_DEL by <DEL> */ |
| 4794 | for (i = ta_len; i < ta_len + len; ++i) |
| 4795 | { |
| 4796 | if (ta_buf[i] == CSI && len - i > 2) |
| 4797 | { |
| 4798 | c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]); |
| 4799 | if (c == K_DEL || c == K_KDEL || c == K_BS) |
| 4800 | { |
| 4801 | mch_memmove(ta_buf + i + 1, ta_buf + i + 3, |
| 4802 | (size_t)(len - i - 2)); |
| 4803 | if (c == K_DEL || c == K_KDEL) |
| 4804 | ta_buf[i] = DEL; |
| 4805 | else |
| 4806 | ta_buf[i] = Ctrl_H; |
| 4807 | len -= 2; |
| 4808 | } |
| 4809 | } |
| 4810 | else if (ta_buf[i] == '\r') |
| 4811 | ta_buf[i] = '\n'; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4812 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4813 | if (has_mbyte) |
Bram Moolenaar | feba08b | 2009-06-16 13:12:07 +0000 | [diff] [blame] | 4814 | i += (*mb_ptr2len_len)(ta_buf + i, |
| 4815 | ta_len + len - i) - 1; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4816 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
| 4819 | /* |
| 4820 | * For pipes: echo the typed characters. |
| 4821 | * For a pty this does not seem to work. |
| 4822 | */ |
| 4823 | if (pty_master_fd < 0) |
| 4824 | { |
| 4825 | for (i = ta_len; i < ta_len + len; ++i) |
| 4826 | { |
| 4827 | if (ta_buf[i] == '\n' || ta_buf[i] == '\b') |
| 4828 | msg_putchar(ta_buf[i]); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4829 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4830 | else if (has_mbyte) |
| 4831 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4832 | int l = (*mb_ptr2len)(ta_buf + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4833 | |
| 4834 | msg_outtrans_len(ta_buf + i, l); |
| 4835 | i += l - 1; |
| 4836 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4837 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4838 | else |
| 4839 | msg_outtrans_len(ta_buf + i, 1); |
| 4840 | } |
| 4841 | windgoto(msg_row, msg_col); |
| 4842 | out_flush(); |
| 4843 | } |
| 4844 | |
| 4845 | ta_len += len; |
| 4846 | |
| 4847 | /* |
| 4848 | * Write the characters to the child, unless EOF has |
| 4849 | * been typed for pipes. Write one character at a |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 4850 | * time, to avoid losing too much typeahead. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4851 | * When writing buffer lines, drop the typed |
| 4852 | * characters (only check for CTRL-C). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4853 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4854 | if (options & SHELL_WRITE) |
| 4855 | ta_len = 0; |
| 4856 | else if (toshell_fd >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4857 | { |
| 4858 | len = write(toshell_fd, (char *)ta_buf, (size_t)1); |
| 4859 | if (len > 0) |
| 4860 | { |
| 4861 | ta_len -= len; |
| 4862 | mch_memmove(ta_buf, ta_buf + len, ta_len); |
| 4863 | } |
| 4864 | } |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4865 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4866 | } |
| 4867 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4868 | if (got_int) |
| 4869 | { |
| 4870 | /* CTRL-C sends a signal to the child, we ignore it |
| 4871 | * ourselves */ |
| 4872 | # ifdef HAVE_SETSID |
| 4873 | kill(-pid, SIGINT); |
| 4874 | # else |
| 4875 | kill(0, SIGINT); |
| 4876 | # endif |
| 4877 | if (wpid > 0) |
| 4878 | kill(wpid, SIGINT); |
| 4879 | got_int = FALSE; |
| 4880 | } |
| 4881 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4882 | /* |
| 4883 | * Check if the child has any characters to be printed. |
| 4884 | * Read them and write them to our window. Repeat this as |
| 4885 | * long as there is something to do, avoid the 10ms wait |
| 4886 | * for mch_inchar(), or sending typeahead characters to |
| 4887 | * the external process. |
| 4888 | * TODO: This should handle escape sequences, compatible |
| 4889 | * to some terminal (vt52?). |
| 4890 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4891 | ++noread_cnt; |
Bram Moolenaar | 8fdd721 | 2016-03-26 19:41:48 +0100 | [diff] [blame] | 4892 | while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4893 | { |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 4894 | len = read_eintr(fromshell_fd, buffer |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4895 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4896 | + buffer_off, (size_t)(BUFLEN - buffer_off) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4897 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4898 | , (size_t)BUFLEN |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4899 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4900 | ); |
| 4901 | if (len <= 0) /* end of file or error */ |
| 4902 | goto finished; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4903 | |
| 4904 | noread_cnt = 0; |
| 4905 | if (options & SHELL_READ) |
| 4906 | { |
| 4907 | /* Do NUL -> NL translation, append NL separated |
| 4908 | * lines to the current buffer. */ |
| 4909 | for (i = 0; i < len; ++i) |
| 4910 | { |
| 4911 | if (buffer[i] == NL) |
| 4912 | append_ga_line(&ga); |
| 4913 | else if (buffer[i] == NUL) |
| 4914 | ga_append(&ga, NL); |
| 4915 | else |
| 4916 | ga_append(&ga, buffer[i]); |
| 4917 | } |
| 4918 | } |
| 4919 | # ifdef FEAT_MBYTE |
| 4920 | else if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4921 | { |
| 4922 | int l; |
| 4923 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4924 | len += buffer_off; |
| 4925 | buffer[len] = NUL; |
| 4926 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4927 | /* Check if the last character in buffer[] is |
| 4928 | * incomplete, keep these bytes for the next |
| 4929 | * round. */ |
| 4930 | for (p = buffer; p < buffer + len; p += l) |
| 4931 | { |
Bram Moolenaar | d3c907b | 2016-08-17 21:32:09 +0200 | [diff] [blame] | 4932 | l = MB_CPTR2LEN(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4933 | if (l == 0) |
| 4934 | l = 1; /* NUL byte? */ |
| 4935 | else if (MB_BYTE2LEN(*p) != l) |
| 4936 | break; |
| 4937 | } |
| 4938 | if (p == buffer) /* no complete character */ |
| 4939 | { |
| 4940 | /* avoid getting stuck at an illegal byte */ |
| 4941 | if (len >= 12) |
| 4942 | ++p; |
| 4943 | else |
| 4944 | { |
| 4945 | buffer_off = len; |
| 4946 | continue; |
| 4947 | } |
| 4948 | } |
| 4949 | c = *p; |
| 4950 | *p = NUL; |
| 4951 | msg_puts(buffer); |
| 4952 | if (p < buffer + len) |
| 4953 | { |
| 4954 | *p = c; |
| 4955 | buffer_off = (buffer + len) - p; |
| 4956 | mch_memmove(buffer, p, buffer_off); |
| 4957 | continue; |
| 4958 | } |
| 4959 | buffer_off = 0; |
| 4960 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4961 | # endif /* FEAT_MBYTE */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4962 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4963 | { |
| 4964 | buffer[len] = NUL; |
| 4965 | msg_puts(buffer); |
| 4966 | } |
| 4967 | |
| 4968 | windgoto(msg_row, msg_col); |
| 4969 | cursor_on(); |
| 4970 | out_flush(); |
| 4971 | if (got_int) |
| 4972 | break; |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4973 | |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 4974 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 17fe5e1 | 2016-04-04 22:03:08 +0200 | [diff] [blame] | 4975 | if (wait_pid == 0) |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4976 | { |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 4977 | long msec = ELAPSED_FUNC(start_tv); |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4978 | |
| 4979 | /* Avoid that we keep looping here without |
| 4980 | * checking for a CTRL-C for a long time. Don't |
| 4981 | * break out too often to avoid losing typeahead. */ |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4982 | if (msec > 2000) |
| 4983 | { |
| 4984 | noread_cnt = 5; |
| 4985 | break; |
| 4986 | } |
| 4987 | } |
| 4988 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
Bram Moolenaar | 17fe5e1 | 2016-04-04 22:03:08 +0200 | [diff] [blame] | 4991 | /* If we already detected the child has finished, continue |
| 4992 | * reading output for a short while. Some text may be |
| 4993 | * buffered. */ |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4994 | if (wait_pid == pid) |
Bram Moolenaar | 17fe5e1 | 2016-04-04 22:03:08 +0200 | [diff] [blame] | 4995 | { |
| 4996 | if (noread_cnt < 5) |
| 4997 | continue; |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4998 | break; |
Bram Moolenaar | 17fe5e1 | 2016-04-04 22:03:08 +0200 | [diff] [blame] | 4999 | } |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5000 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5001 | /* |
| 5002 | * Check if the child still exists, before checking for |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 5003 | * typed characters (otherwise we would lose typeahead). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5004 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5005 | # ifdef __NeXT__ |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 5006 | wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5007 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5008 | wait_pid = waitpid(pid, &status, WNOHANG); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5009 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5010 | if ((wait_pid == (pid_t)-1 && errno == ECHILD) |
| 5011 | || (wait_pid == pid && WIFEXITED(status))) |
| 5012 | { |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5013 | /* Don't break the loop yet, try reading more |
| 5014 | * characters from "fromshell_fd" first. When using |
| 5015 | * pipes there might still be something to read and |
| 5016 | * then we'll break the loop at the "break" above. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5017 | wait_pid = pid; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5018 | } |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5019 | else |
| 5020 | wait_pid = 0; |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5021 | |
Bram Moolenaar | 95a5135 | 2013-03-21 22:53:50 +0100 | [diff] [blame] | 5022 | # if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5023 | /* Handle any X events, e.g. serving the clipboard. */ |
| 5024 | clip_update(); |
| 5025 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5026 | } |
| 5027 | finished: |
| 5028 | p_more = p_more_save; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5029 | if (options & SHELL_READ) |
| 5030 | { |
| 5031 | if (ga.ga_len > 0) |
| 5032 | { |
| 5033 | append_ga_line(&ga); |
| 5034 | /* remember that the NL was missing */ |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 5035 | curbuf->b_no_eol_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5036 | } |
| 5037 | else |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 5038 | curbuf->b_no_eol_lnum = 0; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5039 | ga_clear(&ga); |
| 5040 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5041 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5042 | /* |
| 5043 | * Give all typeahead that wasn't used back to ui_inchar(). |
| 5044 | */ |
| 5045 | if (ta_len) |
| 5046 | ui_inchar_undo(ta_buf, ta_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5047 | State = old_State; |
| 5048 | if (toshell_fd >= 0) |
| 5049 | close(toshell_fd); |
| 5050 | close(fromshell_fd); |
| 5051 | } |
Bram Moolenaar | 95a5135 | 2013-03-21 22:53:50 +0100 | [diff] [blame] | 5052 | # if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5053 | else |
| 5054 | { |
Bram Moolenaar | 0abe052 | 2016-08-28 16:53:12 +0200 | [diff] [blame] | 5055 | long delay_msec = 1; |
| 5056 | |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5057 | /* |
| 5058 | * Similar to the loop above, but only handle X events, no |
| 5059 | * I/O. |
| 5060 | */ |
| 5061 | for (;;) |
| 5062 | { |
| 5063 | if (got_int) |
| 5064 | { |
| 5065 | /* CTRL-C sends a signal to the child, we ignore it |
| 5066 | * ourselves */ |
| 5067 | # ifdef HAVE_SETSID |
| 5068 | kill(-pid, SIGINT); |
| 5069 | # else |
| 5070 | kill(0, SIGINT); |
| 5071 | # endif |
| 5072 | got_int = FALSE; |
| 5073 | } |
| 5074 | # ifdef __NeXT__ |
| 5075 | wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0); |
| 5076 | # else |
| 5077 | wait_pid = waitpid(pid, &status, WNOHANG); |
| 5078 | # endif |
| 5079 | if ((wait_pid == (pid_t)-1 && errno == ECHILD) |
| 5080 | || (wait_pid == pid && WIFEXITED(status))) |
| 5081 | { |
| 5082 | wait_pid = pid; |
| 5083 | break; |
| 5084 | } |
| 5085 | |
| 5086 | /* Handle any X events, e.g. serving the clipboard. */ |
| 5087 | clip_update(); |
| 5088 | |
Bram Moolenaar | 0abe052 | 2016-08-28 16:53:12 +0200 | [diff] [blame] | 5089 | /* Wait for 1 to 10 msec. 1 is faster but gives the child |
| 5090 | * less time. */ |
| 5091 | mch_delay(delay_msec, TRUE); |
| 5092 | if (++delay_msec > 10) |
| 5093 | delay_msec = 10; |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5094 | } |
| 5095 | } |
| 5096 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5097 | |
| 5098 | /* |
| 5099 | * Wait until our child has exited. |
| 5100 | * Ignore wait() returning pids of other children and returning |
| 5101 | * because of some signal like SIGWINCH. |
| 5102 | * Don't wait if wait_pid was already set above, indicating the |
| 5103 | * child already exited. |
| 5104 | */ |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 5105 | if (wait_pid != pid) |
| 5106 | wait_pid = wait4pid(pid, &status); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5107 | |
Bram Moolenaar | 624891f | 2010-10-13 16:22:09 +0200 | [diff] [blame] | 5108 | # ifdef FEAT_GUI |
| 5109 | /* Close slave side of pty. Only do this after the child has |
| 5110 | * exited, otherwise the child may hang when it tries to write on |
| 5111 | * the pty. */ |
| 5112 | if (pty_master_fd >= 0) |
| 5113 | close(pty_slave_fd); |
| 5114 | # endif |
| 5115 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5116 | /* Make sure the child that writes to the external program is |
| 5117 | * dead. */ |
| 5118 | if (wpid > 0) |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 5119 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5120 | kill(wpid, SIGKILL); |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 5121 | wait4pid(wpid, NULL); |
| 5122 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5123 | |
Bram Moolenaar | c4d4ac2 | 2016-11-07 22:42:57 +0100 | [diff] [blame] | 5124 | # ifdef FEAT_JOB_CHANNEL |
| 5125 | --dont_check_job_ended; |
| 5126 | # endif |
| 5127 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5128 | /* |
| 5129 | * Set to raw mode right now, otherwise a CTRL-C after |
| 5130 | * catch_signals() will kill Vim. |
| 5131 | */ |
| 5132 | if (tmode == TMODE_RAW) |
| 5133 | settmode(TMODE_RAW); |
| 5134 | did_settmode = TRUE; |
| 5135 | set_signals(); |
| 5136 | |
| 5137 | if (WIFEXITED(status)) |
| 5138 | { |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 5139 | /* LINTED avoid "bitwise operation on signed value" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5140 | retval = WEXITSTATUS(status); |
Bram Moolenaar | 7567646 | 2013-01-30 14:55:42 +0100 | [diff] [blame] | 5141 | if (retval != 0 && !emsg_silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5142 | { |
| 5143 | if (retval == EXEC_FAILED) |
| 5144 | { |
| 5145 | MSG_PUTS(_("\nCannot execute shell ")); |
| 5146 | msg_outtrans(p_sh); |
| 5147 | msg_putchar('\n'); |
| 5148 | } |
| 5149 | else if (!(options & SHELL_SILENT)) |
| 5150 | { |
| 5151 | MSG_PUTS(_("\nshell returned ")); |
| 5152 | msg_outnum((long)retval); |
| 5153 | msg_putchar('\n'); |
| 5154 | } |
| 5155 | } |
| 5156 | } |
| 5157 | else |
| 5158 | MSG_PUTS(_("\nCommand terminated\n")); |
| 5159 | } |
| 5160 | } |
| 5161 | vim_free(argv); |
Bram Moolenaar | ea35ef6 | 2011-08-04 22:59:28 +0200 | [diff] [blame] | 5162 | vim_free(p_shcf_copy); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5163 | |
| 5164 | error: |
| 5165 | if (!did_settmode) |
| 5166 | if (tmode == TMODE_RAW) |
| 5167 | settmode(TMODE_RAW); /* set to raw mode */ |
| 5168 | # ifdef FEAT_TITLE |
| 5169 | resettitle(); |
| 5170 | # endif |
| 5171 | vim_free(newcmd); |
| 5172 | |
| 5173 | return retval; |
| 5174 | |
| 5175 | #endif /* USE_SYSTEM */ |
| 5176 | } |
| 5177 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 5178 | #if defined(FEAT_JOB_CHANNEL) || defined(PROTO) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5179 | void |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5180 | mch_job_start(char **argv, job_T *job, jobopt_T *options) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5181 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5182 | pid_t pid; |
| 5183 | int fd_in[2]; /* for stdin */ |
| 5184 | int fd_out[2]; /* for stdout */ |
| 5185 | int fd_err[2]; /* for stderr */ |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5186 | int pty_master_fd = -1; |
| 5187 | int pty_slave_fd = -1; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 5188 | channel_T *channel = NULL; |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5189 | int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL; |
| 5190 | int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL; |
| 5191 | int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL; |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5192 | int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5193 | int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE; |
| 5194 | int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE; |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5195 | int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT; |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5196 | SIGSET_DECL(curset) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5197 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5198 | if (use_out_for_err && use_null_for_out) |
| 5199 | use_null_for_err = TRUE; |
| 5200 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5201 | /* default is to fail */ |
| 5202 | job->jv_status = JOB_FAILED; |
| 5203 | fd_in[0] = -1; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5204 | fd_in[1] = -1; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5205 | fd_out[0] = -1; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5206 | fd_out[1] = -1; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5207 | fd_err[0] = -1; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5208 | fd_err[1] = -1; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5209 | |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5210 | if (options->jo_pty) |
| 5211 | open_pty(&pty_master_fd, &pty_slave_fd); |
| 5212 | |
Bram Moolenaar | 12dcf02 | 2016-02-15 23:09:04 +0100 | [diff] [blame] | 5213 | /* TODO: without the channel feature connect the child to /dev/null? */ |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5214 | /* Open pipes for stdin, stdout, stderr. */ |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5215 | if (use_file_for_in) |
| 5216 | { |
| 5217 | char_u *fname = options->jo_io_name[PART_IN]; |
| 5218 | |
| 5219 | fd_in[0] = mch_open((char *)fname, O_RDONLY, 0); |
| 5220 | if (fd_in[0] < 0) |
| 5221 | { |
| 5222 | EMSG2(_(e_notopen), fname); |
| 5223 | goto failed; |
| 5224 | } |
| 5225 | } |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5226 | else if (!use_null_for_in && pty_master_fd < 0 && pipe(fd_in) < 0) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5227 | goto failed; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5228 | |
| 5229 | if (use_file_for_out) |
| 5230 | { |
| 5231 | char_u *fname = options->jo_io_name[PART_OUT]; |
| 5232 | |
| 5233 | fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 5234 | if (fd_out[1] < 0) |
| 5235 | { |
| 5236 | EMSG2(_(e_notopen), fname); |
| 5237 | goto failed; |
| 5238 | } |
| 5239 | } |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5240 | else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5241 | goto failed; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5242 | |
| 5243 | if (use_file_for_err) |
| 5244 | { |
| 5245 | char_u *fname = options->jo_io_name[PART_ERR]; |
| 5246 | |
| 5247 | fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 5248 | if (fd_err[1] < 0) |
| 5249 | { |
| 5250 | EMSG2(_(e_notopen), fname); |
| 5251 | goto failed; |
| 5252 | } |
| 5253 | } |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5254 | else if (!use_out_for_err && !use_null_for_err |
| 5255 | && pty_master_fd < 0 && pipe(fd_err) < 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5256 | goto failed; |
| 5257 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5258 | if (!use_null_for_in || !use_null_for_out || !use_null_for_err) |
| 5259 | { |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 5260 | if (options->jo_set & JO_CHANNEL) |
| 5261 | { |
| 5262 | channel = options->jo_channel; |
| 5263 | if (channel != NULL) |
| 5264 | ++channel->ch_refcount; |
| 5265 | } |
| 5266 | else |
| 5267 | channel = add_channel(); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5268 | if (channel == NULL) |
| 5269 | goto failed; |
| 5270 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5271 | |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5272 | BLOCK_SIGNALS(&curset); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5273 | pid = fork(); /* maybe we should use vfork() */ |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5274 | if (pid == -1) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5275 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5276 | /* failed to fork */ |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5277 | UNBLOCK_SIGNALS(&curset); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5278 | goto failed; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5279 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5280 | if (pid == 0) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5281 | { |
Bram Moolenaar | 4694a17 | 2016-04-21 14:05:23 +0200 | [diff] [blame] | 5282 | int null_fd = -1; |
| 5283 | int stderr_works = TRUE; |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5284 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5285 | /* child */ |
| 5286 | reset_signals(); /* handle signals normally */ |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5287 | UNBLOCK_SIGNALS(&curset); |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5288 | |
| 5289 | # ifdef HAVE_SETSID |
| 5290 | /* Create our own process group, so that the child and all its |
| 5291 | * children can be kill()ed. Don't do this when using pipes, |
| 5292 | * because stdin is not a tty, we would lose /dev/tty. */ |
| 5293 | (void)setsid(); |
| 5294 | # endif |
| 5295 | |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 5296 | # ifdef FEAT_TERMINAL |
| 5297 | if (options->jo_term_rows > 0) |
| 5298 | set_child_environment( |
| 5299 | (long)options->jo_term_rows, |
| 5300 | (long)options->jo_term_cols, |
Bram Moolenaar | 93723a4 | 2017-07-28 15:55:32 +0200 | [diff] [blame] | 5301 | STRNCMP(T_NAME, "xterm", 5) == 0 |
| 5302 | ? (char *)T_NAME : "xterm"); |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 5303 | else |
| 5304 | # endif |
| 5305 | set_default_child_environment(); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5306 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5307 | if (use_null_for_in || use_null_for_out || use_null_for_err) |
| 5308 | null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0); |
| 5309 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5310 | /* set up stdin for the child */ |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5311 | close(0); |
Bram Moolenaar | c0a1d7f | 2016-03-19 14:12:50 +0100 | [diff] [blame] | 5312 | if (use_null_for_in && null_fd >= 0) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5313 | ignored = dup(null_fd); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5314 | else if (fd_in[0] < 0) |
| 5315 | ignored = dup(pty_slave_fd); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5316 | else |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5317 | ignored = dup(fd_in[0]); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5318 | |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5319 | /* set up stderr for the child */ |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5320 | close(2); |
Bram Moolenaar | c0a1d7f | 2016-03-19 14:12:50 +0100 | [diff] [blame] | 5321 | if (use_null_for_err && null_fd >= 0) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5322 | { |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5323 | ignored = dup(null_fd); |
Bram Moolenaar | 4694a17 | 2016-04-21 14:05:23 +0200 | [diff] [blame] | 5324 | stderr_works = FALSE; |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5325 | } |
| 5326 | else if (use_out_for_err) |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5327 | ignored = dup(fd_out[1]); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5328 | else if (fd_err[1] < 0) |
| 5329 | ignored = dup(pty_slave_fd); |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5330 | else |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5331 | ignored = dup(fd_err[1]); |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5332 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5333 | /* set up stdout for the child */ |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5334 | close(1); |
Bram Moolenaar | c0a1d7f | 2016-03-19 14:12:50 +0100 | [diff] [blame] | 5335 | if (use_null_for_out && null_fd >= 0) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5336 | ignored = dup(null_fd); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5337 | else if (fd_out[1] < 0) |
| 5338 | ignored = dup(pty_slave_fd); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5339 | else |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5340 | ignored = dup(fd_out[1]); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5341 | |
| 5342 | if (fd_in[0] >= 0) |
| 5343 | close(fd_in[0]); |
| 5344 | if (fd_in[1] >= 0) |
| 5345 | close(fd_in[1]); |
| 5346 | if (fd_out[0] >= 0) |
| 5347 | close(fd_out[0]); |
| 5348 | if (fd_out[1] >= 0) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5349 | close(fd_out[1]); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5350 | if (fd_err[0] >= 0) |
| 5351 | close(fd_err[0]); |
| 5352 | if (fd_err[1] >= 0) |
| 5353 | close(fd_err[1]); |
| 5354 | if (pty_master_fd >= 0) |
| 5355 | { |
| 5356 | close(pty_master_fd); /* not used */ |
| 5357 | close(pty_slave_fd); /* duped above */ |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5358 | } |
Bram Moolenaar | ea83bf0 | 2016-05-08 09:40:51 +0200 | [diff] [blame] | 5359 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5360 | if (null_fd >= 0) |
| 5361 | close(null_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5362 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5363 | /* See above for type of argv. */ |
| 5364 | execvp(argv[0], argv); |
| 5365 | |
Bram Moolenaar | 4694a17 | 2016-04-21 14:05:23 +0200 | [diff] [blame] | 5366 | if (stderr_works) |
| 5367 | perror("executing job failed"); |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5368 | #ifdef EXITFREE |
| 5369 | /* calling free_all_mem() here causes problems. Ignore valgrind |
| 5370 | * reporting possibly leaked memory. */ |
| 5371 | #endif |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5372 | _exit(EXEC_FAILED); /* exec failed, return failure code */ |
| 5373 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5374 | |
| 5375 | /* parent */ |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5376 | UNBLOCK_SIGNALS(&curset); |
| 5377 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5378 | job->jv_pid = pid; |
| 5379 | job->jv_status = JOB_STARTED; |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 5380 | job->jv_channel = channel; /* ch_refcount was set above */ |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5381 | |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5382 | if (pty_master_fd >= 0) |
| 5383 | close(pty_slave_fd); /* duped above */ |
| 5384 | /* close child stdin, stdout and stderr */ |
Bram Moolenaar | be6aa46 | 2016-03-20 21:02:00 +0100 | [diff] [blame] | 5385 | if (!use_file_for_in && fd_in[0] >= 0) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5386 | close(fd_in[0]); |
Bram Moolenaar | be6aa46 | 2016-03-20 21:02:00 +0100 | [diff] [blame] | 5387 | if (!use_file_for_out && fd_out[1] >= 0) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5388 | close(fd_out[1]); |
Bram Moolenaar | be6aa46 | 2016-03-20 21:02:00 +0100 | [diff] [blame] | 5389 | if (!use_out_for_err && !use_file_for_err && fd_err[1] >= 0) |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5390 | close(fd_err[1]); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5391 | if (channel != NULL) |
| 5392 | { |
| 5393 | channel_set_pipes(channel, |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5394 | use_file_for_in || use_null_for_in |
| 5395 | ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1], |
| 5396 | use_file_for_out || use_null_for_out |
| 5397 | ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0], |
| 5398 | use_out_for_err || use_file_for_err || use_null_for_err |
| 5399 | ? INVALID_FD : fd_err[0] < 0 ? pty_master_fd : fd_err[0]); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5400 | channel_set_job(channel, job, options); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5401 | } |
Bram Moolenaar | 979e8c5 | 2017-08-01 15:08:07 +0200 | [diff] [blame] | 5402 | else |
| 5403 | { |
| 5404 | if (fd_in[1] >= 0) |
| 5405 | close(fd_in[1]); |
| 5406 | if (fd_out[0] >= 0) |
| 5407 | close(fd_out[0]); |
| 5408 | if (fd_err[0] >= 0) |
| 5409 | close(fd_err[0]); |
| 5410 | if (pty_master_fd >= 0) |
| 5411 | close(pty_master_fd); |
| 5412 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5413 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5414 | /* success! */ |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5415 | return; |
| 5416 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 5417 | failed: |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 5418 | channel_unref(channel); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5419 | if (fd_in[0] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5420 | close(fd_in[0]); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5421 | if (fd_in[1] >= 0) |
| 5422 | close(fd_in[1]); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5423 | if (fd_out[0] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5424 | close(fd_out[0]); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5425 | if (fd_out[1] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5426 | close(fd_out[1]); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5427 | if (fd_err[0] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5428 | close(fd_err[0]); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5429 | if (fd_err[1] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5430 | close(fd_err[1]); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5431 | if (pty_master_fd >= 0) |
| 5432 | close(pty_master_fd); |
| 5433 | if (pty_slave_fd >= 0) |
| 5434 | close(pty_slave_fd); |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5435 | } |
| 5436 | |
| 5437 | char * |
| 5438 | mch_job_status(job_T *job) |
| 5439 | { |
| 5440 | # ifdef HAVE_UNION_WAIT |
| 5441 | union wait status; |
| 5442 | # else |
| 5443 | int status = -1; |
| 5444 | # endif |
| 5445 | pid_t wait_pid = 0; |
| 5446 | |
| 5447 | # ifdef __NeXT__ |
| 5448 | wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0); |
| 5449 | # else |
| 5450 | wait_pid = waitpid(job->jv_pid, &status, WNOHANG); |
| 5451 | # endif |
| 5452 | if (wait_pid == -1) |
| 5453 | { |
| 5454 | /* process must have exited */ |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 5455 | goto return_dead; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5456 | } |
| 5457 | if (wait_pid == 0) |
| 5458 | return "run"; |
| 5459 | if (WIFEXITED(status)) |
| 5460 | { |
| 5461 | /* LINTED avoid "bitwise operation on signed value" */ |
| 5462 | job->jv_exitval = WEXITSTATUS(status); |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 5463 | goto return_dead; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5464 | } |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5465 | if (WIFSIGNALED(status)) |
| 5466 | { |
| 5467 | job->jv_exitval = -1; |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 5468 | goto return_dead; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5469 | } |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5470 | return "run"; |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 5471 | |
| 5472 | return_dead: |
Bram Moolenaar | 7df915d | 2016-11-17 17:25:32 +0100 | [diff] [blame] | 5473 | if (job->jv_status < JOB_ENDED) |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 5474 | { |
| 5475 | ch_log(job->jv_channel, "Job ended"); |
| 5476 | job->jv_status = JOB_ENDED; |
| 5477 | } |
| 5478 | return "dead"; |
| 5479 | } |
| 5480 | |
| 5481 | job_T * |
| 5482 | mch_detect_ended_job(job_T *job_list) |
| 5483 | { |
| 5484 | # ifdef HAVE_UNION_WAIT |
| 5485 | union wait status; |
| 5486 | # else |
| 5487 | int status = -1; |
| 5488 | # endif |
| 5489 | pid_t wait_pid = 0; |
| 5490 | job_T *job; |
| 5491 | |
Bram Moolenaar | c4d4ac2 | 2016-11-07 22:42:57 +0100 | [diff] [blame] | 5492 | # ifndef USE_SYSTEM |
| 5493 | /* Do not do this when waiting for a shell command to finish, we would get |
| 5494 | * the exit value here (and discard it), the exit value obtained there |
| 5495 | * would then be wrong. */ |
| 5496 | if (dont_check_job_ended > 0) |
| 5497 | return NULL; |
| 5498 | # endif |
| 5499 | |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 5500 | # ifdef __NeXT__ |
| 5501 | wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0); |
| 5502 | # else |
| 5503 | wait_pid = waitpid(-1, &status, WNOHANG); |
| 5504 | # endif |
| 5505 | if (wait_pid <= 0) |
| 5506 | /* no process ended */ |
| 5507 | return NULL; |
| 5508 | for (job = job_list; job != NULL; job = job->jv_next) |
| 5509 | { |
| 5510 | if (job->jv_pid == wait_pid) |
| 5511 | { |
| 5512 | if (WIFEXITED(status)) |
| 5513 | /* LINTED avoid "bitwise operation on signed value" */ |
| 5514 | job->jv_exitval = WEXITSTATUS(status); |
| 5515 | else if (WIFSIGNALED(status)) |
| 5516 | job->jv_exitval = -1; |
Bram Moolenaar | 7df915d | 2016-11-17 17:25:32 +0100 | [diff] [blame] | 5517 | if (job->jv_status < JOB_ENDED) |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 5518 | { |
| 5519 | ch_log(job->jv_channel, "Job ended"); |
| 5520 | job->jv_status = JOB_ENDED; |
| 5521 | } |
| 5522 | return job; |
| 5523 | } |
| 5524 | } |
| 5525 | return NULL; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5526 | } |
| 5527 | |
Bram Moolenaar | 3a117e1 | 2016-10-30 21:57:52 +0100 | [diff] [blame] | 5528 | /* |
| 5529 | * Send a (deadly) signal to "job". |
| 5530 | * Return FAIL if "how" is not a valid name. |
| 5531 | */ |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5532 | int |
| 5533 | mch_stop_job(job_T *job, char_u *how) |
| 5534 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5535 | int sig = -1; |
| 5536 | pid_t job_pid; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5537 | |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 5538 | if (*how == NUL || STRCMP(how, "term") == 0) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5539 | sig = SIGTERM; |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 5540 | else if (STRCMP(how, "hup") == 0) |
| 5541 | sig = SIGHUP; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5542 | else if (STRCMP(how, "quit") == 0) |
| 5543 | sig = SIGQUIT; |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 5544 | else if (STRCMP(how, "int") == 0) |
| 5545 | sig = SIGINT; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5546 | else if (STRCMP(how, "kill") == 0) |
| 5547 | sig = SIGKILL; |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 5548 | #ifdef SIGWINCH |
| 5549 | else if (STRCMP(how, "winch") == 0) |
| 5550 | sig = SIGWINCH; |
| 5551 | #endif |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5552 | else if (isdigit(*how)) |
| 5553 | sig = atoi((char *)how); |
| 5554 | else |
| 5555 | return FAIL; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5556 | |
Bram Moolenaar | 7280140 | 2016-02-09 11:37:50 +0100 | [diff] [blame] | 5557 | /* TODO: have an option to only kill the process, not the group? */ |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5558 | job_pid = job->jv_pid; |
Bram Moolenaar | 2fcf668 | 2017-03-11 20:03:42 +0100 | [diff] [blame] | 5559 | #ifdef HAVE_GETPGID |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5560 | if (job_pid == getpgid(job_pid)) |
| 5561 | job_pid = -job_pid; |
Bram Moolenaar | 2fcf668 | 2017-03-11 20:03:42 +0100 | [diff] [blame] | 5562 | #endif |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5563 | |
Bram Moolenaar | 61a6605 | 2017-07-22 18:39:00 +0200 | [diff] [blame] | 5564 | /* Never kill ourselves! */ |
| 5565 | if (job_pid != 0) |
| 5566 | kill(job_pid, sig); |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5567 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5568 | return OK; |
| 5569 | } |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5570 | |
| 5571 | /* |
| 5572 | * Clear the data related to "job". |
| 5573 | */ |
| 5574 | void |
| 5575 | mch_clear_job(job_T *job) |
| 5576 | { |
| 5577 | /* call waitpid because child process may become zombie */ |
| 5578 | # ifdef __NeXT__ |
Bram Moolenaar | 4ca812b | 2016-03-02 21:51:16 +0100 | [diff] [blame] | 5579 | (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0); |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5580 | # else |
Bram Moolenaar | 4ca812b | 2016-03-02 21:51:16 +0100 | [diff] [blame] | 5581 | (void)waitpid(job->jv_pid, NULL, WNOHANG); |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5582 | # endif |
| 5583 | } |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5584 | #endif |
| 5585 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5586 | /* |
| 5587 | * Check for CTRL-C typed by reading all available characters. |
| 5588 | * In cooked mode we should get SIGINT, no need to check. |
| 5589 | */ |
| 5590 | void |
Bram Moolenaar | b9c31e7 | 2016-09-29 15:18:57 +0200 | [diff] [blame] | 5591 | mch_breakcheck(int force) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5592 | { |
Bram Moolenaar | b9c31e7 | 2016-09-29 15:18:57 +0200 | [diff] [blame] | 5593 | if ((curr_tmode == TMODE_RAW || force) |
| 5594 | && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5595 | fill_input_buf(FALSE); |
| 5596 | } |
| 5597 | |
| 5598 | /* |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 5599 | * Wait "msec" msec until a character is available from the mouse, keyboard, |
| 5600 | * from inbuf[]. |
| 5601 | * "msec" == -1 will block forever. |
| 5602 | * Invokes timer callbacks when needed. |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5603 | * "interrupted" (if not NULL) is set to TRUE when no character is available |
| 5604 | * but something else needs to be done. |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 5605 | * Returns TRUE when a character is available. |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5606 | * When a GUI is being used, this will never get called -- webb |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5607 | */ |
| 5608 | static int |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5609 | WaitForChar(long msec, int *interrupted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5610 | { |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 5611 | #ifdef FEAT_TIMERS |
| 5612 | long due_time; |
| 5613 | long remaining = msec; |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 5614 | int tb_change_cnt = typebuf.tb_change_cnt; |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 5615 | |
| 5616 | /* When waiting very briefly don't trigger timers. */ |
| 5617 | if (msec >= 0 && msec < 10L) |
Bram Moolenaar | 8fdd721 | 2016-03-26 19:41:48 +0100 | [diff] [blame] | 5618 | return WaitForCharOrMouse(msec, NULL); |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 5619 | |
| 5620 | while (msec < 0 || remaining > 0) |
| 5621 | { |
| 5622 | /* Trigger timers and then get the time in msec until the next one is |
| 5623 | * due. Wait up to that time. */ |
| 5624 | due_time = check_due_timer(); |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 5625 | if (typebuf.tb_change_cnt != tb_change_cnt) |
| 5626 | { |
| 5627 | /* timer may have used feedkeys() */ |
| 5628 | return FALSE; |
| 5629 | } |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 5630 | if (due_time <= 0 || (msec > 0 && due_time > remaining)) |
| 5631 | due_time = remaining; |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5632 | if (WaitForCharOrMouse(due_time, interrupted)) |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 5633 | return TRUE; |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5634 | if (interrupted != NULL && *interrupted) |
Bram Moolenaar | 8fdd721 | 2016-03-26 19:41:48 +0100 | [diff] [blame] | 5635 | /* Nothing available, but need to return so that side effects get |
| 5636 | * handled, such as handling a message on a channel. */ |
| 5637 | return FALSE; |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 5638 | if (msec > 0) |
| 5639 | remaining -= due_time; |
| 5640 | } |
| 5641 | return FALSE; |
| 5642 | #else |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5643 | return WaitForCharOrMouse(msec, interrupted); |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 5644 | #endif |
| 5645 | } |
| 5646 | |
| 5647 | /* |
| 5648 | * Wait "msec" msec until a character is available from the mouse or keyboard |
| 5649 | * or from inbuf[]. |
| 5650 | * "msec" == -1 will block forever. |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5651 | * "interrupted" (if not NULL) is set to TRUE when no character is available |
| 5652 | * but something else needs to be done. |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 5653 | * When a GUI is being used, this will never get called -- webb |
| 5654 | */ |
| 5655 | static int |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5656 | WaitForCharOrMouse(long msec, int *interrupted) |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 5657 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5658 | #ifdef FEAT_MOUSE_GPM |
| 5659 | int gpm_process_wanted; |
| 5660 | #endif |
| 5661 | #ifdef FEAT_XCLIPBOARD |
| 5662 | int rest; |
| 5663 | #endif |
| 5664 | int avail; |
| 5665 | |
| 5666 | if (input_available()) /* something in inbuf[] */ |
| 5667 | return 1; |
| 5668 | |
| 5669 | #if defined(FEAT_MOUSE_DEC) |
| 5670 | /* May need to query the mouse position. */ |
| 5671 | if (WantQueryMouse) |
| 5672 | { |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 5673 | WantQueryMouse = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5674 | mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5); |
| 5675 | } |
| 5676 | #endif |
| 5677 | |
| 5678 | /* |
| 5679 | * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse |
| 5680 | * events. This is a bit complicated, because they might both be defined. |
| 5681 | */ |
| 5682 | #if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD) |
| 5683 | # ifdef FEAT_XCLIPBOARD |
| 5684 | rest = 0; |
| 5685 | if (do_xterm_trace()) |
| 5686 | rest = msec; |
| 5687 | # endif |
| 5688 | do |
| 5689 | { |
| 5690 | # ifdef FEAT_XCLIPBOARD |
| 5691 | if (rest != 0) |
| 5692 | { |
| 5693 | msec = XT_TRACE_DELAY; |
| 5694 | if (rest >= 0 && rest < XT_TRACE_DELAY) |
| 5695 | msec = rest; |
| 5696 | if (rest >= 0) |
| 5697 | rest -= msec; |
| 5698 | } |
| 5699 | # endif |
| 5700 | # ifdef FEAT_MOUSE_GPM |
| 5701 | gpm_process_wanted = 0; |
Bram Moolenaar | 8fdd721 | 2016-03-26 19:41:48 +0100 | [diff] [blame] | 5702 | avail = RealWaitForChar(read_cmd_fd, msec, |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5703 | &gpm_process_wanted, interrupted); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5704 | # else |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5705 | avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5706 | # endif |
| 5707 | if (!avail) |
| 5708 | { |
| 5709 | if (input_available()) |
| 5710 | return 1; |
| 5711 | # ifdef FEAT_XCLIPBOARD |
| 5712 | if (rest == 0 || !do_xterm_trace()) |
| 5713 | # endif |
| 5714 | break; |
| 5715 | } |
| 5716 | } |
| 5717 | while (FALSE |
| 5718 | # ifdef FEAT_MOUSE_GPM |
| 5719 | || (gpm_process_wanted && mch_gpm_process() == 0) |
| 5720 | # endif |
| 5721 | # ifdef FEAT_XCLIPBOARD |
| 5722 | || (!avail && rest != 0) |
| 5723 | # endif |
Bram Moolenaar | 8fdd721 | 2016-03-26 19:41:48 +0100 | [diff] [blame] | 5724 | ) |
| 5725 | ; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5726 | |
| 5727 | #else |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5728 | avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5729 | #endif |
| 5730 | return avail; |
| 5731 | } |
| 5732 | |
Bram Moolenaar | 4ffa070 | 2013-12-11 17:12:37 +0100 | [diff] [blame] | 5733 | #ifndef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5734 | /* |
| 5735 | * Wait "msec" msec until a character is available from file descriptor "fd". |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5736 | * "msec" == 0 will check for characters once. |
| 5737 | * "msec" == -1 will block until a character is available. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5738 | * When a GUI is being used, this will not be used for input -- webb |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5739 | * Or when a Linux GPM mouse event is waiting. |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 5740 | * Or when a clientserver message is on the queue. |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5741 | * "interrupted" (if not NULL) is set to TRUE when no character is available |
| 5742 | * but something else needs to be done. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5743 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5744 | #if defined(__BEOS__) |
| 5745 | int |
| 5746 | #else |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5747 | static int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5748 | #endif |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5749 | RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5750 | { |
| 5751 | int ret; |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5752 | int result; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5753 | #if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5754 | static int busy = FALSE; |
| 5755 | |
| 5756 | /* May retry getting characters after an event was handled. */ |
| 5757 | # define MAY_LOOP |
| 5758 | |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 5759 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5760 | /* Remember at what time we started, so that we know how much longer we |
| 5761 | * should wait after being interrupted. */ |
Bram Moolenaar | 76b6dfe | 2016-06-04 14:37:22 +0200 | [diff] [blame] | 5762 | long start_msec = msec; |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 5763 | ELAPSED_TYPE start_tv; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5764 | |
Bram Moolenaar | 76b6dfe | 2016-06-04 14:37:22 +0200 | [diff] [blame] | 5765 | if (msec > 0) |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 5766 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5767 | # endif |
| 5768 | |
| 5769 | /* Handle being called recursively. This may happen for the session |
| 5770 | * manager stuff, it may save the file, which does a breakcheck. */ |
| 5771 | if (busy) |
| 5772 | return 0; |
| 5773 | #endif |
| 5774 | |
| 5775 | #ifdef MAY_LOOP |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5776 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5777 | #endif |
| 5778 | { |
| 5779 | #ifdef MAY_LOOP |
| 5780 | int finished = TRUE; /* default is to 'loop' just once */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5781 | # ifdef FEAT_MZSCHEME |
| 5782 | int mzquantum_used = FALSE; |
| 5783 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5784 | #endif |
| 5785 | #ifndef HAVE_SELECT |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 5786 | /* each channel may use in, out and err */ |
| 5787 | struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5788 | int nfd; |
| 5789 | # ifdef FEAT_XCLIPBOARD |
| 5790 | int xterm_idx = -1; |
| 5791 | # endif |
| 5792 | # ifdef FEAT_MOUSE_GPM |
| 5793 | int gpm_idx = -1; |
| 5794 | # endif |
| 5795 | # ifdef USE_XSMP |
| 5796 | int xsmp_idx = -1; |
| 5797 | # endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5798 | int towait = (int)msec; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5799 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5800 | # ifdef FEAT_MZSCHEME |
| 5801 | mzvim_check_threads(); |
| 5802 | if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq)) |
| 5803 | { |
| 5804 | towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */ |
| 5805 | mzquantum_used = TRUE; |
| 5806 | } |
| 5807 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5808 | fds[0].fd = fd; |
| 5809 | fds[0].events = POLLIN; |
| 5810 | nfd = 1; |
| 5811 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5812 | # ifdef FEAT_XCLIPBOARD |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 5813 | may_restore_clipboard(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5814 | if (xterm_Shell != (Widget)0) |
| 5815 | { |
| 5816 | xterm_idx = nfd; |
| 5817 | fds[nfd].fd = ConnectionNumber(xterm_dpy); |
| 5818 | fds[nfd].events = POLLIN; |
| 5819 | nfd++; |
| 5820 | } |
| 5821 | # endif |
| 5822 | # ifdef FEAT_MOUSE_GPM |
| 5823 | if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0) |
| 5824 | { |
| 5825 | gpm_idx = nfd; |
| 5826 | fds[nfd].fd = gpm_fd; |
| 5827 | fds[nfd].events = POLLIN; |
| 5828 | nfd++; |
| 5829 | } |
| 5830 | # endif |
| 5831 | # ifdef USE_XSMP |
| 5832 | if (xsmp_icefd != -1) |
| 5833 | { |
| 5834 | xsmp_idx = nfd; |
| 5835 | fds[nfd].fd = xsmp_icefd; |
| 5836 | fds[nfd].events = POLLIN; |
| 5837 | nfd++; |
| 5838 | } |
| 5839 | # endif |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 5840 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 5841 | nfd = channel_poll_setup(nfd, &fds); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 5842 | #endif |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5843 | if (interrupted != NULL) |
| 5844 | *interrupted = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5845 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5846 | ret = poll(fds, nfd, towait); |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5847 | |
| 5848 | result = ret > 0 && (fds[0].revents & POLLIN); |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5849 | if (result == 0 && interrupted != NULL && ret > 0) |
| 5850 | *interrupted = TRUE; |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5851 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5852 | # ifdef FEAT_MZSCHEME |
| 5853 | if (ret == 0 && mzquantum_used) |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 5854 | /* MzThreads scheduling is required and timeout occurred */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5855 | finished = FALSE; |
| 5856 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5857 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5858 | # ifdef FEAT_XCLIPBOARD |
| 5859 | if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN)) |
| 5860 | { |
| 5861 | xterm_update(); /* Maybe we should hand out clipboard */ |
| 5862 | if (--ret == 0 && !input_available()) |
| 5863 | /* Try again */ |
| 5864 | finished = FALSE; |
| 5865 | } |
| 5866 | # endif |
| 5867 | # ifdef FEAT_MOUSE_GPM |
| 5868 | if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN)) |
| 5869 | { |
| 5870 | *check_for_gpm = 1; |
| 5871 | } |
| 5872 | # endif |
| 5873 | # ifdef USE_XSMP |
| 5874 | if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP))) |
| 5875 | { |
| 5876 | if (fds[xsmp_idx].revents & POLLIN) |
| 5877 | { |
| 5878 | busy = TRUE; |
| 5879 | xsmp_handle_requests(); |
| 5880 | busy = FALSE; |
| 5881 | } |
| 5882 | else if (fds[xsmp_idx].revents & POLLHUP) |
| 5883 | { |
| 5884 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 5885 | verb_msg((char_u *)_("XSMP lost ICE connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5886 | xsmp_close(); |
| 5887 | } |
| 5888 | if (--ret == 0) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5889 | finished = FALSE; /* Try again */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5890 | } |
| 5891 | # endif |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 5892 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 5893 | if (ret > 0) |
| 5894 | ret = channel_poll_check(ret, &fds); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 5895 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5896 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5897 | #else /* HAVE_SELECT */ |
| 5898 | |
| 5899 | struct timeval tv; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5900 | struct timeval *tvp; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 5901 | fd_set rfds, wfds, efds; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5902 | int maxfd; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5903 | long towait = msec; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5904 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5905 | # ifdef FEAT_MZSCHEME |
| 5906 | mzvim_check_threads(); |
| 5907 | if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq)) |
| 5908 | { |
| 5909 | towait = p_mzq; /* don't wait longer than 'mzquantum' */ |
| 5910 | mzquantum_used = TRUE; |
| 5911 | } |
| 5912 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5913 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5914 | if (towait >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5915 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5916 | tv.tv_sec = towait / 1000; |
| 5917 | tv.tv_usec = (towait % 1000) * (1000000/1000); |
| 5918 | tvp = &tv; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5919 | } |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5920 | else |
| 5921 | tvp = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5922 | |
| 5923 | /* |
| 5924 | * Select on ready for reading and exceptional condition (end of file). |
| 5925 | */ |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5926 | select_eintr: |
| 5927 | FD_ZERO(&rfds); |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 5928 | FD_ZERO(&wfds); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5929 | FD_ZERO(&efds); |
| 5930 | FD_SET(fd, &rfds); |
| 5931 | # if !defined(__QNX__) && !defined(__CYGWIN32__) |
| 5932 | /* For QNX select() always returns 1 if this is set. Why? */ |
| 5933 | FD_SET(fd, &efds); |
| 5934 | # endif |
| 5935 | maxfd = fd; |
| 5936 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5937 | # ifdef FEAT_XCLIPBOARD |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 5938 | may_restore_clipboard(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5939 | if (xterm_Shell != (Widget)0) |
| 5940 | { |
| 5941 | FD_SET(ConnectionNumber(xterm_dpy), &rfds); |
| 5942 | if (maxfd < ConnectionNumber(xterm_dpy)) |
| 5943 | maxfd = ConnectionNumber(xterm_dpy); |
Bram Moolenaar | dd82d69 | 2012-08-15 17:26:57 +0200 | [diff] [blame] | 5944 | |
| 5945 | /* An event may have already been read but not handled. In |
| 5946 | * particulary, XFlush may cause this. */ |
| 5947 | xterm_update(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5948 | } |
| 5949 | # endif |
| 5950 | # ifdef FEAT_MOUSE_GPM |
| 5951 | if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0) |
| 5952 | { |
| 5953 | FD_SET(gpm_fd, &rfds); |
| 5954 | FD_SET(gpm_fd, &efds); |
| 5955 | if (maxfd < gpm_fd) |
| 5956 | maxfd = gpm_fd; |
| 5957 | } |
| 5958 | # endif |
| 5959 | # ifdef USE_XSMP |
| 5960 | if (xsmp_icefd != -1) |
| 5961 | { |
| 5962 | FD_SET(xsmp_icefd, &rfds); |
| 5963 | FD_SET(xsmp_icefd, &efds); |
| 5964 | if (maxfd < xsmp_icefd) |
| 5965 | maxfd = xsmp_icefd; |
| 5966 | } |
| 5967 | # endif |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 5968 | # ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 5969 | maxfd = channel_select_setup(maxfd, &rfds, &wfds); |
Bram Moolenaar | dd82d69 | 2012-08-15 17:26:57 +0200 | [diff] [blame] | 5970 | # endif |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5971 | if (interrupted != NULL) |
| 5972 | *interrupted = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5973 | |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 5974 | ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp); |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5975 | result = ret > 0 && FD_ISSET(fd, &rfds); |
| 5976 | if (result) |
| 5977 | --ret; |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 5978 | else if (interrupted != NULL && ret > 0) |
| 5979 | *interrupted = TRUE; |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5980 | |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5981 | # ifdef EINTR |
| 5982 | if (ret == -1 && errno == EINTR) |
Bram Moolenaar | 2e7b1df | 2011-10-12 21:04:20 +0200 | [diff] [blame] | 5983 | { |
| 5984 | /* Check whether window has been resized, EINTR may be caused by |
| 5985 | * SIGWINCH. */ |
| 5986 | if (do_resize) |
| 5987 | handle_resize(); |
| 5988 | |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5989 | /* Interrupted by a signal, need to try again. We ignore msec |
| 5990 | * here, because we do want to check even after a timeout if |
| 5991 | * characters are available. Needed for reading output of an |
| 5992 | * external command after the process has finished. */ |
| 5993 | goto select_eintr; |
Bram Moolenaar | 2e7b1df | 2011-10-12 21:04:20 +0200 | [diff] [blame] | 5994 | } |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5995 | # endif |
Bram Moolenaar | 311d982 | 2007-02-27 15:48:28 +0000 | [diff] [blame] | 5996 | # ifdef __TANDEM |
| 5997 | if (ret == -1 && errno == ENOTSUP) |
| 5998 | { |
| 5999 | FD_ZERO(&rfds); |
| 6000 | FD_ZERO(&efds); |
| 6001 | ret = 0; |
| 6002 | } |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 6003 | # endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6004 | # ifdef FEAT_MZSCHEME |
| 6005 | if (ret == 0 && mzquantum_used) |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 6006 | /* loop if MzThreads must be scheduled and timeout occurred */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6007 | finished = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6008 | # endif |
| 6009 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6010 | # ifdef FEAT_XCLIPBOARD |
| 6011 | if (ret > 0 && xterm_Shell != (Widget)0 |
| 6012 | && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds)) |
| 6013 | { |
| 6014 | xterm_update(); /* Maybe we should hand out clipboard */ |
| 6015 | /* continue looping when we only got the X event and the input |
| 6016 | * buffer is empty */ |
| 6017 | if (--ret == 0 && !input_available()) |
| 6018 | { |
| 6019 | /* Try again */ |
| 6020 | finished = FALSE; |
| 6021 | } |
| 6022 | } |
| 6023 | # endif |
| 6024 | # ifdef FEAT_MOUSE_GPM |
| 6025 | if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0) |
| 6026 | { |
| 6027 | if (FD_ISSET(gpm_fd, &efds)) |
| 6028 | gpm_close(); |
| 6029 | else if (FD_ISSET(gpm_fd, &rfds)) |
| 6030 | *check_for_gpm = 1; |
| 6031 | } |
| 6032 | # endif |
| 6033 | # ifdef USE_XSMP |
| 6034 | if (ret > 0 && xsmp_icefd != -1) |
| 6035 | { |
| 6036 | if (FD_ISSET(xsmp_icefd, &efds)) |
| 6037 | { |
| 6038 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 6039 | verb_msg((char_u *)_("XSMP lost ICE connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6040 | xsmp_close(); |
| 6041 | if (--ret == 0) |
| 6042 | finished = FALSE; /* keep going if event was only one */ |
| 6043 | } |
| 6044 | else if (FD_ISSET(xsmp_icefd, &rfds)) |
| 6045 | { |
| 6046 | busy = TRUE; |
| 6047 | xsmp_handle_requests(); |
| 6048 | busy = FALSE; |
| 6049 | if (--ret == 0) |
| 6050 | finished = FALSE; /* keep going if event was only one */ |
| 6051 | } |
| 6052 | } |
| 6053 | # endif |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 6054 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 6055 | if (ret > 0) |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 6056 | ret = channel_select_check(ret, &rfds, &wfds); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 6057 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6058 | |
| 6059 | #endif /* HAVE_SELECT */ |
| 6060 | |
| 6061 | #ifdef MAY_LOOP |
| 6062 | if (finished || msec == 0) |
| 6063 | break; |
| 6064 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 6065 | # ifdef FEAT_CLIENTSERVER |
| 6066 | if (server_waiting()) |
| 6067 | break; |
| 6068 | # endif |
| 6069 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6070 | /* We're going to loop around again, find out for how long */ |
| 6071 | if (msec > 0) |
| 6072 | { |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 6073 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6074 | /* Compute remaining wait time. */ |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 6075 | msec = start_msec - ELAPSED_FUNC(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6076 | # else |
| 6077 | /* Guess we got interrupted halfway. */ |
| 6078 | msec = msec / 2; |
| 6079 | # endif |
| 6080 | if (msec <= 0) |
| 6081 | break; /* waited long enough */ |
| 6082 | } |
| 6083 | #endif |
| 6084 | } |
| 6085 | |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 6086 | return result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6089 | #ifndef NO_EXPANDPATH |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6090 | /* |
Bram Moolenaar | 0274363 | 2005-07-25 20:42:36 +0000 | [diff] [blame] | 6091 | * Expand a path into all matching files and/or directories. Handles "*", |
| 6092 | * "?", "[a-z]", "**", etc. |
| 6093 | * "path" has backslashes before chars that are not to be expanded. |
| 6094 | * Returns the number of matches found. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6095 | */ |
| 6096 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6097 | mch_expandpath( |
| 6098 | garray_T *gap, |
| 6099 | char_u *path, |
| 6100 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6101 | { |
Bram Moolenaar | 0274363 | 2005-07-25 20:42:36 +0000 | [diff] [blame] | 6102 | return unix_expandpath(gap, path, 0, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6103 | } |
| 6104 | #endif |
| 6105 | |
| 6106 | /* |
| 6107 | * mch_expand_wildcards() - this code does wild-card pattern matching using |
| 6108 | * the shell |
| 6109 | * |
| 6110 | * return OK for success, FAIL for error (you may lose some memory) and put |
| 6111 | * an error message in *file. |
| 6112 | * |
| 6113 | * num_pat is number of input patterns |
| 6114 | * pat is array of pointers to input patterns |
| 6115 | * num_file is pointer to number of matched file names |
| 6116 | * file is pointer to array of pointers to matched file names |
| 6117 | */ |
| 6118 | |
| 6119 | #ifndef SEEK_SET |
| 6120 | # define SEEK_SET 0 |
| 6121 | #endif |
| 6122 | #ifndef SEEK_END |
| 6123 | # define SEEK_END 2 |
| 6124 | #endif |
| 6125 | |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 6126 | #define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|" |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6127 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6128 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6129 | mch_expand_wildcards( |
| 6130 | int num_pat, |
| 6131 | char_u **pat, |
| 6132 | int *num_file, |
| 6133 | char_u ***file, |
| 6134 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6135 | { |
| 6136 | int i; |
| 6137 | size_t len; |
Bram Moolenaar | 85325f8 | 2017-03-30 21:18:45 +0200 | [diff] [blame] | 6138 | long llen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6139 | char_u *p; |
| 6140 | int dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6141 | |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6142 | /* |
| 6143 | * This is the non-OS/2 implementation (really Unix). |
| 6144 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6145 | int j; |
| 6146 | char_u *tempname; |
| 6147 | char_u *command; |
| 6148 | FILE *fd; |
| 6149 | char_u *buffer; |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6150 | #define STYLE_ECHO 0 /* use "echo", the default */ |
| 6151 | #define STYLE_GLOB 1 /* use "glob", for csh */ |
| 6152 | #define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */ |
| 6153 | #define STYLE_PRINT 3 /* use "print -N", for zsh */ |
| 6154 | #define STYLE_BT 4 /* `cmd` expansion, execute the pattern |
| 6155 | * directly */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6156 | int shell_style = STYLE_ECHO; |
| 6157 | int check_spaces; |
| 6158 | static int did_find_nul = FALSE; |
| 6159 | int ampersent = FALSE; |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6160 | /* vimglob() function to define for Posix shell */ |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6161 | 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] | 6162 | |
| 6163 | *num_file = 0; /* default: no files found */ |
| 6164 | *file = NULL; |
| 6165 | |
| 6166 | /* |
| 6167 | * If there are no wildcards, just copy the names to allocated memory. |
| 6168 | * Saves a lot of time, because we don't have to start a new shell. |
| 6169 | */ |
| 6170 | if (!have_wildcard(num_pat, pat)) |
| 6171 | return save_patterns(num_pat, pat, num_file, file); |
| 6172 | |
Bram Moolenaar | 0e634da | 2005-07-20 21:57:28 +0000 | [diff] [blame] | 6173 | # ifdef HAVE_SANDBOX |
| 6174 | /* Don't allow any shell command in the sandbox. */ |
| 6175 | if (sandbox != 0 && check_secure()) |
| 6176 | return FAIL; |
| 6177 | # endif |
| 6178 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6179 | /* |
| 6180 | * Don't allow the use of backticks in secure and restricted mode. |
| 6181 | */ |
Bram Moolenaar | 0e634da | 2005-07-20 21:57:28 +0000 | [diff] [blame] | 6182 | if (secure || restricted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6183 | for (i = 0; i < num_pat; ++i) |
| 6184 | if (vim_strchr(pat[i], '`') != NULL |
| 6185 | && (check_restricted() || check_secure())) |
| 6186 | return FAIL; |
| 6187 | |
| 6188 | /* |
| 6189 | * get a name for the temp file |
| 6190 | */ |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 6191 | if ((tempname = vim_tempname('o', FALSE)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6192 | { |
| 6193 | EMSG(_(e_notmp)); |
| 6194 | return FAIL; |
| 6195 | } |
| 6196 | |
| 6197 | /* |
| 6198 | * 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] | 6199 | * file. |
| 6200 | * STYLE_BT: NL separated |
| 6201 | * If expanding `cmd` execute it directly. |
| 6202 | * STYLE_GLOB: NUL separated |
| 6203 | * If we use *csh, "glob" will work better than "echo". |
| 6204 | * STYLE_PRINT: NL or NUL separated |
| 6205 | * If we use *zsh, "print -N" will work better than "glob". |
| 6206 | * STYLE_VIMGLOB: NL separated |
| 6207 | * If we use *sh*, we define "vimglob()". |
| 6208 | * STYLE_ECHO: space separated. |
| 6209 | * A shell we don't know, stay safe and use "echo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6210 | */ |
| 6211 | if (num_pat == 1 && *pat[0] == '`' |
| 6212 | && (len = STRLEN(pat[0])) > 2 |
| 6213 | && *(pat[0] + len - 1) == '`') |
| 6214 | shell_style = STYLE_BT; |
| 6215 | else if ((len = STRLEN(p_sh)) >= 3) |
| 6216 | { |
| 6217 | if (STRCMP(p_sh + len - 3, "csh") == 0) |
| 6218 | shell_style = STYLE_GLOB; |
| 6219 | else if (STRCMP(p_sh + len - 3, "zsh") == 0) |
| 6220 | shell_style = STYLE_PRINT; |
| 6221 | } |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6222 | if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh), |
| 6223 | "sh") != NULL) |
| 6224 | shell_style = STYLE_VIMGLOB; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6225 | |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6226 | /* Compute the length of the command. We need 2 extra bytes: for the |
| 6227 | * optional '&' and for the NUL. |
| 6228 | * Worst case: "unset nonomatch; print -N >" plus two is 29 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6229 | len = STRLEN(tempname) + 29; |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6230 | if (shell_style == STYLE_VIMGLOB) |
| 6231 | len += STRLEN(sh_vimglob_func); |
| 6232 | |
Bram Moolenaar | b23c338 | 2005-01-31 19:09:12 +0000 | [diff] [blame] | 6233 | for (i = 0; i < num_pat; ++i) |
| 6234 | { |
| 6235 | /* Count the length of the patterns in the same way as they are put in |
| 6236 | * "command" below. */ |
| 6237 | #ifdef USE_SYSTEM |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6238 | len += STRLEN(pat[i]) + 3; /* add space and two quotes */ |
Bram Moolenaar | b23c338 | 2005-01-31 19:09:12 +0000 | [diff] [blame] | 6239 | #else |
| 6240 | ++len; /* add space */ |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6241 | for (j = 0; pat[i][j] != NUL; ++j) |
| 6242 | { |
| 6243 | if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL) |
| 6244 | ++len; /* may add a backslash */ |
| 6245 | ++len; |
| 6246 | } |
Bram Moolenaar | b23c338 | 2005-01-31 19:09:12 +0000 | [diff] [blame] | 6247 | #endif |
| 6248 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6249 | command = alloc(len); |
| 6250 | if (command == NULL) |
| 6251 | { |
| 6252 | /* out of memory */ |
| 6253 | vim_free(tempname); |
| 6254 | return FAIL; |
| 6255 | } |
| 6256 | |
| 6257 | /* |
| 6258 | * Build the shell command: |
| 6259 | * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell |
| 6260 | * recognizes this). |
| 6261 | * - Add the shell command to print the expanded names. |
| 6262 | * - Add the temp file name. |
| 6263 | * - Add the file name patterns. |
| 6264 | */ |
| 6265 | if (shell_style == STYLE_BT) |
| 6266 | { |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6267 | /* change `command; command& ` to (command; command ) */ |
| 6268 | STRCPY(command, "("); |
| 6269 | STRCAT(command, pat[0] + 1); /* exclude first backtick */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6270 | p = command + STRLEN(command) - 1; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6271 | *p-- = ')'; /* remove last backtick */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6272 | while (p > command && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6273 | --p; |
| 6274 | if (*p == '&') /* remove trailing '&' */ |
| 6275 | { |
| 6276 | ampersent = TRUE; |
| 6277 | *p = ' '; |
| 6278 | } |
| 6279 | STRCAT(command, ">"); |
| 6280 | } |
| 6281 | else |
| 6282 | { |
| 6283 | if (flags & EW_NOTFOUND) |
| 6284 | STRCPY(command, "set nonomatch; "); |
| 6285 | else |
| 6286 | STRCPY(command, "unset nonomatch; "); |
| 6287 | if (shell_style == STYLE_GLOB) |
| 6288 | STRCAT(command, "glob >"); |
| 6289 | else if (shell_style == STYLE_PRINT) |
| 6290 | STRCAT(command, "print -N >"); |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6291 | else if (shell_style == STYLE_VIMGLOB) |
| 6292 | STRCAT(command, sh_vimglob_func); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6293 | else |
| 6294 | STRCAT(command, "echo >"); |
| 6295 | } |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6296 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6297 | STRCAT(command, tempname); |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6298 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6299 | if (shell_style != STYLE_BT) |
| 6300 | for (i = 0; i < num_pat; ++i) |
| 6301 | { |
| 6302 | /* When using system() always add extra quotes, because the shell |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6303 | * is started twice. Otherwise put a backslash before special |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 6304 | * characters, except inside ``. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6305 | #ifdef USE_SYSTEM |
| 6306 | STRCAT(command, " \""); |
| 6307 | STRCAT(command, pat[i]); |
| 6308 | STRCAT(command, "\""); |
| 6309 | #else |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6310 | int intick = FALSE; |
| 6311 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6312 | p = command + STRLEN(command); |
| 6313 | *p++ = ' '; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6314 | for (j = 0; pat[i][j] != NUL; ++j) |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6315 | { |
| 6316 | if (pat[i][j] == '`') |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6317 | intick = !intick; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6318 | else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL) |
| 6319 | { |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 6320 | /* Remove a backslash, take char literally. But keep |
Bram Moolenaar | 49315f6 | 2006-02-04 00:54:59 +0000 | [diff] [blame] | 6321 | * backslash inside backticks, before a special character |
| 6322 | * and before a backtick. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 6323 | if (intick |
Bram Moolenaar | 49315f6 | 2006-02-04 00:54:59 +0000 | [diff] [blame] | 6324 | || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL |
| 6325 | || pat[i][j + 1] == '`') |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 6326 | *p++ = '\\'; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 6327 | ++j; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6328 | } |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 6329 | else if (!intick |
| 6330 | && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$') |
| 6331 | && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL) |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6332 | /* Put a backslash before a special character, but not |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 6333 | * when inside ``. And not for $var when EW_KEEPDOLLAR is |
| 6334 | * set. */ |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6335 | *p++ = '\\'; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 6336 | |
| 6337 | /* Copy one character. */ |
| 6338 | *p++ = pat[i][j]; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6339 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6340 | *p = NUL; |
| 6341 | #endif |
| 6342 | } |
| 6343 | if (flags & EW_SILENT) |
| 6344 | show_shell_mess = FALSE; |
| 6345 | if (ampersent) |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6346 | STRCAT(command, "&"); /* put the '&' after the redirection */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6347 | |
| 6348 | /* |
| 6349 | * Using zsh -G: If a pattern has no matches, it is just deleted from |
| 6350 | * the argument list, otherwise zsh gives an error message and doesn't |
| 6351 | * expand any other pattern. |
| 6352 | */ |
| 6353 | if (shell_style == STYLE_PRINT) |
| 6354 | extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */ |
| 6355 | |
| 6356 | /* |
| 6357 | * If we use -f then shell variables set in .cshrc won't get expanded. |
| 6358 | * vi can do it, so we will too, but it is only necessary if there is a "$" |
| 6359 | * in one of the patterns, otherwise we can still use the fast option. |
| 6360 | */ |
| 6361 | else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat)) |
| 6362 | extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */ |
| 6363 | |
| 6364 | /* |
| 6365 | * execute the shell command |
| 6366 | */ |
| 6367 | i = call_shell(command, SHELL_EXPAND | SHELL_SILENT); |
| 6368 | |
| 6369 | /* When running in the background, give it some time to create the temp |
| 6370 | * file, but don't wait for it to finish. */ |
| 6371 | if (ampersent) |
| 6372 | mch_delay(10L, TRUE); |
| 6373 | |
| 6374 | extra_shell_arg = NULL; /* cleanup */ |
| 6375 | show_shell_mess = TRUE; |
| 6376 | vim_free(command); |
| 6377 | |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6378 | if (i != 0) /* mch_call_shell() failed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6379 | { |
| 6380 | mch_remove(tempname); |
| 6381 | vim_free(tempname); |
| 6382 | /* |
| 6383 | * With interactive completion, the error message is not printed. |
| 6384 | * However with USE_SYSTEM, I don't know how to turn off error messages |
| 6385 | * from the shell, so screen may still get messed up -- webb. |
| 6386 | */ |
| 6387 | #ifndef USE_SYSTEM |
| 6388 | if (!(flags & EW_SILENT)) |
| 6389 | #endif |
| 6390 | { |
| 6391 | redraw_later_clear(); /* probably messed up screen */ |
| 6392 | msg_putchar('\n'); /* clear bottom line quickly */ |
| 6393 | cmdline_row = Rows - 1; /* continue on last line */ |
| 6394 | #ifdef USE_SYSTEM |
| 6395 | if (!(flags & EW_SILENT)) |
| 6396 | #endif |
| 6397 | { |
| 6398 | MSG(_(e_wildexpand)); |
| 6399 | msg_start(); /* don't overwrite this message */ |
| 6400 | } |
| 6401 | } |
| 6402 | /* If a `cmd` expansion failed, don't list `cmd` as a match, even when |
| 6403 | * EW_NOTFOUND is given */ |
| 6404 | if (shell_style == STYLE_BT) |
| 6405 | return FAIL; |
| 6406 | goto notfound; |
| 6407 | } |
| 6408 | |
| 6409 | /* |
| 6410 | * read the names from the file into memory |
| 6411 | */ |
| 6412 | fd = fopen((char *)tempname, READBIN); |
| 6413 | if (fd == NULL) |
| 6414 | { |
| 6415 | /* Something went wrong, perhaps a file name with a special char. */ |
| 6416 | if (!(flags & EW_SILENT)) |
| 6417 | { |
| 6418 | MSG(_(e_wildexpand)); |
| 6419 | msg_start(); /* don't overwrite this message */ |
| 6420 | } |
| 6421 | vim_free(tempname); |
| 6422 | goto notfound; |
| 6423 | } |
| 6424 | fseek(fd, 0L, SEEK_END); |
Bram Moolenaar | 85325f8 | 2017-03-30 21:18:45 +0200 | [diff] [blame] | 6425 | llen = ftell(fd); /* get size of temp file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6426 | fseek(fd, 0L, SEEK_SET); |
Bram Moolenaar | 85325f8 | 2017-03-30 21:18:45 +0200 | [diff] [blame] | 6427 | if (llen < 0) |
| 6428 | /* just in case ftell() would fail */ |
| 6429 | buffer = NULL; |
| 6430 | else |
| 6431 | buffer = alloc(llen + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6432 | if (buffer == NULL) |
| 6433 | { |
| 6434 | /* out of memory */ |
| 6435 | mch_remove(tempname); |
| 6436 | vim_free(tempname); |
| 6437 | fclose(fd); |
| 6438 | return FAIL; |
| 6439 | } |
Bram Moolenaar | 85325f8 | 2017-03-30 21:18:45 +0200 | [diff] [blame] | 6440 | len = llen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6441 | i = fread((char *)buffer, 1, len, fd); |
| 6442 | fclose(fd); |
| 6443 | mch_remove(tempname); |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 6444 | if (i != (int)len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6445 | { |
| 6446 | /* unexpected read error */ |
| 6447 | EMSG2(_(e_notread), tempname); |
| 6448 | vim_free(tempname); |
| 6449 | vim_free(buffer); |
| 6450 | return FAIL; |
| 6451 | } |
| 6452 | vim_free(tempname); |
| 6453 | |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6454 | # if defined(__CYGWIN__) || defined(__CYGWIN32__) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6455 | /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */ |
| 6456 | p = buffer; |
Bram Moolenaar | fe17e76 | 2013-06-29 14:17:02 +0200 | [diff] [blame] | 6457 | for (i = 0; i < (int)len; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6458 | if (!(buffer[i] == CAR && buffer[i + 1] == NL)) |
| 6459 | *p++ = buffer[i]; |
| 6460 | len = p - buffer; |
| 6461 | # endif |
| 6462 | |
| 6463 | |
| 6464 | /* file names are separated with Space */ |
| 6465 | if (shell_style == STYLE_ECHO) |
| 6466 | { |
| 6467 | buffer[len] = '\n'; /* make sure the buffer ends in NL */ |
| 6468 | p = buffer; |
| 6469 | for (i = 0; *p != '\n'; ++i) /* count number of entries */ |
| 6470 | { |
| 6471 | while (*p != ' ' && *p != '\n') |
| 6472 | ++p; |
| 6473 | p = skipwhite(p); /* skip to next entry */ |
| 6474 | } |
| 6475 | } |
| 6476 | /* file names are separated with NL */ |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6477 | else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6478 | { |
| 6479 | buffer[len] = NUL; /* make sure the buffer ends in NUL */ |
| 6480 | p = buffer; |
| 6481 | for (i = 0; *p != NUL; ++i) /* count number of entries */ |
| 6482 | { |
| 6483 | while (*p != '\n' && *p != NUL) |
| 6484 | ++p; |
| 6485 | if (*p != NUL) |
| 6486 | ++p; |
| 6487 | p = skipwhite(p); /* skip leading white space */ |
| 6488 | } |
| 6489 | } |
| 6490 | /* file names are separated with NUL */ |
| 6491 | else |
| 6492 | { |
| 6493 | /* |
| 6494 | * Some versions of zsh use spaces instead of NULs to separate |
| 6495 | * results. Only do this when there is no NUL before the end of the |
| 6496 | * buffer, otherwise we would never be able to use file names with |
| 6497 | * embedded spaces when zsh does use NULs. |
| 6498 | * When we found a NUL once, we know zsh is OK, set did_find_nul and |
| 6499 | * don't check for spaces again. |
| 6500 | */ |
| 6501 | check_spaces = FALSE; |
| 6502 | if (shell_style == STYLE_PRINT && !did_find_nul) |
| 6503 | { |
| 6504 | /* 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] | 6505 | buffer[len] = NUL; |
Bram Moolenaar | b011af9 | 2013-12-11 13:21:51 +0100 | [diff] [blame] | 6506 | if (len && (int)STRLEN(buffer) < (int)len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6507 | did_find_nul = TRUE; |
| 6508 | else |
| 6509 | check_spaces = TRUE; |
| 6510 | } |
| 6511 | |
| 6512 | /* |
| 6513 | * Make sure the buffer ends with a NUL. For STYLE_PRINT there |
| 6514 | * already is one, for STYLE_GLOB it needs to be added. |
| 6515 | */ |
| 6516 | if (len && buffer[len - 1] == NUL) |
| 6517 | --len; |
| 6518 | else |
| 6519 | buffer[len] = NUL; |
| 6520 | i = 0; |
| 6521 | for (p = buffer; p < buffer + len; ++p) |
| 6522 | if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */ |
| 6523 | { |
| 6524 | ++i; |
| 6525 | *p = NUL; |
| 6526 | } |
| 6527 | if (len) |
| 6528 | ++i; /* count last entry */ |
| 6529 | } |
| 6530 | if (i == 0) |
| 6531 | { |
| 6532 | /* |
| 6533 | * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I". |
| 6534 | * /bin/sh will happily expand it to nothing rather than returning an |
| 6535 | * error; and hey, it's good to check anyway -- webb. |
| 6536 | */ |
| 6537 | vim_free(buffer); |
| 6538 | goto notfound; |
| 6539 | } |
| 6540 | *num_file = i; |
| 6541 | *file = (char_u **)alloc(sizeof(char_u *) * i); |
| 6542 | if (*file == NULL) |
| 6543 | { |
| 6544 | /* out of memory */ |
| 6545 | vim_free(buffer); |
| 6546 | return FAIL; |
| 6547 | } |
| 6548 | |
| 6549 | /* |
| 6550 | * Isolate the individual file names. |
| 6551 | */ |
| 6552 | p = buffer; |
| 6553 | for (i = 0; i < *num_file; ++i) |
| 6554 | { |
| 6555 | (*file)[i] = p; |
| 6556 | /* Space or NL separates */ |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6557 | if (shell_style == STYLE_ECHO || shell_style == STYLE_BT |
| 6558 | || shell_style == STYLE_VIMGLOB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6559 | { |
Bram Moolenaar | 49315f6 | 2006-02-04 00:54:59 +0000 | [diff] [blame] | 6560 | while (!(shell_style == STYLE_ECHO && *p == ' ') |
| 6561 | && *p != '\n' && *p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6562 | ++p; |
| 6563 | if (p == buffer + len) /* last entry */ |
| 6564 | *p = NUL; |
| 6565 | else |
| 6566 | { |
| 6567 | *p++ = NUL; |
| 6568 | p = skipwhite(p); /* skip to next entry */ |
| 6569 | } |
| 6570 | } |
| 6571 | else /* NUL separates */ |
| 6572 | { |
| 6573 | while (*p && p < buffer + len) /* skip entry */ |
| 6574 | ++p; |
| 6575 | ++p; /* skip NUL */ |
| 6576 | } |
| 6577 | } |
| 6578 | |
| 6579 | /* |
| 6580 | * Move the file names to allocated memory. |
| 6581 | */ |
| 6582 | for (j = 0, i = 0; i < *num_file; ++i) |
| 6583 | { |
| 6584 | /* Require the files to exist. Helps when using /bin/sh */ |
| 6585 | if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0) |
| 6586 | continue; |
| 6587 | |
| 6588 | /* check if this entry should be included */ |
| 6589 | dir = (mch_isdir((*file)[i])); |
| 6590 | if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE))) |
| 6591 | continue; |
| 6592 | |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 6593 | /* Skip files that are not executable if we check for that. */ |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 6594 | if (!dir && (flags & EW_EXEC) |
| 6595 | && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD))) |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 6596 | continue; |
| 6597 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6598 | p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir)); |
| 6599 | if (p) |
| 6600 | { |
| 6601 | STRCPY(p, (*file)[i]); |
| 6602 | if (dir) |
Bram Moolenaar | b238909 | 2008-01-03 17:56:04 +0000 | [diff] [blame] | 6603 | add_pathsep(p); /* add '/' to a directory name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6604 | (*file)[j++] = p; |
| 6605 | } |
| 6606 | } |
| 6607 | vim_free(buffer); |
| 6608 | *num_file = j; |
| 6609 | |
| 6610 | if (*num_file == 0) /* rejected all entries */ |
| 6611 | { |
| 6612 | vim_free(*file); |
| 6613 | *file = NULL; |
| 6614 | goto notfound; |
| 6615 | } |
| 6616 | |
| 6617 | return OK; |
| 6618 | |
| 6619 | notfound: |
| 6620 | if (flags & EW_NOTFOUND) |
| 6621 | return save_patterns(num_pat, pat, num_file, file); |
| 6622 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6623 | } |
| 6624 | |
| 6625 | #endif /* VMS */ |
| 6626 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6627 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6628 | save_patterns( |
| 6629 | int num_pat, |
| 6630 | char_u **pat, |
| 6631 | int *num_file, |
| 6632 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6633 | { |
| 6634 | int i; |
Bram Moolenaar | d8b0273 | 2005-01-14 21:48:43 +0000 | [diff] [blame] | 6635 | char_u *s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6636 | |
| 6637 | *file = (char_u **)alloc(num_pat * sizeof(char_u *)); |
| 6638 | if (*file == NULL) |
| 6639 | return FAIL; |
| 6640 | for (i = 0; i < num_pat; i++) |
Bram Moolenaar | d8b0273 | 2005-01-14 21:48:43 +0000 | [diff] [blame] | 6641 | { |
| 6642 | s = vim_strsave(pat[i]); |
| 6643 | if (s != NULL) |
| 6644 | /* Be compatible with expand_filename(): halve the number of |
| 6645 | * backslashes. */ |
| 6646 | backslash_halve(s); |
| 6647 | (*file)[i] = s; |
| 6648 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6649 | *num_file = num_pat; |
| 6650 | return OK; |
| 6651 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6652 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6653 | /* |
| 6654 | * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can |
| 6655 | * expand. |
| 6656 | */ |
| 6657 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6658 | mch_has_exp_wildcard(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6659 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 6660 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6661 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6662 | if (*p == '\\' && p[1] != NUL) |
| 6663 | ++p; |
| 6664 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6665 | if (vim_strchr((char_u *) |
| 6666 | #ifdef VMS |
| 6667 | "*?%" |
| 6668 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6669 | "*?[{'" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6670 | #endif |
| 6671 | , *p) != NULL) |
| 6672 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6673 | } |
| 6674 | return FALSE; |
| 6675 | } |
| 6676 | |
| 6677 | /* |
| 6678 | * Return TRUE if the string "p" contains a wildcard. |
| 6679 | * Don't recognize '~' at the end as a wildcard. |
| 6680 | */ |
| 6681 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6682 | mch_has_wildcard(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6683 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 6684 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6685 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6686 | if (*p == '\\' && p[1] != NUL) |
| 6687 | ++p; |
| 6688 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6689 | if (vim_strchr((char_u *) |
| 6690 | #ifdef VMS |
| 6691 | "*?%$" |
| 6692 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6693 | "*?[{`'$" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6694 | #endif |
| 6695 | , *p) != NULL |
| 6696 | || (*p == '~' && p[1] != NUL)) |
| 6697 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6698 | } |
| 6699 | return FALSE; |
| 6700 | } |
| 6701 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6702 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6703 | have_wildcard(int num, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6704 | { |
| 6705 | int i; |
| 6706 | |
| 6707 | for (i = 0; i < num; i++) |
| 6708 | if (mch_has_wildcard(file[i])) |
| 6709 | return 1; |
| 6710 | return 0; |
| 6711 | } |
| 6712 | |
| 6713 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6714 | have_dollars(int num, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6715 | { |
| 6716 | int i; |
| 6717 | |
| 6718 | for (i = 0; i < num; i++) |
| 6719 | if (vim_strchr(file[i], '$') != NULL) |
| 6720 | return TRUE; |
| 6721 | return FALSE; |
| 6722 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6723 | |
Bram Moolenaar | fdcc9af | 2016-02-29 12:52:39 +0100 | [diff] [blame] | 6724 | #if !defined(HAVE_RENAME) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6725 | /* |
| 6726 | * Scaled-down version of rename(), which is missing in Xenix. |
| 6727 | * This version can only move regular files and will fail if the |
| 6728 | * destination exists. |
| 6729 | */ |
| 6730 | int |
Bram Moolenaar | fdcc9af | 2016-02-29 12:52:39 +0100 | [diff] [blame] | 6731 | mch_rename(const char *src, const char *dest) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6732 | { |
| 6733 | struct stat st; |
| 6734 | |
| 6735 | if (stat(dest, &st) >= 0) /* fail if destination exists */ |
| 6736 | return -1; |
| 6737 | if (link(src, dest) != 0) /* link file to new name */ |
| 6738 | return -1; |
| 6739 | if (mch_remove(src) == 0) /* delete link to old name */ |
| 6740 | return 0; |
| 6741 | return -1; |
| 6742 | } |
| 6743 | #endif /* !HAVE_RENAME */ |
| 6744 | |
| 6745 | #ifdef FEAT_MOUSE_GPM |
| 6746 | /* |
| 6747 | * Initializes connection with gpm (if it isn't already opened) |
| 6748 | * Return 1 if succeeded (or connection already opened), 0 if failed |
| 6749 | */ |
| 6750 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6751 | gpm_open(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6752 | { |
| 6753 | static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */ |
| 6754 | |
| 6755 | if (!gpm_flag) |
| 6756 | { |
| 6757 | gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN); |
| 6758 | gpm_connect.defaultMask = ~GPM_HARD; |
| 6759 | /* Default handling for mouse move*/ |
| 6760 | gpm_connect.minMod = 0; /* Handle any modifier keys */ |
| 6761 | gpm_connect.maxMod = 0xffff; |
| 6762 | if (Gpm_Open(&gpm_connect, 0) > 0) |
| 6763 | { |
| 6764 | /* gpm library tries to handling TSTP causes |
| 6765 | * problems. Anyways, we close connection to Gpm whenever |
| 6766 | * we are going to suspend or starting an external process |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 6767 | * so we shouldn't have problem with this |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6768 | */ |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 6769 | # ifdef SIGTSTP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6770 | signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL); |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 6771 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6772 | return 1; /* succeed */ |
| 6773 | } |
| 6774 | if (gpm_fd == -2) |
| 6775 | Gpm_Close(); /* We don't want to talk to xterm via gpm */ |
| 6776 | return 0; |
| 6777 | } |
| 6778 | return 1; /* already open */ |
| 6779 | } |
| 6780 | |
| 6781 | /* |
| 6782 | * Closes connection to gpm |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6783 | */ |
| 6784 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6785 | gpm_close(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6786 | { |
| 6787 | if (gpm_flag && gpm_fd >= 0) /* if Open */ |
| 6788 | Gpm_Close(); |
| 6789 | } |
| 6790 | |
| 6791 | /* Reads gpm event and adds special keys to input buf. Returns length of |
| 6792 | * generated key sequence. |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 6793 | * This function is styled after gui_send_mouse_event(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6794 | */ |
| 6795 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6796 | mch_gpm_process(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6797 | { |
| 6798 | int button; |
| 6799 | static Gpm_Event gpm_event; |
| 6800 | char_u string[6]; |
| 6801 | int_u vim_modifiers; |
| 6802 | int row,col; |
| 6803 | unsigned char buttons_mask; |
| 6804 | unsigned char gpm_modifiers; |
| 6805 | static unsigned char old_buttons = 0; |
| 6806 | |
| 6807 | Gpm_GetEvent(&gpm_event); |
| 6808 | |
| 6809 | #ifdef FEAT_GUI |
| 6810 | /* Don't put events in the input queue now. */ |
| 6811 | if (hold_gui_events) |
| 6812 | return 0; |
| 6813 | #endif |
| 6814 | |
| 6815 | row = gpm_event.y - 1; |
| 6816 | col = gpm_event.x - 1; |
| 6817 | |
| 6818 | string[0] = ESC; /* Our termcode */ |
| 6819 | string[1] = 'M'; |
| 6820 | string[2] = 'G'; |
| 6821 | switch (GPM_BARE_EVENTS(gpm_event.type)) |
| 6822 | { |
| 6823 | case GPM_DRAG: |
| 6824 | string[3] = MOUSE_DRAG; |
| 6825 | break; |
| 6826 | case GPM_DOWN: |
| 6827 | buttons_mask = gpm_event.buttons & ~old_buttons; |
| 6828 | old_buttons = gpm_event.buttons; |
| 6829 | switch (buttons_mask) |
| 6830 | { |
| 6831 | case GPM_B_LEFT: |
| 6832 | button = MOUSE_LEFT; |
| 6833 | break; |
| 6834 | case GPM_B_MIDDLE: |
| 6835 | button = MOUSE_MIDDLE; |
| 6836 | break; |
| 6837 | case GPM_B_RIGHT: |
| 6838 | button = MOUSE_RIGHT; |
| 6839 | break; |
| 6840 | default: |
| 6841 | return 0; |
| 6842 | /*Don't know what to do. Can more than one button be |
| 6843 | * reported in one event? */ |
| 6844 | } |
| 6845 | string[3] = (char_u)(button | 0x20); |
| 6846 | SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1); |
| 6847 | break; |
| 6848 | case GPM_UP: |
| 6849 | string[3] = MOUSE_RELEASE; |
| 6850 | old_buttons &= ~gpm_event.buttons; |
| 6851 | break; |
| 6852 | default: |
| 6853 | return 0; |
| 6854 | } |
| 6855 | /*This code is based on gui_x11_mouse_cb in gui_x11.c */ |
| 6856 | gpm_modifiers = gpm_event.modifiers; |
| 6857 | vim_modifiers = 0x0; |
| 6858 | /* I ignore capslock stats. Aren't we all just hate capslock mixing with |
| 6859 | * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and |
| 6860 | * K_CAPSSHIFT is defined 8, so it probably isn't even reported |
| 6861 | */ |
| 6862 | if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL))) |
| 6863 | vim_modifiers |= MOUSE_SHIFT; |
| 6864 | |
| 6865 | if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL))) |
| 6866 | vim_modifiers |= MOUSE_CTRL; |
| 6867 | if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR))) |
| 6868 | vim_modifiers |= MOUSE_ALT; |
| 6869 | string[3] |= vim_modifiers; |
| 6870 | string[4] = (char_u)(col + ' ' + 1); |
| 6871 | string[5] = (char_u)(row + ' ' + 1); |
| 6872 | add_to_input_buf(string, 6); |
| 6873 | return 6; |
| 6874 | } |
| 6875 | #endif /* FEAT_MOUSE_GPM */ |
| 6876 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6877 | #ifdef FEAT_SYSMOUSE |
| 6878 | /* |
| 6879 | * Initialize connection with sysmouse. |
| 6880 | * Let virtual console inform us with SIGUSR2 for pending sysmouse |
| 6881 | * output, any sysmouse output than will be processed via sig_sysmouse(). |
| 6882 | * Return OK if succeeded, FAIL if failed. |
| 6883 | */ |
| 6884 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6885 | sysmouse_open(void) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6886 | { |
| 6887 | struct mouse_info mouse; |
| 6888 | |
| 6889 | mouse.operation = MOUSE_MODE; |
| 6890 | mouse.u.mode.mode = 0; |
| 6891 | mouse.u.mode.signal = SIGUSR2; |
| 6892 | if (ioctl(1, CONS_MOUSECTL, &mouse) != -1) |
| 6893 | { |
| 6894 | signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse); |
| 6895 | mouse.operation = MOUSE_SHOW; |
| 6896 | ioctl(1, CONS_MOUSECTL, &mouse); |
| 6897 | return OK; |
| 6898 | } |
| 6899 | return FAIL; |
| 6900 | } |
| 6901 | |
| 6902 | /* |
| 6903 | * Stop processing SIGUSR2 signals, and also make sure that |
| 6904 | * virtual console do not send us any sysmouse related signal. |
| 6905 | */ |
| 6906 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6907 | sysmouse_close(void) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6908 | { |
| 6909 | struct mouse_info mouse; |
| 6910 | |
| 6911 | signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL); |
| 6912 | mouse.operation = MOUSE_MODE; |
| 6913 | mouse.u.mode.mode = 0; |
| 6914 | mouse.u.mode.signal = 0; |
| 6915 | ioctl(1, CONS_MOUSECTL, &mouse); |
| 6916 | } |
| 6917 | |
| 6918 | /* |
| 6919 | * Gets info from sysmouse and adds special keys to input buf. |
| 6920 | */ |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6921 | static RETSIGTYPE |
| 6922 | sig_sysmouse SIGDEFARG(sigarg) |
| 6923 | { |
| 6924 | struct mouse_info mouse; |
| 6925 | struct video_info video; |
| 6926 | char_u string[6]; |
| 6927 | int row, col; |
| 6928 | int button; |
| 6929 | int buttons; |
| 6930 | static int oldbuttons = 0; |
| 6931 | |
| 6932 | #ifdef FEAT_GUI |
| 6933 | /* Don't put events in the input queue now. */ |
| 6934 | if (hold_gui_events) |
| 6935 | return; |
| 6936 | #endif |
| 6937 | |
| 6938 | mouse.operation = MOUSE_GETINFO; |
| 6939 | if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1 |
| 6940 | && ioctl(1, FBIO_MODEINFO, &video) != -1 |
| 6941 | && ioctl(1, CONS_MOUSECTL, &mouse) != -1 |
| 6942 | && video.vi_cheight > 0 && video.vi_cwidth > 0) |
| 6943 | { |
| 6944 | row = mouse.u.data.y / video.vi_cheight; |
| 6945 | col = mouse.u.data.x / video.vi_cwidth; |
| 6946 | buttons = mouse.u.data.buttons; |
| 6947 | string[0] = ESC; /* Our termcode */ |
| 6948 | string[1] = 'M'; |
| 6949 | string[2] = 'S'; |
| 6950 | if (oldbuttons == buttons && buttons != 0) |
| 6951 | { |
| 6952 | button = MOUSE_DRAG; |
| 6953 | } |
| 6954 | else |
| 6955 | { |
| 6956 | switch (buttons) |
| 6957 | { |
| 6958 | case 0: |
| 6959 | button = MOUSE_RELEASE; |
| 6960 | break; |
| 6961 | case 1: |
| 6962 | button = MOUSE_LEFT; |
| 6963 | break; |
| 6964 | case 2: |
| 6965 | button = MOUSE_MIDDLE; |
| 6966 | break; |
| 6967 | case 4: |
| 6968 | button = MOUSE_RIGHT; |
| 6969 | break; |
| 6970 | default: |
| 6971 | return; |
| 6972 | } |
| 6973 | oldbuttons = buttons; |
| 6974 | } |
| 6975 | string[3] = (char_u)(button); |
| 6976 | string[4] = (char_u)(col + ' ' + 1); |
| 6977 | string[5] = (char_u)(row + ' ' + 1); |
| 6978 | add_to_input_buf(string, 6); |
| 6979 | } |
| 6980 | return; |
| 6981 | } |
| 6982 | #endif /* FEAT_SYSMOUSE */ |
| 6983 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6984 | #if defined(FEAT_LIBCALL) || defined(PROTO) |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 6985 | typedef char_u * (*STRPROCSTR)(char_u *); |
| 6986 | typedef char_u * (*INTPROCSTR)(int); |
| 6987 | typedef int (*STRPROCINT)(char_u *); |
| 6988 | typedef int (*INTPROCINT)(int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6989 | |
| 6990 | /* |
| 6991 | * Call a DLL routine which takes either a string or int param |
| 6992 | * and returns an allocated string. |
| 6993 | */ |
| 6994 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6995 | mch_libcall( |
| 6996 | char_u *libname, |
| 6997 | char_u *funcname, |
| 6998 | char_u *argstring, /* NULL when using a argint */ |
| 6999 | int argint, |
| 7000 | char_u **string_result,/* NULL when using number_result */ |
| 7001 | int *number_result) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7002 | { |
| 7003 | # if defined(USE_DLOPEN) |
| 7004 | void *hinstLib; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7005 | char *dlerr = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7006 | # else |
| 7007 | shl_t hinstLib; |
| 7008 | # endif |
| 7009 | STRPROCSTR ProcAdd; |
| 7010 | INTPROCSTR ProcAddI; |
| 7011 | char_u *retval_str = NULL; |
| 7012 | int retval_int = 0; |
| 7013 | int success = FALSE; |
| 7014 | |
Bram Moolenaar | b39ef12 | 2006-06-22 16:19:31 +0000 | [diff] [blame] | 7015 | /* |
| 7016 | * Get a handle to the DLL module. |
| 7017 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7018 | # if defined(USE_DLOPEN) |
Bram Moolenaar | b39ef12 | 2006-06-22 16:19:31 +0000 | [diff] [blame] | 7019 | /* First clear any error, it's not cleared by the dlopen() call. */ |
| 7020 | (void)dlerror(); |
| 7021 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7022 | hinstLib = dlopen((char *)libname, RTLD_LAZY |
| 7023 | # ifdef RTLD_LOCAL |
| 7024 | | RTLD_LOCAL |
| 7025 | # endif |
| 7026 | ); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7027 | if (hinstLib == NULL) |
| 7028 | { |
| 7029 | /* "dlerr" must be used before dlclose() */ |
| 7030 | dlerr = (char *)dlerror(); |
| 7031 | if (dlerr != NULL) |
| 7032 | EMSG2(_("dlerror = \"%s\""), dlerr); |
| 7033 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7034 | # else |
| 7035 | hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L); |
| 7036 | # endif |
| 7037 | |
| 7038 | /* If the handle is valid, try to get the function address. */ |
| 7039 | if (hinstLib != NULL) |
| 7040 | { |
| 7041 | # ifdef HAVE_SETJMP_H |
| 7042 | /* |
| 7043 | * Catch a crash when calling the library function. For example when |
| 7044 | * using a number where a string pointer is expected. |
| 7045 | */ |
| 7046 | mch_startjmp(); |
| 7047 | if (SETJMP(lc_jump_env) != 0) |
| 7048 | { |
| 7049 | success = FALSE; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 7050 | # if defined(USE_DLOPEN) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7051 | dlerr = NULL; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 7052 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7053 | mch_didjmp(); |
| 7054 | } |
| 7055 | else |
| 7056 | # endif |
| 7057 | { |
| 7058 | retval_str = NULL; |
| 7059 | retval_int = 0; |
| 7060 | |
| 7061 | if (argstring != NULL) |
| 7062 | { |
| 7063 | # if defined(USE_DLOPEN) |
Bram Moolenaar | 6d721c7 | 2017-01-17 16:56:28 +0100 | [diff] [blame] | 7064 | *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7065 | dlerr = (char *)dlerror(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7066 | # else |
| 7067 | if (shl_findsym(&hinstLib, (const char *)funcname, |
| 7068 | TYPE_PROCEDURE, (void *)&ProcAdd) < 0) |
| 7069 | ProcAdd = NULL; |
| 7070 | # endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7071 | if ((success = (ProcAdd != NULL |
| 7072 | # if defined(USE_DLOPEN) |
| 7073 | && dlerr == NULL |
| 7074 | # endif |
| 7075 | ))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7076 | { |
| 7077 | if (string_result == NULL) |
| 7078 | retval_int = ((STRPROCINT)ProcAdd)(argstring); |
| 7079 | else |
| 7080 | retval_str = (ProcAdd)(argstring); |
| 7081 | } |
| 7082 | } |
| 7083 | else |
| 7084 | { |
| 7085 | # if defined(USE_DLOPEN) |
Bram Moolenaar | 6d721c7 | 2017-01-17 16:56:28 +0100 | [diff] [blame] | 7086 | *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7087 | dlerr = (char *)dlerror(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7088 | # else |
| 7089 | if (shl_findsym(&hinstLib, (const char *)funcname, |
| 7090 | TYPE_PROCEDURE, (void *)&ProcAddI) < 0) |
| 7091 | ProcAddI = NULL; |
| 7092 | # endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7093 | if ((success = (ProcAddI != NULL |
| 7094 | # if defined(USE_DLOPEN) |
| 7095 | && dlerr == NULL |
| 7096 | # endif |
| 7097 | ))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7098 | { |
| 7099 | if (string_result == NULL) |
| 7100 | retval_int = ((INTPROCINT)ProcAddI)(argint); |
| 7101 | else |
| 7102 | retval_str = (ProcAddI)(argint); |
| 7103 | } |
| 7104 | } |
| 7105 | |
| 7106 | /* Save the string before we free the library. */ |
| 7107 | /* Assume that a "1" or "-1" result is an illegal pointer. */ |
| 7108 | if (string_result == NULL) |
| 7109 | *number_result = retval_int; |
| 7110 | else if (retval_str != NULL |
| 7111 | && retval_str != (char_u *)1 |
| 7112 | && retval_str != (char_u *)-1) |
| 7113 | *string_result = vim_strsave(retval_str); |
| 7114 | } |
| 7115 | |
| 7116 | # ifdef HAVE_SETJMP_H |
| 7117 | mch_endjmp(); |
| 7118 | # ifdef SIGHASARG |
| 7119 | if (lc_signal != 0) |
| 7120 | { |
| 7121 | int i; |
| 7122 | |
| 7123 | /* try to find the name of this signal */ |
| 7124 | for (i = 0; signal_info[i].sig != -1; i++) |
| 7125 | if (lc_signal == signal_info[i].sig) |
| 7126 | break; |
| 7127 | EMSG2("E368: got SIG%s in libcall()", signal_info[i].name); |
| 7128 | } |
| 7129 | # endif |
| 7130 | # endif |
| 7131 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7132 | # if defined(USE_DLOPEN) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7133 | /* "dlerr" must be used before dlclose() */ |
| 7134 | if (dlerr != NULL) |
| 7135 | EMSG2(_("dlerror = \"%s\""), dlerr); |
| 7136 | |
| 7137 | /* Free the DLL module. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7138 | (void)dlclose(hinstLib); |
| 7139 | # else |
| 7140 | (void)shl_unload(hinstLib); |
| 7141 | # endif |
| 7142 | } |
| 7143 | |
| 7144 | if (!success) |
| 7145 | { |
| 7146 | EMSG2(_(e_libcall), funcname); |
| 7147 | return FAIL; |
| 7148 | } |
| 7149 | |
| 7150 | return OK; |
| 7151 | } |
| 7152 | #endif |
| 7153 | |
| 7154 | #if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO) |
| 7155 | static int xterm_trace = -1; /* default: disabled */ |
| 7156 | static int xterm_button; |
| 7157 | |
| 7158 | /* |
| 7159 | * Setup a dummy window for X selections in a terminal. |
| 7160 | */ |
| 7161 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7162 | setup_term_clip(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7163 | { |
| 7164 | int z = 0; |
| 7165 | char *strp = ""; |
| 7166 | Widget AppShell; |
| 7167 | |
| 7168 | if (!x_connect_to_server()) |
| 7169 | return; |
| 7170 | |
| 7171 | open_app_context(); |
| 7172 | if (app_context != NULL && xterm_Shell == (Widget)0) |
| 7173 | { |
| 7174 | int (*oldhandler)(); |
| 7175 | #if defined(HAVE_SETJMP_H) |
| 7176 | int (*oldIOhandler)(); |
| 7177 | #endif |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 7178 | # ifdef ELAPSED_FUNC |
| 7179 | ELAPSED_TYPE start_tv; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7180 | |
| 7181 | if (p_verbose > 0) |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 7182 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7183 | # endif |
| 7184 | |
| 7185 | /* Ignore X errors while opening the display */ |
| 7186 | oldhandler = XSetErrorHandler(x_error_check); |
| 7187 | |
| 7188 | #if defined(HAVE_SETJMP_H) |
| 7189 | /* Ignore X IO errors while opening the display */ |
| 7190 | oldIOhandler = XSetIOErrorHandler(x_IOerror_check); |
| 7191 | mch_startjmp(); |
| 7192 | if (SETJMP(lc_jump_env) != 0) |
| 7193 | { |
| 7194 | mch_didjmp(); |
| 7195 | xterm_dpy = NULL; |
| 7196 | } |
| 7197 | else |
| 7198 | #endif |
| 7199 | { |
| 7200 | xterm_dpy = XtOpenDisplay(app_context, xterm_display, |
| 7201 | "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp); |
| 7202 | #if defined(HAVE_SETJMP_H) |
| 7203 | mch_endjmp(); |
| 7204 | #endif |
| 7205 | } |
| 7206 | |
| 7207 | #if defined(HAVE_SETJMP_H) |
| 7208 | /* Now handle X IO errors normally. */ |
| 7209 | (void)XSetIOErrorHandler(oldIOhandler); |
| 7210 | #endif |
| 7211 | /* Now handle X errors normally. */ |
| 7212 | (void)XSetErrorHandler(oldhandler); |
| 7213 | |
| 7214 | if (xterm_dpy == NULL) |
| 7215 | { |
| 7216 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7217 | verb_msg((char_u *)_("Opening the X display failed")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7218 | return; |
| 7219 | } |
| 7220 | |
| 7221 | /* Catch terminating error of the X server connection. */ |
| 7222 | (void)XSetIOErrorHandler(x_IOerror_handler); |
| 7223 | |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 7224 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7225 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7226 | { |
| 7227 | verbose_enter(); |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 7228 | xopen_message(ELAPSED_FUNC(start_tv)); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7229 | verbose_leave(); |
| 7230 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7231 | # endif |
| 7232 | |
| 7233 | /* Create a Shell to make converters work. */ |
| 7234 | AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm", |
| 7235 | applicationShellWidgetClass, xterm_dpy, |
| 7236 | NULL); |
| 7237 | if (AppShell == (Widget)0) |
| 7238 | return; |
| 7239 | xterm_Shell = XtVaCreatePopupShell("VIM", |
| 7240 | topLevelShellWidgetClass, AppShell, |
| 7241 | XtNmappedWhenManaged, 0, |
| 7242 | XtNwidth, 1, |
| 7243 | XtNheight, 1, |
| 7244 | NULL); |
| 7245 | if (xterm_Shell == (Widget)0) |
| 7246 | return; |
| 7247 | |
| 7248 | x11_setup_atoms(xterm_dpy); |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 7249 | x11_setup_selection(xterm_Shell); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7250 | if (x11_display == NULL) |
| 7251 | x11_display = xterm_dpy; |
| 7252 | |
| 7253 | XtRealizeWidget(xterm_Shell); |
| 7254 | XSync(xterm_dpy, False); |
| 7255 | xterm_update(); |
| 7256 | } |
| 7257 | if (xterm_Shell != (Widget)0) |
| 7258 | { |
| 7259 | clip_init(TRUE); |
| 7260 | if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL) |
| 7261 | x11_window = (Window)atol(strp); |
| 7262 | /* Check if $WINDOWID is valid. */ |
| 7263 | if (test_x11_window(xterm_dpy) == FAIL) |
| 7264 | x11_window = 0; |
| 7265 | if (x11_window != 0) |
| 7266 | xterm_trace = 0; |
| 7267 | } |
| 7268 | } |
| 7269 | |
| 7270 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7271 | start_xterm_trace(int button) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7272 | { |
| 7273 | if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0) |
| 7274 | return; |
| 7275 | xterm_trace = 1; |
| 7276 | xterm_button = button; |
| 7277 | do_xterm_trace(); |
| 7278 | } |
| 7279 | |
| 7280 | |
| 7281 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7282 | stop_xterm_trace(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7283 | { |
| 7284 | if (xterm_trace < 0) |
| 7285 | return; |
| 7286 | xterm_trace = 0; |
| 7287 | } |
| 7288 | |
| 7289 | /* |
| 7290 | * Query the xterm pointer and generate mouse termcodes if necessary |
| 7291 | * return TRUE if dragging is active, else FALSE |
| 7292 | */ |
| 7293 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7294 | do_xterm_trace(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7295 | { |
| 7296 | Window root, child; |
| 7297 | int root_x, root_y; |
| 7298 | int win_x, win_y; |
| 7299 | int row, col; |
| 7300 | int_u mask_return; |
| 7301 | char_u buf[50]; |
| 7302 | char_u *strp; |
| 7303 | long got_hints; |
| 7304 | static char_u *mouse_code; |
| 7305 | static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER}; |
| 7306 | static int prev_row = 0, prev_col = 0; |
| 7307 | static XSizeHints xterm_hints; |
| 7308 | |
| 7309 | if (xterm_trace <= 0) |
| 7310 | return FALSE; |
| 7311 | |
| 7312 | if (xterm_trace == 1) |
| 7313 | { |
| 7314 | /* Get the hints just before tracking starts. The font size might |
Bram Moolenaar | a6c2c91 | 2008-01-13 15:31:00 +0000 | [diff] [blame] | 7315 | * have changed recently. */ |
| 7316 | if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints) |
| 7317 | || !(got_hints & PResizeInc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7318 | || xterm_hints.width_inc <= 1 |
| 7319 | || xterm_hints.height_inc <= 1) |
| 7320 | { |
| 7321 | xterm_trace = -1; /* Not enough data -- disable tracing */ |
| 7322 | return FALSE; |
| 7323 | } |
| 7324 | |
| 7325 | /* Rely on the same mouse code for the duration of this */ |
| 7326 | mouse_code = find_termcode(mouse_name); |
| 7327 | prev_row = mouse_row; |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 7328 | prev_col = mouse_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7329 | xterm_trace = 2; |
| 7330 | |
| 7331 | /* Find the offset of the chars, there might be a scrollbar on the |
| 7332 | * left of the window and/or a menu on the top (eterm etc.) */ |
| 7333 | XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y, |
| 7334 | &win_x, &win_y, &mask_return); |
| 7335 | xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row) |
| 7336 | - (xterm_hints.height_inc / 2); |
| 7337 | if (xterm_hints.y <= xterm_hints.height_inc / 2) |
| 7338 | xterm_hints.y = 2; |
| 7339 | xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col) |
| 7340 | - (xterm_hints.width_inc / 2); |
| 7341 | if (xterm_hints.x <= xterm_hints.width_inc / 2) |
| 7342 | xterm_hints.x = 2; |
| 7343 | return TRUE; |
| 7344 | } |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 7345 | if (mouse_code == NULL || STRLEN(mouse_code) > 45) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7346 | { |
| 7347 | xterm_trace = 0; |
| 7348 | return FALSE; |
| 7349 | } |
| 7350 | |
| 7351 | XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y, |
| 7352 | &win_x, &win_y, &mask_return); |
| 7353 | |
| 7354 | row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc); |
| 7355 | col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc); |
| 7356 | if (row == prev_row && col == prev_col) |
| 7357 | return TRUE; |
| 7358 | |
| 7359 | STRCPY(buf, mouse_code); |
| 7360 | strp = buf + STRLEN(buf); |
| 7361 | *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20; |
| 7362 | *strp++ = (char_u)(col + ' ' + 1); |
| 7363 | *strp++ = (char_u)(row + ' ' + 1); |
| 7364 | *strp = 0; |
| 7365 | add_to_input_buf(buf, STRLEN(buf)); |
| 7366 | |
| 7367 | prev_row = row; |
| 7368 | prev_col = col; |
| 7369 | return TRUE; |
| 7370 | } |
| 7371 | |
| 7372 | # if defined(FEAT_GUI) || defined(PROTO) |
| 7373 | /* |
| 7374 | * Destroy the display, window and app_context. Required for GTK. |
| 7375 | */ |
| 7376 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7377 | clear_xterm_clip(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7378 | { |
| 7379 | if (xterm_Shell != (Widget)0) |
| 7380 | { |
| 7381 | XtDestroyWidget(xterm_Shell); |
| 7382 | xterm_Shell = (Widget)0; |
| 7383 | } |
| 7384 | if (xterm_dpy != NULL) |
| 7385 | { |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7386 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7387 | /* Lesstif and Solaris crash here, lose some memory */ |
| 7388 | XtCloseDisplay(xterm_dpy); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7389 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7390 | if (x11_display == xterm_dpy) |
| 7391 | x11_display = NULL; |
| 7392 | xterm_dpy = NULL; |
| 7393 | } |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7394 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7395 | if (app_context != (XtAppContext)NULL) |
| 7396 | { |
| 7397 | /* Lesstif and Solaris crash here, lose some memory */ |
| 7398 | XtDestroyApplicationContext(app_context); |
| 7399 | app_context = (XtAppContext)NULL; |
| 7400 | } |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7401 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7402 | } |
| 7403 | # endif |
| 7404 | |
| 7405 | /* |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 7406 | * Catch up with GUI or X events. |
| 7407 | */ |
| 7408 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7409 | clip_update(void) |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 7410 | { |
| 7411 | # ifdef FEAT_GUI |
| 7412 | if (gui.in_use) |
| 7413 | gui_mch_update(); |
| 7414 | else |
| 7415 | # endif |
| 7416 | if (xterm_Shell != (Widget)0) |
| 7417 | xterm_update(); |
| 7418 | } |
| 7419 | |
| 7420 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7421 | * Catch up with any queued X events. This may put keyboard input into the |
| 7422 | * input buffer, call resize call-backs, trigger timers etc. If there is |
| 7423 | * nothing in the X event queue (& no timers pending), then we return |
| 7424 | * immediately. |
| 7425 | */ |
| 7426 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7427 | xterm_update(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7428 | { |
| 7429 | XEvent event; |
| 7430 | |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7431 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7432 | { |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7433 | XtInputMask mask = XtAppPending(app_context); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7434 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7435 | if (mask == 0 || vim_is_input_buf_full()) |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7436 | break; |
| 7437 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7438 | if (mask & XtIMXEvent) |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7439 | { |
| 7440 | /* There is an event to process. */ |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7441 | XtAppNextEvent(app_context, &event); |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7442 | #ifdef FEAT_CLIENTSERVER |
| 7443 | { |
| 7444 | XPropertyEvent *e = (XPropertyEvent *)&event; |
| 7445 | |
| 7446 | if (e->type == PropertyNotify && e->window == commWindow |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7447 | && e->atom == commProperty && e->state == PropertyNewValue) |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7448 | serverEventProc(xterm_dpy, &event, 0); |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7449 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7450 | #endif |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7451 | XtDispatchEvent(&event); |
| 7452 | } |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7453 | else |
| 7454 | { |
| 7455 | /* There is something else than an event to process. */ |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7456 | XtAppProcessEvent(app_context, mask); |
| 7457 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7458 | } |
| 7459 | } |
| 7460 | |
| 7461 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7462 | clip_xterm_own_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7463 | { |
| 7464 | if (xterm_Shell != (Widget)0) |
| 7465 | return clip_x11_own_selection(xterm_Shell, cbd); |
| 7466 | return FAIL; |
| 7467 | } |
| 7468 | |
| 7469 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7470 | clip_xterm_lose_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7471 | { |
| 7472 | if (xterm_Shell != (Widget)0) |
| 7473 | clip_x11_lose_selection(xterm_Shell, cbd); |
| 7474 | } |
| 7475 | |
| 7476 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7477 | clip_xterm_request_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7478 | { |
| 7479 | if (xterm_Shell != (Widget)0) |
| 7480 | clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd); |
| 7481 | } |
| 7482 | |
| 7483 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7484 | clip_xterm_set_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7485 | { |
| 7486 | clip_x11_set_selection(cbd); |
| 7487 | } |
| 7488 | #endif |
| 7489 | |
| 7490 | |
| 7491 | #if defined(USE_XSMP) || defined(PROTO) |
| 7492 | /* |
| 7493 | * Code for X Session Management Protocol. |
| 7494 | */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 7495 | static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast); |
| 7496 | static void xsmp_die(SmcConn smc_conn, SmPointer client_data); |
| 7497 | static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data); |
| 7498 | static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data); |
| 7499 | static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7500 | |
| 7501 | |
| 7502 | # if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 7503 | static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7504 | |
| 7505 | /* |
| 7506 | * This is our chance to ask the user if they want to save, |
| 7507 | * or abort the logout |
| 7508 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7509 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7510 | xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7511 | { |
| 7512 | cmdmod_T save_cmdmod; |
| 7513 | int cancel_shutdown = False; |
| 7514 | |
| 7515 | save_cmdmod = cmdmod; |
| 7516 | cmdmod.confirm = TRUE; |
Bram Moolenaar | 027387f | 2016-01-02 22:25:52 +0100 | [diff] [blame] | 7517 | if (check_changed_any(FALSE, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7518 | /* Mustn't logout */ |
| 7519 | cancel_shutdown = True; |
| 7520 | cmdmod = save_cmdmod; |
| 7521 | setcursor(); /* position cursor */ |
| 7522 | out_flush(); |
| 7523 | |
| 7524 | /* Done interaction */ |
| 7525 | SmcInteractDone(smc_conn, cancel_shutdown); |
| 7526 | |
| 7527 | /* Finish off |
| 7528 | * Only end save-yourself here if we're not cancelling shutdown; |
| 7529 | * we'll get a cancelled callback later in which we'll end it. |
| 7530 | * Hopefully get around glitchy SMs (like GNOME-1) |
| 7531 | */ |
| 7532 | if (!cancel_shutdown) |
| 7533 | { |
| 7534 | xsmp.save_yourself = False; |
| 7535 | SmcSaveYourselfDone(smc_conn, True); |
| 7536 | } |
| 7537 | } |
| 7538 | # endif |
| 7539 | |
| 7540 | /* |
| 7541 | * Callback that starts save-yourself. |
| 7542 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7543 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7544 | xsmp_handle_save_yourself( |
| 7545 | SmcConn smc_conn, |
| 7546 | SmPointer client_data UNUSED, |
| 7547 | int save_type UNUSED, |
| 7548 | Bool shutdown, |
| 7549 | int interact_style UNUSED, |
| 7550 | Bool fast UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7551 | { |
| 7552 | /* Handle already being in saveyourself */ |
| 7553 | if (xsmp.save_yourself) |
| 7554 | SmcSaveYourselfDone(smc_conn, True); |
| 7555 | xsmp.save_yourself = True; |
| 7556 | xsmp.shutdown = shutdown; |
| 7557 | |
| 7558 | /* First up, preserve all files */ |
| 7559 | out_flush(); |
| 7560 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 7561 | |
| 7562 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7563 | verb_msg((char_u *)_("XSMP handling save-yourself request")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7564 | |
| 7565 | # if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT) |
| 7566 | /* Now see if we can ask about unsaved files */ |
| 7567 | if (shutdown && !fast && gui.in_use) |
| 7568 | /* Need to interact with user, but need SM's permission */ |
| 7569 | SmcInteractRequest(smc_conn, SmDialogError, |
| 7570 | xsmp_handle_interaction, client_data); |
| 7571 | else |
| 7572 | # endif |
| 7573 | { |
| 7574 | /* Can stop the cycle here */ |
| 7575 | SmcSaveYourselfDone(smc_conn, True); |
| 7576 | xsmp.save_yourself = False; |
| 7577 | } |
| 7578 | } |
| 7579 | |
| 7580 | |
| 7581 | /* |
| 7582 | * Callback to warn us of imminent death. |
| 7583 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7584 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7585 | xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7586 | { |
| 7587 | xsmp_close(); |
| 7588 | |
| 7589 | /* quit quickly leaving swapfiles for modified buffers behind */ |
| 7590 | getout_preserve_modified(0); |
| 7591 | } |
| 7592 | |
| 7593 | |
| 7594 | /* |
| 7595 | * Callback to tell us that save-yourself has completed. |
| 7596 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7597 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7598 | xsmp_save_complete( |
| 7599 | SmcConn smc_conn UNUSED, |
| 7600 | SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7601 | { |
| 7602 | xsmp.save_yourself = False; |
| 7603 | } |
| 7604 | |
| 7605 | |
| 7606 | /* |
| 7607 | * Callback to tell us that an instigated shutdown was cancelled |
| 7608 | * (maybe even by us) |
| 7609 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7610 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7611 | xsmp_shutdown_cancelled( |
| 7612 | SmcConn smc_conn, |
| 7613 | SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7614 | { |
| 7615 | if (xsmp.save_yourself) |
| 7616 | SmcSaveYourselfDone(smc_conn, True); |
| 7617 | xsmp.save_yourself = False; |
| 7618 | xsmp.shutdown = False; |
| 7619 | } |
| 7620 | |
| 7621 | |
| 7622 | /* |
| 7623 | * Callback to tell us that a new ICE connection has been established. |
| 7624 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7625 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7626 | xsmp_ice_connection( |
| 7627 | IceConn iceConn, |
| 7628 | IcePointer clientData UNUSED, |
| 7629 | Bool opening, |
| 7630 | IcePointer *watchData UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7631 | { |
| 7632 | /* Intercept creation of ICE connection fd */ |
| 7633 | if (opening) |
| 7634 | { |
| 7635 | xsmp_icefd = IceConnectionNumber(iceConn); |
| 7636 | IceRemoveConnectionWatch(xsmp_ice_connection, NULL); |
| 7637 | } |
| 7638 | } |
| 7639 | |
| 7640 | |
| 7641 | /* Handle any ICE processing that's required; return FAIL if SM lost */ |
| 7642 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7643 | xsmp_handle_requests(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7644 | { |
| 7645 | Bool rep; |
| 7646 | |
| 7647 | if (IceProcessMessages(xsmp.iceconn, NULL, &rep) |
| 7648 | == IceProcessMessagesIOError) |
| 7649 | { |
| 7650 | /* Lost ICE */ |
| 7651 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7652 | verb_msg((char_u *)_("XSMP lost ICE connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7653 | xsmp_close(); |
| 7654 | return FAIL; |
| 7655 | } |
| 7656 | else |
| 7657 | return OK; |
| 7658 | } |
| 7659 | |
| 7660 | static int dummy; |
| 7661 | |
| 7662 | /* Set up X Session Management Protocol */ |
| 7663 | void |
| 7664 | xsmp_init(void) |
| 7665 | { |
| 7666 | char errorstring[80]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7667 | SmcCallbacks smcallbacks; |
| 7668 | #if 0 |
| 7669 | SmPropValue smname; |
| 7670 | SmProp smnameprop; |
| 7671 | SmProp *smprops[1]; |
| 7672 | #endif |
| 7673 | |
| 7674 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7675 | verb_msg((char_u *)_("XSMP opening connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7676 | |
| 7677 | xsmp.save_yourself = xsmp.shutdown = False; |
| 7678 | |
| 7679 | /* Set up SM callbacks - must have all, even if they're not used */ |
| 7680 | smcallbacks.save_yourself.callback = xsmp_handle_save_yourself; |
| 7681 | smcallbacks.save_yourself.client_data = NULL; |
| 7682 | smcallbacks.die.callback = xsmp_die; |
| 7683 | smcallbacks.die.client_data = NULL; |
| 7684 | smcallbacks.save_complete.callback = xsmp_save_complete; |
| 7685 | smcallbacks.save_complete.client_data = NULL; |
| 7686 | smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled; |
| 7687 | smcallbacks.shutdown_cancelled.client_data = NULL; |
| 7688 | |
| 7689 | /* Set up a watch on ICE connection creations. The "dummy" argument is |
| 7690 | * apparently required for FreeBSD (we get a BUS error when using NULL). */ |
| 7691 | if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0) |
| 7692 | { |
| 7693 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7694 | verb_msg((char_u *)_("XSMP ICE connection watch failed")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7695 | return; |
| 7696 | } |
| 7697 | |
| 7698 | /* Create an SM connection */ |
| 7699 | xsmp.smcconn = SmcOpenConnection( |
| 7700 | NULL, |
| 7701 | NULL, |
| 7702 | SmProtoMajor, |
| 7703 | SmProtoMinor, |
| 7704 | SmcSaveYourselfProcMask | SmcDieProcMask |
| 7705 | | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask, |
| 7706 | &smcallbacks, |
| 7707 | NULL, |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7708 | &xsmp.clientid, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7709 | sizeof(errorstring), |
| 7710 | errorstring); |
| 7711 | if (xsmp.smcconn == NULL) |
| 7712 | { |
| 7713 | char errorreport[132]; |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 7714 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7715 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7716 | { |
| 7717 | vim_snprintf(errorreport, sizeof(errorreport), |
| 7718 | _("XSMP SmcOpenConnection failed: %s"), errorstring); |
| 7719 | verb_msg((char_u *)errorreport); |
| 7720 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7721 | return; |
| 7722 | } |
| 7723 | xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn); |
| 7724 | |
| 7725 | #if 0 |
| 7726 | /* ID ourselves */ |
| 7727 | smname.value = "vim"; |
| 7728 | smname.length = 3; |
| 7729 | smnameprop.name = "SmProgram"; |
| 7730 | smnameprop.type = "SmARRAY8"; |
| 7731 | smnameprop.num_vals = 1; |
| 7732 | smnameprop.vals = &smname; |
| 7733 | |
| 7734 | smprops[0] = &smnameprop; |
| 7735 | SmcSetProperties(xsmp.smcconn, 1, smprops); |
| 7736 | #endif |
| 7737 | } |
| 7738 | |
| 7739 | |
| 7740 | /* Shut down XSMP comms. */ |
| 7741 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7742 | xsmp_close(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7743 | { |
| 7744 | if (xsmp_icefd != -1) |
| 7745 | { |
| 7746 | SmcCloseConnection(xsmp.smcconn, 0, NULL); |
Bram Moolenaar | 5a22181 | 2008-11-12 12:08:45 +0000 | [diff] [blame] | 7747 | if (xsmp.clientid != NULL) |
| 7748 | free(xsmp.clientid); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7749 | xsmp.clientid = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7750 | xsmp_icefd = -1; |
| 7751 | } |
| 7752 | } |
| 7753 | #endif /* USE_XSMP */ |
| 7754 | |
| 7755 | |
| 7756 | #ifdef EBCDIC |
| 7757 | /* Translate character to its CTRL- value */ |
| 7758 | char CtrlTable[] = |
| 7759 | { |
| 7760 | /* 00 - 5E */ |
| 7761 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7762 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7763 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7764 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7765 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7766 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7767 | /* ^ */ 0x1E, |
| 7768 | /* - */ 0x1F, |
| 7769 | /* 61 - 6C */ |
| 7770 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7771 | /* _ */ 0x1F, |
| 7772 | /* 6E - 80 */ |
| 7773 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7774 | /* a */ 0x01, |
| 7775 | /* b */ 0x02, |
| 7776 | /* c */ 0x03, |
| 7777 | /* d */ 0x37, |
| 7778 | /* e */ 0x2D, |
| 7779 | /* f */ 0x2E, |
| 7780 | /* g */ 0x2F, |
| 7781 | /* h */ 0x16, |
| 7782 | /* i */ 0x05, |
| 7783 | /* 8A - 90 */ |
| 7784 | 0, 0, 0, 0, 0, 0, 0, |
| 7785 | /* j */ 0x15, |
| 7786 | /* k */ 0x0B, |
| 7787 | /* l */ 0x0C, |
| 7788 | /* m */ 0x0D, |
| 7789 | /* n */ 0x0E, |
| 7790 | /* o */ 0x0F, |
| 7791 | /* p */ 0x10, |
| 7792 | /* q */ 0x11, |
| 7793 | /* r */ 0x12, |
| 7794 | /* 9A - A1 */ |
| 7795 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 7796 | /* s */ 0x13, |
| 7797 | /* t */ 0x3C, |
| 7798 | /* u */ 0x3D, |
| 7799 | /* v */ 0x32, |
| 7800 | /* w */ 0x26, |
| 7801 | /* x */ 0x18, |
| 7802 | /* y */ 0x19, |
| 7803 | /* z */ 0x3F, |
| 7804 | /* AA - AC */ |
| 7805 | 0, 0, 0, |
| 7806 | /* [ */ 0x27, |
| 7807 | /* AE - BC */ |
| 7808 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7809 | /* ] */ 0x1D, |
| 7810 | /* BE - C0 */ 0, 0, 0, |
| 7811 | /* A */ 0x01, |
| 7812 | /* B */ 0x02, |
| 7813 | /* C */ 0x03, |
| 7814 | /* D */ 0x37, |
| 7815 | /* E */ 0x2D, |
| 7816 | /* F */ 0x2E, |
| 7817 | /* G */ 0x2F, |
| 7818 | /* H */ 0x16, |
| 7819 | /* I */ 0x05, |
| 7820 | /* CA - D0 */ 0, 0, 0, 0, 0, 0, 0, |
| 7821 | /* J */ 0x15, |
| 7822 | /* K */ 0x0B, |
| 7823 | /* L */ 0x0C, |
| 7824 | /* M */ 0x0D, |
| 7825 | /* N */ 0x0E, |
| 7826 | /* O */ 0x0F, |
| 7827 | /* P */ 0x10, |
| 7828 | /* Q */ 0x11, |
| 7829 | /* R */ 0x12, |
| 7830 | /* DA - DF */ 0, 0, 0, 0, 0, 0, |
| 7831 | /* \ */ 0x1C, |
| 7832 | /* E1 */ 0, |
| 7833 | /* S */ 0x13, |
| 7834 | /* T */ 0x3C, |
| 7835 | /* U */ 0x3D, |
| 7836 | /* V */ 0x32, |
| 7837 | /* W */ 0x26, |
| 7838 | /* X */ 0x18, |
| 7839 | /* Y */ 0x19, |
| 7840 | /* Z */ 0x3F, |
| 7841 | /* EA - FF*/ 0, 0, 0, 0, 0, 0, |
| 7842 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7843 | }; |
| 7844 | |
| 7845 | char MetaCharTable[]= |
| 7846 | {/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 7847 | 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0, |
| 7848 | 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0, |
| 7849 | '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0, |
| 7850 | 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0 |
| 7851 | }; |
| 7852 | |
| 7853 | |
| 7854 | /* TODO: Use characters NOT numbers!!! */ |
| 7855 | char CtrlCharTable[]= |
| 7856 | {/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 7857 | 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214, |
| 7858 | 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109, |
| 7859 | 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199, |
| 7860 | 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233, |
| 7861 | }; |
| 7862 | |
| 7863 | |
| 7864 | #endif |