Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * OS/2 port by Paul Slootman |
| 5 | * VMS merge by Zoltan Arpadffy |
| 6 | * |
| 7 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 8 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 9 | * See README.txt for an overview of the Vim source code. |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...) |
| 14 | * Also for OS/2, using the excellent EMX package!!! |
| 15 | * Also for BeOS and Atari MiNT. |
| 16 | * |
| 17 | * A lot of this file was originally written by Juergen Weigert and later |
| 18 | * changed beyond recognition. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Some systems have a prototype for select() that has (int *) instead of |
| 23 | * (fd_set *), which is wrong. This define removes that prototype. We define |
| 24 | * our own prototype below. |
| 25 | * Don't use it for the Mac, it causes a warning for precompiled headers. |
| 26 | * TODO: use a configure check for precompiled headers? |
| 27 | */ |
Bram Moolenaar | 311d982 | 2007-02-27 15:48:28 +0000 | [diff] [blame] | 28 | #if !defined(__APPLE__) && !defined(__TANDEM) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | # define select select_declared_wrong |
| 30 | #endif |
| 31 | |
| 32 | #include "vim.h" |
| 33 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 34 | #ifdef FEAT_MZSCHEME |
| 35 | # include "if_mzsch.h" |
| 36 | #endif |
| 37 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | #include "os_unixx.h" /* unix includes for os_unix.c only */ |
| 39 | |
| 40 | #ifdef USE_XSMP |
| 41 | # include <X11/SM/SMlib.h> |
| 42 | #endif |
| 43 | |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 44 | #ifdef HAVE_SELINUX |
| 45 | # include <selinux/selinux.h> |
| 46 | static int selinux_enabled = -1; |
| 47 | #endif |
| 48 | |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 49 | #ifdef HAVE_SMACK |
| 50 | # include <attr/xattr.h> |
| 51 | # include <linux/xattr.h> |
| 52 | # ifndef SMACK_LABEL_LEN |
| 53 | # define SMACK_LABEL_LEN 1024 |
| 54 | # endif |
| 55 | #endif |
| 56 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 57 | /* |
| 58 | * Use this prototype for select, some include files have a wrong prototype |
| 59 | */ |
Bram Moolenaar | 311d982 | 2007-02-27 15:48:28 +0000 | [diff] [blame] | 60 | #ifndef __TANDEM |
| 61 | # undef select |
| 62 | # ifdef __BEOS__ |
| 63 | # define select beos_select |
| 64 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
Bram Moolenaar | a244243 | 2007-04-26 14:26:37 +0000 | [diff] [blame] | 67 | #ifdef __CYGWIN__ |
| 68 | # ifndef WIN32 |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 69 | # include <cygwin/version.h> |
| 70 | # include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or |
| 71 | * for cygwin_conv_path() */ |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 72 | # ifdef FEAT_CYGWIN_WIN32_CLIPBOARD |
| 73 | # define WIN32_LEAN_AND_MEAN |
| 74 | # include <windows.h> |
| 75 | # include "winclip.pro" |
| 76 | # endif |
Bram Moolenaar | a244243 | 2007-04-26 14:26:37 +0000 | [diff] [blame] | 77 | # endif |
| 78 | #endif |
| 79 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 80 | #if defined(HAVE_SELECT) |
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 | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 178 | static int WaitForChar(long); |
Bram Moolenaar | 4ffa070 | 2013-12-11 17:12:37 +0100 | [diff] [blame] | 179 | #if defined(__BEOS__) || defined(VMS) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 180 | int RealWaitForChar(int, long, int *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 181 | #else |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 182 | static int RealWaitForChar(int, long, int *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 183 | #endif |
| 184 | |
| 185 | #ifdef FEAT_XCLIPBOARD |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 186 | static int do_xterm_trace(void); |
Bram Moolenaar | cf851ce | 2005-06-16 21:52:47 +0000 | [diff] [blame] | 187 | # define XT_TRACE_DELAY 50 /* delay for xterm tracing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 188 | #endif |
| 189 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 190 | static void handle_resize(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 191 | |
| 192 | #if defined(SIGWINCH) |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 193 | static RETSIGTYPE sig_winch SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 194 | #endif |
| 195 | #if defined(SIGINT) |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 196 | static RETSIGTYPE catch_sigint SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 197 | #endif |
| 198 | #if defined(SIGPWR) |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 199 | static RETSIGTYPE catch_sigpwr SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 200 | #endif |
| 201 | #if defined(SIGALRM) && defined(FEAT_X11) \ |
| 202 | && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK) |
| 203 | # define SET_SIG_ALARM |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 204 | static RETSIGTYPE sig_alarm SIGPROTOARG; |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 205 | /* volatile because it is used in signal handler sig_alarm(). */ |
| 206 | static volatile int sig_alarm_called; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 207 | #endif |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 208 | static RETSIGTYPE deathtrap SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 209 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 210 | static void catch_int_signal(void); |
| 211 | static void set_signals(void); |
| 212 | static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 213 | #ifndef __EMX__ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 214 | static int have_wildcard(int, char_u **); |
| 215 | static int have_dollars(int, char_u **); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 216 | #endif |
| 217 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 218 | #ifndef __EMX__ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 219 | 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] | 220 | #endif |
| 221 | |
| 222 | #ifndef SIG_ERR |
Bram Moolenaar | 8f0b2d4 | 2009-05-16 14:41:10 +0000 | [diff] [blame] | 223 | # define SIG_ERR ((RETSIGTYPE (*)())-1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 224 | #endif |
| 225 | |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 226 | /* volatile because it is used in signal handler sig_winch(). */ |
| 227 | static volatile int do_resize = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 228 | #ifndef __EMX__ |
| 229 | static char_u *extra_shell_arg = NULL; |
| 230 | static int show_shell_mess = TRUE; |
| 231 | #endif |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 232 | /* volatile because it is used in signal handler deathtrap(). */ |
| 233 | static volatile int deadly_signal = 0; /* The signal we caught */ |
| 234 | /* volatile because it is used in signal handler deathtrap(). */ |
| 235 | static volatile int in_mch_delay = FALSE; /* sleeping in mch_delay() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 236 | |
| 237 | static int curr_tmode = TMODE_COOK; /* contains current terminal mode */ |
| 238 | |
| 239 | #ifdef USE_XSMP |
| 240 | typedef struct |
| 241 | { |
| 242 | SmcConn smcconn; /* The SM connection ID */ |
| 243 | IceConn iceconn; /* The ICE connection ID */ |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 244 | char *clientid; /* The client ID for the current smc session */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 245 | Bool save_yourself; /* If we're in the middle of a save_yourself */ |
| 246 | Bool shutdown; /* If we're in shutdown mode */ |
| 247 | } xsmp_config_T; |
| 248 | |
| 249 | static xsmp_config_T xsmp; |
| 250 | #endif |
| 251 | |
| 252 | #ifdef SYS_SIGLIST_DECLARED |
| 253 | /* |
| 254 | * I have seen |
| 255 | * extern char *_sys_siglist[NSIG]; |
| 256 | * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings |
| 257 | * that describe the signals. That is nearly what we want here. But |
| 258 | * autoconf does only check for sys_siglist (without the underscore), I |
| 259 | * do not want to change everything today.... jw. |
| 260 | * This is why AC_DECL_SYS_SIGLIST is commented out in configure.in |
| 261 | */ |
| 262 | #endif |
| 263 | |
| 264 | static struct signalinfo |
| 265 | { |
| 266 | int sig; /* Signal number, eg. SIGSEGV etc */ |
| 267 | char *name; /* Signal name (not char_u!). */ |
| 268 | char deadly; /* Catch as a deadly signal? */ |
| 269 | } signal_info[] = |
| 270 | { |
| 271 | #ifdef SIGHUP |
| 272 | {SIGHUP, "HUP", TRUE}, |
| 273 | #endif |
| 274 | #ifdef SIGQUIT |
| 275 | {SIGQUIT, "QUIT", TRUE}, |
| 276 | #endif |
| 277 | #ifdef SIGILL |
| 278 | {SIGILL, "ILL", TRUE}, |
| 279 | #endif |
| 280 | #ifdef SIGTRAP |
| 281 | {SIGTRAP, "TRAP", TRUE}, |
| 282 | #endif |
| 283 | #ifdef SIGABRT |
| 284 | {SIGABRT, "ABRT", TRUE}, |
| 285 | #endif |
| 286 | #ifdef SIGEMT |
| 287 | {SIGEMT, "EMT", TRUE}, |
| 288 | #endif |
| 289 | #ifdef SIGFPE |
| 290 | {SIGFPE, "FPE", TRUE}, |
| 291 | #endif |
| 292 | #ifdef SIGBUS |
| 293 | {SIGBUS, "BUS", TRUE}, |
| 294 | #endif |
Bram Moolenaar | 7567646 | 2013-01-30 14:55:42 +0100 | [diff] [blame] | 295 | #if defined(SIGSEGV) && !defined(FEAT_MZSCHEME) |
| 296 | /* MzScheme uses SEGV in its garbage collector */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 297 | {SIGSEGV, "SEGV", TRUE}, |
| 298 | #endif |
| 299 | #ifdef SIGSYS |
| 300 | {SIGSYS, "SYS", TRUE}, |
| 301 | #endif |
| 302 | #ifdef SIGALRM |
| 303 | {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */ |
| 304 | #endif |
| 305 | #ifdef SIGTERM |
| 306 | {SIGTERM, "TERM", TRUE}, |
| 307 | #endif |
Bram Moolenaar | b292a2a | 2011-02-09 18:47:40 +0100 | [diff] [blame] | 308 | #if defined(SIGVTALRM) && !defined(FEAT_RUBY) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 309 | {SIGVTALRM, "VTALRM", TRUE}, |
| 310 | #endif |
Bram Moolenaar | 02f07e0 | 2008-03-12 12:17:28 +0000 | [diff] [blame] | 311 | #if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING) |
| 312 | /* MzScheme uses SIGPROF for its own needs; On Linux with profiling |
| 313 | * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 314 | {SIGPROF, "PROF", TRUE}, |
| 315 | #endif |
| 316 | #ifdef SIGXCPU |
| 317 | {SIGXCPU, "XCPU", TRUE}, |
| 318 | #endif |
| 319 | #ifdef SIGXFSZ |
| 320 | {SIGXFSZ, "XFSZ", TRUE}, |
| 321 | #endif |
| 322 | #ifdef SIGUSR1 |
| 323 | {SIGUSR1, "USR1", TRUE}, |
| 324 | #endif |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 325 | #if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE) |
| 326 | /* Used for sysmouse handling */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 327 | {SIGUSR2, "USR2", TRUE}, |
| 328 | #endif |
| 329 | #ifdef SIGINT |
| 330 | {SIGINT, "INT", FALSE}, |
| 331 | #endif |
| 332 | #ifdef SIGWINCH |
| 333 | {SIGWINCH, "WINCH", FALSE}, |
| 334 | #endif |
| 335 | #ifdef SIGTSTP |
| 336 | {SIGTSTP, "TSTP", FALSE}, |
| 337 | #endif |
| 338 | #ifdef SIGPIPE |
| 339 | {SIGPIPE, "PIPE", FALSE}, |
| 340 | #endif |
| 341 | {-1, "Unknown!", FALSE} |
| 342 | }; |
| 343 | |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 344 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 345 | mch_chdir(char *path) |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 346 | { |
| 347 | if (p_verbose >= 5) |
| 348 | { |
| 349 | verbose_enter(); |
| 350 | smsg((char_u *)"chdir(%s)", path); |
| 351 | verbose_leave(); |
| 352 | } |
| 353 | # ifdef VMS |
| 354 | return chdir(vms_fixfilename(path)); |
| 355 | # else |
| 356 | return chdir(path); |
| 357 | # endif |
| 358 | } |
| 359 | |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 360 | /* |
| 361 | * Write s[len] to the screen. |
| 362 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 363 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 364 | mch_write(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 365 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 366 | ignored = (int)write(1, (char *)s, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 367 | if (p_wd) /* Unix is too fast, slow down a bit more */ |
| 368 | RealWaitForChar(read_cmd_fd, p_wd, NULL); |
| 369 | } |
| 370 | |
| 371 | /* |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 372 | * mch_inchar(): low level input function. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 373 | * Get a characters from the keyboard. |
| 374 | * Return the number of characters that are available. |
| 375 | * If wtime == 0 do not wait for characters. |
| 376 | * If wtime == n wait a short time for characters. |
| 377 | * If wtime == -1 wait forever for characters. |
| 378 | */ |
| 379 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 380 | mch_inchar( |
| 381 | char_u *buf, |
| 382 | int maxlen, |
| 383 | long wtime, /* don't use "time", MIPS cannot handle it */ |
| 384 | int tb_change_cnt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 385 | { |
| 386 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 387 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 388 | #ifdef MESSAGE_QUEUE |
| 389 | parse_queued_messages(); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 390 | #endif |
| 391 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 392 | /* Check if window changed size while we were busy, perhaps the ":set |
| 393 | * columns=99" command was used. */ |
| 394 | while (do_resize) |
| 395 | handle_resize(); |
| 396 | |
| 397 | if (wtime >= 0) |
| 398 | { |
| 399 | while (WaitForChar(wtime) == 0) /* no character available */ |
| 400 | { |
Bram Moolenaar | 5d8afeb | 2015-11-19 19:55:16 +0100 | [diff] [blame] | 401 | if (do_resize) |
| 402 | handle_resize(); |
| 403 | #ifdef FEAT_CLIENTSERVER |
| 404 | else if (!server_waiting()) |
| 405 | #else |
| 406 | else |
| 407 | #endif |
| 408 | /* return if not interrupted by resize or server */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 409 | return 0; |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 410 | #ifdef MESSAGE_QUEUE |
| 411 | parse_queued_messages(); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 412 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | else /* wtime == -1 */ |
| 416 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 417 | /* |
| 418 | * If there is no character available within 'updatetime' seconds |
Bram Moolenaar | 4317d9b | 2005-03-18 20:25:31 +0000 | [diff] [blame] | 419 | * flush all the swap files to disk. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 420 | * Also done when interrupted by SIGWINCH. |
| 421 | */ |
| 422 | if (WaitForChar(p_ut) == 0) |
| 423 | { |
| 424 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 425 | if (trigger_cursorhold() && maxlen >= 3 |
| 426 | && !typebuf_changed(tb_change_cnt)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 427 | { |
Bram Moolenaar | 4317d9b | 2005-03-18 20:25:31 +0000 | [diff] [blame] | 428 | buf[0] = K_SPECIAL; |
| 429 | buf[1] = KS_EXTRA; |
| 430 | buf[2] = (int)KE_CURSORHOLD; |
| 431 | return 3; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 432 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 433 | #endif |
Bram Moolenaar | d4098f5 | 2005-06-27 22:37:13 +0000 | [diff] [blame] | 434 | before_blocking(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | for (;;) /* repeat until we got a character */ |
| 439 | { |
| 440 | while (do_resize) /* window changed size */ |
| 441 | handle_resize(); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 442 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 443 | #ifdef MESSAGE_QUEUE |
| 444 | parse_queued_messages(); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 445 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 446 | /* |
Bram Moolenaar | 48bae37 | 2010-07-29 23:12:15 +0200 | [diff] [blame] | 447 | * We want to be interrupted by the winch signal |
| 448 | * or by an event on the monitored file descriptors. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 449 | */ |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 450 | if (WaitForChar(-1L) == 0) |
| 451 | { |
| 452 | if (do_resize) /* interrupted by SIGWINCH signal */ |
| 453 | handle_resize(); |
| 454 | return 0; |
| 455 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 456 | |
| 457 | /* If input was put directly in typeahead buffer bail out here. */ |
| 458 | if (typebuf_changed(tb_change_cnt)) |
| 459 | return 0; |
| 460 | |
| 461 | /* |
| 462 | * For some terminals we only get one character at a time. |
| 463 | * We want the get all available characters, so we could keep on |
| 464 | * trying until none is available |
| 465 | * For some other terminals this is quite slow, that's why we don't do |
| 466 | * it. |
| 467 | */ |
| 468 | len = read_from_input_buf(buf, (long)maxlen); |
| 469 | if (len > 0) |
| 470 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 471 | return len; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 477 | handle_resize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 478 | { |
| 479 | do_resize = FALSE; |
| 480 | shell_resized(); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * return non-zero if a character is available |
| 485 | */ |
| 486 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 487 | mch_char_avail(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 488 | { |
| 489 | return WaitForChar(0L); |
| 490 | } |
| 491 | |
| 492 | #if defined(HAVE_TOTAL_MEM) || defined(PROTO) |
| 493 | # ifdef HAVE_SYS_RESOURCE_H |
Bram Moolenaar | 8f0b2d4 | 2009-05-16 14:41:10 +0000 | [diff] [blame] | 494 | # include <sys/resource.h> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 495 | # endif |
| 496 | # if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL) |
| 497 | # include <sys/sysctl.h> |
| 498 | # endif |
| 499 | # if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO) |
| 500 | # include <sys/sysinfo.h> |
| 501 | # endif |
| 502 | |
| 503 | /* |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 504 | * Return total amount of memory available in Kbyte. |
| 505 | * Doesn't change when memory has been allocated. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 506 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 507 | long_u |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 508 | mch_total_mem(int special UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 509 | { |
| 510 | # ifdef __EMX__ |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 511 | return ulimit(3, 0L) >> 10; /* always 32MB? */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 512 | # else |
| 513 | long_u mem = 0; |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 514 | long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 515 | |
| 516 | # ifdef HAVE_SYSCTL |
| 517 | int mib[2], physmem; |
| 518 | size_t len; |
| 519 | |
| 520 | /* BSD way of getting the amount of RAM available. */ |
| 521 | mib[0] = CTL_HW; |
| 522 | mib[1] = HW_USERMEM; |
| 523 | len = sizeof(physmem); |
| 524 | if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0) |
| 525 | mem = (long_u)physmem; |
| 526 | # endif |
| 527 | |
| 528 | # if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO) |
| 529 | if (mem == 0) |
| 530 | { |
| 531 | struct sysinfo sinfo; |
| 532 | |
| 533 | /* Linux way of getting amount of RAM available */ |
| 534 | if (sysinfo(&sinfo) == 0) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 535 | { |
| 536 | # ifdef HAVE_SYSINFO_MEM_UNIT |
| 537 | /* avoid overflow as much as possible */ |
| 538 | while (shiftright > 0 && (sinfo.mem_unit & 1) == 0) |
| 539 | { |
| 540 | sinfo.mem_unit = sinfo.mem_unit >> 1; |
| 541 | --shiftright; |
| 542 | } |
| 543 | mem = sinfo.totalram * sinfo.mem_unit; |
| 544 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 545 | mem = sinfo.totalram; |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 546 | # endif |
| 547 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 548 | } |
| 549 | # endif |
| 550 | |
| 551 | # ifdef HAVE_SYSCONF |
| 552 | if (mem == 0) |
| 553 | { |
| 554 | long pagesize, pagecount; |
| 555 | |
| 556 | /* Solaris way of getting amount of RAM available */ |
| 557 | pagesize = sysconf(_SC_PAGESIZE); |
| 558 | pagecount = sysconf(_SC_PHYS_PAGES); |
| 559 | if (pagesize > 0 && pagecount > 0) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 560 | { |
| 561 | /* avoid overflow as much as possible */ |
| 562 | while (shiftright > 0 && (pagesize & 1) == 0) |
| 563 | { |
Bram Moolenaar | 3d27a45 | 2007-05-10 17:44:18 +0000 | [diff] [blame] | 564 | pagesize = (long_u)pagesize >> 1; |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 565 | --shiftright; |
| 566 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 567 | mem = (long_u)pagesize * pagecount; |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 568 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 569 | } |
| 570 | # endif |
| 571 | |
| 572 | /* Return the minimum of the physical memory and the user limit, because |
| 573 | * using more than the user limit may cause Vim to be terminated. */ |
| 574 | # if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) |
| 575 | { |
| 576 | struct rlimit rlp; |
| 577 | |
| 578 | if (getrlimit(RLIMIT_DATA, &rlp) == 0 |
| 579 | && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1)) |
| 580 | # ifdef RLIM_INFINITY |
| 581 | && rlp.rlim_cur != RLIM_INFINITY |
| 582 | # endif |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 583 | && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 584 | ) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 585 | { |
| 586 | mem = (long_u)rlp.rlim_cur; |
| 587 | shiftright = 10; |
| 588 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 589 | } |
| 590 | # endif |
| 591 | |
| 592 | if (mem > 0) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 593 | return mem >> shiftright; |
| 594 | return (long_u)0x1fffff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 595 | # endif |
| 596 | } |
| 597 | #endif |
| 598 | |
| 599 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 600 | mch_delay(long msec, int ignoreinput) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 601 | { |
| 602 | int old_tmode; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 603 | #ifdef FEAT_MZSCHEME |
| 604 | long total = msec; /* remember original value */ |
| 605 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 606 | |
| 607 | if (ignoreinput) |
| 608 | { |
| 609 | /* Go to cooked mode without echo, to allow SIGINT interrupting us |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 610 | * here. But we don't want QUIT to kill us (CTRL-\ used in a |
| 611 | * shell may produce SIGQUIT). */ |
| 612 | in_mch_delay = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 613 | old_tmode = curr_tmode; |
| 614 | if (curr_tmode == TMODE_RAW) |
| 615 | settmode(TMODE_SLEEP); |
| 616 | |
| 617 | /* |
| 618 | * Everybody sleeps in a different way... |
| 619 | * Prefer nanosleep(), some versions of usleep() can only sleep up to |
| 620 | * one second. |
| 621 | */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 622 | #ifdef FEAT_MZSCHEME |
| 623 | do |
| 624 | { |
| 625 | /* if total is large enough, wait by portions in p_mzq */ |
| 626 | if (total > p_mzq) |
| 627 | msec = p_mzq; |
| 628 | else |
| 629 | msec = total; |
| 630 | total -= msec; |
| 631 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 632 | #ifdef HAVE_NANOSLEEP |
| 633 | { |
| 634 | struct timespec ts; |
| 635 | |
| 636 | ts.tv_sec = msec / 1000; |
| 637 | ts.tv_nsec = (msec % 1000) * 1000000; |
| 638 | (void)nanosleep(&ts, NULL); |
| 639 | } |
| 640 | #else |
| 641 | # ifdef HAVE_USLEEP |
| 642 | while (msec >= 1000) |
| 643 | { |
| 644 | usleep((unsigned int)(999 * 1000)); |
| 645 | msec -= 999; |
| 646 | } |
| 647 | usleep((unsigned int)(msec * 1000)); |
| 648 | # else |
| 649 | # ifndef HAVE_SELECT |
| 650 | poll(NULL, 0, (int)msec); |
| 651 | # else |
| 652 | # ifdef __EMX__ |
| 653 | _sleep2(msec); |
| 654 | # else |
| 655 | { |
| 656 | struct timeval tv; |
| 657 | |
| 658 | tv.tv_sec = msec / 1000; |
| 659 | tv.tv_usec = (msec % 1000) * 1000; |
| 660 | /* |
| 661 | * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get |
| 662 | * a patch from Sun to fix this. Reported by Gunnar Pedersen. |
| 663 | */ |
| 664 | select(0, NULL, NULL, NULL, &tv); |
| 665 | } |
| 666 | # endif /* __EMX__ */ |
| 667 | # endif /* HAVE_SELECT */ |
| 668 | # endif /* HAVE_NANOSLEEP */ |
| 669 | #endif /* HAVE_USLEEP */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 670 | #ifdef FEAT_MZSCHEME |
| 671 | } |
| 672 | while (total > 0); |
| 673 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 674 | |
| 675 | settmode(old_tmode); |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 676 | in_mch_delay = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 677 | } |
| 678 | else |
| 679 | WaitForChar(msec); |
| 680 | } |
| 681 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 682 | #if defined(HAVE_STACK_LIMIT) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 683 | || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK)) |
| 684 | # define HAVE_CHECK_STACK_GROWTH |
| 685 | /* |
| 686 | * Support for checking for an almost-out-of-stack-space situation. |
| 687 | */ |
| 688 | |
| 689 | /* |
| 690 | * Return a pointer to an item on the stack. Used to find out if the stack |
| 691 | * grows up or down. |
| 692 | */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 693 | static void check_stack_growth(char *p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 694 | static int stack_grows_downwards; |
| 695 | |
| 696 | /* |
| 697 | * Find out if the stack grows upwards or downwards. |
| 698 | * "p" points to a variable on the stack of the caller. |
| 699 | */ |
| 700 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 701 | check_stack_growth(char *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 702 | { |
| 703 | int i; |
| 704 | |
| 705 | stack_grows_downwards = (p > (char *)&i); |
| 706 | } |
| 707 | #endif |
| 708 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 709 | #if defined(HAVE_STACK_LIMIT) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 710 | static char *stack_limit = NULL; |
| 711 | |
| 712 | #if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H) |
| 713 | # include <pthread.h> |
| 714 | # include <pthread_np.h> |
| 715 | #endif |
| 716 | |
| 717 | /* |
| 718 | * Find out until how var the stack can grow without getting into trouble. |
| 719 | * Called when starting up and when switching to the signal stack in |
| 720 | * deathtrap(). |
| 721 | */ |
| 722 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 723 | get_stack_limit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 724 | { |
| 725 | struct rlimit rlp; |
| 726 | int i; |
| 727 | long lim; |
| 728 | |
| 729 | /* Set the stack limit to 15/16 of the allowable size. Skip this when the |
| 730 | * limit doesn't fit in a long (rlim_cur might be "long long"). */ |
| 731 | if (getrlimit(RLIMIT_STACK, &rlp) == 0 |
| 732 | && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1)) |
| 733 | # ifdef RLIM_INFINITY |
| 734 | && rlp.rlim_cur != RLIM_INFINITY |
| 735 | # endif |
| 736 | ) |
| 737 | { |
| 738 | lim = (long)rlp.rlim_cur; |
| 739 | #if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H) |
| 740 | { |
| 741 | pthread_attr_t attr; |
| 742 | size_t size; |
| 743 | |
| 744 | /* On FreeBSD the initial thread always has a fixed stack size, no |
| 745 | * matter what the limits are set to. Normally it's 1 Mbyte. */ |
| 746 | pthread_attr_init(&attr); |
| 747 | if (pthread_attr_get_np(pthread_self(), &attr) == 0) |
| 748 | { |
| 749 | pthread_attr_getstacksize(&attr, &size); |
| 750 | if (lim > (long)size) |
| 751 | lim = (long)size; |
| 752 | } |
| 753 | pthread_attr_destroy(&attr); |
| 754 | } |
| 755 | #endif |
| 756 | if (stack_grows_downwards) |
| 757 | { |
| 758 | stack_limit = (char *)((long)&i - (lim / 16L * 15L)); |
| 759 | if (stack_limit >= (char *)&i) |
| 760 | /* overflow, set to 1/16 of current stack position */ |
| 761 | stack_limit = (char *)((long)&i / 16L); |
| 762 | } |
| 763 | else |
| 764 | { |
| 765 | stack_limit = (char *)((long)&i + (lim / 16L * 15L)); |
| 766 | if (stack_limit <= (char *)&i) |
| 767 | stack_limit = NULL; /* overflow */ |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * Return FAIL when running out of stack space. |
| 774 | * "p" must point to any variable local to the caller that's on the stack. |
| 775 | */ |
| 776 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 777 | mch_stackcheck(char *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 778 | { |
| 779 | if (stack_limit != NULL) |
| 780 | { |
| 781 | if (stack_grows_downwards) |
| 782 | { |
| 783 | if (p < stack_limit) |
| 784 | return FAIL; |
| 785 | } |
| 786 | else if (p > stack_limit) |
| 787 | return FAIL; |
| 788 | } |
| 789 | return OK; |
| 790 | } |
| 791 | #endif |
| 792 | |
| 793 | #if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
| 794 | /* |
| 795 | * Support for using the signal stack. |
| 796 | * This helps when we run out of stack space, which causes a SIGSEGV. The |
| 797 | * signal handler then must run on another stack, since the normal stack is |
| 798 | * completely full. |
| 799 | */ |
| 800 | |
Bram Moolenaar | 39766a7 | 2013-11-03 00:41:00 +0100 | [diff] [blame] | 801 | #if defined(HAVE_AVAILABILITYMACROS_H) |
Bram Moolenaar | 4cc95d1 | 2013-11-02 21:49:32 +0100 | [diff] [blame] | 802 | # include <AvailabilityMacros.h> |
| 803 | #endif |
| 804 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 805 | #ifndef SIGSTKSZ |
| 806 | # define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */ |
| 807 | #endif |
| 808 | |
| 809 | # ifdef HAVE_SIGALTSTACK |
| 810 | static stack_t sigstk; /* for sigaltstack() */ |
| 811 | # else |
| 812 | static struct sigstack sigstk; /* for sigstack() */ |
| 813 | # endif |
| 814 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 815 | static void init_signal_stack(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 816 | static char *signal_stack; |
| 817 | |
| 818 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 819 | init_signal_stack(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 820 | { |
| 821 | if (signal_stack != NULL) |
| 822 | { |
| 823 | # ifdef HAVE_SIGALTSTACK |
Bram Moolenaar | 1a3d086 | 2007-08-30 09:47:38 +0000 | [diff] [blame] | 824 | # if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \ |
| 825 | || MAC_OS_X_VERSION_MAX_ALLOWED <= 1040) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 826 | /* missing prototype. Adding it to osdef?.h.in doesn't work, because |
| 827 | * "struct sigaltstack" needs to be declared. */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 828 | extern int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 829 | # endif |
| 830 | |
| 831 | # ifdef HAVE_SS_BASE |
| 832 | sigstk.ss_base = signal_stack; |
| 833 | # else |
| 834 | sigstk.ss_sp = signal_stack; |
| 835 | # endif |
| 836 | sigstk.ss_size = SIGSTKSZ; |
| 837 | sigstk.ss_flags = 0; |
| 838 | (void)sigaltstack(&sigstk, NULL); |
| 839 | # else |
| 840 | sigstk.ss_sp = signal_stack; |
| 841 | if (stack_grows_downwards) |
| 842 | sigstk.ss_sp += SIGSTKSZ - 1; |
| 843 | sigstk.ss_onstack = 0; |
| 844 | (void)sigstack(&sigstk, NULL); |
| 845 | # endif |
| 846 | } |
| 847 | } |
| 848 | #endif |
| 849 | |
| 850 | /* |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 851 | * We need correct prototypes for a signal function, otherwise mean compilers |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 852 | * will barf when the second argument to signal() is ``wrong''. |
| 853 | * Let me try it with a few tricky defines from my own osdef.h (jw). |
| 854 | */ |
| 855 | #if defined(SIGWINCH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 856 | static RETSIGTYPE |
| 857 | sig_winch SIGDEFARG(sigarg) |
| 858 | { |
| 859 | /* this is not required on all systems, but it doesn't hurt anybody */ |
| 860 | signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch); |
| 861 | do_resize = TRUE; |
| 862 | SIGRETURN; |
| 863 | } |
| 864 | #endif |
| 865 | |
| 866 | #if defined(SIGINT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 867 | static RETSIGTYPE |
| 868 | catch_sigint SIGDEFARG(sigarg) |
| 869 | { |
| 870 | /* this is not required on all systems, but it doesn't hurt anybody */ |
| 871 | signal(SIGINT, (RETSIGTYPE (*)())catch_sigint); |
| 872 | got_int = TRUE; |
| 873 | SIGRETURN; |
| 874 | } |
| 875 | #endif |
| 876 | |
| 877 | #if defined(SIGPWR) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 878 | static RETSIGTYPE |
| 879 | catch_sigpwr SIGDEFARG(sigarg) |
| 880 | { |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 881 | /* this is not required on all systems, but it doesn't hurt anybody */ |
| 882 | signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 883 | /* |
| 884 | * I'm not sure we get the SIGPWR signal when the system is really going |
| 885 | * down or when the batteries are almost empty. Just preserve the swap |
| 886 | * files and don't exit, that can't do any harm. |
| 887 | */ |
| 888 | ml_sync_all(FALSE, FALSE); |
| 889 | SIGRETURN; |
| 890 | } |
| 891 | #endif |
| 892 | |
| 893 | #ifdef SET_SIG_ALARM |
| 894 | /* |
| 895 | * signal function for alarm(). |
| 896 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 897 | static RETSIGTYPE |
| 898 | sig_alarm SIGDEFARG(sigarg) |
| 899 | { |
| 900 | /* doesn't do anything, just to break a system call */ |
| 901 | sig_alarm_called = TRUE; |
| 902 | SIGRETURN; |
| 903 | } |
| 904 | #endif |
| 905 | |
Bram Moolenaar | 44ecf65 | 2005-03-07 23:09:59 +0000 | [diff] [blame] | 906 | #if (defined(HAVE_SETJMP_H) \ |
| 907 | && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \ |
| 908 | || defined(FEAT_LIBCALL))) \ |
| 909 | || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 910 | /* |
| 911 | * A simplistic version of setjmp() that only allows one level of using. |
| 912 | * Don't call twice before calling mch_endjmp()!. |
| 913 | * Usage: |
| 914 | * mch_startjmp(); |
| 915 | * if (SETJMP(lc_jump_env) != 0) |
| 916 | * { |
| 917 | * mch_didjmp(); |
| 918 | * EMSG("crash!"); |
| 919 | * } |
| 920 | * else |
| 921 | * { |
| 922 | * do_the_work; |
| 923 | * mch_endjmp(); |
| 924 | * } |
| 925 | * Note: Can't move SETJMP() here, because a function calling setjmp() must |
| 926 | * not return before the saved environment is used. |
| 927 | * Returns OK for normal return, FAIL when the protected code caused a |
| 928 | * problem and LONGJMP() was used. |
| 929 | */ |
| 930 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 931 | mch_startjmp(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 932 | { |
| 933 | #ifdef SIGHASARG |
| 934 | lc_signal = 0; |
| 935 | #endif |
| 936 | lc_active = TRUE; |
| 937 | } |
| 938 | |
| 939 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 940 | mch_endjmp(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | { |
| 942 | lc_active = FALSE; |
| 943 | } |
| 944 | |
| 945 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 946 | mch_didjmp(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 947 | { |
| 948 | # if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
| 949 | /* On FreeBSD the signal stack has to be reset after using siglongjmp(), |
| 950 | * otherwise catching the signal only works once. */ |
| 951 | init_signal_stack(); |
| 952 | # endif |
| 953 | } |
| 954 | #endif |
| 955 | |
| 956 | /* |
| 957 | * This function handles deadly signals. |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 958 | * It tries to preserve any swap files and exit properly. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 959 | * (partly from Elvis). |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 960 | * NOTE: Avoid unsafe functions, such as allocating memory, they can result in |
| 961 | * a deadlock. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 962 | */ |
| 963 | static RETSIGTYPE |
| 964 | deathtrap SIGDEFARG(sigarg) |
| 965 | { |
| 966 | static int entered = 0; /* count the number of times we got here. |
| 967 | Note: when memory has been corrupted |
| 968 | this may get an arbitrary value! */ |
| 969 | #ifdef SIGHASARG |
| 970 | int i; |
| 971 | #endif |
| 972 | |
| 973 | #if defined(HAVE_SETJMP_H) |
| 974 | /* |
| 975 | * Catch a crash in protected code. |
| 976 | * Restores the environment saved in lc_jump_env, which looks like |
| 977 | * SETJMP() returns 1. |
| 978 | */ |
| 979 | if (lc_active) |
| 980 | { |
| 981 | # if defined(SIGHASARG) |
| 982 | lc_signal = sigarg; |
| 983 | # endif |
| 984 | lc_active = FALSE; /* don't jump again */ |
| 985 | LONGJMP(lc_jump_env, 1); |
| 986 | /* NOTREACHED */ |
| 987 | } |
| 988 | #endif |
| 989 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 990 | #ifdef SIGHASARG |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 991 | # ifdef SIGQUIT |
| 992 | /* While in mch_delay() we go to cooked mode to allow a CTRL-C to |
| 993 | * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when |
| 994 | * pressing CTRL-\, but we don't want Vim to exit then. */ |
| 995 | if (in_mch_delay && sigarg == SIGQUIT) |
| 996 | SIGRETURN; |
| 997 | # endif |
| 998 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 999 | /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return |
| 1000 | * here. This avoids that a non-reentrant function is interrupted, e.g., |
| 1001 | * free(). Calling free() again may then cause a crash. */ |
| 1002 | if (entered == 0 |
| 1003 | && (0 |
| 1004 | # ifdef SIGHUP |
| 1005 | || sigarg == SIGHUP |
| 1006 | # endif |
| 1007 | # ifdef SIGQUIT |
| 1008 | || sigarg == SIGQUIT |
| 1009 | # endif |
| 1010 | # ifdef SIGTERM |
| 1011 | || sigarg == SIGTERM |
| 1012 | # endif |
| 1013 | # ifdef SIGPWR |
| 1014 | || sigarg == SIGPWR |
| 1015 | # endif |
| 1016 | # ifdef SIGUSR1 |
| 1017 | || sigarg == SIGUSR1 |
| 1018 | # endif |
| 1019 | # ifdef SIGUSR2 |
| 1020 | || sigarg == SIGUSR2 |
| 1021 | # endif |
| 1022 | ) |
Bram Moolenaar | 1f28b07 | 2005-07-12 22:42:41 +0000 | [diff] [blame] | 1023 | && !vim_handle_signal(sigarg)) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1024 | SIGRETURN; |
| 1025 | #endif |
| 1026 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1027 | /* Remember how often we have been called. */ |
| 1028 | ++entered; |
| 1029 | |
| 1030 | #ifdef FEAT_EVAL |
| 1031 | /* Set the v:dying variable. */ |
| 1032 | set_vim_var_nr(VV_DYING, (long)entered); |
| 1033 | #endif |
| 1034 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1035 | #ifdef HAVE_STACK_LIMIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1036 | /* Since we are now using the signal stack, need to reset the stack |
| 1037 | * limit. Otherwise using a regexp will fail. */ |
| 1038 | get_stack_limit(); |
| 1039 | #endif |
| 1040 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1041 | #if 0 |
| 1042 | /* This is for opening gdb the moment Vim crashes. |
| 1043 | * You need to manually adjust the file name and Vim executable name. |
| 1044 | * Suggested by SungHyun Nam. */ |
| 1045 | { |
| 1046 | # define VI_GDB_FILE "/tmp/vimgdb" |
| 1047 | # define VIM_NAME "/usr/bin/vim" |
| 1048 | FILE *fp = fopen(VI_GDB_FILE, "w"); |
| 1049 | if (fp) |
| 1050 | { |
| 1051 | fprintf(fp, |
| 1052 | "file %s\n" |
| 1053 | "attach %d\n" |
| 1054 | "set height 1000\n" |
| 1055 | "bt full\n" |
| 1056 | , VIM_NAME, getpid()); |
| 1057 | fclose(fp); |
| 1058 | system("xterm -e gdb -x "VI_GDB_FILE); |
| 1059 | unlink(VI_GDB_FILE); |
| 1060 | } |
| 1061 | } |
| 1062 | #endif |
| 1063 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1064 | #ifdef SIGHASARG |
| 1065 | /* try to find the name of this signal */ |
| 1066 | for (i = 0; signal_info[i].sig != -1; i++) |
| 1067 | if (sigarg == signal_info[i].sig) |
| 1068 | break; |
| 1069 | deadly_signal = sigarg; |
| 1070 | #endif |
| 1071 | |
| 1072 | full_screen = FALSE; /* don't write message to the GUI, it might be |
| 1073 | * part of the problem... */ |
| 1074 | /* |
| 1075 | * If something goes wrong after entering here, we may get here again. |
| 1076 | * When this happens, give a message and try to exit nicely (resetting the |
| 1077 | * terminal mode, etc.) |
| 1078 | * When this happens twice, just exit, don't even try to give a message, |
| 1079 | * stack may be corrupt or something weird. |
| 1080 | * When this still happens again (or memory was corrupted in such a way |
| 1081 | * that "entered" was clobbered) use _exit(), don't try freeing resources. |
| 1082 | */ |
| 1083 | if (entered >= 3) |
| 1084 | { |
| 1085 | reset_signals(); /* don't catch any signals anymore */ |
| 1086 | may_core_dump(); |
| 1087 | if (entered >= 4) |
| 1088 | _exit(8); |
| 1089 | exit(7); |
| 1090 | } |
| 1091 | if (entered == 2) |
| 1092 | { |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1093 | /* No translation, it may call malloc(). */ |
| 1094 | OUT_STR("Vim: Double signal, exiting\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1095 | out_flush(); |
| 1096 | getout(1); |
| 1097 | } |
| 1098 | |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1099 | /* No translation, it may call malloc(). */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1100 | #ifdef SIGHASARG |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1101 | sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1102 | signal_info[i].name); |
| 1103 | #else |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1104 | sprintf((char *)IObuff, "Vim: Caught deadly signal\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1105 | #endif |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1106 | |
| 1107 | /* Preserve files and exit. This sets the really_exiting flag to prevent |
| 1108 | * calling free(). */ |
| 1109 | preserve_exit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1110 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1111 | #ifdef NBDEBUG |
| 1112 | reset_signals(); |
| 1113 | may_core_dump(); |
| 1114 | abort(); |
| 1115 | #endif |
| 1116 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1117 | SIGRETURN; |
| 1118 | } |
| 1119 | |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1120 | #if defined(_REENTRANT) && defined(SIGCONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1121 | /* |
| 1122 | * On Solaris with multi-threading, suspending might not work immediately. |
| 1123 | * Catch the SIGCONT signal, which will be used as an indication whether the |
| 1124 | * suspending has been done or not. |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1125 | * |
| 1126 | * On Linux, signal is not always handled immediately either. |
| 1127 | * See https://bugs.launchpad.net/bugs/291373 |
| 1128 | * |
Bram Moolenaar | b292a2a | 2011-02-09 18:47:40 +0100 | [diff] [blame] | 1129 | * volatile because it is used in signal handler sigcont_handler(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1130 | */ |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1131 | static volatile int sigcont_received; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 1132 | static RETSIGTYPE sigcont_handler SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1133 | |
| 1134 | /* |
| 1135 | * signal handler for SIGCONT |
| 1136 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1137 | static RETSIGTYPE |
| 1138 | sigcont_handler SIGDEFARG(sigarg) |
| 1139 | { |
| 1140 | sigcont_received = TRUE; |
| 1141 | SIGRETURN; |
| 1142 | } |
| 1143 | #endif |
| 1144 | |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1145 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1146 | static void loose_clipboard(void); |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1147 | # ifdef USE_SYSTEM |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1148 | static void save_clipboard(void); |
| 1149 | static void restore_clipboard(void); |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1150 | |
| 1151 | static void *clip_star_save = NULL; |
| 1152 | static void *clip_plus_save = NULL; |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1153 | # endif |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1154 | |
| 1155 | /* |
| 1156 | * Called when Vim is going to sleep or execute a shell command. |
| 1157 | * We can't respond to requests for the X selections. Lose them, otherwise |
| 1158 | * other applications will hang. But first copy the text to cut buffer 0. |
| 1159 | */ |
| 1160 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1161 | loose_clipboard(void) |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1162 | { |
| 1163 | if (clip_star.owned || clip_plus.owned) |
| 1164 | { |
| 1165 | x11_export_final_selection(); |
| 1166 | if (clip_star.owned) |
| 1167 | clip_lose_selection(&clip_star); |
| 1168 | if (clip_plus.owned) |
| 1169 | clip_lose_selection(&clip_plus); |
| 1170 | if (x11_display != NULL) |
| 1171 | XFlush(x11_display); |
| 1172 | } |
| 1173 | } |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1174 | |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1175 | # ifdef USE_SYSTEM |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1176 | /* |
| 1177 | * Save clipboard text to restore later. |
| 1178 | */ |
| 1179 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1180 | save_clipboard(void) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1181 | { |
| 1182 | if (clip_star.owned) |
| 1183 | clip_star_save = get_register('*', TRUE); |
| 1184 | if (clip_plus.owned) |
| 1185 | clip_plus_save = get_register('+', TRUE); |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * Restore clipboard text if no one own the X selection. |
| 1190 | */ |
| 1191 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1192 | restore_clipboard(void) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1193 | { |
| 1194 | if (clip_star_save != NULL) |
| 1195 | { |
| 1196 | if (!clip_gen_owner_exists(&clip_star)) |
| 1197 | put_register('*', clip_star_save); |
| 1198 | else |
| 1199 | free_register(clip_star_save); |
| 1200 | clip_star_save = NULL; |
| 1201 | } |
| 1202 | if (clip_plus_save != NULL) |
| 1203 | { |
| 1204 | if (!clip_gen_owner_exists(&clip_plus)) |
| 1205 | put_register('+', clip_plus_save); |
| 1206 | else |
| 1207 | free_register(clip_plus_save); |
| 1208 | clip_plus_save = NULL; |
| 1209 | } |
| 1210 | } |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1211 | # endif |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1212 | #endif |
| 1213 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1214 | /* |
| 1215 | * If the machine has job control, use it to suspend the program, |
| 1216 | * otherwise fake it by starting a new shell. |
| 1217 | */ |
| 1218 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1219 | mch_suspend(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1220 | { |
| 1221 | /* BeOS does have SIGTSTP, but it doesn't work. */ |
| 1222 | #if defined(SIGTSTP) && !defined(__BEOS__) |
| 1223 | out_flush(); /* needed to make cursor visible on some systems */ |
| 1224 | settmode(TMODE_COOK); |
| 1225 | out_flush(); /* needed to disable mouse on some systems */ |
| 1226 | |
| 1227 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1228 | loose_clipboard(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1229 | # endif |
| 1230 | |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1231 | # if defined(_REENTRANT) && defined(SIGCONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1232 | sigcont_received = FALSE; |
| 1233 | # endif |
| 1234 | kill(0, SIGTSTP); /* send ourselves a STOP signal */ |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1235 | # if defined(_REENTRANT) && defined(SIGCONT) |
| 1236 | /* |
| 1237 | * Wait for the SIGCONT signal to be handled. It generally happens |
| 1238 | * immediately, but somehow not all the time. Do not call pause() |
| 1239 | * because there would be race condition which would hang Vim if |
| 1240 | * signal happened in between the test of sigcont_received and the |
| 1241 | * call to pause(). If signal is not yet received, call sleep(0) |
| 1242 | * to just yield CPU. Signal should then be received. If somehow |
| 1243 | * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting |
| 1244 | * further if signal is not received after 1+2+3+4 ms (not expected |
| 1245 | * to happen). |
| 1246 | */ |
| 1247 | { |
Bram Moolenaar | 262735e | 2009-07-14 10:20:22 +0000 | [diff] [blame] | 1248 | long wait_time; |
| 1249 | for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++) |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1250 | /* Loop is not entered most of the time */ |
Bram Moolenaar | 262735e | 2009-07-14 10:20:22 +0000 | [diff] [blame] | 1251 | mch_delay(wait_time, FALSE); |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1252 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1253 | # endif |
| 1254 | |
| 1255 | # ifdef FEAT_TITLE |
| 1256 | /* |
| 1257 | * Set oldtitle to NULL, so the current title is obtained again. |
| 1258 | */ |
| 1259 | vim_free(oldtitle); |
| 1260 | oldtitle = NULL; |
| 1261 | # endif |
| 1262 | settmode(TMODE_RAW); |
| 1263 | need_check_timestamps = TRUE; |
| 1264 | did_check_timestamps = FALSE; |
| 1265 | #else |
| 1266 | suspend_shell(); |
| 1267 | #endif |
| 1268 | } |
| 1269 | |
| 1270 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1271 | mch_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1272 | { |
| 1273 | Columns = 80; |
| 1274 | Rows = 24; |
| 1275 | |
| 1276 | out_flush(); |
| 1277 | set_signals(); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1278 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 1279 | #ifdef MACOS_CONVERT |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1280 | mac_conv_init(); |
| 1281 | #endif |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 1282 | #ifdef FEAT_CYGWIN_WIN32_CLIPBOARD |
| 1283 | win_clip_init(); |
| 1284 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1288 | set_signals(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1289 | { |
| 1290 | #if defined(SIGWINCH) |
| 1291 | /* |
| 1292 | * WINDOW CHANGE signal is handled with sig_winch(). |
| 1293 | */ |
| 1294 | signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch); |
| 1295 | #endif |
| 1296 | |
| 1297 | /* |
| 1298 | * We want the STOP signal to work, to make mch_suspend() work. |
| 1299 | * For "rvim" the STOP signal is ignored. |
| 1300 | */ |
| 1301 | #ifdef SIGTSTP |
| 1302 | signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL); |
| 1303 | #endif |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1304 | #if defined(_REENTRANT) && defined(SIGCONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1305 | signal(SIGCONT, sigcont_handler); |
| 1306 | #endif |
| 1307 | |
| 1308 | /* |
| 1309 | * We want to ignore breaking of PIPEs. |
| 1310 | */ |
| 1311 | #ifdef SIGPIPE |
| 1312 | signal(SIGPIPE, SIG_IGN); |
| 1313 | #endif |
| 1314 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1315 | #ifdef SIGINT |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1316 | catch_int_signal(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1317 | #endif |
| 1318 | |
| 1319 | /* |
| 1320 | * Ignore alarm signals (Perl's alarm() generates it). |
| 1321 | */ |
| 1322 | #ifdef SIGALRM |
| 1323 | signal(SIGALRM, SIG_IGN); |
| 1324 | #endif |
| 1325 | |
| 1326 | /* |
| 1327 | * Catch SIGPWR (power failure?) to preserve the swap files, so that no |
| 1328 | * work will be lost. |
| 1329 | */ |
| 1330 | #ifdef SIGPWR |
| 1331 | signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr); |
| 1332 | #endif |
| 1333 | |
| 1334 | /* |
| 1335 | * Arrange for other signals to gracefully shutdown Vim. |
| 1336 | */ |
| 1337 | catch_signals(deathtrap, SIG_ERR); |
| 1338 | |
| 1339 | #if defined(FEAT_GUI) && defined(SIGHUP) |
| 1340 | /* |
| 1341 | * When the GUI is running, ignore the hangup signal. |
| 1342 | */ |
| 1343 | if (gui.in_use) |
| 1344 | signal(SIGHUP, SIG_IGN); |
| 1345 | #endif |
| 1346 | } |
| 1347 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1348 | #if defined(SIGINT) || defined(PROTO) |
| 1349 | /* |
| 1350 | * Catch CTRL-C (only works while in Cooked mode). |
| 1351 | */ |
| 1352 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1353 | catch_int_signal(void) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1354 | { |
| 1355 | signal(SIGINT, (RETSIGTYPE (*)())catch_sigint); |
| 1356 | } |
| 1357 | #endif |
| 1358 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1359 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1360 | reset_signals(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1361 | { |
| 1362 | catch_signals(SIG_DFL, SIG_DFL); |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1363 | #if defined(_REENTRANT) && defined(SIGCONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1364 | /* SIGCONT isn't in the list, because its default action is ignore */ |
| 1365 | signal(SIGCONT, SIG_DFL); |
| 1366 | #endif |
| 1367 | } |
| 1368 | |
| 1369 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1370 | catch_signals( |
| 1371 | RETSIGTYPE (*func_deadly)(), |
| 1372 | RETSIGTYPE (*func_other)()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1373 | { |
| 1374 | int i; |
| 1375 | |
| 1376 | for (i = 0; signal_info[i].sig != -1; i++) |
| 1377 | if (signal_info[i].deadly) |
| 1378 | { |
| 1379 | #if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION) |
| 1380 | struct sigaction sa; |
| 1381 | |
| 1382 | /* Setup to use the alternate stack for the signal function. */ |
| 1383 | sa.sa_handler = func_deadly; |
| 1384 | sigemptyset(&sa.sa_mask); |
| 1385 | # if defined(__linux__) && defined(_REENTRANT) |
| 1386 | /* On Linux, with glibc compiled for kernel 2.2, there is a bug in |
| 1387 | * thread handling in combination with using the alternate stack: |
| 1388 | * pthread library functions try to use the stack pointer to |
| 1389 | * identify the current thread, causing a SEGV signal, which |
| 1390 | * recursively calls deathtrap() and hangs. */ |
| 1391 | sa.sa_flags = 0; |
| 1392 | # else |
| 1393 | sa.sa_flags = SA_ONSTACK; |
| 1394 | # endif |
| 1395 | sigaction(signal_info[i].sig, &sa, NULL); |
| 1396 | #else |
| 1397 | # if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC) |
| 1398 | struct sigvec sv; |
| 1399 | |
| 1400 | /* Setup to use the alternate stack for the signal function. */ |
| 1401 | sv.sv_handler = func_deadly; |
| 1402 | sv.sv_mask = 0; |
| 1403 | sv.sv_flags = SV_ONSTACK; |
| 1404 | sigvec(signal_info[i].sig, &sv, NULL); |
| 1405 | # else |
| 1406 | signal(signal_info[i].sig, func_deadly); |
| 1407 | # endif |
| 1408 | #endif |
| 1409 | } |
| 1410 | else if (func_other != SIG_ERR) |
| 1411 | signal(signal_info[i].sig, func_other); |
| 1412 | } |
| 1413 | |
| 1414 | /* |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1415 | * Handling of SIGHUP, SIGQUIT and SIGTERM: |
Bram Moolenaar | 9e1d283 | 2007-05-06 12:51:41 +0000 | [diff] [blame] | 1416 | * "when" == a signal: when busy, postpone and return FALSE, otherwise |
| 1417 | * return TRUE |
| 1418 | * "when" == SIGNAL_BLOCK: Going to be busy, block signals |
| 1419 | * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 1420 | * signal |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1421 | * Returns TRUE when Vim should exit. |
| 1422 | */ |
| 1423 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1424 | vim_handle_signal(int sig) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1425 | { |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1426 | static int got_signal = 0; |
| 1427 | static int blocked = TRUE; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1428 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1429 | switch (sig) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1430 | { |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1431 | case SIGNAL_BLOCK: blocked = TRUE; |
| 1432 | break; |
| 1433 | |
| 1434 | case SIGNAL_UNBLOCK: blocked = FALSE; |
| 1435 | if (got_signal != 0) |
| 1436 | { |
| 1437 | kill(getpid(), got_signal); |
| 1438 | got_signal = 0; |
| 1439 | } |
| 1440 | break; |
| 1441 | |
| 1442 | default: if (!blocked) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1443 | return TRUE; /* exit! */ |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1444 | got_signal = sig; |
| 1445 | #ifdef SIGPWR |
| 1446 | if (sig != SIGPWR) |
| 1447 | #endif |
| 1448 | got_int = TRUE; /* break any loops */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1449 | break; |
| 1450 | } |
| 1451 | return FALSE; |
| 1452 | } |
| 1453 | |
| 1454 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1455 | * Check_win checks whether we have an interactive stdout. |
| 1456 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1457 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1458 | mch_check_win(int argc UNUSED, char **argv UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1459 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1460 | if (isatty(1)) |
| 1461 | return OK; |
| 1462 | return FAIL; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * Return TRUE if the input comes from a terminal, FALSE otherwise. |
| 1467 | */ |
| 1468 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1469 | mch_input_isatty(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1470 | { |
| 1471 | if (isatty(read_cmd_fd)) |
| 1472 | return TRUE; |
| 1473 | return FALSE; |
| 1474 | } |
| 1475 | |
| 1476 | #ifdef FEAT_X11 |
| 1477 | |
| 1478 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) \ |
| 1479 | && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)) |
| 1480 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1481 | static void xopen_message(struct timeval *tvp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1482 | |
| 1483 | /* |
| 1484 | * Give a message about the elapsed time for opening the X window. |
| 1485 | */ |
| 1486 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1487 | xopen_message( |
| 1488 | struct timeval *tvp) /* must contain start time */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1489 | { |
| 1490 | struct timeval end_tv; |
| 1491 | |
| 1492 | /* Compute elapsed time. */ |
| 1493 | gettimeofday(&end_tv, NULL); |
| 1494 | smsg((char_u *)_("Opening the X display took %ld msec"), |
| 1495 | (end_tv.tv_sec - tvp->tv_sec) * 1000L |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 1496 | + (end_tv.tv_usec - tvp->tv_usec) / 1000L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1497 | } |
| 1498 | # endif |
| 1499 | #endif |
| 1500 | |
| 1501 | #if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD)) |
| 1502 | /* |
| 1503 | * A few functions shared by X11 title and clipboard code. |
| 1504 | */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1505 | static int x_error_handler(Display *dpy, XErrorEvent *error_event); |
| 1506 | static int x_error_check(Display *dpy, XErrorEvent *error_event); |
| 1507 | static int x_connect_to_server(void); |
| 1508 | static int test_x11_window(Display *dpy); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1509 | |
| 1510 | static int got_x_error = FALSE; |
| 1511 | |
| 1512 | /* |
| 1513 | * X Error handler, otherwise X just exits! (very rude) -- webb |
| 1514 | */ |
| 1515 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1516 | x_error_handler(Display *dpy, XErrorEvent *error_event) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1517 | { |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 1518 | XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1519 | STRCAT(IObuff, _("\nVim: Got X error\n")); |
| 1520 | |
| 1521 | /* We cannot print a message and continue, because no X calls are allowed |
| 1522 | * here (causes my system to hang). Silently continuing might be an |
| 1523 | * alternative... */ |
| 1524 | preserve_exit(); /* preserve files and exit */ |
| 1525 | |
| 1526 | return 0; /* NOTREACHED */ |
| 1527 | } |
| 1528 | |
| 1529 | /* |
| 1530 | * Another X Error handler, just used to check for errors. |
| 1531 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1532 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1533 | x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1534 | { |
| 1535 | got_x_error = TRUE; |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | #if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) |
| 1540 | # if defined(HAVE_SETJMP_H) |
| 1541 | /* |
| 1542 | * An X IO Error handler, used to catch error while opening the display. |
| 1543 | */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1544 | static int x_IOerror_check(Display *dpy); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1545 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1546 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1547 | x_IOerror_check(Display *dpy UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1548 | { |
| 1549 | /* This function should not return, it causes exit(). Longjump instead. */ |
| 1550 | LONGJMP(lc_jump_env, 1); |
Bram Moolenaar | fe17e76 | 2013-06-29 14:17:02 +0200 | [diff] [blame] | 1551 | # if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
Bram Moolenaar | b4990bf | 2010-02-11 18:19:38 +0100 | [diff] [blame] | 1552 | return 0; /* avoid the compiler complains about missing return value */ |
| 1553 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1554 | } |
| 1555 | # endif |
| 1556 | |
| 1557 | /* |
| 1558 | * An X IO Error handler, used to catch terminal errors. |
| 1559 | */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1560 | static int x_IOerror_handler(Display *dpy); |
| 1561 | static void may_restore_clipboard(void); |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1562 | static int xterm_dpy_was_reset = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1563 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1564 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1565 | x_IOerror_handler(Display *dpy UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1566 | { |
| 1567 | xterm_dpy = NULL; |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1568 | xterm_dpy_was_reset = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1569 | x11_window = 0; |
| 1570 | x11_display = NULL; |
| 1571 | xterm_Shell = (Widget)0; |
| 1572 | |
| 1573 | /* This function should not return, it causes exit(). Longjump instead. */ |
| 1574 | LONGJMP(x_jump_env, 1); |
Bram Moolenaar | fe17e76 | 2013-06-29 14:17:02 +0200 | [diff] [blame] | 1575 | # if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
Bram Moolenaar | b4990bf | 2010-02-11 18:19:38 +0100 | [diff] [blame] | 1576 | return 0; /* avoid the compiler complains about missing return value */ |
| 1577 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1578 | } |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1579 | |
| 1580 | /* |
| 1581 | * If the X11 connection was lost try to restore it. |
| 1582 | * 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] | 1583 | * (e.g. through tmux). |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1584 | */ |
| 1585 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1586 | may_restore_clipboard(void) |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1587 | { |
| 1588 | if (xterm_dpy_was_reset) |
| 1589 | { |
| 1590 | xterm_dpy_was_reset = FALSE; |
Bram Moolenaar | 527a678 | 2014-12-17 17:59:31 +0100 | [diff] [blame] | 1591 | |
| 1592 | # ifndef LESSTIF_VERSION |
| 1593 | /* This has been reported to avoid Vim getting stuck. */ |
| 1594 | if (app_context != (XtAppContext)NULL) |
| 1595 | { |
| 1596 | XtDestroyApplicationContext(app_context); |
| 1597 | app_context = (XtAppContext)NULL; |
| 1598 | x11_display = NULL; /* freed by XtDestroyApplicationContext() */ |
| 1599 | } |
| 1600 | # endif |
| 1601 | |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1602 | setup_term_clip(); |
| 1603 | get_x11_title(FALSE); |
| 1604 | } |
| 1605 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1606 | #endif |
| 1607 | |
| 1608 | /* |
| 1609 | * Return TRUE when connection to the X server is desired. |
| 1610 | */ |
| 1611 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1612 | x_connect_to_server(void) |
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 | #if defined(FEAT_CLIENTSERVER) |
| 1615 | if (x_force_connect) |
| 1616 | return TRUE; |
| 1617 | #endif |
| 1618 | if (x_no_connect) |
| 1619 | return FALSE; |
| 1620 | |
| 1621 | /* Check for a match with "exclude:" from 'clipboard'. */ |
| 1622 | if (clip_exclude_prog != NULL) |
| 1623 | { |
Bram Moolenaar | dffa5b8 | 2014-11-19 16:38:07 +0100 | [diff] [blame] | 1624 | 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] | 1625 | return FALSE; |
| 1626 | } |
| 1627 | return TRUE; |
| 1628 | } |
| 1629 | |
| 1630 | /* |
| 1631 | * Test if "dpy" and x11_window are valid by getting the window title. |
| 1632 | * I don't actually want it yet, so there may be a simpler call to use, but |
| 1633 | * this will cause the error handler x_error_check() to be called if anything |
| 1634 | * is wrong, such as the window pointer being invalid (as can happen when the |
| 1635 | * user changes his DISPLAY, but not his WINDOWID) -- webb |
| 1636 | */ |
| 1637 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1638 | test_x11_window(Display *dpy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1639 | { |
| 1640 | int (*old_handler)(); |
| 1641 | XTextProperty text_prop; |
| 1642 | |
| 1643 | old_handler = XSetErrorHandler(x_error_check); |
| 1644 | got_x_error = FALSE; |
| 1645 | if (XGetWMName(dpy, x11_window, &text_prop)) |
| 1646 | XFree((void *)text_prop.value); |
| 1647 | XSync(dpy, False); |
| 1648 | (void)XSetErrorHandler(old_handler); |
| 1649 | |
| 1650 | if (p_verbose > 0 && got_x_error) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 1651 | verb_msg((char_u *)_("Testing the X display failed")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1652 | |
| 1653 | return (got_x_error ? FAIL : OK); |
| 1654 | } |
| 1655 | #endif |
| 1656 | |
| 1657 | #ifdef FEAT_TITLE |
| 1658 | |
| 1659 | #ifdef FEAT_X11 |
| 1660 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1661 | static int get_x11_thing(int get_title, int test_only); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1662 | |
| 1663 | /* |
| 1664 | * try to get x11 window and display |
| 1665 | * |
| 1666 | * return FAIL for failure, OK otherwise |
| 1667 | */ |
| 1668 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1669 | get_x11_windis(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1670 | { |
| 1671 | char *winid; |
| 1672 | static int result = -1; |
| 1673 | #define XD_NONE 0 /* x11_display not set here */ |
| 1674 | #define XD_HERE 1 /* x11_display opened here */ |
| 1675 | #define XD_GUI 2 /* x11_display used from gui.dpy */ |
| 1676 | #define XD_XTERM 3 /* x11_display used from xterm_dpy */ |
| 1677 | static int x11_display_from = XD_NONE; |
| 1678 | static int did_set_error_handler = FALSE; |
| 1679 | |
| 1680 | if (!did_set_error_handler) |
| 1681 | { |
| 1682 | /* X just exits if it finds an error otherwise! */ |
| 1683 | (void)XSetErrorHandler(x_error_handler); |
| 1684 | did_set_error_handler = TRUE; |
| 1685 | } |
| 1686 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 1687 | #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1688 | if (gui.in_use) |
| 1689 | { |
| 1690 | /* |
| 1691 | * If the X11 display was opened here before, for the window where Vim |
| 1692 | * was started, close that one now to avoid a memory leak. |
| 1693 | */ |
| 1694 | if (x11_display_from == XD_HERE && x11_display != NULL) |
| 1695 | { |
| 1696 | XCloseDisplay(x11_display); |
| 1697 | x11_display_from = XD_NONE; |
| 1698 | } |
| 1699 | if (gui_get_x11_windis(&x11_window, &x11_display) == OK) |
| 1700 | { |
| 1701 | x11_display_from = XD_GUI; |
| 1702 | return OK; |
| 1703 | } |
| 1704 | x11_display = NULL; |
| 1705 | return FAIL; |
| 1706 | } |
| 1707 | else if (x11_display_from == XD_GUI) |
| 1708 | { |
| 1709 | /* GUI must have stopped somehow, clear x11_display */ |
| 1710 | x11_window = 0; |
| 1711 | x11_display = NULL; |
| 1712 | x11_display_from = XD_NONE; |
| 1713 | } |
| 1714 | #endif |
| 1715 | |
| 1716 | /* When started with the "-X" argument, don't try connecting. */ |
| 1717 | if (!x_connect_to_server()) |
| 1718 | return FAIL; |
| 1719 | |
| 1720 | /* |
| 1721 | * If WINDOWID not set, should try another method to find out |
| 1722 | * what the current window number is. The only code I know for |
| 1723 | * this is very complicated. |
| 1724 | * We assume that zero is invalid for WINDOWID. |
| 1725 | */ |
| 1726 | if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL) |
| 1727 | x11_window = (Window)atol(winid); |
| 1728 | |
| 1729 | #ifdef FEAT_XCLIPBOARD |
| 1730 | if (xterm_dpy != NULL && x11_window != 0) |
| 1731 | { |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 1732 | /* We may have checked it already, but Gnome terminal can move us to |
| 1733 | * another window, so we need to check every time. */ |
| 1734 | if (x11_display_from != XD_XTERM) |
| 1735 | { |
| 1736 | /* |
| 1737 | * If the X11 display was opened here before, for the window where |
| 1738 | * Vim was started, close that one now to avoid a memory leak. |
| 1739 | */ |
| 1740 | if (x11_display_from == XD_HERE && x11_display != NULL) |
| 1741 | XCloseDisplay(x11_display); |
| 1742 | x11_display = xterm_dpy; |
| 1743 | x11_display_from = XD_XTERM; |
| 1744 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1745 | if (test_x11_window(x11_display) == FAIL) |
| 1746 | { |
| 1747 | /* probably bad $WINDOWID */ |
| 1748 | x11_window = 0; |
| 1749 | x11_display = NULL; |
| 1750 | x11_display_from = XD_NONE; |
| 1751 | return FAIL; |
| 1752 | } |
| 1753 | return OK; |
| 1754 | } |
| 1755 | #endif |
| 1756 | |
| 1757 | if (x11_window == 0 || x11_display == NULL) |
| 1758 | result = -1; |
| 1759 | |
| 1760 | if (result != -1) /* Have already been here and set this */ |
| 1761 | return result; /* Don't do all these X calls again */ |
| 1762 | |
| 1763 | if (x11_window != 0 && x11_display == NULL) |
| 1764 | { |
| 1765 | #ifdef SET_SIG_ALARM |
| 1766 | RETSIGTYPE (*sig_save)(); |
| 1767 | #endif |
| 1768 | #if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 1769 | struct timeval start_tv; |
| 1770 | |
| 1771 | if (p_verbose > 0) |
| 1772 | gettimeofday(&start_tv, NULL); |
| 1773 | #endif |
| 1774 | |
| 1775 | #ifdef SET_SIG_ALARM |
| 1776 | /* |
| 1777 | * Opening the Display may hang if the DISPLAY setting is wrong, or |
| 1778 | * the network connection is bad. Set an alarm timer to get out. |
| 1779 | */ |
| 1780 | sig_alarm_called = FALSE; |
| 1781 | sig_save = (RETSIGTYPE (*)())signal(SIGALRM, |
| 1782 | (RETSIGTYPE (*)())sig_alarm); |
| 1783 | alarm(2); |
| 1784 | #endif |
| 1785 | x11_display = XOpenDisplay(NULL); |
| 1786 | |
| 1787 | #ifdef SET_SIG_ALARM |
| 1788 | alarm(0); |
| 1789 | signal(SIGALRM, (RETSIGTYPE (*)())sig_save); |
| 1790 | if (p_verbose > 0 && sig_alarm_called) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 1791 | verb_msg((char_u *)_("Opening the X display timed out")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1792 | #endif |
| 1793 | if (x11_display != NULL) |
| 1794 | { |
| 1795 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 1796 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 1797 | { |
| 1798 | verbose_enter(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1799 | xopen_message(&start_tv); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 1800 | verbose_leave(); |
| 1801 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1802 | # endif |
| 1803 | if (test_x11_window(x11_display) == FAIL) |
| 1804 | { |
| 1805 | /* Maybe window id is bad */ |
| 1806 | x11_window = 0; |
| 1807 | XCloseDisplay(x11_display); |
| 1808 | x11_display = NULL; |
| 1809 | } |
| 1810 | else |
| 1811 | x11_display_from = XD_HERE; |
| 1812 | } |
| 1813 | } |
| 1814 | if (x11_window == 0 || x11_display == NULL) |
| 1815 | return (result = FAIL); |
Bram Moolenaar | 727c876 | 2010-10-20 19:17:48 +0200 | [diff] [blame] | 1816 | |
| 1817 | # ifdef FEAT_EVAL |
| 1818 | set_vim_var_nr(VV_WINDOWID, (long)x11_window); |
| 1819 | # endif |
| 1820 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1821 | return (result = OK); |
| 1822 | } |
| 1823 | |
| 1824 | /* |
| 1825 | * Determine original x11 Window Title |
| 1826 | */ |
| 1827 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1828 | get_x11_title(int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1829 | { |
Bram Moolenaar | 47136d7 | 2004-10-12 20:02:24 +0000 | [diff] [blame] | 1830 | return get_x11_thing(TRUE, test_only); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | /* |
| 1834 | * Determine original x11 Window icon |
| 1835 | */ |
| 1836 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1837 | get_x11_icon(int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1838 | { |
| 1839 | int retval = FALSE; |
| 1840 | |
| 1841 | retval = get_x11_thing(FALSE, test_only); |
| 1842 | |
| 1843 | /* could not get old icon, use terminal name */ |
| 1844 | if (oldicon == NULL && !test_only) |
| 1845 | { |
| 1846 | if (STRNCMP(T_NAME, "builtin_", 8) == 0) |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 1847 | oldicon = vim_strsave(T_NAME + 8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1848 | else |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 1849 | oldicon = vim_strsave(T_NAME); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | return retval; |
| 1853 | } |
| 1854 | |
| 1855 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1856 | get_x11_thing( |
| 1857 | int get_title, /* get title string */ |
| 1858 | int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1859 | { |
| 1860 | XTextProperty text_prop; |
| 1861 | int retval = FALSE; |
| 1862 | Status status; |
| 1863 | |
| 1864 | if (get_x11_windis() == OK) |
| 1865 | { |
| 1866 | /* Get window/icon name if any */ |
| 1867 | if (get_title) |
| 1868 | status = XGetWMName(x11_display, x11_window, &text_prop); |
| 1869 | else |
| 1870 | status = XGetWMIconName(x11_display, x11_window, &text_prop); |
| 1871 | |
| 1872 | /* |
| 1873 | * If terminal is xterm, then x11_window may be a child window of the |
| 1874 | * outer xterm window that actually contains the window/icon name, so |
| 1875 | * keep traversing up the tree until a window with a title/icon is |
| 1876 | * found. |
| 1877 | */ |
| 1878 | /* Previously this was only done for xterm and alikes. I don't see a |
| 1879 | * reason why it would fail for other terminal emulators. |
| 1880 | * if (term_is_xterm) */ |
| 1881 | { |
| 1882 | Window root; |
| 1883 | Window parent; |
| 1884 | Window win = x11_window; |
| 1885 | Window *children; |
| 1886 | unsigned int num_children; |
| 1887 | |
| 1888 | while (!status || text_prop.value == NULL) |
| 1889 | { |
| 1890 | if (!XQueryTree(x11_display, win, &root, &parent, &children, |
| 1891 | &num_children)) |
| 1892 | break; |
| 1893 | if (children) |
| 1894 | XFree((void *)children); |
| 1895 | if (parent == root || parent == 0) |
| 1896 | break; |
| 1897 | |
| 1898 | win = parent; |
| 1899 | if (get_title) |
| 1900 | status = XGetWMName(x11_display, win, &text_prop); |
| 1901 | else |
| 1902 | status = XGetWMIconName(x11_display, win, &text_prop); |
| 1903 | } |
| 1904 | } |
| 1905 | if (status && text_prop.value != NULL) |
| 1906 | { |
| 1907 | retval = TRUE; |
| 1908 | if (!test_only) |
| 1909 | { |
Bram Moolenaar | f82bac3 | 2010-07-25 22:30:20 +0200 | [diff] [blame] | 1910 | #if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE) |
| 1911 | if (text_prop.encoding == XA_STRING |
| 1912 | # ifdef FEAT_MBYTE |
| 1913 | && !has_mbyte |
| 1914 | # endif |
| 1915 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1916 | { |
| 1917 | #endif |
| 1918 | if (get_title) |
| 1919 | oldtitle = vim_strsave((char_u *)text_prop.value); |
| 1920 | else |
| 1921 | oldicon = vim_strsave((char_u *)text_prop.value); |
Bram Moolenaar | f82bac3 | 2010-07-25 22:30:20 +0200 | [diff] [blame] | 1922 | #if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1923 | } |
| 1924 | else |
| 1925 | { |
| 1926 | char **cl; |
| 1927 | Status transform_status; |
| 1928 | int n = 0; |
| 1929 | |
| 1930 | transform_status = XmbTextPropertyToTextList(x11_display, |
| 1931 | &text_prop, |
| 1932 | &cl, &n); |
| 1933 | if (transform_status >= Success && n > 0 && cl[0]) |
| 1934 | { |
| 1935 | if (get_title) |
| 1936 | oldtitle = vim_strsave((char_u *) cl[0]); |
| 1937 | else |
| 1938 | oldicon = vim_strsave((char_u *) cl[0]); |
| 1939 | XFreeStringList(cl); |
| 1940 | } |
| 1941 | else |
| 1942 | { |
| 1943 | if (get_title) |
| 1944 | oldtitle = vim_strsave((char_u *)text_prop.value); |
| 1945 | else |
| 1946 | oldicon = vim_strsave((char_u *)text_prop.value); |
| 1947 | } |
| 1948 | } |
| 1949 | #endif |
| 1950 | } |
| 1951 | XFree((void *)text_prop.value); |
| 1952 | } |
| 1953 | } |
| 1954 | return retval; |
| 1955 | } |
| 1956 | |
Bram Moolenaar | cbc246a | 2014-10-11 14:47:26 +0200 | [diff] [blame] | 1957 | /* Xutf8 functions are not avaialble on older systems. Note that on some |
| 1958 | * systems X_HAVE_UTF8_STRING may be defined in a header file but |
| 1959 | * Xutf8SetWMProperties() is not in the X11 library. Configure checks for |
| 1960 | * that and defines HAVE_XUTF8SETWMPROPERTIES. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1961 | #if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE) |
Bram Moolenaar | cbc246a | 2014-10-11 14:47:26 +0200 | [diff] [blame] | 1962 | # if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1963 | # define USE_UTF8_STRING |
| 1964 | # endif |
| 1965 | #endif |
| 1966 | |
| 1967 | /* |
| 1968 | * Set x11 Window Title |
| 1969 | * |
| 1970 | * get_x11_windis() must be called before this and have returned OK |
| 1971 | */ |
| 1972 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1973 | set_x11_title(char_u *title) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1974 | { |
| 1975 | /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING |
| 1976 | * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't |
| 1977 | * supported everywhere and STRING doesn't work for multi-byte titles. |
| 1978 | */ |
| 1979 | #ifdef USE_UTF8_STRING |
| 1980 | if (enc_utf8) |
| 1981 | Xutf8SetWMProperties(x11_display, x11_window, (const char *)title, |
| 1982 | NULL, NULL, 0, NULL, NULL, NULL); |
| 1983 | else |
| 1984 | #endif |
| 1985 | { |
| 1986 | #if XtSpecificationRelease >= 4 |
| 1987 | # ifdef FEAT_XFONTSET |
| 1988 | XmbSetWMProperties(x11_display, x11_window, (const char *)title, |
| 1989 | NULL, NULL, 0, NULL, NULL, NULL); |
| 1990 | # else |
| 1991 | XTextProperty text_prop; |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 1992 | char *c_title = (char *)title; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1993 | |
| 1994 | /* directly from example 3-18 "basicwin" of Xlib Programming Manual */ |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 1995 | (void)XStringListToTextProperty(&c_title, 1, &text_prop); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1996 | XSetWMProperties(x11_display, x11_window, &text_prop, |
| 1997 | NULL, NULL, 0, NULL, NULL, NULL); |
| 1998 | # endif |
| 1999 | #else |
| 2000 | XStoreName(x11_display, x11_window, (char *)title); |
| 2001 | #endif |
| 2002 | } |
| 2003 | XFlush(x11_display); |
| 2004 | } |
| 2005 | |
| 2006 | /* |
| 2007 | * Set x11 Window icon |
| 2008 | * |
| 2009 | * get_x11_windis() must be called before this and have returned OK |
| 2010 | */ |
| 2011 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2012 | set_x11_icon(char_u *icon) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2013 | { |
| 2014 | /* See above for comments about using X*SetWMProperties(). */ |
| 2015 | #ifdef USE_UTF8_STRING |
| 2016 | if (enc_utf8) |
| 2017 | Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon, |
| 2018 | NULL, 0, NULL, NULL, NULL); |
| 2019 | else |
| 2020 | #endif |
| 2021 | { |
| 2022 | #if XtSpecificationRelease >= 4 |
| 2023 | # ifdef FEAT_XFONTSET |
| 2024 | XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon, |
| 2025 | NULL, 0, NULL, NULL, NULL); |
| 2026 | # else |
| 2027 | XTextProperty text_prop; |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 2028 | char *c_icon = (char *)icon; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2029 | |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 2030 | (void)XStringListToTextProperty(&c_icon, 1, &text_prop); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2031 | XSetWMProperties(x11_display, x11_window, NULL, &text_prop, |
| 2032 | NULL, 0, NULL, NULL, NULL); |
| 2033 | # endif |
| 2034 | #else |
| 2035 | XSetIconName(x11_display, x11_window, (char *)icon); |
| 2036 | #endif |
| 2037 | } |
| 2038 | XFlush(x11_display); |
| 2039 | } |
| 2040 | |
| 2041 | #else /* FEAT_X11 */ |
| 2042 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2043 | static int |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2044 | get_x11_title(int test_only UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2045 | { |
| 2046 | return FALSE; |
| 2047 | } |
| 2048 | |
| 2049 | static int |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2050 | get_x11_icon(int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2051 | { |
| 2052 | if (!test_only) |
| 2053 | { |
| 2054 | if (STRNCMP(T_NAME, "builtin_", 8) == 0) |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 2055 | oldicon = vim_strsave(T_NAME + 8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2056 | else |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 2057 | oldicon = vim_strsave(T_NAME); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2058 | } |
| 2059 | return FALSE; |
| 2060 | } |
| 2061 | |
| 2062 | #endif /* FEAT_X11 */ |
| 2063 | |
| 2064 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2065 | mch_can_restore_title(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2066 | { |
| 2067 | return get_x11_title(TRUE); |
| 2068 | } |
| 2069 | |
| 2070 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2071 | mch_can_restore_icon(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2072 | { |
| 2073 | return get_x11_icon(TRUE); |
| 2074 | } |
| 2075 | |
| 2076 | /* |
| 2077 | * Set the window title and icon. |
| 2078 | */ |
| 2079 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2080 | mch_settitle(char_u *title, char_u *icon) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2081 | { |
| 2082 | int type = 0; |
| 2083 | static int recursive = 0; |
| 2084 | |
| 2085 | if (T_NAME == NULL) /* no terminal name (yet) */ |
| 2086 | return; |
| 2087 | if (title == NULL && icon == NULL) /* nothing to do */ |
| 2088 | return; |
| 2089 | |
| 2090 | /* When one of the X11 functions causes a deadly signal, we get here again |
| 2091 | * recursively. Avoid hanging then (something is probably locked). */ |
| 2092 | if (recursive) |
| 2093 | return; |
| 2094 | ++recursive; |
| 2095 | |
| 2096 | /* |
| 2097 | * if the window ID and the display is known, we may use X11 calls |
| 2098 | */ |
| 2099 | #ifdef FEAT_X11 |
| 2100 | if (get_x11_windis() == OK) |
| 2101 | type = 1; |
| 2102 | #else |
| 2103 | # if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK) |
| 2104 | if (gui.in_use) |
| 2105 | type = 1; |
| 2106 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2107 | #endif |
| 2108 | |
| 2109 | /* |
Bram Moolenaar | f82bac3 | 2010-07-25 22:30:20 +0200 | [diff] [blame] | 2110 | * 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] | 2111 | * than x11 calls, because the x11 calls don't always work |
| 2112 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2113 | if ((type || *T_TS != NUL) && title != NULL) |
| 2114 | { |
| 2115 | if (oldtitle == NULL |
| 2116 | #ifdef FEAT_GUI |
| 2117 | && !gui.in_use |
| 2118 | #endif |
| 2119 | ) /* first call but not in GUI, save title */ |
| 2120 | (void)get_x11_title(FALSE); |
| 2121 | |
| 2122 | if (*T_TS != NUL) /* it's OK if t_fs is empty */ |
| 2123 | term_settitle(title); |
| 2124 | #ifdef FEAT_X11 |
| 2125 | else |
| 2126 | # ifdef FEAT_GUI_GTK |
| 2127 | if (!gui.in_use) /* don't do this if GTK+ is running */ |
| 2128 | # endif |
| 2129 | set_x11_title(title); /* x11 */ |
| 2130 | #endif |
Bram Moolenaar | 2fa15e6 | 2005-01-04 21:23:48 +0000 | [diff] [blame] | 2131 | #if defined(FEAT_GUI_GTK) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2132 | || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) |
| 2133 | else |
| 2134 | gui_mch_settitle(title, icon); |
| 2135 | #endif |
| 2136 | did_set_title = TRUE; |
| 2137 | } |
| 2138 | |
| 2139 | if ((type || *T_CIS != NUL) && icon != NULL) |
| 2140 | { |
| 2141 | if (oldicon == NULL |
| 2142 | #ifdef FEAT_GUI |
| 2143 | && !gui.in_use |
| 2144 | #endif |
| 2145 | ) /* first call, save icon */ |
| 2146 | get_x11_icon(FALSE); |
| 2147 | |
| 2148 | if (*T_CIS != NUL) |
| 2149 | { |
| 2150 | out_str(T_CIS); /* set icon start */ |
| 2151 | out_str_nf(icon); |
| 2152 | out_str(T_CIE); /* set icon end */ |
| 2153 | out_flush(); |
| 2154 | } |
| 2155 | #ifdef FEAT_X11 |
| 2156 | else |
| 2157 | # ifdef FEAT_GUI_GTK |
| 2158 | if (!gui.in_use) /* don't do this if GTK+ is running */ |
| 2159 | # endif |
| 2160 | set_x11_icon(icon); /* x11 */ |
| 2161 | #endif |
| 2162 | did_set_icon = TRUE; |
| 2163 | } |
| 2164 | --recursive; |
| 2165 | } |
| 2166 | |
| 2167 | /* |
| 2168 | * Restore the window/icon title. |
| 2169 | * "which" is one of: |
| 2170 | * 1 only restore title |
| 2171 | * 2 only restore icon |
| 2172 | * 3 restore title and icon |
| 2173 | */ |
| 2174 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2175 | mch_restore_title(int which) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2176 | { |
| 2177 | /* only restore the title or icon when it has been set */ |
| 2178 | mch_settitle(((which & 1) && did_set_title) ? |
| 2179 | (oldtitle ? oldtitle : p_titleold) : NULL, |
| 2180 | ((which & 2) && did_set_icon) ? oldicon : NULL); |
| 2181 | } |
| 2182 | |
| 2183 | #endif /* FEAT_TITLE */ |
| 2184 | |
| 2185 | /* |
| 2186 | * Return TRUE if "name" looks like some xterm name. |
Bram Moolenaar | 3a7c85b | 2005-02-05 21:39:53 +0000 | [diff] [blame] | 2187 | * Seiichi Sato mentioned that "mlterm" works like xterm. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2188 | */ |
| 2189 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2190 | vim_is_xterm(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2191 | { |
| 2192 | if (name == NULL) |
| 2193 | return FALSE; |
| 2194 | return (STRNICMP(name, "xterm", 5) == 0 |
| 2195 | || STRNICMP(name, "nxterm", 6) == 0 |
| 2196 | || STRNICMP(name, "kterm", 5) == 0 |
Bram Moolenaar | 3a7c85b | 2005-02-05 21:39:53 +0000 | [diff] [blame] | 2197 | || STRNICMP(name, "mlterm", 6) == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2198 | || STRNICMP(name, "rxvt", 4) == 0 |
| 2199 | || STRCMP(name, "builtin_xterm") == 0); |
| 2200 | } |
| 2201 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2202 | #if defined(FEAT_MOUSE_XTERM) || defined(PROTO) |
| 2203 | /* |
| 2204 | * Return TRUE if "name" appears to be that of a terminal |
| 2205 | * known to support the xterm-style mouse protocol. |
| 2206 | * Relies on term_is_xterm having been set to its correct value. |
| 2207 | */ |
| 2208 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2209 | use_xterm_like_mouse(char_u *name) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2210 | { |
| 2211 | return (name != NULL |
| 2212 | && (term_is_xterm || STRNICMP(name, "screen", 6) == 0)); |
| 2213 | } |
| 2214 | #endif |
| 2215 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2216 | #if defined(FEAT_MOUSE_TTY) || defined(PROTO) |
| 2217 | /* |
| 2218 | * Return non-zero when using an xterm mouse, according to 'ttymouse'. |
| 2219 | * Return 1 for "xterm". |
| 2220 | * Return 2 for "xterm2". |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 2221 | * Return 3 for "urxvt". |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 2222 | * Return 4 for "sgr". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2223 | */ |
| 2224 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2225 | use_xterm_mouse(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2226 | { |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 2227 | if (ttym_flags == TTYM_SGR) |
| 2228 | return 4; |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 2229 | if (ttym_flags == TTYM_URXVT) |
| 2230 | return 3; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2231 | if (ttym_flags == TTYM_XTERM2) |
| 2232 | return 2; |
| 2233 | if (ttym_flags == TTYM_XTERM) |
| 2234 | return 1; |
| 2235 | return 0; |
| 2236 | } |
| 2237 | #endif |
| 2238 | |
| 2239 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2240 | vim_is_iris(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2241 | { |
| 2242 | if (name == NULL) |
| 2243 | return FALSE; |
| 2244 | return (STRNICMP(name, "iris-ansi", 9) == 0 |
| 2245 | || STRCMP(name, "builtin_iris-ansi") == 0); |
| 2246 | } |
| 2247 | |
| 2248 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2249 | vim_is_vt300(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2250 | { |
| 2251 | if (name == NULL) |
| 2252 | return FALSE; /* actually all ANSI comp. terminals should be here */ |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 2253 | /* catch VT100 - VT5xx */ |
| 2254 | return ((STRNICMP(name, "vt", 2) == 0 |
| 2255 | && vim_strchr((char_u *)"12345", name[2]) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2256 | || STRCMP(name, "builtin_vt320") == 0); |
| 2257 | } |
| 2258 | |
| 2259 | /* |
| 2260 | * Return TRUE if "name" is a terminal for which 'ttyfast' should be set. |
| 2261 | * This should include all windowed terminal emulators. |
| 2262 | */ |
| 2263 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2264 | vim_is_fastterm(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2265 | { |
| 2266 | if (name == NULL) |
| 2267 | return FALSE; |
| 2268 | if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name)) |
| 2269 | return TRUE; |
| 2270 | return ( STRNICMP(name, "hpterm", 6) == 0 |
| 2271 | || STRNICMP(name, "sun-cmd", 7) == 0 |
| 2272 | || STRNICMP(name, "screen", 6) == 0 |
| 2273 | || STRNICMP(name, "dtterm", 6) == 0); |
| 2274 | } |
| 2275 | |
| 2276 | /* |
| 2277 | * Insert user name in s[len]. |
| 2278 | * Return OK if a name found. |
| 2279 | */ |
| 2280 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2281 | mch_get_user_name(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2282 | { |
| 2283 | #ifdef VMS |
Bram Moolenaar | ffb8ab0 | 2005-09-07 21:15:32 +0000 | [diff] [blame] | 2284 | vim_strncpy(s, (char_u *)cuserid(NULL), len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2285 | return OK; |
| 2286 | #else |
| 2287 | return mch_get_uname(getuid(), s, len); |
| 2288 | #endif |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * Insert user name for "uid" in s[len]. |
| 2293 | * Return OK if a name found. |
| 2294 | */ |
| 2295 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2296 | mch_get_uname(uid_t uid, char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2297 | { |
| 2298 | #if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID) |
| 2299 | struct passwd *pw; |
| 2300 | |
| 2301 | if ((pw = getpwuid(uid)) != NULL |
| 2302 | && pw->pw_name != NULL && *(pw->pw_name) != NUL) |
| 2303 | { |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 2304 | vim_strncpy(s, (char_u *)pw->pw_name, len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2305 | return OK; |
| 2306 | } |
| 2307 | #endif |
| 2308 | sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */ |
| 2309 | return FAIL; /* a number is not a name */ |
| 2310 | } |
| 2311 | |
| 2312 | /* |
| 2313 | * Insert host name is s[len]. |
| 2314 | */ |
| 2315 | |
| 2316 | #ifdef HAVE_SYS_UTSNAME_H |
| 2317 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2318 | mch_get_host_name(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2319 | { |
| 2320 | struct utsname vutsname; |
| 2321 | |
| 2322 | if (uname(&vutsname) < 0) |
| 2323 | *s = NUL; |
| 2324 | else |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 2325 | vim_strncpy(s, (char_u *)vutsname.nodename, len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2326 | } |
| 2327 | #else /* HAVE_SYS_UTSNAME_H */ |
| 2328 | |
| 2329 | # ifdef HAVE_SYS_SYSTEMINFO_H |
| 2330 | # define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len) |
| 2331 | # endif |
| 2332 | |
| 2333 | void |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2334 | mch_get_host_name(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2335 | { |
| 2336 | # ifdef VAXC |
| 2337 | vaxc$gethostname((char *)s, len); |
| 2338 | # else |
| 2339 | gethostname((char *)s, len); |
| 2340 | # endif |
| 2341 | s[len - 1] = NUL; /* make sure it's terminated */ |
| 2342 | } |
| 2343 | #endif /* HAVE_SYS_UTSNAME_H */ |
| 2344 | |
| 2345 | /* |
| 2346 | * return process ID |
| 2347 | */ |
| 2348 | long |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2349 | mch_get_pid(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2350 | { |
| 2351 | return (long)getpid(); |
| 2352 | } |
| 2353 | |
| 2354 | #if !defined(HAVE_STRERROR) && defined(USE_GETCWD) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 2355 | static char *strerror(int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2356 | |
| 2357 | static char * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2358 | strerror(int err) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2359 | { |
| 2360 | extern int sys_nerr; |
| 2361 | extern char *sys_errlist[]; |
| 2362 | static char er[20]; |
| 2363 | |
| 2364 | if (err > 0 && err < sys_nerr) |
| 2365 | return (sys_errlist[err]); |
| 2366 | sprintf(er, "Error %d", err); |
| 2367 | return er; |
| 2368 | } |
| 2369 | #endif |
| 2370 | |
| 2371 | /* |
| 2372 | * Get name of current directory into buffer 'buf' of length 'len' bytes. |
| 2373 | * Return OK for success, FAIL for failure. |
| 2374 | */ |
| 2375 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2376 | mch_dirname(char_u *buf, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2377 | { |
| 2378 | #if defined(USE_GETCWD) |
| 2379 | if (getcwd((char *)buf, len) == NULL) |
| 2380 | { |
| 2381 | STRCPY(buf, strerror(errno)); |
| 2382 | return FAIL; |
| 2383 | } |
| 2384 | return OK; |
| 2385 | #else |
| 2386 | return (getwd((char *)buf) != NULL ? OK : FAIL); |
| 2387 | #endif |
| 2388 | } |
| 2389 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2390 | /* |
Bram Moolenaar | 3d20ca1 | 2006-11-28 16:43:58 +0000 | [diff] [blame] | 2391 | * Get absolute file name into "buf[len]". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2392 | * |
| 2393 | * return FAIL for failure, OK for success |
| 2394 | */ |
| 2395 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2396 | mch_FullName( |
| 2397 | char_u *fname, |
| 2398 | char_u *buf, |
| 2399 | int len, |
| 2400 | int force) /* also expand when already absolute path */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2401 | { |
| 2402 | int l; |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2403 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2404 | int fd = -1; |
| 2405 | static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */ |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2406 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2407 | char_u olddir[MAXPATHL]; |
| 2408 | char_u *p; |
| 2409 | int retval = OK; |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2410 | #ifdef __CYGWIN__ |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2411 | char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but |
| 2412 | it's not always defined */ |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2413 | #endif |
| 2414 | |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2415 | #ifdef VMS |
| 2416 | fname = vms_fixfilename(fname); |
| 2417 | #endif |
| 2418 | |
Bram Moolenaar | a244243 | 2007-04-26 14:26:37 +0000 | [diff] [blame] | 2419 | #ifdef __CYGWIN__ |
| 2420 | /* |
| 2421 | * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts". |
| 2422 | */ |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2423 | # if CYGWIN_VERSION_DLL_MAJOR >= 1007 |
Bram Moolenaar | 06b0734 | 2015-12-31 22:26:28 +0100 | [diff] [blame] | 2424 | /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in |
| 2425 | * a forward slash. */ |
| 2426 | cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE, |
| 2427 | fname, posix_fname, MAXPATHL); |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2428 | # else |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2429 | cygwin_conv_to_posix_path(fname, posix_fname); |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2430 | # endif |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2431 | fname = posix_fname; |
Bram Moolenaar | a244243 | 2007-04-26 14:26:37 +0000 | [diff] [blame] | 2432 | #endif |
| 2433 | |
Bram Moolenaar | e3303cb | 2015-12-31 18:29:46 +0100 | [diff] [blame] | 2434 | /* Expand it if forced or not an absolute path. |
| 2435 | * Do not do it for "/file", the result is always "/". */ |
| 2436 | if ((force || !mch_isFullName(fname)) |
| 2437 | && ((p = vim_strrchr(fname, '/')) == NULL || p != fname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2438 | { |
| 2439 | /* |
| 2440 | * If the file name has a path, change to that directory for a moment, |
| 2441 | * and then do the getwd() (and get back to where we were). |
| 2442 | * This will get the correct path name with "../" things. |
| 2443 | */ |
Bram Moolenaar | e3303cb | 2015-12-31 18:29:46 +0100 | [diff] [blame] | 2444 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | { |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2446 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2447 | /* |
| 2448 | * Use fchdir() if possible, it's said to be faster and more |
| 2449 | * reliable. But on SunOS 4 it might not work. Check this by |
| 2450 | * doing a fchdir() right now. |
| 2451 | */ |
| 2452 | if (!dont_fchdir) |
| 2453 | { |
| 2454 | fd = open(".", O_RDONLY | O_EXTRA, 0); |
| 2455 | if (fd >= 0 && fchdir(fd) < 0) |
| 2456 | { |
| 2457 | close(fd); |
| 2458 | fd = -1; |
| 2459 | dont_fchdir = TRUE; /* don't try again */ |
| 2460 | } |
| 2461 | } |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2462 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2463 | |
| 2464 | /* Only change directory when we are sure we can return to where |
| 2465 | * we are now. After doing "su" chdir(".") might not work. */ |
| 2466 | if ( |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2467 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2468 | fd < 0 && |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2469 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2470 | (mch_dirname(olddir, MAXPATHL) == FAIL |
| 2471 | || mch_chdir((char *)olddir) != 0)) |
| 2472 | { |
| 2473 | p = NULL; /* can't get current dir: don't chdir */ |
| 2474 | retval = FAIL; |
| 2475 | } |
| 2476 | else |
| 2477 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2478 | /* The directory is copied into buf[], to be able to remove |
| 2479 | * the file name without changing it (could be a string in |
| 2480 | * read-only memory) */ |
| 2481 | if (p - fname >= len) |
| 2482 | retval = FAIL; |
| 2483 | else |
| 2484 | { |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 2485 | vim_strncpy(buf, fname, p - fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2486 | if (mch_chdir((char *)buf)) |
| 2487 | retval = FAIL; |
| 2488 | else |
| 2489 | fname = p + 1; |
| 2490 | *buf = NUL; |
| 2491 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2492 | } |
| 2493 | } |
| 2494 | if (mch_dirname(buf, len) == FAIL) |
| 2495 | { |
| 2496 | retval = FAIL; |
| 2497 | *buf = NUL; |
| 2498 | } |
| 2499 | if (p != NULL) |
| 2500 | { |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2501 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2502 | if (fd >= 0) |
| 2503 | { |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 2504 | if (p_verbose >= 5) |
| 2505 | { |
| 2506 | verbose_enter(); |
| 2507 | MSG("fchdir() to previous dir"); |
| 2508 | verbose_leave(); |
| 2509 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2510 | l = fchdir(fd); |
| 2511 | close(fd); |
| 2512 | } |
| 2513 | else |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2514 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2515 | l = mch_chdir((char *)olddir); |
| 2516 | if (l != 0) |
| 2517 | EMSG(_(e_prev_dir)); |
| 2518 | } |
| 2519 | |
| 2520 | l = STRLEN(buf); |
Bram Moolenaar | dac7569 | 2012-10-14 04:35:45 +0200 | [diff] [blame] | 2521 | if (l >= len - 1) |
| 2522 | retval = FAIL; /* no space for trailing "/" */ |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2523 | #ifndef VMS |
Bram Moolenaar | dac7569 | 2012-10-14 04:35:45 +0200 | [diff] [blame] | 2524 | else if (l > 0 && buf[l - 1] != '/' && *fname != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2525 | && STRCMP(fname, ".") != 0) |
Bram Moolenaar | dac7569 | 2012-10-14 04:35:45 +0200 | [diff] [blame] | 2526 | STRCAT(buf, "/"); |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2527 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2528 | } |
Bram Moolenaar | 3d20ca1 | 2006-11-28 16:43:58 +0000 | [diff] [blame] | 2529 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2530 | /* Catch file names which are too long. */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 2531 | if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2532 | return FAIL; |
| 2533 | |
| 2534 | /* Do not append ".", "/dir/." is equal to "/dir". */ |
| 2535 | if (STRCMP(fname, ".") != 0) |
| 2536 | STRCAT(buf, fname); |
| 2537 | |
| 2538 | return OK; |
| 2539 | } |
| 2540 | |
| 2541 | /* |
| 2542 | * Return TRUE if "fname" does not depend on the current directory. |
| 2543 | */ |
| 2544 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2545 | mch_isFullName(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2546 | { |
| 2547 | #ifdef __EMX__ |
| 2548 | return _fnisabs(fname); |
| 2549 | #else |
| 2550 | # ifdef VMS |
| 2551 | return ( fname[0] == '/' || fname[0] == '.' || |
| 2552 | strchr((char *)fname,':') || strchr((char *)fname,'"') || |
| 2553 | (strchr((char *)fname,'[') && strchr((char *)fname,']'))|| |
| 2554 | (strchr((char *)fname,'<') && strchr((char *)fname,'>')) ); |
| 2555 | # else |
| 2556 | return (*fname == '/' || *fname == '~'); |
| 2557 | # endif |
| 2558 | #endif |
| 2559 | } |
| 2560 | |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2561 | #if defined(USE_FNAME_CASE) || defined(PROTO) |
| 2562 | /* |
| 2563 | * Set the case of the file name, if it already exists. This will cause the |
| 2564 | * file name to remain exactly the same. |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 2565 | * Only required for file systems where case is ignored and preserved. |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2566 | */ |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2567 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2568 | fname_case( |
| 2569 | char_u *name, |
| 2570 | int len UNUSED) /* buffer size, only used when name gets longer */ |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2571 | { |
| 2572 | struct stat st; |
| 2573 | char_u *slash, *tail; |
| 2574 | DIR *dirp; |
| 2575 | struct dirent *dp; |
| 2576 | |
| 2577 | if (lstat((char *)name, &st) >= 0) |
| 2578 | { |
| 2579 | /* Open the directory where the file is located. */ |
| 2580 | slash = vim_strrchr(name, '/'); |
| 2581 | if (slash == NULL) |
| 2582 | { |
| 2583 | dirp = opendir("."); |
| 2584 | tail = name; |
| 2585 | } |
| 2586 | else |
| 2587 | { |
| 2588 | *slash = NUL; |
| 2589 | dirp = opendir((char *)name); |
| 2590 | *slash = '/'; |
| 2591 | tail = slash + 1; |
| 2592 | } |
| 2593 | |
| 2594 | if (dirp != NULL) |
| 2595 | { |
| 2596 | while ((dp = readdir(dirp)) != NULL) |
| 2597 | { |
| 2598 | /* Only accept names that differ in case and are the same byte |
| 2599 | * length. TODO: accept different length name. */ |
| 2600 | if (STRICMP(tail, dp->d_name) == 0 |
| 2601 | && STRLEN(tail) == STRLEN(dp->d_name)) |
| 2602 | { |
| 2603 | char_u newname[MAXPATHL + 1]; |
| 2604 | struct stat st2; |
| 2605 | |
| 2606 | /* Verify the inode is equal. */ |
| 2607 | vim_strncpy(newname, name, MAXPATHL); |
| 2608 | vim_strncpy(newname + (tail - name), (char_u *)dp->d_name, |
| 2609 | MAXPATHL - (tail - name)); |
| 2610 | if (lstat((char *)newname, &st2) >= 0 |
| 2611 | && st.st_ino == st2.st_ino |
| 2612 | && st.st_dev == st2.st_dev) |
| 2613 | { |
| 2614 | STRCPY(tail, dp->d_name); |
| 2615 | break; |
| 2616 | } |
| 2617 | } |
| 2618 | } |
| 2619 | |
| 2620 | closedir(dirp); |
| 2621 | } |
| 2622 | } |
| 2623 | } |
| 2624 | #endif |
| 2625 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2626 | /* |
| 2627 | * Get file permissions for 'name'. |
| 2628 | * Returns -1 when it doesn't exist. |
| 2629 | */ |
| 2630 | long |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2631 | mch_getperm(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2632 | { |
| 2633 | struct stat statb; |
| 2634 | |
| 2635 | /* Keep the #ifdef outside of stat(), it may be a macro. */ |
| 2636 | #ifdef VMS |
| 2637 | if (stat((char *)vms_fixfilename(name), &statb)) |
| 2638 | #else |
| 2639 | if (stat((char *)name, &statb)) |
| 2640 | #endif |
| 2641 | return -1; |
Bram Moolenaar | 708f62c | 2007-08-11 20:24:10 +0000 | [diff] [blame] | 2642 | #ifdef __INTERIX |
| 2643 | /* The top bit makes the value negative, which means the file doesn't |
| 2644 | * exist. Remove the bit, we don't use it. */ |
| 2645 | return statb.st_mode & ~S_ADDACE; |
| 2646 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2647 | return statb.st_mode; |
Bram Moolenaar | 708f62c | 2007-08-11 20:24:10 +0000 | [diff] [blame] | 2648 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | /* |
| 2652 | * set file permission for 'name' to 'perm' |
| 2653 | * |
| 2654 | * return FAIL for failure, OK otherwise |
| 2655 | */ |
| 2656 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2657 | mch_setperm(char_u *name, long perm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2658 | { |
| 2659 | return (chmod((char *) |
| 2660 | #ifdef VMS |
| 2661 | vms_fixfilename(name), |
| 2662 | #else |
| 2663 | name, |
| 2664 | #endif |
| 2665 | (mode_t)perm) == 0 ? OK : FAIL); |
| 2666 | } |
| 2667 | |
| 2668 | #if defined(HAVE_ACL) || defined(PROTO) |
| 2669 | # ifdef HAVE_SYS_ACL_H |
| 2670 | # include <sys/acl.h> |
| 2671 | # endif |
| 2672 | # ifdef HAVE_SYS_ACCESS_H |
| 2673 | # include <sys/access.h> |
| 2674 | # endif |
| 2675 | |
| 2676 | # ifdef HAVE_SOLARIS_ACL |
| 2677 | typedef struct vim_acl_solaris_T { |
| 2678 | int acl_cnt; |
| 2679 | aclent_t *acl_entry; |
| 2680 | } vim_acl_solaris_T; |
| 2681 | # endif |
| 2682 | |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 2683 | #if defined(HAVE_SELINUX) || defined(PROTO) |
| 2684 | /* |
| 2685 | * Copy security info from "from_file" to "to_file". |
| 2686 | */ |
| 2687 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2688 | mch_copy_sec(char_u *from_file, char_u *to_file) |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 2689 | { |
| 2690 | if (from_file == NULL) |
| 2691 | return; |
| 2692 | |
| 2693 | if (selinux_enabled == -1) |
| 2694 | selinux_enabled = is_selinux_enabled(); |
| 2695 | |
| 2696 | if (selinux_enabled > 0) |
| 2697 | { |
| 2698 | security_context_t from_context = NULL; |
| 2699 | security_context_t to_context = NULL; |
| 2700 | |
| 2701 | if (getfilecon((char *)from_file, &from_context) < 0) |
| 2702 | { |
| 2703 | /* If the filesystem doesn't support extended attributes, |
| 2704 | the original had no special security context and the |
| 2705 | target cannot have one either. */ |
| 2706 | if (errno == EOPNOTSUPP) |
| 2707 | return; |
| 2708 | |
| 2709 | MSG_PUTS(_("\nCould not get security context for ")); |
| 2710 | msg_outtrans(from_file); |
| 2711 | msg_putchar('\n'); |
| 2712 | return; |
| 2713 | } |
| 2714 | if (getfilecon((char *)to_file, &to_context) < 0) |
| 2715 | { |
| 2716 | MSG_PUTS(_("\nCould not get security context for ")); |
| 2717 | msg_outtrans(to_file); |
| 2718 | msg_putchar('\n'); |
| 2719 | freecon (from_context); |
| 2720 | return ; |
| 2721 | } |
| 2722 | if (strcmp(from_context, to_context) != 0) |
| 2723 | { |
| 2724 | if (setfilecon((char *)to_file, from_context) < 0) |
| 2725 | { |
| 2726 | MSG_PUTS(_("\nCould not set security context for ")); |
| 2727 | msg_outtrans(to_file); |
| 2728 | msg_putchar('\n'); |
| 2729 | } |
| 2730 | } |
| 2731 | freecon(to_context); |
| 2732 | freecon(from_context); |
| 2733 | } |
| 2734 | } |
| 2735 | #endif /* HAVE_SELINUX */ |
| 2736 | |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2737 | #if defined(HAVE_SMACK) && !defined(PROTO) |
| 2738 | /* |
| 2739 | * Copy security info from "from_file" to "to_file". |
| 2740 | */ |
| 2741 | void |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2742 | mch_copy_sec(char_u *from_file, char_u *to_file) |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2743 | { |
Bram Moolenaar | 62f167f | 2014-04-23 12:52:40 +0200 | [diff] [blame] | 2744 | static const char * const smack_copied_attributes[] = |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2745 | { |
| 2746 | XATTR_NAME_SMACK, |
| 2747 | XATTR_NAME_SMACKEXEC, |
| 2748 | XATTR_NAME_SMACKMMAP |
| 2749 | }; |
| 2750 | |
| 2751 | char buffer[SMACK_LABEL_LEN]; |
| 2752 | const char *name; |
| 2753 | int index; |
| 2754 | int ret; |
| 2755 | ssize_t size; |
| 2756 | |
| 2757 | if (from_file == NULL) |
| 2758 | return; |
| 2759 | |
| 2760 | for (index = 0 ; index < (int)(sizeof(smack_copied_attributes) |
| 2761 | / sizeof(smack_copied_attributes)[0]) ; index++) |
| 2762 | { |
| 2763 | /* get the name of the attribute to copy */ |
| 2764 | name = smack_copied_attributes[index]; |
| 2765 | |
| 2766 | /* get the value of the attribute in buffer */ |
| 2767 | size = getxattr((char*)from_file, name, buffer, sizeof(buffer)); |
| 2768 | if (size >= 0) |
| 2769 | { |
| 2770 | /* copy the attribute value of buffer */ |
| 2771 | ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0); |
| 2772 | if (ret < 0) |
| 2773 | { |
Bram Moolenaar | 4a1314c | 2016-01-27 20:47:18 +0100 | [diff] [blame] | 2774 | vim_snprintf((char *)IObuff, IOSIZE, |
| 2775 | _("Could not set security context %s for %s"), |
| 2776 | name, to_file); |
| 2777 | msg_outtrans(IObuff); |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2778 | msg_putchar('\n'); |
| 2779 | } |
| 2780 | } |
| 2781 | else |
| 2782 | { |
| 2783 | /* what reason of not having the attribute value? */ |
| 2784 | switch (errno) |
| 2785 | { |
| 2786 | case ENOTSUP: |
| 2787 | /* extended attributes aren't supported or enabled */ |
| 2788 | /* should a message be echoed? not sure... */ |
| 2789 | return; /* leave because it isn't usefull to continue */ |
| 2790 | |
| 2791 | case ERANGE: |
| 2792 | default: |
| 2793 | /* no enough size OR unexpected error */ |
Bram Moolenaar | 4a1314c | 2016-01-27 20:47:18 +0100 | [diff] [blame] | 2794 | vim_snprintf((char *)IObuff, IOSIZE, |
| 2795 | _("Could not get security context %s for %s. Removing it!"), |
| 2796 | name, from_file); |
| 2797 | msg_puts(IObuff); |
| 2798 | msg_putchar('\n'); |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2799 | /* FALLTHROUGH to remove the attribute */ |
| 2800 | |
| 2801 | case ENODATA: |
| 2802 | /* no attribute of this name */ |
| 2803 | ret = removexattr((char*)to_file, name); |
Bram Moolenaar | 57a728d | 2014-04-02 23:09:26 +0200 | [diff] [blame] | 2804 | /* Silently ignore errors, apparently this happens when |
| 2805 | * smack is not actually being used. */ |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 2806 | break; |
| 2807 | } |
| 2808 | } |
| 2809 | } |
| 2810 | } |
| 2811 | #endif /* HAVE_SMACK */ |
| 2812 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2813 | /* |
| 2814 | * Return a pointer to the ACL of file "fname" in allocated memory. |
| 2815 | * Return NULL if the ACL is not available for whatever reason. |
| 2816 | */ |
| 2817 | vim_acl_T |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2818 | mch_get_acl(char_u *fname UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2819 | { |
| 2820 | vim_acl_T ret = NULL; |
| 2821 | #ifdef HAVE_POSIX_ACL |
| 2822 | ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS); |
| 2823 | #else |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2824 | #ifdef HAVE_SOLARIS_ZFS_ACL |
| 2825 | acl_t *aclent; |
| 2826 | |
| 2827 | if (acl_get((char *)fname, 0, &aclent) < 0) |
| 2828 | return NULL; |
| 2829 | ret = (vim_acl_T)aclent; |
| 2830 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2831 | #ifdef HAVE_SOLARIS_ACL |
| 2832 | vim_acl_solaris_T *aclent; |
| 2833 | |
| 2834 | aclent = malloc(sizeof(vim_acl_solaris_T)); |
| 2835 | if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0) |
| 2836 | { |
| 2837 | free(aclent); |
| 2838 | return NULL; |
| 2839 | } |
| 2840 | aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t)); |
| 2841 | if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0) |
| 2842 | { |
| 2843 | free(aclent->acl_entry); |
| 2844 | free(aclent); |
| 2845 | return NULL; |
| 2846 | } |
| 2847 | ret = (vim_acl_T)aclent; |
| 2848 | #else |
| 2849 | #if defined(HAVE_AIX_ACL) |
| 2850 | int aclsize; |
| 2851 | struct acl *aclent; |
| 2852 | |
| 2853 | aclsize = sizeof(struct acl); |
| 2854 | aclent = malloc(aclsize); |
| 2855 | if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0) |
| 2856 | { |
| 2857 | if (errno == ENOSPC) |
| 2858 | { |
| 2859 | aclsize = aclent->acl_len; |
| 2860 | aclent = realloc(aclent, aclsize); |
| 2861 | if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0) |
| 2862 | { |
| 2863 | free(aclent); |
| 2864 | return NULL; |
| 2865 | } |
| 2866 | } |
| 2867 | else |
| 2868 | { |
| 2869 | free(aclent); |
| 2870 | return NULL; |
| 2871 | } |
| 2872 | } |
| 2873 | ret = (vim_acl_T)aclent; |
| 2874 | #endif /* HAVE_AIX_ACL */ |
| 2875 | #endif /* HAVE_SOLARIS_ACL */ |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2876 | #endif /* HAVE_SOLARIS_ZFS_ACL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2877 | #endif /* HAVE_POSIX_ACL */ |
| 2878 | return ret; |
| 2879 | } |
| 2880 | |
| 2881 | /* |
| 2882 | * Set the ACL of file "fname" to "acl" (unless it's NULL). |
| 2883 | */ |
| 2884 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2885 | mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2886 | { |
| 2887 | if (aclent == NULL) |
| 2888 | return; |
| 2889 | #ifdef HAVE_POSIX_ACL |
| 2890 | acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent); |
| 2891 | #else |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2892 | #ifdef HAVE_SOLARIS_ZFS_ACL |
| 2893 | acl_set((char *)fname, (acl_t *)aclent); |
| 2894 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2895 | #ifdef HAVE_SOLARIS_ACL |
| 2896 | acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt, |
| 2897 | ((vim_acl_solaris_T *)aclent)->acl_entry); |
| 2898 | #else |
| 2899 | #ifdef HAVE_AIX_ACL |
| 2900 | chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len); |
| 2901 | #endif /* HAVE_AIX_ACL */ |
| 2902 | #endif /* HAVE_SOLARIS_ACL */ |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2903 | #endif /* HAVE_SOLARIS_ZFS_ACL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2904 | #endif /* HAVE_POSIX_ACL */ |
| 2905 | } |
| 2906 | |
| 2907 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2908 | mch_free_acl(vim_acl_T aclent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2909 | { |
| 2910 | if (aclent == NULL) |
| 2911 | return; |
| 2912 | #ifdef HAVE_POSIX_ACL |
| 2913 | acl_free((acl_t)aclent); |
| 2914 | #else |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2915 | #ifdef HAVE_SOLARIS_ZFS_ACL |
| 2916 | acl_free((acl_t *)aclent); |
| 2917 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2918 | #ifdef HAVE_SOLARIS_ACL |
| 2919 | free(((vim_acl_solaris_T *)aclent)->acl_entry); |
| 2920 | free(aclent); |
| 2921 | #else |
| 2922 | #ifdef HAVE_AIX_ACL |
| 2923 | free(aclent); |
| 2924 | #endif /* HAVE_AIX_ACL */ |
| 2925 | #endif /* HAVE_SOLARIS_ACL */ |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 2926 | #endif /* HAVE_SOLARIS_ZFS_ACL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2927 | #endif /* HAVE_POSIX_ACL */ |
| 2928 | } |
| 2929 | #endif |
| 2930 | |
| 2931 | /* |
| 2932 | * Set hidden flag for "name". |
| 2933 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2934 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2935 | mch_hide(char_u *name UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2936 | { |
| 2937 | /* can't hide a file */ |
| 2938 | } |
| 2939 | |
| 2940 | /* |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 2941 | * 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] | 2942 | * return FALSE if "name" is not a directory |
| 2943 | * return FALSE for error |
| 2944 | */ |
| 2945 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2946 | mch_isdir(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2947 | { |
| 2948 | struct stat statb; |
| 2949 | |
| 2950 | if (*name == NUL) /* Some stat()s don't flag "" as an error. */ |
| 2951 | return FALSE; |
| 2952 | if (stat((char *)name, &statb)) |
| 2953 | return FALSE; |
| 2954 | #ifdef _POSIX_SOURCE |
| 2955 | return (S_ISDIR(statb.st_mode) ? TRUE : FALSE); |
| 2956 | #else |
| 2957 | return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE); |
| 2958 | #endif |
| 2959 | } |
| 2960 | |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 2961 | /* |
| 2962 | * return TRUE if "name" is a directory, NOT a symlink to a directory |
| 2963 | * return FALSE if "name" is not a directory |
| 2964 | * return FALSE for error |
| 2965 | */ |
| 2966 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2967 | mch_isrealdir(char_u *name) |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 2968 | { |
| 2969 | struct stat statb; |
| 2970 | |
| 2971 | if (*name == NUL) /* Some stat()s don't flag "" as an error. */ |
| 2972 | return FALSE; |
| 2973 | if (lstat((char *)name, &statb)) |
| 2974 | return FALSE; |
| 2975 | #ifdef _POSIX_SOURCE |
| 2976 | return (S_ISDIR(statb.st_mode) ? TRUE : FALSE); |
| 2977 | #else |
| 2978 | return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE); |
| 2979 | #endif |
| 2980 | } |
| 2981 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 2982 | static int executable_file(char_u *name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2983 | |
| 2984 | /* |
| 2985 | * Return 1 if "name" is an executable file, 0 if not or it doesn't exist. |
| 2986 | */ |
| 2987 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2988 | executable_file(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2989 | { |
| 2990 | struct stat st; |
| 2991 | |
| 2992 | if (stat((char *)name, &st)) |
| 2993 | return 0; |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 2994 | #ifdef VMS |
| 2995 | /* Like on Unix system file can have executable rights but not necessarily |
| 2996 | * be an executable, but on Unix is not a default for an ordianry file to |
| 2997 | * have an executable flag - on VMS it is in most cases. |
| 2998 | * Therefore, this check does not have any sense - let keep us to the |
| 2999 | * conventions instead: |
| 3000 | * *.COM and *.EXE files are the executables - the rest are not. This is |
| 3001 | * not ideal but better then it was. |
| 3002 | */ |
| 3003 | int vms_executable = 0; |
| 3004 | if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0) |
| 3005 | { |
| 3006 | if (strstr(vms_tolower((char*)name),".exe") != NULL |
| 3007 | || strstr(vms_tolower((char*)name),".com")!= NULL) |
| 3008 | vms_executable = 1; |
| 3009 | } |
| 3010 | return vms_executable; |
| 3011 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3012 | 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] | 3013 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | /* |
| 3017 | * Return 1 if "name" can be found in $PATH and executed, 0 if not. |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 3018 | * If "use_path" is FALSE only check if "name" is executable. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3019 | * Return -1 if unknown. |
| 3020 | */ |
| 3021 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3022 | mch_can_exe(char_u *name, char_u **path, int use_path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3023 | { |
| 3024 | char_u *buf; |
| 3025 | char_u *p, *e; |
| 3026 | int retval; |
| 3027 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 3028 | /* When "use_path" is false and if it's an absolute or relative path don't |
| 3029 | * need to use $PATH. */ |
| 3030 | if (!use_path || mch_isFullName(name) || (name[0] == '.' |
| 3031 | && (name[1] == '/' || (name[1] == '.' && name[2] == '/')))) |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 3032 | { |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 3033 | /* There must be a path separator, files in the current directory |
| 3034 | * can't be executed. */ |
| 3035 | if (gettail(name) != name && executable_file(name)) |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3036 | { |
| 3037 | if (path != NULL) |
| 3038 | { |
| 3039 | if (name[0] == '.') |
| 3040 | *path = FullName_save(name, TRUE); |
| 3041 | else |
| 3042 | *path = vim_strsave(name); |
| 3043 | } |
| 3044 | return TRUE; |
| 3045 | } |
| 3046 | return FALSE; |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 3047 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3048 | |
| 3049 | p = (char_u *)getenv("PATH"); |
| 3050 | if (p == NULL || *p == NUL) |
| 3051 | return -1; |
| 3052 | buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2)); |
| 3053 | if (buf == NULL) |
| 3054 | return -1; |
| 3055 | |
| 3056 | /* |
| 3057 | * Walk through all entries in $PATH to check if "name" exists there and |
| 3058 | * is an executable file. |
| 3059 | */ |
| 3060 | for (;;) |
| 3061 | { |
| 3062 | e = (char_u *)strchr((char *)p, ':'); |
| 3063 | if (e == NULL) |
| 3064 | e = p + STRLEN(p); |
| 3065 | if (e - p <= 1) /* empty entry means current dir */ |
| 3066 | STRCPY(buf, "./"); |
| 3067 | else |
| 3068 | { |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 3069 | vim_strncpy(buf, p, e - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3070 | add_pathsep(buf); |
| 3071 | } |
| 3072 | STRCAT(buf, name); |
| 3073 | retval = executable_file(buf); |
| 3074 | if (retval == 1) |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3075 | { |
| 3076 | if (path != NULL) |
| 3077 | { |
| 3078 | if (buf[0] == '.') |
| 3079 | *path = FullName_save(buf, TRUE); |
| 3080 | else |
| 3081 | *path = vim_strsave(buf); |
| 3082 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3083 | break; |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3084 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3085 | |
| 3086 | if (*e != ':') |
| 3087 | break; |
| 3088 | p = e + 1; |
| 3089 | } |
| 3090 | |
| 3091 | vim_free(buf); |
| 3092 | return retval; |
| 3093 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3094 | |
| 3095 | /* |
| 3096 | * Check what "name" is: |
| 3097 | * NODE_NORMAL: file or directory (or doesn't exist) |
| 3098 | * NODE_WRITABLE: writable device, socket, fifo, etc. |
| 3099 | * NODE_OTHER: non-writable things |
| 3100 | */ |
| 3101 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3102 | mch_nodetype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3103 | { |
| 3104 | struct stat st; |
| 3105 | |
| 3106 | if (stat((char *)name, &st)) |
| 3107 | return NODE_NORMAL; |
| 3108 | if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) |
| 3109 | return NODE_NORMAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3110 | if (S_ISBLK(st.st_mode)) /* block device isn't writable */ |
| 3111 | return NODE_OTHER; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3112 | /* Everything else is writable? */ |
| 3113 | return NODE_WRITABLE; |
| 3114 | } |
| 3115 | |
| 3116 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3117 | mch_early_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3118 | { |
| 3119 | #ifdef HAVE_CHECK_STACK_GROWTH |
| 3120 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3121 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3122 | check_stack_growth((char *)&i); |
| 3123 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3124 | # ifdef HAVE_STACK_LIMIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3125 | get_stack_limit(); |
| 3126 | # endif |
| 3127 | |
| 3128 | #endif |
| 3129 | |
| 3130 | /* |
| 3131 | * Setup an alternative stack for signals. Helps to catch signals when |
| 3132 | * running out of stack space. |
| 3133 | * Use of sigaltstack() is preferred, it's more portable. |
| 3134 | * Ignore any errors. |
| 3135 | */ |
| 3136 | #if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
Bram Moolenaar | 5a22181 | 2008-11-12 12:08:45 +0000 | [diff] [blame] | 3137 | signal_stack = (char *)alloc(SIGSTKSZ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3138 | init_signal_stack(); |
| 3139 | #endif |
| 3140 | } |
| 3141 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3142 | #if defined(EXITFREE) || defined(PROTO) |
| 3143 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3144 | mch_free_mem(void) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3145 | { |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3146 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
| 3147 | if (clip_star.owned) |
| 3148 | clip_lose_selection(&clip_star); |
| 3149 | if (clip_plus.owned) |
| 3150 | clip_lose_selection(&clip_plus); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3151 | # endif |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3152 | # if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3153 | if (xterm_Shell != (Widget)0) |
| 3154 | XtDestroyWidget(xterm_Shell); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3155 | # ifndef LESSTIF_VERSION |
| 3156 | /* Lesstif crashes here, lose some memory */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3157 | if (xterm_dpy != NULL) |
| 3158 | XtCloseDisplay(xterm_dpy); |
| 3159 | if (app_context != (XtAppContext)NULL) |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3160 | { |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3161 | XtDestroyApplicationContext(app_context); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3162 | # ifdef FEAT_X11 |
| 3163 | x11_display = NULL; /* freed by XtDestroyApplicationContext() */ |
| 3164 | # endif |
| 3165 | } |
| 3166 | # endif |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3167 | # endif |
Bram Moolenaar | 0eda7ac | 2010-06-26 05:38:18 +0200 | [diff] [blame] | 3168 | # if defined(FEAT_X11) |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3169 | if (x11_display != NULL |
| 3170 | # ifdef FEAT_XCLIPBOARD |
| 3171 | && x11_display != xterm_dpy |
| 3172 | # endif |
| 3173 | ) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3174 | XCloseDisplay(x11_display); |
| 3175 | # endif |
| 3176 | # if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
| 3177 | vim_free(signal_stack); |
| 3178 | signal_stack = NULL; |
| 3179 | # endif |
| 3180 | # ifdef FEAT_TITLE |
| 3181 | vim_free(oldtitle); |
| 3182 | vim_free(oldicon); |
| 3183 | # endif |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3184 | } |
| 3185 | #endif |
| 3186 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 3187 | static void exit_scroll(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3188 | |
| 3189 | /* |
| 3190 | * Output a newline when exiting. |
| 3191 | * Make sure the newline goes to the same stream as the text. |
| 3192 | */ |
| 3193 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3194 | exit_scroll(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3195 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3196 | if (silent_mode) |
| 3197 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3198 | if (newline_on_exit || msg_didout) |
| 3199 | { |
| 3200 | if (msg_use_printf()) |
| 3201 | { |
| 3202 | if (info_message) |
| 3203 | mch_msg("\n"); |
| 3204 | else |
| 3205 | mch_errmsg("\r\n"); |
| 3206 | } |
| 3207 | else |
| 3208 | out_char('\n'); |
| 3209 | } |
| 3210 | else |
| 3211 | { |
| 3212 | restore_cterm_colors(); /* get original colors back */ |
| 3213 | msg_clr_eos_force(); /* clear the rest of the display */ |
| 3214 | windgoto((int)Rows - 1, 0); /* may have moved the cursor */ |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3219 | mch_exit(int r) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3220 | { |
| 3221 | exiting = TRUE; |
| 3222 | |
| 3223 | #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD) |
| 3224 | x11_export_final_selection(); |
| 3225 | #endif |
| 3226 | |
| 3227 | #ifdef FEAT_GUI |
| 3228 | if (!gui.in_use) |
| 3229 | #endif |
| 3230 | { |
| 3231 | settmode(TMODE_COOK); |
| 3232 | #ifdef FEAT_TITLE |
| 3233 | mch_restore_title(3); /* restore xterm title and icon name */ |
| 3234 | #endif |
| 3235 | /* |
| 3236 | * When t_ti is not empty but it doesn't cause swapping terminal |
| 3237 | * pages, need to output a newline when msg_didout is set. But when |
| 3238 | * t_ti does swap pages it should not go to the shell page. Do this |
| 3239 | * before stoptermcap(). |
| 3240 | */ |
| 3241 | if (swapping_screen() && !newline_on_exit) |
| 3242 | exit_scroll(); |
| 3243 | |
| 3244 | /* Stop termcap: May need to check for T_CRV response, which |
| 3245 | * requires RAW mode. */ |
| 3246 | stoptermcap(); |
| 3247 | |
| 3248 | /* |
| 3249 | * A newline is only required after a message in the alternate screen. |
| 3250 | * This is set to TRUE by wait_return(). |
| 3251 | */ |
| 3252 | if (!swapping_screen() || newline_on_exit) |
| 3253 | exit_scroll(); |
| 3254 | |
| 3255 | /* Cursor may have been switched off without calling starttermcap() |
| 3256 | * when doing "vim -u vimrc" and vimrc contains ":q". */ |
| 3257 | if (full_screen) |
| 3258 | cursor_on(); |
| 3259 | } |
| 3260 | out_flush(); |
| 3261 | ml_close_all(TRUE); /* remove all memfiles */ |
| 3262 | may_core_dump(); |
| 3263 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3264 | if (gui.in_use) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3265 | gui_exit(r); |
| 3266 | #endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3267 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 3268 | #ifdef MACOS_CONVERT |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3269 | mac_conv_cleanup(); |
| 3270 | #endif |
| 3271 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3272 | #ifdef __QNX__ |
| 3273 | /* A core dump won't be created if the signal handler |
| 3274 | * doesn't return, so we can't call exit() */ |
| 3275 | if (deadly_signal != 0) |
| 3276 | return; |
| 3277 | #endif |
| 3278 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3279 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 3280 | netbeans_send_disconnect(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3281 | #endif |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3282 | |
| 3283 | #ifdef EXITFREE |
| 3284 | free_all_mem(); |
| 3285 | #endif |
| 3286 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3287 | exit(r); |
| 3288 | } |
| 3289 | |
| 3290 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3291 | may_core_dump(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3292 | { |
| 3293 | if (deadly_signal != 0) |
| 3294 | { |
| 3295 | signal(deadly_signal, SIG_DFL); |
| 3296 | kill(getpid(), deadly_signal); /* Die using the signal we caught */ |
| 3297 | } |
| 3298 | } |
| 3299 | |
| 3300 | #ifndef VMS |
| 3301 | |
| 3302 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3303 | mch_settmode(int tmode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3304 | { |
| 3305 | static int first = TRUE; |
| 3306 | |
| 3307 | /* Why is NeXT excluded here (and not in os_unixx.h)? */ |
| 3308 | #if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__) |
| 3309 | /* |
| 3310 | * for "new" tty systems |
| 3311 | */ |
| 3312 | # ifdef HAVE_TERMIOS_H |
| 3313 | static struct termios told; |
| 3314 | struct termios tnew; |
| 3315 | # else |
| 3316 | static struct termio told; |
| 3317 | struct termio tnew; |
| 3318 | # endif |
| 3319 | |
| 3320 | if (first) |
| 3321 | { |
| 3322 | first = FALSE; |
| 3323 | # if defined(HAVE_TERMIOS_H) |
| 3324 | tcgetattr(read_cmd_fd, &told); |
| 3325 | # else |
| 3326 | ioctl(read_cmd_fd, TCGETA, &told); |
| 3327 | # endif |
| 3328 | } |
| 3329 | |
| 3330 | tnew = told; |
| 3331 | if (tmode == TMODE_RAW) |
| 3332 | { |
| 3333 | /* |
| 3334 | * ~ICRNL enables typing ^V^M |
| 3335 | */ |
| 3336 | tnew.c_iflag &= ~ICRNL; |
| 3337 | tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE |
| 3338 | # if defined(IEXTEN) && !defined(__MINT__) |
| 3339 | | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */ |
| 3340 | /* but it breaks function keys on MINT */ |
| 3341 | # endif |
| 3342 | ); |
| 3343 | # ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */ |
| 3344 | tnew.c_oflag &= ~ONLCR; |
| 3345 | # endif |
| 3346 | tnew.c_cc[VMIN] = 1; /* return after 1 char */ |
| 3347 | tnew.c_cc[VTIME] = 0; /* don't wait */ |
| 3348 | } |
| 3349 | else if (tmode == TMODE_SLEEP) |
| 3350 | tnew.c_lflag &= ~(ECHO); |
| 3351 | |
| 3352 | # if defined(HAVE_TERMIOS_H) |
| 3353 | { |
| 3354 | int n = 10; |
| 3355 | |
| 3356 | /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a |
| 3357 | * few times. */ |
| 3358 | while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1 |
| 3359 | && errno == EINTR && n > 0) |
| 3360 | --n; |
| 3361 | } |
| 3362 | # else |
| 3363 | ioctl(read_cmd_fd, TCSETA, &tnew); |
| 3364 | # endif |
| 3365 | |
| 3366 | #else |
| 3367 | |
| 3368 | /* |
| 3369 | * for "old" tty systems |
| 3370 | */ |
| 3371 | # ifndef TIOCSETN |
| 3372 | # define TIOCSETN TIOCSETP /* for hpux 9.0 */ |
| 3373 | # endif |
| 3374 | static struct sgttyb ttybold; |
| 3375 | struct sgttyb ttybnew; |
| 3376 | |
| 3377 | if (first) |
| 3378 | { |
| 3379 | first = FALSE; |
| 3380 | ioctl(read_cmd_fd, TIOCGETP, &ttybold); |
| 3381 | } |
| 3382 | |
| 3383 | ttybnew = ttybold; |
| 3384 | if (tmode == TMODE_RAW) |
| 3385 | { |
| 3386 | ttybnew.sg_flags &= ~(CRMOD | ECHO); |
| 3387 | ttybnew.sg_flags |= RAW; |
| 3388 | } |
| 3389 | else if (tmode == TMODE_SLEEP) |
| 3390 | ttybnew.sg_flags &= ~(ECHO); |
| 3391 | ioctl(read_cmd_fd, TIOCSETN, &ttybnew); |
| 3392 | #endif |
| 3393 | curr_tmode = tmode; |
| 3394 | } |
| 3395 | |
| 3396 | /* |
| 3397 | * Try to get the code for "t_kb" from the stty setting |
| 3398 | * |
| 3399 | * Even if termcap claims a backspace key, the user's setting *should* |
| 3400 | * prevail. stty knows more about reality than termcap does, and if |
| 3401 | * somebody's usual erase key is DEL (which, for most BSD users, it will |
| 3402 | * be), they're going to get really annoyed if their erase key starts |
| 3403 | * doing forward deletes for no reason. (Eric Fischer) |
| 3404 | */ |
| 3405 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3406 | get_stty(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3407 | { |
| 3408 | char_u buf[2]; |
| 3409 | char_u *p; |
| 3410 | |
| 3411 | /* Why is NeXT excluded here (and not in os_unixx.h)? */ |
| 3412 | #if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__) |
| 3413 | /* for "new" tty systems */ |
| 3414 | # ifdef HAVE_TERMIOS_H |
| 3415 | struct termios keys; |
| 3416 | # else |
| 3417 | struct termio keys; |
| 3418 | # endif |
| 3419 | |
| 3420 | # if defined(HAVE_TERMIOS_H) |
| 3421 | if (tcgetattr(read_cmd_fd, &keys) != -1) |
| 3422 | # else |
| 3423 | if (ioctl(read_cmd_fd, TCGETA, &keys) != -1) |
| 3424 | # endif |
| 3425 | { |
| 3426 | buf[0] = keys.c_cc[VERASE]; |
| 3427 | intr_char = keys.c_cc[VINTR]; |
| 3428 | #else |
| 3429 | /* for "old" tty systems */ |
| 3430 | struct sgttyb keys; |
| 3431 | |
| 3432 | if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1) |
| 3433 | { |
| 3434 | buf[0] = keys.sg_erase; |
| 3435 | intr_char = keys.sg_kill; |
| 3436 | #endif |
| 3437 | buf[1] = NUL; |
| 3438 | add_termcode((char_u *)"kb", buf, FALSE); |
| 3439 | |
| 3440 | /* |
| 3441 | * If <BS> and <DEL> are now the same, redefine <DEL>. |
| 3442 | */ |
| 3443 | p = find_termcode((char_u *)"kD"); |
| 3444 | if (p != NULL && p[0] == buf[0] && p[1] == buf[1]) |
| 3445 | do_fixdel(NULL); |
| 3446 | } |
| 3447 | #if 0 |
| 3448 | } /* to keep cindent happy */ |
| 3449 | #endif |
| 3450 | } |
| 3451 | |
| 3452 | #endif /* VMS */ |
| 3453 | |
| 3454 | #if defined(FEAT_MOUSE_TTY) || defined(PROTO) |
| 3455 | /* |
| 3456 | * Set mouse clicks on or off. |
| 3457 | */ |
| 3458 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3459 | mch_setmouse(int on) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3460 | { |
| 3461 | static int ison = FALSE; |
| 3462 | int xterm_mouse_vers; |
| 3463 | |
| 3464 | if (on == ison) /* return quickly if nothing to do */ |
| 3465 | return; |
| 3466 | |
| 3467 | xterm_mouse_vers = use_xterm_mouse(); |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3468 | |
| 3469 | # ifdef FEAT_MOUSE_URXVT |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3470 | if (ttym_flags == TTYM_URXVT) |
| 3471 | { |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3472 | out_str_nf((char_u *) |
| 3473 | (on |
| 3474 | ? IF_EB("\033[?1015h", ESC_STR "[?1015h") |
| 3475 | : IF_EB("\033[?1015l", ESC_STR "[?1015l"))); |
| 3476 | ison = on; |
| 3477 | } |
| 3478 | # endif |
| 3479 | |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3480 | # ifdef FEAT_MOUSE_SGR |
| 3481 | if (ttym_flags == TTYM_SGR) |
| 3482 | { |
| 3483 | out_str_nf((char_u *) |
| 3484 | (on |
| 3485 | ? IF_EB("\033[?1006h", ESC_STR "[?1006h") |
| 3486 | : IF_EB("\033[?1006l", ESC_STR "[?1006l"))); |
| 3487 | ison = on; |
| 3488 | } |
| 3489 | # endif |
| 3490 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3491 | if (xterm_mouse_vers > 0) |
| 3492 | { |
| 3493 | if (on) /* enable mouse events, use mouse tracking if available */ |
| 3494 | out_str_nf((char_u *) |
| 3495 | (xterm_mouse_vers > 1 |
| 3496 | ? IF_EB("\033[?1002h", ESC_STR "[?1002h") |
| 3497 | : IF_EB("\033[?1000h", ESC_STR "[?1000h"))); |
| 3498 | else /* disable mouse events, could probably always send the same */ |
| 3499 | out_str_nf((char_u *) |
| 3500 | (xterm_mouse_vers > 1 |
| 3501 | ? IF_EB("\033[?1002l", ESC_STR "[?1002l") |
| 3502 | : IF_EB("\033[?1000l", ESC_STR "[?1000l"))); |
| 3503 | ison = on; |
| 3504 | } |
| 3505 | |
| 3506 | # ifdef FEAT_MOUSE_DEC |
| 3507 | else if (ttym_flags == TTYM_DEC) |
| 3508 | { |
| 3509 | if (on) /* enable mouse events */ |
| 3510 | out_str_nf((char_u *)"\033[1;2'z\033[1;3'{"); |
| 3511 | else /* disable mouse events */ |
| 3512 | out_str_nf((char_u *)"\033['z"); |
| 3513 | ison = on; |
| 3514 | } |
| 3515 | # endif |
| 3516 | |
| 3517 | # ifdef FEAT_MOUSE_GPM |
| 3518 | else |
| 3519 | { |
| 3520 | if (on) |
| 3521 | { |
| 3522 | if (gpm_open()) |
| 3523 | ison = TRUE; |
| 3524 | } |
| 3525 | else |
| 3526 | { |
| 3527 | gpm_close(); |
| 3528 | ison = FALSE; |
| 3529 | } |
| 3530 | } |
| 3531 | # endif |
| 3532 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 3533 | # ifdef FEAT_SYSMOUSE |
| 3534 | else |
| 3535 | { |
| 3536 | if (on) |
| 3537 | { |
| 3538 | if (sysmouse_open() == OK) |
| 3539 | ison = TRUE; |
| 3540 | } |
| 3541 | else |
| 3542 | { |
| 3543 | sysmouse_close(); |
| 3544 | ison = FALSE; |
| 3545 | } |
| 3546 | } |
| 3547 | # endif |
| 3548 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3549 | # ifdef FEAT_MOUSE_JSB |
| 3550 | else |
| 3551 | { |
| 3552 | if (on) |
| 3553 | { |
| 3554 | /* D - Enable Mouse up/down messages |
| 3555 | * L - Enable Left Button Reporting |
| 3556 | * M - Enable Middle Button Reporting |
| 3557 | * R - Enable Right Button Reporting |
| 3558 | * K - Enable SHIFT and CTRL key Reporting |
| 3559 | * + - Enable Advanced messaging of mouse moves and up/down messages |
| 3560 | * Q - Quiet No Ack |
| 3561 | * # - Numeric value of mouse pointer required |
| 3562 | * 0 = Multiview 2000 cursor, used as standard |
| 3563 | * 1 = Windows Arrow |
| 3564 | * 2 = Windows I Beam |
| 3565 | * 3 = Windows Hour Glass |
| 3566 | * 4 = Windows Cross Hair |
| 3567 | * 5 = Windows UP Arrow |
| 3568 | */ |
Bram Moolenaar | f8de161 | 2013-04-15 15:32:25 +0200 | [diff] [blame] | 3569 | # ifdef JSBTERM_MOUSE_NONADVANCED |
| 3570 | /* Disables full feedback of pointer movements */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3571 | out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\", |
| 3572 | ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\")); |
Bram Moolenaar | f8de161 | 2013-04-15 15:32:25 +0200 | [diff] [blame] | 3573 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3574 | out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\", |
| 3575 | ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\")); |
Bram Moolenaar | f8de161 | 2013-04-15 15:32:25 +0200 | [diff] [blame] | 3576 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3577 | ison = TRUE; |
| 3578 | } |
| 3579 | else |
| 3580 | { |
| 3581 | out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\", |
| 3582 | ESC_STR "[0~ZwQ" ESC_STR "\\")); |
| 3583 | ison = FALSE; |
| 3584 | } |
| 3585 | } |
| 3586 | # endif |
| 3587 | # ifdef FEAT_MOUSE_PTERM |
| 3588 | else |
| 3589 | { |
| 3590 | /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */ |
| 3591 | if (on) |
| 3592 | out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l"); |
| 3593 | else |
| 3594 | out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h"); |
| 3595 | ison = on; |
| 3596 | } |
| 3597 | # endif |
| 3598 | } |
| 3599 | |
| 3600 | /* |
| 3601 | * Set the mouse termcode, depending on the 'term' and 'ttymouse' options. |
| 3602 | */ |
| 3603 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3604 | check_mouse_termcode(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3605 | { |
| 3606 | # ifdef FEAT_MOUSE_XTERM |
| 3607 | if (use_xterm_mouse() |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3608 | # ifdef FEAT_MOUSE_URXVT |
| 3609 | && use_xterm_mouse() != 3 |
| 3610 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3611 | # ifdef FEAT_GUI |
| 3612 | && !gui.in_use |
| 3613 | # endif |
| 3614 | ) |
| 3615 | { |
| 3616 | set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3617 | ? IF_EB("\233M", CSI_STR "M") |
| 3618 | : IF_EB("\033[M", ESC_STR "[M"))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3619 | if (*p_mouse != NUL) |
| 3620 | { |
| 3621 | /* force mouse off and maybe on to send possibly new mouse |
| 3622 | * activation sequence to the xterm, with(out) drag tracing. */ |
| 3623 | mch_setmouse(FALSE); |
| 3624 | setmouse(); |
| 3625 | } |
| 3626 | } |
| 3627 | else |
| 3628 | del_mouse_termcode(KS_MOUSE); |
| 3629 | # endif |
| 3630 | |
| 3631 | # ifdef FEAT_MOUSE_GPM |
| 3632 | if (!use_xterm_mouse() |
| 3633 | # ifdef FEAT_GUI |
| 3634 | && !gui.in_use |
| 3635 | # endif |
| 3636 | ) |
| 3637 | set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG")); |
| 3638 | # endif |
| 3639 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 3640 | # ifdef FEAT_SYSMOUSE |
| 3641 | if (!use_xterm_mouse() |
| 3642 | # ifdef FEAT_GUI |
| 3643 | && !gui.in_use |
| 3644 | # endif |
| 3645 | ) |
| 3646 | set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS")); |
| 3647 | # endif |
| 3648 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3649 | # ifdef FEAT_MOUSE_JSB |
Bram Moolenaar | 4e067c8 | 2014-07-30 17:21:58 +0200 | [diff] [blame] | 3650 | /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3651 | if (!use_xterm_mouse() |
| 3652 | # ifdef FEAT_GUI |
| 3653 | && !gui.in_use |
| 3654 | # endif |
| 3655 | ) |
| 3656 | set_mouse_termcode(KS_JSBTERM_MOUSE, |
| 3657 | (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw")); |
| 3658 | else |
| 3659 | del_mouse_termcode(KS_JSBTERM_MOUSE); |
| 3660 | # endif |
| 3661 | |
| 3662 | # ifdef FEAT_MOUSE_NET |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3663 | /* 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] | 3664 | * define it in the GUI or when using an xterm. */ |
| 3665 | if (!use_xterm_mouse() |
| 3666 | # ifdef FEAT_GUI |
| 3667 | && !gui.in_use |
| 3668 | # endif |
| 3669 | ) |
| 3670 | set_mouse_termcode(KS_NETTERM_MOUSE, |
| 3671 | (char_u *)IF_EB("\033}", ESC_STR "}")); |
| 3672 | else |
| 3673 | del_mouse_termcode(KS_NETTERM_MOUSE); |
| 3674 | # endif |
| 3675 | |
| 3676 | # ifdef FEAT_MOUSE_DEC |
Bram Moolenaar | 4e067c8 | 2014-07-30 17:21:58 +0200 | [diff] [blame] | 3677 | /* Conflicts with xterm mouse: "\033[" and "\033[M" */ |
Bram Moolenaar | cbc17d6 | 2014-05-22 21:22:19 +0200 | [diff] [blame] | 3678 | if (!use_xterm_mouse() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3679 | # ifdef FEAT_GUI |
| 3680 | && !gui.in_use |
| 3681 | # endif |
| 3682 | ) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3683 | set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
| 3684 | ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "["))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3685 | else |
| 3686 | del_mouse_termcode(KS_DEC_MOUSE); |
| 3687 | # endif |
| 3688 | # ifdef FEAT_MOUSE_PTERM |
Bram Moolenaar | 4e067c8 | 2014-07-30 17:21:58 +0200 | [diff] [blame] | 3689 | /* same conflict as the dec mouse */ |
Bram Moolenaar | cbc17d6 | 2014-05-22 21:22:19 +0200 | [diff] [blame] | 3690 | if (!use_xterm_mouse() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3691 | # ifdef FEAT_GUI |
| 3692 | && !gui.in_use |
| 3693 | # endif |
| 3694 | ) |
| 3695 | set_mouse_termcode(KS_PTERM_MOUSE, |
| 3696 | (char_u *) IF_EB("\033[", ESC_STR "[")); |
| 3697 | else |
| 3698 | del_mouse_termcode(KS_PTERM_MOUSE); |
| 3699 | # endif |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3700 | # ifdef FEAT_MOUSE_URXVT |
Bram Moolenaar | 4e067c8 | 2014-07-30 17:21:58 +0200 | [diff] [blame] | 3701 | /* same conflict as the dec mouse */ |
Bram Moolenaar | cbc17d6 | 2014-05-22 21:22:19 +0200 | [diff] [blame] | 3702 | if (use_xterm_mouse() == 3 |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3703 | # ifdef FEAT_GUI |
| 3704 | && !gui.in_use |
| 3705 | # endif |
| 3706 | ) |
| 3707 | { |
| 3708 | set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
| 3709 | ? IF_EB("\233", CSI_STR) |
| 3710 | : IF_EB("\033[", ESC_STR "["))); |
| 3711 | |
| 3712 | if (*p_mouse != NUL) |
| 3713 | { |
| 3714 | mch_setmouse(FALSE); |
| 3715 | setmouse(); |
| 3716 | } |
| 3717 | } |
| 3718 | else |
| 3719 | del_mouse_termcode(KS_URXVT_MOUSE); |
| 3720 | # endif |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3721 | # ifdef FEAT_MOUSE_SGR |
Bram Moolenaar | 24dc230 | 2014-05-13 20:19:58 +0200 | [diff] [blame] | 3722 | /* There is no conflict with xterm mouse */ |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3723 | if (use_xterm_mouse() == 4 |
| 3724 | # ifdef FEAT_GUI |
| 3725 | && !gui.in_use |
| 3726 | # endif |
| 3727 | ) |
| 3728 | { |
| 3729 | set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
| 3730 | ? IF_EB("\233<", CSI_STR "<") |
| 3731 | : IF_EB("\033[<", ESC_STR "[<"))); |
| 3732 | |
| 3733 | if (*p_mouse != NUL) |
| 3734 | { |
| 3735 | mch_setmouse(FALSE); |
| 3736 | setmouse(); |
| 3737 | } |
| 3738 | } |
| 3739 | else |
| 3740 | del_mouse_termcode(KS_SGR_MOUSE); |
| 3741 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3742 | } |
| 3743 | #endif |
| 3744 | |
| 3745 | /* |
| 3746 | * set screen mode, always fails. |
| 3747 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3748 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3749 | mch_screenmode(char_u *arg UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3750 | { |
| 3751 | EMSG(_(e_screenmode)); |
| 3752 | return FAIL; |
| 3753 | } |
| 3754 | |
| 3755 | #ifndef VMS |
| 3756 | |
| 3757 | /* |
| 3758 | * Try to get the current window size: |
| 3759 | * 1. with an ioctl(), most accurate method |
| 3760 | * 2. from the environment variables LINES and COLUMNS |
| 3761 | * 3. from the termcap |
| 3762 | * 4. keep using the old values |
| 3763 | * Return OK when size could be determined, FAIL otherwise. |
| 3764 | */ |
| 3765 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3766 | mch_get_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3767 | { |
| 3768 | long rows = 0; |
| 3769 | long columns = 0; |
| 3770 | char_u *p; |
| 3771 | |
| 3772 | /* |
| 3773 | * For OS/2 use _scrsize(). |
| 3774 | */ |
| 3775 | # ifdef __EMX__ |
| 3776 | { |
| 3777 | int s[2]; |
| 3778 | |
| 3779 | _scrsize(s); |
| 3780 | columns = s[0]; |
| 3781 | rows = s[1]; |
| 3782 | } |
| 3783 | # endif |
| 3784 | |
| 3785 | /* |
| 3786 | * 1. try using an ioctl. It is the most accurate method. |
| 3787 | * |
| 3788 | * Try using TIOCGWINSZ first, some systems that have it also define |
| 3789 | * TIOCGSIZE but don't have a struct ttysize. |
| 3790 | */ |
| 3791 | # ifdef TIOCGWINSZ |
| 3792 | { |
| 3793 | struct winsize ws; |
| 3794 | int fd = 1; |
| 3795 | |
| 3796 | /* When stdout is not a tty, use stdin for the ioctl(). */ |
| 3797 | if (!isatty(fd) && isatty(read_cmd_fd)) |
| 3798 | fd = read_cmd_fd; |
| 3799 | if (ioctl(fd, TIOCGWINSZ, &ws) == 0) |
| 3800 | { |
| 3801 | columns = ws.ws_col; |
| 3802 | rows = ws.ws_row; |
| 3803 | } |
| 3804 | } |
| 3805 | # else /* TIOCGWINSZ */ |
| 3806 | # ifdef TIOCGSIZE |
| 3807 | { |
| 3808 | struct ttysize ts; |
| 3809 | int fd = 1; |
| 3810 | |
| 3811 | /* When stdout is not a tty, use stdin for the ioctl(). */ |
| 3812 | if (!isatty(fd) && isatty(read_cmd_fd)) |
| 3813 | fd = read_cmd_fd; |
| 3814 | if (ioctl(fd, TIOCGSIZE, &ts) == 0) |
| 3815 | { |
| 3816 | columns = ts.ts_cols; |
| 3817 | rows = ts.ts_lines; |
| 3818 | } |
| 3819 | } |
| 3820 | # endif /* TIOCGSIZE */ |
| 3821 | # endif /* TIOCGWINSZ */ |
| 3822 | |
| 3823 | /* |
| 3824 | * 2. get size from environment |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 3825 | * When being POSIX compliant ('|' flag in 'cpoptions') this overrules |
| 3826 | * the ioctl() values! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3827 | */ |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 3828 | if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3829 | { |
| 3830 | if ((p = (char_u *)getenv("LINES"))) |
| 3831 | rows = atoi((char *)p); |
| 3832 | if ((p = (char_u *)getenv("COLUMNS"))) |
| 3833 | columns = atoi((char *)p); |
| 3834 | } |
| 3835 | |
| 3836 | #ifdef HAVE_TGETENT |
| 3837 | /* |
| 3838 | * 3. try reading "co" and "li" entries from termcap |
| 3839 | */ |
| 3840 | if (columns == 0 || rows == 0) |
| 3841 | getlinecol(&columns, &rows); |
| 3842 | #endif |
| 3843 | |
| 3844 | /* |
| 3845 | * 4. If everything fails, use the old values |
| 3846 | */ |
| 3847 | if (columns <= 0 || rows <= 0) |
| 3848 | return FAIL; |
| 3849 | |
| 3850 | Rows = rows; |
| 3851 | Columns = columns; |
Bram Moolenaar | e057d40 | 2013-06-30 17:51:51 +0200 | [diff] [blame] | 3852 | limit_screen_size(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3853 | return OK; |
| 3854 | } |
| 3855 | |
| 3856 | /* |
| 3857 | * Try to set the window size to Rows and Columns. |
| 3858 | */ |
| 3859 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3860 | mch_set_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3861 | { |
| 3862 | if (*T_CWS) |
| 3863 | { |
| 3864 | /* |
| 3865 | * NOTE: if you get an error here that term_set_winsize() is |
| 3866 | * undefined, check the output of configure. It could probably not |
| 3867 | * find a ncurses, termcap or termlib library. |
| 3868 | */ |
| 3869 | term_set_winsize((int)Rows, (int)Columns); |
| 3870 | out_flush(); |
| 3871 | screen_start(); /* don't know where cursor is now */ |
| 3872 | } |
| 3873 | } |
| 3874 | |
| 3875 | #endif /* VMS */ |
| 3876 | |
| 3877 | /* |
| 3878 | * Rows and/or Columns has changed. |
| 3879 | */ |
| 3880 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3881 | mch_new_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3882 | { |
| 3883 | /* Nothing to do. */ |
| 3884 | } |
| 3885 | |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 3886 | /* |
| 3887 | * Wait for process "child" to end. |
| 3888 | * Return "child" if it exited properly, <= 0 on error. |
| 3889 | */ |
| 3890 | static pid_t |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3891 | wait4pid(pid_t child, waitstatus *status) |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 3892 | { |
| 3893 | pid_t wait_pid = 0; |
| 3894 | |
| 3895 | while (wait_pid != child) |
| 3896 | { |
Bram Moolenaar | 6be120e | 2012-04-20 15:55:16 +0200 | [diff] [blame] | 3897 | /* When compiled with Python threads are probably used, in which case |
| 3898 | * wait() sometimes hangs for no obvious reason. Use waitpid() |
| 3899 | * instead and loop (like the GUI). Also needed for other interfaces, |
| 3900 | * they might call system(). */ |
| 3901 | # ifdef __NeXT__ |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 3902 | wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0); |
Bram Moolenaar | 6be120e | 2012-04-20 15:55:16 +0200 | [diff] [blame] | 3903 | # else |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 3904 | wait_pid = waitpid(child, status, WNOHANG); |
Bram Moolenaar | 6be120e | 2012-04-20 15:55:16 +0200 | [diff] [blame] | 3905 | # endif |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 3906 | if (wait_pid == 0) |
| 3907 | { |
Bram Moolenaar | 7567646 | 2013-01-30 14:55:42 +0100 | [diff] [blame] | 3908 | /* Wait for 10 msec before trying again. */ |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 3909 | mch_delay(10L, TRUE); |
| 3910 | continue; |
| 3911 | } |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 3912 | if (wait_pid <= 0 |
| 3913 | # ifdef ECHILD |
| 3914 | && errno == ECHILD |
| 3915 | # endif |
| 3916 | ) |
| 3917 | break; |
| 3918 | } |
| 3919 | return wait_pid; |
| 3920 | } |
| 3921 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 3922 | #if defined(FEAT_JOB) || !defined(USE_SYSTEM) || defined(PROTO) |
Bram Moolenaar | 7280140 | 2016-02-09 11:37:50 +0100 | [diff] [blame] | 3923 | /* |
| 3924 | * Parse "cmd" and put the white-separated parts in "argv". |
| 3925 | * "argv" is an allocated array with "argc" entries. |
| 3926 | * Returns FAIL when out of memory. |
| 3927 | */ |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 3928 | int |
| 3929 | mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc) |
| 3930 | { |
| 3931 | int i; |
| 3932 | char_u *p; |
| 3933 | int inquote; |
| 3934 | |
| 3935 | /* |
| 3936 | * Do this loop twice: |
| 3937 | * 1: find number of arguments |
| 3938 | * 2: separate them and build argv[] |
| 3939 | */ |
| 3940 | for (i = 0; i < 2; ++i) |
| 3941 | { |
| 3942 | p = cmd; |
| 3943 | inquote = FALSE; |
| 3944 | *argc = 0; |
| 3945 | for (;;) |
| 3946 | { |
| 3947 | if (i == 1) |
| 3948 | (*argv)[*argc] = (char *)p; |
| 3949 | ++*argc; |
| 3950 | while (*p != NUL && (inquote || (*p != ' ' && *p != TAB))) |
| 3951 | { |
| 3952 | if (*p == '"') |
| 3953 | inquote = !inquote; |
| 3954 | ++p; |
| 3955 | } |
| 3956 | if (*p == NUL) |
| 3957 | break; |
| 3958 | if (i == 1) |
| 3959 | *p++ = NUL; |
| 3960 | p = skipwhite(p); |
| 3961 | } |
| 3962 | if (*argv == NULL) |
| 3963 | { |
| 3964 | if (use_shcf) |
| 3965 | { |
| 3966 | /* Account for possible multiple args in p_shcf. */ |
| 3967 | p = p_shcf; |
| 3968 | for (;;) |
| 3969 | { |
| 3970 | p = skiptowhite(p); |
| 3971 | if (*p == NUL) |
| 3972 | break; |
| 3973 | ++*argc; |
| 3974 | p = skipwhite(p); |
| 3975 | } |
| 3976 | } |
| 3977 | |
| 3978 | *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *))); |
| 3979 | if (*argv == NULL) /* out of memory */ |
| 3980 | return FAIL; |
| 3981 | } |
| 3982 | } |
| 3983 | return OK; |
| 3984 | } |
| 3985 | #endif |
| 3986 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 3987 | #if !defined(USE_SYSTEM) || defined(FEAT_JOB) |
| 3988 | static void |
| 3989 | set_child_environment(void) |
| 3990 | { |
| 3991 | # ifdef HAVE_SETENV |
| 3992 | char envbuf[50]; |
| 3993 | # else |
| 3994 | static char envbuf_Rows[20]; |
| 3995 | static char envbuf_Columns[20]; |
| 3996 | # endif |
| 3997 | |
| 3998 | /* Simulate to have a dumb terminal (for now) */ |
| 3999 | # ifdef HAVE_SETENV |
| 4000 | setenv("TERM", "dumb", 1); |
| 4001 | sprintf((char *)envbuf, "%ld", Rows); |
| 4002 | setenv("ROWS", (char *)envbuf, 1); |
| 4003 | sprintf((char *)envbuf, "%ld", Rows); |
| 4004 | setenv("LINES", (char *)envbuf, 1); |
| 4005 | sprintf((char *)envbuf, "%ld", Columns); |
| 4006 | setenv("COLUMNS", (char *)envbuf, 1); |
| 4007 | # else |
| 4008 | /* |
| 4009 | * Putenv does not copy the string, it has to remain valid. |
| 4010 | * Use a static array to avoid losing allocated memory. |
| 4011 | */ |
| 4012 | putenv("TERM=dumb"); |
| 4013 | sprintf(envbuf_Rows, "ROWS=%ld", Rows); |
| 4014 | putenv(envbuf_Rows); |
| 4015 | sprintf(envbuf_Rows, "LINES=%ld", Rows); |
| 4016 | putenv(envbuf_Rows); |
| 4017 | sprintf(envbuf_Columns, "COLUMNS=%ld", Columns); |
| 4018 | putenv(envbuf_Columns); |
| 4019 | # endif |
| 4020 | } |
| 4021 | #endif |
| 4022 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4023 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4024 | mch_call_shell( |
| 4025 | char_u *cmd, |
| 4026 | int options) /* SHELL_*, see vim.h */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4027 | { |
| 4028 | #ifdef VMS |
| 4029 | char *ifn = NULL; |
| 4030 | char *ofn = NULL; |
| 4031 | #endif |
| 4032 | int tmode = cur_tmode; |
| 4033 | #ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */ |
| 4034 | int x; |
| 4035 | # ifndef __EMX__ |
| 4036 | char_u *newcmd; /* only needed for unix */ |
| 4037 | # else |
| 4038 | /* |
| 4039 | * Set the preferred shell in the EMXSHELL environment variable (but |
| 4040 | * only if it is different from what is already in the environment). |
| 4041 | * Emx then takes care of whether to use "/c" or "-c" in an |
| 4042 | * intelligent way. Simply pass the whole thing to emx's system() call. |
| 4043 | * Emx also starts an interactive shell if system() is passed an empty |
| 4044 | * string. |
| 4045 | */ |
| 4046 | char_u *p, *old; |
| 4047 | |
| 4048 | if (((old = (char_u *)getenv("EMXSHELL")) == NULL) || STRCMP(old, p_sh)) |
| 4049 | { |
| 4050 | /* should check HAVE_SETENV, but I know we don't have it. */ |
| 4051 | p = alloc(10 + strlen(p_sh)); |
| 4052 | if (p) |
| 4053 | { |
| 4054 | sprintf((char *)p, "EMXSHELL=%s", p_sh); |
| 4055 | putenv((char *)p); /* don't free the pointer! */ |
| 4056 | } |
| 4057 | } |
| 4058 | # endif |
| 4059 | |
| 4060 | out_flush(); |
| 4061 | |
| 4062 | if (options & SHELL_COOKED) |
| 4063 | settmode(TMODE_COOK); /* set to normal mode */ |
| 4064 | |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 4065 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 4066 | save_clipboard(); |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 4067 | loose_clipboard(); |
| 4068 | # endif |
| 4069 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4070 | # ifdef __EMX__ |
| 4071 | if (cmd == NULL) |
| 4072 | x = system(""); /* this starts an interactive shell in emx */ |
| 4073 | else |
| 4074 | x = system((char *)cmd); |
| 4075 | /* system() returns -1 when error occurs in starting shell */ |
| 4076 | if (x == -1 && !emsg_silent) |
| 4077 | { |
| 4078 | MSG_PUTS(_("\nCannot execute shell ")); |
| 4079 | msg_outtrans(p_sh); |
| 4080 | msg_putchar('\n'); |
| 4081 | } |
| 4082 | # else /* not __EMX__ */ |
| 4083 | if (cmd == NULL) |
| 4084 | x = system((char *)p_sh); |
| 4085 | else |
| 4086 | { |
| 4087 | # ifdef VMS |
| 4088 | if (ofn = strchr((char *)cmd, '>')) |
| 4089 | *ofn++ = '\0'; |
| 4090 | if (ifn = strchr((char *)cmd, '<')) |
| 4091 | { |
| 4092 | char *p; |
| 4093 | |
| 4094 | *ifn++ = '\0'; |
| 4095 | p = strchr(ifn,' '); /* chop off any trailing spaces */ |
| 4096 | if (p) |
| 4097 | *p = '\0'; |
| 4098 | } |
| 4099 | if (ofn) |
| 4100 | x = vms_sys((char *)cmd, ofn, ifn); |
| 4101 | else |
| 4102 | x = system((char *)cmd); |
| 4103 | # else |
| 4104 | newcmd = lalloc(STRLEN(p_sh) |
| 4105 | + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg)) |
| 4106 | + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE); |
| 4107 | if (newcmd == NULL) |
| 4108 | x = 0; |
| 4109 | else |
| 4110 | { |
| 4111 | sprintf((char *)newcmd, "%s %s %s %s", p_sh, |
| 4112 | extra_shell_arg == NULL ? "" : (char *)extra_shell_arg, |
| 4113 | (char *)p_shcf, |
| 4114 | (char *)cmd); |
| 4115 | x = system((char *)newcmd); |
| 4116 | vim_free(newcmd); |
| 4117 | } |
| 4118 | # endif |
| 4119 | } |
| 4120 | # ifdef VMS |
| 4121 | x = vms_sys_status(x); |
| 4122 | # endif |
| 4123 | if (emsg_silent) |
| 4124 | ; |
| 4125 | else if (x == 127) |
| 4126 | MSG_PUTS(_("\nCannot execute shell sh\n")); |
| 4127 | # endif /* __EMX__ */ |
| 4128 | else if (x && !(options & SHELL_SILENT)) |
| 4129 | { |
| 4130 | MSG_PUTS(_("\nshell returned ")); |
| 4131 | msg_outnum((long)x); |
| 4132 | msg_putchar('\n'); |
| 4133 | } |
| 4134 | |
| 4135 | if (tmode == TMODE_RAW) |
| 4136 | settmode(TMODE_RAW); /* set to raw mode */ |
| 4137 | # ifdef FEAT_TITLE |
| 4138 | resettitle(); |
| 4139 | # endif |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 4140 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
| 4141 | restore_clipboard(); |
| 4142 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4143 | return x; |
| 4144 | |
| 4145 | #else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */ |
| 4146 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4147 | # define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use |
| 4148 | 127, some shells use that already */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4149 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 4150 | char_u *newcmd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4151 | pid_t pid; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4152 | pid_t wpid = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4153 | pid_t wait_pid = 0; |
| 4154 | # ifdef HAVE_UNION_WAIT |
| 4155 | union wait status; |
| 4156 | # else |
| 4157 | int status = -1; |
| 4158 | # endif |
| 4159 | int retval = -1; |
| 4160 | char **argv = NULL; |
| 4161 | int argc; |
Bram Moolenaar | ea35ef6 | 2011-08-04 22:59:28 +0200 | [diff] [blame] | 4162 | char_u *p_shcf_copy = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4163 | int i; |
| 4164 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4165 | int pty_master_fd = -1; /* for pty's */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4166 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4167 | int pty_slave_fd = -1; |
| 4168 | char *tty_name; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4169 | # endif |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4170 | int fd_toshell[2]; /* for pipes */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4171 | int fd_fromshell[2]; |
| 4172 | int pipe_error = FALSE; |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4173 | int did_settmode = FALSE; /* settmode(TMODE_RAW) called */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4174 | |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 4175 | newcmd = vim_strsave(p_sh); |
| 4176 | if (newcmd == NULL) /* out of memory */ |
| 4177 | goto error; |
| 4178 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4179 | out_flush(); |
| 4180 | if (options & SHELL_COOKED) |
| 4181 | settmode(TMODE_COOK); /* set to normal mode */ |
| 4182 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 4183 | if (mch_parse_cmd(newcmd, TRUE, &argv, &argc) == FAIL) |
| 4184 | goto error; |
Bram Moolenaar | ea35ef6 | 2011-08-04 22:59:28 +0200 | [diff] [blame] | 4185 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4186 | if (cmd != NULL) |
| 4187 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4188 | char_u *s; |
| 4189 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4190 | if (extra_shell_arg != NULL) |
| 4191 | argv[argc++] = (char *)extra_shell_arg; |
Bram Moolenaar | ea35ef6 | 2011-08-04 22:59:28 +0200 | [diff] [blame] | 4192 | |
| 4193 | /* Break 'shellcmdflag' into white separated parts. This doesn't |
| 4194 | * handle quoted strings, they are very unlikely to appear. */ |
| 4195 | p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1); |
| 4196 | if (p_shcf_copy == NULL) /* out of memory */ |
| 4197 | goto error; |
| 4198 | s = p_shcf_copy; |
| 4199 | p = p_shcf; |
| 4200 | while (*p != NUL) |
| 4201 | { |
| 4202 | argv[argc++] = (char *)s; |
| 4203 | while (*p && *p != ' ' && *p != TAB) |
| 4204 | *s++ = *p++; |
| 4205 | *s++ = NUL; |
| 4206 | p = skipwhite(p); |
| 4207 | } |
| 4208 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4209 | argv[argc++] = (char *)cmd; |
| 4210 | } |
| 4211 | argv[argc] = NULL; |
| 4212 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4213 | /* |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4214 | * For the GUI, when writing the output into the buffer and when reading |
| 4215 | * input from the buffer: Try using a pseudo-tty to get the stdin/stdout |
| 4216 | * of the executed command into the Vim window. Or use a pipe. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4217 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4218 | if ((options & (SHELL_READ|SHELL_WRITE)) |
| 4219 | # ifdef FEAT_GUI |
| 4220 | || (gui.in_use && show_shell_mess) |
| 4221 | # endif |
| 4222 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4223 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4224 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4225 | /* |
| 4226 | * Try to open a master pty. |
| 4227 | * If this works, open the slave pty. |
| 4228 | * If the slave can't be opened, close the master pty. |
| 4229 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4230 | if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4231 | { |
| 4232 | pty_master_fd = OpenPTY(&tty_name); /* open pty */ |
Bram Moolenaar | e70172e | 2011-08-04 19:36:52 +0200 | [diff] [blame] | 4233 | if (pty_master_fd >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4234 | { |
Bram Moolenaar | e70172e | 2011-08-04 19:36:52 +0200 | [diff] [blame] | 4235 | /* Leaving out O_NOCTTY may lead to waitpid() always returning |
| 4236 | * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume |
| 4237 | * adding O_NOCTTY always works when defined. */ |
| 4238 | #ifdef O_NOCTTY |
| 4239 | pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0); |
| 4240 | #else |
| 4241 | pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0); |
| 4242 | #endif |
| 4243 | if (pty_slave_fd < 0) |
| 4244 | { |
| 4245 | close(pty_master_fd); |
| 4246 | pty_master_fd = -1; |
| 4247 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4248 | } |
| 4249 | } |
| 4250 | /* |
| 4251 | * If not opening a pty or it didn't work, try using pipes. |
| 4252 | */ |
| 4253 | if (pty_master_fd < 0) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4254 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4255 | { |
| 4256 | pipe_error = (pipe(fd_toshell) < 0); |
| 4257 | if (!pipe_error) /* pipe create OK */ |
| 4258 | { |
| 4259 | pipe_error = (pipe(fd_fromshell) < 0); |
| 4260 | if (pipe_error) /* pipe create failed */ |
| 4261 | { |
| 4262 | close(fd_toshell[0]); |
| 4263 | close(fd_toshell[1]); |
| 4264 | } |
| 4265 | } |
| 4266 | if (pipe_error) |
| 4267 | { |
| 4268 | MSG_PUTS(_("\nCannot create pipes\n")); |
| 4269 | out_flush(); |
| 4270 | } |
| 4271 | } |
| 4272 | } |
| 4273 | |
| 4274 | if (!pipe_error) /* pty or pipe opened or not used */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4275 | { |
| 4276 | # ifdef __BEOS__ |
| 4277 | beos_cleanup_read_thread(); |
| 4278 | # endif |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4279 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4280 | if ((pid = fork()) == -1) /* maybe we should use vfork() */ |
| 4281 | { |
| 4282 | MSG_PUTS(_("\nCannot fork\n")); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4283 | if ((options & (SHELL_READ|SHELL_WRITE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4284 | # ifdef FEAT_GUI |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4285 | || (gui.in_use && show_shell_mess) |
| 4286 | # endif |
| 4287 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4288 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4289 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4290 | if (pty_master_fd >= 0) /* close the pseudo tty */ |
| 4291 | { |
| 4292 | close(pty_master_fd); |
| 4293 | close(pty_slave_fd); |
| 4294 | } |
| 4295 | else /* close the pipes */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4296 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4297 | { |
| 4298 | close(fd_toshell[0]); |
| 4299 | close(fd_toshell[1]); |
| 4300 | close(fd_fromshell[0]); |
| 4301 | close(fd_fromshell[1]); |
| 4302 | } |
| 4303 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4304 | } |
| 4305 | else if (pid == 0) /* child */ |
| 4306 | { |
| 4307 | reset_signals(); /* handle signals normally */ |
| 4308 | |
| 4309 | if (!show_shell_mess || (options & SHELL_EXPAND)) |
| 4310 | { |
| 4311 | int fd; |
| 4312 | |
| 4313 | /* |
| 4314 | * Don't want to show any message from the shell. Can't just |
| 4315 | * close stdout and stderr though, because some systems will |
| 4316 | * break if you try to write to them after that, so we must |
| 4317 | * use dup() to replace them with something else -- webb |
| 4318 | * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang, |
| 4319 | * waiting for input. |
| 4320 | */ |
| 4321 | fd = open("/dev/null", O_RDWR | O_EXTRA, 0); |
| 4322 | fclose(stdin); |
| 4323 | fclose(stdout); |
| 4324 | fclose(stderr); |
| 4325 | |
| 4326 | /* |
| 4327 | * If any of these open()'s and dup()'s fail, we just continue |
| 4328 | * anyway. It's not fatal, and on most systems it will make |
| 4329 | * no difference at all. On a few it will cause the execvp() |
| 4330 | * to exit with a non-zero status even when the completion |
| 4331 | * could be done, which is nothing too serious. If the open() |
| 4332 | * or dup() failed we'd just do the same thing ourselves |
| 4333 | * anyway -- webb |
| 4334 | */ |
| 4335 | if (fd >= 0) |
| 4336 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4337 | ignored = dup(fd); /* To replace stdin (fd 0) */ |
| 4338 | ignored = dup(fd); /* To replace stdout (fd 1) */ |
| 4339 | ignored = dup(fd); /* To replace stderr (fd 2) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4340 | |
| 4341 | /* Don't need this now that we've duplicated it */ |
| 4342 | close(fd); |
| 4343 | } |
| 4344 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4345 | else if ((options & (SHELL_READ|SHELL_WRITE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4346 | # ifdef FEAT_GUI |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4347 | || gui.in_use |
| 4348 | # endif |
| 4349 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4350 | { |
| 4351 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4352 | # ifdef HAVE_SETSID |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4353 | /* Create our own process group, so that the child and all its |
| 4354 | * children can be kill()ed. Don't do this when using pipes, |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 4355 | * because stdin is not a tty, we would lose /dev/tty. */ |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4356 | if (p_stmp) |
Bram Moolenaar | 0725608 | 2009-02-04 13:19:42 +0000 | [diff] [blame] | 4357 | { |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4358 | (void)setsid(); |
Bram Moolenaar | 0725608 | 2009-02-04 13:19:42 +0000 | [diff] [blame] | 4359 | # if defined(SIGHUP) |
| 4360 | /* When doing "!xterm&" and 'shell' is bash: the shell |
| 4361 | * will exit and send SIGHUP to all processes in its |
| 4362 | * group, killing the just started process. Ignore SIGHUP |
| 4363 | * to avoid that. (suggested by Simon Schubert) |
| 4364 | */ |
| 4365 | signal(SIGHUP, SIG_IGN); |
| 4366 | # endif |
| 4367 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4368 | # endif |
| 4369 | # ifdef FEAT_GUI |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4370 | if (pty_slave_fd >= 0) |
| 4371 | { |
| 4372 | /* push stream discipline modules */ |
| 4373 | if (options & SHELL_COOKED) |
| 4374 | SetupSlavePTY(pty_slave_fd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4375 | # ifdef TIOCSCTTY |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4376 | /* Try to become controlling tty (probably doesn't work, |
| 4377 | * unless run by root) */ |
| 4378 | ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4379 | # endif |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4380 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4381 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4382 | set_child_environment(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4383 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4384 | /* |
| 4385 | * stderr is only redirected when using the GUI, so that a |
| 4386 | * program like gpg can still access the terminal to get a |
| 4387 | * passphrase using stderr. |
| 4388 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4389 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4390 | if (pty_master_fd >= 0) |
| 4391 | { |
| 4392 | close(pty_master_fd); /* close master side of pty */ |
| 4393 | |
| 4394 | /* set up stdin/stdout/stderr for the child */ |
| 4395 | close(0); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4396 | ignored = dup(pty_slave_fd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4397 | close(1); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4398 | ignored = dup(pty_slave_fd); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4399 | if (gui.in_use) |
| 4400 | { |
| 4401 | close(2); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4402 | ignored = dup(pty_slave_fd); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4403 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4404 | |
| 4405 | close(pty_slave_fd); /* has been dupped, close it now */ |
| 4406 | } |
| 4407 | else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4408 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4409 | { |
| 4410 | /* set up stdin for the child */ |
| 4411 | close(fd_toshell[1]); |
| 4412 | close(0); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4413 | ignored = dup(fd_toshell[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4414 | close(fd_toshell[0]); |
| 4415 | |
| 4416 | /* set up stdout for the child */ |
| 4417 | close(fd_fromshell[0]); |
| 4418 | close(1); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4419 | ignored = dup(fd_fromshell[1]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4420 | close(fd_fromshell[1]); |
| 4421 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4422 | # ifdef FEAT_GUI |
| 4423 | if (gui.in_use) |
| 4424 | { |
| 4425 | /* set up stderr for the child */ |
| 4426 | close(2); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4427 | ignored = dup(1); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4428 | } |
| 4429 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4430 | } |
| 4431 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4432 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4433 | /* |
| 4434 | * There is no type cast for the argv, because the type may be |
| 4435 | * different on different machines. This may cause a warning |
| 4436 | * message with strict compilers, don't worry about it. |
| 4437 | * Call _exit() instead of exit() to avoid closing the connection |
| 4438 | * to the X server (esp. with GTK, which uses atexit()). |
| 4439 | */ |
| 4440 | execvp(argv[0], argv); |
| 4441 | _exit(EXEC_FAILED); /* exec failed, return failure code */ |
| 4442 | } |
| 4443 | else /* parent */ |
| 4444 | { |
| 4445 | /* |
| 4446 | * While child is running, ignore terminating signals. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4447 | * Do catch CTRL-C, so that "got_int" is set. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4448 | */ |
| 4449 | catch_signals(SIG_IGN, SIG_ERR); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4450 | catch_int_signal(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4451 | |
| 4452 | /* |
| 4453 | * For the GUI we redirect stdin, stdout and stderr to our window. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4454 | * This is also used to pipe stdin/stdout to/from the external |
| 4455 | * command. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4456 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4457 | if ((options & (SHELL_READ|SHELL_WRITE)) |
| 4458 | # ifdef FEAT_GUI |
| 4459 | || (gui.in_use && show_shell_mess) |
| 4460 | # endif |
| 4461 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4462 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4463 | # define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4464 | char_u buffer[BUFLEN + 1]; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4465 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4466 | int buffer_off = 0; /* valid bytes in buffer[] */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4467 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4468 | char_u ta_buf[BUFLEN + 1]; /* TypeAHead */ |
| 4469 | int ta_len = 0; /* valid bytes in ta_buf[] */ |
| 4470 | int len; |
| 4471 | int p_more_save; |
| 4472 | int old_State; |
| 4473 | int c; |
| 4474 | int toshell_fd; |
| 4475 | int fromshell_fd; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4476 | garray_T ga; |
| 4477 | int noread_cnt; |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4478 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 4479 | struct timeval start_tv; |
| 4480 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4481 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4482 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4483 | if (pty_master_fd >= 0) |
| 4484 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4485 | fromshell_fd = pty_master_fd; |
| 4486 | toshell_fd = dup(pty_master_fd); |
| 4487 | } |
| 4488 | else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4489 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4490 | { |
| 4491 | close(fd_toshell[0]); |
| 4492 | close(fd_fromshell[1]); |
| 4493 | toshell_fd = fd_toshell[1]; |
| 4494 | fromshell_fd = fd_fromshell[0]; |
| 4495 | } |
| 4496 | |
| 4497 | /* |
| 4498 | * Write to the child if there are typed characters. |
| 4499 | * Read from the child if there are characters available. |
| 4500 | * Repeat the reading a few times if more characters are |
| 4501 | * available. Need to check for typed keys now and then, but |
| 4502 | * not too often (delays when no chars are available). |
| 4503 | * This loop is quit if no characters can be read from the pty |
| 4504 | * (WaitForChar detected special condition), or there are no |
| 4505 | * characters available and the child has exited. |
| 4506 | * Only check if the child has exited when there is no more |
| 4507 | * output. The child may exit before all the output has |
| 4508 | * been printed. |
| 4509 | * |
| 4510 | * Currently this busy loops! |
| 4511 | * This can probably dead-lock when the write blocks! |
| 4512 | */ |
| 4513 | p_more_save = p_more; |
| 4514 | p_more = FALSE; |
| 4515 | old_State = State; |
| 4516 | State = EXTERNCMD; /* don't redraw at window resize */ |
| 4517 | |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4518 | if ((options & SHELL_WRITE) && toshell_fd >= 0) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4519 | { |
| 4520 | /* Fork a process that will write the lines to the |
| 4521 | * external program. */ |
| 4522 | if ((wpid = fork()) == -1) |
| 4523 | { |
| 4524 | MSG_PUTS(_("\nCannot fork\n")); |
| 4525 | } |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4526 | else if (wpid == 0) /* child */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4527 | { |
| 4528 | linenr_T lnum = curbuf->b_op_start.lnum; |
| 4529 | int written = 0; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4530 | char_u *lp = ml_get(lnum); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4531 | size_t l; |
| 4532 | |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 4533 | close(fromshell_fd); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4534 | for (;;) |
| 4535 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4536 | l = STRLEN(lp + written); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4537 | if (l == 0) |
| 4538 | len = 0; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4539 | else if (lp[written] == NL) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4540 | /* NL -> NUL translation */ |
| 4541 | len = write(toshell_fd, "", (size_t)1); |
| 4542 | else |
| 4543 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4544 | char_u *s = vim_strchr(lp + written, NL); |
| 4545 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4546 | len = write(toshell_fd, (char *)lp + written, |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 4547 | s == NULL ? l |
| 4548 | : (size_t)(s - (lp + written))); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4549 | } |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 4550 | if (len == (int)l) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4551 | { |
| 4552 | /* Finished a line, add a NL, unless this line |
| 4553 | * should not have one. */ |
| 4554 | if (lnum != curbuf->b_op_end.lnum |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 4555 | || (!curbuf->b_p_bin |
| 4556 | && curbuf->b_p_fixeol) |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 4557 | || (lnum != curbuf->b_no_eol_lnum |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4558 | && (lnum != |
| 4559 | curbuf->b_ml.ml_line_count |
| 4560 | || curbuf->b_p_eol))) |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4561 | ignored = write(toshell_fd, "\n", |
| 4562 | (size_t)1); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4563 | ++lnum; |
| 4564 | if (lnum > curbuf->b_op_end.lnum) |
| 4565 | { |
| 4566 | /* finished all the lines, close pipe */ |
| 4567 | close(toshell_fd); |
| 4568 | toshell_fd = -1; |
| 4569 | break; |
| 4570 | } |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4571 | lp = ml_get(lnum); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4572 | written = 0; |
| 4573 | } |
| 4574 | else if (len > 0) |
| 4575 | written += len; |
| 4576 | } |
| 4577 | _exit(0); |
| 4578 | } |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4579 | else /* parent */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4580 | { |
| 4581 | close(toshell_fd); |
| 4582 | toshell_fd = -1; |
| 4583 | } |
| 4584 | } |
| 4585 | |
| 4586 | if (options & SHELL_READ) |
| 4587 | ga_init2(&ga, 1, BUFLEN); |
| 4588 | |
| 4589 | noread_cnt = 0; |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4590 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 4591 | gettimeofday(&start_tv, NULL); |
| 4592 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4593 | for (;;) |
| 4594 | { |
| 4595 | /* |
| 4596 | * Check if keys have been typed, write them to the child |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 4597 | * if there are any. |
| 4598 | * Don't do this if we are expanding wild cards (would eat |
| 4599 | * typeahead). |
| 4600 | * Don't do this when filtering and terminal is in cooked |
| 4601 | * mode, the shell command will handle the I/O. Avoids |
| 4602 | * that a typed password is echoed for ssh or gpg command. |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4603 | * Don't get characters when the child has already |
| 4604 | * finished (wait_pid == 0). |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4605 | * Don't read characters unless we didn't get output for a |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4606 | * while (noread_cnt > 4), avoids that ":r !ls" eats |
| 4607 | * typeahead. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4608 | */ |
| 4609 | len = 0; |
| 4610 | if (!(options & SHELL_EXPAND) |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 4611 | && ((options & |
| 4612 | (SHELL_READ|SHELL_WRITE|SHELL_COOKED)) |
| 4613 | != (SHELL_READ|SHELL_WRITE|SHELL_COOKED) |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4614 | # ifdef FEAT_GUI |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 4615 | || gui.in_use |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4616 | # endif |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 4617 | ) |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4618 | && wait_pid == 0 |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4619 | && (ta_len > 0 || noread_cnt > 4)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4620 | { |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4621 | if (ta_len == 0) |
| 4622 | { |
| 4623 | /* Get extra characters when we don't have any. |
| 4624 | * Reset the counter and timer. */ |
| 4625 | noread_cnt = 0; |
| 4626 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 4627 | gettimeofday(&start_tv, NULL); |
| 4628 | # endif |
| 4629 | len = ui_inchar(ta_buf, BUFLEN, 10L, 0); |
| 4630 | } |
| 4631 | if (ta_len > 0 || len > 0) |
| 4632 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4633 | /* |
| 4634 | * For pipes: |
| 4635 | * Check for CTRL-C: send interrupt signal to child. |
| 4636 | * Check for CTRL-D: EOF, close pipe to child. |
| 4637 | */ |
| 4638 | if (len == 1 && (pty_master_fd < 0 || cmd != NULL)) |
| 4639 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4640 | # ifdef SIGINT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4641 | /* |
| 4642 | * Send SIGINT to the child's group or all |
| 4643 | * processes in our group. |
| 4644 | */ |
| 4645 | if (ta_buf[ta_len] == Ctrl_C |
| 4646 | || ta_buf[ta_len] == intr_char) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4647 | { |
| 4648 | # ifdef HAVE_SETSID |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4649 | kill(-pid, SIGINT); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4650 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4651 | kill(0, SIGINT); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4652 | # endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4653 | if (wpid > 0) |
| 4654 | kill(wpid, SIGINT); |
| 4655 | } |
| 4656 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4657 | if (pty_master_fd < 0 && toshell_fd >= 0 |
| 4658 | && ta_buf[ta_len] == Ctrl_D) |
| 4659 | { |
| 4660 | close(toshell_fd); |
| 4661 | toshell_fd = -1; |
| 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | /* replace K_BS by <BS> and K_DEL by <DEL> */ |
| 4666 | for (i = ta_len; i < ta_len + len; ++i) |
| 4667 | { |
| 4668 | if (ta_buf[i] == CSI && len - i > 2) |
| 4669 | { |
| 4670 | c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]); |
| 4671 | if (c == K_DEL || c == K_KDEL || c == K_BS) |
| 4672 | { |
| 4673 | mch_memmove(ta_buf + i + 1, ta_buf + i + 3, |
| 4674 | (size_t)(len - i - 2)); |
| 4675 | if (c == K_DEL || c == K_KDEL) |
| 4676 | ta_buf[i] = DEL; |
| 4677 | else |
| 4678 | ta_buf[i] = Ctrl_H; |
| 4679 | len -= 2; |
| 4680 | } |
| 4681 | } |
| 4682 | else if (ta_buf[i] == '\r') |
| 4683 | ta_buf[i] = '\n'; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4684 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4685 | if (has_mbyte) |
Bram Moolenaar | feba08b | 2009-06-16 13:12:07 +0000 | [diff] [blame] | 4686 | i += (*mb_ptr2len_len)(ta_buf + i, |
| 4687 | ta_len + len - i) - 1; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4688 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | /* |
| 4692 | * For pipes: echo the typed characters. |
| 4693 | * For a pty this does not seem to work. |
| 4694 | */ |
| 4695 | if (pty_master_fd < 0) |
| 4696 | { |
| 4697 | for (i = ta_len; i < ta_len + len; ++i) |
| 4698 | { |
| 4699 | if (ta_buf[i] == '\n' || ta_buf[i] == '\b') |
| 4700 | msg_putchar(ta_buf[i]); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4701 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4702 | else if (has_mbyte) |
| 4703 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4704 | int l = (*mb_ptr2len)(ta_buf + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4705 | |
| 4706 | msg_outtrans_len(ta_buf + i, l); |
| 4707 | i += l - 1; |
| 4708 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4709 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4710 | else |
| 4711 | msg_outtrans_len(ta_buf + i, 1); |
| 4712 | } |
| 4713 | windgoto(msg_row, msg_col); |
| 4714 | out_flush(); |
| 4715 | } |
| 4716 | |
| 4717 | ta_len += len; |
| 4718 | |
| 4719 | /* |
| 4720 | * Write the characters to the child, unless EOF has |
| 4721 | * been typed for pipes. Write one character at a |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 4722 | * time, to avoid losing too much typeahead. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4723 | * When writing buffer lines, drop the typed |
| 4724 | * characters (only check for CTRL-C). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4725 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4726 | if (options & SHELL_WRITE) |
| 4727 | ta_len = 0; |
| 4728 | else if (toshell_fd >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4729 | { |
| 4730 | len = write(toshell_fd, (char *)ta_buf, (size_t)1); |
| 4731 | if (len > 0) |
| 4732 | { |
| 4733 | ta_len -= len; |
| 4734 | mch_memmove(ta_buf, ta_buf + len, ta_len); |
| 4735 | } |
| 4736 | } |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4737 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4740 | if (got_int) |
| 4741 | { |
| 4742 | /* CTRL-C sends a signal to the child, we ignore it |
| 4743 | * ourselves */ |
| 4744 | # ifdef HAVE_SETSID |
| 4745 | kill(-pid, SIGINT); |
| 4746 | # else |
| 4747 | kill(0, SIGINT); |
| 4748 | # endif |
| 4749 | if (wpid > 0) |
| 4750 | kill(wpid, SIGINT); |
| 4751 | got_int = FALSE; |
| 4752 | } |
| 4753 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4754 | /* |
| 4755 | * Check if the child has any characters to be printed. |
| 4756 | * Read them and write them to our window. Repeat this as |
| 4757 | * long as there is something to do, avoid the 10ms wait |
| 4758 | * for mch_inchar(), or sending typeahead characters to |
| 4759 | * the external process. |
| 4760 | * TODO: This should handle escape sequences, compatible |
| 4761 | * to some terminal (vt52?). |
| 4762 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4763 | ++noread_cnt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4764 | while (RealWaitForChar(fromshell_fd, 10L, NULL)) |
| 4765 | { |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 4766 | len = read_eintr(fromshell_fd, buffer |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4767 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4768 | + buffer_off, (size_t)(BUFLEN - buffer_off) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4769 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4770 | , (size_t)BUFLEN |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4771 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4772 | ); |
| 4773 | if (len <= 0) /* end of file or error */ |
| 4774 | goto finished; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4775 | |
| 4776 | noread_cnt = 0; |
| 4777 | if (options & SHELL_READ) |
| 4778 | { |
| 4779 | /* Do NUL -> NL translation, append NL separated |
| 4780 | * lines to the current buffer. */ |
| 4781 | for (i = 0; i < len; ++i) |
| 4782 | { |
| 4783 | if (buffer[i] == NL) |
| 4784 | append_ga_line(&ga); |
| 4785 | else if (buffer[i] == NUL) |
| 4786 | ga_append(&ga, NL); |
| 4787 | else |
| 4788 | ga_append(&ga, buffer[i]); |
| 4789 | } |
| 4790 | } |
| 4791 | # ifdef FEAT_MBYTE |
| 4792 | else if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4793 | { |
| 4794 | int l; |
| 4795 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4796 | len += buffer_off; |
| 4797 | buffer[len] = NUL; |
| 4798 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4799 | /* Check if the last character in buffer[] is |
| 4800 | * incomplete, keep these bytes for the next |
| 4801 | * round. */ |
| 4802 | for (p = buffer; p < buffer + len; p += l) |
| 4803 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4804 | l = mb_cptr2len(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4805 | if (l == 0) |
| 4806 | l = 1; /* NUL byte? */ |
| 4807 | else if (MB_BYTE2LEN(*p) != l) |
| 4808 | break; |
| 4809 | } |
| 4810 | if (p == buffer) /* no complete character */ |
| 4811 | { |
| 4812 | /* avoid getting stuck at an illegal byte */ |
| 4813 | if (len >= 12) |
| 4814 | ++p; |
| 4815 | else |
| 4816 | { |
| 4817 | buffer_off = len; |
| 4818 | continue; |
| 4819 | } |
| 4820 | } |
| 4821 | c = *p; |
| 4822 | *p = NUL; |
| 4823 | msg_puts(buffer); |
| 4824 | if (p < buffer + len) |
| 4825 | { |
| 4826 | *p = c; |
| 4827 | buffer_off = (buffer + len) - p; |
| 4828 | mch_memmove(buffer, p, buffer_off); |
| 4829 | continue; |
| 4830 | } |
| 4831 | buffer_off = 0; |
| 4832 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4833 | # endif /* FEAT_MBYTE */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4834 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4835 | { |
| 4836 | buffer[len] = NUL; |
| 4837 | msg_puts(buffer); |
| 4838 | } |
| 4839 | |
| 4840 | windgoto(msg_row, msg_col); |
| 4841 | cursor_on(); |
| 4842 | out_flush(); |
| 4843 | if (got_int) |
| 4844 | break; |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 4845 | |
| 4846 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 4847 | { |
| 4848 | struct timeval now_tv; |
| 4849 | long msec; |
| 4850 | |
| 4851 | /* Avoid that we keep looping here without |
| 4852 | * checking for a CTRL-C for a long time. Don't |
| 4853 | * break out too often to avoid losing typeahead. */ |
| 4854 | gettimeofday(&now_tv, NULL); |
| 4855 | msec = (now_tv.tv_sec - start_tv.tv_sec) * 1000L |
| 4856 | + (now_tv.tv_usec - start_tv.tv_usec) / 1000L; |
| 4857 | if (msec > 2000) |
| 4858 | { |
| 4859 | noread_cnt = 5; |
| 4860 | break; |
| 4861 | } |
| 4862 | } |
| 4863 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4866 | /* If we already detected the child has finished break the |
| 4867 | * loop now. */ |
| 4868 | if (wait_pid == pid) |
| 4869 | break; |
| 4870 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4871 | /* |
| 4872 | * Check if the child still exists, before checking for |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 4873 | * typed characters (otherwise we would lose typeahead). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4874 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4875 | # ifdef __NeXT__ |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4876 | wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4877 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4878 | wait_pid = waitpid(pid, &status, WNOHANG); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4879 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4880 | if ((wait_pid == (pid_t)-1 && errno == ECHILD) |
| 4881 | || (wait_pid == pid && WIFEXITED(status))) |
| 4882 | { |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4883 | /* Don't break the loop yet, try reading more |
| 4884 | * characters from "fromshell_fd" first. When using |
| 4885 | * pipes there might still be something to read and |
| 4886 | * then we'll break the loop at the "break" above. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4887 | wait_pid = pid; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4888 | } |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 4889 | else |
| 4890 | wait_pid = 0; |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 4891 | |
Bram Moolenaar | 95a5135 | 2013-03-21 22:53:50 +0100 | [diff] [blame] | 4892 | # if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 4893 | /* Handle any X events, e.g. serving the clipboard. */ |
| 4894 | clip_update(); |
| 4895 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4896 | } |
| 4897 | finished: |
| 4898 | p_more = p_more_save; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4899 | if (options & SHELL_READ) |
| 4900 | { |
| 4901 | if (ga.ga_len > 0) |
| 4902 | { |
| 4903 | append_ga_line(&ga); |
| 4904 | /* remember that the NL was missing */ |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 4905 | curbuf->b_no_eol_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4906 | } |
| 4907 | else |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 4908 | curbuf->b_no_eol_lnum = 0; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4909 | ga_clear(&ga); |
| 4910 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4911 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4912 | /* |
| 4913 | * Give all typeahead that wasn't used back to ui_inchar(). |
| 4914 | */ |
| 4915 | if (ta_len) |
| 4916 | ui_inchar_undo(ta_buf, ta_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4917 | State = old_State; |
| 4918 | if (toshell_fd >= 0) |
| 4919 | close(toshell_fd); |
| 4920 | close(fromshell_fd); |
| 4921 | } |
Bram Moolenaar | 95a5135 | 2013-03-21 22:53:50 +0100 | [diff] [blame] | 4922 | # if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 4923 | else |
| 4924 | { |
| 4925 | /* |
| 4926 | * Similar to the loop above, but only handle X events, no |
| 4927 | * I/O. |
| 4928 | */ |
| 4929 | for (;;) |
| 4930 | { |
| 4931 | if (got_int) |
| 4932 | { |
| 4933 | /* CTRL-C sends a signal to the child, we ignore it |
| 4934 | * ourselves */ |
| 4935 | # ifdef HAVE_SETSID |
| 4936 | kill(-pid, SIGINT); |
| 4937 | # else |
| 4938 | kill(0, SIGINT); |
| 4939 | # endif |
| 4940 | got_int = FALSE; |
| 4941 | } |
| 4942 | # ifdef __NeXT__ |
| 4943 | wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0); |
| 4944 | # else |
| 4945 | wait_pid = waitpid(pid, &status, WNOHANG); |
| 4946 | # endif |
| 4947 | if ((wait_pid == (pid_t)-1 && errno == ECHILD) |
| 4948 | || (wait_pid == pid && WIFEXITED(status))) |
| 4949 | { |
| 4950 | wait_pid = pid; |
| 4951 | break; |
| 4952 | } |
| 4953 | |
| 4954 | /* Handle any X events, e.g. serving the clipboard. */ |
| 4955 | clip_update(); |
| 4956 | |
| 4957 | mch_delay(10L, TRUE); |
| 4958 | } |
| 4959 | } |
| 4960 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4961 | |
| 4962 | /* |
| 4963 | * Wait until our child has exited. |
| 4964 | * Ignore wait() returning pids of other children and returning |
| 4965 | * because of some signal like SIGWINCH. |
| 4966 | * Don't wait if wait_pid was already set above, indicating the |
| 4967 | * child already exited. |
| 4968 | */ |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4969 | if (wait_pid != pid) |
| 4970 | wait_pid = wait4pid(pid, &status); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4971 | |
Bram Moolenaar | 624891f | 2010-10-13 16:22:09 +0200 | [diff] [blame] | 4972 | # ifdef FEAT_GUI |
| 4973 | /* Close slave side of pty. Only do this after the child has |
| 4974 | * exited, otherwise the child may hang when it tries to write on |
| 4975 | * the pty. */ |
| 4976 | if (pty_master_fd >= 0) |
| 4977 | close(pty_slave_fd); |
| 4978 | # endif |
| 4979 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4980 | /* Make sure the child that writes to the external program is |
| 4981 | * dead. */ |
| 4982 | if (wpid > 0) |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4983 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4984 | kill(wpid, SIGKILL); |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4985 | wait4pid(wpid, NULL); |
| 4986 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4987 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4988 | /* |
| 4989 | * Set to raw mode right now, otherwise a CTRL-C after |
| 4990 | * catch_signals() will kill Vim. |
| 4991 | */ |
| 4992 | if (tmode == TMODE_RAW) |
| 4993 | settmode(TMODE_RAW); |
| 4994 | did_settmode = TRUE; |
| 4995 | set_signals(); |
| 4996 | |
| 4997 | if (WIFEXITED(status)) |
| 4998 | { |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 4999 | /* LINTED avoid "bitwise operation on signed value" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5000 | retval = WEXITSTATUS(status); |
Bram Moolenaar | 7567646 | 2013-01-30 14:55:42 +0100 | [diff] [blame] | 5001 | if (retval != 0 && !emsg_silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5002 | { |
| 5003 | if (retval == EXEC_FAILED) |
| 5004 | { |
| 5005 | MSG_PUTS(_("\nCannot execute shell ")); |
| 5006 | msg_outtrans(p_sh); |
| 5007 | msg_putchar('\n'); |
| 5008 | } |
| 5009 | else if (!(options & SHELL_SILENT)) |
| 5010 | { |
| 5011 | MSG_PUTS(_("\nshell returned ")); |
| 5012 | msg_outnum((long)retval); |
| 5013 | msg_putchar('\n'); |
| 5014 | } |
| 5015 | } |
| 5016 | } |
| 5017 | else |
| 5018 | MSG_PUTS(_("\nCommand terminated\n")); |
| 5019 | } |
| 5020 | } |
| 5021 | vim_free(argv); |
Bram Moolenaar | ea35ef6 | 2011-08-04 22:59:28 +0200 | [diff] [blame] | 5022 | vim_free(p_shcf_copy); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5023 | |
| 5024 | error: |
| 5025 | if (!did_settmode) |
| 5026 | if (tmode == TMODE_RAW) |
| 5027 | settmode(TMODE_RAW); /* set to raw mode */ |
| 5028 | # ifdef FEAT_TITLE |
| 5029 | resettitle(); |
| 5030 | # endif |
| 5031 | vim_free(newcmd); |
| 5032 | |
| 5033 | return retval; |
| 5034 | |
| 5035 | #endif /* USE_SYSTEM */ |
| 5036 | } |
| 5037 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5038 | #if defined(FEAT_JOB) || defined(PROTO) |
| 5039 | void |
Bram Moolenaar | 802d559 | 2016-03-05 22:05:27 +0100 | [diff] [blame] | 5040 | mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5041 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5042 | pid_t pid; |
Bram Moolenaar | 802d559 | 2016-03-05 22:05:27 +0100 | [diff] [blame] | 5043 | # ifdef FEAT_CHANNEL |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5044 | int fd_in[2]; /* for stdin */ |
| 5045 | int fd_out[2]; /* for stdout */ |
| 5046 | int fd_err[2]; /* for stderr */ |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 5047 | channel_T *channel = NULL; |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5048 | int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL; |
| 5049 | int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL; |
| 5050 | int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL; |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5051 | int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5052 | int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE; |
| 5053 | int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE; |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5054 | int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5055 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5056 | if (use_out_for_err && use_null_for_out) |
| 5057 | use_null_for_err = TRUE; |
| 5058 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5059 | /* default is to fail */ |
| 5060 | job->jv_status = JOB_FAILED; |
| 5061 | fd_in[0] = -1; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5062 | fd_in[1] = -1; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5063 | fd_out[0] = -1; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5064 | fd_out[1] = -1; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5065 | fd_err[0] = -1; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5066 | fd_err[1] = -1; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5067 | |
Bram Moolenaar | 12dcf02 | 2016-02-15 23:09:04 +0100 | [diff] [blame] | 5068 | /* TODO: without the channel feature connect the child to /dev/null? */ |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5069 | /* Open pipes for stdin, stdout, stderr. */ |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5070 | if (use_file_for_in) |
| 5071 | { |
| 5072 | char_u *fname = options->jo_io_name[PART_IN]; |
| 5073 | |
| 5074 | fd_in[0] = mch_open((char *)fname, O_RDONLY, 0); |
| 5075 | if (fd_in[0] < 0) |
| 5076 | { |
| 5077 | EMSG2(_(e_notopen), fname); |
| 5078 | goto failed; |
| 5079 | } |
| 5080 | } |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5081 | else if (!use_null_for_in && pipe(fd_in) < 0) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5082 | goto failed; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5083 | |
| 5084 | if (use_file_for_out) |
| 5085 | { |
| 5086 | char_u *fname = options->jo_io_name[PART_OUT]; |
| 5087 | |
| 5088 | fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 5089 | if (fd_out[1] < 0) |
| 5090 | { |
| 5091 | EMSG2(_(e_notopen), fname); |
| 5092 | goto failed; |
| 5093 | } |
| 5094 | } |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5095 | else if (!use_null_for_out && pipe(fd_out) < 0) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5096 | goto failed; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5097 | |
| 5098 | if (use_file_for_err) |
| 5099 | { |
| 5100 | char_u *fname = options->jo_io_name[PART_ERR]; |
| 5101 | |
| 5102 | fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 5103 | if (fd_err[1] < 0) |
| 5104 | { |
| 5105 | EMSG2(_(e_notopen), fname); |
| 5106 | goto failed; |
| 5107 | } |
| 5108 | } |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5109 | else if (!use_out_for_err && !use_null_for_err && pipe(fd_err) < 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5110 | goto failed; |
| 5111 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5112 | if (!use_null_for_in || !use_null_for_out || !use_null_for_err) |
| 5113 | { |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame^] | 5114 | if (options->jo_set & JO_CHANNEL) |
| 5115 | { |
| 5116 | channel = options->jo_channel; |
| 5117 | if (channel != NULL) |
| 5118 | ++channel->ch_refcount; |
| 5119 | } |
| 5120 | else |
| 5121 | channel = add_channel(); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5122 | if (channel == NULL) |
| 5123 | goto failed; |
| 5124 | } |
Bram Moolenaar | 12dcf02 | 2016-02-15 23:09:04 +0100 | [diff] [blame] | 5125 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5126 | |
| 5127 | pid = fork(); /* maybe we should use vfork() */ |
| 5128 | if (pid == -1) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5129 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5130 | /* failed to fork */ |
| 5131 | goto failed; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5132 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5133 | |
| 5134 | if (pid == 0) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5135 | { |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5136 | # ifdef FEAT_CHANNEL |
| 5137 | int null_fd = -1; |
| 5138 | # endif |
| 5139 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5140 | /* child */ |
| 5141 | reset_signals(); /* handle signals normally */ |
| 5142 | |
| 5143 | # ifdef HAVE_SETSID |
| 5144 | /* Create our own process group, so that the child and all its |
| 5145 | * children can be kill()ed. Don't do this when using pipes, |
| 5146 | * because stdin is not a tty, we would lose /dev/tty. */ |
| 5147 | (void)setsid(); |
| 5148 | # endif |
| 5149 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5150 | set_child_environment(); |
| 5151 | |
Bram Moolenaar | 12dcf02 | 2016-02-15 23:09:04 +0100 | [diff] [blame] | 5152 | /* TODO: re-enable this when pipes connect without a channel */ |
| 5153 | # ifdef FEAT_CHANNEL |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5154 | if (use_null_for_in || use_null_for_out || use_null_for_err) |
| 5155 | null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0); |
| 5156 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5157 | /* set up stdin for the child */ |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5158 | if (use_null_for_in) |
| 5159 | { |
| 5160 | close(0); |
| 5161 | ignored = dup(null_fd); |
| 5162 | } |
| 5163 | else |
| 5164 | { |
| 5165 | if (!use_file_for_in) |
| 5166 | close(fd_in[1]); |
| 5167 | close(0); |
| 5168 | ignored = dup(fd_in[0]); |
| 5169 | close(fd_in[0]); |
| 5170 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5171 | |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5172 | /* set up stderr for the child */ |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5173 | if (use_null_for_err) |
| 5174 | { |
| 5175 | close(2); |
| 5176 | ignored = dup(null_fd); |
| 5177 | } |
| 5178 | else if (use_out_for_err) |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5179 | { |
| 5180 | close(2); |
| 5181 | ignored = dup(fd_out[1]); |
| 5182 | } |
| 5183 | else |
| 5184 | { |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5185 | if (!use_file_for_err) |
| 5186 | close(fd_err[0]); |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5187 | close(2); |
| 5188 | ignored = dup(fd_err[1]); |
| 5189 | close(fd_err[1]); |
| 5190 | } |
| 5191 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5192 | /* set up stdout for the child */ |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5193 | if (use_null_for_out) |
| 5194 | { |
| 5195 | close(0); |
| 5196 | ignored = dup(null_fd); |
| 5197 | } |
| 5198 | else |
| 5199 | { |
| 5200 | if (!use_file_for_out) |
| 5201 | close(fd_out[0]); |
| 5202 | close(1); |
| 5203 | ignored = dup(fd_out[1]); |
| 5204 | close(fd_out[1]); |
| 5205 | } |
| 5206 | if (null_fd >= 0) |
| 5207 | close(null_fd); |
Bram Moolenaar | 12dcf02 | 2016-02-15 23:09:04 +0100 | [diff] [blame] | 5208 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5209 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5210 | /* See above for type of argv. */ |
| 5211 | execvp(argv[0], argv); |
| 5212 | |
| 5213 | perror("executing job failed"); |
| 5214 | _exit(EXEC_FAILED); /* exec failed, return failure code */ |
| 5215 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5216 | |
| 5217 | /* parent */ |
| 5218 | job->jv_pid = pid; |
| 5219 | job->jv_status = JOB_STARTED; |
Bram Moolenaar | 12dcf02 | 2016-02-15 23:09:04 +0100 | [diff] [blame] | 5220 | # ifdef FEAT_CHANNEL |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame^] | 5221 | job->jv_channel = channel; /* ch_refcount was set above */ |
Bram Moolenaar | 12dcf02 | 2016-02-15 23:09:04 +0100 | [diff] [blame] | 5222 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5223 | |
Bram Moolenaar | 802d559 | 2016-03-05 22:05:27 +0100 | [diff] [blame] | 5224 | # ifdef FEAT_CHANNEL |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5225 | /* child stdin, stdout and stderr */ |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5226 | if (!use_file_for_in) |
| 5227 | close(fd_in[0]); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5228 | if (!use_file_for_out) |
| 5229 | close(fd_out[1]); |
| 5230 | if (!use_out_for_err && !use_file_for_err) |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5231 | close(fd_err[1]); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5232 | if (channel != NULL) |
| 5233 | { |
| 5234 | channel_set_pipes(channel, |
| 5235 | use_file_for_in || use_null_for_in |
| 5236 | ? INVALID_FD : fd_in[1], |
| 5237 | use_file_for_out || use_null_for_out |
| 5238 | ? INVALID_FD : fd_out[0], |
| 5239 | use_out_for_err || use_file_for_err || use_null_for_err |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5240 | ? INVALID_FD : fd_err[0]); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5241 | channel_set_job(channel, job, options); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5242 | } |
Bram Moolenaar | 12dcf02 | 2016-02-15 23:09:04 +0100 | [diff] [blame] | 5243 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5244 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5245 | /* success! */ |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5246 | return; |
| 5247 | |
Bram Moolenaar | 802d559 | 2016-03-05 22:05:27 +0100 | [diff] [blame] | 5248 | failed: ; |
Bram Moolenaar | 12dcf02 | 2016-02-15 23:09:04 +0100 | [diff] [blame] | 5249 | # ifdef FEAT_CHANNEL |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame^] | 5250 | channel_unref(channel); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5251 | if (fd_in[0] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5252 | close(fd_in[0]); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5253 | if (fd_in[1] >= 0) |
| 5254 | close(fd_in[1]); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5255 | if (fd_out[0] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5256 | close(fd_out[0]); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5257 | if (fd_out[1] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5258 | close(fd_out[1]); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5259 | if (fd_err[0] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5260 | close(fd_err[0]); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5261 | if (fd_err[1] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5262 | close(fd_err[1]); |
Bram Moolenaar | 802d559 | 2016-03-05 22:05:27 +0100 | [diff] [blame] | 5263 | # endif |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | char * |
| 5267 | mch_job_status(job_T *job) |
| 5268 | { |
| 5269 | # ifdef HAVE_UNION_WAIT |
| 5270 | union wait status; |
| 5271 | # else |
| 5272 | int status = -1; |
| 5273 | # endif |
| 5274 | pid_t wait_pid = 0; |
| 5275 | |
| 5276 | # ifdef __NeXT__ |
| 5277 | wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0); |
| 5278 | # else |
| 5279 | wait_pid = waitpid(job->jv_pid, &status, WNOHANG); |
| 5280 | # endif |
| 5281 | if (wait_pid == -1) |
| 5282 | { |
| 5283 | /* process must have exited */ |
| 5284 | job->jv_status = JOB_ENDED; |
| 5285 | return "dead"; |
| 5286 | } |
| 5287 | if (wait_pid == 0) |
| 5288 | return "run"; |
| 5289 | if (WIFEXITED(status)) |
| 5290 | { |
| 5291 | /* LINTED avoid "bitwise operation on signed value" */ |
| 5292 | job->jv_exitval = WEXITSTATUS(status); |
| 5293 | job->jv_status = JOB_ENDED; |
| 5294 | return "dead"; |
| 5295 | } |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5296 | if (WIFSIGNALED(status)) |
| 5297 | { |
| 5298 | job->jv_exitval = -1; |
| 5299 | job->jv_status = JOB_ENDED; |
| 5300 | return "dead"; |
| 5301 | } |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5302 | return "run"; |
| 5303 | } |
| 5304 | |
| 5305 | int |
| 5306 | mch_stop_job(job_T *job, char_u *how) |
| 5307 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5308 | int sig = -1; |
| 5309 | pid_t job_pid; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5310 | |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 5311 | if (*how == NUL || STRCMP(how, "term") == 0) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5312 | sig = SIGTERM; |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 5313 | else if (STRCMP(how, "hup") == 0) |
| 5314 | sig = SIGHUP; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5315 | else if (STRCMP(how, "quit") == 0) |
| 5316 | sig = SIGQUIT; |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 5317 | else if (STRCMP(how, "int") == 0) |
| 5318 | sig = SIGINT; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5319 | else if (STRCMP(how, "kill") == 0) |
| 5320 | sig = SIGKILL; |
| 5321 | else if (isdigit(*how)) |
| 5322 | sig = atoi((char *)how); |
| 5323 | else |
| 5324 | return FAIL; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5325 | |
Bram Moolenaar | 7280140 | 2016-02-09 11:37:50 +0100 | [diff] [blame] | 5326 | /* TODO: have an option to only kill the process, not the group? */ |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5327 | job_pid = job->jv_pid; |
| 5328 | if (job_pid == getpgid(job_pid)) |
| 5329 | job_pid = -job_pid; |
| 5330 | |
| 5331 | kill(job_pid, sig); |
| 5332 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5333 | return OK; |
| 5334 | } |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5335 | |
| 5336 | /* |
| 5337 | * Clear the data related to "job". |
| 5338 | */ |
| 5339 | void |
| 5340 | mch_clear_job(job_T *job) |
| 5341 | { |
| 5342 | /* call waitpid because child process may become zombie */ |
| 5343 | # ifdef __NeXT__ |
Bram Moolenaar | 4ca812b | 2016-03-02 21:51:16 +0100 | [diff] [blame] | 5344 | (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0); |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5345 | # else |
Bram Moolenaar | 4ca812b | 2016-03-02 21:51:16 +0100 | [diff] [blame] | 5346 | (void)waitpid(job->jv_pid, NULL, WNOHANG); |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 5347 | # endif |
| 5348 | } |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5349 | #endif |
| 5350 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5351 | /* |
| 5352 | * Check for CTRL-C typed by reading all available characters. |
| 5353 | * In cooked mode we should get SIGINT, no need to check. |
| 5354 | */ |
| 5355 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 5356 | mch_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5357 | { |
| 5358 | if (curr_tmode == TMODE_RAW && RealWaitForChar(read_cmd_fd, 0L, NULL)) |
| 5359 | fill_input_buf(FALSE); |
| 5360 | } |
| 5361 | |
| 5362 | /* |
| 5363 | * Wait "msec" msec until a character is available from the keyboard or from |
| 5364 | * inbuf[]. msec == -1 will block forever. |
| 5365 | * When a GUI is being used, this will never get called -- webb |
| 5366 | */ |
| 5367 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 5368 | WaitForChar(long msec) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5369 | { |
| 5370 | #ifdef FEAT_MOUSE_GPM |
| 5371 | int gpm_process_wanted; |
| 5372 | #endif |
| 5373 | #ifdef FEAT_XCLIPBOARD |
| 5374 | int rest; |
| 5375 | #endif |
| 5376 | int avail; |
| 5377 | |
| 5378 | if (input_available()) /* something in inbuf[] */ |
| 5379 | return 1; |
| 5380 | |
| 5381 | #if defined(FEAT_MOUSE_DEC) |
| 5382 | /* May need to query the mouse position. */ |
| 5383 | if (WantQueryMouse) |
| 5384 | { |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 5385 | WantQueryMouse = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5386 | mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5); |
| 5387 | } |
| 5388 | #endif |
| 5389 | |
| 5390 | /* |
| 5391 | * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse |
| 5392 | * events. This is a bit complicated, because they might both be defined. |
| 5393 | */ |
| 5394 | #if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD) |
| 5395 | # ifdef FEAT_XCLIPBOARD |
| 5396 | rest = 0; |
| 5397 | if (do_xterm_trace()) |
| 5398 | rest = msec; |
| 5399 | # endif |
| 5400 | do |
| 5401 | { |
| 5402 | # ifdef FEAT_XCLIPBOARD |
| 5403 | if (rest != 0) |
| 5404 | { |
| 5405 | msec = XT_TRACE_DELAY; |
| 5406 | if (rest >= 0 && rest < XT_TRACE_DELAY) |
| 5407 | msec = rest; |
| 5408 | if (rest >= 0) |
| 5409 | rest -= msec; |
| 5410 | } |
| 5411 | # endif |
| 5412 | # ifdef FEAT_MOUSE_GPM |
| 5413 | gpm_process_wanted = 0; |
| 5414 | avail = RealWaitForChar(read_cmd_fd, msec, &gpm_process_wanted); |
| 5415 | # else |
| 5416 | avail = RealWaitForChar(read_cmd_fd, msec, NULL); |
| 5417 | # endif |
| 5418 | if (!avail) |
| 5419 | { |
| 5420 | if (input_available()) |
| 5421 | return 1; |
| 5422 | # ifdef FEAT_XCLIPBOARD |
| 5423 | if (rest == 0 || !do_xterm_trace()) |
| 5424 | # endif |
| 5425 | break; |
| 5426 | } |
| 5427 | } |
| 5428 | while (FALSE |
| 5429 | # ifdef FEAT_MOUSE_GPM |
| 5430 | || (gpm_process_wanted && mch_gpm_process() == 0) |
| 5431 | # endif |
| 5432 | # ifdef FEAT_XCLIPBOARD |
| 5433 | || (!avail && rest != 0) |
| 5434 | # endif |
| 5435 | ); |
| 5436 | |
| 5437 | #else |
| 5438 | avail = RealWaitForChar(read_cmd_fd, msec, NULL); |
| 5439 | #endif |
| 5440 | return avail; |
| 5441 | } |
| 5442 | |
Bram Moolenaar | 4ffa070 | 2013-12-11 17:12:37 +0100 | [diff] [blame] | 5443 | #ifndef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5444 | /* |
| 5445 | * Wait "msec" msec until a character is available from file descriptor "fd". |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5446 | * "msec" == 0 will check for characters once. |
| 5447 | * "msec" == -1 will block until a character is available. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5448 | * 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] | 5449 | * Or when a Linux GPM mouse event is waiting. |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 5450 | * Or when a clientserver message is on the queue. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5451 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5452 | #if defined(__BEOS__) |
| 5453 | int |
| 5454 | #else |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5455 | static int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5456 | #endif |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 5457 | RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5458 | { |
| 5459 | int ret; |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5460 | int result; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5461 | #if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5462 | static int busy = FALSE; |
| 5463 | |
| 5464 | /* May retry getting characters after an event was handled. */ |
| 5465 | # define MAY_LOOP |
| 5466 | |
| 5467 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 5468 | /* Remember at what time we started, so that we know how much longer we |
| 5469 | * should wait after being interrupted. */ |
| 5470 | # define USE_START_TV |
| 5471 | struct timeval start_tv; |
| 5472 | |
| 5473 | if (msec > 0 && ( |
| 5474 | # ifdef FEAT_XCLIPBOARD |
| 5475 | xterm_Shell != (Widget)0 |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5476 | # if defined(USE_XSMP) || defined(FEAT_MZSCHEME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5477 | || |
| 5478 | # endif |
| 5479 | # endif |
| 5480 | # ifdef USE_XSMP |
| 5481 | xsmp_icefd != -1 |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5482 | # ifdef FEAT_MZSCHEME |
| 5483 | || |
| 5484 | # endif |
| 5485 | # endif |
| 5486 | # ifdef FEAT_MZSCHEME |
| 5487 | (mzthreads_allowed() && p_mzq > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5488 | # endif |
| 5489 | )) |
| 5490 | gettimeofday(&start_tv, NULL); |
| 5491 | # endif |
| 5492 | |
| 5493 | /* Handle being called recursively. This may happen for the session |
| 5494 | * manager stuff, it may save the file, which does a breakcheck. */ |
| 5495 | if (busy) |
| 5496 | return 0; |
| 5497 | #endif |
| 5498 | |
| 5499 | #ifdef MAY_LOOP |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5500 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5501 | #endif |
| 5502 | { |
| 5503 | #ifdef MAY_LOOP |
| 5504 | int finished = TRUE; /* default is to 'loop' just once */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5505 | # ifdef FEAT_MZSCHEME |
| 5506 | int mzquantum_used = FALSE; |
| 5507 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5508 | #endif |
| 5509 | #ifndef HAVE_SELECT |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 5510 | struct pollfd fds[6 + MAX_OPEN_CHANNELS]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5511 | int nfd; |
| 5512 | # ifdef FEAT_XCLIPBOARD |
| 5513 | int xterm_idx = -1; |
| 5514 | # endif |
| 5515 | # ifdef FEAT_MOUSE_GPM |
| 5516 | int gpm_idx = -1; |
| 5517 | # endif |
| 5518 | # ifdef USE_XSMP |
| 5519 | int xsmp_idx = -1; |
| 5520 | # endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5521 | int towait = (int)msec; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5522 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5523 | # ifdef FEAT_MZSCHEME |
| 5524 | mzvim_check_threads(); |
| 5525 | if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq)) |
| 5526 | { |
| 5527 | towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */ |
| 5528 | mzquantum_used = TRUE; |
| 5529 | } |
| 5530 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5531 | fds[0].fd = fd; |
| 5532 | fds[0].events = POLLIN; |
| 5533 | nfd = 1; |
| 5534 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5535 | # ifdef FEAT_XCLIPBOARD |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 5536 | may_restore_clipboard(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5537 | if (xterm_Shell != (Widget)0) |
| 5538 | { |
| 5539 | xterm_idx = nfd; |
| 5540 | fds[nfd].fd = ConnectionNumber(xterm_dpy); |
| 5541 | fds[nfd].events = POLLIN; |
| 5542 | nfd++; |
| 5543 | } |
| 5544 | # endif |
| 5545 | # ifdef FEAT_MOUSE_GPM |
| 5546 | if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0) |
| 5547 | { |
| 5548 | gpm_idx = nfd; |
| 5549 | fds[nfd].fd = gpm_fd; |
| 5550 | fds[nfd].events = POLLIN; |
| 5551 | nfd++; |
| 5552 | } |
| 5553 | # endif |
| 5554 | # ifdef USE_XSMP |
| 5555 | if (xsmp_icefd != -1) |
| 5556 | { |
| 5557 | xsmp_idx = nfd; |
| 5558 | fds[nfd].fd = xsmp_icefd; |
| 5559 | fds[nfd].events = POLLIN; |
| 5560 | nfd++; |
| 5561 | } |
| 5562 | # endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 5563 | #ifdef FEAT_CHANNEL |
| 5564 | nfd = channel_poll_setup(nfd, &fds); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 5565 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5566 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5567 | ret = poll(fds, nfd, towait); |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5568 | |
| 5569 | result = ret > 0 && (fds[0].revents & POLLIN); |
| 5570 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5571 | # ifdef FEAT_MZSCHEME |
| 5572 | if (ret == 0 && mzquantum_used) |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 5573 | /* MzThreads scheduling is required and timeout occurred */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5574 | finished = FALSE; |
| 5575 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5576 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5577 | # ifdef FEAT_XCLIPBOARD |
| 5578 | if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN)) |
| 5579 | { |
| 5580 | xterm_update(); /* Maybe we should hand out clipboard */ |
| 5581 | if (--ret == 0 && !input_available()) |
| 5582 | /* Try again */ |
| 5583 | finished = FALSE; |
| 5584 | } |
| 5585 | # endif |
| 5586 | # ifdef FEAT_MOUSE_GPM |
| 5587 | if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN)) |
| 5588 | { |
| 5589 | *check_for_gpm = 1; |
| 5590 | } |
| 5591 | # endif |
| 5592 | # ifdef USE_XSMP |
| 5593 | if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP))) |
| 5594 | { |
| 5595 | if (fds[xsmp_idx].revents & POLLIN) |
| 5596 | { |
| 5597 | busy = TRUE; |
| 5598 | xsmp_handle_requests(); |
| 5599 | busy = FALSE; |
| 5600 | } |
| 5601 | else if (fds[xsmp_idx].revents & POLLHUP) |
| 5602 | { |
| 5603 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 5604 | verb_msg((char_u *)_("XSMP lost ICE connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5605 | xsmp_close(); |
| 5606 | } |
| 5607 | if (--ret == 0) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5608 | finished = FALSE; /* Try again */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5609 | } |
| 5610 | # endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 5611 | #ifdef FEAT_CHANNEL |
| 5612 | if (ret > 0) |
| 5613 | ret = channel_poll_check(ret, &fds); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 5614 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5615 | |
| 5616 | |
| 5617 | #else /* HAVE_SELECT */ |
| 5618 | |
| 5619 | struct timeval tv; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5620 | struct timeval *tvp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5621 | fd_set rfds, efds; |
| 5622 | int maxfd; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5623 | long towait = msec; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5624 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5625 | # ifdef FEAT_MZSCHEME |
| 5626 | mzvim_check_threads(); |
| 5627 | if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq)) |
| 5628 | { |
| 5629 | towait = p_mzq; /* don't wait longer than 'mzquantum' */ |
| 5630 | mzquantum_used = TRUE; |
| 5631 | } |
| 5632 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5633 | # ifdef __EMX__ |
| 5634 | /* don't check for incoming chars if not in raw mode, because select() |
| 5635 | * always returns TRUE then (in some version of emx.dll) */ |
| 5636 | if (curr_tmode != TMODE_RAW) |
| 5637 | return 0; |
| 5638 | # endif |
| 5639 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5640 | if (towait >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5641 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5642 | tv.tv_sec = towait / 1000; |
| 5643 | tv.tv_usec = (towait % 1000) * (1000000/1000); |
| 5644 | tvp = &tv; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5645 | } |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5646 | else |
| 5647 | tvp = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5648 | |
| 5649 | /* |
| 5650 | * Select on ready for reading and exceptional condition (end of file). |
| 5651 | */ |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5652 | select_eintr: |
| 5653 | FD_ZERO(&rfds); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5654 | FD_ZERO(&efds); |
| 5655 | FD_SET(fd, &rfds); |
| 5656 | # if !defined(__QNX__) && !defined(__CYGWIN32__) |
| 5657 | /* For QNX select() always returns 1 if this is set. Why? */ |
| 5658 | FD_SET(fd, &efds); |
| 5659 | # endif |
| 5660 | maxfd = fd; |
| 5661 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5662 | # ifdef FEAT_XCLIPBOARD |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 5663 | may_restore_clipboard(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5664 | if (xterm_Shell != (Widget)0) |
| 5665 | { |
| 5666 | FD_SET(ConnectionNumber(xterm_dpy), &rfds); |
| 5667 | if (maxfd < ConnectionNumber(xterm_dpy)) |
| 5668 | maxfd = ConnectionNumber(xterm_dpy); |
Bram Moolenaar | dd82d69 | 2012-08-15 17:26:57 +0200 | [diff] [blame] | 5669 | |
| 5670 | /* An event may have already been read but not handled. In |
| 5671 | * particulary, XFlush may cause this. */ |
| 5672 | xterm_update(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5673 | } |
| 5674 | # endif |
| 5675 | # ifdef FEAT_MOUSE_GPM |
| 5676 | if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0) |
| 5677 | { |
| 5678 | FD_SET(gpm_fd, &rfds); |
| 5679 | FD_SET(gpm_fd, &efds); |
| 5680 | if (maxfd < gpm_fd) |
| 5681 | maxfd = gpm_fd; |
| 5682 | } |
| 5683 | # endif |
| 5684 | # ifdef USE_XSMP |
| 5685 | if (xsmp_icefd != -1) |
| 5686 | { |
| 5687 | FD_SET(xsmp_icefd, &rfds); |
| 5688 | FD_SET(xsmp_icefd, &efds); |
| 5689 | if (maxfd < xsmp_icefd) |
| 5690 | maxfd = xsmp_icefd; |
| 5691 | } |
| 5692 | # endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 5693 | # ifdef FEAT_CHANNEL |
| 5694 | maxfd = channel_select_setup(maxfd, &rfds); |
Bram Moolenaar | dd82d69 | 2012-08-15 17:26:57 +0200 | [diff] [blame] | 5695 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5696 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5697 | ret = select(maxfd + 1, &rfds, NULL, &efds, tvp); |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5698 | result = ret > 0 && FD_ISSET(fd, &rfds); |
| 5699 | if (result) |
| 5700 | --ret; |
| 5701 | |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5702 | # ifdef EINTR |
| 5703 | if (ret == -1 && errno == EINTR) |
Bram Moolenaar | 2e7b1df | 2011-10-12 21:04:20 +0200 | [diff] [blame] | 5704 | { |
| 5705 | /* Check whether window has been resized, EINTR may be caused by |
| 5706 | * SIGWINCH. */ |
| 5707 | if (do_resize) |
| 5708 | handle_resize(); |
| 5709 | |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5710 | /* Interrupted by a signal, need to try again. We ignore msec |
| 5711 | * here, because we do want to check even after a timeout if |
| 5712 | * characters are available. Needed for reading output of an |
| 5713 | * external command after the process has finished. */ |
| 5714 | goto select_eintr; |
Bram Moolenaar | 2e7b1df | 2011-10-12 21:04:20 +0200 | [diff] [blame] | 5715 | } |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5716 | # endif |
Bram Moolenaar | 311d982 | 2007-02-27 15:48:28 +0000 | [diff] [blame] | 5717 | # ifdef __TANDEM |
| 5718 | if (ret == -1 && errno == ENOTSUP) |
| 5719 | { |
| 5720 | FD_ZERO(&rfds); |
| 5721 | FD_ZERO(&efds); |
| 5722 | ret = 0; |
| 5723 | } |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 5724 | # endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5725 | # ifdef FEAT_MZSCHEME |
| 5726 | if (ret == 0 && mzquantum_used) |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 5727 | /* loop if MzThreads must be scheduled and timeout occurred */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 5728 | finished = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5729 | # endif |
| 5730 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5731 | # ifdef FEAT_XCLIPBOARD |
| 5732 | if (ret > 0 && xterm_Shell != (Widget)0 |
| 5733 | && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds)) |
| 5734 | { |
| 5735 | xterm_update(); /* Maybe we should hand out clipboard */ |
| 5736 | /* continue looping when we only got the X event and the input |
| 5737 | * buffer is empty */ |
| 5738 | if (--ret == 0 && !input_available()) |
| 5739 | { |
| 5740 | /* Try again */ |
| 5741 | finished = FALSE; |
| 5742 | } |
| 5743 | } |
| 5744 | # endif |
| 5745 | # ifdef FEAT_MOUSE_GPM |
| 5746 | if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0) |
| 5747 | { |
| 5748 | if (FD_ISSET(gpm_fd, &efds)) |
| 5749 | gpm_close(); |
| 5750 | else if (FD_ISSET(gpm_fd, &rfds)) |
| 5751 | *check_for_gpm = 1; |
| 5752 | } |
| 5753 | # endif |
| 5754 | # ifdef USE_XSMP |
| 5755 | if (ret > 0 && xsmp_icefd != -1) |
| 5756 | { |
| 5757 | if (FD_ISSET(xsmp_icefd, &efds)) |
| 5758 | { |
| 5759 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 5760 | verb_msg((char_u *)_("XSMP lost ICE connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5761 | xsmp_close(); |
| 5762 | if (--ret == 0) |
| 5763 | finished = FALSE; /* keep going if event was only one */ |
| 5764 | } |
| 5765 | else if (FD_ISSET(xsmp_icefd, &rfds)) |
| 5766 | { |
| 5767 | busy = TRUE; |
| 5768 | xsmp_handle_requests(); |
| 5769 | busy = FALSE; |
| 5770 | if (--ret == 0) |
| 5771 | finished = FALSE; /* keep going if event was only one */ |
| 5772 | } |
| 5773 | } |
| 5774 | # endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 5775 | #ifdef FEAT_CHANNEL |
| 5776 | if (ret > 0) |
| 5777 | ret = channel_select_check(ret, &rfds); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 5778 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5779 | |
| 5780 | #endif /* HAVE_SELECT */ |
| 5781 | |
| 5782 | #ifdef MAY_LOOP |
| 5783 | if (finished || msec == 0) |
| 5784 | break; |
| 5785 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 5786 | # ifdef FEAT_CLIENTSERVER |
| 5787 | if (server_waiting()) |
| 5788 | break; |
| 5789 | # endif |
| 5790 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5791 | /* We're going to loop around again, find out for how long */ |
| 5792 | if (msec > 0) |
| 5793 | { |
| 5794 | # ifdef USE_START_TV |
| 5795 | struct timeval mtv; |
| 5796 | |
| 5797 | /* Compute remaining wait time. */ |
| 5798 | gettimeofday(&mtv, NULL); |
| 5799 | msec -= (mtv.tv_sec - start_tv.tv_sec) * 1000L |
| 5800 | + (mtv.tv_usec - start_tv.tv_usec) / 1000L; |
| 5801 | # else |
| 5802 | /* Guess we got interrupted halfway. */ |
| 5803 | msec = msec / 2; |
| 5804 | # endif |
| 5805 | if (msec <= 0) |
| 5806 | break; /* waited long enough */ |
| 5807 | } |
| 5808 | #endif |
| 5809 | } |
| 5810 | |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 5811 | return result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5814 | #ifndef NO_EXPANDPATH |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5815 | /* |
Bram Moolenaar | 0274363 | 2005-07-25 20:42:36 +0000 | [diff] [blame] | 5816 | * Expand a path into all matching files and/or directories. Handles "*", |
| 5817 | * "?", "[a-z]", "**", etc. |
| 5818 | * "path" has backslashes before chars that are not to be expanded. |
| 5819 | * Returns the number of matches found. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5820 | */ |
| 5821 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 5822 | mch_expandpath( |
| 5823 | garray_T *gap, |
| 5824 | char_u *path, |
| 5825 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5826 | { |
Bram Moolenaar | 0274363 | 2005-07-25 20:42:36 +0000 | [diff] [blame] | 5827 | return unix_expandpath(gap, path, 0, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5828 | } |
| 5829 | #endif |
| 5830 | |
| 5831 | /* |
| 5832 | * mch_expand_wildcards() - this code does wild-card pattern matching using |
| 5833 | * the shell |
| 5834 | * |
| 5835 | * return OK for success, FAIL for error (you may lose some memory) and put |
| 5836 | * an error message in *file. |
| 5837 | * |
| 5838 | * num_pat is number of input patterns |
| 5839 | * pat is array of pointers to input patterns |
| 5840 | * num_file is pointer to number of matched file names |
| 5841 | * file is pointer to array of pointers to matched file names |
| 5842 | */ |
| 5843 | |
| 5844 | #ifndef SEEK_SET |
| 5845 | # define SEEK_SET 0 |
| 5846 | #endif |
| 5847 | #ifndef SEEK_END |
| 5848 | # define SEEK_END 2 |
| 5849 | #endif |
| 5850 | |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 5851 | #define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|" |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 5852 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5853 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 5854 | mch_expand_wildcards( |
| 5855 | int num_pat, |
| 5856 | char_u **pat, |
| 5857 | int *num_file, |
| 5858 | char_u ***file, |
| 5859 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5860 | { |
| 5861 | int i; |
| 5862 | size_t len; |
| 5863 | char_u *p; |
| 5864 | int dir; |
| 5865 | #ifdef __EMX__ |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 5866 | /* |
| 5867 | * This is the OS/2 implementation. |
| 5868 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5869 | # define EXPL_ALLOC_INC 16 |
| 5870 | char_u **expl_files; |
| 5871 | size_t files_alloced, files_free; |
| 5872 | char_u *buf; |
| 5873 | int has_wildcard; |
| 5874 | |
| 5875 | *num_file = 0; /* default: no files found */ |
| 5876 | files_alloced = EXPL_ALLOC_INC; /* how much space is allocated */ |
| 5877 | files_free = EXPL_ALLOC_INC; /* how much space is not used */ |
| 5878 | *file = (char_u **)alloc(sizeof(char_u **) * files_alloced); |
| 5879 | if (*file == NULL) |
| 5880 | return FAIL; |
| 5881 | |
| 5882 | for (; num_pat > 0; num_pat--, pat++) |
| 5883 | { |
| 5884 | expl_files = NULL; |
| 5885 | if (vim_strchr(*pat, '$') || vim_strchr(*pat, '~')) |
| 5886 | /* expand environment var or home dir */ |
| 5887 | buf = expand_env_save(*pat); |
| 5888 | else |
| 5889 | buf = vim_strsave(*pat); |
| 5890 | expl_files = NULL; |
Bram Moolenaar | d8b0273 | 2005-01-14 21:48:43 +0000 | [diff] [blame] | 5891 | has_wildcard = mch_has_exp_wildcard(buf); /* (still) wildcards? */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5892 | if (has_wildcard) /* yes, so expand them */ |
| 5893 | expl_files = (char_u **)_fnexplode(buf); |
| 5894 | |
| 5895 | /* |
| 5896 | * return value of buf if no wildcards left, |
| 5897 | * OR if no match AND EW_NOTFOUND is set. |
| 5898 | */ |
| 5899 | if ((!has_wildcard && ((flags & EW_NOTFOUND) || mch_getperm(buf) >= 0)) |
| 5900 | || (expl_files == NULL && (flags & EW_NOTFOUND))) |
| 5901 | { /* simply save the current contents of *buf */ |
| 5902 | expl_files = (char_u **)alloc(sizeof(char_u **) * 2); |
| 5903 | if (expl_files != NULL) |
| 5904 | { |
| 5905 | expl_files[0] = vim_strsave(buf); |
| 5906 | expl_files[1] = NULL; |
| 5907 | } |
| 5908 | } |
| 5909 | vim_free(buf); |
| 5910 | |
| 5911 | /* |
| 5912 | * Count number of names resulting from expansion, |
| 5913 | * At the same time add a backslash to the end of names that happen to |
| 5914 | * be directories, and replace slashes with backslashes. |
| 5915 | */ |
| 5916 | if (expl_files) |
| 5917 | { |
| 5918 | for (i = 0; (p = expl_files[i]) != NULL; i++) |
| 5919 | { |
| 5920 | dir = mch_isdir(p); |
| 5921 | /* If we don't want dirs and this is one, skip it */ |
| 5922 | if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE))) |
| 5923 | continue; |
| 5924 | |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 5925 | /* Skip files that are not executable if we check for that. */ |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5926 | if (!dir && (flags & EW_EXEC) |
| 5927 | && !mch_can_exe(p, NULL, !(flags & EW_SHELLCMD))) |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 5928 | continue; |
| 5929 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5930 | if (--files_free == 0) |
| 5931 | { |
| 5932 | /* need more room in table of pointers */ |
| 5933 | files_alloced += EXPL_ALLOC_INC; |
| 5934 | *file = (char_u **)vim_realloc(*file, |
| 5935 | sizeof(char_u **) * files_alloced); |
| 5936 | if (*file == NULL) |
| 5937 | { |
| 5938 | EMSG(_(e_outofmem)); |
| 5939 | *num_file = 0; |
| 5940 | return FAIL; |
| 5941 | } |
| 5942 | files_free = EXPL_ALLOC_INC; |
| 5943 | } |
| 5944 | slash_adjust(p); |
| 5945 | if (dir) |
| 5946 | { |
| 5947 | /* For a directory we add a '/', unless it's already |
| 5948 | * there. */ |
| 5949 | len = STRLEN(p); |
| 5950 | if (((*file)[*num_file] = alloc(len + 2)) != NULL) |
| 5951 | { |
| 5952 | STRCPY((*file)[*num_file], p); |
Bram Moolenaar | 654b5b5 | 2006-06-22 17:47:10 +0000 | [diff] [blame] | 5953 | if (!after_pathsep((*file)[*num_file], |
| 5954 | (*file)[*num_file] + len)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5955 | { |
| 5956 | (*file)[*num_file][len] = psepc; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5957 | (*file)[*num_file][len + 1] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5958 | } |
| 5959 | } |
| 5960 | } |
| 5961 | else |
| 5962 | { |
| 5963 | (*file)[*num_file] = vim_strsave(p); |
| 5964 | } |
| 5965 | |
| 5966 | /* |
| 5967 | * Error message already given by either alloc or vim_strsave. |
| 5968 | * Should return FAIL, but returning OK works also. |
| 5969 | */ |
| 5970 | if ((*file)[*num_file] == NULL) |
| 5971 | break; |
| 5972 | (*num_file)++; |
| 5973 | } |
| 5974 | _fnexplodefree((char **)expl_files); |
| 5975 | } |
| 5976 | } |
| 5977 | return OK; |
| 5978 | |
| 5979 | #else /* __EMX__ */ |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 5980 | /* |
| 5981 | * This is the non-OS/2 implementation (really Unix). |
| 5982 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5983 | int j; |
| 5984 | char_u *tempname; |
| 5985 | char_u *command; |
| 5986 | FILE *fd; |
| 5987 | char_u *buffer; |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 5988 | #define STYLE_ECHO 0 /* use "echo", the default */ |
| 5989 | #define STYLE_GLOB 1 /* use "glob", for csh */ |
| 5990 | #define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */ |
| 5991 | #define STYLE_PRINT 3 /* use "print -N", for zsh */ |
| 5992 | #define STYLE_BT 4 /* `cmd` expansion, execute the pattern |
| 5993 | * directly */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5994 | int shell_style = STYLE_ECHO; |
| 5995 | int check_spaces; |
| 5996 | static int did_find_nul = FALSE; |
| 5997 | int ampersent = FALSE; |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 5998 | /* vimglob() function to define for Posix shell */ |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 5999 | 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] | 6000 | |
| 6001 | *num_file = 0; /* default: no files found */ |
| 6002 | *file = NULL; |
| 6003 | |
| 6004 | /* |
| 6005 | * If there are no wildcards, just copy the names to allocated memory. |
| 6006 | * Saves a lot of time, because we don't have to start a new shell. |
| 6007 | */ |
| 6008 | if (!have_wildcard(num_pat, pat)) |
| 6009 | return save_patterns(num_pat, pat, num_file, file); |
| 6010 | |
Bram Moolenaar | 0e634da | 2005-07-20 21:57:28 +0000 | [diff] [blame] | 6011 | # ifdef HAVE_SANDBOX |
| 6012 | /* Don't allow any shell command in the sandbox. */ |
| 6013 | if (sandbox != 0 && check_secure()) |
| 6014 | return FAIL; |
| 6015 | # endif |
| 6016 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6017 | /* |
| 6018 | * Don't allow the use of backticks in secure and restricted mode. |
| 6019 | */ |
Bram Moolenaar | 0e634da | 2005-07-20 21:57:28 +0000 | [diff] [blame] | 6020 | if (secure || restricted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6021 | for (i = 0; i < num_pat; ++i) |
| 6022 | if (vim_strchr(pat[i], '`') != NULL |
| 6023 | && (check_restricted() || check_secure())) |
| 6024 | return FAIL; |
| 6025 | |
| 6026 | /* |
| 6027 | * get a name for the temp file |
| 6028 | */ |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 6029 | if ((tempname = vim_tempname('o', FALSE)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6030 | { |
| 6031 | EMSG(_(e_notmp)); |
| 6032 | return FAIL; |
| 6033 | } |
| 6034 | |
| 6035 | /* |
| 6036 | * 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] | 6037 | * file. |
| 6038 | * STYLE_BT: NL separated |
| 6039 | * If expanding `cmd` execute it directly. |
| 6040 | * STYLE_GLOB: NUL separated |
| 6041 | * If we use *csh, "glob" will work better than "echo". |
| 6042 | * STYLE_PRINT: NL or NUL separated |
| 6043 | * If we use *zsh, "print -N" will work better than "glob". |
| 6044 | * STYLE_VIMGLOB: NL separated |
| 6045 | * If we use *sh*, we define "vimglob()". |
| 6046 | * STYLE_ECHO: space separated. |
| 6047 | * A shell we don't know, stay safe and use "echo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6048 | */ |
| 6049 | if (num_pat == 1 && *pat[0] == '`' |
| 6050 | && (len = STRLEN(pat[0])) > 2 |
| 6051 | && *(pat[0] + len - 1) == '`') |
| 6052 | shell_style = STYLE_BT; |
| 6053 | else if ((len = STRLEN(p_sh)) >= 3) |
| 6054 | { |
| 6055 | if (STRCMP(p_sh + len - 3, "csh") == 0) |
| 6056 | shell_style = STYLE_GLOB; |
| 6057 | else if (STRCMP(p_sh + len - 3, "zsh") == 0) |
| 6058 | shell_style = STYLE_PRINT; |
| 6059 | } |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6060 | if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh), |
| 6061 | "sh") != NULL) |
| 6062 | shell_style = STYLE_VIMGLOB; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6063 | |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6064 | /* Compute the length of the command. We need 2 extra bytes: for the |
| 6065 | * optional '&' and for the NUL. |
| 6066 | * Worst case: "unset nonomatch; print -N >" plus two is 29 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6067 | len = STRLEN(tempname) + 29; |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6068 | if (shell_style == STYLE_VIMGLOB) |
| 6069 | len += STRLEN(sh_vimglob_func); |
| 6070 | |
Bram Moolenaar | b23c338 | 2005-01-31 19:09:12 +0000 | [diff] [blame] | 6071 | for (i = 0; i < num_pat; ++i) |
| 6072 | { |
| 6073 | /* Count the length of the patterns in the same way as they are put in |
| 6074 | * "command" below. */ |
| 6075 | #ifdef USE_SYSTEM |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6076 | len += STRLEN(pat[i]) + 3; /* add space and two quotes */ |
Bram Moolenaar | b23c338 | 2005-01-31 19:09:12 +0000 | [diff] [blame] | 6077 | #else |
| 6078 | ++len; /* add space */ |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6079 | for (j = 0; pat[i][j] != NUL; ++j) |
| 6080 | { |
| 6081 | if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL) |
| 6082 | ++len; /* may add a backslash */ |
| 6083 | ++len; |
| 6084 | } |
Bram Moolenaar | b23c338 | 2005-01-31 19:09:12 +0000 | [diff] [blame] | 6085 | #endif |
| 6086 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6087 | command = alloc(len); |
| 6088 | if (command == NULL) |
| 6089 | { |
| 6090 | /* out of memory */ |
| 6091 | vim_free(tempname); |
| 6092 | return FAIL; |
| 6093 | } |
| 6094 | |
| 6095 | /* |
| 6096 | * Build the shell command: |
| 6097 | * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell |
| 6098 | * recognizes this). |
| 6099 | * - Add the shell command to print the expanded names. |
| 6100 | * - Add the temp file name. |
| 6101 | * - Add the file name patterns. |
| 6102 | */ |
| 6103 | if (shell_style == STYLE_BT) |
| 6104 | { |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6105 | /* change `command; command& ` to (command; command ) */ |
| 6106 | STRCPY(command, "("); |
| 6107 | STRCAT(command, pat[0] + 1); /* exclude first backtick */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6108 | p = command + STRLEN(command) - 1; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6109 | *p-- = ')'; /* remove last backtick */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6110 | while (p > command && vim_iswhite(*p)) |
| 6111 | --p; |
| 6112 | if (*p == '&') /* remove trailing '&' */ |
| 6113 | { |
| 6114 | ampersent = TRUE; |
| 6115 | *p = ' '; |
| 6116 | } |
| 6117 | STRCAT(command, ">"); |
| 6118 | } |
| 6119 | else |
| 6120 | { |
| 6121 | if (flags & EW_NOTFOUND) |
| 6122 | STRCPY(command, "set nonomatch; "); |
| 6123 | else |
| 6124 | STRCPY(command, "unset nonomatch; "); |
| 6125 | if (shell_style == STYLE_GLOB) |
| 6126 | STRCAT(command, "glob >"); |
| 6127 | else if (shell_style == STYLE_PRINT) |
| 6128 | STRCAT(command, "print -N >"); |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6129 | else if (shell_style == STYLE_VIMGLOB) |
| 6130 | STRCAT(command, sh_vimglob_func); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6131 | else |
| 6132 | STRCAT(command, "echo >"); |
| 6133 | } |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6134 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6135 | STRCAT(command, tempname); |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6136 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6137 | if (shell_style != STYLE_BT) |
| 6138 | for (i = 0; i < num_pat; ++i) |
| 6139 | { |
| 6140 | /* When using system() always add extra quotes, because the shell |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6141 | * is started twice. Otherwise put a backslash before special |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 6142 | * characters, except inside ``. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6143 | #ifdef USE_SYSTEM |
| 6144 | STRCAT(command, " \""); |
| 6145 | STRCAT(command, pat[i]); |
| 6146 | STRCAT(command, "\""); |
| 6147 | #else |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6148 | int intick = FALSE; |
| 6149 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6150 | p = command + STRLEN(command); |
| 6151 | *p++ = ' '; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6152 | for (j = 0; pat[i][j] != NUL; ++j) |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6153 | { |
| 6154 | if (pat[i][j] == '`') |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6155 | intick = !intick; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6156 | else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL) |
| 6157 | { |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 6158 | /* Remove a backslash, take char literally. But keep |
Bram Moolenaar | 49315f6 | 2006-02-04 00:54:59 +0000 | [diff] [blame] | 6159 | * backslash inside backticks, before a special character |
| 6160 | * and before a backtick. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 6161 | if (intick |
Bram Moolenaar | 49315f6 | 2006-02-04 00:54:59 +0000 | [diff] [blame] | 6162 | || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL |
| 6163 | || pat[i][j + 1] == '`') |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 6164 | *p++ = '\\'; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 6165 | ++j; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6166 | } |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 6167 | else if (!intick |
| 6168 | && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$') |
| 6169 | && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL) |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6170 | /* Put a backslash before a special character, but not |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 6171 | * when inside ``. And not for $var when EW_KEEPDOLLAR is |
| 6172 | * set. */ |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6173 | *p++ = '\\'; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 6174 | |
| 6175 | /* Copy one character. */ |
| 6176 | *p++ = pat[i][j]; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6177 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6178 | *p = NUL; |
| 6179 | #endif |
| 6180 | } |
| 6181 | if (flags & EW_SILENT) |
| 6182 | show_shell_mess = FALSE; |
| 6183 | if (ampersent) |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6184 | STRCAT(command, "&"); /* put the '&' after the redirection */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6185 | |
| 6186 | /* |
| 6187 | * Using zsh -G: If a pattern has no matches, it is just deleted from |
| 6188 | * the argument list, otherwise zsh gives an error message and doesn't |
| 6189 | * expand any other pattern. |
| 6190 | */ |
| 6191 | if (shell_style == STYLE_PRINT) |
| 6192 | extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */ |
| 6193 | |
| 6194 | /* |
| 6195 | * If we use -f then shell variables set in .cshrc won't get expanded. |
| 6196 | * vi can do it, so we will too, but it is only necessary if there is a "$" |
| 6197 | * in one of the patterns, otherwise we can still use the fast option. |
| 6198 | */ |
| 6199 | else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat)) |
| 6200 | extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */ |
| 6201 | |
| 6202 | /* |
| 6203 | * execute the shell command |
| 6204 | */ |
| 6205 | i = call_shell(command, SHELL_EXPAND | SHELL_SILENT); |
| 6206 | |
| 6207 | /* When running in the background, give it some time to create the temp |
| 6208 | * file, but don't wait for it to finish. */ |
| 6209 | if (ampersent) |
| 6210 | mch_delay(10L, TRUE); |
| 6211 | |
| 6212 | extra_shell_arg = NULL; /* cleanup */ |
| 6213 | show_shell_mess = TRUE; |
| 6214 | vim_free(command); |
| 6215 | |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6216 | if (i != 0) /* mch_call_shell() failed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6217 | { |
| 6218 | mch_remove(tempname); |
| 6219 | vim_free(tempname); |
| 6220 | /* |
| 6221 | * With interactive completion, the error message is not printed. |
| 6222 | * However with USE_SYSTEM, I don't know how to turn off error messages |
| 6223 | * from the shell, so screen may still get messed up -- webb. |
| 6224 | */ |
| 6225 | #ifndef USE_SYSTEM |
| 6226 | if (!(flags & EW_SILENT)) |
| 6227 | #endif |
| 6228 | { |
| 6229 | redraw_later_clear(); /* probably messed up screen */ |
| 6230 | msg_putchar('\n'); /* clear bottom line quickly */ |
| 6231 | cmdline_row = Rows - 1; /* continue on last line */ |
| 6232 | #ifdef USE_SYSTEM |
| 6233 | if (!(flags & EW_SILENT)) |
| 6234 | #endif |
| 6235 | { |
| 6236 | MSG(_(e_wildexpand)); |
| 6237 | msg_start(); /* don't overwrite this message */ |
| 6238 | } |
| 6239 | } |
| 6240 | /* If a `cmd` expansion failed, don't list `cmd` as a match, even when |
| 6241 | * EW_NOTFOUND is given */ |
| 6242 | if (shell_style == STYLE_BT) |
| 6243 | return FAIL; |
| 6244 | goto notfound; |
| 6245 | } |
| 6246 | |
| 6247 | /* |
| 6248 | * read the names from the file into memory |
| 6249 | */ |
| 6250 | fd = fopen((char *)tempname, READBIN); |
| 6251 | if (fd == NULL) |
| 6252 | { |
| 6253 | /* Something went wrong, perhaps a file name with a special char. */ |
| 6254 | if (!(flags & EW_SILENT)) |
| 6255 | { |
| 6256 | MSG(_(e_wildexpand)); |
| 6257 | msg_start(); /* don't overwrite this message */ |
| 6258 | } |
| 6259 | vim_free(tempname); |
| 6260 | goto notfound; |
| 6261 | } |
| 6262 | fseek(fd, 0L, SEEK_END); |
| 6263 | len = ftell(fd); /* get size of temp file */ |
| 6264 | fseek(fd, 0L, SEEK_SET); |
| 6265 | buffer = alloc(len + 1); |
| 6266 | if (buffer == NULL) |
| 6267 | { |
| 6268 | /* out of memory */ |
| 6269 | mch_remove(tempname); |
| 6270 | vim_free(tempname); |
| 6271 | fclose(fd); |
| 6272 | return FAIL; |
| 6273 | } |
| 6274 | i = fread((char *)buffer, 1, len, fd); |
| 6275 | fclose(fd); |
| 6276 | mch_remove(tempname); |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 6277 | if (i != (int)len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6278 | { |
| 6279 | /* unexpected read error */ |
| 6280 | EMSG2(_(e_notread), tempname); |
| 6281 | vim_free(tempname); |
| 6282 | vim_free(buffer); |
| 6283 | return FAIL; |
| 6284 | } |
| 6285 | vim_free(tempname); |
| 6286 | |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6287 | # if defined(__CYGWIN__) || defined(__CYGWIN32__) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6288 | /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */ |
| 6289 | p = buffer; |
Bram Moolenaar | fe17e76 | 2013-06-29 14:17:02 +0200 | [diff] [blame] | 6290 | for (i = 0; i < (int)len; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6291 | if (!(buffer[i] == CAR && buffer[i + 1] == NL)) |
| 6292 | *p++ = buffer[i]; |
| 6293 | len = p - buffer; |
| 6294 | # endif |
| 6295 | |
| 6296 | |
| 6297 | /* file names are separated with Space */ |
| 6298 | if (shell_style == STYLE_ECHO) |
| 6299 | { |
| 6300 | buffer[len] = '\n'; /* make sure the buffer ends in NL */ |
| 6301 | p = buffer; |
| 6302 | for (i = 0; *p != '\n'; ++i) /* count number of entries */ |
| 6303 | { |
| 6304 | while (*p != ' ' && *p != '\n') |
| 6305 | ++p; |
| 6306 | p = skipwhite(p); /* skip to next entry */ |
| 6307 | } |
| 6308 | } |
| 6309 | /* file names are separated with NL */ |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6310 | else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6311 | { |
| 6312 | buffer[len] = NUL; /* make sure the buffer ends in NUL */ |
| 6313 | p = buffer; |
| 6314 | for (i = 0; *p != NUL; ++i) /* count number of entries */ |
| 6315 | { |
| 6316 | while (*p != '\n' && *p != NUL) |
| 6317 | ++p; |
| 6318 | if (*p != NUL) |
| 6319 | ++p; |
| 6320 | p = skipwhite(p); /* skip leading white space */ |
| 6321 | } |
| 6322 | } |
| 6323 | /* file names are separated with NUL */ |
| 6324 | else |
| 6325 | { |
| 6326 | /* |
| 6327 | * Some versions of zsh use spaces instead of NULs to separate |
| 6328 | * results. Only do this when there is no NUL before the end of the |
| 6329 | * buffer, otherwise we would never be able to use file names with |
| 6330 | * embedded spaces when zsh does use NULs. |
| 6331 | * When we found a NUL once, we know zsh is OK, set did_find_nul and |
| 6332 | * don't check for spaces again. |
| 6333 | */ |
| 6334 | check_spaces = FALSE; |
| 6335 | if (shell_style == STYLE_PRINT && !did_find_nul) |
| 6336 | { |
| 6337 | /* 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] | 6338 | buffer[len] = NUL; |
Bram Moolenaar | b011af9 | 2013-12-11 13:21:51 +0100 | [diff] [blame] | 6339 | if (len && (int)STRLEN(buffer) < (int)len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6340 | did_find_nul = TRUE; |
| 6341 | else |
| 6342 | check_spaces = TRUE; |
| 6343 | } |
| 6344 | |
| 6345 | /* |
| 6346 | * Make sure the buffer ends with a NUL. For STYLE_PRINT there |
| 6347 | * already is one, for STYLE_GLOB it needs to be added. |
| 6348 | */ |
| 6349 | if (len && buffer[len - 1] == NUL) |
| 6350 | --len; |
| 6351 | else |
| 6352 | buffer[len] = NUL; |
| 6353 | i = 0; |
| 6354 | for (p = buffer; p < buffer + len; ++p) |
| 6355 | if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */ |
| 6356 | { |
| 6357 | ++i; |
| 6358 | *p = NUL; |
| 6359 | } |
| 6360 | if (len) |
| 6361 | ++i; /* count last entry */ |
| 6362 | } |
| 6363 | if (i == 0) |
| 6364 | { |
| 6365 | /* |
| 6366 | * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I". |
| 6367 | * /bin/sh will happily expand it to nothing rather than returning an |
| 6368 | * error; and hey, it's good to check anyway -- webb. |
| 6369 | */ |
| 6370 | vim_free(buffer); |
| 6371 | goto notfound; |
| 6372 | } |
| 6373 | *num_file = i; |
| 6374 | *file = (char_u **)alloc(sizeof(char_u *) * i); |
| 6375 | if (*file == NULL) |
| 6376 | { |
| 6377 | /* out of memory */ |
| 6378 | vim_free(buffer); |
| 6379 | return FAIL; |
| 6380 | } |
| 6381 | |
| 6382 | /* |
| 6383 | * Isolate the individual file names. |
| 6384 | */ |
| 6385 | p = buffer; |
| 6386 | for (i = 0; i < *num_file; ++i) |
| 6387 | { |
| 6388 | (*file)[i] = p; |
| 6389 | /* Space or NL separates */ |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6390 | if (shell_style == STYLE_ECHO || shell_style == STYLE_BT |
| 6391 | || shell_style == STYLE_VIMGLOB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6392 | { |
Bram Moolenaar | 49315f6 | 2006-02-04 00:54:59 +0000 | [diff] [blame] | 6393 | while (!(shell_style == STYLE_ECHO && *p == ' ') |
| 6394 | && *p != '\n' && *p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6395 | ++p; |
| 6396 | if (p == buffer + len) /* last entry */ |
| 6397 | *p = NUL; |
| 6398 | else |
| 6399 | { |
| 6400 | *p++ = NUL; |
| 6401 | p = skipwhite(p); /* skip to next entry */ |
| 6402 | } |
| 6403 | } |
| 6404 | else /* NUL separates */ |
| 6405 | { |
| 6406 | while (*p && p < buffer + len) /* skip entry */ |
| 6407 | ++p; |
| 6408 | ++p; /* skip NUL */ |
| 6409 | } |
| 6410 | } |
| 6411 | |
| 6412 | /* |
| 6413 | * Move the file names to allocated memory. |
| 6414 | */ |
| 6415 | for (j = 0, i = 0; i < *num_file; ++i) |
| 6416 | { |
| 6417 | /* Require the files to exist. Helps when using /bin/sh */ |
| 6418 | if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0) |
| 6419 | continue; |
| 6420 | |
| 6421 | /* check if this entry should be included */ |
| 6422 | dir = (mch_isdir((*file)[i])); |
| 6423 | if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE))) |
| 6424 | continue; |
| 6425 | |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 6426 | /* Skip files that are not executable if we check for that. */ |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 6427 | if (!dir && (flags & EW_EXEC) |
| 6428 | && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD))) |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 6429 | continue; |
| 6430 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6431 | p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir)); |
| 6432 | if (p) |
| 6433 | { |
| 6434 | STRCPY(p, (*file)[i]); |
| 6435 | if (dir) |
Bram Moolenaar | b238909 | 2008-01-03 17:56:04 +0000 | [diff] [blame] | 6436 | add_pathsep(p); /* add '/' to a directory name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6437 | (*file)[j++] = p; |
| 6438 | } |
| 6439 | } |
| 6440 | vim_free(buffer); |
| 6441 | *num_file = j; |
| 6442 | |
| 6443 | if (*num_file == 0) /* rejected all entries */ |
| 6444 | { |
| 6445 | vim_free(*file); |
| 6446 | *file = NULL; |
| 6447 | goto notfound; |
| 6448 | } |
| 6449 | |
| 6450 | return OK; |
| 6451 | |
| 6452 | notfound: |
| 6453 | if (flags & EW_NOTFOUND) |
| 6454 | return save_patterns(num_pat, pat, num_file, file); |
| 6455 | return FAIL; |
| 6456 | |
| 6457 | #endif /* __EMX__ */ |
| 6458 | } |
| 6459 | |
| 6460 | #endif /* VMS */ |
| 6461 | |
| 6462 | #ifndef __EMX__ |
| 6463 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6464 | save_patterns( |
| 6465 | int num_pat, |
| 6466 | char_u **pat, |
| 6467 | int *num_file, |
| 6468 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6469 | { |
| 6470 | int i; |
Bram Moolenaar | d8b0273 | 2005-01-14 21:48:43 +0000 | [diff] [blame] | 6471 | char_u *s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6472 | |
| 6473 | *file = (char_u **)alloc(num_pat * sizeof(char_u *)); |
| 6474 | if (*file == NULL) |
| 6475 | return FAIL; |
| 6476 | for (i = 0; i < num_pat; i++) |
Bram Moolenaar | d8b0273 | 2005-01-14 21:48:43 +0000 | [diff] [blame] | 6477 | { |
| 6478 | s = vim_strsave(pat[i]); |
| 6479 | if (s != NULL) |
| 6480 | /* Be compatible with expand_filename(): halve the number of |
| 6481 | * backslashes. */ |
| 6482 | backslash_halve(s); |
| 6483 | (*file)[i] = s; |
| 6484 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6485 | *num_file = num_pat; |
| 6486 | return OK; |
| 6487 | } |
| 6488 | #endif |
| 6489 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6490 | /* |
| 6491 | * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can |
| 6492 | * expand. |
| 6493 | */ |
| 6494 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6495 | mch_has_exp_wildcard(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6496 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 6497 | for ( ; *p; mb_ptr_adv(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6498 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6499 | if (*p == '\\' && p[1] != NUL) |
| 6500 | ++p; |
| 6501 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6502 | if (vim_strchr((char_u *) |
| 6503 | #ifdef VMS |
| 6504 | "*?%" |
| 6505 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6506 | "*?[{'" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6507 | #endif |
| 6508 | , *p) != NULL) |
| 6509 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6510 | } |
| 6511 | return FALSE; |
| 6512 | } |
| 6513 | |
| 6514 | /* |
| 6515 | * Return TRUE if the string "p" contains a wildcard. |
| 6516 | * Don't recognize '~' at the end as a wildcard. |
| 6517 | */ |
| 6518 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6519 | mch_has_wildcard(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6520 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 6521 | for ( ; *p; mb_ptr_adv(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6522 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6523 | if (*p == '\\' && p[1] != NUL) |
| 6524 | ++p; |
| 6525 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6526 | if (vim_strchr((char_u *) |
| 6527 | #ifdef VMS |
| 6528 | "*?%$" |
| 6529 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6530 | "*?[{`'$" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6531 | #endif |
| 6532 | , *p) != NULL |
| 6533 | || (*p == '~' && p[1] != NUL)) |
| 6534 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6535 | } |
| 6536 | return FALSE; |
| 6537 | } |
| 6538 | |
| 6539 | #ifndef __EMX__ |
| 6540 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6541 | have_wildcard(int num, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6542 | { |
| 6543 | int i; |
| 6544 | |
| 6545 | for (i = 0; i < num; i++) |
| 6546 | if (mch_has_wildcard(file[i])) |
| 6547 | return 1; |
| 6548 | return 0; |
| 6549 | } |
| 6550 | |
| 6551 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6552 | have_dollars(int num, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6553 | { |
| 6554 | int i; |
| 6555 | |
| 6556 | for (i = 0; i < num; i++) |
| 6557 | if (vim_strchr(file[i], '$') != NULL) |
| 6558 | return TRUE; |
| 6559 | return FALSE; |
| 6560 | } |
| 6561 | #endif /* ifndef __EMX__ */ |
| 6562 | |
Bram Moolenaar | fdcc9af | 2016-02-29 12:52:39 +0100 | [diff] [blame] | 6563 | #if !defined(HAVE_RENAME) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6564 | /* |
| 6565 | * Scaled-down version of rename(), which is missing in Xenix. |
| 6566 | * This version can only move regular files and will fail if the |
| 6567 | * destination exists. |
| 6568 | */ |
| 6569 | int |
Bram Moolenaar | fdcc9af | 2016-02-29 12:52:39 +0100 | [diff] [blame] | 6570 | mch_rename(const char *src, const char *dest) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6571 | { |
| 6572 | struct stat st; |
| 6573 | |
| 6574 | if (stat(dest, &st) >= 0) /* fail if destination exists */ |
| 6575 | return -1; |
| 6576 | if (link(src, dest) != 0) /* link file to new name */ |
| 6577 | return -1; |
| 6578 | if (mch_remove(src) == 0) /* delete link to old name */ |
| 6579 | return 0; |
| 6580 | return -1; |
| 6581 | } |
| 6582 | #endif /* !HAVE_RENAME */ |
| 6583 | |
| 6584 | #ifdef FEAT_MOUSE_GPM |
| 6585 | /* |
| 6586 | * Initializes connection with gpm (if it isn't already opened) |
| 6587 | * Return 1 if succeeded (or connection already opened), 0 if failed |
| 6588 | */ |
| 6589 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6590 | gpm_open(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6591 | { |
| 6592 | static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */ |
| 6593 | |
| 6594 | if (!gpm_flag) |
| 6595 | { |
| 6596 | gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN); |
| 6597 | gpm_connect.defaultMask = ~GPM_HARD; |
| 6598 | /* Default handling for mouse move*/ |
| 6599 | gpm_connect.minMod = 0; /* Handle any modifier keys */ |
| 6600 | gpm_connect.maxMod = 0xffff; |
| 6601 | if (Gpm_Open(&gpm_connect, 0) > 0) |
| 6602 | { |
| 6603 | /* gpm library tries to handling TSTP causes |
| 6604 | * problems. Anyways, we close connection to Gpm whenever |
| 6605 | * we are going to suspend or starting an external process |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 6606 | * so we shouldn't have problem with this |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6607 | */ |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 6608 | # ifdef SIGTSTP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6609 | signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL); |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 6610 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6611 | return 1; /* succeed */ |
| 6612 | } |
| 6613 | if (gpm_fd == -2) |
| 6614 | Gpm_Close(); /* We don't want to talk to xterm via gpm */ |
| 6615 | return 0; |
| 6616 | } |
| 6617 | return 1; /* already open */ |
| 6618 | } |
| 6619 | |
| 6620 | /* |
| 6621 | * Closes connection to gpm |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6622 | */ |
| 6623 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6624 | gpm_close(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6625 | { |
| 6626 | if (gpm_flag && gpm_fd >= 0) /* if Open */ |
| 6627 | Gpm_Close(); |
| 6628 | } |
| 6629 | |
| 6630 | /* Reads gpm event and adds special keys to input buf. Returns length of |
| 6631 | * generated key sequence. |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 6632 | * This function is styled after gui_send_mouse_event(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6633 | */ |
| 6634 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6635 | mch_gpm_process(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6636 | { |
| 6637 | int button; |
| 6638 | static Gpm_Event gpm_event; |
| 6639 | char_u string[6]; |
| 6640 | int_u vim_modifiers; |
| 6641 | int row,col; |
| 6642 | unsigned char buttons_mask; |
| 6643 | unsigned char gpm_modifiers; |
| 6644 | static unsigned char old_buttons = 0; |
| 6645 | |
| 6646 | Gpm_GetEvent(&gpm_event); |
| 6647 | |
| 6648 | #ifdef FEAT_GUI |
| 6649 | /* Don't put events in the input queue now. */ |
| 6650 | if (hold_gui_events) |
| 6651 | return 0; |
| 6652 | #endif |
| 6653 | |
| 6654 | row = gpm_event.y - 1; |
| 6655 | col = gpm_event.x - 1; |
| 6656 | |
| 6657 | string[0] = ESC; /* Our termcode */ |
| 6658 | string[1] = 'M'; |
| 6659 | string[2] = 'G'; |
| 6660 | switch (GPM_BARE_EVENTS(gpm_event.type)) |
| 6661 | { |
| 6662 | case GPM_DRAG: |
| 6663 | string[3] = MOUSE_DRAG; |
| 6664 | break; |
| 6665 | case GPM_DOWN: |
| 6666 | buttons_mask = gpm_event.buttons & ~old_buttons; |
| 6667 | old_buttons = gpm_event.buttons; |
| 6668 | switch (buttons_mask) |
| 6669 | { |
| 6670 | case GPM_B_LEFT: |
| 6671 | button = MOUSE_LEFT; |
| 6672 | break; |
| 6673 | case GPM_B_MIDDLE: |
| 6674 | button = MOUSE_MIDDLE; |
| 6675 | break; |
| 6676 | case GPM_B_RIGHT: |
| 6677 | button = MOUSE_RIGHT; |
| 6678 | break; |
| 6679 | default: |
| 6680 | return 0; |
| 6681 | /*Don't know what to do. Can more than one button be |
| 6682 | * reported in one event? */ |
| 6683 | } |
| 6684 | string[3] = (char_u)(button | 0x20); |
| 6685 | SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1); |
| 6686 | break; |
| 6687 | case GPM_UP: |
| 6688 | string[3] = MOUSE_RELEASE; |
| 6689 | old_buttons &= ~gpm_event.buttons; |
| 6690 | break; |
| 6691 | default: |
| 6692 | return 0; |
| 6693 | } |
| 6694 | /*This code is based on gui_x11_mouse_cb in gui_x11.c */ |
| 6695 | gpm_modifiers = gpm_event.modifiers; |
| 6696 | vim_modifiers = 0x0; |
| 6697 | /* I ignore capslock stats. Aren't we all just hate capslock mixing with |
| 6698 | * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and |
| 6699 | * K_CAPSSHIFT is defined 8, so it probably isn't even reported |
| 6700 | */ |
| 6701 | if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL))) |
| 6702 | vim_modifiers |= MOUSE_SHIFT; |
| 6703 | |
| 6704 | if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL))) |
| 6705 | vim_modifiers |= MOUSE_CTRL; |
| 6706 | if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR))) |
| 6707 | vim_modifiers |= MOUSE_ALT; |
| 6708 | string[3] |= vim_modifiers; |
| 6709 | string[4] = (char_u)(col + ' ' + 1); |
| 6710 | string[5] = (char_u)(row + ' ' + 1); |
| 6711 | add_to_input_buf(string, 6); |
| 6712 | return 6; |
| 6713 | } |
| 6714 | #endif /* FEAT_MOUSE_GPM */ |
| 6715 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6716 | #ifdef FEAT_SYSMOUSE |
| 6717 | /* |
| 6718 | * Initialize connection with sysmouse. |
| 6719 | * Let virtual console inform us with SIGUSR2 for pending sysmouse |
| 6720 | * output, any sysmouse output than will be processed via sig_sysmouse(). |
| 6721 | * Return OK if succeeded, FAIL if failed. |
| 6722 | */ |
| 6723 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6724 | sysmouse_open(void) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6725 | { |
| 6726 | struct mouse_info mouse; |
| 6727 | |
| 6728 | mouse.operation = MOUSE_MODE; |
| 6729 | mouse.u.mode.mode = 0; |
| 6730 | mouse.u.mode.signal = SIGUSR2; |
| 6731 | if (ioctl(1, CONS_MOUSECTL, &mouse) != -1) |
| 6732 | { |
| 6733 | signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse); |
| 6734 | mouse.operation = MOUSE_SHOW; |
| 6735 | ioctl(1, CONS_MOUSECTL, &mouse); |
| 6736 | return OK; |
| 6737 | } |
| 6738 | return FAIL; |
| 6739 | } |
| 6740 | |
| 6741 | /* |
| 6742 | * Stop processing SIGUSR2 signals, and also make sure that |
| 6743 | * virtual console do not send us any sysmouse related signal. |
| 6744 | */ |
| 6745 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6746 | sysmouse_close(void) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6747 | { |
| 6748 | struct mouse_info mouse; |
| 6749 | |
| 6750 | signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL); |
| 6751 | mouse.operation = MOUSE_MODE; |
| 6752 | mouse.u.mode.mode = 0; |
| 6753 | mouse.u.mode.signal = 0; |
| 6754 | ioctl(1, CONS_MOUSECTL, &mouse); |
| 6755 | } |
| 6756 | |
| 6757 | /* |
| 6758 | * Gets info from sysmouse and adds special keys to input buf. |
| 6759 | */ |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6760 | static RETSIGTYPE |
| 6761 | sig_sysmouse SIGDEFARG(sigarg) |
| 6762 | { |
| 6763 | struct mouse_info mouse; |
| 6764 | struct video_info video; |
| 6765 | char_u string[6]; |
| 6766 | int row, col; |
| 6767 | int button; |
| 6768 | int buttons; |
| 6769 | static int oldbuttons = 0; |
| 6770 | |
| 6771 | #ifdef FEAT_GUI |
| 6772 | /* Don't put events in the input queue now. */ |
| 6773 | if (hold_gui_events) |
| 6774 | return; |
| 6775 | #endif |
| 6776 | |
| 6777 | mouse.operation = MOUSE_GETINFO; |
| 6778 | if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1 |
| 6779 | && ioctl(1, FBIO_MODEINFO, &video) != -1 |
| 6780 | && ioctl(1, CONS_MOUSECTL, &mouse) != -1 |
| 6781 | && video.vi_cheight > 0 && video.vi_cwidth > 0) |
| 6782 | { |
| 6783 | row = mouse.u.data.y / video.vi_cheight; |
| 6784 | col = mouse.u.data.x / video.vi_cwidth; |
| 6785 | buttons = mouse.u.data.buttons; |
| 6786 | string[0] = ESC; /* Our termcode */ |
| 6787 | string[1] = 'M'; |
| 6788 | string[2] = 'S'; |
| 6789 | if (oldbuttons == buttons && buttons != 0) |
| 6790 | { |
| 6791 | button = MOUSE_DRAG; |
| 6792 | } |
| 6793 | else |
| 6794 | { |
| 6795 | switch (buttons) |
| 6796 | { |
| 6797 | case 0: |
| 6798 | button = MOUSE_RELEASE; |
| 6799 | break; |
| 6800 | case 1: |
| 6801 | button = MOUSE_LEFT; |
| 6802 | break; |
| 6803 | case 2: |
| 6804 | button = MOUSE_MIDDLE; |
| 6805 | break; |
| 6806 | case 4: |
| 6807 | button = MOUSE_RIGHT; |
| 6808 | break; |
| 6809 | default: |
| 6810 | return; |
| 6811 | } |
| 6812 | oldbuttons = buttons; |
| 6813 | } |
| 6814 | string[3] = (char_u)(button); |
| 6815 | string[4] = (char_u)(col + ' ' + 1); |
| 6816 | string[5] = (char_u)(row + ' ' + 1); |
| 6817 | add_to_input_buf(string, 6); |
| 6818 | } |
| 6819 | return; |
| 6820 | } |
| 6821 | #endif /* FEAT_SYSMOUSE */ |
| 6822 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6823 | #if defined(FEAT_LIBCALL) || defined(PROTO) |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 6824 | typedef char_u * (*STRPROCSTR)(char_u *); |
| 6825 | typedef char_u * (*INTPROCSTR)(int); |
| 6826 | typedef int (*STRPROCINT)(char_u *); |
| 6827 | typedef int (*INTPROCINT)(int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6828 | |
| 6829 | /* |
| 6830 | * Call a DLL routine which takes either a string or int param |
| 6831 | * and returns an allocated string. |
| 6832 | */ |
| 6833 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6834 | mch_libcall( |
| 6835 | char_u *libname, |
| 6836 | char_u *funcname, |
| 6837 | char_u *argstring, /* NULL when using a argint */ |
| 6838 | int argint, |
| 6839 | char_u **string_result,/* NULL when using number_result */ |
| 6840 | int *number_result) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6841 | { |
| 6842 | # if defined(USE_DLOPEN) |
| 6843 | void *hinstLib; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6844 | char *dlerr = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6845 | # else |
| 6846 | shl_t hinstLib; |
| 6847 | # endif |
| 6848 | STRPROCSTR ProcAdd; |
| 6849 | INTPROCSTR ProcAddI; |
| 6850 | char_u *retval_str = NULL; |
| 6851 | int retval_int = 0; |
| 6852 | int success = FALSE; |
| 6853 | |
Bram Moolenaar | b39ef12 | 2006-06-22 16:19:31 +0000 | [diff] [blame] | 6854 | /* |
| 6855 | * Get a handle to the DLL module. |
| 6856 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6857 | # if defined(USE_DLOPEN) |
Bram Moolenaar | b39ef12 | 2006-06-22 16:19:31 +0000 | [diff] [blame] | 6858 | /* First clear any error, it's not cleared by the dlopen() call. */ |
| 6859 | (void)dlerror(); |
| 6860 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6861 | hinstLib = dlopen((char *)libname, RTLD_LAZY |
| 6862 | # ifdef RTLD_LOCAL |
| 6863 | | RTLD_LOCAL |
| 6864 | # endif |
| 6865 | ); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6866 | if (hinstLib == NULL) |
| 6867 | { |
| 6868 | /* "dlerr" must be used before dlclose() */ |
| 6869 | dlerr = (char *)dlerror(); |
| 6870 | if (dlerr != NULL) |
| 6871 | EMSG2(_("dlerror = \"%s\""), dlerr); |
| 6872 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6873 | # else |
| 6874 | hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L); |
| 6875 | # endif |
| 6876 | |
| 6877 | /* If the handle is valid, try to get the function address. */ |
| 6878 | if (hinstLib != NULL) |
| 6879 | { |
| 6880 | # ifdef HAVE_SETJMP_H |
| 6881 | /* |
| 6882 | * Catch a crash when calling the library function. For example when |
| 6883 | * using a number where a string pointer is expected. |
| 6884 | */ |
| 6885 | mch_startjmp(); |
| 6886 | if (SETJMP(lc_jump_env) != 0) |
| 6887 | { |
| 6888 | success = FALSE; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 6889 | # if defined(USE_DLOPEN) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6890 | dlerr = NULL; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 6891 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6892 | mch_didjmp(); |
| 6893 | } |
| 6894 | else |
| 6895 | # endif |
| 6896 | { |
| 6897 | retval_str = NULL; |
| 6898 | retval_int = 0; |
| 6899 | |
| 6900 | if (argstring != NULL) |
| 6901 | { |
| 6902 | # if defined(USE_DLOPEN) |
| 6903 | ProcAdd = (STRPROCSTR)dlsym(hinstLib, (const char *)funcname); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6904 | dlerr = (char *)dlerror(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6905 | # else |
| 6906 | if (shl_findsym(&hinstLib, (const char *)funcname, |
| 6907 | TYPE_PROCEDURE, (void *)&ProcAdd) < 0) |
| 6908 | ProcAdd = NULL; |
| 6909 | # endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6910 | if ((success = (ProcAdd != NULL |
| 6911 | # if defined(USE_DLOPEN) |
| 6912 | && dlerr == NULL |
| 6913 | # endif |
| 6914 | ))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6915 | { |
| 6916 | if (string_result == NULL) |
| 6917 | retval_int = ((STRPROCINT)ProcAdd)(argstring); |
| 6918 | else |
| 6919 | retval_str = (ProcAdd)(argstring); |
| 6920 | } |
| 6921 | } |
| 6922 | else |
| 6923 | { |
| 6924 | # if defined(USE_DLOPEN) |
| 6925 | ProcAddI = (INTPROCSTR)dlsym(hinstLib, (const char *)funcname); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6926 | dlerr = (char *)dlerror(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6927 | # else |
| 6928 | if (shl_findsym(&hinstLib, (const char *)funcname, |
| 6929 | TYPE_PROCEDURE, (void *)&ProcAddI) < 0) |
| 6930 | ProcAddI = NULL; |
| 6931 | # endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6932 | if ((success = (ProcAddI != NULL |
| 6933 | # if defined(USE_DLOPEN) |
| 6934 | && dlerr == NULL |
| 6935 | # endif |
| 6936 | ))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6937 | { |
| 6938 | if (string_result == NULL) |
| 6939 | retval_int = ((INTPROCINT)ProcAddI)(argint); |
| 6940 | else |
| 6941 | retval_str = (ProcAddI)(argint); |
| 6942 | } |
| 6943 | } |
| 6944 | |
| 6945 | /* Save the string before we free the library. */ |
| 6946 | /* Assume that a "1" or "-1" result is an illegal pointer. */ |
| 6947 | if (string_result == NULL) |
| 6948 | *number_result = retval_int; |
| 6949 | else if (retval_str != NULL |
| 6950 | && retval_str != (char_u *)1 |
| 6951 | && retval_str != (char_u *)-1) |
| 6952 | *string_result = vim_strsave(retval_str); |
| 6953 | } |
| 6954 | |
| 6955 | # ifdef HAVE_SETJMP_H |
| 6956 | mch_endjmp(); |
| 6957 | # ifdef SIGHASARG |
| 6958 | if (lc_signal != 0) |
| 6959 | { |
| 6960 | int i; |
| 6961 | |
| 6962 | /* try to find the name of this signal */ |
| 6963 | for (i = 0; signal_info[i].sig != -1; i++) |
| 6964 | if (lc_signal == signal_info[i].sig) |
| 6965 | break; |
| 6966 | EMSG2("E368: got SIG%s in libcall()", signal_info[i].name); |
| 6967 | } |
| 6968 | # endif |
| 6969 | # endif |
| 6970 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6971 | # if defined(USE_DLOPEN) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6972 | /* "dlerr" must be used before dlclose() */ |
| 6973 | if (dlerr != NULL) |
| 6974 | EMSG2(_("dlerror = \"%s\""), dlerr); |
| 6975 | |
| 6976 | /* Free the DLL module. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6977 | (void)dlclose(hinstLib); |
| 6978 | # else |
| 6979 | (void)shl_unload(hinstLib); |
| 6980 | # endif |
| 6981 | } |
| 6982 | |
| 6983 | if (!success) |
| 6984 | { |
| 6985 | EMSG2(_(e_libcall), funcname); |
| 6986 | return FAIL; |
| 6987 | } |
| 6988 | |
| 6989 | return OK; |
| 6990 | } |
| 6991 | #endif |
| 6992 | |
| 6993 | #if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO) |
| 6994 | static int xterm_trace = -1; /* default: disabled */ |
| 6995 | static int xterm_button; |
| 6996 | |
| 6997 | /* |
| 6998 | * Setup a dummy window for X selections in a terminal. |
| 6999 | */ |
| 7000 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7001 | setup_term_clip(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7002 | { |
| 7003 | int z = 0; |
| 7004 | char *strp = ""; |
| 7005 | Widget AppShell; |
| 7006 | |
| 7007 | if (!x_connect_to_server()) |
| 7008 | return; |
| 7009 | |
| 7010 | open_app_context(); |
| 7011 | if (app_context != NULL && xterm_Shell == (Widget)0) |
| 7012 | { |
| 7013 | int (*oldhandler)(); |
| 7014 | #if defined(HAVE_SETJMP_H) |
| 7015 | int (*oldIOhandler)(); |
| 7016 | #endif |
| 7017 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 7018 | struct timeval start_tv; |
| 7019 | |
| 7020 | if (p_verbose > 0) |
| 7021 | gettimeofday(&start_tv, NULL); |
| 7022 | # endif |
| 7023 | |
| 7024 | /* Ignore X errors while opening the display */ |
| 7025 | oldhandler = XSetErrorHandler(x_error_check); |
| 7026 | |
| 7027 | #if defined(HAVE_SETJMP_H) |
| 7028 | /* Ignore X IO errors while opening the display */ |
| 7029 | oldIOhandler = XSetIOErrorHandler(x_IOerror_check); |
| 7030 | mch_startjmp(); |
| 7031 | if (SETJMP(lc_jump_env) != 0) |
| 7032 | { |
| 7033 | mch_didjmp(); |
| 7034 | xterm_dpy = NULL; |
| 7035 | } |
| 7036 | else |
| 7037 | #endif |
| 7038 | { |
| 7039 | xterm_dpy = XtOpenDisplay(app_context, xterm_display, |
| 7040 | "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp); |
| 7041 | #if defined(HAVE_SETJMP_H) |
| 7042 | mch_endjmp(); |
| 7043 | #endif |
| 7044 | } |
| 7045 | |
| 7046 | #if defined(HAVE_SETJMP_H) |
| 7047 | /* Now handle X IO errors normally. */ |
| 7048 | (void)XSetIOErrorHandler(oldIOhandler); |
| 7049 | #endif |
| 7050 | /* Now handle X errors normally. */ |
| 7051 | (void)XSetErrorHandler(oldhandler); |
| 7052 | |
| 7053 | if (xterm_dpy == NULL) |
| 7054 | { |
| 7055 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7056 | verb_msg((char_u *)_("Opening the X display failed")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7057 | return; |
| 7058 | } |
| 7059 | |
| 7060 | /* Catch terminating error of the X server connection. */ |
| 7061 | (void)XSetIOErrorHandler(x_IOerror_handler); |
| 7062 | |
| 7063 | # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) |
| 7064 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7065 | { |
| 7066 | verbose_enter(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7067 | xopen_message(&start_tv); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7068 | verbose_leave(); |
| 7069 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7070 | # endif |
| 7071 | |
| 7072 | /* Create a Shell to make converters work. */ |
| 7073 | AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm", |
| 7074 | applicationShellWidgetClass, xterm_dpy, |
| 7075 | NULL); |
| 7076 | if (AppShell == (Widget)0) |
| 7077 | return; |
| 7078 | xterm_Shell = XtVaCreatePopupShell("VIM", |
| 7079 | topLevelShellWidgetClass, AppShell, |
| 7080 | XtNmappedWhenManaged, 0, |
| 7081 | XtNwidth, 1, |
| 7082 | XtNheight, 1, |
| 7083 | NULL); |
| 7084 | if (xterm_Shell == (Widget)0) |
| 7085 | return; |
| 7086 | |
| 7087 | x11_setup_atoms(xterm_dpy); |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 7088 | x11_setup_selection(xterm_Shell); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7089 | if (x11_display == NULL) |
| 7090 | x11_display = xterm_dpy; |
| 7091 | |
| 7092 | XtRealizeWidget(xterm_Shell); |
| 7093 | XSync(xterm_dpy, False); |
| 7094 | xterm_update(); |
| 7095 | } |
| 7096 | if (xterm_Shell != (Widget)0) |
| 7097 | { |
| 7098 | clip_init(TRUE); |
| 7099 | if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL) |
| 7100 | x11_window = (Window)atol(strp); |
| 7101 | /* Check if $WINDOWID is valid. */ |
| 7102 | if (test_x11_window(xterm_dpy) == FAIL) |
| 7103 | x11_window = 0; |
| 7104 | if (x11_window != 0) |
| 7105 | xterm_trace = 0; |
| 7106 | } |
| 7107 | } |
| 7108 | |
| 7109 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7110 | start_xterm_trace(int button) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7111 | { |
| 7112 | if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0) |
| 7113 | return; |
| 7114 | xterm_trace = 1; |
| 7115 | xterm_button = button; |
| 7116 | do_xterm_trace(); |
| 7117 | } |
| 7118 | |
| 7119 | |
| 7120 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7121 | stop_xterm_trace(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7122 | { |
| 7123 | if (xterm_trace < 0) |
| 7124 | return; |
| 7125 | xterm_trace = 0; |
| 7126 | } |
| 7127 | |
| 7128 | /* |
| 7129 | * Query the xterm pointer and generate mouse termcodes if necessary |
| 7130 | * return TRUE if dragging is active, else FALSE |
| 7131 | */ |
| 7132 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7133 | do_xterm_trace(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7134 | { |
| 7135 | Window root, child; |
| 7136 | int root_x, root_y; |
| 7137 | int win_x, win_y; |
| 7138 | int row, col; |
| 7139 | int_u mask_return; |
| 7140 | char_u buf[50]; |
| 7141 | char_u *strp; |
| 7142 | long got_hints; |
| 7143 | static char_u *mouse_code; |
| 7144 | static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER}; |
| 7145 | static int prev_row = 0, prev_col = 0; |
| 7146 | static XSizeHints xterm_hints; |
| 7147 | |
| 7148 | if (xterm_trace <= 0) |
| 7149 | return FALSE; |
| 7150 | |
| 7151 | if (xterm_trace == 1) |
| 7152 | { |
| 7153 | /* Get the hints just before tracking starts. The font size might |
Bram Moolenaar | a6c2c91 | 2008-01-13 15:31:00 +0000 | [diff] [blame] | 7154 | * have changed recently. */ |
| 7155 | if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints) |
| 7156 | || !(got_hints & PResizeInc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7157 | || xterm_hints.width_inc <= 1 |
| 7158 | || xterm_hints.height_inc <= 1) |
| 7159 | { |
| 7160 | xterm_trace = -1; /* Not enough data -- disable tracing */ |
| 7161 | return FALSE; |
| 7162 | } |
| 7163 | |
| 7164 | /* Rely on the same mouse code for the duration of this */ |
| 7165 | mouse_code = find_termcode(mouse_name); |
| 7166 | prev_row = mouse_row; |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 7167 | prev_col = mouse_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7168 | xterm_trace = 2; |
| 7169 | |
| 7170 | /* Find the offset of the chars, there might be a scrollbar on the |
| 7171 | * left of the window and/or a menu on the top (eterm etc.) */ |
| 7172 | XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y, |
| 7173 | &win_x, &win_y, &mask_return); |
| 7174 | xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row) |
| 7175 | - (xterm_hints.height_inc / 2); |
| 7176 | if (xterm_hints.y <= xterm_hints.height_inc / 2) |
| 7177 | xterm_hints.y = 2; |
| 7178 | xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col) |
| 7179 | - (xterm_hints.width_inc / 2); |
| 7180 | if (xterm_hints.x <= xterm_hints.width_inc / 2) |
| 7181 | xterm_hints.x = 2; |
| 7182 | return TRUE; |
| 7183 | } |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 7184 | if (mouse_code == NULL || STRLEN(mouse_code) > 45) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7185 | { |
| 7186 | xterm_trace = 0; |
| 7187 | return FALSE; |
| 7188 | } |
| 7189 | |
| 7190 | XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y, |
| 7191 | &win_x, &win_y, &mask_return); |
| 7192 | |
| 7193 | row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc); |
| 7194 | col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc); |
| 7195 | if (row == prev_row && col == prev_col) |
| 7196 | return TRUE; |
| 7197 | |
| 7198 | STRCPY(buf, mouse_code); |
| 7199 | strp = buf + STRLEN(buf); |
| 7200 | *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20; |
| 7201 | *strp++ = (char_u)(col + ' ' + 1); |
| 7202 | *strp++ = (char_u)(row + ' ' + 1); |
| 7203 | *strp = 0; |
| 7204 | add_to_input_buf(buf, STRLEN(buf)); |
| 7205 | |
| 7206 | prev_row = row; |
| 7207 | prev_col = col; |
| 7208 | return TRUE; |
| 7209 | } |
| 7210 | |
| 7211 | # if defined(FEAT_GUI) || defined(PROTO) |
| 7212 | /* |
| 7213 | * Destroy the display, window and app_context. Required for GTK. |
| 7214 | */ |
| 7215 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7216 | clear_xterm_clip(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7217 | { |
| 7218 | if (xterm_Shell != (Widget)0) |
| 7219 | { |
| 7220 | XtDestroyWidget(xterm_Shell); |
| 7221 | xterm_Shell = (Widget)0; |
| 7222 | } |
| 7223 | if (xterm_dpy != NULL) |
| 7224 | { |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7225 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7226 | /* Lesstif and Solaris crash here, lose some memory */ |
| 7227 | XtCloseDisplay(xterm_dpy); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7228 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7229 | if (x11_display == xterm_dpy) |
| 7230 | x11_display = NULL; |
| 7231 | xterm_dpy = NULL; |
| 7232 | } |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7233 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7234 | if (app_context != (XtAppContext)NULL) |
| 7235 | { |
| 7236 | /* Lesstif and Solaris crash here, lose some memory */ |
| 7237 | XtDestroyApplicationContext(app_context); |
| 7238 | app_context = (XtAppContext)NULL; |
| 7239 | } |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7240 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7241 | } |
| 7242 | # endif |
| 7243 | |
| 7244 | /* |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 7245 | * Catch up with GUI or X events. |
| 7246 | */ |
| 7247 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7248 | clip_update(void) |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 7249 | { |
| 7250 | # ifdef FEAT_GUI |
| 7251 | if (gui.in_use) |
| 7252 | gui_mch_update(); |
| 7253 | else |
| 7254 | # endif |
| 7255 | if (xterm_Shell != (Widget)0) |
| 7256 | xterm_update(); |
| 7257 | } |
| 7258 | |
| 7259 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7260 | * Catch up with any queued X events. This may put keyboard input into the |
| 7261 | * input buffer, call resize call-backs, trigger timers etc. If there is |
| 7262 | * nothing in the X event queue (& no timers pending), then we return |
| 7263 | * immediately. |
| 7264 | */ |
| 7265 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7266 | xterm_update(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7267 | { |
| 7268 | XEvent event; |
| 7269 | |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7270 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7271 | { |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7272 | XtInputMask mask = XtAppPending(app_context); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7273 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7274 | if (mask == 0 || vim_is_input_buf_full()) |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7275 | break; |
| 7276 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7277 | if (mask & XtIMXEvent) |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7278 | { |
| 7279 | /* There is an event to process. */ |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7280 | XtAppNextEvent(app_context, &event); |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7281 | #ifdef FEAT_CLIENTSERVER |
| 7282 | { |
| 7283 | XPropertyEvent *e = (XPropertyEvent *)&event; |
| 7284 | |
| 7285 | if (e->type == PropertyNotify && e->window == commWindow |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7286 | && e->atom == commProperty && e->state == PropertyNewValue) |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7287 | serverEventProc(xterm_dpy, &event, 0); |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7288 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7289 | #endif |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7290 | XtDispatchEvent(&event); |
| 7291 | } |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 7292 | else |
| 7293 | { |
| 7294 | /* There is something else than an event to process. */ |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7295 | XtAppProcessEvent(app_context, mask); |
| 7296 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7297 | } |
| 7298 | } |
| 7299 | |
| 7300 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7301 | clip_xterm_own_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7302 | { |
| 7303 | if (xterm_Shell != (Widget)0) |
| 7304 | return clip_x11_own_selection(xterm_Shell, cbd); |
| 7305 | return FAIL; |
| 7306 | } |
| 7307 | |
| 7308 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7309 | clip_xterm_lose_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7310 | { |
| 7311 | if (xterm_Shell != (Widget)0) |
| 7312 | clip_x11_lose_selection(xterm_Shell, cbd); |
| 7313 | } |
| 7314 | |
| 7315 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7316 | clip_xterm_request_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7317 | { |
| 7318 | if (xterm_Shell != (Widget)0) |
| 7319 | clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd); |
| 7320 | } |
| 7321 | |
| 7322 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7323 | clip_xterm_set_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7324 | { |
| 7325 | clip_x11_set_selection(cbd); |
| 7326 | } |
| 7327 | #endif |
| 7328 | |
| 7329 | |
| 7330 | #if defined(USE_XSMP) || defined(PROTO) |
| 7331 | /* |
| 7332 | * Code for X Session Management Protocol. |
| 7333 | */ |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 7334 | static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast); |
| 7335 | static void xsmp_die(SmcConn smc_conn, SmPointer client_data); |
| 7336 | static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data); |
| 7337 | static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data); |
| 7338 | 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] | 7339 | |
| 7340 | |
| 7341 | # if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 7342 | static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7343 | |
| 7344 | /* |
| 7345 | * This is our chance to ask the user if they want to save, |
| 7346 | * or abort the logout |
| 7347 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7348 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7349 | xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7350 | { |
| 7351 | cmdmod_T save_cmdmod; |
| 7352 | int cancel_shutdown = False; |
| 7353 | |
| 7354 | save_cmdmod = cmdmod; |
| 7355 | cmdmod.confirm = TRUE; |
Bram Moolenaar | 027387f | 2016-01-02 22:25:52 +0100 | [diff] [blame] | 7356 | if (check_changed_any(FALSE, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7357 | /* Mustn't logout */ |
| 7358 | cancel_shutdown = True; |
| 7359 | cmdmod = save_cmdmod; |
| 7360 | setcursor(); /* position cursor */ |
| 7361 | out_flush(); |
| 7362 | |
| 7363 | /* Done interaction */ |
| 7364 | SmcInteractDone(smc_conn, cancel_shutdown); |
| 7365 | |
| 7366 | /* Finish off |
| 7367 | * Only end save-yourself here if we're not cancelling shutdown; |
| 7368 | * we'll get a cancelled callback later in which we'll end it. |
| 7369 | * Hopefully get around glitchy SMs (like GNOME-1) |
| 7370 | */ |
| 7371 | if (!cancel_shutdown) |
| 7372 | { |
| 7373 | xsmp.save_yourself = False; |
| 7374 | SmcSaveYourselfDone(smc_conn, True); |
| 7375 | } |
| 7376 | } |
| 7377 | # endif |
| 7378 | |
| 7379 | /* |
| 7380 | * Callback that starts save-yourself. |
| 7381 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7382 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7383 | xsmp_handle_save_yourself( |
| 7384 | SmcConn smc_conn, |
| 7385 | SmPointer client_data UNUSED, |
| 7386 | int save_type UNUSED, |
| 7387 | Bool shutdown, |
| 7388 | int interact_style UNUSED, |
| 7389 | Bool fast UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7390 | { |
| 7391 | /* Handle already being in saveyourself */ |
| 7392 | if (xsmp.save_yourself) |
| 7393 | SmcSaveYourselfDone(smc_conn, True); |
| 7394 | xsmp.save_yourself = True; |
| 7395 | xsmp.shutdown = shutdown; |
| 7396 | |
| 7397 | /* First up, preserve all files */ |
| 7398 | out_flush(); |
| 7399 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 7400 | |
| 7401 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7402 | verb_msg((char_u *)_("XSMP handling save-yourself request")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7403 | |
| 7404 | # if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT) |
| 7405 | /* Now see if we can ask about unsaved files */ |
| 7406 | if (shutdown && !fast && gui.in_use) |
| 7407 | /* Need to interact with user, but need SM's permission */ |
| 7408 | SmcInteractRequest(smc_conn, SmDialogError, |
| 7409 | xsmp_handle_interaction, client_data); |
| 7410 | else |
| 7411 | # endif |
| 7412 | { |
| 7413 | /* Can stop the cycle here */ |
| 7414 | SmcSaveYourselfDone(smc_conn, True); |
| 7415 | xsmp.save_yourself = False; |
| 7416 | } |
| 7417 | } |
| 7418 | |
| 7419 | |
| 7420 | /* |
| 7421 | * Callback to warn us of imminent death. |
| 7422 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7423 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7424 | xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7425 | { |
| 7426 | xsmp_close(); |
| 7427 | |
| 7428 | /* quit quickly leaving swapfiles for modified buffers behind */ |
| 7429 | getout_preserve_modified(0); |
| 7430 | } |
| 7431 | |
| 7432 | |
| 7433 | /* |
| 7434 | * Callback to tell us that save-yourself has completed. |
| 7435 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7436 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7437 | xsmp_save_complete( |
| 7438 | SmcConn smc_conn UNUSED, |
| 7439 | SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7440 | { |
| 7441 | xsmp.save_yourself = False; |
| 7442 | } |
| 7443 | |
| 7444 | |
| 7445 | /* |
| 7446 | * Callback to tell us that an instigated shutdown was cancelled |
| 7447 | * (maybe even by us) |
| 7448 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7449 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7450 | xsmp_shutdown_cancelled( |
| 7451 | SmcConn smc_conn, |
| 7452 | SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7453 | { |
| 7454 | if (xsmp.save_yourself) |
| 7455 | SmcSaveYourselfDone(smc_conn, True); |
| 7456 | xsmp.save_yourself = False; |
| 7457 | xsmp.shutdown = False; |
| 7458 | } |
| 7459 | |
| 7460 | |
| 7461 | /* |
| 7462 | * Callback to tell us that a new ICE connection has been established. |
| 7463 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7464 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7465 | xsmp_ice_connection( |
| 7466 | IceConn iceConn, |
| 7467 | IcePointer clientData UNUSED, |
| 7468 | Bool opening, |
| 7469 | IcePointer *watchData UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7470 | { |
| 7471 | /* Intercept creation of ICE connection fd */ |
| 7472 | if (opening) |
| 7473 | { |
| 7474 | xsmp_icefd = IceConnectionNumber(iceConn); |
| 7475 | IceRemoveConnectionWatch(xsmp_ice_connection, NULL); |
| 7476 | } |
| 7477 | } |
| 7478 | |
| 7479 | |
| 7480 | /* Handle any ICE processing that's required; return FAIL if SM lost */ |
| 7481 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7482 | xsmp_handle_requests(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7483 | { |
| 7484 | Bool rep; |
| 7485 | |
| 7486 | if (IceProcessMessages(xsmp.iceconn, NULL, &rep) |
| 7487 | == IceProcessMessagesIOError) |
| 7488 | { |
| 7489 | /* Lost ICE */ |
| 7490 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7491 | verb_msg((char_u *)_("XSMP lost ICE connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7492 | xsmp_close(); |
| 7493 | return FAIL; |
| 7494 | } |
| 7495 | else |
| 7496 | return OK; |
| 7497 | } |
| 7498 | |
| 7499 | static int dummy; |
| 7500 | |
| 7501 | /* Set up X Session Management Protocol */ |
| 7502 | void |
| 7503 | xsmp_init(void) |
| 7504 | { |
| 7505 | char errorstring[80]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7506 | SmcCallbacks smcallbacks; |
| 7507 | #if 0 |
| 7508 | SmPropValue smname; |
| 7509 | SmProp smnameprop; |
| 7510 | SmProp *smprops[1]; |
| 7511 | #endif |
| 7512 | |
| 7513 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7514 | verb_msg((char_u *)_("XSMP opening connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7515 | |
| 7516 | xsmp.save_yourself = xsmp.shutdown = False; |
| 7517 | |
| 7518 | /* Set up SM callbacks - must have all, even if they're not used */ |
| 7519 | smcallbacks.save_yourself.callback = xsmp_handle_save_yourself; |
| 7520 | smcallbacks.save_yourself.client_data = NULL; |
| 7521 | smcallbacks.die.callback = xsmp_die; |
| 7522 | smcallbacks.die.client_data = NULL; |
| 7523 | smcallbacks.save_complete.callback = xsmp_save_complete; |
| 7524 | smcallbacks.save_complete.client_data = NULL; |
| 7525 | smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled; |
| 7526 | smcallbacks.shutdown_cancelled.client_data = NULL; |
| 7527 | |
| 7528 | /* Set up a watch on ICE connection creations. The "dummy" argument is |
| 7529 | * apparently required for FreeBSD (we get a BUS error when using NULL). */ |
| 7530 | if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0) |
| 7531 | { |
| 7532 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7533 | verb_msg((char_u *)_("XSMP ICE connection watch failed")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7534 | return; |
| 7535 | } |
| 7536 | |
| 7537 | /* Create an SM connection */ |
| 7538 | xsmp.smcconn = SmcOpenConnection( |
| 7539 | NULL, |
| 7540 | NULL, |
| 7541 | SmProtoMajor, |
| 7542 | SmProtoMinor, |
| 7543 | SmcSaveYourselfProcMask | SmcDieProcMask |
| 7544 | | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask, |
| 7545 | &smcallbacks, |
| 7546 | NULL, |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7547 | &xsmp.clientid, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7548 | sizeof(errorstring), |
| 7549 | errorstring); |
| 7550 | if (xsmp.smcconn == NULL) |
| 7551 | { |
| 7552 | char errorreport[132]; |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 7553 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7554 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7555 | { |
| 7556 | vim_snprintf(errorreport, sizeof(errorreport), |
| 7557 | _("XSMP SmcOpenConnection failed: %s"), errorstring); |
| 7558 | verb_msg((char_u *)errorreport); |
| 7559 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7560 | return; |
| 7561 | } |
| 7562 | xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn); |
| 7563 | |
| 7564 | #if 0 |
| 7565 | /* ID ourselves */ |
| 7566 | smname.value = "vim"; |
| 7567 | smname.length = 3; |
| 7568 | smnameprop.name = "SmProgram"; |
| 7569 | smnameprop.type = "SmARRAY8"; |
| 7570 | smnameprop.num_vals = 1; |
| 7571 | smnameprop.vals = &smname; |
| 7572 | |
| 7573 | smprops[0] = &smnameprop; |
| 7574 | SmcSetProperties(xsmp.smcconn, 1, smprops); |
| 7575 | #endif |
| 7576 | } |
| 7577 | |
| 7578 | |
| 7579 | /* Shut down XSMP comms. */ |
| 7580 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7581 | xsmp_close(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7582 | { |
| 7583 | if (xsmp_icefd != -1) |
| 7584 | { |
| 7585 | SmcCloseConnection(xsmp.smcconn, 0, NULL); |
Bram Moolenaar | 5a22181 | 2008-11-12 12:08:45 +0000 | [diff] [blame] | 7586 | if (xsmp.clientid != NULL) |
| 7587 | free(xsmp.clientid); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 7588 | xsmp.clientid = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7589 | xsmp_icefd = -1; |
| 7590 | } |
| 7591 | } |
| 7592 | #endif /* USE_XSMP */ |
| 7593 | |
| 7594 | |
| 7595 | #ifdef EBCDIC |
| 7596 | /* Translate character to its CTRL- value */ |
| 7597 | char CtrlTable[] = |
| 7598 | { |
| 7599 | /* 00 - 5E */ |
| 7600 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7601 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7602 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7603 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7604 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7605 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7606 | /* ^ */ 0x1E, |
| 7607 | /* - */ 0x1F, |
| 7608 | /* 61 - 6C */ |
| 7609 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7610 | /* _ */ 0x1F, |
| 7611 | /* 6E - 80 */ |
| 7612 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7613 | /* a */ 0x01, |
| 7614 | /* b */ 0x02, |
| 7615 | /* c */ 0x03, |
| 7616 | /* d */ 0x37, |
| 7617 | /* e */ 0x2D, |
| 7618 | /* f */ 0x2E, |
| 7619 | /* g */ 0x2F, |
| 7620 | /* h */ 0x16, |
| 7621 | /* i */ 0x05, |
| 7622 | /* 8A - 90 */ |
| 7623 | 0, 0, 0, 0, 0, 0, 0, |
| 7624 | /* j */ 0x15, |
| 7625 | /* k */ 0x0B, |
| 7626 | /* l */ 0x0C, |
| 7627 | /* m */ 0x0D, |
| 7628 | /* n */ 0x0E, |
| 7629 | /* o */ 0x0F, |
| 7630 | /* p */ 0x10, |
| 7631 | /* q */ 0x11, |
| 7632 | /* r */ 0x12, |
| 7633 | /* 9A - A1 */ |
| 7634 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 7635 | /* s */ 0x13, |
| 7636 | /* t */ 0x3C, |
| 7637 | /* u */ 0x3D, |
| 7638 | /* v */ 0x32, |
| 7639 | /* w */ 0x26, |
| 7640 | /* x */ 0x18, |
| 7641 | /* y */ 0x19, |
| 7642 | /* z */ 0x3F, |
| 7643 | /* AA - AC */ |
| 7644 | 0, 0, 0, |
| 7645 | /* [ */ 0x27, |
| 7646 | /* AE - BC */ |
| 7647 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7648 | /* ] */ 0x1D, |
| 7649 | /* BE - C0 */ 0, 0, 0, |
| 7650 | /* A */ 0x01, |
| 7651 | /* B */ 0x02, |
| 7652 | /* C */ 0x03, |
| 7653 | /* D */ 0x37, |
| 7654 | /* E */ 0x2D, |
| 7655 | /* F */ 0x2E, |
| 7656 | /* G */ 0x2F, |
| 7657 | /* H */ 0x16, |
| 7658 | /* I */ 0x05, |
| 7659 | /* CA - D0 */ 0, 0, 0, 0, 0, 0, 0, |
| 7660 | /* J */ 0x15, |
| 7661 | /* K */ 0x0B, |
| 7662 | /* L */ 0x0C, |
| 7663 | /* M */ 0x0D, |
| 7664 | /* N */ 0x0E, |
| 7665 | /* O */ 0x0F, |
| 7666 | /* P */ 0x10, |
| 7667 | /* Q */ 0x11, |
| 7668 | /* R */ 0x12, |
| 7669 | /* DA - DF */ 0, 0, 0, 0, 0, 0, |
| 7670 | /* \ */ 0x1C, |
| 7671 | /* E1 */ 0, |
| 7672 | /* S */ 0x13, |
| 7673 | /* T */ 0x3C, |
| 7674 | /* U */ 0x3D, |
| 7675 | /* V */ 0x32, |
| 7676 | /* W */ 0x26, |
| 7677 | /* X */ 0x18, |
| 7678 | /* Y */ 0x19, |
| 7679 | /* Z */ 0x3F, |
| 7680 | /* EA - FF*/ 0, 0, 0, 0, 0, 0, |
| 7681 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7682 | }; |
| 7683 | |
| 7684 | char MetaCharTable[]= |
| 7685 | {/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 7686 | 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0, |
| 7687 | 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0, |
| 7688 | '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0, |
| 7689 | 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0 |
| 7690 | }; |
| 7691 | |
| 7692 | |
| 7693 | /* TODO: Use characters NOT numbers!!! */ |
| 7694 | char CtrlCharTable[]= |
| 7695 | {/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 7696 | 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214, |
| 7697 | 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109, |
| 7698 | 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199, |
| 7699 | 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233, |
| 7700 | }; |
| 7701 | |
| 7702 | |
| 7703 | #endif |