Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * OS/2 port by Paul Slootman |
| 5 | * VMS merge by Zoltan Arpadffy |
| 6 | * |
| 7 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 8 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 9 | * See README.txt for an overview of the Vim source code. |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...) |
| 14 | * Also for OS/2, using the excellent EMX package!!! |
Bram Moolenaar | 041c710 | 2020-05-30 18:14:57 +0200 | [diff] [blame] | 15 | * Also for Atari MiNT. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 16 | * |
| 17 | * A lot of this file was originally written by Juergen Weigert and later |
| 18 | * changed beyond recognition. |
| 19 | */ |
| 20 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 21 | #include "vim.h" |
| 22 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 23 | #ifdef FEAT_MZSCHEME |
| 24 | # include "if_mzsch.h" |
| 25 | #endif |
| 26 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 27 | #include "os_unixx.h" // unix includes for os_unix.c only |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 28 | |
| 29 | #ifdef USE_XSMP |
| 30 | # include <X11/SM/SMlib.h> |
| 31 | #endif |
| 32 | |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 33 | #ifdef HAVE_SELINUX |
| 34 | # include <selinux/selinux.h> |
| 35 | static int selinux_enabled = -1; |
| 36 | #endif |
| 37 | |
Christian Brabandt | e085dfd | 2023-09-30 12:49:18 +0200 | [diff] [blame] | 38 | #ifdef FEAT_XATTR |
zeertzjq | 6de4e58 | 2023-09-30 14:19:14 +0200 | [diff] [blame] | 39 | # include <sys/xattr.h> |
Christian Brabandt | e085dfd | 2023-09-30 12:49:18 +0200 | [diff] [blame] | 40 | #endif |
| 41 | |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 42 | #ifdef HAVE_SMACK |
zeertzjq | 6de4e58 | 2023-09-30 14:19:14 +0200 | [diff] [blame] | 43 | # include <sys/xattr.h> |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 44 | # include <linux/xattr.h> |
| 45 | # ifndef SMACK_LABEL_LEN |
| 46 | # define SMACK_LABEL_LEN 1024 |
| 47 | # endif |
| 48 | #endif |
| 49 | |
Bram Moolenaar | a244243 | 2007-04-26 14:26:37 +0000 | [diff] [blame] | 50 | #ifdef __CYGWIN__ |
K.Takata | 972db23 | 2022-02-04 10:45:38 +0000 | [diff] [blame] | 51 | # include <cygwin/version.h> |
| 52 | # include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 53 | // for cygwin_conv_path() |
K.Takata | 972db23 | 2022-02-04 10:45:38 +0000 | [diff] [blame] | 54 | # ifdef FEAT_CYGWIN_WIN32_CLIPBOARD |
| 55 | # define WIN32_LEAN_AND_MEAN |
| 56 | # include <windows.h> |
| 57 | # include "winclip.pro" |
Bram Moolenaar | a244243 | 2007-04-26 14:26:37 +0000 | [diff] [blame] | 58 | # endif |
| 59 | #endif |
| 60 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | #ifdef FEAT_MOUSE_GPM |
Bram Moolenaar | 33fc4a6 | 2022-02-23 18:07:38 +0000 | [diff] [blame] | 62 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 63 | # include <gpm.h> |
Bram Moolenaar | 33fc4a6 | 2022-02-23 18:07:38 +0000 | [diff] [blame] | 64 | |
| 65 | # ifdef DYNAMIC_GPM |
| 66 | # define Gpm_Open (*dll_Gpm_Open) |
| 67 | # define Gpm_Close (*dll_Gpm_Close) |
| 68 | # define Gpm_GetEvent (*dll_Gpm_GetEvent) |
| 69 | # define gpm_flag (dll_gpm_flag != NULL ? *dll_gpm_flag : 0) |
| 70 | # define gpm_fd (dll_gpm_fd != NULL ? *dll_gpm_fd : -1) |
| 71 | |
| 72 | static int (*dll_Gpm_Open) (Gpm_Connect *, int); |
| 73 | static int (*dll_Gpm_Close) (void); |
| 74 | static int (*dll_Gpm_GetEvent) (Gpm_Event *); |
| 75 | static int *dll_gpm_flag; |
| 76 | static int *dll_gpm_fd; |
| 77 | |
| 78 | static void *libgpm_hinst; |
| 79 | # endif |
| 80 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 81 | // <linux/keyboard.h> contains defines conflicting with "keymap.h", |
| 82 | // I just copied relevant defines here. A cleaner solution would be to put gpm |
| 83 | // code into separate file and include there linux/keyboard.h |
| 84 | // #include <linux/keyboard.h> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 85 | # define KG_SHIFT 0 |
| 86 | # define KG_CTRL 2 |
| 87 | # define KG_ALT 3 |
| 88 | # define KG_ALTGR 1 |
| 89 | # define KG_SHIFTL 4 |
| 90 | # define KG_SHIFTR 5 |
| 91 | # define KG_CTRLL 6 |
| 92 | # define KG_CTRLR 7 |
| 93 | # define KG_CAPSSHIFT 8 |
| 94 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 95 | static void gpm_close(void); |
| 96 | static int gpm_open(void); |
| 97 | static int mch_gpm_process(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 98 | #endif |
| 99 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 100 | #ifdef FEAT_SYSMOUSE |
| 101 | # include <sys/consio.h> |
| 102 | # include <sys/fbio.h> |
| 103 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 104 | static int sysmouse_open(void); |
| 105 | static void sysmouse_close(void); |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 106 | static void sig_sysmouse SIGPROTOARG; |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 107 | #endif |
| 108 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 109 | /* |
| 110 | * end of autoconf section. To be extended... |
| 111 | */ |
| 112 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 113 | // Are the following #ifdefs still required? And why? Is that for X11? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 114 | |
| 115 | #if defined(ESIX) || defined(M_UNIX) && !defined(SCO) |
| 116 | # ifdef SIGWINCH |
| 117 | # undef SIGWINCH |
| 118 | # endif |
| 119 | # ifdef TIOCGWINSZ |
| 120 | # undef TIOCGWINSZ |
| 121 | # endif |
| 122 | #endif |
| 123 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 124 | #if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 125 | # define SIGWINCH SIGWINDOW |
| 126 | #endif |
| 127 | |
| 128 | #ifdef FEAT_X11 |
| 129 | # include <X11/Xlib.h> |
| 130 | # include <X11/Xutil.h> |
| 131 | # include <X11/Xatom.h> |
| 132 | # ifdef FEAT_XCLIPBOARD |
| 133 | # include <X11/Intrinsic.h> |
| 134 | # include <X11/Shell.h> |
| 135 | # include <X11/StringDefs.h> |
| 136 | static Widget xterm_Shell = (Widget)0; |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 137 | static void clip_update(void); |
| 138 | static void xterm_update(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | # endif |
| 140 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 141 | Window x11_window = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 142 | Display *x11_display = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 143 | #endif |
| 144 | |
Bram Moolenaar | 5c3128e | 2020-05-11 20:54:42 +0200 | [diff] [blame] | 145 | static int ignore_sigtstp = FALSE; |
| 146 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 147 | static int get_x11_title(int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 148 | |
| 149 | static char_u *oldtitle = NULL; |
Bram Moolenaar | 8e82c05 | 2018-08-21 19:47:48 +0200 | [diff] [blame] | 150 | static volatile sig_atomic_t oldtitle_outdated = FALSE; |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 151 | static int unix_did_set_title = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 152 | static char_u *oldicon = NULL; |
| 153 | static int did_set_icon = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 154 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 155 | static void may_core_dump(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 157 | #ifdef HAVE_UNION_WAIT |
| 158 | typedef union wait waitstatus; |
| 159 | #else |
| 160 | typedef int waitstatus; |
| 161 | #endif |
Bram Moolenaar | e9c21ae | 2017-08-03 20:44:48 +0200 | [diff] [blame] | 162 | static int WaitForChar(long msec, int *interrupted, int ignore_input); |
| 163 | static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input); |
Bram Moolenaar | 041c710 | 2020-05-30 18:14:57 +0200 | [diff] [blame] | 164 | #ifdef VMS |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 165 | int RealWaitForChar(int, long, int *, int *interrupted); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 166 | #else |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 167 | static int RealWaitForChar(int, long, int *, int *interrupted); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 168 | #endif |
| 169 | |
| 170 | #ifdef FEAT_XCLIPBOARD |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 171 | static int do_xterm_trace(void); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 172 | # define XT_TRACE_DELAY 50 // delay for xterm tracing |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 173 | #endif |
| 174 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 175 | static void handle_resize(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 176 | |
| 177 | #if defined(SIGWINCH) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 178 | static void sig_winch SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 179 | #endif |
dbivolaru | ab16ad3 | 2021-12-29 19:41:47 +0000 | [diff] [blame] | 180 | #if defined(SIGTSTP) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 181 | static void sig_tstp SIGPROTOARG; |
Bram Moolenaar | abd56da | 2022-06-23 20:46:27 +0100 | [diff] [blame] | 182 | // volatile because it is used in signal handler sig_tstp() and |
| 183 | // sigcont_handler(). |
dbivolaru | ab16ad3 | 2021-12-29 19:41:47 +0000 | [diff] [blame] | 184 | static volatile sig_atomic_t in_mch_suspend = FALSE; |
| 185 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 186 | #if defined(SIGINT) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 187 | static void catch_sigint SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 188 | #endif |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 189 | #if defined(SIGUSR1) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 190 | static void catch_sigusr1 SIGPROTOARG; |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 191 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 192 | #if defined(SIGPWR) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 193 | static void catch_sigpwr SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 194 | #endif |
Bram Moolenaar | 651fca8 | 2021-11-29 20:39:38 +0000 | [diff] [blame] | 195 | #if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 196 | # define SET_SIG_ALARM |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 197 | static void sig_alarm SIGPROTOARG; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 198 | // volatile because it is used in signal handler sig_alarm(). |
Bram Moolenaar | 8e82c05 | 2018-08-21 19:47:48 +0200 | [diff] [blame] | 199 | static volatile sig_atomic_t sig_alarm_called; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 200 | #endif |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 201 | static void deathtrap SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 202 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 203 | static void catch_int_signal(void); |
| 204 | static void set_signals(void); |
Michael Jarvis | be9624e | 2023-04-19 20:28:48 +0100 | [diff] [blame] | 205 | static void catch_signals(void (*func_deadly)(int), void (*func_other)(int)); |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 206 | #ifdef HAVE_SIGPROCMASK |
| 207 | # define SIGSET_DECL(set) sigset_t set; |
| 208 | # define BLOCK_SIGNALS(set) block_signals(set) |
| 209 | # define UNBLOCK_SIGNALS(set) unblock_signals(set) |
| 210 | #else |
| 211 | # define SIGSET_DECL(set) |
| 212 | # define BLOCK_SIGNALS(set) do { /**/ } while (0) |
| 213 | # define UNBLOCK_SIGNALS(set) do { /**/ } while (0) |
| 214 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 215 | static int have_wildcard(int, char_u **); |
| 216 | static int have_dollars(int, char_u **); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 217 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 218 | 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] | 219 | |
| 220 | #ifndef SIG_ERR |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 221 | # define SIG_ERR ((sighandler_T)-1) |
| 222 | #endif |
| 223 | #ifndef SIG_HOLD |
| 224 | # define SIG_HOLD ((sighandler_T)-2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 225 | #endif |
| 226 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 227 | // volatile because it is used in signal handler sig_winch(). |
Bram Moolenaar | 8e82c05 | 2018-08-21 19:47:48 +0200 | [diff] [blame] | 228 | static volatile sig_atomic_t do_resize = FALSE; |
dbivolaru | ab16ad3 | 2021-12-29 19:41:47 +0000 | [diff] [blame] | 229 | // volatile because it is used in signal handler sig_tstp(). |
| 230 | static volatile sig_atomic_t got_tstp = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 231 | static char_u *extra_shell_arg = NULL; |
| 232 | static int show_shell_mess = TRUE; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 233 | // volatile because it is used in signal handler deathtrap(). |
| 234 | static volatile sig_atomic_t deadly_signal = 0; // The signal we caught |
| 235 | // volatile because it is used in signal handler deathtrap(). |
| 236 | static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 237 | |
Bram Moolenaar | c4d4ac2 | 2016-11-07 22:42:57 +0100 | [diff] [blame] | 238 | #if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM) |
| 239 | static int dont_check_job_ended = 0; |
| 240 | #endif |
| 241 | |
Bram Moolenaar | 26e8644 | 2020-05-17 14:06:16 +0200 | [diff] [blame] | 242 | // Current terminal mode from mch_settmode(). Can differ from cur_tmode. |
| 243 | static tmode_T mch_cur_tmode = TMODE_COOK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 244 | |
| 245 | #ifdef USE_XSMP |
| 246 | typedef struct |
| 247 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 248 | SmcConn smcconn; // The SM connection ID |
| 249 | IceConn iceconn; // The ICE connection ID |
| 250 | char *clientid; // The client ID for the current smc session |
| 251 | Bool save_yourself; // If we're in the middle of a save_yourself |
| 252 | Bool shutdown; // If we're in shutdown mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 253 | } xsmp_config_T; |
| 254 | |
| 255 | static xsmp_config_T xsmp; |
| 256 | #endif |
| 257 | |
| 258 | #ifdef SYS_SIGLIST_DECLARED |
| 259 | /* |
| 260 | * I have seen |
| 261 | * extern char *_sys_siglist[NSIG]; |
Yegappan Lakshmanan | aebc6ef | 2022-08-27 21:24:26 +0100 | [diff] [blame] | 262 | * on Linux, NetBSD and Solaris. It contains a nice list of strings |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 263 | * that describe the signals. That is nearly what we want here. But |
| 264 | * autoconf does only check for sys_siglist (without the underscore), I |
| 265 | * do not want to change everything today.... jw. |
Bram Moolenaar | 3f7d090 | 2016-11-12 21:13:42 +0100 | [diff] [blame] | 266 | * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 267 | */ |
| 268 | #endif |
| 269 | |
| 270 | static struct signalinfo |
| 271 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 272 | int sig; // Signal number, eg. SIGSEGV etc |
| 273 | char *name; // Signal name (not char_u!). |
| 274 | char deadly; // Catch as a deadly signal? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 275 | } signal_info[] = |
| 276 | { |
| 277 | #ifdef SIGHUP |
| 278 | {SIGHUP, "HUP", TRUE}, |
| 279 | #endif |
| 280 | #ifdef SIGQUIT |
| 281 | {SIGQUIT, "QUIT", TRUE}, |
| 282 | #endif |
| 283 | #ifdef SIGILL |
| 284 | {SIGILL, "ILL", TRUE}, |
| 285 | #endif |
| 286 | #ifdef SIGTRAP |
| 287 | {SIGTRAP, "TRAP", TRUE}, |
| 288 | #endif |
| 289 | #ifdef SIGABRT |
| 290 | {SIGABRT, "ABRT", TRUE}, |
| 291 | #endif |
| 292 | #ifdef SIGEMT |
| 293 | {SIGEMT, "EMT", TRUE}, |
| 294 | #endif |
| 295 | #ifdef SIGFPE |
| 296 | {SIGFPE, "FPE", TRUE}, |
| 297 | #endif |
| 298 | #ifdef SIGBUS |
| 299 | {SIGBUS, "BUS", TRUE}, |
| 300 | #endif |
Bram Moolenaar | 7567646 | 2013-01-30 14:55:42 +0100 | [diff] [blame] | 301 | #if defined(SIGSEGV) && !defined(FEAT_MZSCHEME) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 302 | // MzScheme uses SEGV in its garbage collector |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 303 | {SIGSEGV, "SEGV", TRUE}, |
| 304 | #endif |
| 305 | #ifdef SIGSYS |
| 306 | {SIGSYS, "SYS", TRUE}, |
| 307 | #endif |
| 308 | #ifdef SIGALRM |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 309 | {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 310 | #endif |
| 311 | #ifdef SIGTERM |
| 312 | {SIGTERM, "TERM", TRUE}, |
| 313 | #endif |
Bram Moolenaar | b292a2a | 2011-02-09 18:47:40 +0100 | [diff] [blame] | 314 | #if defined(SIGVTALRM) && !defined(FEAT_RUBY) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 315 | {SIGVTALRM, "VTALRM", TRUE}, |
| 316 | #endif |
Bram Moolenaar | 02f07e0 | 2008-03-12 12:17:28 +0000 | [diff] [blame] | 317 | #if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 318 | // MzScheme uses SIGPROF for its own needs; On Linux with profiling |
| 319 | // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 320 | {SIGPROF, "PROF", TRUE}, |
| 321 | #endif |
| 322 | #ifdef SIGXCPU |
| 323 | {SIGXCPU, "XCPU", TRUE}, |
| 324 | #endif |
| 325 | #ifdef SIGXFSZ |
| 326 | {SIGXFSZ, "XFSZ", TRUE}, |
| 327 | #endif |
| 328 | #ifdef SIGUSR1 |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 329 | {SIGUSR1, "USR1", FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 330 | #endif |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 331 | #if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 332 | // Used for sysmouse handling |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 333 | {SIGUSR2, "USR2", TRUE}, |
| 334 | #endif |
| 335 | #ifdef SIGINT |
| 336 | {SIGINT, "INT", FALSE}, |
| 337 | #endif |
| 338 | #ifdef SIGWINCH |
| 339 | {SIGWINCH, "WINCH", FALSE}, |
| 340 | #endif |
| 341 | #ifdef SIGTSTP |
| 342 | {SIGTSTP, "TSTP", FALSE}, |
| 343 | #endif |
| 344 | #ifdef SIGPIPE |
| 345 | {SIGPIPE, "PIPE", FALSE}, |
| 346 | #endif |
| 347 | {-1, "Unknown!", FALSE} |
| 348 | }; |
| 349 | |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 350 | sighandler_T |
| 351 | mch_signal(int sig, sighandler_T func) |
| 352 | { |
| 353 | #if defined(HAVE_SIGACTION) && defined(HAVE_SIGPROCMASK) |
| 354 | // Modern implementation: use sigaction(). |
| 355 | struct sigaction sa, old; |
| 356 | sigset_t curset; |
| 357 | int blocked; |
| 358 | |
| 359 | if (sigprocmask(SIG_BLOCK, NULL, &curset) == -1) |
| 360 | return SIG_ERR; |
| 361 | |
| 362 | blocked = sigismember(&curset, sig); |
| 363 | |
| 364 | if (func == SIG_HOLD) |
| 365 | { |
| 366 | if (blocked) |
| 367 | return SIG_HOLD; |
| 368 | |
| 369 | sigemptyset(&curset); |
| 370 | sigaddset(&curset, sig); |
| 371 | |
| 372 | if (sigaction(sig, NULL, &old) == -1 |
| 373 | || sigprocmask(SIG_BLOCK, &curset, NULL) == -1) |
| 374 | return SIG_ERR; |
| 375 | return old.sa_handler; |
| 376 | } |
| 377 | |
| 378 | if (blocked) |
| 379 | { |
| 380 | sigemptyset(&curset); |
| 381 | sigaddset(&curset, sig); |
| 382 | |
| 383 | if (sigprocmask(SIG_UNBLOCK, &curset, NULL) == -1) |
| 384 | return SIG_ERR; |
| 385 | } |
| 386 | |
| 387 | sa.sa_handler = func; |
| 388 | sigemptyset(&sa.sa_mask); |
| 389 | # ifdef SA_RESTART |
| 390 | sa.sa_flags = SA_RESTART; |
| 391 | # else |
| 392 | sa.sa_flags = 0; |
| 393 | # endif |
| 394 | if (sigaction(sig, &sa, &old) == -1) |
| 395 | return SIG_ERR; |
| 396 | return blocked ? SIG_HOLD: old.sa_handler; |
| 397 | #elif defined(HAVE_SIGSET) |
| 398 | // Using sigset() is preferred above signal(). |
| 399 | return sigset(sig, func); |
| 400 | #else |
| 401 | // Oldest and most compatible solution. |
| 402 | return signal(sig, func); |
| 403 | #endif |
| 404 | } |
| 405 | |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 406 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 407 | mch_chdir(char *path) |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 408 | { |
| 409 | if (p_verbose >= 5) |
| 410 | { |
| 411 | verbose_enter(); |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 412 | smsg("chdir(%s)", path); |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 413 | verbose_leave(); |
| 414 | } |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 415 | #ifdef VMS |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 416 | return chdir(vms_fixfilename(path)); |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 417 | #else |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 418 | return chdir(path); |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 419 | #endif |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 422 | // Why is NeXT excluded here (and not in os_unixx.h)? |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 423 | #if defined(ECHOE) && defined(ICANON) \ |
| 424 | && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \ |
| 425 | && !defined(__NeXT__) |
Bram Moolenaar | 4f44b88 | 2017-08-13 20:06:18 +0200 | [diff] [blame] | 426 | # define NEW_TTY_SYSTEM |
| 427 | #endif |
| 428 | |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 429 | /* |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 430 | * Write s[len] to the screen (stdout). |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 431 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 432 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 433 | mch_write(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 434 | { |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 435 | vim_ignored = (int)write(1, (char *)s, len); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 436 | if (p_wd) // Unix is too fast, slow down a bit more |
Bram Moolenaar | 8fdd721 | 2016-03-26 19:41:48 +0100 | [diff] [blame] | 437 | RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 441 | * Function passed to inchar_loop() to handle window resizing. |
| 442 | * If "check_only" is TRUE: Return whether there was a resize. |
| 443 | * If "check_only" is FALSE: Deal with the window resized. |
| 444 | */ |
| 445 | static int |
| 446 | resize_func(int check_only) |
| 447 | { |
| 448 | if (check_only) |
| 449 | return do_resize; |
| 450 | while (do_resize) |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 451 | { |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 452 | #ifdef FEAT_EVAL |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 453 | ch_log(NULL, "calling handle_resize() in resize_func()"); |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 454 | #endif |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 455 | handle_resize(); |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 456 | } |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 457 | return FALSE; |
| 458 | } |
| 459 | |
| 460 | /* |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 461 | * mch_inchar(): low level input function. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 462 | * Get a characters from the keyboard. |
| 463 | * Return the number of characters that are available. |
| 464 | * If wtime == 0 do not wait for characters. |
| 465 | * If wtime == n wait a short time for characters. |
| 466 | * If wtime == -1 wait forever for characters. |
| 467 | */ |
| 468 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 469 | mch_inchar( |
| 470 | char_u *buf, |
| 471 | int maxlen, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 472 | long wtime, // don't use "time", MIPS cannot handle it |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 473 | int tb_change_cnt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 474 | { |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 475 | return inchar_loop(buf, maxlen, wtime, tb_change_cnt, |
| 476 | WaitForChar, resize_func); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 480 | handle_resize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 481 | { |
| 482 | do_resize = FALSE; |
| 483 | shell_resized(); |
| 484 | } |
| 485 | |
| 486 | /* |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 487 | * Return non-zero if a character is available. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 488 | */ |
| 489 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 490 | mch_char_avail(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 491 | { |
Bram Moolenaar | e9c21ae | 2017-08-03 20:44:48 +0200 | [diff] [blame] | 492 | return WaitForChar(0L, NULL, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Bram Moolenaar | e9c21ae | 2017-08-03 20:44:48 +0200 | [diff] [blame] | 495 | #if defined(FEAT_TERMINAL) || defined(PROTO) |
| 496 | /* |
| 497 | * Check for any pending input or messages. |
| 498 | */ |
| 499 | int |
| 500 | mch_check_messages(void) |
| 501 | { |
| 502 | return WaitForChar(0L, NULL, TRUE); |
| 503 | } |
| 504 | #endif |
| 505 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 506 | #if defined(HAVE_TOTAL_MEM) || defined(PROTO) |
| 507 | # ifdef HAVE_SYS_RESOURCE_H |
Bram Moolenaar | 8f0b2d4 | 2009-05-16 14:41:10 +0000 | [diff] [blame] | 508 | # include <sys/resource.h> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 509 | # endif |
| 510 | # if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL) |
| 511 | # include <sys/sysctl.h> |
| 512 | # endif |
| 513 | # if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO) |
| 514 | # include <sys/sysinfo.h> |
| 515 | # endif |
Bram Moolenaar | 362dc33 | 2018-03-05 21:59:37 +0100 | [diff] [blame] | 516 | # ifdef MACOS_X |
Bram Moolenaar | 988615f | 2018-02-27 17:58:20 +0100 | [diff] [blame] | 517 | # include <mach/mach_host.h> |
| 518 | # include <mach/mach_port.h> |
Bram Moolenaar | 988615f | 2018-02-27 17:58:20 +0100 | [diff] [blame] | 519 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 520 | |
| 521 | /* |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 522 | * Return total amount of memory available in Kbyte. |
| 523 | * Doesn't change when memory has been allocated. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 524 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 525 | long_u |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 526 | mch_total_mem(int special UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 527 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 528 | long_u mem = 0; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 529 | long_u shiftright = 10; // how much to shift "mem" right for Kbyte |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 530 | |
Bram Moolenaar | 362dc33 | 2018-03-05 21:59:37 +0100 | [diff] [blame] | 531 | # ifdef MACOS_X |
Bram Moolenaar | 988615f | 2018-02-27 17:58:20 +0100 | [diff] [blame] | 532 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 533 | // Mac (Darwin) way of getting the amount of RAM available |
Bram Moolenaar | 988615f | 2018-02-27 17:58:20 +0100 | [diff] [blame] | 534 | mach_port_t host = mach_host_self(); |
| 535 | kern_return_t kret; |
| 536 | # ifdef HOST_VM_INFO64 |
| 537 | struct vm_statistics64 vm_stat; |
| 538 | natural_t count = HOST_VM_INFO64_COUNT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 539 | |
Bram Moolenaar | 988615f | 2018-02-27 17:58:20 +0100 | [diff] [blame] | 540 | kret = host_statistics64(host, HOST_VM_INFO64, |
| 541 | (host_info64_t)&vm_stat, &count); |
| 542 | # else |
| 543 | struct vm_statistics vm_stat; |
| 544 | natural_t count = HOST_VM_INFO_COUNT; |
| 545 | |
| 546 | kret = host_statistics(host, HOST_VM_INFO, |
| 547 | (host_info_t)&vm_stat, &count); |
| 548 | # endif |
| 549 | if (kret == KERN_SUCCESS) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 550 | // get the amount of user memory by summing each usage |
Bram Moolenaar | 988615f | 2018-02-27 17:58:20 +0100 | [diff] [blame] | 551 | mem = (long_u)(vm_stat.free_count + vm_stat.active_count |
| 552 | + vm_stat.inactive_count |
| 553 | # ifdef MAC_OS_X_VERSION_10_9 |
| 554 | + vm_stat.compressor_page_count |
| 555 | # endif |
Bram Moolenaar | 62b7f6a | 2018-03-22 21:44:07 +0100 | [diff] [blame] | 556 | ) * sysconf(_SC_PAGESIZE); |
Bram Moolenaar | 988615f | 2018-02-27 17:58:20 +0100 | [diff] [blame] | 557 | mach_port_deallocate(mach_task_self(), host); |
| 558 | } |
| 559 | # endif |
| 560 | |
| 561 | # ifdef HAVE_SYSCTL |
| 562 | if (mem == 0) |
| 563 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 564 | // BSD way of getting the amount of RAM available. |
Bram Moolenaar | 988615f | 2018-02-27 17:58:20 +0100 | [diff] [blame] | 565 | int mib[2]; |
| 566 | size_t len = sizeof(long_u); |
| 567 | # ifdef HW_USERMEM64 |
| 568 | long_u physmem; |
| 569 | # else |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 570 | // sysctl() may return 32 bit or 64 bit, accept both |
Bram Moolenaar | 988615f | 2018-02-27 17:58:20 +0100 | [diff] [blame] | 571 | union { |
| 572 | int_u u32; |
| 573 | long_u u64; |
| 574 | } physmem; |
| 575 | # endif |
| 576 | |
| 577 | mib[0] = CTL_HW; |
| 578 | # ifdef HW_USERMEM64 |
| 579 | mib[1] = HW_USERMEM64; |
| 580 | # else |
| 581 | mib[1] = HW_USERMEM; |
| 582 | # endif |
| 583 | if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0) |
| 584 | { |
| 585 | # ifdef HW_USERMEM64 |
| 586 | mem = (long_u)physmem; |
| 587 | # else |
| 588 | if (len == sizeof(physmem.u64)) |
| 589 | mem = (long_u)physmem.u64; |
| 590 | else |
| 591 | mem = (long_u)physmem.u32; |
| 592 | # endif |
| 593 | } |
| 594 | } |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 595 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 596 | |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 597 | # if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 598 | if (mem == 0) |
| 599 | { |
| 600 | struct sysinfo sinfo; |
| 601 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 602 | // Linux way of getting amount of RAM available |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 603 | if (sysinfo(&sinfo) == 0) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 604 | { |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 605 | # ifdef HAVE_SYSINFO_MEM_UNIT |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 606 | // avoid overflow as much as possible |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 607 | while (shiftright > 0 && (sinfo.mem_unit & 1) == 0) |
| 608 | { |
| 609 | sinfo.mem_unit = sinfo.mem_unit >> 1; |
| 610 | --shiftright; |
| 611 | } |
| 612 | mem = sinfo.totalram * sinfo.mem_unit; |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 613 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 614 | mem = sinfo.totalram; |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 615 | # endif |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 616 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 617 | } |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 618 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 619 | |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 620 | # ifdef HAVE_SYSCONF |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 621 | if (mem == 0) |
| 622 | { |
| 623 | long pagesize, pagecount; |
| 624 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 625 | // Solaris way of getting amount of RAM available |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 626 | pagesize = sysconf(_SC_PAGESIZE); |
| 627 | pagecount = sysconf(_SC_PHYS_PAGES); |
| 628 | if (pagesize > 0 && pagecount > 0) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 629 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 630 | // avoid overflow as much as possible |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 631 | while (shiftright > 0 && (pagesize & 1) == 0) |
| 632 | { |
Bram Moolenaar | 3d27a45 | 2007-05-10 17:44:18 +0000 | [diff] [blame] | 633 | pagesize = (long_u)pagesize >> 1; |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 634 | --shiftright; |
| 635 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 636 | mem = (long_u)pagesize * pagecount; |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 637 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 638 | } |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 639 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 640 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 641 | // Return the minimum of the physical memory and the user limit, because |
| 642 | // using more than the user limit may cause Vim to be terminated. |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 643 | # if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 644 | { |
| 645 | struct rlimit rlp; |
| 646 | |
| 647 | if (getrlimit(RLIMIT_DATA, &rlp) == 0 |
| 648 | && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1)) |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 649 | # ifdef RLIM_INFINITY |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 650 | && rlp.rlim_cur != RLIM_INFINITY |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 651 | # endif |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 652 | && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 653 | ) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 654 | { |
| 655 | mem = (long_u)rlp.rlim_cur; |
| 656 | shiftright = 10; |
| 657 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 658 | } |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 659 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 660 | |
| 661 | if (mem > 0) |
Bram Moolenaar | 914572a | 2007-05-01 11:37:47 +0000 | [diff] [blame] | 662 | return mem >> shiftright; |
| 663 | return (long_u)0x1fffff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 664 | } |
| 665 | #endif |
| 666 | |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 667 | /* |
| 668 | * "flags": MCH_DELAY_IGNOREINPUT - don't read input |
| 669 | * MCH_DELAY_SETTMODE - use settmode() even for short delays |
| 670 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 671 | void |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 672 | mch_delay(long msec, int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 673 | { |
Bram Moolenaar | 26e8644 | 2020-05-17 14:06:16 +0200 | [diff] [blame] | 674 | tmode_T old_tmode; |
Bram Moolenaar | 80361a5 | 2020-10-05 21:39:25 +0200 | [diff] [blame] | 675 | int call_settmode; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 676 | #ifdef FEAT_MZSCHEME |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 677 | long total = msec; // remember original value |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 678 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 679 | |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 680 | if (flags & MCH_DELAY_IGNOREINPUT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 681 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 682 | // Go to cooked mode without echo, to allow SIGINT interrupting us |
| 683 | // here. But we don't want QUIT to kill us (CTRL-\ used in a |
| 684 | // shell may produce SIGQUIT). |
Bram Moolenaar | 26e8644 | 2020-05-17 14:06:16 +0200 | [diff] [blame] | 685 | // Only do this if sleeping for more than half a second. |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 686 | in_mch_delay = TRUE; |
Bram Moolenaar | 80361a5 | 2020-10-05 21:39:25 +0200 | [diff] [blame] | 687 | call_settmode = mch_cur_tmode == TMODE_RAW |
| 688 | && (msec > 500 || (flags & MCH_DELAY_SETTMODE)); |
| 689 | if (call_settmode) |
| 690 | { |
| 691 | old_tmode = mch_cur_tmode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 692 | settmode(TMODE_SLEEP); |
Bram Moolenaar | 80361a5 | 2020-10-05 21:39:25 +0200 | [diff] [blame] | 693 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 694 | |
| 695 | /* |
| 696 | * Everybody sleeps in a different way... |
| 697 | * Prefer nanosleep(), some versions of usleep() can only sleep up to |
| 698 | * one second. |
| 699 | */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 700 | #ifdef FEAT_MZSCHEME |
| 701 | do |
| 702 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 703 | // if total is large enough, wait by portions in p_mzq |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 704 | if (total > p_mzq) |
| 705 | msec = p_mzq; |
| 706 | else |
| 707 | msec = total; |
| 708 | total -= msec; |
| 709 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 710 | #ifdef HAVE_NANOSLEEP |
| 711 | { |
| 712 | struct timespec ts; |
| 713 | |
| 714 | ts.tv_sec = msec / 1000; |
| 715 | ts.tv_nsec = (msec % 1000) * 1000000; |
| 716 | (void)nanosleep(&ts, NULL); |
| 717 | } |
| 718 | #else |
| 719 | # ifdef HAVE_USLEEP |
| 720 | while (msec >= 1000) |
| 721 | { |
| 722 | usleep((unsigned int)(999 * 1000)); |
| 723 | msec -= 999; |
| 724 | } |
| 725 | usleep((unsigned int)(msec * 1000)); |
| 726 | # else |
| 727 | # ifndef HAVE_SELECT |
| 728 | poll(NULL, 0, (int)msec); |
| 729 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 730 | { |
| 731 | struct timeval tv; |
| 732 | |
| 733 | tv.tv_sec = msec / 1000; |
| 734 | tv.tv_usec = (msec % 1000) * 1000; |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 735 | // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get |
| 736 | // a patch from Sun to fix this. Reported by Gunnar Pedersen. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 737 | select(0, NULL, NULL, NULL, &tv); |
| 738 | } |
Michael Jarvis | be9624e | 2023-04-19 20:28:48 +0100 | [diff] [blame] | 739 | # endif |
| 740 | # endif |
| 741 | #endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 742 | #ifdef FEAT_MZSCHEME |
| 743 | } |
| 744 | while (total > 0); |
| 745 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 746 | |
Bram Moolenaar | 80361a5 | 2020-10-05 21:39:25 +0200 | [diff] [blame] | 747 | if (call_settmode) |
Bram Moolenaar | 26e8644 | 2020-05-17 14:06:16 +0200 | [diff] [blame] | 748 | settmode(old_tmode); |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 749 | in_mch_delay = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 750 | } |
| 751 | else |
Bram Moolenaar | e9c21ae | 2017-08-03 20:44:48 +0200 | [diff] [blame] | 752 | WaitForChar(msec, NULL, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 755 | #if defined(HAVE_STACK_LIMIT) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 756 | || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK)) |
| 757 | # define HAVE_CHECK_STACK_GROWTH |
| 758 | /* |
| 759 | * Support for checking for an almost-out-of-stack-space situation. |
| 760 | */ |
| 761 | |
| 762 | /* |
| 763 | * Return a pointer to an item on the stack. Used to find out if the stack |
| 764 | * grows up or down. |
| 765 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 766 | static int stack_grows_downwards; |
| 767 | |
| 768 | /* |
| 769 | * Find out if the stack grows upwards or downwards. |
| 770 | * "p" points to a variable on the stack of the caller. |
| 771 | */ |
| 772 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 773 | check_stack_growth(char *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 774 | { |
| 775 | int i; |
| 776 | |
| 777 | stack_grows_downwards = (p > (char *)&i); |
| 778 | } |
| 779 | #endif |
| 780 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 781 | #if defined(HAVE_STACK_LIMIT) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 782 | static char *stack_limit = NULL; |
| 783 | |
| 784 | #if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H) |
| 785 | # include <pthread.h> |
| 786 | # include <pthread_np.h> |
| 787 | #endif |
| 788 | |
| 789 | /* |
| 790 | * Find out until how var the stack can grow without getting into trouble. |
| 791 | * Called when starting up and when switching to the signal stack in |
| 792 | * deathtrap(). |
| 793 | */ |
| 794 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 795 | get_stack_limit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 796 | { |
| 797 | struct rlimit rlp; |
| 798 | int i; |
| 799 | long lim; |
| 800 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 801 | // Set the stack limit to 15/16 of the allowable size. Skip this when the |
| 802 | // limit doesn't fit in a long (rlim_cur might be "long long"). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 803 | if (getrlimit(RLIMIT_STACK, &rlp) == 0 |
| 804 | && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1)) |
| 805 | # ifdef RLIM_INFINITY |
| 806 | && rlp.rlim_cur != RLIM_INFINITY |
| 807 | # endif |
| 808 | ) |
| 809 | { |
| 810 | lim = (long)rlp.rlim_cur; |
| 811 | #if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H) |
| 812 | { |
| 813 | pthread_attr_t attr; |
| 814 | size_t size; |
| 815 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 816 | // On FreeBSD the initial thread always has a fixed stack size, no |
| 817 | // matter what the limits are set to. Normally it's 1 Mbyte. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 818 | pthread_attr_init(&attr); |
| 819 | if (pthread_attr_get_np(pthread_self(), &attr) == 0) |
| 820 | { |
| 821 | pthread_attr_getstacksize(&attr, &size); |
| 822 | if (lim > (long)size) |
| 823 | lim = (long)size; |
| 824 | } |
| 825 | pthread_attr_destroy(&attr); |
| 826 | } |
| 827 | #endif |
| 828 | if (stack_grows_downwards) |
| 829 | { |
| 830 | stack_limit = (char *)((long)&i - (lim / 16L * 15L)); |
| 831 | if (stack_limit >= (char *)&i) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 832 | // overflow, set to 1/16 of current stack position |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 833 | stack_limit = (char *)((long)&i / 16L); |
| 834 | } |
| 835 | else |
| 836 | { |
| 837 | stack_limit = (char *)((long)&i + (lim / 16L * 15L)); |
| 838 | if (stack_limit <= (char *)&i) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 839 | stack_limit = NULL; // overflow |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Return FAIL when running out of stack space. |
| 846 | * "p" must point to any variable local to the caller that's on the stack. |
| 847 | */ |
| 848 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 849 | mch_stackcheck(char *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 850 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 851 | if (stack_limit == NULL) |
| 852 | return OK; |
| 853 | |
| 854 | if (stack_grows_downwards) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 855 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 856 | if (p < stack_limit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 857 | return FAIL; |
| 858 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 859 | else if (p > stack_limit) |
| 860 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 861 | return OK; |
| 862 | } |
| 863 | #endif |
| 864 | |
| 865 | #if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
| 866 | /* |
| 867 | * Support for using the signal stack. |
| 868 | * This helps when we run out of stack space, which causes a SIGSEGV. The |
| 869 | * signal handler then must run on another stack, since the normal stack is |
| 870 | * completely full. |
| 871 | */ |
| 872 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 873 | # ifdef HAVE_SIGALTSTACK |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 874 | static stack_t sigstk; // for sigaltstack() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 875 | # else |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 876 | static struct sigstack sigstk; // for sigstack() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 877 | # endif |
| 878 | |
Zdenek Dohnal | ba9c23e | 2021-08-11 14:20:05 +0200 | [diff] [blame] | 879 | /* |
| 880 | * Get a size of signal stack. |
| 881 | * Preference (if available): sysconf > SIGSTKSZ > guessed size |
| 882 | */ |
Michael Jarvis | be9624e | 2023-04-19 20:28:48 +0100 | [diff] [blame] | 883 | static long int get_signal_stack_size(void) |
Zdenek Dohnal | ba9c23e | 2021-08-11 14:20:05 +0200 | [diff] [blame] | 884 | { |
| 885 | # ifdef HAVE_SYSCONF_SIGSTKSZ |
| 886 | long int size = -1; |
| 887 | |
| 888 | // return size only if sysconf doesn't return an error |
| 889 | if ((size = sysconf(_SC_SIGSTKSZ)) > -1) |
| 890 | return size; |
| 891 | # endif |
| 892 | |
| 893 | # ifdef SIGSTKSZ |
| 894 | // if sysconf() isn't available or gives error, return SIGSTKSZ |
| 895 | // if defined |
| 896 | return SIGSTKSZ; |
| 897 | # endif |
| 898 | |
| 899 | // otherwise guess the size |
| 900 | return 8000; |
| 901 | } |
| 902 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 903 | static char *signal_stack; |
| 904 | |
| 905 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 906 | init_signal_stack(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 907 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 908 | if (signal_stack == NULL) |
| 909 | return; |
| 910 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 911 | # ifdef HAVE_SIGALTSTACK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 912 | # ifdef HAVE_SS_BASE |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 913 | sigstk.ss_base = signal_stack; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 914 | # else |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 915 | sigstk.ss_sp = signal_stack; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 916 | # endif |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 917 | sigstk.ss_size = get_signal_stack_size(); |
| 918 | sigstk.ss_flags = 0; |
| 919 | (void)sigaltstack(&sigstk, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 920 | # else |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 921 | sigstk.ss_sp = signal_stack; |
| 922 | if (stack_grows_downwards) |
| 923 | sigstk.ss_sp += get_signal_stack_size() - 1; |
| 924 | sigstk.ss_onstack = 0; |
| 925 | (void)sigstack(&sigstk, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 926 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 927 | } |
| 928 | #endif |
| 929 | |
| 930 | /* |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 931 | * We need correct prototypes for a signal function, otherwise mean compilers |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 932 | * will barf when the second argument to signal() is ``wrong''. |
| 933 | * Let me try it with a few tricky defines from my own osdef.h (jw). |
| 934 | */ |
| 935 | #if defined(SIGWINCH) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 936 | static void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 937 | sig_winch SIGDEFARG(sigarg) |
| 938 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 939 | // this is not required on all systems, but it doesn't hurt anybody |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 940 | mch_signal(SIGWINCH, sig_winch); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | do_resize = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 942 | } |
| 943 | #endif |
| 944 | |
dbivolaru | ab16ad3 | 2021-12-29 19:41:47 +0000 | [diff] [blame] | 945 | #if defined(SIGTSTP) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 946 | static void |
dbivolaru | ab16ad3 | 2021-12-29 19:41:47 +0000 | [diff] [blame] | 947 | sig_tstp SIGDEFARG(sigarg) |
| 948 | { |
| 949 | // Second time we get called we actually need to suspend |
| 950 | if (in_mch_suspend) |
| 951 | { |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 952 | mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL); |
dbivolaru | ab16ad3 | 2021-12-29 19:41:47 +0000 | [diff] [blame] | 953 | raise(sigarg); |
| 954 | } |
dbivolaru | 79a6e25 | 2022-01-23 16:41:14 +0000 | [diff] [blame] | 955 | else |
| 956 | got_tstp = TRUE; |
dbivolaru | ab16ad3 | 2021-12-29 19:41:47 +0000 | [diff] [blame] | 957 | |
ichizok | 5f823d1 | 2022-03-13 17:27:38 +0000 | [diff] [blame] | 958 | #if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__) |
| 959 | // This is not required on all systems. On some systems (at least Android, |
| 960 | // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z. |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 961 | mch_signal(SIGTSTP, sig_tstp); |
xtkoba | cbef12e | 2022-02-27 12:31:52 +0000 | [diff] [blame] | 962 | #endif |
dbivolaru | ab16ad3 | 2021-12-29 19:41:47 +0000 | [diff] [blame] | 963 | } |
| 964 | #endif |
| 965 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 966 | #if defined(SIGINT) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 967 | static void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 968 | catch_sigint SIGDEFARG(sigarg) |
| 969 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 970 | // this is not required on all systems, but it doesn't hurt anybody |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 971 | mch_signal(SIGINT, catch_sigint); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 972 | got_int = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 973 | } |
| 974 | #endif |
| 975 | |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 976 | #if defined(SIGUSR1) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 977 | static void |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 978 | catch_sigusr1 SIGDEFARG(sigarg) |
| 979 | { |
| 980 | // this is not required on all systems, but it doesn't hurt anybody |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 981 | mch_signal(SIGUSR1, catch_sigusr1); |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 982 | got_sigusr1 = TRUE; |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 983 | } |
| 984 | #endif |
| 985 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 986 | #if defined(SIGPWR) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 987 | static void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 988 | catch_sigpwr SIGDEFARG(sigarg) |
| 989 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 990 | // this is not required on all systems, but it doesn't hurt anybody |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 991 | mch_signal(SIGPWR, catch_sigpwr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 992 | /* |
| 993 | * I'm not sure we get the SIGPWR signal when the system is really going |
| 994 | * down or when the batteries are almost empty. Just preserve the swap |
| 995 | * files and don't exit, that can't do any harm. |
| 996 | */ |
| 997 | ml_sync_all(FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 998 | } |
| 999 | #endif |
| 1000 | |
| 1001 | #ifdef SET_SIG_ALARM |
| 1002 | /* |
| 1003 | * signal function for alarm(). |
| 1004 | */ |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 1005 | static void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1006 | sig_alarm SIGDEFARG(sigarg) |
| 1007 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1008 | // doesn't do anything, just to break a system call |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1009 | sig_alarm_called = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1010 | } |
| 1011 | #endif |
| 1012 | |
Bram Moolenaar | edce742 | 2019-01-20 18:39:30 +0100 | [diff] [blame] | 1013 | #if (defined(HAVE_SETJMP_H) \ |
| 1014 | && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \ |
| 1015 | || defined(FEAT_LIBCALL))) \ |
| 1016 | || defined(PROTO) |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 1017 | # define USING_SETJMP 1 |
Bram Moolenaar | edce742 | 2019-01-20 18:39:30 +0100 | [diff] [blame] | 1018 | |
Bram Moolenaar | b7a7e03 | 2018-12-28 17:01:59 +0100 | [diff] [blame] | 1019 | // argument to SETJMP() |
| 1020 | static JMP_BUF lc_jump_env; |
| 1021 | |
| 1022 | # ifdef SIGHASARG |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 1023 | // Caught signal number, 0 when no signal was caught; used for mch_libcall(). |
Bram Moolenaar | b7a7e03 | 2018-12-28 17:01:59 +0100 | [diff] [blame] | 1024 | // Volatile because it is used in signal handlers. |
| 1025 | static volatile sig_atomic_t lc_signal; |
| 1026 | # endif |
| 1027 | |
| 1028 | // TRUE when lc_jump_env is valid. |
| 1029 | // Volatile because it is used in signal handler deathtrap(). |
zeertzjq | 9b7d2a9 | 2022-08-26 10:33:54 +0100 | [diff] [blame] | 1030 | static volatile sig_atomic_t lc_active = FALSE; |
Bram Moolenaar | b7a7e03 | 2018-12-28 17:01:59 +0100 | [diff] [blame] | 1031 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1032 | /* |
| 1033 | * A simplistic version of setjmp() that only allows one level of using. |
Bram Moolenaar | b7a7e03 | 2018-12-28 17:01:59 +0100 | [diff] [blame] | 1034 | * Used to protect areas where we could crash. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1035 | * Don't call twice before calling mch_endjmp()!. |
Bram Moolenaar | b7a7e03 | 2018-12-28 17:01:59 +0100 | [diff] [blame] | 1036 | * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1037 | * Usage: |
| 1038 | * mch_startjmp(); |
| 1039 | * if (SETJMP(lc_jump_env) != 0) |
| 1040 | * { |
| 1041 | * mch_didjmp(); |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1042 | * emsg("crash!"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1043 | * } |
| 1044 | * else |
| 1045 | * { |
| 1046 | * do_the_work; |
| 1047 | * mch_endjmp(); |
| 1048 | * } |
| 1049 | * Note: Can't move SETJMP() here, because a function calling setjmp() must |
| 1050 | * not return before the saved environment is used. |
| 1051 | * Returns OK for normal return, FAIL when the protected code caused a |
| 1052 | * problem and LONGJMP() was used. |
| 1053 | */ |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 1054 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1055 | mch_startjmp(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | { |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 1057 | # ifdef SIGHASARG |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1058 | lc_signal = 0; |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 1059 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1060 | lc_active = TRUE; |
| 1061 | } |
| 1062 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 1063 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1064 | mch_endjmp(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1065 | { |
| 1066 | lc_active = FALSE; |
| 1067 | } |
| 1068 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 1069 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1070 | mch_didjmp(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1071 | { |
| 1072 | # if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
Bram Moolenaar | b7a7e03 | 2018-12-28 17:01:59 +0100 | [diff] [blame] | 1073 | // On FreeBSD the signal stack has to be reset after using siglongjmp(), |
| 1074 | // otherwise catching the signal only works once. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1075 | init_signal_stack(); |
| 1076 | # endif |
| 1077 | } |
| 1078 | #endif |
| 1079 | |
| 1080 | /* |
| 1081 | * This function handles deadly signals. |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1082 | * It tries to preserve any swap files and exit properly. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1083 | * (partly from Elvis). |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1084 | * NOTE: Avoid unsafe functions, such as allocating memory, they can result in |
| 1085 | * a deadlock. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1086 | */ |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 1087 | static void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1088 | deathtrap SIGDEFARG(sigarg) |
| 1089 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1090 | static int entered = 0; // count the number of times we got here. |
| 1091 | // Note: when memory has been corrupted |
| 1092 | // this may get an arbitrary value! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1093 | #ifdef SIGHASARG |
| 1094 | int i; |
| 1095 | #endif |
| 1096 | |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 1097 | #if defined(USING_SETJMP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | /* |
| 1099 | * Catch a crash in protected code. |
| 1100 | * Restores the environment saved in lc_jump_env, which looks like |
| 1101 | * SETJMP() returns 1. |
| 1102 | */ |
| 1103 | if (lc_active) |
| 1104 | { |
| 1105 | # if defined(SIGHASARG) |
| 1106 | lc_signal = sigarg; |
| 1107 | # endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1108 | lc_active = FALSE; // don't jump again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1109 | LONGJMP(lc_jump_env, 1); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1110 | // NOTREACHED |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1111 | } |
| 1112 | #endif |
| 1113 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1114 | #ifdef SIGHASARG |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 1115 | # ifdef SIGQUIT |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1116 | // While in mch_delay() we go to cooked mode to allow a CTRL-C to |
| 1117 | // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when |
| 1118 | // pressing CTRL-\, but we don't want Vim to exit then. |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 1119 | if (in_mch_delay && sigarg == SIGQUIT) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 1120 | return; |
Bram Moolenaar | ae0f2ca | 2008-02-10 21:25:55 +0000 | [diff] [blame] | 1121 | # endif |
| 1122 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1123 | // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return |
| 1124 | // here. This avoids that a non-reentrant function is interrupted, e.g., |
| 1125 | // free(). Calling free() again may then cause a crash. |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1126 | if (entered == 0 |
| 1127 | && (0 |
| 1128 | # ifdef SIGHUP |
| 1129 | || sigarg == SIGHUP |
| 1130 | # endif |
| 1131 | # ifdef SIGQUIT |
| 1132 | || sigarg == SIGQUIT |
| 1133 | # endif |
| 1134 | # ifdef SIGTERM |
| 1135 | || sigarg == SIGTERM |
| 1136 | # endif |
| 1137 | # ifdef SIGPWR |
| 1138 | || sigarg == SIGPWR |
| 1139 | # endif |
| 1140 | # ifdef SIGUSR1 |
| 1141 | || sigarg == SIGUSR1 |
| 1142 | # endif |
| 1143 | # ifdef SIGUSR2 |
| 1144 | || sigarg == SIGUSR2 |
| 1145 | # endif |
| 1146 | ) |
Bram Moolenaar | 1f28b07 | 2005-07-12 22:42:41 +0000 | [diff] [blame] | 1147 | && !vim_handle_signal(sigarg)) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 1148 | return; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1149 | #endif |
| 1150 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1151 | // Remember how often we have been called. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1152 | ++entered; |
| 1153 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1154 | // Executing autocommands is likely to use more stack space than we have |
| 1155 | // available in the signal stack. |
Bram Moolenaar | e429e70 | 2016-06-10 19:49:14 +0200 | [diff] [blame] | 1156 | block_autocmds(); |
Bram Moolenaar | e429e70 | 2016-06-10 19:49:14 +0200 | [diff] [blame] | 1157 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1159 | // Set the v:dying variable. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1160 | set_vim_var_nr(VV_DYING, (long)entered); |
| 1161 | #endif |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1162 | v_dying = entered; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1163 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1164 | #ifdef HAVE_STACK_LIMIT |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1165 | // Since we are now using the signal stack, need to reset the stack |
| 1166 | // limit. Otherwise using a regexp will fail. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1167 | get_stack_limit(); |
| 1168 | #endif |
| 1169 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1170 | #if 0 |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1171 | // This is for opening gdb the moment Vim crashes. |
| 1172 | // You need to manually adjust the file name and Vim executable name. |
| 1173 | // Suggested by SungHyun Nam. |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1174 | { |
| 1175 | # define VI_GDB_FILE "/tmp/vimgdb" |
| 1176 | # define VIM_NAME "/usr/bin/vim" |
| 1177 | FILE *fp = fopen(VI_GDB_FILE, "w"); |
| 1178 | if (fp) |
| 1179 | { |
| 1180 | fprintf(fp, |
| 1181 | "file %s\n" |
| 1182 | "attach %d\n" |
| 1183 | "set height 1000\n" |
| 1184 | "bt full\n" |
| 1185 | , VIM_NAME, getpid()); |
| 1186 | fclose(fp); |
| 1187 | system("xterm -e gdb -x "VI_GDB_FILE); |
| 1188 | unlink(VI_GDB_FILE); |
| 1189 | } |
| 1190 | } |
| 1191 | #endif |
| 1192 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1193 | #ifdef SIGHASARG |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1194 | // try to find the name of this signal |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1195 | for (i = 0; signal_info[i].sig != -1; i++) |
| 1196 | if (sigarg == signal_info[i].sig) |
| 1197 | break; |
| 1198 | deadly_signal = sigarg; |
| 1199 | #endif |
| 1200 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1201 | full_screen = FALSE; // don't write message to the GUI, it might be |
| 1202 | // part of the problem... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1203 | /* |
| 1204 | * If something goes wrong after entering here, we may get here again. |
| 1205 | * When this happens, give a message and try to exit nicely (resetting the |
| 1206 | * terminal mode, etc.) |
| 1207 | * When this happens twice, just exit, don't even try to give a message, |
| 1208 | * stack may be corrupt or something weird. |
| 1209 | * When this still happens again (or memory was corrupted in such a way |
| 1210 | * that "entered" was clobbered) use _exit(), don't try freeing resources. |
| 1211 | */ |
| 1212 | if (entered >= 3) |
| 1213 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1214 | reset_signals(); // don't catch any signals anymore |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1215 | may_core_dump(); |
| 1216 | if (entered >= 4) |
| 1217 | _exit(8); |
| 1218 | exit(7); |
| 1219 | } |
| 1220 | if (entered == 2) |
| 1221 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1222 | // No translation, it may call malloc(). |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1223 | OUT_STR("Vim: Double signal, exiting\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1224 | out_flush(); |
| 1225 | getout(1); |
| 1226 | } |
| 1227 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1228 | // No translation, it may call malloc(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1229 | #ifdef SIGHASARG |
Bram Moolenaar | 69212b1 | 2020-05-10 14:14:03 +0200 | [diff] [blame] | 1230 | sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1231 | signal_info[i].name); |
| 1232 | #else |
Bram Moolenaar | 69212b1 | 2020-05-10 14:14:03 +0200 | [diff] [blame] | 1233 | sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1234 | #endif |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1235 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1236 | // Preserve files and exit. This sets the really_exiting flag to prevent |
| 1237 | // calling free(). |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 1238 | preserve_exit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1239 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1240 | // NOTREACHED |
Bram Moolenaar | e429e70 | 2016-06-10 19:49:14 +0200 | [diff] [blame] | 1241 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1242 | #ifdef NBDEBUG |
| 1243 | reset_signals(); |
| 1244 | may_core_dump(); |
| 1245 | abort(); |
| 1246 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1249 | /* |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1250 | * Invoked after receiving SIGCONT. We don't know what happened while |
| 1251 | * sleeping, deal with part of that. |
| 1252 | */ |
| 1253 | static void |
| 1254 | after_sigcont(void) |
| 1255 | { |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1256 | // Don't change "oldtitle" in a signal handler, set a flag to obtain it |
| 1257 | // again later. |
| 1258 | oldtitle_outdated = TRUE; |
Bram Moolenaar | 651fca8 | 2021-11-29 20:39:38 +0000 | [diff] [blame] | 1259 | |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1260 | settmode(TMODE_RAW); |
| 1261 | need_check_timestamps = TRUE; |
| 1262 | did_check_timestamps = FALSE; |
| 1263 | } |
| 1264 | |
| 1265 | #if defined(SIGCONT) |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 1266 | static void sigcont_handler SIGPROTOARG; |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1267 | |
| 1268 | /* |
| 1269 | * With multi-threading, suspending might not work immediately. Catch the |
| 1270 | * SIGCONT signal, which will be used as an indication whether the suspending |
| 1271 | * has been done or not. |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1272 | * |
| 1273 | * On Linux, signal is not always handled immediately either. |
| 1274 | * See https://bugs.launchpad.net/bugs/291373 |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1275 | * Probably because the signal is handled in another thread. |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1276 | * |
Bram Moolenaar | b292a2a | 2011-02-09 18:47:40 +0100 | [diff] [blame] | 1277 | * volatile because it is used in signal handler sigcont_handler(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1278 | */ |
Bram Moolenaar | 8e82c05 | 2018-08-21 19:47:48 +0200 | [diff] [blame] | 1279 | static volatile sig_atomic_t sigcont_received; |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 1280 | static void sigcont_handler SIGPROTOARG; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1281 | |
| 1282 | /* |
| 1283 | * signal handler for SIGCONT |
| 1284 | */ |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 1285 | static void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1286 | sigcont_handler SIGDEFARG(sigarg) |
| 1287 | { |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1288 | if (in_mch_suspend) |
| 1289 | { |
| 1290 | sigcont_received = TRUE; |
| 1291 | } |
| 1292 | else |
| 1293 | { |
| 1294 | // We didn't suspend ourselves, assume we were stopped by a SIGSTOP |
| 1295 | // signal (which can't be intercepted) and get a SIGCONT. Need to get |
| 1296 | // back to a sane mode. We should redraw, but we can't really do that |
| 1297 | // in a signal handler, do a redraw later. |
| 1298 | after_sigcont(); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1299 | redraw_later(UPD_CLEAR); |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1300 | cursor_on_force(); |
| 1301 | out_flush(); |
| 1302 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | } |
| 1304 | #endif |
| 1305 | |
Bram Moolenaar | 6dff58f | 2018-09-30 21:43:26 +0200 | [diff] [blame] | 1306 | #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1307 | # ifdef USE_SYSTEM |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1308 | static void *clip_star_save = NULL; |
| 1309 | static void *clip_plus_save = NULL; |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1310 | # endif |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1311 | |
| 1312 | /* |
| 1313 | * Called when Vim is going to sleep or execute a shell command. |
| 1314 | * We can't respond to requests for the X selections. Lose them, otherwise |
| 1315 | * other applications will hang. But first copy the text to cut buffer 0. |
| 1316 | */ |
| 1317 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1318 | loose_clipboard(void) |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1319 | { |
| 1320 | if (clip_star.owned || clip_plus.owned) |
| 1321 | { |
| 1322 | x11_export_final_selection(); |
| 1323 | if (clip_star.owned) |
| 1324 | clip_lose_selection(&clip_star); |
| 1325 | if (clip_plus.owned) |
| 1326 | clip_lose_selection(&clip_plus); |
| 1327 | if (x11_display != NULL) |
| 1328 | XFlush(x11_display); |
| 1329 | } |
| 1330 | } |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1331 | |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1332 | # ifdef USE_SYSTEM |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1333 | /* |
| 1334 | * Save clipboard text to restore later. |
| 1335 | */ |
| 1336 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1337 | save_clipboard(void) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1338 | { |
| 1339 | if (clip_star.owned) |
| 1340 | clip_star_save = get_register('*', TRUE); |
| 1341 | if (clip_plus.owned) |
| 1342 | clip_plus_save = get_register('+', TRUE); |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * Restore clipboard text if no one own the X selection. |
| 1347 | */ |
| 1348 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1349 | restore_clipboard(void) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1350 | { |
| 1351 | if (clip_star_save != NULL) |
| 1352 | { |
| 1353 | if (!clip_gen_owner_exists(&clip_star)) |
| 1354 | put_register('*', clip_star_save); |
| 1355 | else |
| 1356 | free_register(clip_star_save); |
| 1357 | clip_star_save = NULL; |
| 1358 | } |
| 1359 | if (clip_plus_save != NULL) |
| 1360 | { |
| 1361 | if (!clip_gen_owner_exists(&clip_plus)) |
| 1362 | put_register('+', clip_plus_save); |
| 1363 | else |
| 1364 | free_register(clip_plus_save); |
| 1365 | clip_plus_save = NULL; |
| 1366 | } |
| 1367 | } |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 1368 | # endif |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1369 | #endif |
| 1370 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1371 | /* |
| 1372 | * If the machine has job control, use it to suspend the program, |
| 1373 | * otherwise fake it by starting a new shell. |
| 1374 | */ |
| 1375 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1376 | mch_suspend(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1377 | { |
Bram Moolenaar | 5c3128e | 2020-05-11 20:54:42 +0200 | [diff] [blame] | 1378 | if (ignore_sigtstp) |
| 1379 | return; |
| 1380 | |
Bram Moolenaar | 041c710 | 2020-05-30 18:14:57 +0200 | [diff] [blame] | 1381 | #if defined(SIGTSTP) |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1382 | in_mch_suspend = TRUE; |
| 1383 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1384 | out_flush(); // needed to make cursor visible on some systems |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1385 | settmode(TMODE_COOK); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1386 | out_flush(); // needed to disable mouse on some systems |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1387 | |
| 1388 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 1389 | loose_clipboard(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1390 | # endif |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1391 | # if defined(SIGCONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1392 | sigcont_received = FALSE; |
| 1393 | # endif |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1394 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1395 | kill(0, SIGTSTP); // send ourselves a STOP signal |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1396 | |
| 1397 | # if defined(SIGCONT) |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1398 | /* |
| 1399 | * Wait for the SIGCONT signal to be handled. It generally happens |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1400 | * immediately, but somehow not all the time, probably because it's handled |
| 1401 | * in another thread. Do not call pause() because there would be race |
| 1402 | * condition which would hang Vim if signal happened in between the test of |
| 1403 | * sigcont_received and the call to pause(). If signal is not yet received, |
| 1404 | * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not |
| 1405 | * received after 1+2+3 ms (not expected to happen). |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1406 | */ |
| 1407 | { |
Bram Moolenaar | 262735e | 2009-07-14 10:20:22 +0000 | [diff] [blame] | 1408 | long wait_time; |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1409 | |
Bram Moolenaar | 262735e | 2009-07-14 10:20:22 +0000 | [diff] [blame] | 1410 | for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++) |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 1411 | mch_delay(wait_time, 0); |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 1412 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1413 | # endif |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1414 | in_mch_suspend = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1415 | |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1416 | after_sigcont(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1417 | #else |
| 1418 | suspend_shell(); |
| 1419 | #endif |
| 1420 | } |
| 1421 | |
| 1422 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1423 | mch_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1424 | { |
| 1425 | Columns = 80; |
| 1426 | Rows = 24; |
| 1427 | |
| 1428 | out_flush(); |
Bram Moolenaar | 5c3128e | 2020-05-11 20:54:42 +0200 | [diff] [blame] | 1429 | |
| 1430 | #ifdef SIGTSTP |
| 1431 | // Check whether we were invoked with SIGTSTP set to be ignored. If it is |
| 1432 | // that indicates the shell (or program) that launched us does not support |
| 1433 | // tty job control and thus we should ignore that signal. If invoked as a |
| 1434 | // restricted editor (e.g., as "rvim") SIGTSTP is always ignored. |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1435 | ignore_sigtstp = restricted || SIG_IGN == mch_signal(SIGTSTP, SIG_ERR); |
Bram Moolenaar | 5c3128e | 2020-05-11 20:54:42 +0200 | [diff] [blame] | 1436 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1437 | set_signals(); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1438 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 1439 | #ifdef MACOS_CONVERT |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1440 | mac_conv_init(); |
| 1441 | #endif |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 1442 | #ifdef FEAT_CYGWIN_WIN32_CLIPBOARD |
| 1443 | win_clip_init(); |
| 1444 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1448 | set_signals(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1449 | { |
| 1450 | #if defined(SIGWINCH) |
| 1451 | /* |
| 1452 | * WINDOW CHANGE signal is handled with sig_winch(). |
| 1453 | */ |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1454 | mch_signal(SIGWINCH, sig_winch); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1455 | #endif |
| 1456 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1457 | #ifdef SIGTSTP |
Bram Moolenaar | 5c3128e | 2020-05-11 20:54:42 +0200 | [diff] [blame] | 1458 | // See mch_init() for the conditions under which we ignore SIGTSTP. |
Bram Moolenaar | 8e4af85 | 2022-01-24 12:20:45 +0000 | [diff] [blame] | 1459 | // In the GUI default TSTP processing is OK. |
| 1460 | // Checking both gui.in_use and gui.starting because gui.in_use is not set |
| 1461 | // at this point (set after menus are displayed), but gui.starting is set. |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1462 | mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN |
Bram Moolenaar | 8e4af85 | 2022-01-24 12:20:45 +0000 | [diff] [blame] | 1463 | # ifdef FEAT_GUI |
| 1464 | : gui.in_use || gui.starting ? SIG_DFL |
| 1465 | # endif |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1466 | : sig_tstp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1467 | #endif |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1468 | #if defined(SIGCONT) |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1469 | mch_signal(SIGCONT, sigcont_handler); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1470 | #endif |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 1471 | #ifdef SIGPIPE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1472 | /* |
| 1473 | * We want to ignore breaking of PIPEs. |
| 1474 | */ |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1475 | mch_signal(SIGPIPE, SIG_IGN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1476 | #endif |
| 1477 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1478 | #ifdef SIGINT |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1479 | catch_int_signal(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1480 | #endif |
| 1481 | |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 1482 | #ifdef SIGUSR1 |
| 1483 | /* |
| 1484 | * Call user's handler on SIGUSR1 |
| 1485 | */ |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1486 | mch_signal(SIGUSR1, catch_sigusr1); |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 1487 | #endif |
| 1488 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1489 | /* |
| 1490 | * Ignore alarm signals (Perl's alarm() generates it). |
| 1491 | */ |
| 1492 | #ifdef SIGALRM |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1493 | mch_signal(SIGALRM, SIG_IGN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1494 | #endif |
| 1495 | |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 1496 | #ifdef SIGPWR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1497 | /* |
| 1498 | * Catch SIGPWR (power failure?) to preserve the swap files, so that no |
| 1499 | * work will be lost. |
| 1500 | */ |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1501 | mch_signal(SIGPWR, catch_sigpwr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1502 | #endif |
| 1503 | |
| 1504 | /* |
| 1505 | * Arrange for other signals to gracefully shutdown Vim. |
| 1506 | */ |
| 1507 | catch_signals(deathtrap, SIG_ERR); |
| 1508 | |
| 1509 | #if defined(FEAT_GUI) && defined(SIGHUP) |
| 1510 | /* |
| 1511 | * When the GUI is running, ignore the hangup signal. |
| 1512 | */ |
| 1513 | if (gui.in_use) |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1514 | mch_signal(SIGHUP, SIG_IGN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1515 | #endif |
| 1516 | } |
| 1517 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1518 | #if defined(SIGINT) || defined(PROTO) |
| 1519 | /* |
| 1520 | * Catch CTRL-C (only works while in Cooked mode). |
| 1521 | */ |
| 1522 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1523 | catch_int_signal(void) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1524 | { |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1525 | mch_signal(SIGINT, catch_sigint); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1526 | } |
| 1527 | #endif |
| 1528 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1529 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1530 | reset_signals(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1531 | { |
| 1532 | catch_signals(SIG_DFL, SIG_DFL); |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1533 | #if defined(SIGCONT) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1534 | // SIGCONT isn't in the list, because its default action is ignore |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1535 | mch_signal(SIGCONT, SIG_DFL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1536 | #endif |
| 1537 | } |
| 1538 | |
| 1539 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1540 | catch_signals( |
Michael Jarvis | be9624e | 2023-04-19 20:28:48 +0100 | [diff] [blame] | 1541 | void (*func_deadly)(int), |
| 1542 | void (*func_other)(int)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1543 | { |
| 1544 | int i; |
| 1545 | |
| 1546 | for (i = 0; signal_info[i].sig != -1; i++) |
Bram Moolenaar | 5c3128e | 2020-05-11 20:54:42 +0200 | [diff] [blame] | 1547 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1548 | if (signal_info[i].deadly) |
| 1549 | { |
| 1550 | #if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION) |
| 1551 | struct sigaction sa; |
| 1552 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1553 | // Setup to use the alternate stack for the signal function. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1554 | sa.sa_handler = func_deadly; |
| 1555 | sigemptyset(&sa.sa_mask); |
| 1556 | # if defined(__linux__) && defined(_REENTRANT) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1557 | // On Linux, with glibc compiled for kernel 2.2, there is a bug in |
| 1558 | // thread handling in combination with using the alternate stack: |
| 1559 | // pthread library functions try to use the stack pointer to |
| 1560 | // identify the current thread, causing a SEGV signal, which |
| 1561 | // recursively calls deathtrap() and hangs. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1562 | sa.sa_flags = 0; |
| 1563 | # else |
| 1564 | sa.sa_flags = SA_ONSTACK; |
| 1565 | # endif |
| 1566 | sigaction(signal_info[i].sig, &sa, NULL); |
| 1567 | #else |
| 1568 | # if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC) |
| 1569 | struct sigvec sv; |
| 1570 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1571 | // Setup to use the alternate stack for the signal function. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1572 | sv.sv_handler = func_deadly; |
| 1573 | sv.sv_mask = 0; |
| 1574 | sv.sv_flags = SV_ONSTACK; |
| 1575 | sigvec(signal_info[i].sig, &sv, NULL); |
| 1576 | # else |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1577 | mch_signal(signal_info[i].sig, func_deadly); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1578 | # endif |
| 1579 | #endif |
| 1580 | } |
| 1581 | else if (func_other != SIG_ERR) |
Bram Moolenaar | 5c3128e | 2020-05-11 20:54:42 +0200 | [diff] [blame] | 1582 | { |
| 1583 | // Deal with non-deadly signals. |
| 1584 | #ifdef SIGTSTP |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1585 | mch_signal(signal_info[i].sig, |
Bram Moolenaar | 5c3128e | 2020-05-11 20:54:42 +0200 | [diff] [blame] | 1586 | signal_info[i].sig == SIGTSTP && ignore_sigtstp |
| 1587 | ? SIG_IGN : func_other); |
| 1588 | #else |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1589 | mch_signal(signal_info[i].sig, func_other); |
Bram Moolenaar | 5c3128e | 2020-05-11 20:54:42 +0200 | [diff] [blame] | 1590 | #endif |
| 1591 | } |
| 1592 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 1595 | #ifdef HAVE_SIGPROCMASK |
| 1596 | static void |
| 1597 | block_signals(sigset_t *set) |
| 1598 | { |
| 1599 | sigset_t newset; |
| 1600 | int i; |
| 1601 | |
| 1602 | sigemptyset(&newset); |
| 1603 | |
| 1604 | for (i = 0; signal_info[i].sig != -1; i++) |
| 1605 | sigaddset(&newset, signal_info[i].sig); |
| 1606 | |
Bram Moolenaar | 2e31048 | 2018-08-21 13:09:10 +0200 | [diff] [blame] | 1607 | # if defined(SIGCONT) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1608 | // SIGCONT isn't in the list, because its default action is ignore |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 1609 | sigaddset(&newset, SIGCONT); |
| 1610 | # endif |
| 1611 | |
| 1612 | sigprocmask(SIG_BLOCK, &newset, set); |
| 1613 | } |
| 1614 | |
| 1615 | static void |
| 1616 | unblock_signals(sigset_t *set) |
| 1617 | { |
| 1618 | sigprocmask(SIG_SETMASK, set, NULL); |
| 1619 | } |
| 1620 | #endif |
| 1621 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1622 | /* |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1623 | * Handling of SIGHUP, SIGQUIT and SIGTERM: |
Bram Moolenaar | 9e1d283 | 2007-05-06 12:51:41 +0000 | [diff] [blame] | 1624 | * "when" == a signal: when busy, postpone and return FALSE, otherwise |
| 1625 | * return TRUE |
| 1626 | * "when" == SIGNAL_BLOCK: Going to be busy, block signals |
| 1627 | * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 1628 | * signal |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1629 | * Returns TRUE when Vim should exit. |
| 1630 | */ |
| 1631 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1632 | vim_handle_signal(int sig) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1633 | { |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1634 | static int got_signal = 0; |
| 1635 | static int blocked = TRUE; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1636 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1637 | switch (sig) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1638 | { |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1639 | case SIGNAL_BLOCK: blocked = TRUE; |
| 1640 | break; |
| 1641 | |
| 1642 | case SIGNAL_UNBLOCK: blocked = FALSE; |
| 1643 | if (got_signal != 0) |
| 1644 | { |
| 1645 | kill(getpid(), got_signal); |
| 1646 | got_signal = 0; |
| 1647 | } |
| 1648 | break; |
| 1649 | |
| 1650 | default: if (!blocked) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1651 | return TRUE; // exit! |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1652 | got_signal = sig; |
| 1653 | #ifdef SIGPWR |
| 1654 | if (sig != SIGPWR) |
| 1655 | #endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1656 | got_int = TRUE; // break any loops |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1657 | break; |
| 1658 | } |
| 1659 | return FALSE; |
| 1660 | } |
| 1661 | |
| 1662 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1663 | * Check_win checks whether we have an interactive stdout. |
| 1664 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1665 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1666 | mch_check_win(int argc UNUSED, char **argv UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1667 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1668 | if (isatty(1)) |
| 1669 | return OK; |
| 1670 | return FAIL; |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * Return TRUE if the input comes from a terminal, FALSE otherwise. |
| 1675 | */ |
| 1676 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1677 | mch_input_isatty(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1678 | { |
| 1679 | if (isatty(read_cmd_fd)) |
| 1680 | return TRUE; |
| 1681 | return FALSE; |
| 1682 | } |
| 1683 | |
| 1684 | #ifdef FEAT_X11 |
| 1685 | |
Bram Moolenaar | 651fca8 | 2021-11-29 20:39:38 +0000 | [diff] [blame] | 1686 | # if defined(ELAPSED_TIMEVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1687 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1688 | /* |
| 1689 | * Give a message about the elapsed time for opening the X window. |
| 1690 | */ |
| 1691 | static void |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 1692 | xopen_message(long elapsed_msec) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1693 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1694 | smsg(_("Opening the X display took %ld msec"), elapsed_msec); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1695 | } |
| 1696 | # endif |
| 1697 | #endif |
| 1698 | |
Bram Moolenaar | 651fca8 | 2021-11-29 20:39:38 +0000 | [diff] [blame] | 1699 | #if defined(FEAT_X11) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1700 | /* |
| 1701 | * A few functions shared by X11 title and clipboard code. |
| 1702 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1703 | |
| 1704 | static int got_x_error = FALSE; |
| 1705 | |
| 1706 | /* |
| 1707 | * X Error handler, otherwise X just exits! (very rude) -- webb |
| 1708 | */ |
| 1709 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1710 | x_error_handler(Display *dpy, XErrorEvent *error_event) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1711 | { |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 1712 | XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1713 | STRCAT(IObuff, _("\nVim: Got X error\n")); |
| 1714 | |
Bram Moolenaar | b1062eb | 2020-05-09 16:11:33 +0200 | [diff] [blame] | 1715 | // In the GUI we cannot print a message and continue, because no X calls |
| 1716 | // are allowed here (causes my system to hang). Silently continuing seems |
| 1717 | // like the best alternative. Do preserve files, in case we crash. |
| 1718 | ml_sync_all(FALSE, FALSE); |
| 1719 | |
| 1720 | #ifdef FEAT_GUI |
| 1721 | if (!gui.in_use) |
| 1722 | #endif |
| 1723 | msg((char *)IObuff); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1724 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1725 | return 0; // NOTREACHED |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | /* |
| 1729 | * Another X Error handler, just used to check for errors. |
| 1730 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1731 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1732 | x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1733 | { |
| 1734 | got_x_error = TRUE; |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
Bram Moolenaar | c0c7549 | 2018-12-29 11:03:23 +0100 | [diff] [blame] | 1738 | /* |
| 1739 | * Return TRUE when connection to the X server is desired. |
| 1740 | */ |
| 1741 | static int |
| 1742 | x_connect_to_server(void) |
| 1743 | { |
| 1744 | // No point in connecting if we are exiting or dying. |
| 1745 | if (exiting || v_dying) |
| 1746 | return FALSE; |
| 1747 | |
| 1748 | #if defined(FEAT_CLIENTSERVER) |
| 1749 | if (x_force_connect) |
| 1750 | return TRUE; |
| 1751 | #endif |
| 1752 | if (x_no_connect) |
| 1753 | return FALSE; |
| 1754 | |
Bram Moolenaar | a8bfa17 | 2018-12-29 22:28:46 +0100 | [diff] [blame] | 1755 | // Check for a match with "exclude:" from 'clipboard'. |
Bram Moolenaar | c0c7549 | 2018-12-29 11:03:23 +0100 | [diff] [blame] | 1756 | if (clip_exclude_prog != NULL) |
| 1757 | { |
Bram Moolenaar | a8bfa17 | 2018-12-29 22:28:46 +0100 | [diff] [blame] | 1758 | // Just in case we get called recursively, return FALSE. This could |
| 1759 | // happen if vpeekc() is used while executing the prog and it causes a |
| 1760 | // related callback to be invoked. |
| 1761 | if (regprog_in_use(clip_exclude_prog)) |
| 1762 | return FALSE; |
| 1763 | |
Bram Moolenaar | c0c7549 | 2018-12-29 11:03:23 +0100 | [diff] [blame] | 1764 | if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0)) |
| 1765 | return FALSE; |
| 1766 | } |
| 1767 | return TRUE; |
| 1768 | } |
| 1769 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1770 | #if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 1771 | # if defined(USING_SETJMP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1772 | /* |
| 1773 | * An X IO Error handler, used to catch error while opening the display. |
| 1774 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1775 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1776 | x_IOerror_check(Display *dpy UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1777 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1778 | // This function should not return, it causes exit(). Longjump instead. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1779 | LONGJMP(lc_jump_env, 1); |
Bram Moolenaar | 1eed532 | 2019-02-26 17:03:54 +0100 | [diff] [blame] | 1780 | # if defined(VMS) || defined(__CYGWIN__) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1781 | return 0; // avoid the compiler complains about missing return value |
Bram Moolenaar | b4990bf | 2010-02-11 18:19:38 +0100 | [diff] [blame] | 1782 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1783 | } |
| 1784 | # endif |
| 1785 | |
| 1786 | /* |
| 1787 | * An X IO Error handler, used to catch terminal errors. |
| 1788 | */ |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 1789 | static int xterm_dpy_retry_count = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1790 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1791 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1792 | x_IOerror_handler(Display *dpy UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1793 | { |
| 1794 | xterm_dpy = NULL; |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 1795 | xterm_dpy_retry_count = 5; // Try reconnecting five times |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1796 | x11_window = 0; |
| 1797 | x11_display = NULL; |
| 1798 | xterm_Shell = (Widget)0; |
| 1799 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1800 | // This function should not return, it causes exit(). Longjump instead. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1801 | LONGJMP(x_jump_env, 1); |
Bram Moolenaar | 1eed532 | 2019-02-26 17:03:54 +0100 | [diff] [blame] | 1802 | # if defined(VMS) || defined(__CYGWIN__) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1803 | return 0; // avoid the compiler complains about missing return value |
Bram Moolenaar | b4990bf | 2010-02-11 18:19:38 +0100 | [diff] [blame] | 1804 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1805 | } |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1806 | |
| 1807 | /* |
| 1808 | * If the X11 connection was lost try to restore it. |
| 1809 | * 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] | 1810 | * (e.g. through tmux). |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1811 | */ |
| 1812 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1813 | may_restore_clipboard(void) |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1814 | { |
Bram Moolenaar | 01e51e5 | 2018-12-29 13:09:46 +0100 | [diff] [blame] | 1815 | // No point in restoring the connecting if we are exiting or dying. |
| 1816 | if (!exiting && !v_dying && xterm_dpy_retry_count > 0) |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1817 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 1818 | --xterm_dpy_retry_count; |
Bram Moolenaar | 527a678 | 2014-12-17 17:59:31 +0100 | [diff] [blame] | 1819 | |
| 1820 | # ifndef LESSTIF_VERSION |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1821 | // This has been reported to avoid Vim getting stuck. |
Bram Moolenaar | 527a678 | 2014-12-17 17:59:31 +0100 | [diff] [blame] | 1822 | if (app_context != (XtAppContext)NULL) |
| 1823 | { |
| 1824 | XtDestroyApplicationContext(app_context); |
| 1825 | app_context = (XtAppContext)NULL; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1826 | x11_display = NULL; // freed by XtDestroyApplicationContext() |
Bram Moolenaar | 527a678 | 2014-12-17 17:59:31 +0100 | [diff] [blame] | 1827 | } |
| 1828 | # endif |
| 1829 | |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 1830 | setup_term_clip(); |
| 1831 | get_x11_title(FALSE); |
| 1832 | } |
| 1833 | } |
Bram Moolenaar | d4aa83a | 2019-05-09 18:59:31 +0200 | [diff] [blame] | 1834 | |
| 1835 | void |
| 1836 | ex_xrestore(exarg_T *eap) |
| 1837 | { |
| 1838 | if (eap->arg != NULL && STRLEN(eap->arg) > 0) |
| 1839 | { |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 1840 | if (xterm_display_allocated) |
| 1841 | vim_free(xterm_display); |
| 1842 | xterm_display = (char *)vim_strsave(eap->arg); |
| 1843 | xterm_display_allocated = TRUE; |
Bram Moolenaar | d4aa83a | 2019-05-09 18:59:31 +0200 | [diff] [blame] | 1844 | } |
| 1845 | smsg(_("restoring display %s"), xterm_display == NULL |
Bram Moolenaar | 0c5c3fa | 2019-11-30 22:38:16 +0100 | [diff] [blame] | 1846 | ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display); |
Bram Moolenaar | d4aa83a | 2019-05-09 18:59:31 +0200 | [diff] [blame] | 1847 | |
| 1848 | clear_xterm_clip(); |
| 1849 | x11_window = 0; |
| 1850 | xterm_dpy_retry_count = 5; // Try reconnecting five times |
| 1851 | may_restore_clipboard(); |
| 1852 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1853 | #endif |
| 1854 | |
| 1855 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1856 | * Test if "dpy" and x11_window are valid by getting the window title. |
| 1857 | * I don't actually want it yet, so there may be a simpler call to use, but |
| 1858 | * this will cause the error handler x_error_check() to be called if anything |
| 1859 | * is wrong, such as the window pointer being invalid (as can happen when the |
| 1860 | * user changes his DISPLAY, but not his WINDOWID) -- webb |
| 1861 | */ |
| 1862 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1863 | test_x11_window(Display *dpy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1864 | { |
Dominique Pellé | 4927bc7 | 2023-09-24 16:12:07 +0200 | [diff] [blame] | 1865 | int (*old_handler)(Display*, XErrorEvent*); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1866 | XTextProperty text_prop; |
| 1867 | |
| 1868 | old_handler = XSetErrorHandler(x_error_check); |
| 1869 | got_x_error = FALSE; |
| 1870 | if (XGetWMName(dpy, x11_window, &text_prop)) |
| 1871 | XFree((void *)text_prop.value); |
| 1872 | XSync(dpy, False); |
| 1873 | (void)XSetErrorHandler(old_handler); |
| 1874 | |
| 1875 | if (p_verbose > 0 && got_x_error) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 1876 | verb_msg(_("Testing the X display failed")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1877 | |
| 1878 | return (got_x_error ? FAIL : OK); |
| 1879 | } |
| 1880 | #endif |
| 1881 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1882 | |
| 1883 | #ifdef FEAT_X11 |
| 1884 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1885 | static int get_x11_thing(int get_title, int test_only); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1886 | |
| 1887 | /* |
| 1888 | * try to get x11 window and display |
| 1889 | * |
| 1890 | * return FAIL for failure, OK otherwise |
| 1891 | */ |
| 1892 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1893 | get_x11_windis(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1894 | { |
| 1895 | char *winid; |
| 1896 | static int result = -1; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1897 | #define XD_NONE 0 // x11_display not set here |
| 1898 | #define XD_HERE 1 // x11_display opened here |
| 1899 | #define XD_GUI 2 // x11_display used from gui.dpy |
| 1900 | #define XD_XTERM 3 // x11_display used from xterm_dpy |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1901 | static int x11_display_from = XD_NONE; |
| 1902 | static int did_set_error_handler = FALSE; |
| 1903 | |
| 1904 | if (!did_set_error_handler) |
| 1905 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1906 | // X just exits if it finds an error otherwise! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1907 | (void)XSetErrorHandler(x_error_handler); |
| 1908 | did_set_error_handler = TRUE; |
| 1909 | } |
| 1910 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 1911 | #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1912 | if (gui.in_use) |
| 1913 | { |
| 1914 | /* |
| 1915 | * If the X11 display was opened here before, for the window where Vim |
| 1916 | * was started, close that one now to avoid a memory leak. |
| 1917 | */ |
| 1918 | if (x11_display_from == XD_HERE && x11_display != NULL) |
| 1919 | { |
| 1920 | XCloseDisplay(x11_display); |
| 1921 | x11_display_from = XD_NONE; |
| 1922 | } |
| 1923 | if (gui_get_x11_windis(&x11_window, &x11_display) == OK) |
| 1924 | { |
| 1925 | x11_display_from = XD_GUI; |
| 1926 | return OK; |
| 1927 | } |
| 1928 | x11_display = NULL; |
| 1929 | return FAIL; |
| 1930 | } |
| 1931 | else if (x11_display_from == XD_GUI) |
| 1932 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1933 | // GUI must have stopped somehow, clear x11_display |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1934 | x11_window = 0; |
| 1935 | x11_display = NULL; |
| 1936 | x11_display_from = XD_NONE; |
| 1937 | } |
| 1938 | #endif |
| 1939 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1940 | // When started with the "-X" argument, don't try connecting. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1941 | if (!x_connect_to_server()) |
| 1942 | return FAIL; |
| 1943 | |
| 1944 | /* |
| 1945 | * If WINDOWID not set, should try another method to find out |
| 1946 | * what the current window number is. The only code I know for |
| 1947 | * this is very complicated. |
| 1948 | * We assume that zero is invalid for WINDOWID. |
| 1949 | */ |
| 1950 | if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL) |
| 1951 | x11_window = (Window)atol(winid); |
| 1952 | |
| 1953 | #ifdef FEAT_XCLIPBOARD |
Bram Moolenaar | d4aa83a | 2019-05-09 18:59:31 +0200 | [diff] [blame] | 1954 | if (xterm_dpy == x11_display) |
| 1955 | // x11_display may have been set to xterm_dpy elsewhere |
| 1956 | x11_display_from = XD_XTERM; |
| 1957 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1958 | if (xterm_dpy != NULL && x11_window != 0) |
| 1959 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1960 | // We may have checked it already, but Gnome terminal can move us to |
| 1961 | // another window, so we need to check every time. |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 1962 | if (x11_display_from != XD_XTERM) |
| 1963 | { |
| 1964 | /* |
| 1965 | * If the X11 display was opened here before, for the window where |
| 1966 | * Vim was started, close that one now to avoid a memory leak. |
| 1967 | */ |
| 1968 | if (x11_display_from == XD_HERE && x11_display != NULL) |
| 1969 | XCloseDisplay(x11_display); |
| 1970 | x11_display = xterm_dpy; |
| 1971 | x11_display_from = XD_XTERM; |
| 1972 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1973 | if (test_x11_window(x11_display) == FAIL) |
| 1974 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1975 | // probably bad $WINDOWID |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1976 | x11_window = 0; |
| 1977 | x11_display = NULL; |
| 1978 | x11_display_from = XD_NONE; |
| 1979 | return FAIL; |
| 1980 | } |
| 1981 | return OK; |
| 1982 | } |
| 1983 | #endif |
| 1984 | |
| 1985 | if (x11_window == 0 || x11_display == NULL) |
| 1986 | result = -1; |
| 1987 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 1988 | if (result != -1) // Have already been here and set this |
| 1989 | return result; // Don't do all these X calls again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1990 | |
| 1991 | if (x11_window != 0 && x11_display == NULL) |
| 1992 | { |
| 1993 | #ifdef SET_SIG_ALARM |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 1994 | sighandler_T sig_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1995 | #endif |
Bram Moolenaar | 1ac56c2 | 2019-01-17 22:28:22 +0100 | [diff] [blame] | 1996 | #ifdef ELAPSED_FUNC |
| 1997 | elapsed_T start_tv; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1998 | |
| 1999 | if (p_verbose > 0) |
Bram Moolenaar | 1ac56c2 | 2019-01-17 22:28:22 +0100 | [diff] [blame] | 2000 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2001 | #endif |
| 2002 | |
| 2003 | #ifdef SET_SIG_ALARM |
| 2004 | /* |
| 2005 | * Opening the Display may hang if the DISPLAY setting is wrong, or |
| 2006 | * the network connection is bad. Set an alarm timer to get out. |
| 2007 | */ |
| 2008 | sig_alarm_called = FALSE; |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 2009 | sig_save = mch_signal(SIGALRM, sig_alarm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2010 | alarm(2); |
| 2011 | #endif |
| 2012 | x11_display = XOpenDisplay(NULL); |
| 2013 | |
| 2014 | #ifdef SET_SIG_ALARM |
| 2015 | alarm(0); |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 2016 | mch_signal(SIGALRM, sig_save); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2017 | if (p_verbose > 0 && sig_alarm_called) |
Bram Moolenaar | 563bbea | 2019-01-22 21:45:40 +0100 | [diff] [blame] | 2018 | verb_msg(_("Opening the X display timed out")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2019 | #endif |
| 2020 | if (x11_display != NULL) |
| 2021 | { |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 2022 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2023 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 2024 | { |
| 2025 | verbose_enter(); |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 2026 | xopen_message(ELAPSED_FUNC(start_tv)); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 2027 | verbose_leave(); |
| 2028 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2029 | # endif |
| 2030 | if (test_x11_window(x11_display) == FAIL) |
| 2031 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2032 | // Maybe window id is bad |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2033 | x11_window = 0; |
| 2034 | XCloseDisplay(x11_display); |
| 2035 | x11_display = NULL; |
| 2036 | } |
| 2037 | else |
| 2038 | x11_display_from = XD_HERE; |
| 2039 | } |
| 2040 | } |
| 2041 | if (x11_window == 0 || x11_display == NULL) |
| 2042 | return (result = FAIL); |
Bram Moolenaar | 727c876 | 2010-10-20 19:17:48 +0200 | [diff] [blame] | 2043 | |
| 2044 | # ifdef FEAT_EVAL |
| 2045 | set_vim_var_nr(VV_WINDOWID, (long)x11_window); |
| 2046 | # endif |
| 2047 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2048 | return (result = OK); |
| 2049 | } |
| 2050 | |
| 2051 | /* |
| 2052 | * Determine original x11 Window Title |
| 2053 | */ |
| 2054 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2055 | get_x11_title(int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2056 | { |
Bram Moolenaar | 47136d7 | 2004-10-12 20:02:24 +0000 | [diff] [blame] | 2057 | return get_x11_thing(TRUE, test_only); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * Determine original x11 Window icon |
| 2062 | */ |
| 2063 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2064 | get_x11_icon(int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | { |
| 2066 | int retval = FALSE; |
| 2067 | |
| 2068 | retval = get_x11_thing(FALSE, test_only); |
| 2069 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2070 | // could not get old icon, use terminal name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2071 | if (oldicon == NULL && !test_only) |
| 2072 | { |
| 2073 | if (STRNCMP(T_NAME, "builtin_", 8) == 0) |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 2074 | oldicon = vim_strsave(T_NAME + 8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2075 | else |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 2076 | oldicon = vim_strsave(T_NAME); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | return retval; |
| 2080 | } |
| 2081 | |
| 2082 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2083 | get_x11_thing( |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2084 | int get_title, // get title string |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2085 | int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2086 | { |
| 2087 | XTextProperty text_prop; |
| 2088 | int retval = FALSE; |
| 2089 | Status status; |
| 2090 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2091 | if (get_x11_windis() != OK) |
| 2092 | return FALSE; |
| 2093 | |
| 2094 | // Get window/icon name if any |
| 2095 | if (get_title) |
| 2096 | status = XGetWMName(x11_display, x11_window, &text_prop); |
| 2097 | else |
| 2098 | status = XGetWMIconName(x11_display, x11_window, &text_prop); |
| 2099 | |
| 2100 | /* |
| 2101 | * If terminal is xterm, then x11_window may be a child window of the |
| 2102 | * outer xterm window that actually contains the window/icon name, so |
| 2103 | * keep traversing up the tree until a window with a title/icon is |
| 2104 | * found. |
| 2105 | */ |
| 2106 | // Previously this was only done for xterm and alike. I don't see a |
| 2107 | // reason why it would fail for other terminal emulators. |
| 2108 | // if (term_is_xterm) |
| 2109 | Window root; |
| 2110 | Window parent; |
| 2111 | Window win = x11_window; |
| 2112 | Window *children; |
| 2113 | unsigned int num_children; |
| 2114 | |
| 2115 | while (!status || text_prop.value == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2116 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2117 | if (!XQueryTree(x11_display, win, &root, &parent, &children, |
| 2118 | &num_children)) |
| 2119 | break; |
| 2120 | if (children) |
| 2121 | XFree((void *)children); |
| 2122 | if (parent == root || parent == 0) |
| 2123 | break; |
| 2124 | |
| 2125 | win = parent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2126 | if (get_title) |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2127 | status = XGetWMName(x11_display, win, &text_prop); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2128 | else |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2129 | status = XGetWMIconName(x11_display, win, &text_prop); |
| 2130 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2131 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2132 | if (status && text_prop.value != NULL) |
| 2133 | { |
| 2134 | retval = TRUE; |
| 2135 | if (!test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2136 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2137 | if (get_title) |
| 2138 | vim_free(oldtitle); |
| 2139 | else |
| 2140 | vim_free(oldicon); |
| 2141 | if (text_prop.encoding == XA_STRING && !has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2142 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2143 | if (get_title) |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2144 | oldtitle = vim_strsave((char_u *)text_prop.value); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2145 | else |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2146 | oldicon = vim_strsave((char_u *)text_prop.value); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2147 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2148 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2149 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2150 | char **cl; |
| 2151 | Status transform_status; |
| 2152 | int n = 0; |
| 2153 | |
| 2154 | transform_status = XmbTextPropertyToTextList(x11_display, |
| 2155 | &text_prop, |
| 2156 | &cl, &n); |
| 2157 | if (transform_status >= Success && n > 0 && cl[0]) |
| 2158 | { |
| 2159 | if (get_title) |
| 2160 | oldtitle = vim_strsave((char_u *) cl[0]); |
| 2161 | else |
| 2162 | oldicon = vim_strsave((char_u *) cl[0]); |
| 2163 | XFreeStringList(cl); |
| 2164 | } |
Bram Moolenaar | 6b649ac | 2019-12-07 17:47:22 +0100 | [diff] [blame] | 2165 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2166 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2167 | if (get_title) |
| 2168 | oldtitle = vim_strsave((char_u *)text_prop.value); |
| 2169 | else |
| 2170 | oldicon = vim_strsave((char_u *)text_prop.value); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2171 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2172 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2173 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2174 | XFree((void *)text_prop.value); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2175 | } |
| 2176 | return retval; |
| 2177 | } |
| 2178 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2179 | // Xutf8 functions are not available on older systems. Note that on some |
| 2180 | // systems X_HAVE_UTF8_STRING may be defined in a header file but |
| 2181 | // Xutf8SetWMProperties() is not in the X11 library. Configure checks for |
| 2182 | // that and defines HAVE_XUTF8SETWMPROPERTIES. |
Bram Moolenaar | a12a161 | 2019-01-24 16:39:02 +0100 | [diff] [blame] | 2183 | #if defined(X_HAVE_UTF8_STRING) |
Bram Moolenaar | cbc246a | 2014-10-11 14:47:26 +0200 | [diff] [blame] | 2184 | # if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2185 | # define USE_UTF8_STRING |
| 2186 | # endif |
| 2187 | #endif |
| 2188 | |
| 2189 | /* |
| 2190 | * Set x11 Window Title |
| 2191 | * |
| 2192 | * get_x11_windis() must be called before this and have returned OK |
| 2193 | */ |
| 2194 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2195 | set_x11_title(char_u *title) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2196 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2197 | // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING |
| 2198 | // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't |
| 2199 | // supported everywhere and STRING doesn't work for multi-byte titles. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2200 | #ifdef USE_UTF8_STRING |
| 2201 | if (enc_utf8) |
| 2202 | Xutf8SetWMProperties(x11_display, x11_window, (const char *)title, |
| 2203 | NULL, NULL, 0, NULL, NULL, NULL); |
| 2204 | else |
| 2205 | #endif |
| 2206 | { |
| 2207 | #if XtSpecificationRelease >= 4 |
| 2208 | # ifdef FEAT_XFONTSET |
| 2209 | XmbSetWMProperties(x11_display, x11_window, (const char *)title, |
| 2210 | NULL, NULL, 0, NULL, NULL, NULL); |
| 2211 | # else |
| 2212 | XTextProperty text_prop; |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 2213 | char *c_title = (char *)title; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2214 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2215 | // directly from example 3-18 "basicwin" of Xlib Programming Manual |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 2216 | (void)XStringListToTextProperty(&c_title, 1, &text_prop); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2217 | XSetWMProperties(x11_display, x11_window, &text_prop, |
| 2218 | NULL, NULL, 0, NULL, NULL, NULL); |
| 2219 | # endif |
| 2220 | #else |
| 2221 | XStoreName(x11_display, x11_window, (char *)title); |
| 2222 | #endif |
| 2223 | } |
| 2224 | XFlush(x11_display); |
| 2225 | } |
| 2226 | |
| 2227 | /* |
| 2228 | * Set x11 Window icon |
| 2229 | * |
| 2230 | * get_x11_windis() must be called before this and have returned OK |
| 2231 | */ |
| 2232 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2233 | set_x11_icon(char_u *icon) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2234 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2235 | // See above for comments about using X*SetWMProperties(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2236 | #ifdef USE_UTF8_STRING |
| 2237 | if (enc_utf8) |
| 2238 | Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon, |
| 2239 | NULL, 0, NULL, NULL, NULL); |
| 2240 | else |
| 2241 | #endif |
| 2242 | { |
| 2243 | #if XtSpecificationRelease >= 4 |
| 2244 | # ifdef FEAT_XFONTSET |
| 2245 | XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon, |
| 2246 | NULL, 0, NULL, NULL, NULL); |
| 2247 | # else |
| 2248 | XTextProperty text_prop; |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 2249 | char *c_icon = (char *)icon; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2250 | |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 2251 | (void)XStringListToTextProperty(&c_icon, 1, &text_prop); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2252 | XSetWMProperties(x11_display, x11_window, NULL, &text_prop, |
| 2253 | NULL, 0, NULL, NULL, NULL); |
| 2254 | # endif |
| 2255 | #else |
| 2256 | XSetIconName(x11_display, x11_window, (char *)icon); |
| 2257 | #endif |
| 2258 | } |
| 2259 | XFlush(x11_display); |
| 2260 | } |
| 2261 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2262 | #else // FEAT_X11 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2263 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2264 | static int |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2265 | get_x11_title(int test_only UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2266 | { |
| 2267 | return FALSE; |
| 2268 | } |
| 2269 | |
| 2270 | static int |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2271 | get_x11_icon(int test_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2272 | { |
| 2273 | if (!test_only) |
| 2274 | { |
| 2275 | if (STRNCMP(T_NAME, "builtin_", 8) == 0) |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 2276 | oldicon = vim_strsave(T_NAME + 8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2277 | else |
Bram Moolenaar | 20de1c2 | 2009-07-22 11:28:11 +0000 | [diff] [blame] | 2278 | oldicon = vim_strsave(T_NAME); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2279 | } |
| 2280 | return FALSE; |
| 2281 | } |
| 2282 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2283 | #endif // FEAT_X11 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2284 | |
| 2285 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2286 | mch_can_restore_title(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2287 | { |
| 2288 | return get_x11_title(TRUE); |
| 2289 | } |
| 2290 | |
| 2291 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2292 | mch_can_restore_icon(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2293 | { |
| 2294 | return get_x11_icon(TRUE); |
| 2295 | } |
| 2296 | |
| 2297 | /* |
| 2298 | * Set the window title and icon. |
| 2299 | */ |
| 2300 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2301 | mch_settitle(char_u *title, char_u *icon) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2302 | { |
| 2303 | int type = 0; |
| 2304 | static int recursive = 0; |
| 2305 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2306 | if (T_NAME == NULL) // no terminal name (yet) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2307 | return; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2308 | if (title == NULL && icon == NULL) // nothing to do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2309 | return; |
| 2310 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2311 | // When one of the X11 functions causes a deadly signal, we get here again |
| 2312 | // recursively. Avoid hanging then (something is probably locked). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2313 | if (recursive) |
| 2314 | return; |
| 2315 | ++recursive; |
| 2316 | |
| 2317 | /* |
| 2318 | * if the window ID and the display is known, we may use X11 calls |
| 2319 | */ |
| 2320 | #ifdef FEAT_X11 |
| 2321 | if (get_x11_windis() == OK) |
| 2322 | type = 1; |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 2323 | #endif |
| 2324 | #if defined(FEAT_GUI_PHOTON) \ |
Bram Moolenaar | b3f7406 | 2020-02-26 16:16:53 +0100 | [diff] [blame] | 2325 | || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2326 | if (gui.in_use) |
| 2327 | type = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2328 | #endif |
| 2329 | |
| 2330 | /* |
Bram Moolenaar | f82bac3 | 2010-07-25 22:30:20 +0200 | [diff] [blame] | 2331 | * 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] | 2332 | * than x11 calls, because the x11 calls don't always work |
| 2333 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2334 | if ((type || *T_TS != NUL) && title != NULL) |
| 2335 | { |
Bram Moolenaar | d8f0cef | 2018-08-19 22:20:16 +0200 | [diff] [blame] | 2336 | if (oldtitle_outdated) |
| 2337 | { |
| 2338 | oldtitle_outdated = FALSE; |
| 2339 | VIM_CLEAR(oldtitle); |
| 2340 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2341 | if (oldtitle == NULL |
| 2342 | #ifdef FEAT_GUI |
| 2343 | && !gui.in_use |
| 2344 | #endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2345 | ) // first call but not in GUI, save title |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2346 | (void)get_x11_title(FALSE); |
| 2347 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2348 | if (*T_TS != NUL) // it's OK if t_fs is empty |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2349 | term_settitle(title); |
| 2350 | #ifdef FEAT_X11 |
| 2351 | else |
| 2352 | # ifdef FEAT_GUI_GTK |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2353 | if (!gui.in_use) // don't do this if GTK+ is running |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2354 | # endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2355 | set_x11_title(title); // x11 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2356 | #endif |
Bram Moolenaar | b3f7406 | 2020-02-26 16:16:53 +0100 | [diff] [blame] | 2357 | #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \ |
Bram Moolenaar | 097148e | 2020-08-11 21:58:20 +0200 | [diff] [blame] | 2358 | || defined(FEAT_GUI_PHOTON) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2359 | else |
| 2360 | gui_mch_settitle(title, icon); |
| 2361 | #endif |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 2362 | unix_did_set_title = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | if ((type || *T_CIS != NUL) && icon != NULL) |
| 2366 | { |
| 2367 | if (oldicon == NULL |
| 2368 | #ifdef FEAT_GUI |
| 2369 | && !gui.in_use |
| 2370 | #endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2371 | ) // first call, save icon |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2372 | get_x11_icon(FALSE); |
| 2373 | |
| 2374 | if (*T_CIS != NUL) |
| 2375 | { |
Bram Moolenaar | faf626e | 2019-10-24 17:43:25 +0200 | [diff] [blame] | 2376 | out_str(T_CIS); // set icon start |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2377 | out_str_nf(icon); |
Bram Moolenaar | faf626e | 2019-10-24 17:43:25 +0200 | [diff] [blame] | 2378 | out_str(T_CIE); // set icon end |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2379 | out_flush(); |
| 2380 | } |
| 2381 | #ifdef FEAT_X11 |
| 2382 | else |
| 2383 | # ifdef FEAT_GUI_GTK |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2384 | if (!gui.in_use) // don't do this if GTK+ is running |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2385 | # endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2386 | set_x11_icon(icon); // x11 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2387 | #endif |
| 2388 | did_set_icon = TRUE; |
| 2389 | } |
| 2390 | --recursive; |
| 2391 | } |
| 2392 | |
| 2393 | /* |
| 2394 | * Restore the window/icon title. |
| 2395 | * "which" is one of: |
Bram Moolenaar | 40385db | 2018-08-07 22:31:44 +0200 | [diff] [blame] | 2396 | * SAVE_RESTORE_TITLE only restore title |
| 2397 | * SAVE_RESTORE_ICON only restore icon |
| 2398 | * SAVE_RESTORE_BOTH restore title and icon |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2399 | */ |
| 2400 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2401 | mch_restore_title(int which) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2402 | { |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 2403 | int do_push_pop = unix_did_set_title || did_set_icon; |
Bram Moolenaar | e5c8328 | 2019-05-03 23:15:37 +0200 | [diff] [blame] | 2404 | |
Yegappan Lakshmanan | 14113fd | 2023-03-07 17:13:51 +0000 | [diff] [blame] | 2405 | // Only restore the title or icon when it has been set. |
| 2406 | // When using "oldtitle" make a copy, it might be freed halfway. |
| 2407 | char_u *title = ((which & SAVE_RESTORE_TITLE) && unix_did_set_title) |
| 2408 | ? (oldtitle ? oldtitle : p_titleold) : NULL; |
| 2409 | char_u *tofree = NULL; |
| 2410 | if (title == oldtitle && oldtitle != NULL) |
| 2411 | { |
| 2412 | tofree = vim_strsave(title); |
| 2413 | if (tofree != NULL) |
| 2414 | title = tofree; |
| 2415 | } |
| 2416 | mch_settitle(title, |
Bram Moolenaar | 40385db | 2018-08-07 22:31:44 +0200 | [diff] [blame] | 2417 | ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL); |
Yegappan Lakshmanan | 14113fd | 2023-03-07 17:13:51 +0000 | [diff] [blame] | 2418 | vim_free(tofree); |
Bram Moolenaar | 40385db | 2018-08-07 22:31:44 +0200 | [diff] [blame] | 2419 | |
Bram Moolenaar | e5c8328 | 2019-05-03 23:15:37 +0200 | [diff] [blame] | 2420 | if (do_push_pop) |
| 2421 | { |
| 2422 | // pop and push from/to the stack |
| 2423 | term_pop_title(which); |
| 2424 | term_push_title(which); |
| 2425 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2428 | |
| 2429 | /* |
| 2430 | * Return TRUE if "name" looks like some xterm name. |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 2431 | * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc. |
Bram Moolenaar | afa3f1c | 2022-12-19 18:56:48 +0000 | [diff] [blame] | 2432 | * Do not consider "xterm-kitty" an xterm, it is not fully xterm compatible, |
| 2433 | * using the "xterm-kitty" terminfo entry should work better. |
Bram Moolenaar | 3a7c85b | 2005-02-05 21:39:53 +0000 | [diff] [blame] | 2434 | * Seiichi Sato mentioned that "mlterm" works like xterm. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2435 | */ |
| 2436 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2437 | vim_is_xterm(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2438 | { |
| 2439 | if (name == NULL) |
| 2440 | return FALSE; |
Bram Moolenaar | afa3f1c | 2022-12-19 18:56:48 +0000 | [diff] [blame] | 2441 | return ((STRNICMP(name, "xterm", 5) == 0 |
| 2442 | && STRNICMP(name, "xterm-kitty", 11) != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2443 | || STRNICMP(name, "nxterm", 6) == 0 |
| 2444 | || STRNICMP(name, "kterm", 5) == 0 |
Bram Moolenaar | 3a7c85b | 2005-02-05 21:39:53 +0000 | [diff] [blame] | 2445 | || STRNICMP(name, "mlterm", 6) == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2446 | || STRNICMP(name, "rxvt", 4) == 0 |
Bram Moolenaar | 995e4af | 2017-09-01 20:24:03 +0200 | [diff] [blame] | 2447 | || STRNICMP(name, "screen.xterm", 12) == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2448 | || STRCMP(name, "builtin_xterm") == 0); |
| 2449 | } |
| 2450 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2451 | #if defined(FEAT_MOUSE_XTERM) || defined(PROTO) |
| 2452 | /* |
| 2453 | * Return TRUE if "name" appears to be that of a terminal |
| 2454 | * known to support the xterm-style mouse protocol. |
| 2455 | * Relies on term_is_xterm having been set to its correct value. |
| 2456 | */ |
| 2457 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2458 | use_xterm_like_mouse(char_u *name) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2459 | { |
| 2460 | return (name != NULL |
Bram Moolenaar | e56132b | 2016-08-14 18:23:21 +0200 | [diff] [blame] | 2461 | && (term_is_xterm |
| 2462 | || STRNICMP(name, "screen", 6) == 0 |
Bram Moolenaar | 0ba4070 | 2016-10-12 14:50:54 +0200 | [diff] [blame] | 2463 | || STRNICMP(name, "tmux", 4) == 0 |
Bram Moolenaar | 48873ae | 2021-12-08 21:00:24 +0000 | [diff] [blame] | 2464 | || STRNICMP(name, "gnome", 5) == 0 |
Bram Moolenaar | e56132b | 2016-08-14 18:23:21 +0200 | [diff] [blame] | 2465 | || STRICMP(name, "st") == 0 |
| 2466 | || STRNICMP(name, "st-", 3) == 0 |
| 2467 | || STRNICMP(name, "stterm", 6) == 0)); |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2468 | } |
| 2469 | #endif |
| 2470 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2471 | /* |
| 2472 | * Return non-zero when using an xterm mouse, according to 'ttymouse'. |
| 2473 | * Return 1 for "xterm". |
| 2474 | * Return 2 for "xterm2". |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 2475 | * Return 3 for "urxvt". |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 2476 | * Return 4 for "sgr". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2477 | */ |
| 2478 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2479 | use_xterm_mouse(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2480 | { |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 2481 | if (ttym_flags == TTYM_SGR) |
| 2482 | return 4; |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 2483 | if (ttym_flags == TTYM_URXVT) |
| 2484 | return 3; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2485 | if (ttym_flags == TTYM_XTERM2) |
| 2486 | return 2; |
| 2487 | if (ttym_flags == TTYM_XTERM) |
| 2488 | return 1; |
| 2489 | return 0; |
| 2490 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2491 | |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 2492 | /* |
| 2493 | * Return TRUE if "name" is an iris-ansi terminal name. |
| 2494 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2495 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2496 | vim_is_iris(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2497 | { |
| 2498 | if (name == NULL) |
| 2499 | return FALSE; |
| 2500 | return (STRNICMP(name, "iris-ansi", 9) == 0 |
| 2501 | || STRCMP(name, "builtin_iris-ansi") == 0); |
| 2502 | } |
| 2503 | |
Dominique Pelle | e764d1b | 2023-03-12 21:20:59 +0000 | [diff] [blame] | 2504 | #if defined(VMS) || defined(PROTO) |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 2505 | /* |
| 2506 | * Return TRUE if "name" is a vt300-like terminal name. |
| 2507 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2508 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2509 | vim_is_vt300(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2510 | { |
| 2511 | if (name == NULL) |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 2512 | return FALSE; |
| 2513 | // Actually all ANSI compatible terminals should be here. |
| 2514 | // Catch at least VT1xx - VT5xx |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 2515 | return ((STRNICMP(name, "vt", 2) == 0 |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 2516 | && vim_strchr((char_u *)"12345", name[2]) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2517 | || STRCMP(name, "builtin_vt320") == 0); |
| 2518 | } |
Dominique Pelle | e764d1b | 2023-03-12 21:20:59 +0000 | [diff] [blame] | 2519 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2520 | |
| 2521 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2522 | * Insert user name in s[len]. |
| 2523 | * Return OK if a name found. |
| 2524 | */ |
| 2525 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2526 | mch_get_user_name(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2527 | { |
| 2528 | #ifdef VMS |
Bram Moolenaar | ffb8ab0 | 2005-09-07 21:15:32 +0000 | [diff] [blame] | 2529 | vim_strncpy(s, (char_u *)cuserid(NULL), len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2530 | return OK; |
| 2531 | #else |
| 2532 | return mch_get_uname(getuid(), s, len); |
| 2533 | #endif |
| 2534 | } |
| 2535 | |
| 2536 | /* |
| 2537 | * Insert user name for "uid" in s[len]. |
| 2538 | * Return OK if a name found. |
| 2539 | */ |
| 2540 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2541 | mch_get_uname(uid_t uid, char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2542 | { |
| 2543 | #if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID) |
| 2544 | struct passwd *pw; |
| 2545 | |
| 2546 | if ((pw = getpwuid(uid)) != NULL |
| 2547 | && pw->pw_name != NULL && *(pw->pw_name) != NUL) |
| 2548 | { |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 2549 | vim_strncpy(s, (char_u *)pw->pw_name, len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2550 | return OK; |
| 2551 | } |
| 2552 | #endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2553 | sprintf((char *)s, "%d", (int)uid); // assumes s is long enough |
| 2554 | return FAIL; // a number is not a name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | /* |
| 2558 | * Insert host name is s[len]. |
| 2559 | */ |
| 2560 | |
| 2561 | #ifdef HAVE_SYS_UTSNAME_H |
| 2562 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2563 | mch_get_host_name(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2564 | { |
| 2565 | struct utsname vutsname; |
| 2566 | |
| 2567 | if (uname(&vutsname) < 0) |
| 2568 | *s = NUL; |
| 2569 | else |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 2570 | vim_strncpy(s, (char_u *)vutsname.nodename, len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2571 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2572 | #else // HAVE_SYS_UTSNAME_H |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2573 | |
| 2574 | # ifdef HAVE_SYS_SYSTEMINFO_H |
| 2575 | # define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len) |
| 2576 | # endif |
| 2577 | |
| 2578 | void |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 2579 | mch_get_host_name(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2580 | { |
| 2581 | # ifdef VAXC |
| 2582 | vaxc$gethostname((char *)s, len); |
| 2583 | # else |
| 2584 | gethostname((char *)s, len); |
| 2585 | # endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2586 | s[len - 1] = NUL; // make sure it's terminated |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2587 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2588 | #endif // HAVE_SYS_UTSNAME_H |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2589 | |
| 2590 | /* |
| 2591 | * return process ID |
| 2592 | */ |
| 2593 | long |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2594 | mch_get_pid(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2595 | { |
| 2596 | return (long)getpid(); |
| 2597 | } |
| 2598 | |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 2599 | /* |
| 2600 | * return TRUE if process "pid" is still running |
| 2601 | */ |
| 2602 | int |
Bram Moolenaar | 1b243ea | 2019-04-28 22:50:40 +0200 | [diff] [blame] | 2603 | mch_process_running(long pid) |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 2604 | { |
Bram Moolenaar | 44dea9d | 2021-06-23 21:13:20 +0200 | [diff] [blame] | 2605 | // If there is no error the process must be running. |
| 2606 | if (kill(pid, 0) == 0) |
| 2607 | return TRUE; |
| 2608 | #ifdef ESRCH |
| 2609 | // If the error is ESRCH then the process is not running. |
| 2610 | if (errno == ESRCH) |
| 2611 | return FALSE; |
| 2612 | #endif |
| 2613 | // If the process is running and owned by another user we get EPERM. With |
| 2614 | // other errors the process might be running, assuming it is then. |
| 2615 | return TRUE; |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 2616 | } |
| 2617 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2618 | #if !defined(HAVE_STRERROR) && defined(USE_GETCWD) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2619 | static char * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2620 | strerror(int err) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2621 | { |
| 2622 | extern int sys_nerr; |
| 2623 | extern char *sys_errlist[]; |
| 2624 | static char er[20]; |
| 2625 | |
| 2626 | if (err > 0 && err < sys_nerr) |
| 2627 | return (sys_errlist[err]); |
| 2628 | sprintf(er, "Error %d", err); |
| 2629 | return er; |
| 2630 | } |
| 2631 | #endif |
| 2632 | |
| 2633 | /* |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 2634 | * Get name of current directory into buffer "buf" of length "len" bytes. |
| 2635 | * "len" must be at least PATH_MAX. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2636 | * Return OK for success, FAIL for failure. |
| 2637 | */ |
| 2638 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2639 | mch_dirname(char_u *buf, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2640 | { |
| 2641 | #if defined(USE_GETCWD) |
| 2642 | if (getcwd((char *)buf, len) == NULL) |
| 2643 | { |
| 2644 | STRCPY(buf, strerror(errno)); |
| 2645 | return FAIL; |
| 2646 | } |
| 2647 | return OK; |
| 2648 | #else |
| 2649 | return (getwd((char *)buf) != NULL ? OK : FAIL); |
| 2650 | #endif |
| 2651 | } |
| 2652 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2653 | /* |
Bram Moolenaar | 3d20ca1 | 2006-11-28 16:43:58 +0000 | [diff] [blame] | 2654 | * Get absolute file name into "buf[len]". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2655 | * |
| 2656 | * return FAIL for failure, OK for success |
| 2657 | */ |
| 2658 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2659 | mch_FullName( |
| 2660 | char_u *fname, |
| 2661 | char_u *buf, |
| 2662 | int len, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2663 | int force) // also expand when already absolute path |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2664 | { |
| 2665 | int l; |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2666 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2667 | int fd = -1; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2668 | static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2669 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2670 | char_u olddir[MAXPATHL]; |
| 2671 | char_u *p; |
| 2672 | int retval = OK; |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2673 | #ifdef __CYGWIN__ |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2674 | char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but |
| 2675 | // it's not always defined |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2676 | #endif |
| 2677 | |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2678 | #ifdef VMS |
| 2679 | fname = vms_fixfilename(fname); |
| 2680 | #endif |
| 2681 | |
Bram Moolenaar | a244243 | 2007-04-26 14:26:37 +0000 | [diff] [blame] | 2682 | #ifdef __CYGWIN__ |
| 2683 | /* |
| 2684 | * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts". |
| 2685 | */ |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2686 | # if CYGWIN_VERSION_DLL_MAJOR >= 1007 |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2687 | // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in |
| 2688 | // a forward slash. |
Bram Moolenaar | 06b0734 | 2015-12-31 22:26:28 +0100 | [diff] [blame] | 2689 | cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE, |
| 2690 | fname, posix_fname, MAXPATHL); |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2691 | # else |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2692 | cygwin_conv_to_posix_path(fname, posix_fname); |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2693 | # endif |
Bram Moolenaar | bf82072 | 2008-06-21 11:12:49 +0000 | [diff] [blame] | 2694 | fname = posix_fname; |
Bram Moolenaar | a244243 | 2007-04-26 14:26:37 +0000 | [diff] [blame] | 2695 | #endif |
| 2696 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2697 | // Expand it if forced or not an absolute path. |
| 2698 | // Do not do it for "/file", the result is always "/". |
Bram Moolenaar | e3303cb | 2015-12-31 18:29:46 +0100 | [diff] [blame] | 2699 | if ((force || !mch_isFullName(fname)) |
| 2700 | && ((p = vim_strrchr(fname, '/')) == NULL || p != fname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2701 | { |
zeertzjq | 1ee7420 | 2024-07-12 07:29:14 +0200 | [diff] [blame] | 2702 | if (p == NULL && STRCMP(fname, "..") == 0) |
| 2703 | // Handle ".." without path separators. |
| 2704 | p = fname + 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2705 | /* |
| 2706 | * If the file name has a path, change to that directory for a moment, |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 2707 | * and then get the directory (and get back to where we were). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2708 | * This will get the correct path name with "../" things. |
| 2709 | */ |
Bram Moolenaar | e3303cb | 2015-12-31 18:29:46 +0100 | [diff] [blame] | 2710 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2711 | { |
Bram Moolenaar | 4eaef99 | 2021-08-30 21:26:16 +0200 | [diff] [blame] | 2712 | if (STRCMP(p, "/..") == 0) |
zeertzjq | 1ee7420 | 2024-07-12 07:29:14 +0200 | [diff] [blame] | 2713 | // For "/path/dir/.." include the "/..". |
Bram Moolenaar | 4eaef99 | 2021-08-30 21:26:16 +0200 | [diff] [blame] | 2714 | p += 3; |
| 2715 | |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2716 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2717 | /* |
| 2718 | * Use fchdir() if possible, it's said to be faster and more |
| 2719 | * reliable. But on SunOS 4 it might not work. Check this by |
| 2720 | * doing a fchdir() right now. |
| 2721 | */ |
| 2722 | if (!dont_fchdir) |
| 2723 | { |
| 2724 | fd = open(".", O_RDONLY | O_EXTRA, 0); |
| 2725 | if (fd >= 0 && fchdir(fd) < 0) |
| 2726 | { |
| 2727 | close(fd); |
| 2728 | fd = -1; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2729 | dont_fchdir = TRUE; // don't try again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2730 | } |
| 2731 | } |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2732 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2733 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2734 | // Only change directory when we are sure we can return to where |
| 2735 | // we are now. After doing "su" chdir(".") might not work. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2736 | if ( |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2737 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2738 | fd < 0 && |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2739 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2740 | (mch_dirname(olddir, MAXPATHL) == FAIL |
| 2741 | || mch_chdir((char *)olddir) != 0)) |
| 2742 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2743 | p = NULL; // can't get current dir: don't chdir |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2744 | retval = FAIL; |
| 2745 | } |
| 2746 | else |
| 2747 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2748 | // The directory is copied into buf[], to be able to remove |
| 2749 | // the file name without changing it (could be a string in |
| 2750 | // read-only memory) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2751 | if (p - fname >= len) |
| 2752 | retval = FAIL; |
| 2753 | else |
| 2754 | { |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 2755 | vim_strncpy(buf, fname, p - fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2756 | if (mch_chdir((char *)buf)) |
Bram Moolenaar | c6376c7 | 2021-10-03 19:29:48 +0100 | [diff] [blame] | 2757 | { |
| 2758 | // Path does not exist (yet). For a full path fail, |
| 2759 | // will use the path as-is. For a relative path use |
| 2760 | // the current directory and append the file name. |
| 2761 | if (mch_isFullName(fname)) |
| 2762 | retval = FAIL; |
| 2763 | else |
| 2764 | p = NULL; |
| 2765 | } |
Bram Moolenaar | 4eaef99 | 2021-08-30 21:26:16 +0200 | [diff] [blame] | 2766 | else if (*p == '/') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2767 | fname = p + 1; |
Bram Moolenaar | 4eaef99 | 2021-08-30 21:26:16 +0200 | [diff] [blame] | 2768 | else |
| 2769 | fname = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2770 | *buf = NUL; |
| 2771 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2772 | } |
| 2773 | } |
| 2774 | if (mch_dirname(buf, len) == FAIL) |
| 2775 | { |
| 2776 | retval = FAIL; |
| 2777 | *buf = NUL; |
| 2778 | } |
| 2779 | if (p != NULL) |
| 2780 | { |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2781 | #ifdef HAVE_FCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2782 | if (fd >= 0) |
| 2783 | { |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 2784 | if (p_verbose >= 5) |
| 2785 | { |
| 2786 | verbose_enter(); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 2787 | msg("fchdir() to previous dir"); |
Bram Moolenaar | 2572492 | 2009-07-14 15:38:41 +0000 | [diff] [blame] | 2788 | verbose_leave(); |
| 2789 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2790 | l = fchdir(fd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2791 | } |
| 2792 | else |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2793 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2794 | l = mch_chdir((char *)olddir); |
| 2795 | if (l != 0) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 2796 | emsg(_(e_cannot_go_back_to_previous_directory)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2797 | } |
itchyny | 051a40c | 2021-10-20 10:00:05 +0100 | [diff] [blame] | 2798 | #ifdef HAVE_FCHDIR |
| 2799 | if (fd >= 0) |
| 2800 | close(fd); |
| 2801 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2802 | |
| 2803 | l = STRLEN(buf); |
Bram Moolenaar | dac7569 | 2012-10-14 04:35:45 +0200 | [diff] [blame] | 2804 | if (l >= len - 1) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2805 | retval = FAIL; // no space for trailing "/" |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2806 | #ifndef VMS |
Bram Moolenaar | dac7569 | 2012-10-14 04:35:45 +0200 | [diff] [blame] | 2807 | else if (l > 0 && buf[l - 1] != '/' && *fname != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2808 | && STRCMP(fname, ".") != 0) |
Bram Moolenaar | dac7569 | 2012-10-14 04:35:45 +0200 | [diff] [blame] | 2809 | STRCAT(buf, "/"); |
Bram Moolenaar | 38323e4 | 2007-03-06 19:22:53 +0000 | [diff] [blame] | 2810 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2811 | } |
Bram Moolenaar | 3d20ca1 | 2006-11-28 16:43:58 +0000 | [diff] [blame] | 2812 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2813 | // Catch file names which are too long. |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 2814 | if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2815 | return FAIL; |
| 2816 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2817 | // Do not append ".", "/dir/." is equal to "/dir". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2818 | if (STRCMP(fname, ".") != 0) |
| 2819 | STRCAT(buf, fname); |
| 2820 | |
| 2821 | return OK; |
| 2822 | } |
| 2823 | |
| 2824 | /* |
| 2825 | * Return TRUE if "fname" does not depend on the current directory. |
| 2826 | */ |
| 2827 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2828 | mch_isFullName(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2829 | { |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 2830 | #ifdef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2831 | return ( fname[0] == '/' || fname[0] == '.' || |
| 2832 | strchr((char *)fname,':') || strchr((char *)fname,'"') || |
| 2833 | (strchr((char *)fname,'[') && strchr((char *)fname,']'))|| |
| 2834 | (strchr((char *)fname,'<') && strchr((char *)fname,'>')) ); |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 2835 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2836 | return (*fname == '/' || *fname == '~'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2837 | #endif |
| 2838 | } |
| 2839 | |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2840 | #if defined(USE_FNAME_CASE) || defined(PROTO) |
| 2841 | /* |
| 2842 | * Set the case of the file name, if it already exists. This will cause the |
| 2843 | * file name to remain exactly the same. |
Bram Moolenaar | c2a27c3 | 2007-12-01 16:19:33 +0000 | [diff] [blame] | 2844 | * Only required for file systems where case is ignored and preserved. |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2845 | */ |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2846 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2847 | fname_case( |
| 2848 | char_u *name, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2849 | int len UNUSED) // buffer size, only used when name gets longer |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2850 | { |
| 2851 | struct stat st; |
| 2852 | char_u *slash, *tail; |
| 2853 | DIR *dirp; |
| 2854 | struct dirent *dp; |
| 2855 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2856 | if (mch_lstat((char *)name, &st) < 0) |
| 2857 | return; |
| 2858 | |
| 2859 | // Open the directory where the file is located. |
| 2860 | slash = vim_strrchr(name, '/'); |
| 2861 | if (slash == NULL) |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2862 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2863 | dirp = opendir("."); |
| 2864 | tail = name; |
| 2865 | } |
| 2866 | else |
| 2867 | { |
| 2868 | *slash = NUL; |
| 2869 | dirp = opendir((char *)name); |
| 2870 | *slash = '/'; |
| 2871 | tail = slash + 1; |
| 2872 | } |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2873 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2874 | if (dirp == NULL) |
| 2875 | return; |
| 2876 | |
| 2877 | while ((dp = readdir(dirp)) != NULL) |
| 2878 | { |
| 2879 | // Only accept names that differ in case and are the same byte |
| 2880 | // length. TODO: accept different length name. |
| 2881 | if (STRICMP(tail, dp->d_name) == 0 |
| 2882 | && STRLEN(tail) == STRLEN(dp->d_name)) |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2883 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2884 | char_u newname[MAXPATHL + 1]; |
| 2885 | struct stat st2; |
| 2886 | |
| 2887 | // Verify the inode is equal. |
| 2888 | vim_strncpy(newname, name, MAXPATHL); |
| 2889 | vim_strncpy(newname + (tail - name), (char_u *)dp->d_name, |
| 2890 | MAXPATHL - (tail - name)); |
| 2891 | if (mch_lstat((char *)newname, &st2) >= 0 |
| 2892 | && st.st_ino == st2.st_ino |
| 2893 | && st.st_dev == st2.st_dev) |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2894 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2895 | STRCPY(tail, dp->d_name); |
| 2896 | break; |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2897 | } |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2898 | } |
| 2899 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2900 | |
| 2901 | closedir(dirp); |
Bram Moolenaar | 24552be | 2005-12-10 20:17:30 +0000 | [diff] [blame] | 2902 | } |
| 2903 | #endif |
| 2904 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2905 | /* |
| 2906 | * Get file permissions for 'name'. |
| 2907 | * Returns -1 when it doesn't exist. |
| 2908 | */ |
| 2909 | long |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2910 | mch_getperm(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2911 | { |
| 2912 | struct stat statb; |
| 2913 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2914 | // Keep the #ifdef outside of stat(), it may be a macro. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2915 | #ifdef VMS |
| 2916 | if (stat((char *)vms_fixfilename(name), &statb)) |
| 2917 | #else |
| 2918 | if (stat((char *)name, &statb)) |
| 2919 | #endif |
| 2920 | return -1; |
Bram Moolenaar | 708f62c | 2007-08-11 20:24:10 +0000 | [diff] [blame] | 2921 | #ifdef __INTERIX |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 2922 | // The top bit makes the value negative, which means the file doesn't |
| 2923 | // exist. Remove the bit, we don't use it. |
Bram Moolenaar | 708f62c | 2007-08-11 20:24:10 +0000 | [diff] [blame] | 2924 | return statb.st_mode & ~S_ADDACE; |
| 2925 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2926 | return statb.st_mode; |
Bram Moolenaar | 708f62c | 2007-08-11 20:24:10 +0000 | [diff] [blame] | 2927 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | /* |
Bram Moolenaar | cd142e3 | 2017-11-16 17:03:45 +0100 | [diff] [blame] | 2931 | * Set file permission for "name" to "perm". |
| 2932 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2933 | */ |
| 2934 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2935 | mch_setperm(char_u *name, long perm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2936 | { |
| 2937 | return (chmod((char *) |
| 2938 | #ifdef VMS |
| 2939 | vms_fixfilename(name), |
| 2940 | #else |
| 2941 | name, |
| 2942 | #endif |
| 2943 | (mode_t)perm) == 0 ? OK : FAIL); |
| 2944 | } |
| 2945 | |
Bram Moolenaar | cd142e3 | 2017-11-16 17:03:45 +0100 | [diff] [blame] | 2946 | #if defined(HAVE_FCHMOD) || defined(PROTO) |
| 2947 | /* |
| 2948 | * Set file permission for open file "fd" to "perm". |
| 2949 | * Return FAIL for failure, OK otherwise. |
| 2950 | */ |
| 2951 | int |
| 2952 | mch_fsetperm(int fd, long perm) |
| 2953 | { |
| 2954 | return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL); |
| 2955 | } |
| 2956 | #endif |
| 2957 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2958 | #if defined(HAVE_ACL) || defined(PROTO) |
| 2959 | # ifdef HAVE_SYS_ACL_H |
| 2960 | # include <sys/acl.h> |
| 2961 | # endif |
| 2962 | # ifdef HAVE_SYS_ACCESS_H |
| 2963 | # include <sys/access.h> |
| 2964 | # endif |
| 2965 | |
| 2966 | # ifdef HAVE_SOLARIS_ACL |
| 2967 | typedef struct vim_acl_solaris_T { |
| 2968 | int acl_cnt; |
| 2969 | aclent_t *acl_entry; |
| 2970 | } vim_acl_solaris_T; |
| 2971 | # endif |
| 2972 | |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 2973 | #if defined(HAVE_SELINUX) || defined(PROTO) |
| 2974 | /* |
| 2975 | * Copy security info from "from_file" to "to_file". |
| 2976 | */ |
| 2977 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2978 | mch_copy_sec(char_u *from_file, char_u *to_file) |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 2979 | { |
| 2980 | if (from_file == NULL) |
| 2981 | return; |
| 2982 | |
| 2983 | if (selinux_enabled == -1) |
| 2984 | selinux_enabled = is_selinux_enabled(); |
| 2985 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2986 | if (selinux_enabled <= 0) |
| 2987 | return; |
| 2988 | |
| 2989 | // Use "char *" instead of "security_context_t" to avoid a deprecation |
| 2990 | // warning. |
| 2991 | char *from_context = NULL; |
| 2992 | char *to_context = NULL; |
| 2993 | |
| 2994 | if (getfilecon((char *)from_file, &from_context) < 0) |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 2995 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 2996 | // If the filesystem doesn't support extended attributes, |
| 2997 | // the original had no special security context and the |
| 2998 | // target cannot have one either. |
| 2999 | if (errno == EOPNOTSUPP) |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 3000 | return; |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 3001 | |
| 3002 | msg_puts(_("\nCould not get security context for ")); |
| 3003 | msg_outtrans(from_file); |
| 3004 | msg_putchar('\n'); |
| 3005 | return; |
| 3006 | } |
| 3007 | if (getfilecon((char *)to_file, &to_context) < 0) |
| 3008 | { |
| 3009 | msg_puts(_("\nCould not get security context for ")); |
| 3010 | msg_outtrans(to_file); |
| 3011 | msg_putchar('\n'); |
| 3012 | freecon (from_context); |
| 3013 | return ; |
| 3014 | } |
| 3015 | if (strcmp(from_context, to_context) != 0) |
| 3016 | { |
| 3017 | if (setfilecon((char *)to_file, from_context) < 0) |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 3018 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 3019 | msg_puts(_("\nCould not set security context for ")); |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 3020 | msg_outtrans(to_file); |
| 3021 | msg_putchar('\n'); |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 3022 | } |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 3023 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 3024 | freecon(to_context); |
| 3025 | freecon(from_context); |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 3026 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3027 | #endif // HAVE_SELINUX |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 3028 | |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3029 | #if defined(HAVE_SMACK) && !defined(PROTO) |
| 3030 | /* |
| 3031 | * Copy security info from "from_file" to "to_file". |
| 3032 | */ |
| 3033 | void |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 3034 | mch_copy_sec(char_u *from_file, char_u *to_file) |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3035 | { |
Bram Moolenaar | 62f167f | 2014-04-23 12:52:40 +0200 | [diff] [blame] | 3036 | static const char * const smack_copied_attributes[] = |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3037 | { |
| 3038 | XATTR_NAME_SMACK, |
| 3039 | XATTR_NAME_SMACKEXEC, |
| 3040 | XATTR_NAME_SMACKMMAP |
| 3041 | }; |
| 3042 | |
| 3043 | char buffer[SMACK_LABEL_LEN]; |
| 3044 | const char *name; |
| 3045 | int index; |
| 3046 | int ret; |
| 3047 | ssize_t size; |
| 3048 | |
| 3049 | if (from_file == NULL) |
| 3050 | return; |
| 3051 | |
Christian Brabandt | 5a679b2 | 2023-10-16 10:17:13 +0200 | [diff] [blame] | 3052 | size = listxattr((char *)from_file, NULL, 0); |
| 3053 | // not supported or no attributes to copy |
Paul R. Tagliamonte | 14759de | 2024-03-10 08:35:10 +0100 | [diff] [blame] | 3054 | if (size <= 0) |
Christian Brabandt | 5a679b2 | 2023-10-16 10:17:13 +0200 | [diff] [blame] | 3055 | return; |
| 3056 | |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3057 | for (index = 0 ; index < (int)(sizeof(smack_copied_attributes) |
| 3058 | / sizeof(smack_copied_attributes)[0]) ; index++) |
| 3059 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3060 | // get the name of the attribute to copy |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3061 | name = smack_copied_attributes[index]; |
| 3062 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3063 | // get the value of the attribute in buffer |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3064 | size = getxattr((char*)from_file, name, buffer, sizeof(buffer)); |
| 3065 | if (size >= 0) |
| 3066 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3067 | // copy the attribute value of buffer |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3068 | ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0); |
| 3069 | if (ret < 0) |
| 3070 | { |
Bram Moolenaar | 4a1314c | 2016-01-27 20:47:18 +0100 | [diff] [blame] | 3071 | vim_snprintf((char *)IObuff, IOSIZE, |
| 3072 | _("Could not set security context %s for %s"), |
| 3073 | name, to_file); |
| 3074 | msg_outtrans(IObuff); |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3075 | msg_putchar('\n'); |
| 3076 | } |
| 3077 | } |
| 3078 | else |
| 3079 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3080 | // what reason of not having the attribute value? |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3081 | switch (errno) |
| 3082 | { |
| 3083 | case ENOTSUP: |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3084 | // extended attributes aren't supported or enabled |
| 3085 | // should a message be echoed? not sure... |
| 3086 | return; // leave because it isn't useful to continue |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3087 | |
| 3088 | case ERANGE: |
| 3089 | default: |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3090 | // no enough size OR unexpected error |
Bram Moolenaar | 4a1314c | 2016-01-27 20:47:18 +0100 | [diff] [blame] | 3091 | vim_snprintf((char *)IObuff, IOSIZE, |
| 3092 | _("Could not get security context %s for %s. Removing it!"), |
| 3093 | name, from_file); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3094 | msg_puts((char *)IObuff); |
Bram Moolenaar | 4a1314c | 2016-01-27 20:47:18 +0100 | [diff] [blame] | 3095 | msg_putchar('\n'); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3096 | // FALLTHROUGH to remove the attribute |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3097 | |
| 3098 | case ENODATA: |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3099 | // no attribute of this name |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3100 | ret = removexattr((char*)to_file, name); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3101 | // Silently ignore errors, apparently this happens when |
| 3102 | // smack is not actually being used. |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3103 | break; |
| 3104 | } |
| 3105 | } |
| 3106 | } |
| 3107 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3108 | #endif // HAVE_SMACK |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 3109 | |
Christian Brabandt | e085dfd | 2023-09-30 12:49:18 +0200 | [diff] [blame] | 3110 | #ifdef FEAT_XATTR |
| 3111 | /* |
| 3112 | * Copy extended attributes from_file to to_file |
| 3113 | */ |
| 3114 | void |
| 3115 | mch_copy_xattr(char_u *from_file, char_u *to_file) |
| 3116 | { |
| 3117 | char *xattr_buf; |
Paul R. Tagliamonte | 14759de | 2024-03-10 08:35:10 +0100 | [diff] [blame] | 3118 | ssize_t size; |
| 3119 | ssize_t tsize; |
Christian Brabandt | e085dfd | 2023-09-30 12:49:18 +0200 | [diff] [blame] | 3120 | ssize_t keylen, vallen, max_vallen = 0; |
| 3121 | char *key; |
| 3122 | char *val = NULL; |
| 3123 | char *errmsg = NULL; |
| 3124 | |
| 3125 | if (from_file == NULL) |
| 3126 | return; |
| 3127 | |
| 3128 | // get the length of the extended attributes |
| 3129 | size = listxattr((char *)from_file, NULL, 0); |
| 3130 | // not supported or no attributes to copy |
Paul R. Tagliamonte | 14759de | 2024-03-10 08:35:10 +0100 | [diff] [blame] | 3131 | if (size <= 0) |
Christian Brabandt | e085dfd | 2023-09-30 12:49:18 +0200 | [diff] [blame] | 3132 | return; |
| 3133 | xattr_buf = (char*)alloc(size); |
| 3134 | if (xattr_buf == NULL) |
| 3135 | return; |
| 3136 | size = listxattr((char *)from_file, xattr_buf, size); |
| 3137 | tsize = size; |
| 3138 | |
| 3139 | errno = 0; |
| 3140 | |
| 3141 | for (int round = 0; round < 2; round++) |
| 3142 | { |
| 3143 | |
| 3144 | key = xattr_buf; |
| 3145 | if (round == 1) |
| 3146 | size = tsize; |
| 3147 | |
| 3148 | while (size > 0) |
| 3149 | { |
| 3150 | vallen = getxattr((char *)from_file, key, |
| 3151 | val, round ? max_vallen : 0); |
| 3152 | // only set the attribute in the second round |
| 3153 | if (vallen >= 0 && round && |
| 3154 | setxattr((char *)to_file, key, val, vallen, 0) == 0) |
| 3155 | ; |
| 3156 | else if (errno) |
| 3157 | { |
| 3158 | switch (errno) |
| 3159 | { |
| 3160 | case E2BIG: |
| 3161 | errmsg = e_xattr_e2big; |
| 3162 | goto error_exit; |
| 3163 | case ENOTSUP: |
Gene C | 993b175 | 2023-10-02 22:42:26 +0200 | [diff] [blame] | 3164 | case EACCES: |
| 3165 | case EPERM: |
| 3166 | break; |
Christian Brabandt | e085dfd | 2023-09-30 12:49:18 +0200 | [diff] [blame] | 3167 | case ERANGE: |
| 3168 | errmsg = e_xattr_erange; |
| 3169 | goto error_exit; |
| 3170 | default: |
| 3171 | errmsg = e_xattr_other; |
| 3172 | goto error_exit; |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | if (round == 0 && vallen > max_vallen) |
| 3177 | max_vallen = vallen; |
| 3178 | |
| 3179 | // add one for terminating null |
| 3180 | keylen = STRLEN(key) + 1; |
| 3181 | size -= keylen; |
| 3182 | key += keylen; |
| 3183 | } |
| 3184 | if (round) |
| 3185 | break; |
| 3186 | |
| 3187 | val = (char*)alloc(max_vallen + 1); |
| 3188 | if (val == NULL) |
| 3189 | goto error_exit; |
| 3190 | |
| 3191 | } |
| 3192 | error_exit: |
| 3193 | vim_free(xattr_buf); |
| 3194 | vim_free(val); |
| 3195 | |
| 3196 | if (errmsg != NULL) |
zeertzjq | 7ece036 | 2023-10-01 09:07:14 +0200 | [diff] [blame] | 3197 | emsg(_(errmsg)); |
Christian Brabandt | e085dfd | 2023-09-30 12:49:18 +0200 | [diff] [blame] | 3198 | } |
| 3199 | #endif |
| 3200 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3201 | /* |
| 3202 | * Return a pointer to the ACL of file "fname" in allocated memory. |
| 3203 | * Return NULL if the ACL is not available for whatever reason. |
| 3204 | */ |
| 3205 | vim_acl_T |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3206 | mch_get_acl(char_u *fname UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3207 | { |
| 3208 | vim_acl_T ret = NULL; |
| 3209 | #ifdef HAVE_POSIX_ACL |
| 3210 | ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS); |
| 3211 | #else |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 3212 | #ifdef HAVE_SOLARIS_ZFS_ACL |
| 3213 | acl_t *aclent; |
| 3214 | |
| 3215 | if (acl_get((char *)fname, 0, &aclent) < 0) |
| 3216 | return NULL; |
| 3217 | ret = (vim_acl_T)aclent; |
| 3218 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3219 | #ifdef HAVE_SOLARIS_ACL |
| 3220 | vim_acl_solaris_T *aclent; |
| 3221 | |
| 3222 | aclent = malloc(sizeof(vim_acl_solaris_T)); |
| 3223 | if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0) |
| 3224 | { |
| 3225 | free(aclent); |
| 3226 | return NULL; |
| 3227 | } |
| 3228 | aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t)); |
| 3229 | if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0) |
| 3230 | { |
| 3231 | free(aclent->acl_entry); |
| 3232 | free(aclent); |
| 3233 | return NULL; |
| 3234 | } |
| 3235 | ret = (vim_acl_T)aclent; |
| 3236 | #else |
| 3237 | #if defined(HAVE_AIX_ACL) |
| 3238 | int aclsize; |
| 3239 | struct acl *aclent; |
| 3240 | |
| 3241 | aclsize = sizeof(struct acl); |
| 3242 | aclent = malloc(aclsize); |
| 3243 | if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0) |
| 3244 | { |
| 3245 | if (errno == ENOSPC) |
| 3246 | { |
| 3247 | aclsize = aclent->acl_len; |
| 3248 | aclent = realloc(aclent, aclsize); |
| 3249 | if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0) |
| 3250 | { |
| 3251 | free(aclent); |
| 3252 | return NULL; |
| 3253 | } |
| 3254 | } |
| 3255 | else |
| 3256 | { |
| 3257 | free(aclent); |
| 3258 | return NULL; |
| 3259 | } |
| 3260 | } |
| 3261 | ret = (vim_acl_T)aclent; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3262 | #endif // HAVE_AIX_ACL |
| 3263 | #endif // HAVE_SOLARIS_ACL |
| 3264 | #endif // HAVE_SOLARIS_ZFS_ACL |
| 3265 | #endif // HAVE_POSIX_ACL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3266 | return ret; |
| 3267 | } |
| 3268 | |
| 3269 | /* |
| 3270 | * Set the ACL of file "fname" to "acl" (unless it's NULL). |
| 3271 | */ |
| 3272 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3273 | mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3274 | { |
| 3275 | if (aclent == NULL) |
| 3276 | return; |
| 3277 | #ifdef HAVE_POSIX_ACL |
| 3278 | acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent); |
| 3279 | #else |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 3280 | #ifdef HAVE_SOLARIS_ZFS_ACL |
| 3281 | acl_set((char *)fname, (acl_t *)aclent); |
| 3282 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3283 | #ifdef HAVE_SOLARIS_ACL |
| 3284 | acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt, |
| 3285 | ((vim_acl_solaris_T *)aclent)->acl_entry); |
| 3286 | #else |
| 3287 | #ifdef HAVE_AIX_ACL |
| 3288 | chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3289 | #endif // HAVE_AIX_ACL |
| 3290 | #endif // HAVE_SOLARIS_ACL |
| 3291 | #endif // HAVE_SOLARIS_ZFS_ACL |
| 3292 | #endif // HAVE_POSIX_ACL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3296 | mch_free_acl(vim_acl_T aclent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3297 | { |
| 3298 | if (aclent == NULL) |
| 3299 | return; |
| 3300 | #ifdef HAVE_POSIX_ACL |
| 3301 | acl_free((acl_t)aclent); |
| 3302 | #else |
Bram Moolenaar | 8d462f9 | 2012-02-05 22:51:33 +0100 | [diff] [blame] | 3303 | #ifdef HAVE_SOLARIS_ZFS_ACL |
| 3304 | acl_free((acl_t *)aclent); |
| 3305 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3306 | #ifdef HAVE_SOLARIS_ACL |
| 3307 | free(((vim_acl_solaris_T *)aclent)->acl_entry); |
| 3308 | free(aclent); |
| 3309 | #else |
| 3310 | #ifdef HAVE_AIX_ACL |
| 3311 | free(aclent); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3312 | #endif // HAVE_AIX_ACL |
| 3313 | #endif // HAVE_SOLARIS_ACL |
| 3314 | #endif // HAVE_SOLARIS_ZFS_ACL |
| 3315 | #endif // HAVE_POSIX_ACL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3316 | } |
| 3317 | #endif |
| 3318 | |
| 3319 | /* |
| 3320 | * Set hidden flag for "name". |
| 3321 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3322 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3323 | mch_hide(char_u *name UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3324 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3325 | // can't hide a file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | /* |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3329 | * 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] | 3330 | * return FALSE if "name" is not a directory |
| 3331 | * return FALSE for error |
| 3332 | */ |
| 3333 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3334 | mch_isdir(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3335 | { |
| 3336 | struct stat statb; |
| 3337 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3338 | if (*name == NUL) // Some stat()s don't flag "" as an error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3339 | return FALSE; |
| 3340 | if (stat((char *)name, &statb)) |
| 3341 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3342 | return (S_ISDIR(statb.st_mode) ? TRUE : FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3345 | /* |
| 3346 | * return TRUE if "name" is a directory, NOT a symlink to a directory |
| 3347 | * return FALSE if "name" is not a directory |
| 3348 | * return FALSE for error |
| 3349 | */ |
| 3350 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3351 | mch_isrealdir(char_u *name) |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3352 | { |
| 3353 | struct stat statb; |
| 3354 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3355 | if (*name == NUL) // Some stat()s don't flag "" as an error. |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3356 | return FALSE; |
Bram Moolenaar | de5e2c2 | 2016-11-04 20:35:31 +0100 | [diff] [blame] | 3357 | if (mch_lstat((char *)name, &statb)) |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3358 | return FALSE; |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3359 | return (S_ISDIR(statb.st_mode) ? TRUE : FALSE); |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 3360 | } |
| 3361 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3362 | /* |
| 3363 | * Return 1 if "name" is an executable file, 0 if not or it doesn't exist. |
| 3364 | */ |
| 3365 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3366 | executable_file(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3367 | { |
| 3368 | struct stat st; |
| 3369 | |
| 3370 | if (stat((char *)name, &st)) |
| 3371 | return 0; |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 3372 | #ifdef VMS |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3373 | // Like on Unix system file can have executable rights but not necessarily |
| 3374 | // be an executable, but on Unix is not a default for an ordinary file to |
| 3375 | // have an executable flag - on VMS it is in most cases. |
| 3376 | // Therefore, this check does not have any sense - let keep us to the |
| 3377 | // conventions instead: |
| 3378 | // *.COM and *.EXE files are the executables - the rest are not. This is |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 3379 | // not ideal but better than it was. |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 3380 | int vms_executable = 0; |
| 3381 | if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0) |
| 3382 | { |
| 3383 | if (strstr(vms_tolower((char*)name),".exe") != NULL |
| 3384 | || strstr(vms_tolower((char*)name),".com")!= NULL) |
| 3385 | vms_executable = 1; |
| 3386 | } |
| 3387 | return vms_executable; |
| 3388 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3389 | 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] | 3390 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | /* |
Bram Moolenaar | 2fcf668 | 2017-03-11 20:03:42 +0100 | [diff] [blame] | 3394 | * Return TRUE if "name" can be found in $PATH and executed, FALSE if not. |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 3395 | * If "use_path" is FALSE only check if "name" is executable. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3396 | * Return -1 if unknown. |
| 3397 | */ |
| 3398 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3399 | mch_can_exe(char_u *name, char_u **path, int use_path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3400 | { |
| 3401 | char_u *buf; |
| 3402 | char_u *p, *e; |
| 3403 | int retval; |
| 3404 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3405 | // When "use_path" is false and if it's an absolute or relative path don't |
| 3406 | // need to use $PATH. |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 3407 | if (!use_path || gettail(name) != name) |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 3408 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3409 | // There must be a path separator, files in the current directory |
| 3410 | // can't be executed. |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 3411 | if ((use_path || gettail(name) != name) && executable_file(name)) |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3412 | { |
| 3413 | if (path != NULL) |
| 3414 | { |
Bram Moolenaar | 4366319 | 2017-03-05 14:29:12 +0100 | [diff] [blame] | 3415 | if (name[0] != '/') |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3416 | *path = FullName_save(name, TRUE); |
| 3417 | else |
| 3418 | *path = vim_strsave(name); |
| 3419 | } |
| 3420 | return TRUE; |
| 3421 | } |
| 3422 | return FALSE; |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 3423 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3424 | |
| 3425 | p = (char_u *)getenv("PATH"); |
| 3426 | if (p == NULL || *p == NUL) |
| 3427 | return -1; |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 3428 | buf = alloc(STRLEN(name) + STRLEN(p) + 2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3429 | if (buf == NULL) |
| 3430 | return -1; |
| 3431 | |
| 3432 | /* |
| 3433 | * Walk through all entries in $PATH to check if "name" exists there and |
| 3434 | * is an executable file. |
| 3435 | */ |
| 3436 | for (;;) |
| 3437 | { |
| 3438 | e = (char_u *)strchr((char *)p, ':'); |
| 3439 | if (e == NULL) |
| 3440 | e = p + STRLEN(p); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3441 | if (e - p <= 1) // empty entry means current dir |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3442 | STRCPY(buf, "./"); |
| 3443 | else |
| 3444 | { |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 3445 | vim_strncpy(buf, p, e - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3446 | add_pathsep(buf); |
| 3447 | } |
| 3448 | STRCAT(buf, name); |
| 3449 | retval = executable_file(buf); |
| 3450 | if (retval == 1) |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3451 | { |
| 3452 | if (path != NULL) |
| 3453 | { |
Bram Moolenaar | 4366319 | 2017-03-05 14:29:12 +0100 | [diff] [blame] | 3454 | if (buf[0] != '/') |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3455 | *path = FullName_save(buf, TRUE); |
| 3456 | else |
| 3457 | *path = vim_strsave(buf); |
| 3458 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3459 | break; |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 3460 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3461 | |
| 3462 | if (*e != ':') |
| 3463 | break; |
| 3464 | p = e + 1; |
| 3465 | } |
| 3466 | |
| 3467 | vim_free(buf); |
| 3468 | return retval; |
| 3469 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3470 | |
| 3471 | /* |
| 3472 | * Check what "name" is: |
| 3473 | * NODE_NORMAL: file or directory (or doesn't exist) |
| 3474 | * NODE_WRITABLE: writable device, socket, fifo, etc. |
| 3475 | * NODE_OTHER: non-writable things |
| 3476 | */ |
| 3477 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3478 | mch_nodetype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3479 | { |
| 3480 | struct stat st; |
| 3481 | |
| 3482 | if (stat((char *)name, &st)) |
| 3483 | return NODE_NORMAL; |
| 3484 | if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) |
| 3485 | return NODE_NORMAL; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3486 | if (S_ISBLK(st.st_mode)) // block device isn't writable |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3487 | return NODE_OTHER; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3488 | // Everything else is writable? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3489 | return NODE_WRITABLE; |
| 3490 | } |
| 3491 | |
| 3492 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3493 | mch_early_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3494 | { |
| 3495 | #ifdef HAVE_CHECK_STACK_GROWTH |
| 3496 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3497 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3498 | check_stack_growth((char *)&i); |
| 3499 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3500 | # ifdef HAVE_STACK_LIMIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3501 | get_stack_limit(); |
| 3502 | # endif |
| 3503 | |
| 3504 | #endif |
| 3505 | |
| 3506 | /* |
| 3507 | * Setup an alternative stack for signals. Helps to catch signals when |
| 3508 | * running out of stack space. |
| 3509 | * Use of sigaltstack() is preferred, it's more portable. |
| 3510 | * Ignore any errors. |
| 3511 | */ |
| 3512 | #if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
Zdenek Dohnal | ba9c23e | 2021-08-11 14:20:05 +0200 | [diff] [blame] | 3513 | signal_stack = alloc(get_signal_stack_size()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3514 | init_signal_stack(); |
| 3515 | #endif |
| 3516 | } |
| 3517 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3518 | #if defined(EXITFREE) || defined(PROTO) |
| 3519 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3520 | mch_free_mem(void) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3521 | { |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3522 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
| 3523 | if (clip_star.owned) |
| 3524 | clip_lose_selection(&clip_star); |
| 3525 | if (clip_plus.owned) |
| 3526 | clip_lose_selection(&clip_plus); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3527 | # endif |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3528 | # if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3529 | if (xterm_Shell != (Widget)0) |
| 3530 | XtDestroyWidget(xterm_Shell); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3531 | # ifndef LESSTIF_VERSION |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3532 | // Lesstif crashes here, lose some memory |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3533 | if (xterm_dpy != NULL) |
| 3534 | XtCloseDisplay(xterm_dpy); |
| 3535 | if (app_context != (XtAppContext)NULL) |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3536 | { |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3537 | XtDestroyApplicationContext(app_context); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3538 | # ifdef FEAT_X11 |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3539 | x11_display = NULL; // freed by XtDestroyApplicationContext() |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3540 | # endif |
| 3541 | } |
| 3542 | # endif |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3543 | # endif |
Bram Moolenaar | 0eda7ac | 2010-06-26 05:38:18 +0200 | [diff] [blame] | 3544 | # if defined(FEAT_X11) |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 3545 | if (x11_display != NULL |
| 3546 | # ifdef FEAT_XCLIPBOARD |
| 3547 | && x11_display != xterm_dpy |
| 3548 | # endif |
| 3549 | ) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3550 | XCloseDisplay(x11_display); |
| 3551 | # endif |
| 3552 | # if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3553 | VIM_CLEAR(signal_stack); |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3554 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3555 | vim_free(oldtitle); |
| 3556 | vim_free(oldicon); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3557 | } |
| 3558 | #endif |
| 3559 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3560 | /* |
| 3561 | * Output a newline when exiting. |
| 3562 | * Make sure the newline goes to the same stream as the text. |
| 3563 | */ |
| 3564 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3565 | exit_scroll(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3566 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3567 | if (silent_mode) |
| 3568 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3569 | if (newline_on_exit || msg_didout) |
| 3570 | { |
| 3571 | if (msg_use_printf()) |
| 3572 | { |
| 3573 | if (info_message) |
| 3574 | mch_msg("\n"); |
| 3575 | else |
| 3576 | mch_errmsg("\r\n"); |
| 3577 | } |
| 3578 | else |
| 3579 | out_char('\n'); |
| 3580 | } |
Bram Moolenaar | 7007e31 | 2021-03-27 12:11:33 +0100 | [diff] [blame] | 3581 | else if (!is_not_a_term()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3582 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3583 | restore_cterm_colors(); // get original colors back |
| 3584 | msg_clr_eos_force(); // clear the rest of the display |
| 3585 | windgoto((int)Rows - 1, 0); // may have moved the cursor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3586 | } |
| 3587 | } |
| 3588 | |
Bram Moolenaar | b415168 | 2020-05-11 22:13:28 +0200 | [diff] [blame] | 3589 | #ifdef USE_GCOV_FLUSH |
ichizok | dee78e1 | 2021-12-09 21:08:01 +0000 | [diff] [blame] | 3590 | # if (defined(__GNUC__) \ |
| 3591 | && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \ |
| 3592 | || (defined(__clang__) && (__clang_major__ >= 12)) |
| 3593 | extern void __gcov_dump(void); |
| 3594 | extern void __gcov_reset(void); |
| 3595 | # define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0) |
| 3596 | # else |
| 3597 | extern void __gcov_flush(void); |
| 3598 | # endif |
Bram Moolenaar | b415168 | 2020-05-11 22:13:28 +0200 | [diff] [blame] | 3599 | #endif |
| 3600 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3601 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3602 | mch_exit(int r) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3603 | { |
| 3604 | exiting = TRUE; |
| 3605 | |
| 3606 | #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD) |
| 3607 | x11_export_final_selection(); |
| 3608 | #endif |
| 3609 | |
| 3610 | #ifdef FEAT_GUI |
| 3611 | if (!gui.in_use) |
| 3612 | #endif |
| 3613 | { |
| 3614 | settmode(TMODE_COOK); |
Bram Moolenaar | 7007e31 | 2021-03-27 12:11:33 +0100 | [diff] [blame] | 3615 | if (!is_not_a_term()) |
| 3616 | { |
| 3617 | // restore xterm title and icon name |
| 3618 | mch_restore_title(SAVE_RESTORE_BOTH); |
| 3619 | term_pop_title(SAVE_RESTORE_BOTH); |
| 3620 | } |
Bram Moolenaar | 651fca8 | 2021-11-29 20:39:38 +0000 | [diff] [blame] | 3621 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3622 | /* |
| 3623 | * When t_ti is not empty but it doesn't cause swapping terminal |
| 3624 | * pages, need to output a newline when msg_didout is set. But when |
| 3625 | * t_ti does swap pages it should not go to the shell page. Do this |
| 3626 | * before stoptermcap(). |
| 3627 | */ |
| 3628 | if (swapping_screen() && !newline_on_exit) |
| 3629 | exit_scroll(); |
| 3630 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3631 | // Stop termcap: May need to check for T_CRV response, which |
| 3632 | // requires RAW mode. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3633 | stoptermcap(); |
| 3634 | |
| 3635 | /* |
| 3636 | * A newline is only required after a message in the alternate screen. |
| 3637 | * This is set to TRUE by wait_return(). |
| 3638 | */ |
| 3639 | if (!swapping_screen() || newline_on_exit) |
| 3640 | exit_scroll(); |
| 3641 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3642 | // Cursor may have been switched off without calling starttermcap() |
| 3643 | // when doing "vim -u vimrc" and vimrc contains ":q". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3644 | if (full_screen) |
| 3645 | cursor_on(); |
| 3646 | } |
| 3647 | out_flush(); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3648 | ml_close_all(TRUE); // remove all memfiles |
Bram Moolenaar | b415168 | 2020-05-11 22:13:28 +0200 | [diff] [blame] | 3649 | |
| 3650 | #ifdef USE_GCOV_FLUSH |
| 3651 | // Flush coverage info before possibly being killed by a deadly signal. |
| 3652 | __gcov_flush(); |
| 3653 | #endif |
| 3654 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3655 | may_core_dump(); |
| 3656 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3657 | if (gui.in_use) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3658 | gui_exit(r); |
| 3659 | #endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3660 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 3661 | #ifdef MACOS_CONVERT |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3662 | mac_conv_cleanup(); |
| 3663 | #endif |
| 3664 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3665 | #ifdef __QNX__ |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3666 | // A core dump won't be created if the signal handler |
| 3667 | // doesn't return, so we can't call exit() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3668 | if (deadly_signal != 0) |
| 3669 | return; |
| 3670 | #endif |
| 3671 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3672 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 3673 | netbeans_send_disconnect(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3674 | #endif |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 3675 | |
| 3676 | #ifdef EXITFREE |
| 3677 | free_all_mem(); |
| 3678 | #endif |
| 3679 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3680 | exit(r); |
| 3681 | } |
| 3682 | |
| 3683 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3684 | may_core_dump(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3685 | { |
| 3686 | if (deadly_signal != 0) |
| 3687 | { |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 3688 | mch_signal(deadly_signal, SIG_DFL); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3689 | kill(getpid(), deadly_signal); // Die using the signal we caught |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | #ifndef VMS |
| 3694 | |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 3695 | /* |
| 3696 | * Get the file descriptor to use for tty operations. |
| 3697 | */ |
| 3698 | static int |
| 3699 | get_tty_fd(int fd) |
| 3700 | { |
| 3701 | int tty_fd = fd; |
| 3702 | |
| 3703 | #if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM) |
| 3704 | // On SunOS: Get the terminal parameters from "fd", or the slave device of |
| 3705 | // "fd" when it is a master device. |
| 3706 | if (mch_isatty(fd) > 1) |
| 3707 | { |
| 3708 | char *name; |
| 3709 | |
| 3710 | name = ptsname(fd); |
| 3711 | if (name == NULL) |
| 3712 | return -1; |
| 3713 | |
| 3714 | tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0); |
| 3715 | if (tty_fd < 0) |
| 3716 | return -1; |
| 3717 | } |
| 3718 | #endif |
| 3719 | return tty_fd; |
| 3720 | } |
| 3721 | |
| 3722 | static int |
| 3723 | mch_tcgetattr(int fd, void *term) |
| 3724 | { |
| 3725 | int tty_fd; |
| 3726 | int retval = -1; |
| 3727 | |
| 3728 | tty_fd = get_tty_fd(fd); |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 3729 | if (tty_fd < 0) |
| 3730 | return -1; |
| 3731 | |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 3732 | #ifdef NEW_TTY_SYSTEM |
| 3733 | # ifdef HAVE_TERMIOS_H |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 3734 | retval = tcgetattr(tty_fd, (struct termios *)term); |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 3735 | # else |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 3736 | retval = ioctl(tty_fd, TCGETA, (struct termio *)term); |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 3737 | # endif |
| 3738 | #else |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 3739 | // for "old" tty systems |
| 3740 | retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term); |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 3741 | #endif |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 3742 | if (tty_fd != fd) |
| 3743 | close(tty_fd); |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 3744 | return retval; |
| 3745 | } |
| 3746 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3747 | void |
Bram Moolenaar | 26e8644 | 2020-05-17 14:06:16 +0200 | [diff] [blame] | 3748 | mch_settmode(tmode_T tmode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3749 | { |
| 3750 | static int first = TRUE; |
| 3751 | |
Bram Moolenaar | 4f44b88 | 2017-08-13 20:06:18 +0200 | [diff] [blame] | 3752 | #ifdef NEW_TTY_SYSTEM |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3753 | # ifdef HAVE_TERMIOS_H |
| 3754 | static struct termios told; |
| 3755 | struct termios tnew; |
| 3756 | # else |
| 3757 | static struct termio told; |
| 3758 | struct termio tnew; |
| 3759 | # endif |
| 3760 | |
| 3761 | if (first) |
| 3762 | { |
| 3763 | first = FALSE; |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 3764 | mch_tcgetattr(read_cmd_fd, &told); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | tnew = told; |
| 3768 | if (tmode == TMODE_RAW) |
| 3769 | { |
Bram Moolenaar | 041c710 | 2020-05-30 18:14:57 +0200 | [diff] [blame] | 3770 | // ~ICRNL enables typing ^V^M |
Bram Moolenaar | 928eec6 | 2020-05-31 13:09:47 +0200 | [diff] [blame] | 3771 | // ~IXON disables CTRL-S stopping output, so that it can be mapped. |
Anton Sharonov | 49528da | 2024-04-14 20:02:24 +0200 | [diff] [blame] | 3772 | tnew.c_iflag &= ~(ICRNL | |
| 3773 | (T_XON == NULL || *T_XON == NUL ? IXON : 0)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3774 | tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE |
Bram Moolenaar | e3f915d | 2020-07-14 23:02:44 +0200 | [diff] [blame] | 3775 | # if defined(IEXTEN) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3776 | | IEXTEN // IEXTEN enables typing ^V on SOLARIS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3777 | # endif |
| 3778 | ); |
Bram Moolenaar | faf626e | 2019-10-24 17:43:25 +0200 | [diff] [blame] | 3779 | # ifdef ONLCR |
| 3780 | // Don't map NL -> CR NL, we do it ourselves. |
| 3781 | // Also disable expanding tabs if possible. |
| 3782 | # ifdef XTABS |
| 3783 | tnew.c_oflag &= ~(ONLCR | XTABS); |
| 3784 | # else |
| 3785 | # ifdef TAB3 |
| 3786 | tnew.c_oflag &= ~(ONLCR | TAB3); |
| 3787 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3788 | tnew.c_oflag &= ~ONLCR; |
Bram Moolenaar | faf626e | 2019-10-24 17:43:25 +0200 | [diff] [blame] | 3789 | # endif |
| 3790 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3791 | # endif |
Bram Moolenaar | faf626e | 2019-10-24 17:43:25 +0200 | [diff] [blame] | 3792 | tnew.c_cc[VMIN] = 1; // return after 1 char |
| 3793 | tnew.c_cc[VTIME] = 0; // don't wait |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3794 | } |
| 3795 | else if (tmode == TMODE_SLEEP) |
Bram Moolenaar | 40de456 | 2016-07-01 15:03:46 +0200 | [diff] [blame] | 3796 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3797 | // Also reset ICANON here, otherwise on Solaris select() won't see |
| 3798 | // typeahead characters. |
Bram Moolenaar | 40de456 | 2016-07-01 15:03:46 +0200 | [diff] [blame] | 3799 | tnew.c_lflag &= ~(ICANON | ECHO); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3800 | tnew.c_cc[VMIN] = 1; // return after 1 char |
| 3801 | tnew.c_cc[VTIME] = 0; // don't wait |
Bram Moolenaar | 40de456 | 2016-07-01 15:03:46 +0200 | [diff] [blame] | 3802 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3803 | |
| 3804 | # if defined(HAVE_TERMIOS_H) |
| 3805 | { |
| 3806 | int n = 10; |
| 3807 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3808 | // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a |
| 3809 | // few times. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3810 | while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1 |
| 3811 | && errno == EINTR && n > 0) |
| 3812 | --n; |
| 3813 | } |
| 3814 | # else |
| 3815 | ioctl(read_cmd_fd, TCSETA, &tnew); |
| 3816 | # endif |
| 3817 | |
| 3818 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3819 | /* |
| 3820 | * for "old" tty systems |
| 3821 | */ |
| 3822 | # ifndef TIOCSETN |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3823 | # define TIOCSETN TIOCSETP // for hpux 9.0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3824 | # endif |
| 3825 | static struct sgttyb ttybold; |
| 3826 | struct sgttyb ttybnew; |
| 3827 | |
| 3828 | if (first) |
| 3829 | { |
| 3830 | first = FALSE; |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 3831 | mch_tcgetattr(read_cmd_fd, &ttybold); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | ttybnew = ttybold; |
| 3835 | if (tmode == TMODE_RAW) |
| 3836 | { |
| 3837 | ttybnew.sg_flags &= ~(CRMOD | ECHO); |
| 3838 | ttybnew.sg_flags |= RAW; |
| 3839 | } |
| 3840 | else if (tmode == TMODE_SLEEP) |
| 3841 | ttybnew.sg_flags &= ~(ECHO); |
| 3842 | ioctl(read_cmd_fd, TIOCSETN, &ttybnew); |
| 3843 | #endif |
Bram Moolenaar | 26e8644 | 2020-05-17 14:06:16 +0200 | [diff] [blame] | 3844 | mch_cur_tmode = tmode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | /* |
| 3848 | * Try to get the code for "t_kb" from the stty setting |
| 3849 | * |
| 3850 | * Even if termcap claims a backspace key, the user's setting *should* |
| 3851 | * prevail. stty knows more about reality than termcap does, and if |
| 3852 | * somebody's usual erase key is DEL (which, for most BSD users, it will |
| 3853 | * be), they're going to get really annoyed if their erase key starts |
| 3854 | * doing forward deletes for no reason. (Eric Fischer) |
| 3855 | */ |
| 3856 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3857 | get_stty(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3858 | { |
Bram Moolenaar | 4f44b88 | 2017-08-13 20:06:18 +0200 | [diff] [blame] | 3859 | ttyinfo_T info; |
| 3860 | char_u buf[2]; |
| 3861 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3862 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 3863 | if (get_tty_info(read_cmd_fd, &info) != OK) |
| 3864 | return; |
Bram Moolenaar | 4f44b88 | 2017-08-13 20:06:18 +0200 | [diff] [blame] | 3865 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 3866 | intr_char = info.interrupt; |
| 3867 | buf[0] = info.backspace; |
| 3868 | buf[1] = NUL; |
| 3869 | add_termcode((char_u *)"kb", buf, FALSE); |
| 3870 | |
| 3871 | // If <BS> and <DEL> are now the same, redefine <DEL>. |
| 3872 | p = find_termcode((char_u *)"kD"); |
| 3873 | if (p != NULL && p[0] == buf[0] && p[1] == buf[1]) |
| 3874 | do_fixdel(NULL); |
Bram Moolenaar | 4f44b88 | 2017-08-13 20:06:18 +0200 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | /* |
| 3878 | * Obtain the characters that Backspace and Enter produce on "fd". |
| 3879 | * Returns OK or FAIL. |
| 3880 | */ |
| 3881 | int |
| 3882 | get_tty_info(int fd, ttyinfo_T *info) |
| 3883 | { |
| 3884 | #ifdef NEW_TTY_SYSTEM |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3885 | # ifdef HAVE_TERMIOS_H |
| 3886 | struct termios keys; |
| 3887 | # else |
| 3888 | struct termio keys; |
| 3889 | # endif |
| 3890 | |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 3891 | if (mch_tcgetattr(fd, &keys) != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3892 | { |
Bram Moolenaar | 4f44b88 | 2017-08-13 20:06:18 +0200 | [diff] [blame] | 3893 | info->backspace = keys.c_cc[VERASE]; |
| 3894 | info->interrupt = keys.c_cc[VINTR]; |
| 3895 | if (keys.c_iflag & ICRNL) |
| 3896 | info->enter = NL; |
| 3897 | else |
| 3898 | info->enter = CAR; |
| 3899 | if (keys.c_oflag & ONLCR) |
| 3900 | info->nl_does_cr = TRUE; |
| 3901 | else |
| 3902 | info->nl_does_cr = FALSE; |
| 3903 | return OK; |
| 3904 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3905 | #else |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3906 | // for "old" tty systems |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3907 | struct sgttyb keys; |
| 3908 | |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 3909 | if (mch_tcgetattr(fd, &keys) != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3910 | { |
Bram Moolenaar | 4f44b88 | 2017-08-13 20:06:18 +0200 | [diff] [blame] | 3911 | info->backspace = keys.sg_erase; |
| 3912 | info->interrupt = keys.sg_kill; |
| 3913 | info->enter = CAR; |
| 3914 | info->nl_does_cr = TRUE; |
| 3915 | return OK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3916 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3917 | #endif |
Bram Moolenaar | 4f44b88 | 2017-08-13 20:06:18 +0200 | [diff] [blame] | 3918 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3921 | #endif // VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3922 | |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3923 | static int mouse_ison = FALSE; |
| 3924 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3925 | /* |
Bram Moolenaar | b4ad3b0 | 2022-03-30 10:57:45 +0100 | [diff] [blame] | 3926 | * Set mouse clicks on or off and possible enable mouse movement events. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3927 | */ |
| 3928 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3929 | mch_setmouse(int on) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3930 | { |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3931 | #ifdef FEAT_BEVAL_TERM |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3932 | static int bevalterm_ison = FALSE; |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3933 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3934 | int xterm_mouse_vers; |
| 3935 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3936 | #if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) |
Bram Moolenaar | a06afc7 | 2018-08-27 23:24:16 +0200 | [diff] [blame] | 3937 | if (!on) |
| 3938 | // Make sure not tracing mouse movements. Important when a button-down |
| 3939 | // was received but no release yet. |
| 3940 | stop_xterm_trace(); |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3941 | #endif |
Bram Moolenaar | a06afc7 | 2018-08-27 23:24:16 +0200 | [diff] [blame] | 3942 | |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3943 | if (on == mouse_ison |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3944 | #ifdef FEAT_BEVAL_TERM |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3945 | && p_bevalterm == bevalterm_ison |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3946 | #endif |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3947 | ) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3948 | // return quickly if nothing to do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3949 | return; |
| 3950 | |
| 3951 | xterm_mouse_vers = use_xterm_mouse(); |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3952 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3953 | #ifdef FEAT_MOUSE_URXVT |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3954 | if (ttym_flags == TTYM_URXVT) |
| 3955 | { |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 3956 | out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l")); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3957 | mouse_ison = on; |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3958 | } |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3959 | #endif |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 3960 | |
Bram Moolenaar | 06cd14d | 2023-01-10 12:37:38 +0000 | [diff] [blame] | 3961 | if (T_CXM != NULL && *T_CXM != NUL) |
| 3962 | { |
| 3963 | term_enable_mouse(on); |
| 3964 | } |
| 3965 | else if (ttym_flags == TTYM_SGR) |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3966 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3967 | // SGR mode supports columns above 223 |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 3968 | out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l")); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3969 | mouse_ison = on; |
| 3970 | } |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3971 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3972 | #ifdef FEAT_BEVAL_TERM |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3973 | if (bevalterm_ison != (p_bevalterm && on)) |
| 3974 | { |
| 3975 | bevalterm_ison = (p_bevalterm && on); |
| 3976 | if (xterm_mouse_vers > 1 && !bevalterm_ison) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3977 | // disable mouse movement events, enabling is below |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 3978 | out_str_nf((char_u *)("\033[?1003l")); |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3979 | } |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3980 | #endif |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 3981 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3982 | if (xterm_mouse_vers > 0) |
| 3983 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3984 | if (on) // enable mouse events, use mouse tracking if available |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3985 | out_str_nf((char_u *) |
| 3986 | (xterm_mouse_vers > 1 |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3987 | ? ( |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3988 | #ifdef FEAT_BEVAL_TERM |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 3989 | bevalterm_ison ? "\033[?1003h" : |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3990 | #endif |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 3991 | "\033[?1002h") |
| 3992 | : "\033[?1000h")); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 3993 | else // disable mouse events, could probably always send the same |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3994 | out_str_nf((char_u *) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 3995 | (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l")); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3996 | mouse_ison = on; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 3999 | #ifdef FEAT_MOUSE_DEC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4000 | else if (ttym_flags == TTYM_DEC) |
| 4001 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4002 | if (on) // enable mouse events |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4003 | out_str_nf((char_u *)"\033[1;2'z\033[1;3'{"); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4004 | else // disable mouse events |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4005 | out_str_nf((char_u *)"\033['z"); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 4006 | mouse_ison = on; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4007 | } |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4008 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4009 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4010 | #ifdef FEAT_MOUSE_GPM |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4011 | else |
| 4012 | { |
| 4013 | if (on) |
| 4014 | { |
| 4015 | if (gpm_open()) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 4016 | mouse_ison = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4017 | } |
| 4018 | else |
| 4019 | { |
| 4020 | gpm_close(); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 4021 | mouse_ison = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4022 | } |
| 4023 | } |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4024 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4025 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4026 | #ifdef FEAT_SYSMOUSE |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 4027 | else |
| 4028 | { |
| 4029 | if (on) |
| 4030 | { |
| 4031 | if (sysmouse_open() == OK) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 4032 | mouse_ison = TRUE; |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 4033 | } |
| 4034 | else |
| 4035 | { |
| 4036 | sysmouse_close(); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 4037 | mouse_ison = FALSE; |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 4038 | } |
| 4039 | } |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4040 | #endif |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 4041 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4042 | #ifdef FEAT_MOUSE_JSB |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4043 | else |
| 4044 | { |
| 4045 | if (on) |
| 4046 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4047 | // D - Enable Mouse up/down messages |
| 4048 | // L - Enable Left Button Reporting |
| 4049 | // M - Enable Middle Button Reporting |
| 4050 | // R - Enable Right Button Reporting |
| 4051 | // K - Enable SHIFT and CTRL key Reporting |
| 4052 | // + - Enable Advanced messaging of mouse moves and up/down messages |
| 4053 | // Q - Quiet No Ack |
| 4054 | // # - Numeric value of mouse pointer required |
| 4055 | // 0 = Multiview 2000 cursor, used as standard |
| 4056 | // 1 = Windows Arrow |
| 4057 | // 2 = Windows I Beam |
| 4058 | // 3 = Windows Hour Glass |
| 4059 | // 4 = Windows Cross Hair |
| 4060 | // 5 = Windows UP Arrow |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4061 | # ifdef JSBTERM_MOUSE_NONADVANCED |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4062 | // Disables full feedback of pointer movements |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4063 | out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\"); |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4064 | # else |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4065 | out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\"); |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4066 | # endif |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 4067 | mouse_ison = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4068 | } |
| 4069 | else |
| 4070 | { |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4071 | out_str_nf((char_u *)"\033[0~ZwQ\033\\"); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 4072 | mouse_ison = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4073 | } |
| 4074 | } |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4075 | #endif |
| 4076 | #ifdef FEAT_MOUSE_PTERM |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4077 | else |
| 4078 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4079 | // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4080 | if (on) |
| 4081 | out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l"); |
| 4082 | else |
| 4083 | out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h"); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 4084 | mouse_ison = on; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4085 | } |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 4086 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
Bram Moolenaar | c3719bd | 2017-11-18 22:13:31 +0100 | [diff] [blame] | 4089 | #if defined(FEAT_BEVAL_TERM) || defined(PROTO) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 4090 | /* |
| 4091 | * Called when 'balloonevalterm' changed. |
| 4092 | */ |
| 4093 | void |
| 4094 | mch_bevalterm_changed(void) |
| 4095 | { |
| 4096 | mch_setmouse(mouse_ison); |
| 4097 | } |
| 4098 | #endif |
| 4099 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4100 | /* |
| 4101 | * Set the mouse termcode, depending on the 'term' and 'ttymouse' options. |
| 4102 | */ |
| 4103 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4104 | check_mouse_termcode(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4105 | { |
| 4106 | # ifdef FEAT_MOUSE_XTERM |
| 4107 | if (use_xterm_mouse() |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 4108 | # ifdef FEAT_MOUSE_URXVT |
| 4109 | && use_xterm_mouse() != 3 |
| 4110 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4111 | # ifdef FEAT_GUI |
| 4112 | && !gui.in_use |
| 4113 | # endif |
| 4114 | ) |
| 4115 | { |
| 4116 | set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4117 | ? "\233M" : "\033[M")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4118 | if (*p_mouse != NUL) |
| 4119 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4120 | // force mouse off and maybe on to send possibly new mouse |
| 4121 | // activation sequence to the xterm, with(out) drag tracing. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4122 | mch_setmouse(FALSE); |
| 4123 | setmouse(); |
| 4124 | } |
| 4125 | } |
| 4126 | else |
| 4127 | del_mouse_termcode(KS_MOUSE); |
| 4128 | # endif |
| 4129 | |
| 4130 | # ifdef FEAT_MOUSE_GPM |
| 4131 | if (!use_xterm_mouse() |
| 4132 | # ifdef FEAT_GUI |
| 4133 | && !gui.in_use |
| 4134 | # endif |
| 4135 | ) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4136 | set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG"); |
Bram Moolenaar | bedf091 | 2019-05-04 16:58:45 +0200 | [diff] [blame] | 4137 | else |
| 4138 | del_mouse_termcode(KS_GPM_MOUSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4139 | # endif |
| 4140 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 4141 | # ifdef FEAT_SYSMOUSE |
| 4142 | if (!use_xterm_mouse() |
| 4143 | # ifdef FEAT_GUI |
| 4144 | && !gui.in_use |
| 4145 | # endif |
| 4146 | ) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4147 | set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS"); |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 4148 | # endif |
| 4149 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4150 | # ifdef FEAT_MOUSE_JSB |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4151 | // Conflicts with xterm mouse: "\033[" and "\033[M" ??? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4152 | if (!use_xterm_mouse() |
| 4153 | # ifdef FEAT_GUI |
| 4154 | && !gui.in_use |
| 4155 | # endif |
| 4156 | ) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4157 | set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4158 | else |
| 4159 | del_mouse_termcode(KS_JSBTERM_MOUSE); |
| 4160 | # endif |
| 4161 | |
| 4162 | # ifdef FEAT_MOUSE_NET |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4163 | // There is no conflict, but one may type "ESC }" from Insert mode. Don't |
| 4164 | // define it in the GUI or when using an xterm. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4165 | if (!use_xterm_mouse() |
| 4166 | # ifdef FEAT_GUI |
| 4167 | && !gui.in_use |
| 4168 | # endif |
| 4169 | ) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4170 | set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4171 | else |
| 4172 | del_mouse_termcode(KS_NETTERM_MOUSE); |
| 4173 | # endif |
| 4174 | |
| 4175 | # ifdef FEAT_MOUSE_DEC |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4176 | // Conflicts with xterm mouse: "\033[" and "\033[M" |
Bram Moolenaar | cbc17d6 | 2014-05-22 21:22:19 +0200 | [diff] [blame] | 4177 | if (!use_xterm_mouse() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4178 | # ifdef FEAT_GUI |
| 4179 | && !gui.in_use |
| 4180 | # endif |
| 4181 | ) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4182 | set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4183 | ? "\233" : "\033[")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4184 | else |
| 4185 | del_mouse_termcode(KS_DEC_MOUSE); |
| 4186 | # endif |
| 4187 | # ifdef FEAT_MOUSE_PTERM |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4188 | // same conflict as the dec mouse |
Bram Moolenaar | cbc17d6 | 2014-05-22 21:22:19 +0200 | [diff] [blame] | 4189 | if (!use_xterm_mouse() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4190 | # ifdef FEAT_GUI |
| 4191 | && !gui.in_use |
| 4192 | # endif |
| 4193 | ) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4194 | set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033["); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4195 | else |
| 4196 | del_mouse_termcode(KS_PTERM_MOUSE); |
| 4197 | # endif |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 4198 | # ifdef FEAT_MOUSE_URXVT |
Bram Moolenaar | cbc17d6 | 2014-05-22 21:22:19 +0200 | [diff] [blame] | 4199 | if (use_xterm_mouse() == 3 |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 4200 | # ifdef FEAT_GUI |
| 4201 | && !gui.in_use |
| 4202 | # endif |
| 4203 | ) |
| 4204 | { |
| 4205 | set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4206 | ? "\233*M" : "\033[*M")); |
Bram Moolenaar | c842748 | 2011-10-20 21:09:35 +0200 | [diff] [blame] | 4207 | |
| 4208 | if (*p_mouse != NUL) |
| 4209 | { |
| 4210 | mch_setmouse(FALSE); |
| 4211 | setmouse(); |
| 4212 | } |
| 4213 | } |
| 4214 | else |
| 4215 | del_mouse_termcode(KS_URXVT_MOUSE); |
| 4216 | # endif |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 4217 | if (use_xterm_mouse() == 4 |
Bram Moolenaar | 2ace1bd | 2019-03-22 12:03:30 +0100 | [diff] [blame] | 4218 | # ifdef FEAT_GUI |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 4219 | && !gui.in_use |
Bram Moolenaar | 2ace1bd | 2019-03-22 12:03:30 +0100 | [diff] [blame] | 4220 | # endif |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 4221 | ) |
| 4222 | { |
| 4223 | set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4224 | ? "\233<*M" : "\033[<*M")); |
Bram Moolenaar | a529ce0 | 2017-06-22 22:37:57 +0200 | [diff] [blame] | 4225 | |
| 4226 | set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 4227 | ? "\233<*m" : "\033[<*m")); |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 4228 | |
| 4229 | if (*p_mouse != NUL) |
| 4230 | { |
| 4231 | mch_setmouse(FALSE); |
| 4232 | setmouse(); |
| 4233 | } |
| 4234 | } |
| 4235 | else |
Bram Moolenaar | a529ce0 | 2017-06-22 22:37:57 +0200 | [diff] [blame] | 4236 | { |
Bram Moolenaar | 2b9578f | 2012-08-15 16:21:32 +0200 | [diff] [blame] | 4237 | del_mouse_termcode(KS_SGR_MOUSE); |
Bram Moolenaar | a529ce0 | 2017-06-22 22:37:57 +0200 | [diff] [blame] | 4238 | del_mouse_termcode(KS_SGR_MOUSE_RELEASE); |
| 4239 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4240 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4241 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4242 | #ifndef VMS |
| 4243 | |
| 4244 | /* |
| 4245 | * Try to get the current window size: |
| 4246 | * 1. with an ioctl(), most accurate method |
| 4247 | * 2. from the environment variables LINES and COLUMNS |
| 4248 | * 3. from the termcap |
| 4249 | * 4. keep using the old values |
| 4250 | * Return OK when size could be determined, FAIL otherwise. |
| 4251 | */ |
| 4252 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4253 | mch_get_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4254 | { |
| 4255 | long rows = 0; |
| 4256 | long columns = 0; |
| 4257 | char_u *p; |
| 4258 | |
| 4259 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4260 | * 1. try using an ioctl. It is the most accurate method. |
| 4261 | * |
| 4262 | * Try using TIOCGWINSZ first, some systems that have it also define |
| 4263 | * TIOCGSIZE but don't have a struct ttysize. |
| 4264 | */ |
| 4265 | # ifdef TIOCGWINSZ |
| 4266 | { |
| 4267 | struct winsize ws; |
| 4268 | int fd = 1; |
| 4269 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4270 | // When stdout is not a tty, use stdin for the ioctl(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4271 | if (!isatty(fd) && isatty(read_cmd_fd)) |
| 4272 | fd = read_cmd_fd; |
| 4273 | if (ioctl(fd, TIOCGWINSZ, &ws) == 0) |
| 4274 | { |
| 4275 | columns = ws.ws_col; |
| 4276 | rows = ws.ws_row; |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 4277 | # ifdef FEAT_EVAL |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4278 | ch_log(NULL, "Got size with TIOCGWINSZ: %ld x %ld", columns, rows); |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 4279 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4280 | } |
| 4281 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4282 | # else // TIOCGWINSZ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4283 | # ifdef TIOCGSIZE |
| 4284 | { |
| 4285 | struct ttysize ts; |
| 4286 | int fd = 1; |
| 4287 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4288 | // When stdout is not a tty, use stdin for the ioctl(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4289 | if (!isatty(fd) && isatty(read_cmd_fd)) |
| 4290 | fd = read_cmd_fd; |
| 4291 | if (ioctl(fd, TIOCGSIZE, &ts) == 0) |
| 4292 | { |
| 4293 | columns = ts.ts_cols; |
| 4294 | rows = ts.ts_lines; |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 4295 | # ifdef FEAT_EVAL |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4296 | ch_log(NULL, "Got size with TIOCGSIZE: %ld x %ld", columns, rows); |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 4297 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4298 | } |
| 4299 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4300 | # endif // TIOCGSIZE |
| 4301 | # endif // TIOCGWINSZ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4302 | |
| 4303 | /* |
| 4304 | * 2. get size from environment |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 4305 | * When being POSIX compliant ('|' flag in 'cpoptions') this overrules |
| 4306 | * the ioctl() values! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4307 | */ |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 4308 | if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4309 | { |
| 4310 | if ((p = (char_u *)getenv("LINES"))) |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4311 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4312 | rows = atoi((char *)p); |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 4313 | # ifdef FEAT_EVAL |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4314 | ch_log(NULL, "Got 'lines' from $LINES: %ld", rows); |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 4315 | # endif |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4316 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4317 | if ((p = (char_u *)getenv("COLUMNS"))) |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4318 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4319 | columns = atoi((char *)p); |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 4320 | # ifdef FEAT_EVAL |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4321 | ch_log(NULL, "Got 'columns' from $COLUMNS: %ld", columns); |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 4322 | # endif |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4323 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | #ifdef HAVE_TGETENT |
| 4327 | /* |
| 4328 | * 3. try reading "co" and "li" entries from termcap |
| 4329 | */ |
| 4330 | if (columns == 0 || rows == 0) |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4331 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4332 | getlinecol(&columns, &rows); |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 4333 | # ifdef FEAT_EVAL |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4334 | ch_log(NULL, "Got size from termcap: %ld x %ld", columns, rows); |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 4335 | # endif |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4336 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4337 | #endif |
| 4338 | |
| 4339 | /* |
| 4340 | * 4. If everything fails, use the old values |
| 4341 | */ |
| 4342 | if (columns <= 0 || rows <= 0) |
| 4343 | return FAIL; |
| 4344 | |
| 4345 | Rows = rows; |
| 4346 | Columns = columns; |
Bram Moolenaar | e057d40 | 2013-06-30 17:51:51 +0200 | [diff] [blame] | 4347 | limit_screen_size(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4348 | return OK; |
| 4349 | } |
| 4350 | |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 4351 | #if defined(FEAT_TERMINAL) || defined(PROTO) |
| 4352 | /* |
| 4353 | * Report the windows size "rows" and "cols" to tty "fd". |
| 4354 | */ |
| 4355 | int |
| 4356 | mch_report_winsize(int fd, int rows, int cols) |
| 4357 | { |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 4358 | int tty_fd; |
| 4359 | int retval = -1; |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 4360 | |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 4361 | tty_fd = get_tty_fd(fd); |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 4362 | if (tty_fd < 0) |
| 4363 | return FAIL; |
| 4364 | |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 4365 | # if defined(TIOCSWINSZ) |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 4366 | struct winsize ws; |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 4367 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 4368 | ws.ws_col = cols; |
| 4369 | ws.ws_row = rows; |
| 4370 | ws.ws_xpixel = cols * 5; |
| 4371 | ws.ws_ypixel = rows * 10; |
| 4372 | retval = ioctl(tty_fd, TIOCSWINSZ, &ws); |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4373 | ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed"); |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 4374 | # elif defined(TIOCSSIZE) |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 4375 | struct ttysize ts; |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 4376 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 4377 | ts.ts_cols = cols; |
| 4378 | ts.ts_lines = rows; |
| 4379 | retval = ioctl(tty_fd, TIOCSSIZE, &ts); |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 4380 | ch_log(NULL, "ioctl(TIOCSSIZE) %s", retval == 0 ? "success" : "failed"); |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 4381 | # endif |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 4382 | if (tty_fd != fd) |
| 4383 | close(tty_fd); |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 4384 | return retval == 0 ? OK : FAIL; |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 4385 | } |
| 4386 | #endif |
| 4387 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4388 | /* |
| 4389 | * Try to set the window size to Rows and Columns. |
| 4390 | */ |
| 4391 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4392 | mch_set_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4393 | { |
| 4394 | if (*T_CWS) |
| 4395 | { |
| 4396 | /* |
| 4397 | * NOTE: if you get an error here that term_set_winsize() is |
| 4398 | * undefined, check the output of configure. It could probably not |
| 4399 | * find a ncurses, termcap or termlib library. |
| 4400 | */ |
| 4401 | term_set_winsize((int)Rows, (int)Columns); |
| 4402 | out_flush(); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4403 | screen_start(); // don't know where cursor is now |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4407 | #endif // VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4408 | |
| 4409 | /* |
| 4410 | * Rows and/or Columns has changed. |
| 4411 | */ |
| 4412 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4413 | mch_new_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4414 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4415 | // Nothing to do. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4418 | /* |
| 4419 | * Wait for process "child" to end. |
| 4420 | * Return "child" if it exited properly, <= 0 on error. |
| 4421 | */ |
| 4422 | static pid_t |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4423 | wait4pid(pid_t child, waitstatus *status) |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4424 | { |
| 4425 | pid_t wait_pid = 0; |
Bram Moolenaar | 0abe052 | 2016-08-28 16:53:12 +0200 | [diff] [blame] | 4426 | long delay_msec = 1; |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4427 | |
| 4428 | while (wait_pid != child) |
| 4429 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4430 | // When compiled with Python threads are probably used, in which case |
| 4431 | // wait() sometimes hangs for no obvious reason. Use waitpid() |
| 4432 | // instead and loop (like the GUI). Also needed for other interfaces, |
| 4433 | // they might call system(). |
Bram Moolenaar | 6be120e | 2012-04-20 15:55:16 +0200 | [diff] [blame] | 4434 | # ifdef __NeXT__ |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4435 | wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0); |
Bram Moolenaar | 6be120e | 2012-04-20 15:55:16 +0200 | [diff] [blame] | 4436 | # else |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4437 | wait_pid = waitpid(child, status, WNOHANG); |
Bram Moolenaar | 6be120e | 2012-04-20 15:55:16 +0200 | [diff] [blame] | 4438 | # endif |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4439 | if (wait_pid == 0) |
| 4440 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4441 | // Wait for 1 to 10 msec before trying again. |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 4442 | mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE); |
Bram Moolenaar | 0abe052 | 2016-08-28 16:53:12 +0200 | [diff] [blame] | 4443 | if (++delay_msec > 10) |
| 4444 | delay_msec = 10; |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4445 | continue; |
| 4446 | } |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 4447 | if (wait_pid <= 0 |
| 4448 | # ifdef ECHILD |
| 4449 | && errno == ECHILD |
| 4450 | # endif |
| 4451 | ) |
| 4452 | break; |
| 4453 | } |
| 4454 | return wait_pid; |
| 4455 | } |
| 4456 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 4457 | #if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL) |
Bram Moolenaar | 7da3460 | 2017-08-01 17:14:21 +0200 | [diff] [blame] | 4458 | /* |
| 4459 | * Set the environment for a child process. |
| 4460 | */ |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4461 | static void |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 4462 | set_child_environment( |
| 4463 | long rows, |
| 4464 | long columns, |
| 4465 | char *term, |
| 4466 | int is_terminal UNUSED) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4467 | { |
| 4468 | # ifdef HAVE_SETENV |
| 4469 | char envbuf[50]; |
| 4470 | # else |
Bram Moolenaar | 5f7e7bd | 2017-07-22 14:08:43 +0200 | [diff] [blame] | 4471 | static char envbuf_Term[30]; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4472 | static char envbuf_Rows[20]; |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4473 | static char envbuf_Lines[20]; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4474 | static char envbuf_Columns[20]; |
Bram Moolenaar | b7a8dfe | 2017-07-22 20:33:05 +0200 | [diff] [blame] | 4475 | static char envbuf_Colors[20]; |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 4476 | # ifdef FEAT_TERMINAL |
Bram Moolenaar | d7a137f | 2018-06-12 18:05:24 +0200 | [diff] [blame] | 4477 | static char envbuf_Version[20]; |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 4478 | # endif |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4479 | # ifdef FEAT_CLIENTSERVER |
Bram Moolenaar | 7da3460 | 2017-08-01 17:14:21 +0200 | [diff] [blame] | 4480 | static char envbuf_Servername[60]; |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4481 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4482 | # endif |
| 4483 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4484 | # ifdef HAVE_SETENV |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4485 | setenv("TERM", term, 1); |
| 4486 | sprintf((char *)envbuf, "%ld", rows); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4487 | setenv("ROWS", (char *)envbuf, 1); |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4488 | sprintf((char *)envbuf, "%ld", rows); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4489 | setenv("LINES", (char *)envbuf, 1); |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4490 | sprintf((char *)envbuf, "%ld", columns); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4491 | setenv("COLUMNS", (char *)envbuf, 1); |
Bram Moolenaar | 759d815 | 2020-04-26 16:52:49 +0200 | [diff] [blame] | 4492 | sprintf((char *)envbuf, "%d", t_colors); |
Bram Moolenaar | b7a8dfe | 2017-07-22 20:33:05 +0200 | [diff] [blame] | 4493 | setenv("COLORS", (char *)envbuf, 1); |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 4494 | # ifdef FEAT_TERMINAL |
| 4495 | if (is_terminal) |
| 4496 | { |
Bram Moolenaar | 5ecdf96 | 2018-06-13 20:49:50 +0200 | [diff] [blame] | 4497 | sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION)); |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 4498 | setenv("VIM_TERMINAL", (char *)envbuf, 1); |
| 4499 | } |
| 4500 | # endif |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4501 | # ifdef FEAT_CLIENTSERVER |
Bram Moolenaar | 7da3460 | 2017-08-01 17:14:21 +0200 | [diff] [blame] | 4502 | setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1); |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4503 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4504 | # else |
| 4505 | /* |
| 4506 | * Putenv does not copy the string, it has to remain valid. |
| 4507 | * Use a static array to avoid losing allocated memory. |
Bram Moolenaar | 7da3460 | 2017-08-01 17:14:21 +0200 | [diff] [blame] | 4508 | * This won't work well when running multiple children... |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4509 | */ |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4510 | vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term); |
| 4511 | putenv(envbuf_Term); |
| 4512 | vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4513 | putenv(envbuf_Rows); |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4514 | vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows); |
| 4515 | putenv(envbuf_Lines); |
| 4516 | vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns), |
| 4517 | "COLUMNS=%ld", columns); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4518 | putenv(envbuf_Columns); |
Bram Moolenaar | affc8fd | 2020-04-28 21:58:29 +0200 | [diff] [blame] | 4519 | vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors); |
Bram Moolenaar | b7a8dfe | 2017-07-22 20:33:05 +0200 | [diff] [blame] | 4520 | putenv(envbuf_Colors); |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 4521 | # ifdef FEAT_TERMINAL |
| 4522 | if (is_terminal) |
| 4523 | { |
| 4524 | vim_snprintf(envbuf_Version, sizeof(envbuf_Version), |
Bram Moolenaar | 5ecdf96 | 2018-06-13 20:49:50 +0200 | [diff] [blame] | 4525 | "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION)); |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 4526 | putenv(envbuf_Version); |
| 4527 | } |
| 4528 | # endif |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4529 | # ifdef FEAT_CLIENTSERVER |
Bram Moolenaar | 7da3460 | 2017-08-01 17:14:21 +0200 | [diff] [blame] | 4530 | vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername), |
| 4531 | "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName); |
| 4532 | putenv(envbuf_Servername); |
Bram Moolenaar | 2a4f06f | 2017-08-01 18:44:29 +0200 | [diff] [blame] | 4533 | # endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4534 | # endif |
| 4535 | } |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4536 | |
| 4537 | static void |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 4538 | set_default_child_environment(int is_terminal) |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4539 | { |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 4540 | set_child_environment(Rows, Columns, "dumb", is_terminal); |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 4541 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4542 | #endif |
| 4543 | |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 4544 | #if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL) |
Bram Moolenaar | 979e8c5 | 2017-08-01 15:08:07 +0200 | [diff] [blame] | 4545 | /* |
| 4546 | * Open a PTY, with FD for the master and slave side. |
| 4547 | * When failing "pty_master_fd" and "pty_slave_fd" are -1. |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 4548 | * When successful both file descriptors are stored and the allocated pty name |
| 4549 | * is stored in both "*name1" and "*name2". |
Bram Moolenaar | 979e8c5 | 2017-08-01 15:08:07 +0200 | [diff] [blame] | 4550 | */ |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 4551 | static void |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 4552 | open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2) |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 4553 | { |
| 4554 | char *tty_name; |
| 4555 | |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 4556 | if (name1 != NULL) |
| 4557 | *name1 = NULL; |
| 4558 | if (name2 != NULL) |
| 4559 | *name2 = NULL; |
| 4560 | |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 4561 | *pty_master_fd = mch_openpty(&tty_name); // open pty |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 4562 | if (*pty_master_fd < 0) |
| 4563 | return; |
| 4564 | |
| 4565 | // Leaving out O_NOCTTY may lead to waitpid() always returning |
| 4566 | // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume |
| 4567 | // adding O_NOCTTY always works when defined. |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 4568 | #ifdef O_NOCTTY |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 4569 | *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 4570 | #else |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 4571 | *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 4572 | #endif |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 4573 | if (*pty_slave_fd < 0) |
| 4574 | { |
| 4575 | close(*pty_master_fd); |
| 4576 | *pty_master_fd = -1; |
| 4577 | } |
| 4578 | else |
| 4579 | { |
| 4580 | if (name1 != NULL) |
| 4581 | *name1 = vim_strsave((char_u *)tty_name); |
| 4582 | if (name2 != NULL) |
| 4583 | *name2 = vim_strsave((char_u *)tty_name); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 4584 | } |
| 4585 | } |
| 4586 | #endif |
| 4587 | |
Bram Moolenaar | fae4283 | 2017-08-01 22:24:26 +0200 | [diff] [blame] | 4588 | /* |
| 4589 | * Send SIGINT to a child process if "c" is an interrupt character. |
| 4590 | */ |
Bram Moolenaar | 840d16f | 2019-09-10 21:27:18 +0200 | [diff] [blame] | 4591 | static void |
Bram Moolenaar | fae4283 | 2017-08-01 22:24:26 +0200 | [diff] [blame] | 4592 | may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED) |
| 4593 | { |
| 4594 | # ifdef SIGINT |
| 4595 | if (c == Ctrl_C || c == intr_char) |
| 4596 | { |
| 4597 | # ifdef HAVE_SETSID |
| 4598 | kill(-pid, SIGINT); |
| 4599 | # else |
| 4600 | kill(0, SIGINT); |
| 4601 | # endif |
| 4602 | if (wpid > 0) |
| 4603 | kill(wpid, SIGINT); |
| 4604 | } |
| 4605 | # endif |
| 4606 | } |
| 4607 | |
Bram Moolenaar | 197c6b7 | 2019-11-03 23:37:12 +0100 | [diff] [blame] | 4608 | #if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO) |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4609 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4610 | /* |
| 4611 | * Parse "cmd" and return the result in "argvp" which is an allocated array of |
| 4612 | * pointers, the last one is NULL. |
| 4613 | * The "sh_tofree" and "shcf_tofree" must be later freed by the caller. |
| 4614 | */ |
Bram Moolenaar | 197c6b7 | 2019-11-03 23:37:12 +0100 | [diff] [blame] | 4615 | int |
| 4616 | unix_build_argv( |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4617 | char_u *cmd, |
| 4618 | char ***argvp, |
| 4619 | char_u **sh_tofree, |
| 4620 | char_u **shcf_tofree) |
| 4621 | { |
| 4622 | char **argv = NULL; |
| 4623 | int argc; |
| 4624 | |
| 4625 | *sh_tofree = vim_strsave(p_sh); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4626 | if (*sh_tofree == NULL) // out of memory |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4627 | return FAIL; |
| 4628 | |
| 4629 | if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL) |
| 4630 | return FAIL; |
| 4631 | *argvp = argv; |
| 4632 | |
| 4633 | if (cmd != NULL) |
| 4634 | { |
| 4635 | char_u *s; |
| 4636 | char_u *p; |
| 4637 | |
| 4638 | if (extra_shell_arg != NULL) |
| 4639 | argv[argc++] = (char *)extra_shell_arg; |
| 4640 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4641 | // Break 'shellcmdflag' into white separated parts. This doesn't |
| 4642 | // handle quoted strings, they are very unlikely to appear. |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 4643 | *shcf_tofree = alloc(STRLEN(p_shcf) + 1); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4644 | if (*shcf_tofree == NULL) // out of memory |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4645 | return FAIL; |
| 4646 | s = *shcf_tofree; |
| 4647 | p = p_shcf; |
| 4648 | while (*p != NUL) |
| 4649 | { |
| 4650 | argv[argc++] = (char *)s; |
| 4651 | while (*p && *p != ' ' && *p != TAB) |
| 4652 | *s++ = *p++; |
| 4653 | *s++ = NUL; |
| 4654 | p = skipwhite(p); |
| 4655 | } |
| 4656 | |
| 4657 | argv[argc++] = (char *)cmd; |
| 4658 | } |
| 4659 | argv[argc] = NULL; |
| 4660 | return OK; |
| 4661 | } |
| 4662 | #endif |
| 4663 | |
| 4664 | #if defined(FEAT_GUI) && defined(FEAT_TERMINAL) |
| 4665 | /* |
| 4666 | * Use a terminal window to run a shell command in. |
| 4667 | */ |
| 4668 | static int |
| 4669 | mch_call_shell_terminal( |
| 4670 | char_u *cmd, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4671 | int options UNUSED) // SHELL_*, see vim.h |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4672 | { |
| 4673 | jobopt_T opt; |
| 4674 | char **argv = NULL; |
| 4675 | char_u *tofree1 = NULL; |
| 4676 | char_u *tofree2 = NULL; |
| 4677 | int retval = -1; |
| 4678 | buf_T *buf; |
Bram Moolenaar | f9c3883 | 2018-06-19 19:59:20 +0200 | [diff] [blame] | 4679 | job_T *job; |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4680 | aco_save_T aco; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4681 | oparg_T oa; // operator arguments |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4682 | |
Bram Moolenaar | 197c6b7 | 2019-11-03 23:37:12 +0100 | [diff] [blame] | 4683 | if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL) |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4684 | goto theend; |
| 4685 | |
| 4686 | init_job_options(&opt); |
| 4687 | ch_log(NULL, "starting terminal for system command '%s'", cmd); |
| 4688 | buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM); |
Bram Moolenaar | f9c3883 | 2018-06-19 19:59:20 +0200 | [diff] [blame] | 4689 | if (buf == NULL) |
| 4690 | goto theend; |
| 4691 | |
| 4692 | job = term_getjob(buf->b_term); |
| 4693 | ++job->jv_refcount; |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4694 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4695 | // Find a window to make "buf" curbuf. |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4696 | aucmd_prepbuf(&aco, buf); |
Bram Moolenaar | e76062c | 2022-11-28 18:51:43 +0000 | [diff] [blame] | 4697 | if (curbuf == buf) |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4698 | { |
Bram Moolenaar | e76062c | 2022-11-28 18:51:43 +0000 | [diff] [blame] | 4699 | // Only when managed to find a window for "buf", |
| 4700 | clear_oparg(&oa); |
| 4701 | while (term_use_loop()) |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4702 | { |
Bram Moolenaar | e76062c | 2022-11-28 18:51:43 +0000 | [diff] [blame] | 4703 | if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active) |
| 4704 | { |
| 4705 | // If terminal_loop() returns OK we got a key that is handled |
| 4706 | // in Normal model. We don't do redrawing anyway. |
| 4707 | if (terminal_loop(TRUE) == OK) |
| 4708 | normal_cmd(&oa, TRUE); |
| 4709 | } |
| 4710 | else |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4711 | normal_cmd(&oa, TRUE); |
| 4712 | } |
Bram Moolenaar | e76062c | 2022-11-28 18:51:43 +0000 | [diff] [blame] | 4713 | retval = job->jv_exitval; |
| 4714 | ch_log(NULL, "system command finished"); |
| 4715 | |
| 4716 | job_unref(job); |
| 4717 | |
| 4718 | // restore curwin/curbuf and a few other things |
| 4719 | aucmd_restbuf(&aco); |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4720 | } |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4721 | |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 4722 | // Only require pressing Enter when redrawing, to avoid that system() gets |
Bram Moolenaar | cb5ed4d | 2022-07-28 12:54:08 +0100 | [diff] [blame] | 4723 | // the hit-enter prompt even though it didn't output anything. |
Bram Moolenaar | 79cdf02 | 2023-05-20 14:07:00 +0100 | [diff] [blame] | 4724 | if (RedrawingDisabled == 0) |
Bram Moolenaar | cb5ed4d | 2022-07-28 12:54:08 +0100 | [diff] [blame] | 4725 | wait_return(TRUE); |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4726 | do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE); |
| 4727 | |
| 4728 | theend: |
| 4729 | vim_free(argv); |
| 4730 | vim_free(tofree1); |
| 4731 | vim_free(tofree2); |
| 4732 | return retval; |
| 4733 | } |
| 4734 | #endif |
| 4735 | |
| 4736 | #ifdef USE_SYSTEM |
| 4737 | /* |
| 4738 | * Use system() to start the shell: simple but slow. |
| 4739 | */ |
| 4740 | static int |
| 4741 | mch_call_shell_system( |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4742 | char_u *cmd, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4743 | int options) // SHELL_*, see vim.h |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4744 | { |
| 4745 | #ifdef VMS |
| 4746 | char *ifn = NULL; |
| 4747 | char *ofn = NULL; |
| 4748 | #endif |
Bram Moolenaar | 26e8644 | 2020-05-17 14:06:16 +0200 | [diff] [blame] | 4749 | tmode_T tmode = cur_tmode; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4750 | char_u *newcmd; // only needed for unix |
Bram Moolenaar | de5e2c2 | 2016-11-04 20:35:31 +0100 | [diff] [blame] | 4751 | int x; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4752 | |
| 4753 | out_flush(); |
| 4754 | |
| 4755 | if (options & SHELL_COOKED) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4756 | settmode(TMODE_COOK); // set to normal mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4757 | |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 4758 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 4759 | save_clipboard(); |
Bram Moolenaar | 62b4218 | 2010-09-21 22:09:37 +0200 | [diff] [blame] | 4760 | loose_clipboard(); |
| 4761 | # endif |
| 4762 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4763 | if (cmd == NULL) |
| 4764 | x = system((char *)p_sh); |
| 4765 | else |
| 4766 | { |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 4767 | # ifdef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4768 | if (ofn = strchr((char *)cmd, '>')) |
| 4769 | *ofn++ = '\0'; |
| 4770 | if (ifn = strchr((char *)cmd, '<')) |
| 4771 | { |
| 4772 | char *p; |
| 4773 | |
| 4774 | *ifn++ = '\0'; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4775 | p = strchr(ifn,' '); // chop off any trailing spaces |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4776 | if (p) |
| 4777 | *p = '\0'; |
| 4778 | } |
| 4779 | if (ofn) |
| 4780 | x = vms_sys((char *)cmd, ofn, ifn); |
| 4781 | else |
| 4782 | x = system((char *)cmd); |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 4783 | # else |
Bram Moolenaar | 18a4ba2 | 2019-05-24 19:39:03 +0200 | [diff] [blame] | 4784 | newcmd = alloc(STRLEN(p_sh) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4785 | + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg)) |
Bram Moolenaar | 18a4ba2 | 2019-05-24 19:39:03 +0200 | [diff] [blame] | 4786 | + STRLEN(p_shcf) + STRLEN(cmd) + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4787 | if (newcmd == NULL) |
| 4788 | x = 0; |
| 4789 | else |
| 4790 | { |
| 4791 | sprintf((char *)newcmd, "%s %s %s %s", p_sh, |
| 4792 | extra_shell_arg == NULL ? "" : (char *)extra_shell_arg, |
| 4793 | (char *)p_shcf, |
| 4794 | (char *)cmd); |
| 4795 | x = system((char *)newcmd); |
| 4796 | vim_free(newcmd); |
| 4797 | } |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 4798 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4799 | } |
| 4800 | # ifdef VMS |
| 4801 | x = vms_sys_status(x); |
| 4802 | # endif |
| 4803 | if (emsg_silent) |
| 4804 | ; |
| 4805 | else if (x == 127) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4806 | msg_puts(_("\nCannot execute shell sh\n")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4807 | else if (x && !(options & SHELL_SILENT)) |
| 4808 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4809 | msg_puts(_("\nshell returned ")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4810 | msg_outnum((long)x); |
| 4811 | msg_putchar('\n'); |
| 4812 | } |
| 4813 | |
| 4814 | if (tmode == TMODE_RAW) |
Bram Moolenaar | 3b1f18f | 2020-05-16 23:15:08 +0200 | [diff] [blame] | 4815 | { |
| 4816 | // The shell may have messed with the mode, always set it. |
| 4817 | cur_tmode = TMODE_UNKNOWN; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4818 | settmode(TMODE_RAW); // set to raw mode |
Bram Moolenaar | 3b1f18f | 2020-05-16 23:15:08 +0200 | [diff] [blame] | 4819 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4820 | resettitle(); |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 4821 | # if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
| 4822 | restore_clipboard(); |
| 4823 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4824 | return x; |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4825 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4826 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4827 | #else // USE_SYSTEM |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4828 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4829 | # define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use |
| 4830 | // 127, some shells use that already |
| 4831 | # define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4832 | |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4833 | /* |
| 4834 | * Don't use system(), use fork()/exec(). |
| 4835 | */ |
| 4836 | static int |
| 4837 | mch_call_shell_fork( |
| 4838 | char_u *cmd, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4839 | int options) // SHELL_*, see vim.h |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4840 | { |
Bram Moolenaar | 26e8644 | 2020-05-17 14:06:16 +0200 | [diff] [blame] | 4841 | tmode_T tmode = cur_tmode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4842 | pid_t pid; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4843 | pid_t wpid = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4844 | pid_t wait_pid = 0; |
| 4845 | # ifdef HAVE_UNION_WAIT |
| 4846 | union wait status; |
| 4847 | # else |
| 4848 | int status = -1; |
| 4849 | # endif |
| 4850 | int retval = -1; |
| 4851 | char **argv = NULL; |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4852 | char_u *tofree1 = NULL; |
| 4853 | char_u *tofree2 = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4854 | int i; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4855 | int pty_master_fd = -1; // for pty's |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4856 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4857 | int pty_slave_fd = -1; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4858 | # endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4859 | int fd_toshell[2]; // for pipes |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4860 | int fd_fromshell[2]; |
| 4861 | int pipe_error = FALSE; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4862 | int did_settmode = FALSE; // settmode(TMODE_RAW) called |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4863 | |
| 4864 | out_flush(); |
| 4865 | if (options & SHELL_COOKED) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4866 | settmode(TMODE_COOK); // set to normal mode |
Bram Moolenaar | 3b1f18f | 2020-05-16 23:15:08 +0200 | [diff] [blame] | 4867 | if (tmode == TMODE_RAW) |
| 4868 | // The shell may have messed with the mode, always set it later. |
| 4869 | cur_tmode = TMODE_UNKNOWN; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4870 | |
Bram Moolenaar | 197c6b7 | 2019-11-03 23:37:12 +0100 | [diff] [blame] | 4871 | if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 4872 | goto error; |
Bram Moolenaar | ea35ef6 | 2011-08-04 22:59:28 +0200 | [diff] [blame] | 4873 | |
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 | * For the GUI, when writing the output into the buffer and when reading |
| 4876 | * input from the buffer: Try using a pseudo-tty to get the stdin/stdout |
| 4877 | * of the executed command into the Vim window. Or use a pipe. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4878 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4879 | if ((options & (SHELL_READ|SHELL_WRITE)) |
| 4880 | # ifdef FEAT_GUI |
| 4881 | || (gui.in_use && show_shell_mess) |
| 4882 | # endif |
| 4883 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4884 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4885 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4886 | /* |
| 4887 | * Try to open a master pty. |
| 4888 | * If this works, open the slave pty. |
| 4889 | * If the slave can't be opened, close the master pty. |
| 4890 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4891 | if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE))) |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 4892 | open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4893 | /* |
| 4894 | * If not opening a pty or it didn't work, try using pipes. |
| 4895 | */ |
| 4896 | if (pty_master_fd < 0) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4897 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4898 | { |
| 4899 | pipe_error = (pipe(fd_toshell) < 0); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4900 | if (!pipe_error) // pipe create OK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4901 | { |
| 4902 | pipe_error = (pipe(fd_fromshell) < 0); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4903 | if (pipe_error) // pipe create failed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4904 | { |
| 4905 | close(fd_toshell[0]); |
| 4906 | close(fd_toshell[1]); |
| 4907 | } |
| 4908 | } |
| 4909 | if (pipe_error) |
| 4910 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4911 | msg_puts(_("\nCannot create pipes\n")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4912 | out_flush(); |
| 4913 | } |
| 4914 | } |
| 4915 | } |
| 4916 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4917 | if (!pipe_error) // pty or pipe opened or not used |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4918 | { |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 4919 | SIGSET_DECL(curset) |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 4920 | BLOCK_SIGNALS(&curset); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4921 | pid = fork(); // maybe we should use vfork() |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 4922 | if (pid == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4923 | { |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 4924 | UNBLOCK_SIGNALS(&curset); |
| 4925 | |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4926 | msg_puts(_("\nCannot fork\n")); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4927 | if ((options & (SHELL_READ|SHELL_WRITE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4928 | # ifdef FEAT_GUI |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4929 | || (gui.in_use && show_shell_mess) |
| 4930 | # endif |
| 4931 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4932 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4933 | # ifdef FEAT_GUI |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4934 | if (pty_master_fd >= 0) // close the pseudo tty |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4935 | { |
| 4936 | close(pty_master_fd); |
| 4937 | close(pty_slave_fd); |
| 4938 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4939 | else // close the pipes |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4940 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4941 | { |
| 4942 | close(fd_toshell[0]); |
| 4943 | close(fd_toshell[1]); |
| 4944 | close(fd_fromshell[0]); |
| 4945 | close(fd_fromshell[1]); |
| 4946 | } |
| 4947 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4948 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4949 | else if (pid == 0) // child |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4950 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4951 | reset_signals(); // handle signals normally |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 4952 | UNBLOCK_SIGNALS(&curset); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4953 | |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 4954 | # ifdef FEAT_EVAL |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 4955 | if (ch_log_active()) |
Bram Moolenaar | 76603ba | 2020-08-30 17:24:37 +0200 | [diff] [blame] | 4956 | { |
| 4957 | ch_log(NULL, "closing channel log in the child process"); |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 4958 | ch_logfile((char_u *)"", (char_u *)""); |
Bram Moolenaar | 76603ba | 2020-08-30 17:24:37 +0200 | [diff] [blame] | 4959 | } |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 4960 | # endif |
| 4961 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4962 | if (!show_shell_mess || (options & SHELL_EXPAND)) |
| 4963 | { |
| 4964 | int fd; |
| 4965 | |
| 4966 | /* |
| 4967 | * Don't want to show any message from the shell. Can't just |
| 4968 | * close stdout and stderr though, because some systems will |
| 4969 | * break if you try to write to them after that, so we must |
| 4970 | * use dup() to replace them with something else -- webb |
| 4971 | * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang, |
| 4972 | * waiting for input. |
| 4973 | */ |
| 4974 | fd = open("/dev/null", O_RDWR | O_EXTRA, 0); |
| 4975 | fclose(stdin); |
| 4976 | fclose(stdout); |
| 4977 | fclose(stderr); |
| 4978 | |
| 4979 | /* |
| 4980 | * If any of these open()'s and dup()'s fail, we just continue |
| 4981 | * anyway. It's not fatal, and on most systems it will make |
| 4982 | * no difference at all. On a few it will cause the execvp() |
| 4983 | * to exit with a non-zero status even when the completion |
| 4984 | * could be done, which is nothing too serious. If the open() |
| 4985 | * or dup() failed we'd just do the same thing ourselves |
| 4986 | * anyway -- webb |
| 4987 | */ |
| 4988 | if (fd >= 0) |
| 4989 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4990 | vim_ignored = dup(fd); // To replace stdin (fd 0) |
| 4991 | vim_ignored = dup(fd); // To replace stdout (fd 1) |
| 4992 | vim_ignored = dup(fd); // To replace stderr (fd 2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4993 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 4994 | // Don't need this now that we've duplicated it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4995 | close(fd); |
| 4996 | } |
| 4997 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4998 | else if ((options & (SHELL_READ|SHELL_WRITE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4999 | # ifdef FEAT_GUI |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5000 | || gui.in_use |
| 5001 | # endif |
| 5002 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5003 | { |
| 5004 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5005 | # ifdef HAVE_SETSID |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5006 | // Create our own process group, so that the child and all its |
| 5007 | // children can be kill()ed. Don't do this when using pipes, |
| 5008 | // because stdin is not a tty, we would lose /dev/tty. |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5009 | if (p_stmp) |
Bram Moolenaar | 0725608 | 2009-02-04 13:19:42 +0000 | [diff] [blame] | 5010 | { |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5011 | (void)setsid(); |
Bram Moolenaar | 0725608 | 2009-02-04 13:19:42 +0000 | [diff] [blame] | 5012 | # if defined(SIGHUP) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5013 | // When doing "!xterm&" and 'shell' is bash: the shell |
| 5014 | // will exit and send SIGHUP to all processes in its |
| 5015 | // group, killing the just started process. Ignore SIGHUP |
| 5016 | // to avoid that. (suggested by Simon Schubert) |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 5017 | mch_signal(SIGHUP, SIG_IGN); |
Bram Moolenaar | 0725608 | 2009-02-04 13:19:42 +0000 | [diff] [blame] | 5018 | # endif |
| 5019 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5020 | # endif |
| 5021 | # ifdef FEAT_GUI |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5022 | if (pty_slave_fd >= 0) |
| 5023 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5024 | // push stream discipline modules |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5025 | if (options & SHELL_COOKED) |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 5026 | setup_slavepty(pty_slave_fd); |
Bram Moolenaar | fff10d9 | 2021-10-13 10:05:30 +0100 | [diff] [blame] | 5027 | # ifdef TIOCSCTTY |
| 5028 | // Try to become controlling tty (probably doesn't work, |
| 5029 | // unless run by root) |
| 5030 | ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL); |
| 5031 | # endif |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5032 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5033 | # endif |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 5034 | set_default_child_environment(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5035 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 5036 | /* |
| 5037 | * stderr is only redirected when using the GUI, so that a |
| 5038 | * program like gpg can still access the terminal to get a |
| 5039 | * passphrase using stderr. |
| 5040 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5041 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5042 | if (pty_master_fd >= 0) |
| 5043 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5044 | close(pty_master_fd); // close master side of pty |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5045 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5046 | // set up stdin/stdout/stderr for the child |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5047 | close(0); |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5048 | vim_ignored = dup(pty_slave_fd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5049 | close(1); |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5050 | vim_ignored = dup(pty_slave_fd); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 5051 | if (gui.in_use) |
| 5052 | { |
| 5053 | close(2); |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5054 | vim_ignored = dup(pty_slave_fd); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 5055 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5056 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5057 | close(pty_slave_fd); // has been dupped, close it now |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5058 | } |
| 5059 | else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5060 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5061 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5062 | // set up stdin for the child |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5063 | close(fd_toshell[1]); |
| 5064 | close(0); |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5065 | vim_ignored = dup(fd_toshell[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5066 | close(fd_toshell[0]); |
| 5067 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5068 | // set up stdout for the child |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5069 | close(fd_fromshell[0]); |
| 5070 | close(1); |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5071 | vim_ignored = dup(fd_fromshell[1]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5072 | close(fd_fromshell[1]); |
| 5073 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 5074 | # ifdef FEAT_GUI |
| 5075 | if (gui.in_use) |
| 5076 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5077 | // set up stderr for the child |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 5078 | close(2); |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5079 | vim_ignored = dup(1); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 5080 | } |
| 5081 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5082 | } |
| 5083 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5084 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5085 | /* |
| 5086 | * There is no type cast for the argv, because the type may be |
| 5087 | * different on different machines. This may cause a warning |
| 5088 | * message with strict compilers, don't worry about it. |
| 5089 | * Call _exit() instead of exit() to avoid closing the connection |
| 5090 | * to the X server (esp. with GTK, which uses atexit()). |
| 5091 | */ |
| 5092 | execvp(argv[0], argv); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5093 | _exit(EXEC_FAILED); // exec failed, return failure code |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5094 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5095 | else // parent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5096 | { |
| 5097 | /* |
| 5098 | * While child is running, ignore terminating signals. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5099 | * Do catch CTRL-C, so that "got_int" is set. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5100 | */ |
| 5101 | catch_signals(SIG_IGN, SIG_ERR); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5102 | catch_int_signal(); |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5103 | UNBLOCK_SIGNALS(&curset); |
Bram Moolenaar | c4d4ac2 | 2016-11-07 22:42:57 +0100 | [diff] [blame] | 5104 | # ifdef FEAT_JOB_CHANNEL |
| 5105 | ++dont_check_job_ended; |
| 5106 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5107 | /* |
| 5108 | * For the GUI we redirect stdin, stdout and stderr to our window. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5109 | * This is also used to pipe stdin/stdout to/from the external |
| 5110 | * command. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5111 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5112 | if ((options & (SHELL_READ|SHELL_WRITE)) |
| 5113 | # ifdef FEAT_GUI |
| 5114 | || (gui.in_use && show_shell_mess) |
| 5115 | # endif |
| 5116 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5117 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5118 | # define BUFLEN 100 // length for buffer, pseudo tty limit is 128 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5119 | char_u buffer[BUFLEN + 1]; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5120 | int buffer_off = 0; // valid bytes in buffer[] |
| 5121 | char_u ta_buf[BUFLEN + 1]; // TypeAHead |
| 5122 | int ta_len = 0; // valid bytes in ta_buf[] |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5123 | int len; |
| 5124 | int p_more_save; |
| 5125 | int old_State; |
| 5126 | int c; |
| 5127 | int toshell_fd; |
| 5128 | int fromshell_fd; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5129 | garray_T ga; |
| 5130 | int noread_cnt; |
Bram Moolenaar | 1ac56c2 | 2019-01-17 22:28:22 +0100 | [diff] [blame] | 5131 | # ifdef ELAPSED_FUNC |
| 5132 | elapsed_T start_tv; |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5133 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5134 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5135 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5136 | if (pty_master_fd >= 0) |
| 5137 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5138 | fromshell_fd = pty_master_fd; |
| 5139 | toshell_fd = dup(pty_master_fd); |
| 5140 | } |
| 5141 | else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5142 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5143 | { |
| 5144 | close(fd_toshell[0]); |
| 5145 | close(fd_fromshell[1]); |
| 5146 | toshell_fd = fd_toshell[1]; |
| 5147 | fromshell_fd = fd_fromshell[0]; |
| 5148 | } |
| 5149 | |
| 5150 | /* |
| 5151 | * Write to the child if there are typed characters. |
| 5152 | * Read from the child if there are characters available. |
| 5153 | * Repeat the reading a few times if more characters are |
| 5154 | * available. Need to check for typed keys now and then, but |
| 5155 | * not too often (delays when no chars are available). |
| 5156 | * This loop is quit if no characters can be read from the pty |
| 5157 | * (WaitForChar detected special condition), or there are no |
| 5158 | * characters available and the child has exited. |
| 5159 | * Only check if the child has exited when there is no more |
| 5160 | * output. The child may exit before all the output has |
| 5161 | * been printed. |
| 5162 | * |
| 5163 | * Currently this busy loops! |
| 5164 | * This can probably dead-lock when the write blocks! |
| 5165 | */ |
| 5166 | p_more_save = p_more; |
| 5167 | p_more = FALSE; |
| 5168 | old_State = State; |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 5169 | State = MODE_EXTERNCMD; // don't redraw at window resize |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5170 | |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5171 | if ((options & SHELL_WRITE) && toshell_fd >= 0) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5172 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5173 | // Fork a process that will write the lines to the |
| 5174 | // external program. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5175 | if ((wpid = fork()) == -1) |
| 5176 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5177 | msg_puts(_("\nCannot fork\n")); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5178 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5179 | else if (wpid == 0) // child |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5180 | { |
| 5181 | linenr_T lnum = curbuf->b_op_start.lnum; |
| 5182 | int written = 0; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5183 | char_u *lp = ml_get(lnum); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5184 | size_t l; |
| 5185 | |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 5186 | close(fromshell_fd); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5187 | for (;;) |
| 5188 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5189 | l = STRLEN(lp + written); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5190 | if (l == 0) |
| 5191 | len = 0; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5192 | else if (lp[written] == NL) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5193 | // NL -> NUL translation |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5194 | len = write(toshell_fd, "", (size_t)1); |
| 5195 | else |
| 5196 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 5197 | char_u *s = vim_strchr(lp + written, NL); |
| 5198 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5199 | len = write(toshell_fd, (char *)lp + written, |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 5200 | s == NULL ? l |
| 5201 | : (size_t)(s - (lp + written))); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5202 | } |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 5203 | if (len == (int)l) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5204 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5205 | // Finished a line, add a NL, unless this line |
| 5206 | // should not have one. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5207 | if (lnum != curbuf->b_op_end.lnum |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 5208 | || (!curbuf->b_p_bin |
| 5209 | && curbuf->b_p_fixeol) |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 5210 | || (lnum != curbuf->b_no_eol_lnum |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 5211 | && (lnum != curbuf->b_ml.ml_line_count |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5212 | || curbuf->b_p_eol))) |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5213 | vim_ignored = write(toshell_fd, "\n", |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 5214 | (size_t)1); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5215 | ++lnum; |
| 5216 | if (lnum > curbuf->b_op_end.lnum) |
| 5217 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5218 | // finished all the lines, close pipe |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5219 | close(toshell_fd); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5220 | break; |
| 5221 | } |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5222 | lp = ml_get(lnum); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5223 | written = 0; |
| 5224 | } |
| 5225 | else if (len > 0) |
| 5226 | written += len; |
| 5227 | } |
| 5228 | _exit(0); |
| 5229 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5230 | else // parent |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5231 | { |
| 5232 | close(toshell_fd); |
| 5233 | toshell_fd = -1; |
| 5234 | } |
| 5235 | } |
| 5236 | |
| 5237 | if (options & SHELL_READ) |
| 5238 | ga_init2(&ga, 1, BUFLEN); |
| 5239 | |
| 5240 | noread_cnt = 0; |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 5241 | # ifdef ELAPSED_FUNC |
| 5242 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5243 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5244 | for (;;) |
| 5245 | { |
| 5246 | /* |
| 5247 | * Check if keys have been typed, write them to the child |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 5248 | * if there are any. |
| 5249 | * Don't do this if we are expanding wild cards (would eat |
| 5250 | * typeahead). |
| 5251 | * Don't do this when filtering and terminal is in cooked |
| 5252 | * mode, the shell command will handle the I/O. Avoids |
| 5253 | * that a typed password is echoed for ssh or gpg command. |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5254 | * Don't get characters when the child has already |
| 5255 | * finished (wait_pid == 0). |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5256 | * Don't read characters unless we didn't get output for a |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5257 | * while (noread_cnt > 4), avoids that ":r !ls" eats |
| 5258 | * typeahead. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5259 | */ |
| 5260 | len = 0; |
| 5261 | if (!(options & SHELL_EXPAND) |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 5262 | && ((options & |
| 5263 | (SHELL_READ|SHELL_WRITE|SHELL_COOKED)) |
| 5264 | != (SHELL_READ|SHELL_WRITE|SHELL_COOKED) |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5265 | # ifdef FEAT_GUI |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 5266 | || gui.in_use |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5267 | # endif |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 5268 | ) |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5269 | && wait_pid == 0 |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5270 | && (ta_len > 0 || noread_cnt > 4)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5271 | { |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5272 | if (ta_len == 0) |
| 5273 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5274 | // Get extra characters when we don't have any. |
| 5275 | // Reset the counter and timer. |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5276 | noread_cnt = 0; |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 5277 | # ifdef ELAPSED_FUNC |
| 5278 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5279 | # endif |
| 5280 | len = ui_inchar(ta_buf, BUFLEN, 10L, 0); |
| 5281 | } |
| 5282 | if (ta_len > 0 || len > 0) |
| 5283 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5284 | /* |
| 5285 | * For pipes: |
| 5286 | * Check for CTRL-C: send interrupt signal to child. |
| 5287 | * Check for CTRL-D: EOF, close pipe to child. |
| 5288 | */ |
| 5289 | if (len == 1 && (pty_master_fd < 0 || cmd != NULL)) |
| 5290 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5291 | /* |
| 5292 | * Send SIGINT to the child's group or all |
| 5293 | * processes in our group. |
| 5294 | */ |
Bram Moolenaar | fae4283 | 2017-08-01 22:24:26 +0200 | [diff] [blame] | 5295 | may_send_sigint(ta_buf[ta_len], pid, wpid); |
| 5296 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5297 | if (pty_master_fd < 0 && toshell_fd >= 0 |
| 5298 | && ta_buf[ta_len] == Ctrl_D) |
| 5299 | { |
| 5300 | close(toshell_fd); |
| 5301 | toshell_fd = -1; |
| 5302 | } |
| 5303 | } |
| 5304 | |
Bram Moolenaar | 2f7e1b8 | 2022-10-04 13:17:31 +0100 | [diff] [blame] | 5305 | // Remove Vim-specific codes from the input. |
| 5306 | len = term_replace_keycodes(ta_buf, ta_len, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5307 | |
| 5308 | /* |
| 5309 | * For pipes: echo the typed characters. |
| 5310 | * For a pty this does not seem to work. |
| 5311 | */ |
| 5312 | if (pty_master_fd < 0) |
| 5313 | { |
| 5314 | for (i = ta_len; i < ta_len + len; ++i) |
| 5315 | { |
| 5316 | if (ta_buf[i] == '\n' || ta_buf[i] == '\b') |
| 5317 | msg_putchar(ta_buf[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5318 | else if (has_mbyte) |
| 5319 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5320 | int l = (*mb_ptr2len)(ta_buf + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5321 | |
| 5322 | msg_outtrans_len(ta_buf + i, l); |
| 5323 | i += l - 1; |
| 5324 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5325 | else |
| 5326 | msg_outtrans_len(ta_buf + i, 1); |
| 5327 | } |
| 5328 | windgoto(msg_row, msg_col); |
| 5329 | out_flush(); |
| 5330 | } |
| 5331 | |
| 5332 | ta_len += len; |
| 5333 | |
| 5334 | /* |
| 5335 | * Write the characters to the child, unless EOF has |
| 5336 | * been typed for pipes. Write one character at a |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 5337 | * time, to avoid losing too much typeahead. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5338 | * When writing buffer lines, drop the typed |
| 5339 | * characters (only check for CTRL-C). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5340 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5341 | if (options & SHELL_WRITE) |
| 5342 | ta_len = 0; |
| 5343 | else if (toshell_fd >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5344 | { |
| 5345 | len = write(toshell_fd, (char *)ta_buf, (size_t)1); |
| 5346 | if (len > 0) |
| 5347 | { |
| 5348 | ta_len -= len; |
| 5349 | mch_memmove(ta_buf, ta_buf + len, ta_len); |
| 5350 | } |
| 5351 | } |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5352 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5355 | if (got_int) |
| 5356 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5357 | // CTRL-C sends a signal to the child, we ignore it |
| 5358 | // ourselves |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5359 | # ifdef HAVE_SETSID |
| 5360 | kill(-pid, SIGINT); |
| 5361 | # else |
| 5362 | kill(0, SIGINT); |
| 5363 | # endif |
| 5364 | if (wpid > 0) |
| 5365 | kill(wpid, SIGINT); |
| 5366 | got_int = FALSE; |
| 5367 | } |
| 5368 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5369 | /* |
| 5370 | * Check if the child has any characters to be printed. |
| 5371 | * Read them and write them to our window. Repeat this as |
| 5372 | * long as there is something to do, avoid the 10ms wait |
| 5373 | * for mch_inchar(), or sending typeahead characters to |
| 5374 | * the external process. |
| 5375 | * TODO: This should handle escape sequences, compatible |
| 5376 | * to some terminal (vt52?). |
| 5377 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5378 | ++noread_cnt; |
Bram Moolenaar | 8fdd721 | 2016-03-26 19:41:48 +0100 | [diff] [blame] | 5379 | while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5380 | { |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 5381 | len = read_eintr(fromshell_fd, buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5382 | + buffer_off, (size_t)(BUFLEN - buffer_off) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5383 | ); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5384 | if (len <= 0) // end of file or error |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5385 | goto finished; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5386 | |
| 5387 | noread_cnt = 0; |
| 5388 | if (options & SHELL_READ) |
| 5389 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5390 | // Do NUL -> NL translation, append NL separated |
| 5391 | // lines to the current buffer. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5392 | for (i = 0; i < len; ++i) |
| 5393 | { |
| 5394 | if (buffer[i] == NL) |
| 5395 | append_ga_line(&ga); |
| 5396 | else if (buffer[i] == NUL) |
| 5397 | ga_append(&ga, NL); |
| 5398 | else |
| 5399 | ga_append(&ga, buffer[i]); |
| 5400 | } |
| 5401 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5402 | else if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5403 | { |
| 5404 | int l; |
Bram Moolenaar | a2150ac | 2018-03-17 13:15:17 +0100 | [diff] [blame] | 5405 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5406 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5407 | len += buffer_off; |
| 5408 | buffer[len] = NUL; |
| 5409 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5410 | // Check if the last character in buffer[] is |
| 5411 | // incomplete, keep these bytes for the next |
| 5412 | // round. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5413 | for (p = buffer; p < buffer + len; p += l) |
| 5414 | { |
Bram Moolenaar | d3c907b | 2016-08-17 21:32:09 +0200 | [diff] [blame] | 5415 | l = MB_CPTR2LEN(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5416 | if (l == 0) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5417 | l = 1; // NUL byte? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5418 | else if (MB_BYTE2LEN(*p) != l) |
| 5419 | break; |
| 5420 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5421 | if (p == buffer) // no complete character |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5422 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5423 | // avoid getting stuck at an illegal byte |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5424 | if (len >= 12) |
| 5425 | ++p; |
| 5426 | else |
| 5427 | { |
| 5428 | buffer_off = len; |
| 5429 | continue; |
| 5430 | } |
| 5431 | } |
| 5432 | c = *p; |
| 5433 | *p = NUL; |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5434 | msg_puts((char *)buffer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5435 | if (p < buffer + len) |
| 5436 | { |
| 5437 | *p = c; |
| 5438 | buffer_off = (buffer + len) - p; |
| 5439 | mch_memmove(buffer, p, buffer_off); |
| 5440 | continue; |
| 5441 | } |
| 5442 | buffer_off = 0; |
| 5443 | } |
| 5444 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5445 | { |
| 5446 | buffer[len] = NUL; |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5447 | msg_puts((char *)buffer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
| 5450 | windgoto(msg_row, msg_col); |
| 5451 | cursor_on(); |
| 5452 | out_flush(); |
| 5453 | if (got_int) |
| 5454 | break; |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5455 | |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 5456 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 17fe5e1 | 2016-04-04 22:03:08 +0200 | [diff] [blame] | 5457 | if (wait_pid == 0) |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5458 | { |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 5459 | long msec = ELAPSED_FUNC(start_tv); |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5460 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5461 | // Avoid that we keep looping here without |
| 5462 | // checking for a CTRL-C for a long time. Don't |
| 5463 | // break out too often to avoid losing typeahead. |
Bram Moolenaar | b3dc8fd | 2009-02-22 01:52:59 +0000 | [diff] [blame] | 5464 | if (msec > 2000) |
| 5465 | { |
| 5466 | noread_cnt = 5; |
| 5467 | break; |
| 5468 | } |
| 5469 | } |
| 5470 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5471 | } |
| 5472 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5473 | // If we already detected the child has finished, continue |
| 5474 | // reading output for a short while. Some text may be |
| 5475 | // buffered. |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5476 | if (wait_pid == pid) |
Bram Moolenaar | 17fe5e1 | 2016-04-04 22:03:08 +0200 | [diff] [blame] | 5477 | { |
| 5478 | if (noread_cnt < 5) |
| 5479 | continue; |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5480 | break; |
Bram Moolenaar | 17fe5e1 | 2016-04-04 22:03:08 +0200 | [diff] [blame] | 5481 | } |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5482 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5483 | /* |
| 5484 | * Check if the child still exists, before checking for |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 5485 | * typed characters (otherwise we would lose typeahead). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5486 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5487 | # ifdef __NeXT__ |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 5488 | wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5489 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5490 | wait_pid = waitpid(pid, &status, WNOHANG); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5491 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5492 | if ((wait_pid == (pid_t)-1 && errno == ECHILD) |
| 5493 | || (wait_pid == pid && WIFEXITED(status))) |
| 5494 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5495 | // Don't break the loop yet, try reading more |
| 5496 | // characters from "fromshell_fd" first. When using |
| 5497 | // pipes there might still be something to read and |
| 5498 | // then we'll break the loop at the "break" above. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5499 | wait_pid = pid; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5500 | } |
Bram Moolenaar | 12033fb | 2005-12-16 21:49:31 +0000 | [diff] [blame] | 5501 | else |
| 5502 | wait_pid = 0; |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5503 | |
Bram Moolenaar | 95a5135 | 2013-03-21 22:53:50 +0100 | [diff] [blame] | 5504 | # if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5505 | // Handle any X events, e.g. serving the clipboard. |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5506 | clip_update(); |
| 5507 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5508 | } |
| 5509 | finished: |
| 5510 | p_more = p_more_save; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5511 | if (options & SHELL_READ) |
| 5512 | { |
| 5513 | if (ga.ga_len > 0) |
| 5514 | { |
| 5515 | append_ga_line(&ga); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5516 | // remember that the NL was missing |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 5517 | curbuf->b_no_eol_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5518 | } |
| 5519 | else |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 5520 | curbuf->b_no_eol_lnum = 0; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5521 | ga_clear(&ga); |
| 5522 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5523 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5524 | /* |
| 5525 | * Give all typeahead that wasn't used back to ui_inchar(). |
| 5526 | */ |
| 5527 | if (ta_len) |
| 5528 | ui_inchar_undo(ta_buf, ta_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5529 | State = old_State; |
| 5530 | if (toshell_fd >= 0) |
| 5531 | close(toshell_fd); |
| 5532 | close(fromshell_fd); |
| 5533 | } |
Bram Moolenaar | 95a5135 | 2013-03-21 22:53:50 +0100 | [diff] [blame] | 5534 | # if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5535 | else |
| 5536 | { |
Bram Moolenaar | 0abe052 | 2016-08-28 16:53:12 +0200 | [diff] [blame] | 5537 | long delay_msec = 1; |
| 5538 | |
Bram Moolenaar | 8a3da6a | 2020-12-08 19:18:37 +0100 | [diff] [blame] | 5539 | if (tmode == TMODE_RAW) |
Bram Moolenaar | 63a2e36 | 2022-11-23 20:20:18 +0000 | [diff] [blame] | 5540 | // Possibly disables modifyOtherKeys, so that the system |
| 5541 | // can recognize CTRL-C. |
| 5542 | out_str_t_TE(); |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 5543 | |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5544 | /* |
| 5545 | * Similar to the loop above, but only handle X events, no |
| 5546 | * I/O. |
| 5547 | */ |
| 5548 | for (;;) |
| 5549 | { |
| 5550 | if (got_int) |
| 5551 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5552 | // CTRL-C sends a signal to the child, we ignore it |
| 5553 | // ourselves |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5554 | # ifdef HAVE_SETSID |
| 5555 | kill(-pid, SIGINT); |
| 5556 | # else |
| 5557 | kill(0, SIGINT); |
| 5558 | # endif |
| 5559 | got_int = FALSE; |
| 5560 | } |
| 5561 | # ifdef __NeXT__ |
| 5562 | wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0); |
| 5563 | # else |
| 5564 | wait_pid = waitpid(pid, &status, WNOHANG); |
| 5565 | # endif |
| 5566 | if ((wait_pid == (pid_t)-1 && errno == ECHILD) |
| 5567 | || (wait_pid == pid && WIFEXITED(status))) |
| 5568 | { |
| 5569 | wait_pid = pid; |
| 5570 | break; |
| 5571 | } |
| 5572 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5573 | // Handle any X events, e.g. serving the clipboard. |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5574 | clip_update(); |
| 5575 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5576 | // Wait for 1 to 10 msec. 1 is faster but gives the child |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 5577 | // less time, gradually wait longer. |
| 5578 | mch_delay(delay_msec, |
| 5579 | MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE); |
Bram Moolenaar | 0abe052 | 2016-08-28 16:53:12 +0200 | [diff] [blame] | 5580 | if (++delay_msec > 10) |
| 5581 | delay_msec = 10; |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5582 | } |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 5583 | |
Bram Moolenaar | 8a3da6a | 2020-12-08 19:18:37 +0100 | [diff] [blame] | 5584 | if (tmode == TMODE_RAW) |
| 5585 | // possibly enables modifyOtherKeys again |
Bram Moolenaar | 733a69b | 2022-12-01 12:03:47 +0000 | [diff] [blame] | 5586 | out_str_t_TI(); |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 5587 | } |
| 5588 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5589 | |
| 5590 | /* |
| 5591 | * Wait until our child has exited. |
| 5592 | * Ignore wait() returning pids of other children and returning |
| 5593 | * because of some signal like SIGWINCH. |
| 5594 | * Don't wait if wait_pid was already set above, indicating the |
| 5595 | * child already exited. |
| 5596 | */ |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 5597 | if (wait_pid != pid) |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 5598 | (void)wait4pid(pid, &status); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5599 | |
Bram Moolenaar | 624891f | 2010-10-13 16:22:09 +0200 | [diff] [blame] | 5600 | # ifdef FEAT_GUI |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5601 | // Close slave side of pty. Only do this after the child has |
| 5602 | // exited, otherwise the child may hang when it tries to write on |
| 5603 | // the pty. |
Bram Moolenaar | 624891f | 2010-10-13 16:22:09 +0200 | [diff] [blame] | 5604 | if (pty_master_fd >= 0) |
| 5605 | close(pty_slave_fd); |
| 5606 | # endif |
| 5607 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5608 | // Make sure the child that writes to the external program is |
| 5609 | // dead. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5610 | if (wpid > 0) |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 5611 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5612 | kill(wpid, SIGKILL); |
Bram Moolenaar | 205b886 | 2011-09-07 15:04:31 +0200 | [diff] [blame] | 5613 | wait4pid(wpid, NULL); |
| 5614 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5615 | |
Bram Moolenaar | c4d4ac2 | 2016-11-07 22:42:57 +0100 | [diff] [blame] | 5616 | # ifdef FEAT_JOB_CHANNEL |
| 5617 | --dont_check_job_ended; |
| 5618 | # endif |
| 5619 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5620 | /* |
| 5621 | * Set to raw mode right now, otherwise a CTRL-C after |
| 5622 | * catch_signals() will kill Vim. |
| 5623 | */ |
| 5624 | if (tmode == TMODE_RAW) |
| 5625 | settmode(TMODE_RAW); |
| 5626 | did_settmode = TRUE; |
| 5627 | set_signals(); |
| 5628 | |
| 5629 | if (WIFEXITED(status)) |
| 5630 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5631 | // LINTED avoid "bitwise operation on signed value" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5632 | retval = WEXITSTATUS(status); |
Bram Moolenaar | 7567646 | 2013-01-30 14:55:42 +0100 | [diff] [blame] | 5633 | if (retval != 0 && !emsg_silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | { |
| 5635 | if (retval == EXEC_FAILED) |
| 5636 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5637 | msg_puts(_("\nCannot execute shell ")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5638 | msg_outtrans(p_sh); |
| 5639 | msg_putchar('\n'); |
| 5640 | } |
| 5641 | else if (!(options & SHELL_SILENT)) |
| 5642 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5643 | msg_puts(_("\nshell returned ")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5644 | msg_outnum((long)retval); |
| 5645 | msg_putchar('\n'); |
| 5646 | } |
| 5647 | } |
| 5648 | } |
| 5649 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5650 | msg_puts(_("\nCommand terminated\n")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5651 | } |
| 5652 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5653 | |
| 5654 | error: |
| 5655 | if (!did_settmode) |
| 5656 | if (tmode == TMODE_RAW) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5657 | settmode(TMODE_RAW); // set to raw mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5658 | resettitle(); |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 5659 | vim_free(argv); |
| 5660 | vim_free(tofree1); |
| 5661 | vim_free(tofree2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5662 | |
| 5663 | return retval; |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 5664 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5665 | #endif // USE_SYSTEM |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 5666 | |
| 5667 | int |
| 5668 | mch_call_shell( |
| 5669 | char_u *cmd, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5670 | int options) // SHELL_*, see vim.h |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 5671 | { |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 5672 | #ifdef FEAT_EVAL |
Bram Moolenaar | c9a9a0a | 2022-04-12 15:09:23 +0100 | [diff] [blame] | 5673 | ch_log(NULL, "executing shell command: %s", cmd); |
| 5674 | #endif |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 5675 | #if defined(FEAT_GUI) && defined(FEAT_TERMINAL) |
Bram Moolenaar | 524c853 | 2022-09-27 15:48:20 +0100 | [diff] [blame] | 5676 | if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL |
| 5677 | && (options & SHELL_SILENT) == 0) |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 5678 | return mch_call_shell_terminal(cmd, options); |
| 5679 | #endif |
| 5680 | #ifdef USE_SYSTEM |
| 5681 | return mch_call_shell_system(cmd, options); |
| 5682 | #else |
| 5683 | return mch_call_shell_fork(cmd, options); |
| 5684 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 5687 | #if defined(FEAT_JOB_CHANNEL) || defined(PROTO) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5688 | void |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 5689 | mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5690 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5691 | pid_t pid; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5692 | int fd_in[2] = {-1, -1}; // for stdin |
| 5693 | int fd_out[2] = {-1, -1}; // for stdout |
| 5694 | int fd_err[2] = {-1, -1}; // for stderr |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5695 | int pty_master_fd = -1; |
| 5696 | int pty_slave_fd = -1; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 5697 | channel_T *channel = NULL; |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5698 | int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL; |
| 5699 | int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL; |
| 5700 | int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL; |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5701 | int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5702 | int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE; |
| 5703 | int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE; |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 5704 | int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER; |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5705 | int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT; |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5706 | SIGSET_DECL(curset) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5707 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5708 | if (use_out_for_err && use_null_for_out) |
| 5709 | use_null_for_err = TRUE; |
| 5710 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5711 | // default is to fail |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5712 | job->jv_status = JOB_FAILED; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5713 | |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 5714 | if (options->jo_pty |
| 5715 | && (!(use_file_for_in || use_null_for_in) |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 5716 | || !(use_file_for_out || use_null_for_out) |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 5717 | || !(use_out_for_err || use_file_for_err || use_null_for_err))) |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 5718 | open_pty(&pty_master_fd, &pty_slave_fd, |
| 5719 | &job->jv_tty_out, &job->jv_tty_in); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5720 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5721 | // TODO: without the channel feature connect the child to /dev/null? |
| 5722 | // Open pipes for stdin, stdout, stderr. |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5723 | if (use_file_for_in) |
| 5724 | { |
| 5725 | char_u *fname = options->jo_io_name[PART_IN]; |
| 5726 | |
| 5727 | fd_in[0] = mch_open((char *)fname, O_RDONLY, 0); |
| 5728 | if (fd_in[0] < 0) |
| 5729 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 5730 | semsg(_(e_cant_open_file_str), fname); |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5731 | goto failed; |
| 5732 | } |
| 5733 | } |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 5734 | else |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5735 | // When writing buffer lines to the input don't use the pty, so that |
| 5736 | // the pipe can be closed when all lines were written. |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 5737 | if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in) |
| 5738 | && pipe(fd_in) < 0) |
| 5739 | goto failed; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5740 | |
| 5741 | if (use_file_for_out) |
| 5742 | { |
| 5743 | char_u *fname = options->jo_io_name[PART_OUT]; |
| 5744 | |
| 5745 | fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 5746 | if (fd_out[1] < 0) |
| 5747 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 5748 | semsg(_(e_cant_open_file_str), fname); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5749 | goto failed; |
| 5750 | } |
| 5751 | } |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5752 | else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5753 | goto failed; |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5754 | |
| 5755 | if (use_file_for_err) |
| 5756 | { |
| 5757 | char_u *fname = options->jo_io_name[PART_ERR]; |
| 5758 | |
| 5759 | fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 5760 | if (fd_err[1] < 0) |
| 5761 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 5762 | semsg(_(e_cant_open_file_str), fname); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5763 | goto failed; |
| 5764 | } |
| 5765 | } |
Christian Brabandt | b50bc9a | 2024-09-30 21:29:43 +0200 | [diff] [blame] | 5766 | // only create a pipe for the error fd, when either a callback has been setup |
| 5767 | // or pty is not used (e.g. terminal uses pty by default) |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5768 | else if (!use_out_for_err && !use_null_for_err |
Christian Brabandt | b50bc9a | 2024-09-30 21:29:43 +0200 | [diff] [blame] | 5769 | && (pty_master_fd < 0 || (options->jo_set & JO_ERR_CALLBACK)) |
| 5770 | && pipe(fd_err) < 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5771 | goto failed; |
| 5772 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5773 | if (!use_null_for_in || !use_null_for_out || !use_null_for_err) |
| 5774 | { |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 5775 | if (options->jo_set & JO_CHANNEL) |
| 5776 | { |
| 5777 | channel = options->jo_channel; |
| 5778 | if (channel != NULL) |
| 5779 | ++channel->ch_refcount; |
| 5780 | } |
| 5781 | else |
| 5782 | channel = add_channel(); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5783 | if (channel == NULL) |
| 5784 | goto failed; |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 5785 | if (job->jv_tty_out != NULL) |
| 5786 | ch_log(channel, "using pty %s on fd %d", |
| 5787 | job->jv_tty_out, pty_master_fd); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5788 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5789 | |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5790 | BLOCK_SIGNALS(&curset); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5791 | pid = fork(); // maybe we should use vfork() |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5792 | if (pid == -1) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5793 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5794 | // failed to fork |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5795 | UNBLOCK_SIGNALS(&curset); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5796 | goto failed; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5797 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5798 | if (pid == 0) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5799 | { |
Bram Moolenaar | 4694a17 | 2016-04-21 14:05:23 +0200 | [diff] [blame] | 5800 | int null_fd = -1; |
| 5801 | int stderr_works = TRUE; |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5802 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5803 | // child |
| 5804 | reset_signals(); // handle signals normally |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5805 | UNBLOCK_SIGNALS(&curset); |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5806 | |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 5807 | # ifdef FEAT_EVAL |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 5808 | if (ch_log_active()) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5809 | // close the log file in the child |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 5810 | ch_logfile((char_u *)"", (char_u *)""); |
| 5811 | # endif |
| 5812 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5813 | # ifdef HAVE_SETSID |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5814 | // Create our own process group, so that the child and all its |
| 5815 | // children can be kill()ed. Don't do this when using pipes, |
| 5816 | // because stdin is not a tty, we would lose /dev/tty. |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5817 | (void)setsid(); |
| 5818 | # endif |
| 5819 | |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 5820 | # ifdef FEAT_TERMINAL |
| 5821 | if (options->jo_term_rows > 0) |
Bram Moolenaar | 9a993e3 | 2018-04-05 22:15:22 +0200 | [diff] [blame] | 5822 | { |
| 5823 | char *term = (char *)T_NAME; |
| 5824 | |
| 5825 | #ifdef FEAT_GUI |
| 5826 | if (term_is_gui(T_NAME)) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5827 | // In the GUI 'term' is not what we want, use $TERM. |
Bram Moolenaar | 9a993e3 | 2018-04-05 22:15:22 +0200 | [diff] [blame] | 5828 | term = getenv("TERM"); |
| 5829 | #endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5830 | // Use 'term' or $TERM if it starts with "xterm", otherwise fall |
Bram Moolenaar | 4d5d0df | 2020-04-14 20:56:31 +0200 | [diff] [blame] | 5831 | // back to "xterm" or "xterm-color". |
Bram Moolenaar | 9a993e3 | 2018-04-05 22:15:22 +0200 | [diff] [blame] | 5832 | if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0) |
Bram Moolenaar | 5ba8d35 | 2020-04-05 21:42:12 +0200 | [diff] [blame] | 5833 | { |
Bram Moolenaar | 5ba8d35 | 2020-04-05 21:42:12 +0200 | [diff] [blame] | 5834 | if (t_colors >= 256) |
Bram Moolenaar | 4d5d0df | 2020-04-14 20:56:31 +0200 | [diff] [blame] | 5835 | // TODO: should we check this name is supported? |
Bram Moolenaar | 5ba8d35 | 2020-04-05 21:42:12 +0200 | [diff] [blame] | 5836 | term = "xterm-256color"; |
Bram Moolenaar | 4d5d0df | 2020-04-14 20:56:31 +0200 | [diff] [blame] | 5837 | else if (t_colors > 16) |
| 5838 | term = "xterm-color"; |
Bram Moolenaar | 5ba8d35 | 2020-04-05 21:42:12 +0200 | [diff] [blame] | 5839 | else |
| 5840 | term = "xterm"; |
| 5841 | } |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 5842 | set_child_environment( |
| 5843 | (long)options->jo_term_rows, |
| 5844 | (long)options->jo_term_cols, |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 5845 | term, |
| 5846 | is_terminal); |
Bram Moolenaar | 9a993e3 | 2018-04-05 22:15:22 +0200 | [diff] [blame] | 5847 | } |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 5848 | else |
| 5849 | # endif |
Bram Moolenaar | 493359e | 2018-06-12 20:25:52 +0200 | [diff] [blame] | 5850 | set_default_child_environment(is_terminal); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5851 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 5852 | if (options->jo_env != NULL) |
| 5853 | { |
| 5854 | dict_T *dict = options->jo_env; |
| 5855 | hashitem_T *hi; |
| 5856 | int todo = (int)dict->dv_hashtab.ht_used; |
| 5857 | |
Yegappan Lakshmanan | 14113fd | 2023-03-07 17:13:51 +0000 | [diff] [blame] | 5858 | FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo) |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 5859 | if (!HASHITEM_EMPTY(hi)) |
| 5860 | { |
| 5861 | typval_T *item = &dict_lookup(hi)->di_tv; |
| 5862 | |
=?UTF-8?q?Dundar=20G=C3=B6c?= | 420fabc | 2022-01-28 15:28:04 +0000 | [diff] [blame] | 5863 | vim_setenv(hi->hi_key, tv_get_string(item)); |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 5864 | --todo; |
| 5865 | } |
| 5866 | } |
| 5867 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5868 | if (use_null_for_in || use_null_for_out || use_null_for_err) |
Bram Moolenaar | b109bb4 | 2017-08-21 21:07:29 +0200 | [diff] [blame] | 5869 | { |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5870 | null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0); |
Bram Moolenaar | b109bb4 | 2017-08-21 21:07:29 +0200 | [diff] [blame] | 5871 | if (null_fd < 0) |
| 5872 | { |
| 5873 | perror("opening /dev/null failed"); |
| 5874 | _exit(OPEN_NULL_FAILED); |
| 5875 | } |
| 5876 | } |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5877 | |
Bram Moolenaar | 223896d | 2017-08-02 22:33:28 +0200 | [diff] [blame] | 5878 | if (pty_slave_fd >= 0) |
| 5879 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5880 | // push stream discipline modules |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 5881 | setup_slavepty(pty_slave_fd); |
Bram Moolenaar | 223896d | 2017-08-02 22:33:28 +0200 | [diff] [blame] | 5882 | # ifdef TIOCSCTTY |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5883 | // Try to become controlling tty (probably doesn't work, |
| 5884 | // unless run by root) |
Bram Moolenaar | 223896d | 2017-08-02 22:33:28 +0200 | [diff] [blame] | 5885 | ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL); |
| 5886 | # endif |
| 5887 | } |
| 5888 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5889 | // set up stdin for the child |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5890 | close(0); |
Bram Moolenaar | c0a1d7f | 2016-03-19 14:12:50 +0100 | [diff] [blame] | 5891 | if (use_null_for_in && null_fd >= 0) |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5892 | vim_ignored = dup(null_fd); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5893 | else if (fd_in[0] < 0) |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5894 | vim_ignored = dup(pty_slave_fd); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5895 | else |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5896 | vim_ignored = dup(fd_in[0]); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5897 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5898 | // set up stderr for the child |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5899 | close(2); |
Bram Moolenaar | c0a1d7f | 2016-03-19 14:12:50 +0100 | [diff] [blame] | 5900 | if (use_null_for_err && null_fd >= 0) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5901 | { |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5902 | vim_ignored = dup(null_fd); |
Bram Moolenaar | 4694a17 | 2016-04-21 14:05:23 +0200 | [diff] [blame] | 5903 | stderr_works = FALSE; |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5904 | } |
| 5905 | else if (use_out_for_err) |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5906 | vim_ignored = dup(fd_out[1]); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5907 | else if (fd_err[1] < 0) |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5908 | vim_ignored = dup(pty_slave_fd); |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5909 | else |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5910 | vim_ignored = dup(fd_err[1]); |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5911 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5912 | // set up stdout for the child |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5913 | close(1); |
Bram Moolenaar | c0a1d7f | 2016-03-19 14:12:50 +0100 | [diff] [blame] | 5914 | if (use_null_for_out && null_fd >= 0) |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5915 | vim_ignored = dup(null_fd); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5916 | else if (fd_out[1] < 0) |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5917 | vim_ignored = dup(pty_slave_fd); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5918 | else |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 5919 | vim_ignored = dup(fd_out[1]); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5920 | |
| 5921 | if (fd_in[0] >= 0) |
| 5922 | close(fd_in[0]); |
| 5923 | if (fd_in[1] >= 0) |
| 5924 | close(fd_in[1]); |
| 5925 | if (fd_out[0] >= 0) |
| 5926 | close(fd_out[0]); |
| 5927 | if (fd_out[1] >= 0) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5928 | close(fd_out[1]); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5929 | if (fd_err[0] >= 0) |
| 5930 | close(fd_err[0]); |
| 5931 | if (fd_err[1] >= 0) |
| 5932 | close(fd_err[1]); |
| 5933 | if (pty_master_fd >= 0) |
| 5934 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5935 | close(pty_master_fd); // not used in the child |
| 5936 | close(pty_slave_fd); // was duped above |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5937 | } |
Bram Moolenaar | ea83bf0 | 2016-05-08 09:40:51 +0200 | [diff] [blame] | 5938 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5939 | if (null_fd >= 0) |
| 5940 | close(null_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5941 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 5942 | if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0) |
| 5943 | _exit(EXEC_FAILED); |
| 5944 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5945 | // See above for type of argv. |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5946 | execvp(argv[0], argv); |
| 5947 | |
Bram Moolenaar | 4694a17 | 2016-04-21 14:05:23 +0200 | [diff] [blame] | 5948 | if (stderr_works) |
| 5949 | perror("executing job failed"); |
Bram Moolenaar | fae4283 | 2017-08-01 22:24:26 +0200 | [diff] [blame] | 5950 | # ifdef EXITFREE |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5951 | // calling free_all_mem() here causes problems. Ignore valgrind |
| 5952 | // reporting possibly leaked memory. |
Bram Moolenaar | fae4283 | 2017-08-01 22:24:26 +0200 | [diff] [blame] | 5953 | # endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5954 | _exit(EXEC_FAILED); // exec failed, return failure code |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 5955 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5956 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5957 | // parent |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 5958 | UNBLOCK_SIGNALS(&curset); |
| 5959 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5960 | job->jv_pid = pid; |
| 5961 | job->jv_status = JOB_STARTED; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5962 | job->jv_channel = channel; // ch_refcount was set above |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 5963 | |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 5964 | if (pty_master_fd >= 0) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 5965 | close(pty_slave_fd); // not used in the parent |
| 5966 | // close child stdin, stdout and stderr |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 5967 | if (fd_in[0] >= 0) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 5968 | close(fd_in[0]); |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 5969 | if (fd_out[1] >= 0) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 5970 | close(fd_out[1]); |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 5971 | if (fd_err[1] >= 0) |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 5972 | close(fd_err[1]); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5973 | if (channel != NULL) |
| 5974 | { |
Bram Moolenaar | 652de23 | 2019-04-04 20:13:09 +0200 | [diff] [blame] | 5975 | int in_fd = INVALID_FD; |
| 5976 | int out_fd = INVALID_FD; |
| 5977 | int err_fd = INVALID_FD; |
| 5978 | |
| 5979 | if (!(use_file_for_in || use_null_for_in)) |
| 5980 | in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd; |
| 5981 | |
| 5982 | if (!(use_file_for_out || use_null_for_out)) |
| 5983 | out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd; |
| 5984 | |
| 5985 | // When using pty_master_fd only set it for stdout, do not duplicate |
| 5986 | // it for stderr, it only needs to be read once. |
| 5987 | if (!(use_out_for_err || use_file_for_err || use_null_for_err)) |
| 5988 | { |
| 5989 | if (fd_err[0] >= 0) |
| 5990 | err_fd = fd_err[0]; |
| 5991 | else if (out_fd != pty_master_fd) |
| 5992 | err_fd = pty_master_fd; |
| 5993 | } |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 5994 | |
| 5995 | channel_set_pipes(channel, in_fd, out_fd, err_fd); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5996 | channel_set_job(channel, job, options); |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 5997 | } |
Bram Moolenaar | 979e8c5 | 2017-08-01 15:08:07 +0200 | [diff] [blame] | 5998 | else |
| 5999 | { |
| 6000 | if (fd_in[1] >= 0) |
| 6001 | close(fd_in[1]); |
| 6002 | if (fd_out[0] >= 0) |
| 6003 | close(fd_out[0]); |
| 6004 | if (fd_err[0] >= 0) |
| 6005 | close(fd_err[0]); |
| 6006 | if (pty_master_fd >= 0) |
| 6007 | close(pty_master_fd); |
| 6008 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6009 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6010 | // success! |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6011 | return; |
| 6012 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 6013 | failed: |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 6014 | channel_unref(channel); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6015 | if (fd_in[0] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6016 | close(fd_in[0]); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 6017 | if (fd_in[1] >= 0) |
| 6018 | close(fd_in[1]); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6019 | if (fd_out[0] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6020 | close(fd_out[0]); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 6021 | if (fd_out[1] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6022 | close(fd_out[1]); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6023 | if (fd_err[0] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6024 | close(fd_err[0]); |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 6025 | if (fd_err[1] >= 0) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6026 | close(fd_err[1]); |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 6027 | if (pty_master_fd >= 0) |
| 6028 | close(pty_master_fd); |
| 6029 | if (pty_slave_fd >= 0) |
| 6030 | close(pty_slave_fd); |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6031 | } |
| 6032 | |
Bram Moolenaar | b3051ce | 2019-01-31 15:52:11 +0100 | [diff] [blame] | 6033 | static char_u * |
| 6034 | get_signal_name(int sig) |
| 6035 | { |
| 6036 | int i; |
| 6037 | char_u numbuf[NUMBUFLEN]; |
| 6038 | |
| 6039 | if (sig == SIGKILL) |
| 6040 | return vim_strsave((char_u *)"kill"); |
| 6041 | |
| 6042 | for (i = 0; signal_info[i].sig != -1; i++) |
| 6043 | if (sig == signal_info[i].sig) |
| 6044 | return strlow_save((char_u *)signal_info[i].name); |
| 6045 | |
| 6046 | vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig); |
| 6047 | return vim_strsave(numbuf); |
| 6048 | } |
| 6049 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6050 | char * |
| 6051 | mch_job_status(job_T *job) |
| 6052 | { |
| 6053 | # ifdef HAVE_UNION_WAIT |
| 6054 | union wait status; |
| 6055 | # else |
| 6056 | int status = -1; |
| 6057 | # endif |
| 6058 | pid_t wait_pid = 0; |
| 6059 | |
| 6060 | # ifdef __NeXT__ |
| 6061 | wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0); |
| 6062 | # else |
| 6063 | wait_pid = waitpid(job->jv_pid, &status, WNOHANG); |
| 6064 | # endif |
| 6065 | if (wait_pid == -1) |
| 6066 | { |
Bram Moolenaar | 5c6a3c9 | 2023-03-04 13:23:26 +0000 | [diff] [blame] | 6067 | int waitpid_errno = errno; |
| 6068 | if (waitpid_errno == ECHILD && mch_process_running(job->jv_pid)) |
| 6069 | // The process is alive, but it was probably reparented (for |
| 6070 | // example by ptrace called by a debugger like lldb or gdb). |
| 6071 | // Note: This assumes that process IDs are not reused. |
| 6072 | return "run"; |
| 6073 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6074 | // process must have exited |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 6075 | if (job->jv_status < JOB_ENDED) |
| 6076 | ch_log(job->jv_channel, "Job no longer exists: %s", |
Bram Moolenaar | 5c6a3c9 | 2023-03-04 13:23:26 +0000 | [diff] [blame] | 6077 | strerror(waitpid_errno)); |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6078 | goto return_dead; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6079 | } |
| 6080 | if (wait_pid == 0) |
| 6081 | return "run"; |
| 6082 | if (WIFEXITED(status)) |
| 6083 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6084 | // LINTED avoid "bitwise operation on signed value" |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6085 | job->jv_exitval = WEXITSTATUS(status); |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 6086 | if (job->jv_status < JOB_ENDED) |
| 6087 | ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval); |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6088 | goto return_dead; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6089 | } |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 6090 | if (WIFSIGNALED(status)) |
| 6091 | { |
| 6092 | job->jv_exitval = -1; |
Bram Moolenaar | b3051ce | 2019-01-31 15:52:11 +0100 | [diff] [blame] | 6093 | job->jv_termsig = get_signal_name(WTERMSIG(status)); |
| 6094 | if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL) |
| 6095 | ch_log(job->jv_channel, "Job terminated by signal \"%s\"", |
| 6096 | job->jv_termsig); |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6097 | goto return_dead; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 6098 | } |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6099 | return "run"; |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6100 | |
| 6101 | return_dead: |
Bram Moolenaar | 7df915d | 2016-11-17 17:25:32 +0100 | [diff] [blame] | 6102 | if (job->jv_status < JOB_ENDED) |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6103 | job->jv_status = JOB_ENDED; |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6104 | return "dead"; |
| 6105 | } |
| 6106 | |
| 6107 | job_T * |
| 6108 | mch_detect_ended_job(job_T *job_list) |
| 6109 | { |
| 6110 | # ifdef HAVE_UNION_WAIT |
| 6111 | union wait status; |
| 6112 | # else |
| 6113 | int status = -1; |
| 6114 | # endif |
| 6115 | pid_t wait_pid = 0; |
| 6116 | job_T *job; |
| 6117 | |
Bram Moolenaar | c4d4ac2 | 2016-11-07 22:42:57 +0100 | [diff] [blame] | 6118 | # ifndef USE_SYSTEM |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6119 | // Do not do this when waiting for a shell command to finish, we would get |
| 6120 | // the exit value here (and discard it), the exit value obtained there |
| 6121 | // would then be wrong. |
Bram Moolenaar | c4d4ac2 | 2016-11-07 22:42:57 +0100 | [diff] [blame] | 6122 | if (dont_check_job_ended > 0) |
| 6123 | return NULL; |
| 6124 | # endif |
| 6125 | |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6126 | # ifdef __NeXT__ |
| 6127 | wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0); |
| 6128 | # else |
| 6129 | wait_pid = waitpid(-1, &status, WNOHANG); |
| 6130 | # endif |
| 6131 | if (wait_pid <= 0) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6132 | // no process ended |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6133 | return NULL; |
| 6134 | for (job = job_list; job != NULL; job = job->jv_next) |
| 6135 | { |
| 6136 | if (job->jv_pid == wait_pid) |
| 6137 | { |
| 6138 | if (WIFEXITED(status)) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6139 | // LINTED avoid "bitwise operation on signed value" |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6140 | job->jv_exitval = WEXITSTATUS(status); |
| 6141 | else if (WIFSIGNALED(status)) |
Bram Moolenaar | b3051ce | 2019-01-31 15:52:11 +0100 | [diff] [blame] | 6142 | { |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6143 | job->jv_exitval = -1; |
Bram Moolenaar | b3051ce | 2019-01-31 15:52:11 +0100 | [diff] [blame] | 6144 | job->jv_termsig = get_signal_name(WTERMSIG(status)); |
| 6145 | } |
Bram Moolenaar | 7df915d | 2016-11-17 17:25:32 +0100 | [diff] [blame] | 6146 | if (job->jv_status < JOB_ENDED) |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 6147 | { |
| 6148 | ch_log(job->jv_channel, "Job ended"); |
| 6149 | job->jv_status = JOB_ENDED; |
| 6150 | } |
| 6151 | return job; |
| 6152 | } |
| 6153 | } |
| 6154 | return NULL; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6155 | } |
| 6156 | |
Bram Moolenaar | 3a117e1 | 2016-10-30 21:57:52 +0100 | [diff] [blame] | 6157 | /* |
| 6158 | * Send a (deadly) signal to "job". |
| 6159 | * Return FAIL if "how" is not a valid name. |
| 6160 | */ |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6161 | int |
Bram Moolenaar | 2d33e90 | 2017-08-11 16:31:54 +0200 | [diff] [blame] | 6162 | mch_signal_job(job_T *job, char_u *how) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6163 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 6164 | int sig = -1; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6165 | |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 6166 | if (*how == NUL || STRCMP(how, "term") == 0) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6167 | sig = SIGTERM; |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 6168 | else if (STRCMP(how, "hup") == 0) |
| 6169 | sig = SIGHUP; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6170 | else if (STRCMP(how, "quit") == 0) |
| 6171 | sig = SIGQUIT; |
Bram Moolenaar | 923d926 | 2016-02-25 20:56:01 +0100 | [diff] [blame] | 6172 | else if (STRCMP(how, "int") == 0) |
| 6173 | sig = SIGINT; |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6174 | else if (STRCMP(how, "kill") == 0) |
| 6175 | sig = SIGKILL; |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 6176 | #ifdef SIGWINCH |
| 6177 | else if (STRCMP(how, "winch") == 0) |
| 6178 | sig = SIGWINCH; |
| 6179 | #endif |
Keith Thompson | 184f71c | 2024-01-04 21:19:04 +0100 | [diff] [blame] | 6180 | else if (SAFE_isdigit(*how)) |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6181 | sig = atoi((char *)how); |
| 6182 | else |
| 6183 | return FAIL; |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 6184 | |
Bram Moolenaar | 76ab4fd | 2018-12-08 14:39:05 +0100 | [diff] [blame] | 6185 | // Never kill ourselves! |
| 6186 | if (job->jv_pid != 0) |
| 6187 | { |
| 6188 | // TODO: have an option to only kill the process, not the group? |
| 6189 | kill(-job->jv_pid, sig); |
| 6190 | kill(job->jv_pid, sig); |
| 6191 | } |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 6192 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6193 | return OK; |
| 6194 | } |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 6195 | |
| 6196 | /* |
| 6197 | * Clear the data related to "job". |
| 6198 | */ |
| 6199 | void |
| 6200 | mch_clear_job(job_T *job) |
| 6201 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6202 | // call waitpid because child process may become zombie |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 6203 | # ifdef __NeXT__ |
Bram Moolenaar | 4ca812b | 2016-03-02 21:51:16 +0100 | [diff] [blame] | 6204 | (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0); |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 6205 | # else |
Bram Moolenaar | 4ca812b | 2016-03-02 21:51:16 +0100 | [diff] [blame] | 6206 | (void)waitpid(job->jv_pid, NULL, WNOHANG); |
Bram Moolenaar | 76467df | 2016-02-12 19:30:26 +0100 | [diff] [blame] | 6207 | # endif |
| 6208 | } |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 6209 | #endif |
| 6210 | |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 6211 | #if defined(FEAT_TERMINAL) || defined(PROTO) |
| 6212 | int |
| 6213 | mch_create_pty_channel(job_T *job, jobopt_T *options) |
| 6214 | { |
| 6215 | int pty_master_fd = -1; |
| 6216 | int pty_slave_fd = -1; |
| 6217 | channel_T *channel; |
| 6218 | |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 6219 | open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in); |
Bram Moolenaar | d034220 | 2020-06-29 22:40:42 +0200 | [diff] [blame] | 6220 | if (pty_master_fd < 0 || pty_slave_fd < 0) |
| 6221 | return FAIL; |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 6222 | close(pty_slave_fd); |
| 6223 | |
| 6224 | channel = add_channel(); |
| 6225 | if (channel == NULL) |
Bram Moolenaar | 1b9f9d3 | 2017-09-05 23:32:38 +0200 | [diff] [blame] | 6226 | { |
| 6227 | close(pty_master_fd); |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 6228 | return FAIL; |
Bram Moolenaar | 1b9f9d3 | 2017-09-05 23:32:38 +0200 | [diff] [blame] | 6229 | } |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 6230 | if (job->jv_tty_out != NULL) |
| 6231 | ch_log(channel, "using pty %s on fd %d", |
| 6232 | job->jv_tty_out, pty_master_fd); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6233 | job->jv_channel = channel; // ch_refcount was set by add_channel() |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 6234 | channel->ch_keep_open = TRUE; |
| 6235 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6236 | // Only set the pty_master_fd for stdout, do not duplicate it for stderr, |
| 6237 | // it only needs to be read once. |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 6238 | channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD); |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 6239 | channel_set_job(channel, job, options); |
| 6240 | return OK; |
| 6241 | } |
| 6242 | #endif |
| 6243 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6244 | /* |
| 6245 | * Check for CTRL-C typed by reading all available characters. |
| 6246 | * In cooked mode we should get SIGINT, no need to check. |
| 6247 | */ |
| 6248 | void |
Bram Moolenaar | b9c31e7 | 2016-09-29 15:18:57 +0200 | [diff] [blame] | 6249 | mch_breakcheck(int force) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6250 | { |
Bram Moolenaar | 26e8644 | 2020-05-17 14:06:16 +0200 | [diff] [blame] | 6251 | if ((mch_cur_tmode == TMODE_RAW || force) |
Bram Moolenaar | b9c31e7 | 2016-09-29 15:18:57 +0200 | [diff] [blame] | 6252 | && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6253 | fill_input_buf(FALSE); |
| 6254 | } |
| 6255 | |
| 6256 | /* |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 6257 | * Wait "msec" msec until a character is available from the mouse, keyboard, |
| 6258 | * from inbuf[]. |
| 6259 | * "msec" == -1 will block forever. |
| 6260 | * Invokes timer callbacks when needed. |
Bram Moolenaar | e9c21ae | 2017-08-03 20:44:48 +0200 | [diff] [blame] | 6261 | * When "ignore_input" is TRUE even check for pending input when input is |
| 6262 | * already available. |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6263 | * "interrupted" (if not NULL) is set to TRUE when no character is available |
| 6264 | * but something else needs to be done. |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 6265 | * Returns TRUE when a character is available. |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6266 | * When a GUI is being used, this will never get called -- webb |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6267 | */ |
| 6268 | static int |
Bram Moolenaar | e9c21ae | 2017-08-03 20:44:48 +0200 | [diff] [blame] | 6269 | WaitForChar(long msec, int *interrupted, int ignore_input) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6270 | { |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 6271 | #ifdef FEAT_TIMERS |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 6272 | return ui_wait_for_chars_or_timer( |
| 6273 | msec, WaitForCharOrMouse, interrupted, ignore_input) == OK; |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 6274 | #else |
Bram Moolenaar | e9c21ae | 2017-08-03 20:44:48 +0200 | [diff] [blame] | 6275 | return WaitForCharOrMouse(msec, interrupted, ignore_input); |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 6276 | #endif |
| 6277 | } |
| 6278 | |
| 6279 | /* |
| 6280 | * Wait "msec" msec until a character is available from the mouse or keyboard |
| 6281 | * or from inbuf[]. |
| 6282 | * "msec" == -1 will block forever. |
Bram Moolenaar | e9c21ae | 2017-08-03 20:44:48 +0200 | [diff] [blame] | 6283 | * for "ignore_input" see WaitForCharOr(). |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6284 | * "interrupted" (if not NULL) is set to TRUE when no character is available |
| 6285 | * but something else needs to be done. |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 6286 | * When a GUI is being used, this will never get called -- webb |
| 6287 | */ |
| 6288 | static int |
Bram Moolenaar | e9c21ae | 2017-08-03 20:44:48 +0200 | [diff] [blame] | 6289 | WaitForCharOrMouse(long msec, int *interrupted, int ignore_input) |
Bram Moolenaar | 943bb2b | 2016-03-19 14:11:18 +0100 | [diff] [blame] | 6290 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6291 | #ifdef FEAT_MOUSE_GPM |
| 6292 | int gpm_process_wanted; |
| 6293 | #endif |
| 6294 | #ifdef FEAT_XCLIPBOARD |
| 6295 | int rest; |
| 6296 | #endif |
| 6297 | int avail; |
| 6298 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6299 | if (!ignore_input && input_available()) // something in inbuf[] |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6300 | return 1; |
| 6301 | |
| 6302 | #if defined(FEAT_MOUSE_DEC) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6303 | // May need to query the mouse position. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6304 | if (WantQueryMouse) |
| 6305 | { |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 6306 | WantQueryMouse = FALSE; |
Bram Moolenaar | 92fd599 | 2019-05-02 23:00:22 +0200 | [diff] [blame] | 6307 | if (!no_query_mouse_for_testing) |
Bram Moolenaar | 424bcae | 2022-01-31 14:59:41 +0000 | [diff] [blame] | 6308 | mch_write((char_u *)"\033[1'|", 5); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6309 | } |
| 6310 | #endif |
| 6311 | |
| 6312 | /* |
| 6313 | * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse |
| 6314 | * events. This is a bit complicated, because they might both be defined. |
| 6315 | */ |
| 6316 | #if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD) |
| 6317 | # ifdef FEAT_XCLIPBOARD |
| 6318 | rest = 0; |
| 6319 | if (do_xterm_trace()) |
| 6320 | rest = msec; |
| 6321 | # endif |
| 6322 | do |
| 6323 | { |
| 6324 | # ifdef FEAT_XCLIPBOARD |
| 6325 | if (rest != 0) |
| 6326 | { |
| 6327 | msec = XT_TRACE_DELAY; |
| 6328 | if (rest >= 0 && rest < XT_TRACE_DELAY) |
| 6329 | msec = rest; |
| 6330 | if (rest >= 0) |
| 6331 | rest -= msec; |
| 6332 | } |
| 6333 | # endif |
Yee Cheng Chin | 4314e4f | 2022-10-08 13:50:05 +0100 | [diff] [blame] | 6334 | # ifdef FEAT_SOUND_MACOSX |
| 6335 | // Invoke any pending sound callbacks. |
| 6336 | process_cfrunloop(); |
| 6337 | # endif |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 6338 | # ifdef FEAT_SOUND_CANBERRA |
| 6339 | // Invoke any pending sound callbacks. |
| 6340 | if (has_sound_callback_in_queue()) |
| 6341 | invoke_sound_callback(); |
| 6342 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6343 | # ifdef FEAT_MOUSE_GPM |
| 6344 | gpm_process_wanted = 0; |
Bram Moolenaar | 8fdd721 | 2016-03-26 19:41:48 +0100 | [diff] [blame] | 6345 | avail = RealWaitForChar(read_cmd_fd, msec, |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6346 | &gpm_process_wanted, interrupted); |
Bram Moolenaar | b5432d8 | 2019-08-30 19:28:25 +0200 | [diff] [blame] | 6347 | if (!avail && !gpm_process_wanted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6348 | # else |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6349 | avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6350 | if (!avail) |
Bram Moolenaar | b5432d8 | 2019-08-30 19:28:25 +0200 | [diff] [blame] | 6351 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6352 | { |
Bram Moolenaar | e9c21ae | 2017-08-03 20:44:48 +0200 | [diff] [blame] | 6353 | if (!ignore_input && input_available()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6354 | return 1; |
| 6355 | # ifdef FEAT_XCLIPBOARD |
| 6356 | if (rest == 0 || !do_xterm_trace()) |
| 6357 | # endif |
| 6358 | break; |
| 6359 | } |
| 6360 | } |
| 6361 | while (FALSE |
| 6362 | # ifdef FEAT_MOUSE_GPM |
| 6363 | || (gpm_process_wanted && mch_gpm_process() == 0) |
| 6364 | # endif |
| 6365 | # ifdef FEAT_XCLIPBOARD |
| 6366 | || (!avail && rest != 0) |
| 6367 | # endif |
Bram Moolenaar | 8fdd721 | 2016-03-26 19:41:48 +0100 | [diff] [blame] | 6368 | ) |
| 6369 | ; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6370 | |
| 6371 | #else |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6372 | avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6373 | #endif |
| 6374 | return avail; |
| 6375 | } |
| 6376 | |
Bram Moolenaar | 4ffa070 | 2013-12-11 17:12:37 +0100 | [diff] [blame] | 6377 | #ifndef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6378 | /* |
| 6379 | * Wait "msec" msec until a character is available from file descriptor "fd". |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 6380 | * "msec" == 0 will check for characters once. |
| 6381 | * "msec" == -1 will block until a character is available. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6382 | * 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] | 6383 | * Or when a Linux GPM mouse event is waiting. |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 6384 | * Or when a clientserver message is on the queue. |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6385 | * "interrupted" (if not NULL) is set to TRUE when no character is available |
| 6386 | * but something else needs to be done. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6387 | */ |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 6388 | static int |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6389 | RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6390 | { |
| 6391 | int ret; |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 6392 | int result; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6393 | #if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6394 | static int busy = FALSE; |
| 6395 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6396 | // May retry getting characters after an event was handled. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6397 | # define MAY_LOOP |
| 6398 | |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 6399 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6400 | // Remember at what time we started, so that we know how much longer we |
| 6401 | // should wait after being interrupted. |
Bram Moolenaar | 1ac56c2 | 2019-01-17 22:28:22 +0100 | [diff] [blame] | 6402 | long start_msec = msec; |
| 6403 | elapsed_T start_tv; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6404 | |
Bram Moolenaar | 76b6dfe | 2016-06-04 14:37:22 +0200 | [diff] [blame] | 6405 | if (msec > 0) |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 6406 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6407 | # endif |
| 6408 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6409 | // Handle being called recursively. This may happen for the session |
| 6410 | // manager stuff, it may save the file, which does a breakcheck. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6411 | if (busy) |
| 6412 | return 0; |
| 6413 | #endif |
| 6414 | |
| 6415 | #ifdef MAY_LOOP |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 6416 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6417 | #endif |
| 6418 | { |
| 6419 | #ifdef MAY_LOOP |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6420 | int finished = TRUE; // default is to 'loop' just once |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6421 | # ifdef FEAT_MZSCHEME |
| 6422 | int mzquantum_used = FALSE; |
| 6423 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6424 | #endif |
| 6425 | #ifndef HAVE_SELECT |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6426 | // each channel may use in, out and err |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 6427 | struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6428 | int nfd; |
| 6429 | # ifdef FEAT_XCLIPBOARD |
| 6430 | int xterm_idx = -1; |
| 6431 | # endif |
| 6432 | # ifdef FEAT_MOUSE_GPM |
| 6433 | int gpm_idx = -1; |
| 6434 | # endif |
| 6435 | # ifdef USE_XSMP |
| 6436 | int xsmp_idx = -1; |
| 6437 | # endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6438 | int towait = (int)msec; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6439 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6440 | # ifdef FEAT_MZSCHEME |
| 6441 | mzvim_check_threads(); |
| 6442 | if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq)) |
| 6443 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6444 | towait = (int)p_mzq; // don't wait longer than 'mzquantum' |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6445 | mzquantum_used = TRUE; |
| 6446 | } |
| 6447 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6448 | fds[0].fd = fd; |
| 6449 | fds[0].events = POLLIN; |
| 6450 | nfd = 1; |
| 6451 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6452 | # ifdef FEAT_XCLIPBOARD |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 6453 | may_restore_clipboard(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6454 | if (xterm_Shell != (Widget)0) |
| 6455 | { |
| 6456 | xterm_idx = nfd; |
| 6457 | fds[nfd].fd = ConnectionNumber(xterm_dpy); |
| 6458 | fds[nfd].events = POLLIN; |
| 6459 | nfd++; |
| 6460 | } |
| 6461 | # endif |
| 6462 | # ifdef FEAT_MOUSE_GPM |
| 6463 | if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0) |
| 6464 | { |
| 6465 | gpm_idx = nfd; |
| 6466 | fds[nfd].fd = gpm_fd; |
| 6467 | fds[nfd].events = POLLIN; |
| 6468 | nfd++; |
| 6469 | } |
| 6470 | # endif |
| 6471 | # ifdef USE_XSMP |
| 6472 | if (xsmp_icefd != -1) |
| 6473 | { |
| 6474 | xsmp_idx = nfd; |
| 6475 | fds[nfd].fd = xsmp_icefd; |
| 6476 | fds[nfd].events = POLLIN; |
| 6477 | nfd++; |
| 6478 | } |
| 6479 | # endif |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 6480 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 6481 | nfd = channel_poll_setup(nfd, &fds, &towait); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 6482 | #endif |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6483 | if (interrupted != NULL) |
| 6484 | *interrupted = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6485 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6486 | ret = poll(fds, nfd, towait); |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 6487 | |
| 6488 | result = ret > 0 && (fds[0].revents & POLLIN); |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6489 | if (result == 0 && interrupted != NULL && ret > 0) |
| 6490 | *interrupted = TRUE; |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 6491 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6492 | # ifdef FEAT_MZSCHEME |
| 6493 | if (ret == 0 && mzquantum_used) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6494 | // MzThreads scheduling is required and timeout occurred |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6495 | finished = FALSE; |
| 6496 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6497 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6498 | # ifdef FEAT_XCLIPBOARD |
| 6499 | if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN)) |
| 6500 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6501 | xterm_update(); // Maybe we should hand out clipboard |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6502 | if (--ret == 0 && !input_available()) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6503 | // Try again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6504 | finished = FALSE; |
| 6505 | } |
| 6506 | # endif |
| 6507 | # ifdef FEAT_MOUSE_GPM |
| 6508 | if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6509 | *check_for_gpm = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6510 | # endif |
| 6511 | # ifdef USE_XSMP |
| 6512 | if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP))) |
| 6513 | { |
| 6514 | if (fds[xsmp_idx].revents & POLLIN) |
| 6515 | { |
| 6516 | busy = TRUE; |
| 6517 | xsmp_handle_requests(); |
| 6518 | busy = FALSE; |
| 6519 | } |
| 6520 | else if (fds[xsmp_idx].revents & POLLHUP) |
| 6521 | { |
| 6522 | if (p_verbose > 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 6523 | verb_msg(_("XSMP lost ICE connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6524 | xsmp_close(); |
| 6525 | } |
| 6526 | if (--ret == 0) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6527 | finished = FALSE; // Try again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6528 | } |
| 6529 | # endif |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 6530 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 2c519cf | 2019-03-21 21:45:34 +0100 | [diff] [blame] | 6531 | // also call when ret == 0, we may be polling a keep-open channel |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 6532 | if (ret >= 0) |
Bram Moolenaar | 2c519cf | 2019-03-21 21:45:34 +0100 | [diff] [blame] | 6533 | channel_poll_check(ret, &fds); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 6534 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6535 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6536 | #else // HAVE_SELECT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6537 | |
| 6538 | struct timeval tv; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6539 | struct timeval *tvp; |
Bram Moolenaar | 61fb8d8 | 2018-11-12 21:45:08 +0100 | [diff] [blame] | 6540 | // These are static because they can take 8 Kbyte each and cause the |
| 6541 | // signal stack to run out with -O3. |
| 6542 | static fd_set rfds, wfds, efds; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6543 | int maxfd; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6544 | long towait = msec; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6545 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6546 | # ifdef FEAT_MZSCHEME |
| 6547 | mzvim_check_threads(); |
| 6548 | if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq)) |
| 6549 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6550 | towait = p_mzq; // don't wait longer than 'mzquantum' |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6551 | mzquantum_used = TRUE; |
| 6552 | } |
| 6553 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6554 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6555 | if (towait >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6556 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6557 | tv.tv_sec = towait / 1000; |
| 6558 | tv.tv_usec = (towait % 1000) * (1000000/1000); |
| 6559 | tvp = &tv; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6560 | } |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6561 | else |
| 6562 | tvp = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6563 | |
| 6564 | /* |
| 6565 | * Select on ready for reading and exceptional condition (end of file). |
| 6566 | */ |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 6567 | select_eintr: |
| 6568 | FD_ZERO(&rfds); |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 6569 | FD_ZERO(&wfds); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6570 | FD_ZERO(&efds); |
| 6571 | FD_SET(fd, &rfds); |
K.Takata | b247e06 | 2022-02-07 10:45:23 +0000 | [diff] [blame] | 6572 | # ifndef __QNX__ |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6573 | // For QNX select() always returns 1 if this is set. Why? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6574 | FD_SET(fd, &efds); |
| 6575 | # endif |
| 6576 | maxfd = fd; |
| 6577 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6578 | # ifdef FEAT_XCLIPBOARD |
Bram Moolenaar | b1e2650 | 2014-11-19 18:48:46 +0100 | [diff] [blame] | 6579 | may_restore_clipboard(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6580 | if (xterm_Shell != (Widget)0) |
| 6581 | { |
| 6582 | FD_SET(ConnectionNumber(xterm_dpy), &rfds); |
| 6583 | if (maxfd < ConnectionNumber(xterm_dpy)) |
| 6584 | maxfd = ConnectionNumber(xterm_dpy); |
Bram Moolenaar | dd82d69 | 2012-08-15 17:26:57 +0200 | [diff] [blame] | 6585 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6586 | // An event may have already been read but not handled. In |
| 6587 | // particularly, XFlush may cause this. |
Bram Moolenaar | dd82d69 | 2012-08-15 17:26:57 +0200 | [diff] [blame] | 6588 | xterm_update(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6589 | } |
| 6590 | # endif |
| 6591 | # ifdef FEAT_MOUSE_GPM |
| 6592 | if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0) |
| 6593 | { |
| 6594 | FD_SET(gpm_fd, &rfds); |
| 6595 | FD_SET(gpm_fd, &efds); |
| 6596 | if (maxfd < gpm_fd) |
| 6597 | maxfd = gpm_fd; |
| 6598 | } |
| 6599 | # endif |
| 6600 | # ifdef USE_XSMP |
| 6601 | if (xsmp_icefd != -1) |
| 6602 | { |
| 6603 | FD_SET(xsmp_icefd, &rfds); |
| 6604 | FD_SET(xsmp_icefd, &efds); |
| 6605 | if (maxfd < xsmp_icefd) |
| 6606 | maxfd = xsmp_icefd; |
| 6607 | } |
| 6608 | # endif |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 6609 | # ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 6610 | maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp); |
Bram Moolenaar | dd82d69 | 2012-08-15 17:26:57 +0200 | [diff] [blame] | 6611 | # endif |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6612 | if (interrupted != NULL) |
| 6613 | *interrupted = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6614 | |
Bram Moolenaar | 643b614 | 2018-09-12 20:29:09 +0200 | [diff] [blame] | 6615 | ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds, |
| 6616 | SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp); |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 6617 | result = ret > 0 && FD_ISSET(fd, &rfds); |
| 6618 | if (result) |
| 6619 | --ret; |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 6620 | else if (interrupted != NULL && ret > 0) |
| 6621 | *interrupted = TRUE; |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 6622 | |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 6623 | # ifdef EINTR |
| 6624 | if (ret == -1 && errno == EINTR) |
Bram Moolenaar | 2e7b1df | 2011-10-12 21:04:20 +0200 | [diff] [blame] | 6625 | { |
dbivolaru | ab16ad3 | 2021-12-29 19:41:47 +0000 | [diff] [blame] | 6626 | // Check whether the EINTR is caused by SIGTSTP |
| 6627 | if (got_tstp && !in_mch_suspend) |
| 6628 | { |
| 6629 | exarg_T ea; |
dbivolaru | 79a6e25 | 2022-01-23 16:41:14 +0000 | [diff] [blame] | 6630 | |
dbivolaru | ab16ad3 | 2021-12-29 19:41:47 +0000 | [diff] [blame] | 6631 | ea.forceit = TRUE; |
| 6632 | ex_stop(&ea); |
| 6633 | got_tstp = FALSE; |
| 6634 | } |
| 6635 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6636 | // Check whether window has been resized, EINTR may be caused by |
| 6637 | // SIGWINCH. |
Bram Moolenaar | 2e7b1df | 2011-10-12 21:04:20 +0200 | [diff] [blame] | 6638 | if (do_resize) |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 6639 | { |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 6640 | # ifdef FEAT_EVAL |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 6641 | ch_log(NULL, "calling handle_resize() in RealWaitForChar()"); |
Bram Moolenaar | 545c8a5 | 2023-06-21 15:51:47 +0100 | [diff] [blame] | 6642 | # endif |
Bram Moolenaar | 2e7b1df | 2011-10-12 21:04:20 +0200 | [diff] [blame] | 6643 | handle_resize(); |
Bram Moolenaar | 55f1b82 | 2023-06-21 13:42:48 +0100 | [diff] [blame] | 6644 | } |
Bram Moolenaar | 2e7b1df | 2011-10-12 21:04:20 +0200 | [diff] [blame] | 6645 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6646 | // Interrupted by a signal, need to try again. We ignore msec |
| 6647 | // here, because we do want to check even after a timeout if |
| 6648 | // characters are available. Needed for reading output of an |
| 6649 | // external command after the process has finished. |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 6650 | goto select_eintr; |
Bram Moolenaar | 2e7b1df | 2011-10-12 21:04:20 +0200 | [diff] [blame] | 6651 | } |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 6652 | # endif |
Bram Moolenaar | 311d982 | 2007-02-27 15:48:28 +0000 | [diff] [blame] | 6653 | # ifdef __TANDEM |
| 6654 | if (ret == -1 && errno == ENOTSUP) |
| 6655 | { |
| 6656 | FD_ZERO(&rfds); |
| 6657 | FD_ZERO(&efds); |
| 6658 | ret = 0; |
| 6659 | } |
Bram Moolenaar | 493c7a8 | 2011-09-07 14:06:47 +0200 | [diff] [blame] | 6660 | # endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6661 | # ifdef FEAT_MZSCHEME |
| 6662 | if (ret == 0 && mzquantum_used) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6663 | // loop if MzThreads must be scheduled and timeout occurred |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 6664 | finished = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6665 | # endif |
| 6666 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6667 | # ifdef FEAT_XCLIPBOARD |
| 6668 | if (ret > 0 && xterm_Shell != (Widget)0 |
| 6669 | && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds)) |
| 6670 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6671 | xterm_update(); // Maybe we should hand out clipboard |
| 6672 | // continue looping when we only got the X event and the input |
| 6673 | // buffer is empty |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6674 | if (--ret == 0 && !input_available()) |
| 6675 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6676 | // Try again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6677 | finished = FALSE; |
| 6678 | } |
| 6679 | } |
| 6680 | # endif |
| 6681 | # ifdef FEAT_MOUSE_GPM |
Bram Moolenaar | 33fc4a6 | 2022-02-23 18:07:38 +0000 | [diff] [blame] | 6682 | if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6683 | { |
| 6684 | if (FD_ISSET(gpm_fd, &efds)) |
| 6685 | gpm_close(); |
| 6686 | else if (FD_ISSET(gpm_fd, &rfds)) |
| 6687 | *check_for_gpm = 1; |
| 6688 | } |
| 6689 | # endif |
| 6690 | # ifdef USE_XSMP |
| 6691 | if (ret > 0 && xsmp_icefd != -1) |
| 6692 | { |
| 6693 | if (FD_ISSET(xsmp_icefd, &efds)) |
| 6694 | { |
| 6695 | if (p_verbose > 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 6696 | verb_msg(_("XSMP lost ICE connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6697 | xsmp_close(); |
| 6698 | if (--ret == 0) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6699 | finished = FALSE; // keep going if event was only one |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6700 | } |
| 6701 | else if (FD_ISSET(xsmp_icefd, &rfds)) |
| 6702 | { |
| 6703 | busy = TRUE; |
| 6704 | xsmp_handle_requests(); |
| 6705 | busy = FALSE; |
| 6706 | if (--ret == 0) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6707 | finished = FALSE; // keep going if event was only one |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6708 | } |
| 6709 | } |
| 6710 | # endif |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 6711 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6712 | // also call when ret == 0, we may be polling a keep-open channel |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 6713 | if (ret >= 0) |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 6714 | (void)channel_select_check(ret, &rfds, &wfds); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 6715 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6716 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6717 | #endif // HAVE_SELECT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6718 | |
| 6719 | #ifdef MAY_LOOP |
| 6720 | if (finished || msec == 0) |
| 6721 | break; |
| 6722 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 6723 | # ifdef FEAT_CLIENTSERVER |
| 6724 | if (server_waiting()) |
| 6725 | break; |
| 6726 | # endif |
| 6727 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6728 | // We're going to loop around again, find out for how long |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6729 | if (msec > 0) |
| 6730 | { |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 6731 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6732 | // Compute remaining wait time. |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 6733 | msec = start_msec - ELAPSED_FUNC(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6734 | # else |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6735 | // Guess we got interrupted halfway. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6736 | msec = msec / 2; |
| 6737 | # endif |
| 6738 | if (msec <= 0) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6739 | break; // waited long enough |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6740 | } |
| 6741 | #endif |
| 6742 | } |
| 6743 | |
Bram Moolenaar | ec70bdd | 2016-02-18 22:17:42 +0100 | [diff] [blame] | 6744 | return result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6745 | } |
| 6746 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6747 | /* |
Bram Moolenaar | 0274363 | 2005-07-25 20:42:36 +0000 | [diff] [blame] | 6748 | * Expand a path into all matching files and/or directories. Handles "*", |
| 6749 | * "?", "[a-z]", "**", etc. |
| 6750 | * "path" has backslashes before chars that are not to be expanded. |
| 6751 | * Returns the number of matches found. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6752 | */ |
| 6753 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6754 | mch_expandpath( |
| 6755 | garray_T *gap, |
| 6756 | char_u *path, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6757 | int flags) // EW_* flags |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6758 | { |
Bram Moolenaar | 0274363 | 2005-07-25 20:42:36 +0000 | [diff] [blame] | 6759 | return unix_expandpath(gap, path, 0, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6760 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6761 | |
| 6762 | /* |
| 6763 | * mch_expand_wildcards() - this code does wild-card pattern matching using |
| 6764 | * the shell |
| 6765 | * |
| 6766 | * return OK for success, FAIL for error (you may lose some memory) and put |
| 6767 | * an error message in *file. |
| 6768 | * |
| 6769 | * num_pat is number of input patterns |
| 6770 | * pat is array of pointers to input patterns |
| 6771 | * num_file is pointer to number of matched file names |
| 6772 | * file is pointer to array of pointers to matched file names |
| 6773 | */ |
| 6774 | |
| 6775 | #ifndef SEEK_SET |
| 6776 | # define SEEK_SET 0 |
| 6777 | #endif |
| 6778 | #ifndef SEEK_END |
| 6779 | # define SEEK_END 2 |
| 6780 | #endif |
| 6781 | |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 6782 | #define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|" |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6783 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6784 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6785 | mch_expand_wildcards( |
| 6786 | int num_pat, |
| 6787 | char_u **pat, |
| 6788 | int *num_file, |
| 6789 | char_u ***file, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6790 | int flags) // EW_* flags |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6791 | { |
| 6792 | int i; |
| 6793 | size_t len; |
Bram Moolenaar | 85325f8 | 2017-03-30 21:18:45 +0200 | [diff] [blame] | 6794 | long llen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6795 | char_u *p; |
| 6796 | int dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6797 | |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6798 | /* |
| 6799 | * This is the non-OS/2 implementation (really Unix). |
| 6800 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6801 | int j; |
| 6802 | char_u *tempname; |
| 6803 | char_u *command; |
| 6804 | FILE *fd; |
| 6805 | char_u *buffer; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6806 | #define STYLE_ECHO 0 // use "echo", the default |
| 6807 | #define STYLE_GLOB 1 // use "glob", for csh |
| 6808 | #define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh |
| 6809 | #define STYLE_PRINT 3 // use "print -N", for zsh |
Christian Brabandt | 9eb1ce5 | 2023-09-27 19:08:25 +0200 | [diff] [blame] | 6810 | #define STYLE_BT 4 // `cmd` expansion, execute the pattern directly |
| 6811 | #define STYLE_GLOBSTAR 5 // use extended shell glob for bash (this uses extended |
| 6812 | // globbing functionality using globstar, needs bash > 4) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6813 | int shell_style = STYLE_ECHO; |
| 6814 | int check_spaces; |
| 6815 | static int did_find_nul = FALSE; |
Bram Moolenaar | bdace83 | 2019-03-02 10:13:42 +0100 | [diff] [blame] | 6816 | int ampersand = FALSE; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6817 | // vimglob() function to define for Posix shell |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6818 | static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >"; |
Christian Brabandt | 9eb1ce5 | 2023-09-27 19:08:25 +0200 | [diff] [blame] | 6819 | // vimglob() function with globstar setting enabled, only for bash >= 4.X |
| 6820 | static char *sh_globstar_opt = "[[ ${BASH_VERSINFO[0]} -ge 4 ]] && shopt -s globstar; "; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6821 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6822 | *num_file = 0; // default: no files found |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6823 | *file = NULL; |
| 6824 | |
| 6825 | /* |
| 6826 | * If there are no wildcards, just copy the names to allocated memory. |
| 6827 | * Saves a lot of time, because we don't have to start a new shell. |
| 6828 | */ |
| 6829 | if (!have_wildcard(num_pat, pat)) |
| 6830 | return save_patterns(num_pat, pat, num_file, file); |
| 6831 | |
Bram Moolenaar | 0e634da | 2005-07-20 21:57:28 +0000 | [diff] [blame] | 6832 | # ifdef HAVE_SANDBOX |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6833 | // Don't allow any shell command in the sandbox. |
Bram Moolenaar | 0e634da | 2005-07-20 21:57:28 +0000 | [diff] [blame] | 6834 | if (sandbox != 0 && check_secure()) |
| 6835 | return FAIL; |
| 6836 | # endif |
| 6837 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6838 | /* |
| 6839 | * Don't allow the use of backticks in secure and restricted mode. |
| 6840 | */ |
Bram Moolenaar | 0e634da | 2005-07-20 21:57:28 +0000 | [diff] [blame] | 6841 | if (secure || restricted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6842 | for (i = 0; i < num_pat; ++i) |
| 6843 | if (vim_strchr(pat[i], '`') != NULL |
| 6844 | && (check_restricted() || check_secure())) |
| 6845 | return FAIL; |
| 6846 | |
| 6847 | /* |
| 6848 | * get a name for the temp file |
| 6849 | */ |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 6850 | if ((tempname = vim_tempname('o', FALSE)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6851 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 6852 | emsg(_(e_cant_get_temp_file_name)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6853 | return FAIL; |
| 6854 | } |
| 6855 | |
| 6856 | /* |
| 6857 | * 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] | 6858 | * file. |
| 6859 | * STYLE_BT: NL separated |
| 6860 | * If expanding `cmd` execute it directly. |
| 6861 | * STYLE_GLOB: NUL separated |
| 6862 | * If we use *csh, "glob" will work better than "echo". |
| 6863 | * STYLE_PRINT: NL or NUL separated |
| 6864 | * If we use *zsh, "print -N" will work better than "glob". |
| 6865 | * STYLE_VIMGLOB: NL separated |
| 6866 | * If we use *sh*, we define "vimglob()". |
Christian Brabandt | 9eb1ce5 | 2023-09-27 19:08:25 +0200 | [diff] [blame] | 6867 | * STYLE_GLOBSTAR: NL separated |
| 6868 | * If we use *bash*, we define "vimglob() and enable globstar option". |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6869 | * STYLE_ECHO: space separated. |
| 6870 | * A shell we don't know, stay safe and use "echo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6871 | */ |
| 6872 | if (num_pat == 1 && *pat[0] == '`' |
| 6873 | && (len = STRLEN(pat[0])) > 2 |
| 6874 | && *(pat[0] + len - 1) == '`') |
| 6875 | shell_style = STYLE_BT; |
| 6876 | else if ((len = STRLEN(p_sh)) >= 3) |
| 6877 | { |
| 6878 | if (STRCMP(p_sh + len - 3, "csh") == 0) |
| 6879 | shell_style = STYLE_GLOB; |
| 6880 | else if (STRCMP(p_sh + len - 3, "zsh") == 0) |
| 6881 | shell_style = STYLE_PRINT; |
| 6882 | } |
Christian Brabandt | 9eb1ce5 | 2023-09-27 19:08:25 +0200 | [diff] [blame] | 6883 | if (shell_style == STYLE_ECHO) |
| 6884 | { |
| 6885 | if (strstr((char *)gettail(p_sh), "bash") != NULL) |
| 6886 | shell_style = STYLE_GLOBSTAR; |
| 6887 | else if (strstr((char *)gettail(p_sh), "sh") != NULL) |
| 6888 | shell_style = STYLE_VIMGLOB; |
| 6889 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6890 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6891 | // Compute the length of the command. We need 2 extra bytes: for the |
| 6892 | // optional '&' and for the NUL. |
| 6893 | // Worst case: "unset nonomatch; print -N >" plus two is 29 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6894 | len = STRLEN(tempname) + 29; |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6895 | if (shell_style == STYLE_VIMGLOB) |
| 6896 | len += STRLEN(sh_vimglob_func); |
Christian Brabandt | 9eb1ce5 | 2023-09-27 19:08:25 +0200 | [diff] [blame] | 6897 | else if (shell_style == STYLE_GLOBSTAR) |
| 6898 | len += STRLEN(sh_vimglob_func) |
| 6899 | + STRLEN(sh_globstar_opt); |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6900 | |
Bram Moolenaar | b23c338 | 2005-01-31 19:09:12 +0000 | [diff] [blame] | 6901 | for (i = 0; i < num_pat; ++i) |
| 6902 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6903 | // Count the length of the patterns in the same way as they are put in |
| 6904 | // "command" below. |
Bram Moolenaar | b23c338 | 2005-01-31 19:09:12 +0000 | [diff] [blame] | 6905 | #ifdef USE_SYSTEM |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6906 | len += STRLEN(pat[i]) + 3; // add space and two quotes |
Bram Moolenaar | b23c338 | 2005-01-31 19:09:12 +0000 | [diff] [blame] | 6907 | #else |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6908 | ++len; // add space |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6909 | for (j = 0; pat[i][j] != NUL; ++j) |
| 6910 | { |
| 6911 | if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6912 | ++len; // may add a backslash |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6913 | ++len; |
| 6914 | } |
Bram Moolenaar | b23c338 | 2005-01-31 19:09:12 +0000 | [diff] [blame] | 6915 | #endif |
| 6916 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6917 | command = alloc(len); |
| 6918 | if (command == NULL) |
| 6919 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6920 | // out of memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6921 | vim_free(tempname); |
| 6922 | return FAIL; |
| 6923 | } |
| 6924 | |
| 6925 | /* |
| 6926 | * Build the shell command: |
| 6927 | * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell |
| 6928 | * recognizes this). |
| 6929 | * - Add the shell command to print the expanded names. |
| 6930 | * - Add the temp file name. |
| 6931 | * - Add the file name patterns. |
| 6932 | */ |
| 6933 | if (shell_style == STYLE_BT) |
| 6934 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6935 | // change `command; command& ` to (command; command ) |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6936 | STRCPY(command, "("); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6937 | STRCAT(command, pat[0] + 1); // exclude first backtick |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6938 | p = command + STRLEN(command) - 1; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6939 | *p-- = ')'; // remove last backtick |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6940 | while (p > command && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6941 | --p; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6942 | if (*p == '&') // remove trailing '&' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6943 | { |
Bram Moolenaar | bdace83 | 2019-03-02 10:13:42 +0100 | [diff] [blame] | 6944 | ampersand = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6945 | *p = ' '; |
| 6946 | } |
| 6947 | STRCAT(command, ">"); |
| 6948 | } |
| 6949 | else |
| 6950 | { |
Christian Brabandt | 8b8d829 | 2021-11-19 12:37:36 +0000 | [diff] [blame] | 6951 | STRCPY(command, ""); |
| 6952 | if (shell_style == STYLE_GLOB) |
| 6953 | { |
| 6954 | // Assume the nonomatch option is valid only for csh like shells, |
| 6955 | // otherwise, this may set the positional parameters for the shell, |
| 6956 | // e.g. "$*". |
| 6957 | if (flags & EW_NOTFOUND) |
| 6958 | STRCAT(command, "set nonomatch; "); |
| 6959 | else |
| 6960 | STRCAT(command, "unset nonomatch; "); |
| 6961 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6962 | if (shell_style == STYLE_GLOB) |
| 6963 | STRCAT(command, "glob >"); |
| 6964 | else if (shell_style == STYLE_PRINT) |
| 6965 | STRCAT(command, "print -N >"); |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6966 | else if (shell_style == STYLE_VIMGLOB) |
| 6967 | STRCAT(command, sh_vimglob_func); |
Christian Brabandt | 9eb1ce5 | 2023-09-27 19:08:25 +0200 | [diff] [blame] | 6968 | else if (shell_style == STYLE_GLOBSTAR) |
| 6969 | { |
| 6970 | STRCAT(command, sh_globstar_opt); |
| 6971 | STRCAT(command, sh_vimglob_func); |
| 6972 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6973 | else |
| 6974 | STRCAT(command, "echo >"); |
| 6975 | } |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6976 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6977 | STRCAT(command, tempname); |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 6978 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6979 | if (shell_style != STYLE_BT) |
| 6980 | for (i = 0; i < num_pat; ++i) |
| 6981 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 6982 | // When using system() always add extra quotes, because the shell |
| 6983 | // is started twice. Otherwise put a backslash before special |
| 6984 | // characters, except inside ``. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6985 | #ifdef USE_SYSTEM |
| 6986 | STRCAT(command, " \""); |
| 6987 | STRCAT(command, pat[i]); |
| 6988 | STRCAT(command, "\""); |
| 6989 | #else |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6990 | int intick = FALSE; |
| 6991 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6992 | p = command + STRLEN(command); |
| 6993 | *p++ = ' '; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6994 | for (j = 0; pat[i][j] != NUL; ++j) |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6995 | { |
| 6996 | if (pat[i][j] == '`') |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6997 | intick = !intick; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6998 | else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL) |
| 6999 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7000 | // Remove a backslash, take char literally. But keep |
| 7001 | // backslash inside backticks, before a special character |
| 7002 | // and before a backtick. |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 7003 | if (intick |
Bram Moolenaar | 49315f6 | 2006-02-04 00:54:59 +0000 | [diff] [blame] | 7004 | || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL |
| 7005 | || pat[i][j + 1] == '`') |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 7006 | *p++ = '\\'; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 7007 | ++j; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7008 | } |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 7009 | else if (!intick |
| 7010 | && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$') |
| 7011 | && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7012 | // Put a backslash before a special character, but not |
| 7013 | // when inside ``. And not for $var when EW_KEEPDOLLAR is |
| 7014 | // set. |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7015 | *p++ = '\\'; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 7016 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7017 | // Copy one character. |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 7018 | *p++ = pat[i][j]; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 7019 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7020 | *p = NUL; |
| 7021 | #endif |
| 7022 | } |
| 7023 | if (flags & EW_SILENT) |
| 7024 | show_shell_mess = FALSE; |
Bram Moolenaar | bdace83 | 2019-03-02 10:13:42 +0100 | [diff] [blame] | 7025 | if (ampersand) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7026 | STRCAT(command, "&"); // put the '&' after the redirection |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7027 | |
| 7028 | /* |
| 7029 | * Using zsh -G: If a pattern has no matches, it is just deleted from |
| 7030 | * the argument list, otherwise zsh gives an error message and doesn't |
| 7031 | * expand any other pattern. |
| 7032 | */ |
| 7033 | if (shell_style == STYLE_PRINT) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7034 | extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7035 | |
| 7036 | /* |
| 7037 | * If we use -f then shell variables set in .cshrc won't get expanded. |
| 7038 | * vi can do it, so we will too, but it is only necessary if there is a "$" |
| 7039 | * in one of the patterns, otherwise we can still use the fast option. |
| 7040 | */ |
| 7041 | else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat)) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7042 | extra_shell_arg = (char_u *)"-f"; // Use csh fast option |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7043 | |
| 7044 | /* |
| 7045 | * execute the shell command |
| 7046 | */ |
| 7047 | i = call_shell(command, SHELL_EXPAND | SHELL_SILENT); |
| 7048 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7049 | // When running in the background, give it some time to create the temp |
| 7050 | // file, but don't wait for it to finish. |
Bram Moolenaar | bdace83 | 2019-03-02 10:13:42 +0100 | [diff] [blame] | 7051 | if (ampersand) |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 7052 | mch_delay(10L, MCH_DELAY_IGNOREINPUT); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7053 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7054 | extra_shell_arg = NULL; // cleanup |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7055 | show_shell_mess = TRUE; |
| 7056 | vim_free(command); |
| 7057 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7058 | if (i != 0) // mch_call_shell() failed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7059 | { |
| 7060 | mch_remove(tempname); |
| 7061 | vim_free(tempname); |
| 7062 | /* |
| 7063 | * With interactive completion, the error message is not printed. |
| 7064 | * However with USE_SYSTEM, I don't know how to turn off error messages |
| 7065 | * from the shell, so screen may still get messed up -- webb. |
| 7066 | */ |
| 7067 | #ifndef USE_SYSTEM |
| 7068 | if (!(flags & EW_SILENT)) |
| 7069 | #endif |
| 7070 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7071 | redraw_later_clear(); // probably messed up screen |
| 7072 | msg_putchar('\n'); // clear bottom line quickly |
| 7073 | cmdline_row = Rows - 1; // continue on last line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7074 | #ifdef USE_SYSTEM |
| 7075 | if (!(flags & EW_SILENT)) |
| 7076 | #endif |
| 7077 | { |
Bram Moolenaar | 40bcec1 | 2021-12-05 22:19:27 +0000 | [diff] [blame] | 7078 | msg(_(e_cannot_expand_wildcards)); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7079 | msg_start(); // don't overwrite this message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7080 | } |
| 7081 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7082 | // If a `cmd` expansion failed, don't list `cmd` as a match, even when |
| 7083 | // EW_NOTFOUND is given |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7084 | if (shell_style == STYLE_BT) |
| 7085 | return FAIL; |
| 7086 | goto notfound; |
| 7087 | } |
| 7088 | |
| 7089 | /* |
| 7090 | * read the names from the file into memory |
| 7091 | */ |
| 7092 | fd = fopen((char *)tempname, READBIN); |
| 7093 | if (fd == NULL) |
| 7094 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7095 | // Something went wrong, perhaps a file name with a special char. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7096 | if (!(flags & EW_SILENT)) |
| 7097 | { |
Bram Moolenaar | 40bcec1 | 2021-12-05 22:19:27 +0000 | [diff] [blame] | 7098 | msg(_(e_cannot_expand_wildcards)); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7099 | msg_start(); // don't overwrite this message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7100 | } |
| 7101 | vim_free(tempname); |
| 7102 | goto notfound; |
| 7103 | } |
| 7104 | fseek(fd, 0L, SEEK_END); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7105 | llen = ftell(fd); // get size of temp file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7106 | fseek(fd, 0L, SEEK_SET); |
Bram Moolenaar | 85325f8 | 2017-03-30 21:18:45 +0200 | [diff] [blame] | 7107 | if (llen < 0) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7108 | // just in case ftell() would fail |
Bram Moolenaar | 85325f8 | 2017-03-30 21:18:45 +0200 | [diff] [blame] | 7109 | buffer = NULL; |
| 7110 | else |
| 7111 | buffer = alloc(llen + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7112 | if (buffer == NULL) |
| 7113 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7114 | // out of memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7115 | mch_remove(tempname); |
| 7116 | vim_free(tempname); |
| 7117 | fclose(fd); |
| 7118 | return FAIL; |
| 7119 | } |
Bram Moolenaar | 85325f8 | 2017-03-30 21:18:45 +0200 | [diff] [blame] | 7120 | len = llen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7121 | i = fread((char *)buffer, 1, len, fd); |
| 7122 | fclose(fd); |
| 7123 | mch_remove(tempname); |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 7124 | if (i != (int)len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7125 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7126 | // unexpected read error |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 7127 | semsg(_(e_cant_read_file_str), tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7128 | vim_free(tempname); |
| 7129 | vim_free(buffer); |
| 7130 | return FAIL; |
| 7131 | } |
| 7132 | vim_free(tempname); |
| 7133 | |
Bram Moolenaar | 1eed532 | 2019-02-26 17:03:54 +0100 | [diff] [blame] | 7134 | # ifdef __CYGWIN__ |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7135 | // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7136 | p = buffer; |
Bram Moolenaar | fe17e76 | 2013-06-29 14:17:02 +0200 | [diff] [blame] | 7137 | for (i = 0; i < (int)len; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7138 | if (!(buffer[i] == CAR && buffer[i + 1] == NL)) |
| 7139 | *p++ = buffer[i]; |
| 7140 | len = p - buffer; |
| 7141 | # endif |
| 7142 | |
| 7143 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7144 | // file names are separated with Space |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7145 | if (shell_style == STYLE_ECHO) |
| 7146 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7147 | buffer[len] = '\n'; // make sure the buffer ends in NL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7148 | p = buffer; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7149 | for (i = 0; *p != '\n'; ++i) // count number of entries |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7150 | { |
| 7151 | while (*p != ' ' && *p != '\n') |
| 7152 | ++p; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7153 | p = skipwhite(p); // skip to next entry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7154 | } |
| 7155 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7156 | // file names are separated with NL |
Christian Brabandt | 9eb1ce5 | 2023-09-27 19:08:25 +0200 | [diff] [blame] | 7157 | else if (shell_style == STYLE_BT || |
| 7158 | shell_style == STYLE_VIMGLOB || |
| 7159 | shell_style == STYLE_GLOBSTAR) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7160 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7161 | buffer[len] = NUL; // make sure the buffer ends in NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7162 | p = buffer; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7163 | for (i = 0; *p != NUL; ++i) // count number of entries |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7164 | { |
| 7165 | while (*p != '\n' && *p != NUL) |
| 7166 | ++p; |
| 7167 | if (*p != NUL) |
| 7168 | ++p; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7169 | p = skipwhite(p); // skip leading white space |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7170 | } |
| 7171 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7172 | // file names are separated with NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7173 | else |
| 7174 | { |
| 7175 | /* |
| 7176 | * Some versions of zsh use spaces instead of NULs to separate |
| 7177 | * results. Only do this when there is no NUL before the end of the |
| 7178 | * buffer, otherwise we would never be able to use file names with |
| 7179 | * embedded spaces when zsh does use NULs. |
| 7180 | * When we found a NUL once, we know zsh is OK, set did_find_nul and |
| 7181 | * don't check for spaces again. |
| 7182 | */ |
| 7183 | check_spaces = FALSE; |
| 7184 | if (shell_style == STYLE_PRINT && !did_find_nul) |
| 7185 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7186 | // 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] | 7187 | buffer[len] = NUL; |
Bram Moolenaar | b011af9 | 2013-12-11 13:21:51 +0100 | [diff] [blame] | 7188 | if (len && (int)STRLEN(buffer) < (int)len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7189 | did_find_nul = TRUE; |
| 7190 | else |
| 7191 | check_spaces = TRUE; |
| 7192 | } |
| 7193 | |
| 7194 | /* |
| 7195 | * Make sure the buffer ends with a NUL. For STYLE_PRINT there |
| 7196 | * already is one, for STYLE_GLOB it needs to be added. |
| 7197 | */ |
| 7198 | if (len && buffer[len - 1] == NUL) |
| 7199 | --len; |
| 7200 | else |
| 7201 | buffer[len] = NUL; |
| 7202 | i = 0; |
| 7203 | for (p = buffer; p < buffer + len; ++p) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7204 | if (*p == NUL || (*p == ' ' && check_spaces)) // count entry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7205 | { |
| 7206 | ++i; |
| 7207 | *p = NUL; |
| 7208 | } |
| 7209 | if (len) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7210 | ++i; // count last entry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7211 | } |
| 7212 | if (i == 0) |
| 7213 | { |
| 7214 | /* |
| 7215 | * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I". |
| 7216 | * /bin/sh will happily expand it to nothing rather than returning an |
| 7217 | * error; and hey, it's good to check anyway -- webb. |
| 7218 | */ |
| 7219 | vim_free(buffer); |
| 7220 | goto notfound; |
| 7221 | } |
| 7222 | *num_file = i; |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 7223 | *file = ALLOC_MULT(char_u *, i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7224 | if (*file == NULL) |
| 7225 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7226 | // out of memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7227 | vim_free(buffer); |
| 7228 | return FAIL; |
| 7229 | } |
| 7230 | |
| 7231 | /* |
| 7232 | * Isolate the individual file names. |
| 7233 | */ |
| 7234 | p = buffer; |
| 7235 | for (i = 0; i < *num_file; ++i) |
| 7236 | { |
| 7237 | (*file)[i] = p; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7238 | // Space or NL separates |
Bram Moolenaar | c724791 | 2008-01-13 12:54:11 +0000 | [diff] [blame] | 7239 | if (shell_style == STYLE_ECHO || shell_style == STYLE_BT |
Christian Brabandt | 9eb1ce5 | 2023-09-27 19:08:25 +0200 | [diff] [blame] | 7240 | || shell_style == STYLE_VIMGLOB || shell_style == STYLE_GLOBSTAR) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7241 | { |
Bram Moolenaar | 49315f6 | 2006-02-04 00:54:59 +0000 | [diff] [blame] | 7242 | while (!(shell_style == STYLE_ECHO && *p == ' ') |
| 7243 | && *p != '\n' && *p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7244 | ++p; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7245 | if (p == buffer + len) // last entry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7246 | *p = NUL; |
| 7247 | else |
| 7248 | { |
| 7249 | *p++ = NUL; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7250 | p = skipwhite(p); // skip to next entry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7251 | } |
| 7252 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7253 | else // NUL separates |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7254 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7255 | while (*p && p < buffer + len) // skip entry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7256 | ++p; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7257 | ++p; // skip NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7258 | } |
| 7259 | } |
| 7260 | |
| 7261 | /* |
| 7262 | * Move the file names to allocated memory. |
| 7263 | */ |
| 7264 | for (j = 0, i = 0; i < *num_file; ++i) |
| 7265 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7266 | // Require the files to exist. Helps when using /bin/sh |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7267 | if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0) |
| 7268 | continue; |
| 7269 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7270 | // check if this entry should be included |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7271 | dir = (mch_isdir((*file)[i])); |
| 7272 | if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE))) |
| 7273 | continue; |
| 7274 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7275 | // Skip files that are not executable if we check for that. |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 7276 | if (!dir && (flags & EW_EXEC) |
| 7277 | && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD))) |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 7278 | continue; |
| 7279 | |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 7280 | p = alloc(STRLEN((*file)[i]) + 1 + dir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7281 | if (p) |
| 7282 | { |
| 7283 | STRCPY(p, (*file)[i]); |
| 7284 | if (dir) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7285 | add_pathsep(p); // add '/' to a directory name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7286 | (*file)[j++] = p; |
| 7287 | } |
| 7288 | } |
| 7289 | vim_free(buffer); |
| 7290 | *num_file = j; |
| 7291 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7292 | if (*num_file == 0) // rejected all entries |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7293 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 7294 | VIM_CLEAR(*file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7295 | goto notfound; |
| 7296 | } |
| 7297 | |
| 7298 | return OK; |
| 7299 | |
| 7300 | notfound: |
| 7301 | if (flags & EW_NOTFOUND) |
| 7302 | return save_patterns(num_pat, pat, num_file, file); |
| 7303 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7304 | } |
| 7305 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7306 | #endif // VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7307 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7308 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7309 | save_patterns( |
| 7310 | int num_pat, |
| 7311 | char_u **pat, |
| 7312 | int *num_file, |
| 7313 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7314 | { |
| 7315 | int i; |
Bram Moolenaar | d8b0273 | 2005-01-14 21:48:43 +0000 | [diff] [blame] | 7316 | char_u *s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7317 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 7318 | *file = ALLOC_MULT(char_u *, num_pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7319 | if (*file == NULL) |
| 7320 | return FAIL; |
| 7321 | for (i = 0; i < num_pat; i++) |
Bram Moolenaar | d8b0273 | 2005-01-14 21:48:43 +0000 | [diff] [blame] | 7322 | { |
| 7323 | s = vim_strsave(pat[i]); |
| 7324 | if (s != NULL) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7325 | // Be compatible with expand_filename(): halve the number of |
| 7326 | // backslashes. |
Bram Moolenaar | d8b0273 | 2005-01-14 21:48:43 +0000 | [diff] [blame] | 7327 | backslash_halve(s); |
| 7328 | (*file)[i] = s; |
| 7329 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7330 | *num_file = num_pat; |
| 7331 | return OK; |
| 7332 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7333 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7334 | /* |
| 7335 | * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can |
| 7336 | * expand. |
| 7337 | */ |
| 7338 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7339 | mch_has_exp_wildcard(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7340 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 7341 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7342 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7343 | if (*p == '\\' && p[1] != NUL) |
| 7344 | ++p; |
| 7345 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7346 | if (vim_strchr((char_u *) |
| 7347 | #ifdef VMS |
| 7348 | "*?%" |
| 7349 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7350 | "*?[{'" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7351 | #endif |
| 7352 | , *p) != NULL) |
| 7353 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7354 | } |
| 7355 | return FALSE; |
| 7356 | } |
| 7357 | |
| 7358 | /* |
| 7359 | * Return TRUE if the string "p" contains a wildcard. |
| 7360 | * Don't recognize '~' at the end as a wildcard. |
| 7361 | */ |
| 7362 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7363 | mch_has_wildcard(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7364 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 7365 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7366 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7367 | if (*p == '\\' && p[1] != NUL) |
| 7368 | ++p; |
| 7369 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7370 | if (vim_strchr((char_u *) |
| 7371 | #ifdef VMS |
| 7372 | "*?%$" |
| 7373 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7374 | "*?[{`'$" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7375 | #endif |
| 7376 | , *p) != NULL |
| 7377 | || (*p == '~' && p[1] != NUL)) |
| 7378 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7379 | } |
| 7380 | return FALSE; |
| 7381 | } |
| 7382 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7383 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7384 | have_wildcard(int num, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7385 | { |
| 7386 | int i; |
| 7387 | |
| 7388 | for (i = 0; i < num; i++) |
| 7389 | if (mch_has_wildcard(file[i])) |
| 7390 | return 1; |
| 7391 | return 0; |
| 7392 | } |
| 7393 | |
| 7394 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7395 | have_dollars(int num, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7396 | { |
| 7397 | int i; |
| 7398 | |
| 7399 | for (i = 0; i < num; i++) |
| 7400 | if (vim_strchr(file[i], '$') != NULL) |
| 7401 | return TRUE; |
| 7402 | return FALSE; |
| 7403 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7404 | |
Bram Moolenaar | fdcc9af | 2016-02-29 12:52:39 +0100 | [diff] [blame] | 7405 | #if !defined(HAVE_RENAME) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7406 | /* |
| 7407 | * Scaled-down version of rename(), which is missing in Xenix. |
| 7408 | * This version can only move regular files and will fail if the |
| 7409 | * destination exists. |
| 7410 | */ |
| 7411 | int |
Bram Moolenaar | fdcc9af | 2016-02-29 12:52:39 +0100 | [diff] [blame] | 7412 | mch_rename(const char *src, const char *dest) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7413 | { |
| 7414 | struct stat st; |
| 7415 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7416 | if (stat(dest, &st) >= 0) // fail if destination exists |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7417 | return -1; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7418 | if (link(src, dest) != 0) // link file to new name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7419 | return -1; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7420 | if (mch_remove(src) == 0) // delete link to old name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7421 | return 0; |
| 7422 | return -1; |
| 7423 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7424 | #endif // !HAVE_RENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7425 | |
Bram Moolenaar | 4b8366b | 2019-05-04 17:34:34 +0200 | [diff] [blame] | 7426 | #if defined(FEAT_MOUSE_GPM) || defined(PROTO) |
Bram Moolenaar | 33fc4a6 | 2022-02-23 18:07:38 +0000 | [diff] [blame] | 7427 | # if defined(DYNAMIC_GPM) || defined(PROTO) |
| 7428 | /* |
| 7429 | * Initialize Gpm's symbols for dynamic linking. |
| 7430 | * Must be called only if libgpm_hinst is NULL. |
| 7431 | */ |
| 7432 | static int |
| 7433 | load_libgpm(void) |
| 7434 | { |
| 7435 | libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL); |
| 7436 | |
| 7437 | if (libgpm_hinst == NULL) |
| 7438 | { |
| 7439 | if (p_verbose > 0) |
| 7440 | smsg_attr(HL_ATTR(HLF_W), |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 7441 | _("Could not load gpm library: %s"), dlerror()); |
Bram Moolenaar | 33fc4a6 | 2022-02-23 18:07:38 +0000 | [diff] [blame] | 7442 | return FAIL; |
| 7443 | } |
| 7444 | |
| 7445 | if ( |
| 7446 | (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL |
| 7447 | || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL |
| 7448 | || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL |
| 7449 | || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL |
| 7450 | || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL |
| 7451 | ) |
| 7452 | { |
| 7453 | semsg(_(e_could_not_load_library_str_str), "gpm", dlerror()); |
| 7454 | dlclose(libgpm_hinst); |
| 7455 | libgpm_hinst = NULL; |
| 7456 | dll_gpm_flag = NULL; |
| 7457 | dll_gpm_fd = NULL; |
| 7458 | return FAIL; |
| 7459 | } |
| 7460 | return OK; |
| 7461 | } |
| 7462 | |
| 7463 | int |
| 7464 | gpm_available(void) |
| 7465 | { |
| 7466 | return libgpm_hinst != NULL || load_libgpm() == OK; |
| 7467 | } |
| 7468 | # endif // DYNAMIC_GPM |
| 7469 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7470 | /* |
| 7471 | * Initializes connection with gpm (if it isn't already opened) |
| 7472 | * Return 1 if succeeded (or connection already opened), 0 if failed |
| 7473 | */ |
| 7474 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7475 | gpm_open(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7476 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7477 | static Gpm_Connect gpm_connect; // Must it be kept till closing ? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7478 | |
Bram Moolenaar | 33fc4a6 | 2022-02-23 18:07:38 +0000 | [diff] [blame] | 7479 | #ifdef DYNAMIC_GPM |
| 7480 | if (!gpm_available()) |
| 7481 | return 0; |
| 7482 | #endif |
| 7483 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 7484 | if (gpm_flag) |
| 7485 | return 1; // already open |
| 7486 | |
| 7487 | gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN); |
| 7488 | gpm_connect.defaultMask = ~GPM_HARD; |
| 7489 | // Default handling for mouse move |
| 7490 | gpm_connect.minMod = 0; // Handle any modifier keys |
| 7491 | gpm_connect.maxMod = 0xffff; |
| 7492 | if (Gpm_Open(&gpm_connect, 0) > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7493 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 7494 | // gpm library tries to handling TSTP causes |
| 7495 | // problems. Anyways, we close connection to Gpm whenever |
| 7496 | // we are going to suspend or starting an external process |
| 7497 | // so we shouldn't have problem with this |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 7498 | # ifdef SIGTSTP |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 7499 | mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp); |
Bram Moolenaar | 76243bd | 2009-03-02 01:47:02 +0000 | [diff] [blame] | 7500 | # endif |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 7501 | return 1; // succeed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7502 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 7503 | if (gpm_fd == -2) |
Julio B | cc59d62 | 2024-04-04 21:55:10 +0200 | [diff] [blame] | 7504 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 7505 | Gpm_Close(); // We don't want to talk to xterm via gpm |
Julio B | cc59d62 | 2024-04-04 21:55:10 +0200 | [diff] [blame] | 7506 | |
zeertzjq | d9be94c | 2024-07-14 10:20:20 +0200 | [diff] [blame] | 7507 | // Gpm_Close fails to properly restore the WINCH and TSTP handlers, |
| 7508 | // leading to Vim ignoring resize signals. We have to re-initialize |
| 7509 | // these handlers again here. |
Julio B | cc59d62 | 2024-04-04 21:55:10 +0200 | [diff] [blame] | 7510 | # ifdef SIGWINCH |
| 7511 | mch_signal(SIGWINCH, sig_winch); |
| 7512 | # endif |
| 7513 | # ifdef SIGTSTP |
| 7514 | mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp); |
| 7515 | # endif |
| 7516 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 7517 | return 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7518 | } |
| 7519 | |
| 7520 | /* |
Bram Moolenaar | 4b8366b | 2019-05-04 17:34:34 +0200 | [diff] [blame] | 7521 | * Returns TRUE if the GPM mouse is enabled. |
| 7522 | */ |
| 7523 | int |
| 7524 | gpm_enabled(void) |
| 7525 | { |
| 7526 | return gpm_flag && gpm_fd >= 0; |
| 7527 | } |
| 7528 | |
| 7529 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7530 | * Closes connection to gpm |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7531 | */ |
| 7532 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7533 | gpm_close(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7534 | { |
Bram Moolenaar | 4b8366b | 2019-05-04 17:34:34 +0200 | [diff] [blame] | 7535 | if (gpm_enabled()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7536 | Gpm_Close(); |
| 7537 | } |
| 7538 | |
Bram Moolenaar | bedf091 | 2019-05-04 16:58:45 +0200 | [diff] [blame] | 7539 | /* |
| 7540 | * Reads gpm event and adds special keys to input buf. Returns length of |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7541 | * generated key sequence. |
Bram Moolenaar | c7f0255 | 2014-04-01 21:00:59 +0200 | [diff] [blame] | 7542 | * This function is styled after gui_send_mouse_event(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7543 | */ |
| 7544 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7545 | mch_gpm_process(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7546 | { |
| 7547 | int button; |
| 7548 | static Gpm_Event gpm_event; |
| 7549 | char_u string[6]; |
| 7550 | int_u vim_modifiers; |
| 7551 | int row,col; |
| 7552 | unsigned char buttons_mask; |
| 7553 | unsigned char gpm_modifiers; |
| 7554 | static unsigned char old_buttons = 0; |
| 7555 | |
| 7556 | Gpm_GetEvent(&gpm_event); |
| 7557 | |
| 7558 | #ifdef FEAT_GUI |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7559 | // Don't put events in the input queue now. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7560 | if (hold_gui_events) |
| 7561 | return 0; |
| 7562 | #endif |
| 7563 | |
| 7564 | row = gpm_event.y - 1; |
| 7565 | col = gpm_event.x - 1; |
| 7566 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7567 | string[0] = ESC; // Our termcode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7568 | string[1] = 'M'; |
| 7569 | string[2] = 'G'; |
| 7570 | switch (GPM_BARE_EVENTS(gpm_event.type)) |
| 7571 | { |
| 7572 | case GPM_DRAG: |
| 7573 | string[3] = MOUSE_DRAG; |
| 7574 | break; |
| 7575 | case GPM_DOWN: |
| 7576 | buttons_mask = gpm_event.buttons & ~old_buttons; |
| 7577 | old_buttons = gpm_event.buttons; |
| 7578 | switch (buttons_mask) |
| 7579 | { |
| 7580 | case GPM_B_LEFT: |
| 7581 | button = MOUSE_LEFT; |
| 7582 | break; |
| 7583 | case GPM_B_MIDDLE: |
| 7584 | button = MOUSE_MIDDLE; |
| 7585 | break; |
| 7586 | case GPM_B_RIGHT: |
| 7587 | button = MOUSE_RIGHT; |
| 7588 | break; |
| 7589 | default: |
| 7590 | return 0; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7591 | // Don't know what to do. Can more than one button be |
| 7592 | // reported in one event? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7593 | } |
| 7594 | string[3] = (char_u)(button | 0x20); |
| 7595 | SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1); |
| 7596 | break; |
| 7597 | case GPM_UP: |
| 7598 | string[3] = MOUSE_RELEASE; |
| 7599 | old_buttons &= ~gpm_event.buttons; |
| 7600 | break; |
| 7601 | default: |
| 7602 | return 0; |
| 7603 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7604 | // This code is based on gui_x11_mouse_cb in gui_x11.c |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7605 | gpm_modifiers = gpm_event.modifiers; |
| 7606 | vim_modifiers = 0x0; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7607 | // I ignore capslock stats. Aren't we all just hate capslock mixing with |
| 7608 | // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and |
| 7609 | // K_CAPSSHIFT is defined 8, so it probably isn't even reported |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7610 | if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL))) |
| 7611 | vim_modifiers |= MOUSE_SHIFT; |
| 7612 | |
| 7613 | if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL))) |
| 7614 | vim_modifiers |= MOUSE_CTRL; |
| 7615 | if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR))) |
| 7616 | vim_modifiers |= MOUSE_ALT; |
| 7617 | string[3] |= vim_modifiers; |
| 7618 | string[4] = (char_u)(col + ' ' + 1); |
| 7619 | string[5] = (char_u)(row + ' ' + 1); |
| 7620 | add_to_input_buf(string, 6); |
| 7621 | return 6; |
| 7622 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7623 | #endif // FEAT_MOUSE_GPM |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7624 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7625 | #ifdef FEAT_SYSMOUSE |
| 7626 | /* |
| 7627 | * Initialize connection with sysmouse. |
| 7628 | * Let virtual console inform us with SIGUSR2 for pending sysmouse |
| 7629 | * output, any sysmouse output than will be processed via sig_sysmouse(). |
| 7630 | * Return OK if succeeded, FAIL if failed. |
| 7631 | */ |
| 7632 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7633 | sysmouse_open(void) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7634 | { |
| 7635 | struct mouse_info mouse; |
| 7636 | |
| 7637 | mouse.operation = MOUSE_MODE; |
| 7638 | mouse.u.mode.mode = 0; |
| 7639 | mouse.u.mode.signal = SIGUSR2; |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 7640 | if (ioctl(1, CONS_MOUSECTL, &mouse) == -1) |
| 7641 | return FAIL; |
| 7642 | |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 7643 | mch_signal(SIGUSR2, sig_sysmouse); |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 7644 | mouse.operation = MOUSE_SHOW; |
| 7645 | ioctl(1, CONS_MOUSECTL, &mouse); |
| 7646 | return OK; |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7647 | } |
| 7648 | |
| 7649 | /* |
| 7650 | * Stop processing SIGUSR2 signals, and also make sure that |
| 7651 | * virtual console do not send us any sysmouse related signal. |
| 7652 | */ |
| 7653 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7654 | sysmouse_close(void) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7655 | { |
| 7656 | struct mouse_info mouse; |
| 7657 | |
ichizok | 378447f | 2023-05-11 22:25:42 +0100 | [diff] [blame] | 7658 | mch_signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL); |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7659 | mouse.operation = MOUSE_MODE; |
| 7660 | mouse.u.mode.mode = 0; |
| 7661 | mouse.u.mode.signal = 0; |
| 7662 | ioctl(1, CONS_MOUSECTL, &mouse); |
| 7663 | } |
| 7664 | |
| 7665 | /* |
| 7666 | * Gets info from sysmouse and adds special keys to input buf. |
| 7667 | */ |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 7668 | static void |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7669 | sig_sysmouse SIGDEFARG(sigarg) |
| 7670 | { |
| 7671 | struct mouse_info mouse; |
| 7672 | struct video_info video; |
| 7673 | char_u string[6]; |
| 7674 | int row, col; |
| 7675 | int button; |
| 7676 | int buttons; |
| 7677 | static int oldbuttons = 0; |
| 7678 | |
| 7679 | #ifdef FEAT_GUI |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7680 | // Don't put events in the input queue now. |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7681 | if (hold_gui_events) |
| 7682 | return; |
| 7683 | #endif |
| 7684 | |
| 7685 | mouse.operation = MOUSE_GETINFO; |
| 7686 | if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1 |
| 7687 | && ioctl(1, FBIO_MODEINFO, &video) != -1 |
| 7688 | && ioctl(1, CONS_MOUSECTL, &mouse) != -1 |
| 7689 | && video.vi_cheight > 0 && video.vi_cwidth > 0) |
| 7690 | { |
| 7691 | row = mouse.u.data.y / video.vi_cheight; |
| 7692 | col = mouse.u.data.x / video.vi_cwidth; |
| 7693 | buttons = mouse.u.data.buttons; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7694 | string[0] = ESC; // Our termcode |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7695 | string[1] = 'M'; |
| 7696 | string[2] = 'S'; |
| 7697 | if (oldbuttons == buttons && buttons != 0) |
| 7698 | { |
| 7699 | button = MOUSE_DRAG; |
| 7700 | } |
| 7701 | else |
| 7702 | { |
| 7703 | switch (buttons) |
| 7704 | { |
| 7705 | case 0: |
| 7706 | button = MOUSE_RELEASE; |
| 7707 | break; |
| 7708 | case 1: |
| 7709 | button = MOUSE_LEFT; |
| 7710 | break; |
| 7711 | case 2: |
| 7712 | button = MOUSE_MIDDLE; |
| 7713 | break; |
| 7714 | case 4: |
| 7715 | button = MOUSE_RIGHT; |
| 7716 | break; |
| 7717 | default: |
| 7718 | return; |
| 7719 | } |
| 7720 | oldbuttons = buttons; |
| 7721 | } |
| 7722 | string[3] = (char_u)(button); |
| 7723 | string[4] = (char_u)(col + ' ' + 1); |
| 7724 | string[5] = (char_u)(row + ' ' + 1); |
| 7725 | add_to_input_buf(string, 6); |
| 7726 | } |
| 7727 | return; |
| 7728 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7729 | #endif // FEAT_SYSMOUSE |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7730 | |
LemonBoy | 9987fe8 | 2024-07-04 13:20:49 +0200 | [diff] [blame] | 7731 | /* |
| 7732 | * Fill the buffer 'buf' with 'len' random bytes. |
| 7733 | * Returns FAIL if the OS PRNG is not available or something went wrong. |
| 7734 | */ |
| 7735 | int |
| 7736 | mch_get_random(char_u *buf, int len) |
| 7737 | { |
| 7738 | static int dev_urandom_state = NOTDONE; |
| 7739 | |
| 7740 | if (dev_urandom_state == FAIL) |
| 7741 | return FAIL; |
| 7742 | |
| 7743 | int fd = open("/dev/urandom", O_RDONLY); |
| 7744 | |
| 7745 | // Attempt reading /dev/urandom. |
| 7746 | if (fd == -1) |
| 7747 | dev_urandom_state = FAIL; |
| 7748 | else if (read(fd, buf, len) == len) |
Christian Brabandt | 93a3d2b | 2024-07-05 09:54:30 +0200 | [diff] [blame] | 7749 | { |
LemonBoy | 9987fe8 | 2024-07-04 13:20:49 +0200 | [diff] [blame] | 7750 | dev_urandom_state = OK; |
Christian Brabandt | 93a3d2b | 2024-07-05 09:54:30 +0200 | [diff] [blame] | 7751 | close(fd); |
| 7752 | } |
LemonBoy | 9987fe8 | 2024-07-04 13:20:49 +0200 | [diff] [blame] | 7753 | else |
| 7754 | { |
| 7755 | dev_urandom_state = FAIL; |
| 7756 | close(fd); |
| 7757 | } |
| 7758 | |
| 7759 | return dev_urandom_state; |
| 7760 | } |
| 7761 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7762 | #if defined(FEAT_LIBCALL) || defined(PROTO) |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 7763 | typedef char_u * (*STRPROCSTR)(char_u *); |
| 7764 | typedef char_u * (*INTPROCSTR)(int); |
| 7765 | typedef int (*STRPROCINT)(char_u *); |
| 7766 | typedef int (*INTPROCINT)(int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7767 | |
| 7768 | /* |
| 7769 | * Call a DLL routine which takes either a string or int param |
| 7770 | * and returns an allocated string. |
| 7771 | */ |
| 7772 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7773 | mch_libcall( |
| 7774 | char_u *libname, |
| 7775 | char_u *funcname, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7776 | char_u *argstring, // NULL when using a argint |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7777 | int argint, |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7778 | char_u **string_result, // NULL when using number_result |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7779 | int *number_result) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7780 | { |
| 7781 | # if defined(USE_DLOPEN) |
| 7782 | void *hinstLib; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7783 | char *dlerr = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7784 | # else |
| 7785 | shl_t hinstLib; |
| 7786 | # endif |
| 7787 | STRPROCSTR ProcAdd; |
| 7788 | INTPROCSTR ProcAddI; |
| 7789 | char_u *retval_str = NULL; |
| 7790 | int retval_int = 0; |
| 7791 | int success = FALSE; |
| 7792 | |
Bram Moolenaar | b39ef12 | 2006-06-22 16:19:31 +0000 | [diff] [blame] | 7793 | /* |
| 7794 | * Get a handle to the DLL module. |
| 7795 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7796 | # if defined(USE_DLOPEN) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7797 | // First clear any error, it's not cleared by the dlopen() call. |
Bram Moolenaar | b39ef12 | 2006-06-22 16:19:31 +0000 | [diff] [blame] | 7798 | (void)dlerror(); |
| 7799 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7800 | hinstLib = dlopen((char *)libname, RTLD_LAZY |
| 7801 | # ifdef RTLD_LOCAL |
| 7802 | | RTLD_LOCAL |
| 7803 | # endif |
| 7804 | ); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7805 | if (hinstLib == NULL) |
| 7806 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7807 | // "dlerr" must be used before dlclose() |
=?UTF-8?q?Dundar=20G=C3=B6c?= | 420fabc | 2022-01-28 15:28:04 +0000 | [diff] [blame] | 7808 | dlerr = dlerror(); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7809 | if (dlerr != NULL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7810 | semsg(_("dlerror = \"%s\""), dlerr); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7811 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7812 | # else |
| 7813 | hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L); |
| 7814 | # endif |
| 7815 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7816 | // If the handle is valid, try to get the function address. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7817 | if (hinstLib != NULL) |
| 7818 | { |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 7819 | # ifdef USING_SETJMP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7820 | /* |
| 7821 | * Catch a crash when calling the library function. For example when |
| 7822 | * using a number where a string pointer is expected. |
| 7823 | */ |
| 7824 | mch_startjmp(); |
| 7825 | if (SETJMP(lc_jump_env) != 0) |
| 7826 | { |
| 7827 | success = FALSE; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 7828 | # if defined(USE_DLOPEN) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7829 | dlerr = NULL; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 7830 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7831 | mch_didjmp(); |
| 7832 | } |
| 7833 | else |
| 7834 | # endif |
| 7835 | { |
| 7836 | retval_str = NULL; |
| 7837 | retval_int = 0; |
| 7838 | |
| 7839 | if (argstring != NULL) |
| 7840 | { |
| 7841 | # if defined(USE_DLOPEN) |
Bram Moolenaar | 6d721c7 | 2017-01-17 16:56:28 +0100 | [diff] [blame] | 7842 | *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname); |
=?UTF-8?q?Dundar=20G=C3=B6c?= | 420fabc | 2022-01-28 15:28:04 +0000 | [diff] [blame] | 7843 | dlerr = dlerror(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7844 | # else |
| 7845 | if (shl_findsym(&hinstLib, (const char *)funcname, |
| 7846 | TYPE_PROCEDURE, (void *)&ProcAdd) < 0) |
| 7847 | ProcAdd = NULL; |
| 7848 | # endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7849 | if ((success = (ProcAdd != NULL |
| 7850 | # if defined(USE_DLOPEN) |
| 7851 | && dlerr == NULL |
| 7852 | # endif |
| 7853 | ))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7854 | { |
| 7855 | if (string_result == NULL) |
Bram Moolenaar | a422486 | 2020-09-13 22:00:12 +0200 | [diff] [blame] | 7856 | retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7857 | else |
| 7858 | retval_str = (ProcAdd)(argstring); |
| 7859 | } |
| 7860 | } |
| 7861 | else |
| 7862 | { |
| 7863 | # if defined(USE_DLOPEN) |
Bram Moolenaar | 6d721c7 | 2017-01-17 16:56:28 +0100 | [diff] [blame] | 7864 | *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname); |
=?UTF-8?q?Dundar=20G=C3=B6c?= | 420fabc | 2022-01-28 15:28:04 +0000 | [diff] [blame] | 7865 | dlerr = dlerror(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7866 | # else |
| 7867 | if (shl_findsym(&hinstLib, (const char *)funcname, |
| 7868 | TYPE_PROCEDURE, (void *)&ProcAddI) < 0) |
| 7869 | ProcAddI = NULL; |
| 7870 | # endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7871 | if ((success = (ProcAddI != NULL |
| 7872 | # if defined(USE_DLOPEN) |
| 7873 | && dlerr == NULL |
| 7874 | # endif |
| 7875 | ))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7876 | { |
| 7877 | if (string_result == NULL) |
Bram Moolenaar | a422486 | 2020-09-13 22:00:12 +0200 | [diff] [blame] | 7878 | retval_int = ((INTPROCINT)(void *)ProcAddI)(argint); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7879 | else |
| 7880 | retval_str = (ProcAddI)(argint); |
| 7881 | } |
| 7882 | } |
| 7883 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7884 | // Save the string before we free the library. |
| 7885 | // Assume that a "1" or "-1" result is an illegal pointer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7886 | if (string_result == NULL) |
| 7887 | *number_result = retval_int; |
| 7888 | else if (retval_str != NULL |
| 7889 | && retval_str != (char_u *)1 |
| 7890 | && retval_str != (char_u *)-1) |
| 7891 | *string_result = vim_strsave(retval_str); |
| 7892 | } |
| 7893 | |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 7894 | # ifdef USING_SETJMP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7895 | mch_endjmp(); |
| 7896 | # ifdef SIGHASARG |
| 7897 | if (lc_signal != 0) |
| 7898 | { |
| 7899 | int i; |
| 7900 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7901 | // try to find the name of this signal |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7902 | for (i = 0; signal_info[i].sig != -1; i++) |
| 7903 | if (lc_signal == signal_info[i].sig) |
| 7904 | break; |
Bram Moolenaar | 50809a4 | 2023-05-20 16:39:07 +0100 | [diff] [blame] | 7905 | semsg(_(e_got_sig_str_in_libcall), signal_info[i].name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7906 | } |
| 7907 | # endif |
| 7908 | # endif |
| 7909 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7910 | # if defined(USE_DLOPEN) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7911 | // "dlerr" must be used before dlclose() |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7912 | if (dlerr != NULL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7913 | semsg(_("dlerror = \"%s\""), dlerr); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7914 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7915 | // Free the DLL module. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7916 | (void)dlclose(hinstLib); |
| 7917 | # else |
| 7918 | (void)shl_unload(hinstLib); |
| 7919 | # endif |
| 7920 | } |
| 7921 | |
| 7922 | if (!success) |
| 7923 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 7924 | semsg(_(e_library_call_failed_for_str), funcname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7925 | return FAIL; |
| 7926 | } |
| 7927 | |
| 7928 | return OK; |
| 7929 | } |
| 7930 | #endif |
| 7931 | |
| 7932 | #if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7933 | static int xterm_trace = -1; // default: disabled |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7934 | static int xterm_button; |
| 7935 | |
| 7936 | /* |
| 7937 | * Setup a dummy window for X selections in a terminal. |
| 7938 | */ |
| 7939 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7940 | setup_term_clip(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7941 | { |
| 7942 | int z = 0; |
| 7943 | char *strp = ""; |
| 7944 | Widget AppShell; |
| 7945 | |
| 7946 | if (!x_connect_to_server()) |
| 7947 | return; |
| 7948 | |
| 7949 | open_app_context(); |
| 7950 | if (app_context != NULL && xterm_Shell == (Widget)0) |
| 7951 | { |
Dominique Pellé | 4927bc7 | 2023-09-24 16:12:07 +0200 | [diff] [blame] | 7952 | int (*oldhandler)(Display*, XErrorEvent*); |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 7953 | # if defined(USING_SETJMP) |
Dominique Pellé | 4927bc7 | 2023-09-24 16:12:07 +0200 | [diff] [blame] | 7954 | int (*oldIOhandler)(Display*); |
Bram Moolenaar | edce742 | 2019-01-20 18:39:30 +0100 | [diff] [blame] | 7955 | # endif |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 7956 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 1ac56c2 | 2019-01-17 22:28:22 +0100 | [diff] [blame] | 7957 | elapsed_T start_tv; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7958 | |
| 7959 | if (p_verbose > 0) |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 7960 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7961 | # endif |
| 7962 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7963 | // Ignore X errors while opening the display |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7964 | oldhandler = XSetErrorHandler(x_error_check); |
| 7965 | |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 7966 | # if defined(USING_SETJMP) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7967 | // Ignore X IO errors while opening the display |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7968 | oldIOhandler = XSetIOErrorHandler(x_IOerror_check); |
| 7969 | mch_startjmp(); |
| 7970 | if (SETJMP(lc_jump_env) != 0) |
| 7971 | { |
| 7972 | mch_didjmp(); |
| 7973 | xterm_dpy = NULL; |
| 7974 | } |
| 7975 | else |
Bram Moolenaar | edce742 | 2019-01-20 18:39:30 +0100 | [diff] [blame] | 7976 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7977 | { |
| 7978 | xterm_dpy = XtOpenDisplay(app_context, xterm_display, |
| 7979 | "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp); |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 7980 | if (xterm_dpy != NULL) |
| 7981 | xterm_dpy_retry_count = 0; |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 7982 | # if defined(USING_SETJMP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7983 | mch_endjmp(); |
Bram Moolenaar | edce742 | 2019-01-20 18:39:30 +0100 | [diff] [blame] | 7984 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7985 | } |
| 7986 | |
Bram Moolenaar | b2148f5 | 2019-01-20 23:43:57 +0100 | [diff] [blame] | 7987 | # if defined(USING_SETJMP) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7988 | // Now handle X IO errors normally. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7989 | (void)XSetIOErrorHandler(oldIOhandler); |
Bram Moolenaar | edce742 | 2019-01-20 18:39:30 +0100 | [diff] [blame] | 7990 | # endif |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 7991 | // Now handle X errors normally. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7992 | (void)XSetErrorHandler(oldhandler); |
| 7993 | |
| 7994 | if (xterm_dpy == NULL) |
| 7995 | { |
| 7996 | if (p_verbose > 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 7997 | verb_msg(_("Opening the X display failed")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7998 | return; |
| 7999 | } |
| 8000 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8001 | // Catch terminating error of the X server connection. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8002 | (void)XSetIOErrorHandler(x_IOerror_handler); |
| 8003 | |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 8004 | # ifdef ELAPSED_FUNC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8005 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 8006 | { |
| 8007 | verbose_enter(); |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 8008 | xopen_message(ELAPSED_FUNC(start_tv)); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 8009 | verbose_leave(); |
| 8010 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8011 | # endif |
| 8012 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8013 | // Create a Shell to make converters work. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8014 | AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm", |
| 8015 | applicationShellWidgetClass, xterm_dpy, |
| 8016 | NULL); |
| 8017 | if (AppShell == (Widget)0) |
| 8018 | return; |
| 8019 | xterm_Shell = XtVaCreatePopupShell("VIM", |
| 8020 | topLevelShellWidgetClass, AppShell, |
| 8021 | XtNmappedWhenManaged, 0, |
| 8022 | XtNwidth, 1, |
| 8023 | XtNheight, 1, |
| 8024 | NULL); |
| 8025 | if (xterm_Shell == (Widget)0) |
| 8026 | return; |
| 8027 | |
| 8028 | x11_setup_atoms(xterm_dpy); |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 8029 | x11_setup_selection(xterm_Shell); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8030 | if (x11_display == NULL) |
| 8031 | x11_display = xterm_dpy; |
| 8032 | |
| 8033 | XtRealizeWidget(xterm_Shell); |
| 8034 | XSync(xterm_dpy, False); |
| 8035 | xterm_update(); |
| 8036 | } |
| 8037 | if (xterm_Shell != (Widget)0) |
| 8038 | { |
| 8039 | clip_init(TRUE); |
| 8040 | if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL) |
| 8041 | x11_window = (Window)atol(strp); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8042 | // Check if $WINDOWID is valid. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8043 | if (test_x11_window(xterm_dpy) == FAIL) |
| 8044 | x11_window = 0; |
| 8045 | if (x11_window != 0) |
| 8046 | xterm_trace = 0; |
| 8047 | } |
| 8048 | } |
| 8049 | |
| 8050 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8051 | start_xterm_trace(int button) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8052 | { |
| 8053 | if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0) |
| 8054 | return; |
| 8055 | xterm_trace = 1; |
| 8056 | xterm_button = button; |
| 8057 | do_xterm_trace(); |
| 8058 | } |
| 8059 | |
| 8060 | |
| 8061 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8062 | stop_xterm_trace(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8063 | { |
| 8064 | if (xterm_trace < 0) |
| 8065 | return; |
| 8066 | xterm_trace = 0; |
| 8067 | } |
| 8068 | |
| 8069 | /* |
| 8070 | * Query the xterm pointer and generate mouse termcodes if necessary |
| 8071 | * return TRUE if dragging is active, else FALSE |
| 8072 | */ |
| 8073 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8074 | do_xterm_trace(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8075 | { |
| 8076 | Window root, child; |
| 8077 | int root_x, root_y; |
| 8078 | int win_x, win_y; |
| 8079 | int row, col; |
| 8080 | int_u mask_return; |
| 8081 | char_u buf[50]; |
| 8082 | char_u *strp; |
| 8083 | long got_hints; |
| 8084 | static char_u *mouse_code; |
| 8085 | static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER}; |
| 8086 | static int prev_row = 0, prev_col = 0; |
| 8087 | static XSizeHints xterm_hints; |
| 8088 | |
| 8089 | if (xterm_trace <= 0) |
| 8090 | return FALSE; |
| 8091 | |
| 8092 | if (xterm_trace == 1) |
| 8093 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8094 | // Get the hints just before tracking starts. The font size might |
| 8095 | // have changed recently. |
Bram Moolenaar | a6c2c91 | 2008-01-13 15:31:00 +0000 | [diff] [blame] | 8096 | if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints) |
| 8097 | || !(got_hints & PResizeInc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8098 | || xterm_hints.width_inc <= 1 |
| 8099 | || xterm_hints.height_inc <= 1) |
| 8100 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8101 | xterm_trace = -1; // Not enough data -- disable tracing |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8102 | return FALSE; |
| 8103 | } |
| 8104 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8105 | // Rely on the same mouse code for the duration of this |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8106 | mouse_code = find_termcode(mouse_name); |
| 8107 | prev_row = mouse_row; |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 8108 | prev_col = mouse_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8109 | xterm_trace = 2; |
| 8110 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8111 | // Find the offset of the chars, there might be a scrollbar on the |
| 8112 | // left of the window and/or a menu on the top (eterm etc.) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8113 | XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y, |
| 8114 | &win_x, &win_y, &mask_return); |
| 8115 | xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row) |
| 8116 | - (xterm_hints.height_inc / 2); |
| 8117 | if (xterm_hints.y <= xterm_hints.height_inc / 2) |
| 8118 | xterm_hints.y = 2; |
| 8119 | xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col) |
| 8120 | - (xterm_hints.width_inc / 2); |
| 8121 | if (xterm_hints.x <= xterm_hints.width_inc / 2) |
| 8122 | xterm_hints.x = 2; |
| 8123 | return TRUE; |
| 8124 | } |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 8125 | if (mouse_code == NULL || STRLEN(mouse_code) > 45) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8126 | { |
| 8127 | xterm_trace = 0; |
| 8128 | return FALSE; |
| 8129 | } |
| 8130 | |
| 8131 | XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y, |
| 8132 | &win_x, &win_y, &mask_return); |
| 8133 | |
| 8134 | row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc); |
| 8135 | col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc); |
| 8136 | if (row == prev_row && col == prev_col) |
| 8137 | return TRUE; |
| 8138 | |
| 8139 | STRCPY(buf, mouse_code); |
| 8140 | strp = buf + STRLEN(buf); |
| 8141 | *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20; |
| 8142 | *strp++ = (char_u)(col + ' ' + 1); |
| 8143 | *strp++ = (char_u)(row + ' ' + 1); |
| 8144 | *strp = 0; |
| 8145 | add_to_input_buf(buf, STRLEN(buf)); |
| 8146 | |
| 8147 | prev_row = row; |
| 8148 | prev_col = col; |
| 8149 | return TRUE; |
| 8150 | } |
| 8151 | |
Bram Moolenaar | d4aa83a | 2019-05-09 18:59:31 +0200 | [diff] [blame] | 8152 | # if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8153 | /* |
| 8154 | * Destroy the display, window and app_context. Required for GTK. |
| 8155 | */ |
| 8156 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8157 | clear_xterm_clip(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8158 | { |
| 8159 | if (xterm_Shell != (Widget)0) |
| 8160 | { |
| 8161 | XtDestroyWidget(xterm_Shell); |
| 8162 | xterm_Shell = (Widget)0; |
| 8163 | } |
| 8164 | if (xterm_dpy != NULL) |
| 8165 | { |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 8166 | # if 0 |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8167 | // Lesstif and Solaris crash here, lose some memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8168 | XtCloseDisplay(xterm_dpy); |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 8169 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8170 | if (x11_display == xterm_dpy) |
| 8171 | x11_display = NULL; |
| 8172 | xterm_dpy = NULL; |
| 8173 | } |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 8174 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8175 | if (app_context != (XtAppContext)NULL) |
| 8176 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8177 | // Lesstif and Solaris crash here, lose some memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8178 | XtDestroyApplicationContext(app_context); |
| 8179 | app_context = (XtAppContext)NULL; |
| 8180 | } |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 8181 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8182 | } |
| 8183 | # endif |
| 8184 | |
| 8185 | /* |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 8186 | * Catch up with GUI or X events. |
| 8187 | */ |
| 8188 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8189 | clip_update(void) |
Bram Moolenaar | 090cfc1 | 2013-03-19 12:35:42 +0100 | [diff] [blame] | 8190 | { |
| 8191 | # ifdef FEAT_GUI |
| 8192 | if (gui.in_use) |
| 8193 | gui_mch_update(); |
| 8194 | else |
| 8195 | # endif |
| 8196 | if (xterm_Shell != (Widget)0) |
| 8197 | xterm_update(); |
| 8198 | } |
| 8199 | |
| 8200 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8201 | * Catch up with any queued X events. This may put keyboard input into the |
| 8202 | * input buffer, call resize call-backs, trigger timers etc. If there is |
| 8203 | * nothing in the X event queue (& no timers pending), then we return |
| 8204 | * immediately. |
| 8205 | */ |
| 8206 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8207 | xterm_update(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8208 | { |
| 8209 | XEvent event; |
| 8210 | |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 8211 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8212 | { |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 8213 | XtInputMask mask = XtAppPending(app_context); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8214 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 8215 | if (mask == 0 || vim_is_input_buf_full()) |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 8216 | break; |
| 8217 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 8218 | if (mask & XtIMXEvent) |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 8219 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8220 | // There is an event to process. |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 8221 | XtAppNextEvent(app_context, &event); |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 8222 | #ifdef FEAT_CLIENTSERVER |
| 8223 | { |
| 8224 | XPropertyEvent *e = (XPropertyEvent *)&event; |
| 8225 | |
| 8226 | if (e->type == PropertyNotify && e->window == commWindow |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8227 | && e->atom == commProperty && e->state == PropertyNewValue) |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 8228 | serverEventProc(xterm_dpy, &event, 0); |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 8229 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8230 | #endif |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 8231 | XtDispatchEvent(&event); |
| 8232 | } |
Bram Moolenaar | b1fc2bf | 2015-03-20 16:26:54 +0100 | [diff] [blame] | 8233 | else |
| 8234 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8235 | // There is something else than an event to process. |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 8236 | XtAppProcessEvent(app_context, mask); |
| 8237 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8238 | } |
| 8239 | } |
| 8240 | |
| 8241 | int |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 8242 | clip_xterm_own_selection(Clipboard_T *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8243 | { |
| 8244 | if (xterm_Shell != (Widget)0) |
| 8245 | return clip_x11_own_selection(xterm_Shell, cbd); |
| 8246 | return FAIL; |
| 8247 | } |
| 8248 | |
| 8249 | void |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 8250 | clip_xterm_lose_selection(Clipboard_T *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8251 | { |
| 8252 | if (xterm_Shell != (Widget)0) |
| 8253 | clip_x11_lose_selection(xterm_Shell, cbd); |
| 8254 | } |
| 8255 | |
| 8256 | void |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 8257 | clip_xterm_request_selection(Clipboard_T *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8258 | { |
| 8259 | if (xterm_Shell != (Widget)0) |
| 8260 | clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd); |
| 8261 | } |
| 8262 | |
| 8263 | void |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 8264 | clip_xterm_set_selection(Clipboard_T *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8265 | { |
| 8266 | clip_x11_set_selection(cbd); |
| 8267 | } |
| 8268 | #endif |
| 8269 | |
| 8270 | |
| 8271 | #if defined(USE_XSMP) || defined(PROTO) |
| 8272 | /* |
| 8273 | * Code for X Session Management Protocol. |
| 8274 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8275 | |
| 8276 | # if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8277 | /* |
| 8278 | * This is our chance to ask the user if they want to save, |
| 8279 | * or abort the logout |
| 8280 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8281 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8282 | xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8283 | { |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 8284 | int save_cmod_flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8285 | int cancel_shutdown = False; |
| 8286 | |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 8287 | save_cmod_flags = cmdmod.cmod_flags; |
| 8288 | cmdmod.cmod_flags |= CMOD_CONFIRM; |
Bram Moolenaar | 027387f | 2016-01-02 22:25:52 +0100 | [diff] [blame] | 8289 | if (check_changed_any(FALSE, FALSE)) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8290 | // Mustn't logout |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8291 | cancel_shutdown = True; |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 8292 | cmdmod.cmod_flags = save_cmod_flags; |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8293 | setcursor(); // position cursor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8294 | out_flush(); |
| 8295 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8296 | // Done interaction |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8297 | SmcInteractDone(smc_conn, cancel_shutdown); |
| 8298 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8299 | // Finish off |
| 8300 | // Only end save-yourself here if we're not cancelling shutdown; |
| 8301 | // we'll get a cancelled callback later in which we'll end it. |
| 8302 | // Hopefully get around glitchy SMs (like GNOME-1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8303 | if (!cancel_shutdown) |
| 8304 | { |
| 8305 | xsmp.save_yourself = False; |
| 8306 | SmcSaveYourselfDone(smc_conn, True); |
| 8307 | } |
| 8308 | } |
| 8309 | # endif |
| 8310 | |
| 8311 | /* |
| 8312 | * Callback that starts save-yourself. |
| 8313 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8314 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8315 | xsmp_handle_save_yourself( |
| 8316 | SmcConn smc_conn, |
| 8317 | SmPointer client_data UNUSED, |
| 8318 | int save_type UNUSED, |
| 8319 | Bool shutdown, |
| 8320 | int interact_style UNUSED, |
| 8321 | Bool fast UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8322 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8323 | // Handle already being in saveyourself |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8324 | if (xsmp.save_yourself) |
| 8325 | SmcSaveYourselfDone(smc_conn, True); |
| 8326 | xsmp.save_yourself = True; |
| 8327 | xsmp.shutdown = shutdown; |
| 8328 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8329 | // First up, preserve all files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8330 | out_flush(); |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8331 | ml_sync_all(FALSE, FALSE); // preserve all swap files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8332 | |
| 8333 | if (p_verbose > 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 8334 | verb_msg(_("XSMP handling save-yourself request")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8335 | |
| 8336 | # if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8337 | // Now see if we can ask about unsaved files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8338 | if (shutdown && !fast && gui.in_use) |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8339 | // Need to interact with user, but need SM's permission |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8340 | SmcInteractRequest(smc_conn, SmDialogError, |
| 8341 | xsmp_handle_interaction, client_data); |
| 8342 | else |
| 8343 | # endif |
| 8344 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8345 | // Can stop the cycle here |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8346 | SmcSaveYourselfDone(smc_conn, True); |
| 8347 | xsmp.save_yourself = False; |
| 8348 | } |
| 8349 | } |
| 8350 | |
| 8351 | |
| 8352 | /* |
| 8353 | * Callback to warn us of imminent death. |
| 8354 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8355 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8356 | xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8357 | { |
| 8358 | xsmp_close(); |
| 8359 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8360 | // quit quickly leaving swapfiles for modified buffers behind |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8361 | getout_preserve_modified(0); |
| 8362 | } |
| 8363 | |
| 8364 | |
| 8365 | /* |
| 8366 | * Callback to tell us that save-yourself has completed. |
| 8367 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8368 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8369 | xsmp_save_complete( |
| 8370 | SmcConn smc_conn UNUSED, |
| 8371 | SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8372 | { |
| 8373 | xsmp.save_yourself = False; |
| 8374 | } |
| 8375 | |
| 8376 | |
| 8377 | /* |
| 8378 | * Callback to tell us that an instigated shutdown was cancelled |
| 8379 | * (maybe even by us) |
| 8380 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8381 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8382 | xsmp_shutdown_cancelled( |
| 8383 | SmcConn smc_conn, |
| 8384 | SmPointer client_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8385 | { |
| 8386 | if (xsmp.save_yourself) |
| 8387 | SmcSaveYourselfDone(smc_conn, True); |
| 8388 | xsmp.save_yourself = False; |
| 8389 | xsmp.shutdown = False; |
| 8390 | } |
| 8391 | |
| 8392 | |
| 8393 | /* |
| 8394 | * Callback to tell us that a new ICE connection has been established. |
| 8395 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8396 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8397 | xsmp_ice_connection( |
| 8398 | IceConn iceConn, |
| 8399 | IcePointer clientData UNUSED, |
| 8400 | Bool opening, |
| 8401 | IcePointer *watchData UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8402 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8403 | // Intercept creation of ICE connection fd |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8404 | if (opening) |
| 8405 | { |
| 8406 | xsmp_icefd = IceConnectionNumber(iceConn); |
| 8407 | IceRemoveConnectionWatch(xsmp_ice_connection, NULL); |
| 8408 | } |
| 8409 | } |
| 8410 | |
| 8411 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8412 | // Handle any ICE processing that's required; return FAIL if SM lost |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8413 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8414 | xsmp_handle_requests(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8415 | { |
| 8416 | Bool rep; |
| 8417 | |
| 8418 | if (IceProcessMessages(xsmp.iceconn, NULL, &rep) |
| 8419 | == IceProcessMessagesIOError) |
| 8420 | { |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8421 | // Lost ICE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8422 | if (p_verbose > 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 8423 | verb_msg(_("XSMP lost ICE connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8424 | xsmp_close(); |
| 8425 | return FAIL; |
| 8426 | } |
| 8427 | else |
| 8428 | return OK; |
| 8429 | } |
| 8430 | |
| 8431 | static int dummy; |
| 8432 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8433 | // Set up X Session Management Protocol |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8434 | void |
| 8435 | xsmp_init(void) |
| 8436 | { |
| 8437 | char errorstring[80]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8438 | SmcCallbacks smcallbacks; |
| 8439 | #if 0 |
| 8440 | SmPropValue smname; |
| 8441 | SmProp smnameprop; |
| 8442 | SmProp *smprops[1]; |
| 8443 | #endif |
| 8444 | |
| 8445 | if (p_verbose > 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 8446 | verb_msg(_("XSMP opening connection")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8447 | |
| 8448 | xsmp.save_yourself = xsmp.shutdown = False; |
| 8449 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8450 | // Set up SM callbacks - must have all, even if they're not used |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8451 | smcallbacks.save_yourself.callback = xsmp_handle_save_yourself; |
| 8452 | smcallbacks.save_yourself.client_data = NULL; |
| 8453 | smcallbacks.die.callback = xsmp_die; |
| 8454 | smcallbacks.die.client_data = NULL; |
| 8455 | smcallbacks.save_complete.callback = xsmp_save_complete; |
| 8456 | smcallbacks.save_complete.client_data = NULL; |
| 8457 | smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled; |
| 8458 | smcallbacks.shutdown_cancelled.client_data = NULL; |
| 8459 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8460 | // Set up a watch on ICE connection creations. The "dummy" argument is |
| 8461 | // apparently required for FreeBSD (we get a BUS error when using NULL). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8462 | if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0) |
| 8463 | { |
| 8464 | if (p_verbose > 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 8465 | verb_msg(_("XSMP ICE connection watch failed")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8466 | return; |
| 8467 | } |
| 8468 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8469 | // Create an SM connection |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8470 | xsmp.smcconn = SmcOpenConnection( |
| 8471 | NULL, |
| 8472 | NULL, |
| 8473 | SmProtoMajor, |
| 8474 | SmProtoMinor, |
| 8475 | SmcSaveYourselfProcMask | SmcDieProcMask |
| 8476 | | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask, |
| 8477 | &smcallbacks, |
| 8478 | NULL, |
Bram Moolenaar | e820801 | 2008-06-20 09:59:25 +0000 | [diff] [blame] | 8479 | &xsmp.clientid, |
Bram Moolenaar | 4841a7c | 2018-09-22 14:08:49 +0200 | [diff] [blame] | 8480 | sizeof(errorstring) - 1, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8481 | errorstring); |
| 8482 | if (xsmp.smcconn == NULL) |
| 8483 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8484 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 8485 | { |
Bram Moolenaar | e1be118 | 2020-10-24 13:30:51 +0200 | [diff] [blame] | 8486 | char errorreport[132]; |
| 8487 | |
| 8488 | // If the message is too long it might not be NUL terminated. Add |
| 8489 | // a NUL at the end to make sure we don't go over the end. |
| 8490 | errorstring[sizeof(errorstring) - 1] = NUL; |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 8491 | vim_snprintf(errorreport, sizeof(errorreport), |
| 8492 | _("XSMP SmcOpenConnection failed: %s"), errorstring); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 8493 | verb_msg(errorreport); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 8494 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8495 | return; |
| 8496 | } |
| 8497 | xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn); |
| 8498 | |
| 8499 | #if 0 |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8500 | // ID ourselves |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8501 | smname.value = "vim"; |
| 8502 | smname.length = 3; |
| 8503 | smnameprop.name = "SmProgram"; |
| 8504 | smnameprop.type = "SmARRAY8"; |
| 8505 | smnameprop.num_vals = 1; |
| 8506 | smnameprop.vals = &smname; |
| 8507 | |
| 8508 | smprops[0] = &smnameprop; |
| 8509 | SmcSetProperties(xsmp.smcconn, 1, smprops); |
| 8510 | #endif |
| 8511 | } |
| 8512 | |
| 8513 | |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8514 | // Shut down XSMP comms. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8515 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 8516 | xsmp_close(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8517 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 8518 | if (xsmp_icefd == -1) |
| 8519 | return; |
| 8520 | |
| 8521 | SmcCloseConnection(xsmp.smcconn, 0, NULL); |
| 8522 | if (xsmp.clientid != NULL) |
| 8523 | free(xsmp.clientid); |
| 8524 | xsmp.clientid = NULL; |
| 8525 | xsmp_icefd = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8526 | } |
Bram Moolenaar | 0f87373 | 2019-12-05 20:28:46 +0100 | [diff] [blame] | 8527 | #endif // USE_XSMP |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8528 | |
| 8529 | #if defined(FEAT_RELTIME) || defined(PROTO) |
Bram Moolenaar | 08210f8 | 2023-04-13 19:15:54 +0100 | [diff] [blame] | 8530 | # if defined(PROF_NSEC) || defined(PROTO) |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8531 | /* |
| 8532 | * Implement timeout with timer_create() and timer_settime(). |
| 8533 | */ |
Bram Moolenaar | 155f2d1 | 2022-06-20 13:38:33 +0100 | [diff] [blame] | 8534 | static volatile sig_atomic_t timeout_flag = FALSE; |
| 8535 | static timer_t timer_id; |
| 8536 | static int timer_created = FALSE; |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8537 | |
| 8538 | /* |
| 8539 | * Callback for when the timer expires. |
| 8540 | */ |
| 8541 | static void |
| 8542 | set_flag(union sigval _unused UNUSED) |
| 8543 | { |
| 8544 | timeout_flag = TRUE; |
| 8545 | } |
| 8546 | |
| 8547 | /* |
| 8548 | * Stop any active timeout. |
| 8549 | */ |
| 8550 | void |
| 8551 | stop_timeout(void) |
| 8552 | { |
| 8553 | static struct itimerspec disarm = {{0, 0}, {0, 0}}; |
| 8554 | |
| 8555 | if (timer_created) |
| 8556 | { |
| 8557 | int ret = timer_settime(timer_id, 0, &disarm, NULL); |
| 8558 | |
| 8559 | if (ret < 0) |
| 8560 | semsg(_(e_could_not_clear_timeout_str), strerror(errno)); |
| 8561 | } |
| 8562 | |
| 8563 | // Clear the current timeout flag; any previous timeout should be |
| 8564 | // considered _not_ triggered. |
| 8565 | timeout_flag = FALSE; |
| 8566 | } |
| 8567 | |
| 8568 | /* |
| 8569 | * Start the timeout timer. |
| 8570 | * |
| 8571 | * The return value is a pointer to a flag that is initialised to FALSE. If the |
| 8572 | * timeout expires, the flag is set to TRUE. This will only return pointers to |
| 8573 | * static memory; i.e. any pointer returned by this function may always be |
| 8574 | * safely dereferenced. |
| 8575 | * |
| 8576 | * This function is not expected to fail, but if it does it will still return a |
| 8577 | * valid flag pointer; the flag will remain stuck as FALSE . |
| 8578 | */ |
Bram Moolenaar | 155f2d1 | 2022-06-20 13:38:33 +0100 | [diff] [blame] | 8579 | volatile sig_atomic_t * |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8580 | start_timeout(long msec) |
| 8581 | { |
| 8582 | struct itimerspec interval = { |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 8583 | {0, 0}, // Do not repeat. |
| 8584 | {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8585 | int ret; |
| 8586 | |
| 8587 | // This is really the caller's responsibility, but let's make sure the |
| 8588 | // previous timer has been stopped. |
| 8589 | stop_timeout(); |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8590 | |
| 8591 | if (!timer_created) |
| 8592 | { |
| 8593 | struct sigevent action = {0}; |
| 8594 | |
| 8595 | action.sigev_notify = SIGEV_THREAD; |
| 8596 | action.sigev_notify_function = set_flag; |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 8597 | ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id); |
| 8598 | if (ret < 0) |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8599 | { |
| 8600 | semsg(_(e_could_not_set_timeout_str), strerror(errno)); |
| 8601 | return &timeout_flag; |
| 8602 | } |
| 8603 | timer_created = TRUE; |
| 8604 | } |
| 8605 | |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 8606 | # ifdef FEAT_EVAL |
Bram Moolenaar | 616592e | 2022-06-17 15:17:10 +0100 | [diff] [blame] | 8607 | ch_log(NULL, "setting timeout timer to %d sec %ld nsec", |
| 8608 | (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec); |
Bram Moolenaar | 509ce03 | 2022-06-20 11:23:01 +0100 | [diff] [blame] | 8609 | # endif |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8610 | ret = timer_settime(timer_id, 0, &interval, NULL); |
| 8611 | if (ret < 0) |
| 8612 | semsg(_(e_could_not_set_timeout_str), strerror(errno)); |
| 8613 | |
| 8614 | return &timeout_flag; |
| 8615 | } |
| 8616 | |
Bram Moolenaar | c72e31d | 2022-06-16 18:47:20 +0100 | [diff] [blame] | 8617 | /* |
| 8618 | * To be used before fork/exec: delete any created timer. |
| 8619 | */ |
| 8620 | void |
| 8621 | delete_timer(void) |
| 8622 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 8623 | if (!timer_created) |
| 8624 | return; |
| 8625 | |
| 8626 | timer_delete(timer_id); |
| 8627 | timer_created = FALSE; |
Bram Moolenaar | c72e31d | 2022-06-16 18:47:20 +0100 | [diff] [blame] | 8628 | } |
| 8629 | |
Bram Moolenaar | 08210f8 | 2023-04-13 19:15:54 +0100 | [diff] [blame] | 8630 | # else // PROF_NSEC |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8631 | |
| 8632 | /* |
| 8633 | * Implement timeout with setitimer() |
| 8634 | */ |
Bram Moolenaar | 155f2d1 | 2022-06-20 13:38:33 +0100 | [diff] [blame] | 8635 | static struct sigaction prev_sigaction; |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 8636 | static volatile sig_atomic_t timeout_flag = FALSE; |
| 8637 | static int timer_active = FALSE; |
Bram Moolenaar | 155f2d1 | 2022-06-20 13:38:33 +0100 | [diff] [blame] | 8638 | static int timer_handler_active = FALSE; |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 8639 | static volatile sig_atomic_t alarm_pending = FALSE; |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8640 | |
| 8641 | /* |
| 8642 | * Handle SIGALRM for a timeout. |
| 8643 | */ |
Bram Moolenaar | 99c48fe | 2022-06-05 22:05:19 +0100 | [diff] [blame] | 8644 | static void |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8645 | set_flag SIGDEFARG(sigarg) |
| 8646 | { |
| 8647 | if (alarm_pending) |
| 8648 | alarm_pending = FALSE; |
| 8649 | else |
| 8650 | timeout_flag = TRUE; |
| 8651 | } |
| 8652 | |
| 8653 | /* |
| 8654 | * Stop any active timeout. |
| 8655 | */ |
| 8656 | void |
| 8657 | stop_timeout(void) |
| 8658 | { |
| 8659 | static struct itimerval disarm = {{0, 0}, {0, 0}}; |
| 8660 | int ret; |
| 8661 | |
| 8662 | if (timer_active) |
| 8663 | { |
| 8664 | timer_active = FALSE; |
Bram Moolenaar | 155f2d1 | 2022-06-20 13:38:33 +0100 | [diff] [blame] | 8665 | ret = setitimer(ITIMER_REAL, &disarm, NULL); |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8666 | if (ret < 0) |
| 8667 | // Should only get here as a result of coding errors. |
| 8668 | semsg(_(e_could_not_clear_timeout_str), strerror(errno)); |
| 8669 | } |
| 8670 | |
| 8671 | if (timer_handler_active) |
| 8672 | { |
| 8673 | timer_handler_active = FALSE; |
| 8674 | ret = sigaction(SIGALRM, &prev_sigaction, NULL); |
| 8675 | if (ret < 0) |
| 8676 | // Should only get here as a result of coding errors. |
| 8677 | semsg(_(e_could_not_reset_handler_for_timeout_str), |
| 8678 | strerror(errno)); |
| 8679 | } |
Bram Moolenaar | 155f2d1 | 2022-06-20 13:38:33 +0100 | [diff] [blame] | 8680 | timeout_flag = FALSE; |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8681 | } |
| 8682 | |
| 8683 | /* |
| 8684 | * Start the timeout timer. |
| 8685 | * |
| 8686 | * The return value is a pointer to a flag that is initialised to FALSE. If the |
| 8687 | * timeout expires, the flag is set to TRUE. This will only return pointers to |
| 8688 | * static memory; i.e. any pointer returned by this function may always be |
| 8689 | * safely dereferenced. |
| 8690 | * |
| 8691 | * This function is not expected to fail, but if it does it will still return a |
| 8692 | * valid flag pointer; the flag will remain stuck as FALSE . |
| 8693 | */ |
Bram Moolenaar | 155f2d1 | 2022-06-20 13:38:33 +0100 | [diff] [blame] | 8694 | volatile sig_atomic_t * |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8695 | start_timeout(long msec) |
| 8696 | { |
| 8697 | struct itimerval interval = { |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 8698 | {0, 0}, // Do not repeat. |
| 8699 | {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8700 | struct sigaction handle_alarm; |
| 8701 | int ret; |
| 8702 | sigset_t sigs; |
| 8703 | sigset_t saved_sigs; |
| 8704 | |
| 8705 | // This is really the caller's responsibility, but let's make sure the |
| 8706 | // previous timer has been stopped. |
| 8707 | stop_timeout(); |
| 8708 | |
| 8709 | // There is a small chance that SIGALRM is pending and so the handler must |
| 8710 | // ignore it on the first call. |
| 8711 | alarm_pending = FALSE; |
| 8712 | ret = sigemptyset(&sigs); |
| 8713 | ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret; |
| 8714 | ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret; |
| 8715 | timeout_flag = FALSE; |
| 8716 | ret = ret == 0 ? sigpending(&sigs) : ret; |
| 8717 | if (ret == 0) |
| 8718 | { |
| 8719 | alarm_pending = sigismember(&sigs, SIGALRM); |
Bram Moolenaar | 1f89abf | 2022-06-06 10:07:01 +0100 | [diff] [blame] | 8720 | ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL); |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8721 | } |
| 8722 | if (unlikely(ret != 0 || alarm_pending < 0)) |
| 8723 | { |
| 8724 | // Just catching coding errors. Write an error message, but carry on. |
| 8725 | semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno)); |
| 8726 | alarm_pending = FALSE; |
| 8727 | } |
| 8728 | |
| 8729 | // Set up the alarm handler first. |
| 8730 | ret = sigemptyset(&handle_alarm.sa_mask); |
| 8731 | handle_alarm.sa_handler = set_flag; |
| 8732 | handle_alarm.sa_flags = 0; |
| 8733 | ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret; |
| 8734 | if (ret < 0) |
| 8735 | { |
| 8736 | // Should only get here as a result of coding errors. |
| 8737 | semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno)); |
| 8738 | return &timeout_flag; |
| 8739 | } |
| 8740 | timer_handler_active = TRUE; |
| 8741 | |
| 8742 | // Set up the interval timer once the alarm handler is in place. |
Bram Moolenaar | 155f2d1 | 2022-06-20 13:38:33 +0100 | [diff] [blame] | 8743 | ret = setitimer(ITIMER_REAL, &interval, NULL); |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8744 | if (ret < 0) |
| 8745 | { |
| 8746 | // Should only get here as a result of coding errors. |
| 8747 | semsg(_(e_could_not_set_timeout_str), strerror(errno)); |
| 8748 | stop_timeout(); |
| 8749 | return &timeout_flag; |
| 8750 | } |
| 8751 | |
| 8752 | timer_active = TRUE; |
| 8753 | return &timeout_flag; |
| 8754 | } |
Bram Moolenaar | 08210f8 | 2023-04-13 19:15:54 +0100 | [diff] [blame] | 8755 | # endif // PROF_NSEC |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 8756 | #endif // FEAT_RELTIME |