Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Implements starting jobs and controlling them. |
| 11 | */ |
| 12 | |
| 13 | #include "vim.h" |
| 14 | |
| 15 | #if defined(FEAT_JOB_CHANNEL) || defined(PROTO) |
| 16 | |
| 17 | #define FOR_ALL_JOBS(job) \ |
| 18 | for ((job) = first_job; (job) != NULL; (job) = (job)->jv_next) |
| 19 | |
| 20 | static int |
| 21 | handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo) |
| 22 | { |
| 23 | char_u *val = tv_get_string(item); |
| 24 | |
| 25 | opt->jo_set |= jo; |
| 26 | if (STRCMP(val, "nl") == 0) |
| 27 | *modep = MODE_NL; |
| 28 | else if (STRCMP(val, "raw") == 0) |
| 29 | *modep = MODE_RAW; |
| 30 | else if (STRCMP(val, "js") == 0) |
| 31 | *modep = MODE_JS; |
| 32 | else if (STRCMP(val, "json") == 0) |
| 33 | *modep = MODE_JSON; |
| 34 | else |
| 35 | { |
| 36 | semsg(_(e_invarg2), val); |
| 37 | return FAIL; |
| 38 | } |
| 39 | return OK; |
| 40 | } |
| 41 | |
| 42 | static int |
| 43 | handle_io(typval_T *item, ch_part_T part, jobopt_T *opt) |
| 44 | { |
| 45 | char_u *val = tv_get_string(item); |
| 46 | |
| 47 | opt->jo_set |= JO_OUT_IO << (part - PART_OUT); |
| 48 | if (STRCMP(val, "null") == 0) |
| 49 | opt->jo_io[part] = JIO_NULL; |
| 50 | else if (STRCMP(val, "pipe") == 0) |
| 51 | opt->jo_io[part] = JIO_PIPE; |
| 52 | else if (STRCMP(val, "file") == 0) |
| 53 | opt->jo_io[part] = JIO_FILE; |
| 54 | else if (STRCMP(val, "buffer") == 0) |
| 55 | opt->jo_io[part] = JIO_BUFFER; |
| 56 | else if (STRCMP(val, "out") == 0 && part == PART_ERR) |
| 57 | opt->jo_io[part] = JIO_OUT; |
| 58 | else |
| 59 | { |
| 60 | semsg(_(e_invarg2), val); |
| 61 | return FAIL; |
| 62 | } |
| 63 | return OK; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Clear a jobopt_T before using it. |
| 68 | */ |
| 69 | void |
| 70 | clear_job_options(jobopt_T *opt) |
| 71 | { |
| 72 | CLEAR_POINTER(opt); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Free any members of a jobopt_T. |
| 77 | */ |
| 78 | void |
| 79 | free_job_options(jobopt_T *opt) |
| 80 | { |
| 81 | if (opt->jo_callback.cb_partial != NULL) |
| 82 | partial_unref(opt->jo_callback.cb_partial); |
| 83 | else if (opt->jo_callback.cb_name != NULL) |
| 84 | func_unref(opt->jo_callback.cb_name); |
| 85 | if (opt->jo_out_cb.cb_partial != NULL) |
| 86 | partial_unref(opt->jo_out_cb.cb_partial); |
| 87 | else if (opt->jo_out_cb.cb_name != NULL) |
| 88 | func_unref(opt->jo_out_cb.cb_name); |
| 89 | if (opt->jo_err_cb.cb_partial != NULL) |
| 90 | partial_unref(opt->jo_err_cb.cb_partial); |
| 91 | else if (opt->jo_err_cb.cb_name != NULL) |
| 92 | func_unref(opt->jo_err_cb.cb_name); |
| 93 | if (opt->jo_close_cb.cb_partial != NULL) |
| 94 | partial_unref(opt->jo_close_cb.cb_partial); |
| 95 | else if (opt->jo_close_cb.cb_name != NULL) |
| 96 | func_unref(opt->jo_close_cb.cb_name); |
| 97 | if (opt->jo_exit_cb.cb_partial != NULL) |
| 98 | partial_unref(opt->jo_exit_cb.cb_partial); |
| 99 | else if (opt->jo_exit_cb.cb_name != NULL) |
| 100 | func_unref(opt->jo_exit_cb.cb_name); |
| 101 | if (opt->jo_env != NULL) |
| 102 | dict_unref(opt->jo_env); |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Get the PART_ number from the first character of an option name. |
| 107 | */ |
| 108 | static int |
| 109 | part_from_char(int c) |
| 110 | { |
| 111 | return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Get the option entries from the dict in "tv", parse them and put the result |
| 116 | * in "opt". |
| 117 | * Only accept JO_ options in "supported" and JO2_ options in "supported2". |
| 118 | * If an option value is invalid return FAIL. |
| 119 | */ |
| 120 | int |
| 121 | get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2) |
| 122 | { |
| 123 | typval_T *item; |
| 124 | char_u *val; |
| 125 | dict_T *dict; |
| 126 | int todo; |
| 127 | hashitem_T *hi; |
| 128 | ch_part_T part; |
| 129 | |
| 130 | if (tv->v_type == VAR_UNKNOWN) |
| 131 | return OK; |
| 132 | if (tv->v_type != VAR_DICT) |
| 133 | { |
| 134 | emsg(_(e_dictreq)); |
| 135 | return FAIL; |
| 136 | } |
| 137 | dict = tv->vval.v_dict; |
| 138 | if (dict == NULL) |
| 139 | return OK; |
| 140 | |
| 141 | todo = (int)dict->dv_hashtab.ht_used; |
| 142 | for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) |
| 143 | if (!HASHITEM_EMPTY(hi)) |
| 144 | { |
| 145 | item = &dict_lookup(hi)->di_tv; |
| 146 | |
| 147 | if (STRCMP(hi->hi_key, "mode") == 0) |
| 148 | { |
| 149 | if (!(supported & JO_MODE)) |
| 150 | break; |
| 151 | if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL) |
| 152 | return FAIL; |
| 153 | } |
| 154 | else if (STRCMP(hi->hi_key, "in_mode") == 0) |
| 155 | { |
| 156 | if (!(supported & JO_IN_MODE)) |
| 157 | break; |
| 158 | if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE) |
| 159 | == FAIL) |
| 160 | return FAIL; |
| 161 | } |
| 162 | else if (STRCMP(hi->hi_key, "out_mode") == 0) |
| 163 | { |
| 164 | if (!(supported & JO_OUT_MODE)) |
| 165 | break; |
| 166 | if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE) |
| 167 | == FAIL) |
| 168 | return FAIL; |
| 169 | } |
| 170 | else if (STRCMP(hi->hi_key, "err_mode") == 0) |
| 171 | { |
| 172 | if (!(supported & JO_ERR_MODE)) |
| 173 | break; |
| 174 | if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE) |
| 175 | == FAIL) |
| 176 | return FAIL; |
| 177 | } |
| 178 | else if (STRCMP(hi->hi_key, "noblock") == 0) |
| 179 | { |
| 180 | if (!(supported & JO_MODE)) |
| 181 | break; |
| 182 | opt->jo_noblock = tv_get_bool(item); |
| 183 | } |
| 184 | else if (STRCMP(hi->hi_key, "in_io") == 0 |
| 185 | || STRCMP(hi->hi_key, "out_io") == 0 |
| 186 | || STRCMP(hi->hi_key, "err_io") == 0) |
| 187 | { |
| 188 | if (!(supported & JO_OUT_IO)) |
| 189 | break; |
| 190 | if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL) |
| 191 | return FAIL; |
| 192 | } |
| 193 | else if (STRCMP(hi->hi_key, "in_name") == 0 |
| 194 | || STRCMP(hi->hi_key, "out_name") == 0 |
| 195 | || STRCMP(hi->hi_key, "err_name") == 0) |
| 196 | { |
| 197 | part = part_from_char(*hi->hi_key); |
| 198 | |
| 199 | if (!(supported & JO_OUT_IO)) |
| 200 | break; |
| 201 | opt->jo_set |= JO_OUT_NAME << (part - PART_OUT); |
| 202 | opt->jo_io_name[part] = tv_get_string_buf_chk(item, |
| 203 | opt->jo_io_name_buf[part]); |
| 204 | } |
| 205 | else if (STRCMP(hi->hi_key, "pty") == 0) |
| 206 | { |
| 207 | if (!(supported & JO_MODE)) |
| 208 | break; |
| 209 | opt->jo_pty = tv_get_bool(item); |
| 210 | } |
| 211 | else if (STRCMP(hi->hi_key, "in_buf") == 0 |
| 212 | || STRCMP(hi->hi_key, "out_buf") == 0 |
| 213 | || STRCMP(hi->hi_key, "err_buf") == 0) |
| 214 | { |
| 215 | part = part_from_char(*hi->hi_key); |
| 216 | |
| 217 | if (!(supported & JO_OUT_IO)) |
| 218 | break; |
| 219 | opt->jo_set |= JO_OUT_BUF << (part - PART_OUT); |
| 220 | opt->jo_io_buf[part] = tv_get_number(item); |
| 221 | if (opt->jo_io_buf[part] <= 0) |
| 222 | { |
| 223 | semsg(_(e_invargNval), hi->hi_key, tv_get_string(item)); |
| 224 | return FAIL; |
| 225 | } |
| 226 | if (buflist_findnr(opt->jo_io_buf[part]) == NULL) |
| 227 | { |
| 228 | semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]); |
| 229 | return FAIL; |
| 230 | } |
| 231 | } |
| 232 | else if (STRCMP(hi->hi_key, "out_modifiable") == 0 |
| 233 | || STRCMP(hi->hi_key, "err_modifiable") == 0) |
| 234 | { |
| 235 | part = part_from_char(*hi->hi_key); |
| 236 | |
| 237 | if (!(supported & JO_OUT_IO)) |
| 238 | break; |
| 239 | opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT); |
| 240 | opt->jo_modifiable[part] = tv_get_bool(item); |
| 241 | } |
| 242 | else if (STRCMP(hi->hi_key, "out_msg") == 0 |
| 243 | || STRCMP(hi->hi_key, "err_msg") == 0) |
| 244 | { |
| 245 | part = part_from_char(*hi->hi_key); |
| 246 | |
| 247 | if (!(supported & JO_OUT_IO)) |
| 248 | break; |
| 249 | opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT); |
| 250 | opt->jo_message[part] = tv_get_bool(item); |
| 251 | } |
| 252 | else if (STRCMP(hi->hi_key, "in_top") == 0 |
| 253 | || STRCMP(hi->hi_key, "in_bot") == 0) |
| 254 | { |
| 255 | linenr_T *lp; |
| 256 | |
| 257 | if (!(supported & JO_OUT_IO)) |
| 258 | break; |
| 259 | if (hi->hi_key[3] == 't') |
| 260 | { |
| 261 | lp = &opt->jo_in_top; |
| 262 | opt->jo_set |= JO_IN_TOP; |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | lp = &opt->jo_in_bot; |
| 267 | opt->jo_set |= JO_IN_BOT; |
| 268 | } |
| 269 | *lp = tv_get_number(item); |
| 270 | if (*lp < 0) |
| 271 | { |
| 272 | semsg(_(e_invargNval), hi->hi_key, tv_get_string(item)); |
| 273 | return FAIL; |
| 274 | } |
| 275 | } |
| 276 | else if (STRCMP(hi->hi_key, "channel") == 0) |
| 277 | { |
| 278 | if (!(supported & JO_OUT_IO)) |
| 279 | break; |
| 280 | opt->jo_set |= JO_CHANNEL; |
| 281 | if (item->v_type != VAR_CHANNEL) |
| 282 | { |
| 283 | semsg(_(e_invargval), "channel"); |
| 284 | return FAIL; |
| 285 | } |
| 286 | opt->jo_channel = item->vval.v_channel; |
| 287 | } |
| 288 | else if (STRCMP(hi->hi_key, "callback") == 0) |
| 289 | { |
| 290 | if (!(supported & JO_CALLBACK)) |
| 291 | break; |
| 292 | opt->jo_set |= JO_CALLBACK; |
| 293 | opt->jo_callback = get_callback(item); |
| 294 | if (opt->jo_callback.cb_name == NULL) |
| 295 | { |
| 296 | semsg(_(e_invargval), "callback"); |
| 297 | return FAIL; |
| 298 | } |
| 299 | } |
| 300 | else if (STRCMP(hi->hi_key, "out_cb") == 0) |
| 301 | { |
| 302 | if (!(supported & JO_OUT_CALLBACK)) |
| 303 | break; |
| 304 | opt->jo_set |= JO_OUT_CALLBACK; |
| 305 | opt->jo_out_cb = get_callback(item); |
| 306 | if (opt->jo_out_cb.cb_name == NULL) |
| 307 | { |
| 308 | semsg(_(e_invargval), "out_cb"); |
| 309 | return FAIL; |
| 310 | } |
| 311 | } |
| 312 | else if (STRCMP(hi->hi_key, "err_cb") == 0) |
| 313 | { |
| 314 | if (!(supported & JO_ERR_CALLBACK)) |
| 315 | break; |
| 316 | opt->jo_set |= JO_ERR_CALLBACK; |
| 317 | opt->jo_err_cb = get_callback(item); |
| 318 | if (opt->jo_err_cb.cb_name == NULL) |
| 319 | { |
| 320 | semsg(_(e_invargval), "err_cb"); |
| 321 | return FAIL; |
| 322 | } |
| 323 | } |
| 324 | else if (STRCMP(hi->hi_key, "close_cb") == 0) |
| 325 | { |
| 326 | if (!(supported & JO_CLOSE_CALLBACK)) |
| 327 | break; |
| 328 | opt->jo_set |= JO_CLOSE_CALLBACK; |
| 329 | opt->jo_close_cb = get_callback(item); |
| 330 | if (opt->jo_close_cb.cb_name == NULL) |
| 331 | { |
| 332 | semsg(_(e_invargval), "close_cb"); |
| 333 | return FAIL; |
| 334 | } |
| 335 | } |
| 336 | else if (STRCMP(hi->hi_key, "drop") == 0) |
| 337 | { |
| 338 | int never = FALSE; |
| 339 | val = tv_get_string(item); |
| 340 | |
| 341 | if (STRCMP(val, "never") == 0) |
| 342 | never = TRUE; |
| 343 | else if (STRCMP(val, "auto") != 0) |
| 344 | { |
| 345 | semsg(_(e_invargNval), "drop", val); |
| 346 | return FAIL; |
| 347 | } |
| 348 | opt->jo_drop_never = never; |
| 349 | } |
| 350 | else if (STRCMP(hi->hi_key, "exit_cb") == 0) |
| 351 | { |
| 352 | if (!(supported & JO_EXIT_CB)) |
| 353 | break; |
| 354 | opt->jo_set |= JO_EXIT_CB; |
| 355 | opt->jo_exit_cb = get_callback(item); |
| 356 | if (opt->jo_exit_cb.cb_name == NULL) |
| 357 | { |
| 358 | semsg(_(e_invargval), "exit_cb"); |
| 359 | return FAIL; |
| 360 | } |
| 361 | } |
| 362 | #ifdef FEAT_TERMINAL |
| 363 | else if (STRCMP(hi->hi_key, "term_name") == 0) |
| 364 | { |
| 365 | if (!(supported2 & JO2_TERM_NAME)) |
| 366 | break; |
| 367 | opt->jo_set2 |= JO2_TERM_NAME; |
| 368 | opt->jo_term_name = tv_get_string_buf_chk(item, |
| 369 | opt->jo_term_name_buf); |
| 370 | if (opt->jo_term_name == NULL) |
| 371 | { |
| 372 | semsg(_(e_invargval), "term_name"); |
| 373 | return FAIL; |
| 374 | } |
| 375 | } |
| 376 | else if (STRCMP(hi->hi_key, "term_finish") == 0) |
| 377 | { |
| 378 | if (!(supported2 & JO2_TERM_FINISH)) |
| 379 | break; |
| 380 | val = tv_get_string(item); |
| 381 | if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0) |
| 382 | { |
| 383 | semsg(_(e_invargNval), "term_finish", val); |
| 384 | return FAIL; |
| 385 | } |
| 386 | opt->jo_set2 |= JO2_TERM_FINISH; |
| 387 | opt->jo_term_finish = *val; |
| 388 | } |
| 389 | else if (STRCMP(hi->hi_key, "term_opencmd") == 0) |
| 390 | { |
| 391 | char_u *p; |
| 392 | |
| 393 | if (!(supported2 & JO2_TERM_OPENCMD)) |
| 394 | break; |
| 395 | opt->jo_set2 |= JO2_TERM_OPENCMD; |
| 396 | p = opt->jo_term_opencmd = tv_get_string_buf_chk(item, |
| 397 | opt->jo_term_opencmd_buf); |
| 398 | if (p != NULL) |
| 399 | { |
| 400 | // Must have %d and no other %. |
| 401 | p = vim_strchr(p, '%'); |
| 402 | if (p != NULL && (p[1] != 'd' |
| 403 | || vim_strchr(p + 2, '%') != NULL)) |
| 404 | p = NULL; |
| 405 | } |
| 406 | if (p == NULL) |
| 407 | { |
| 408 | semsg(_(e_invargval), "term_opencmd"); |
| 409 | return FAIL; |
| 410 | } |
| 411 | } |
| 412 | else if (STRCMP(hi->hi_key, "eof_chars") == 0) |
| 413 | { |
| 414 | if (!(supported2 & JO2_EOF_CHARS)) |
| 415 | break; |
| 416 | opt->jo_set2 |= JO2_EOF_CHARS; |
| 417 | opt->jo_eof_chars = tv_get_string_buf_chk(item, |
| 418 | opt->jo_eof_chars_buf); |
| 419 | if (opt->jo_eof_chars == NULL) |
| 420 | { |
| 421 | semsg(_(e_invargval), "eof_chars"); |
| 422 | return FAIL; |
| 423 | } |
| 424 | } |
| 425 | else if (STRCMP(hi->hi_key, "term_rows") == 0) |
| 426 | { |
| 427 | if (!(supported2 & JO2_TERM_ROWS)) |
| 428 | break; |
| 429 | opt->jo_set2 |= JO2_TERM_ROWS; |
| 430 | opt->jo_term_rows = tv_get_number(item); |
| 431 | } |
| 432 | else if (STRCMP(hi->hi_key, "term_cols") == 0) |
| 433 | { |
| 434 | if (!(supported2 & JO2_TERM_COLS)) |
| 435 | break; |
| 436 | opt->jo_set2 |= JO2_TERM_COLS; |
| 437 | opt->jo_term_cols = tv_get_number(item); |
| 438 | } |
| 439 | else if (STRCMP(hi->hi_key, "vertical") == 0) |
| 440 | { |
| 441 | if (!(supported2 & JO2_VERTICAL)) |
| 442 | break; |
| 443 | opt->jo_set2 |= JO2_VERTICAL; |
| 444 | opt->jo_vertical = tv_get_bool(item); |
| 445 | } |
| 446 | else if (STRCMP(hi->hi_key, "curwin") == 0) |
| 447 | { |
| 448 | if (!(supported2 & JO2_CURWIN)) |
| 449 | break; |
| 450 | opt->jo_set2 |= JO2_CURWIN; |
Bram Moolenaar | ad30470 | 2020-09-06 18:22:53 +0200 | [diff] [blame] | 451 | opt->jo_curwin = tv_get_bool(item); |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 452 | } |
| 453 | else if (STRCMP(hi->hi_key, "bufnr") == 0) |
| 454 | { |
| 455 | int nr; |
| 456 | |
| 457 | if (!(supported2 & JO2_CURWIN)) |
| 458 | break; |
| 459 | opt->jo_set2 |= JO2_BUFNR; |
| 460 | nr = tv_get_number(item); |
| 461 | if (nr <= 0) |
| 462 | { |
| 463 | semsg(_(e_invargNval), hi->hi_key, tv_get_string(item)); |
| 464 | return FAIL; |
| 465 | } |
| 466 | opt->jo_bufnr_buf = buflist_findnr(nr); |
| 467 | if (opt->jo_bufnr_buf == NULL) |
| 468 | { |
| 469 | semsg(_(e_nobufnr), (long)nr); |
| 470 | return FAIL; |
| 471 | } |
| 472 | if (opt->jo_bufnr_buf->b_nwindows == 0 |
| 473 | || opt->jo_bufnr_buf->b_term == NULL) |
| 474 | { |
| 475 | semsg(_(e_invarg2), "bufnr"); |
| 476 | return FAIL; |
| 477 | } |
| 478 | } |
| 479 | else if (STRCMP(hi->hi_key, "hidden") == 0) |
| 480 | { |
| 481 | if (!(supported2 & JO2_HIDDEN)) |
| 482 | break; |
| 483 | opt->jo_set2 |= JO2_HIDDEN; |
| 484 | opt->jo_hidden = tv_get_bool(item); |
| 485 | } |
| 486 | else if (STRCMP(hi->hi_key, "norestore") == 0) |
| 487 | { |
| 488 | if (!(supported2 & JO2_NORESTORE)) |
| 489 | break; |
| 490 | opt->jo_set2 |= JO2_NORESTORE; |
| 491 | opt->jo_term_norestore = tv_get_bool(item); |
| 492 | } |
| 493 | else if (STRCMP(hi->hi_key, "term_kill") == 0) |
| 494 | { |
| 495 | if (!(supported2 & JO2_TERM_KILL)) |
| 496 | break; |
| 497 | opt->jo_set2 |= JO2_TERM_KILL; |
| 498 | opt->jo_term_kill = tv_get_string_buf_chk(item, |
| 499 | opt->jo_term_kill_buf); |
| 500 | if (opt->jo_term_kill == NULL) |
| 501 | { |
| 502 | semsg(_(e_invargval), "term_kill"); |
| 503 | return FAIL; |
| 504 | } |
| 505 | } |
| 506 | else if (STRCMP(hi->hi_key, "tty_type") == 0) |
| 507 | { |
| 508 | char_u *p; |
| 509 | |
| 510 | if (!(supported2 & JO2_TTY_TYPE)) |
| 511 | break; |
| 512 | opt->jo_set2 |= JO2_TTY_TYPE; |
| 513 | p = tv_get_string_chk(item); |
| 514 | if (p == NULL) |
| 515 | { |
| 516 | semsg(_(e_invargval), "tty_type"); |
| 517 | return FAIL; |
| 518 | } |
| 519 | // Allow empty string, "winpty", "conpty". |
| 520 | if (!(*p == NUL || STRCMP(p, "winpty") == 0 |
| 521 | || STRCMP(p, "conpty") == 0)) |
| 522 | { |
| 523 | semsg(_(e_invargval), "tty_type"); |
| 524 | return FAIL; |
| 525 | } |
| 526 | opt->jo_tty_type = p[0]; |
| 527 | } |
| 528 | # if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) |
| 529 | else if (STRCMP(hi->hi_key, "ansi_colors") == 0) |
| 530 | { |
| 531 | int n = 0; |
| 532 | listitem_T *li; |
| 533 | long_u rgb[16]; |
| 534 | |
| 535 | if (!(supported2 & JO2_ANSI_COLORS)) |
| 536 | break; |
| 537 | |
| 538 | if (item == NULL || item->v_type != VAR_LIST |
| 539 | || item->vval.v_list == NULL) |
| 540 | { |
| 541 | semsg(_(e_invargval), "ansi_colors"); |
| 542 | return FAIL; |
| 543 | } |
| 544 | |
| 545 | CHECK_LIST_MATERIALIZE(item->vval.v_list); |
| 546 | li = item->vval.v_list->lv_first; |
| 547 | for (; li != NULL && n < 16; li = li->li_next, n++) |
| 548 | { |
| 549 | char_u *color_name; |
| 550 | guicolor_T guicolor; |
| 551 | int called_emsg_before = called_emsg; |
| 552 | |
| 553 | color_name = tv_get_string_chk(&li->li_tv); |
| 554 | if (color_name == NULL) |
| 555 | return FAIL; |
| 556 | |
| 557 | guicolor = GUI_GET_COLOR(color_name); |
| 558 | if (guicolor == INVALCOLOR) |
| 559 | { |
| 560 | if (called_emsg_before == called_emsg) |
| 561 | // may not get the error if the GUI didn't start |
| 562 | semsg(_(e_alloc_color), color_name); |
| 563 | return FAIL; |
| 564 | } |
| 565 | |
| 566 | rgb[n] = GUI_MCH_GET_RGB(guicolor); |
| 567 | } |
| 568 | |
| 569 | if (n != 16 || li != NULL) |
| 570 | { |
| 571 | semsg(_(e_invargval), "ansi_colors"); |
| 572 | return FAIL; |
| 573 | } |
| 574 | |
| 575 | opt->jo_set2 |= JO2_ANSI_COLORS; |
| 576 | memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb)); |
| 577 | } |
| 578 | # endif |
| 579 | else if (STRCMP(hi->hi_key, "term_highlight") == 0) |
| 580 | { |
| 581 | char_u *p; |
| 582 | |
| 583 | if (!(supported2 & JO2_TERM_HIGHLIGHT)) |
| 584 | break; |
| 585 | opt->jo_set2 |= JO2_TERM_HIGHLIGHT; |
| 586 | p = tv_get_string_buf_chk(item, opt->jo_term_highlight_buf); |
| 587 | if (p == NULL || *p == NUL) |
| 588 | { |
| 589 | semsg(_(e_invargval), "term_highlight"); |
| 590 | return FAIL; |
| 591 | } |
| 592 | opt->jo_term_highlight = p; |
| 593 | } |
| 594 | else if (STRCMP(hi->hi_key, "term_api") == 0) |
| 595 | { |
| 596 | if (!(supported2 & JO2_TERM_API)) |
| 597 | break; |
| 598 | opt->jo_set2 |= JO2_TERM_API; |
| 599 | opt->jo_term_api = tv_get_string_buf_chk(item, |
| 600 | opt->jo_term_api_buf); |
| 601 | if (opt->jo_term_api == NULL) |
| 602 | { |
| 603 | semsg(_(e_invargval), "term_api"); |
| 604 | return FAIL; |
| 605 | } |
| 606 | } |
| 607 | #endif |
| 608 | else if (STRCMP(hi->hi_key, "env") == 0) |
| 609 | { |
| 610 | if (!(supported2 & JO2_ENV)) |
| 611 | break; |
| 612 | if (item->v_type != VAR_DICT) |
| 613 | { |
| 614 | semsg(_(e_invargval), "env"); |
| 615 | return FAIL; |
| 616 | } |
| 617 | opt->jo_set2 |= JO2_ENV; |
| 618 | opt->jo_env = item->vval.v_dict; |
| 619 | if (opt->jo_env != NULL) |
| 620 | ++opt->jo_env->dv_refcount; |
| 621 | } |
| 622 | else if (STRCMP(hi->hi_key, "cwd") == 0) |
| 623 | { |
| 624 | if (!(supported2 & JO2_CWD)) |
| 625 | break; |
| 626 | opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf); |
| 627 | if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd) |
| 628 | #ifndef MSWIN // Win32 directories don't have the concept of "executable" |
| 629 | || mch_access((char *)opt->jo_cwd, X_OK) != 0 |
| 630 | #endif |
| 631 | ) |
| 632 | { |
| 633 | semsg(_(e_invargval), "cwd"); |
| 634 | return FAIL; |
| 635 | } |
| 636 | opt->jo_set2 |= JO2_CWD; |
| 637 | } |
| 638 | else if (STRCMP(hi->hi_key, "waittime") == 0) |
| 639 | { |
| 640 | if (!(supported & JO_WAITTIME)) |
| 641 | break; |
| 642 | opt->jo_set |= JO_WAITTIME; |
| 643 | opt->jo_waittime = tv_get_number(item); |
| 644 | } |
| 645 | else if (STRCMP(hi->hi_key, "timeout") == 0) |
| 646 | { |
| 647 | if (!(supported & JO_TIMEOUT)) |
| 648 | break; |
| 649 | opt->jo_set |= JO_TIMEOUT; |
| 650 | opt->jo_timeout = tv_get_number(item); |
| 651 | } |
| 652 | else if (STRCMP(hi->hi_key, "out_timeout") == 0) |
| 653 | { |
| 654 | if (!(supported & JO_OUT_TIMEOUT)) |
| 655 | break; |
| 656 | opt->jo_set |= JO_OUT_TIMEOUT; |
| 657 | opt->jo_out_timeout = tv_get_number(item); |
| 658 | } |
| 659 | else if (STRCMP(hi->hi_key, "err_timeout") == 0) |
| 660 | { |
| 661 | if (!(supported & JO_ERR_TIMEOUT)) |
| 662 | break; |
| 663 | opt->jo_set |= JO_ERR_TIMEOUT; |
| 664 | opt->jo_err_timeout = tv_get_number(item); |
| 665 | } |
| 666 | else if (STRCMP(hi->hi_key, "part") == 0) |
| 667 | { |
| 668 | if (!(supported & JO_PART)) |
| 669 | break; |
| 670 | opt->jo_set |= JO_PART; |
| 671 | val = tv_get_string(item); |
| 672 | if (STRCMP(val, "err") == 0) |
| 673 | opt->jo_part = PART_ERR; |
| 674 | else if (STRCMP(val, "out") == 0) |
| 675 | opt->jo_part = PART_OUT; |
| 676 | else |
| 677 | { |
| 678 | semsg(_(e_invargNval), "part", val); |
| 679 | return FAIL; |
| 680 | } |
| 681 | } |
| 682 | else if (STRCMP(hi->hi_key, "id") == 0) |
| 683 | { |
| 684 | if (!(supported & JO_ID)) |
| 685 | break; |
| 686 | opt->jo_set |= JO_ID; |
| 687 | opt->jo_id = tv_get_number(item); |
| 688 | } |
| 689 | else if (STRCMP(hi->hi_key, "stoponexit") == 0) |
| 690 | { |
| 691 | if (!(supported & JO_STOPONEXIT)) |
| 692 | break; |
| 693 | opt->jo_set |= JO_STOPONEXIT; |
| 694 | opt->jo_stoponexit = tv_get_string_buf_chk(item, |
| 695 | opt->jo_stoponexit_buf); |
| 696 | if (opt->jo_stoponexit == NULL) |
| 697 | { |
| 698 | semsg(_(e_invargval), "stoponexit"); |
| 699 | return FAIL; |
| 700 | } |
| 701 | } |
| 702 | else if (STRCMP(hi->hi_key, "block_write") == 0) |
| 703 | { |
| 704 | if (!(supported & JO_BLOCK_WRITE)) |
| 705 | break; |
| 706 | opt->jo_set |= JO_BLOCK_WRITE; |
| 707 | opt->jo_block_write = tv_get_number(item); |
| 708 | } |
| 709 | else |
| 710 | break; |
| 711 | --todo; |
| 712 | } |
| 713 | if (todo > 0) |
| 714 | { |
| 715 | semsg(_(e_invarg2), hi->hi_key); |
| 716 | return FAIL; |
| 717 | } |
| 718 | |
| 719 | return OK; |
| 720 | } |
| 721 | |
| 722 | static job_T *first_job = NULL; |
| 723 | |
| 724 | static void |
| 725 | job_free_contents(job_T *job) |
| 726 | { |
| 727 | int i; |
| 728 | |
| 729 | ch_log(job->jv_channel, "Freeing job"); |
| 730 | if (job->jv_channel != NULL) |
| 731 | { |
| 732 | // The link from the channel to the job doesn't count as a reference, |
| 733 | // thus don't decrement the refcount of the job. The reference from |
| 734 | // the job to the channel does count the reference, decrement it and |
| 735 | // NULL the reference. We don't set ch_job_killed, unreferencing the |
| 736 | // job doesn't mean it stops running. |
| 737 | job->jv_channel->ch_job = NULL; |
| 738 | channel_unref(job->jv_channel); |
| 739 | } |
| 740 | mch_clear_job(job); |
| 741 | |
| 742 | vim_free(job->jv_tty_in); |
| 743 | vim_free(job->jv_tty_out); |
| 744 | vim_free(job->jv_stoponexit); |
| 745 | #ifdef UNIX |
| 746 | vim_free(job->jv_termsig); |
| 747 | #endif |
| 748 | #ifdef MSWIN |
| 749 | vim_free(job->jv_tty_type); |
| 750 | #endif |
| 751 | free_callback(&job->jv_exit_cb); |
| 752 | if (job->jv_argv != NULL) |
| 753 | { |
| 754 | for (i = 0; job->jv_argv[i] != NULL; i++) |
| 755 | vim_free(job->jv_argv[i]); |
| 756 | vim_free(job->jv_argv); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * Remove "job" from the list of jobs. |
| 762 | */ |
| 763 | static void |
| 764 | job_unlink(job_T *job) |
| 765 | { |
| 766 | if (job->jv_next != NULL) |
| 767 | job->jv_next->jv_prev = job->jv_prev; |
| 768 | if (job->jv_prev == NULL) |
| 769 | first_job = job->jv_next; |
| 770 | else |
| 771 | job->jv_prev->jv_next = job->jv_next; |
| 772 | } |
| 773 | |
| 774 | static void |
| 775 | job_free_job(job_T *job) |
| 776 | { |
| 777 | job_unlink(job); |
| 778 | vim_free(job); |
| 779 | } |
| 780 | |
| 781 | static void |
| 782 | job_free(job_T *job) |
| 783 | { |
| 784 | if (!in_free_unref_items) |
| 785 | { |
| 786 | job_free_contents(job); |
| 787 | job_free_job(job); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | static job_T *jobs_to_free = NULL; |
| 792 | |
| 793 | /* |
| 794 | * Put "job" in a list to be freed later, when it's no longer referenced. |
| 795 | */ |
| 796 | static void |
| 797 | job_free_later(job_T *job) |
| 798 | { |
| 799 | job_unlink(job); |
| 800 | job->jv_next = jobs_to_free; |
| 801 | jobs_to_free = job; |
| 802 | } |
| 803 | |
| 804 | static void |
| 805 | free_jobs_to_free_later(void) |
| 806 | { |
| 807 | job_T *job; |
| 808 | |
| 809 | while (jobs_to_free != NULL) |
| 810 | { |
| 811 | job = jobs_to_free; |
| 812 | jobs_to_free = job->jv_next; |
| 813 | job_free_contents(job); |
| 814 | vim_free(job); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | #if defined(EXITFREE) || defined(PROTO) |
| 819 | void |
| 820 | job_free_all(void) |
| 821 | { |
| 822 | while (first_job != NULL) |
| 823 | job_free(first_job); |
| 824 | free_jobs_to_free_later(); |
| 825 | |
| 826 | # ifdef FEAT_TERMINAL |
| 827 | free_unused_terminals(); |
| 828 | # endif |
| 829 | } |
| 830 | #endif |
| 831 | |
| 832 | /* |
| 833 | * Return TRUE if we need to check if the process of "job" has ended. |
| 834 | */ |
| 835 | static int |
| 836 | job_need_end_check(job_T *job) |
| 837 | { |
| 838 | return job->jv_status == JOB_STARTED |
| 839 | && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL); |
| 840 | } |
| 841 | |
| 842 | /* |
| 843 | * Return TRUE if the channel of "job" is still useful. |
| 844 | */ |
| 845 | static int |
| 846 | job_channel_still_useful(job_T *job) |
| 847 | { |
| 848 | return job->jv_channel != NULL && channel_still_useful(job->jv_channel); |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Return TRUE if the channel of "job" is closeable. |
| 853 | */ |
| 854 | static int |
| 855 | job_channel_can_close(job_T *job) |
| 856 | { |
| 857 | return job->jv_channel != NULL && channel_can_close(job->jv_channel); |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * Return TRUE if the job should not be freed yet. Do not free the job when |
| 862 | * it has not ended yet and there is a "stoponexit" flag, an exit callback |
| 863 | * or when the associated channel will do something with the job output. |
| 864 | */ |
| 865 | static int |
| 866 | job_still_useful(job_T *job) |
| 867 | { |
| 868 | return job_need_end_check(job) || job_channel_still_useful(job); |
| 869 | } |
| 870 | |
| 871 | #if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO) |
| 872 | /* |
| 873 | * Return TRUE when there is any running job that we care about. |
| 874 | */ |
| 875 | int |
| 876 | job_any_running() |
| 877 | { |
| 878 | job_T *job; |
| 879 | |
| 880 | FOR_ALL_JOBS(job) |
| 881 | if (job_still_useful(job)) |
| 882 | { |
| 883 | ch_log(NULL, "GUI not forking because a job is running"); |
| 884 | return TRUE; |
| 885 | } |
| 886 | return FALSE; |
| 887 | } |
| 888 | #endif |
| 889 | |
Bram Moolenaar | f46bf52 | 2020-12-09 13:16:13 +0100 | [diff] [blame] | 890 | // Unix uses argv[] for the command, other systems use a string. |
| 891 | #if defined(UNIX) |
| 892 | # define USE_ARGV |
| 893 | #endif |
| 894 | |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 895 | #if !defined(USE_ARGV) || defined(PROTO) |
| 896 | /* |
| 897 | * Escape one argument for an external command. |
| 898 | * Returns the escaped string in allocated memory. NULL when out of memory. |
| 899 | */ |
| 900 | static char_u * |
| 901 | win32_escape_arg(char_u *arg) |
| 902 | { |
| 903 | int slen, dlen; |
| 904 | int escaping = 0; |
| 905 | int i; |
| 906 | char_u *s, *d; |
| 907 | char_u *escaped_arg; |
| 908 | int has_spaces = FALSE; |
| 909 | |
| 910 | // First count the number of extra bytes required. |
| 911 | slen = (int)STRLEN(arg); |
| 912 | dlen = slen; |
| 913 | for (s = arg; *s != NUL; MB_PTR_ADV(s)) |
| 914 | { |
| 915 | if (*s == '"' || *s == '\\') |
| 916 | ++dlen; |
| 917 | if (*s == ' ' || *s == '\t') |
| 918 | has_spaces = TRUE; |
| 919 | } |
| 920 | |
| 921 | if (has_spaces) |
| 922 | dlen += 2; |
| 923 | |
| 924 | if (dlen == slen) |
| 925 | return vim_strsave(arg); |
| 926 | |
| 927 | // Allocate memory for the result and fill it. |
| 928 | escaped_arg = alloc(dlen + 1); |
| 929 | if (escaped_arg == NULL) |
| 930 | return NULL; |
| 931 | memset(escaped_arg, 0, dlen+1); |
| 932 | |
| 933 | d = escaped_arg; |
| 934 | |
| 935 | if (has_spaces) |
| 936 | *d++ = '"'; |
| 937 | |
| 938 | for (s = arg; *s != NUL;) |
| 939 | { |
| 940 | switch (*s) |
| 941 | { |
| 942 | case '"': |
| 943 | for (i = 0; i < escaping; i++) |
| 944 | *d++ = '\\'; |
| 945 | escaping = 0; |
| 946 | *d++ = '\\'; |
| 947 | *d++ = *s++; |
| 948 | break; |
| 949 | case '\\': |
| 950 | escaping++; |
| 951 | *d++ = *s++; |
| 952 | break; |
| 953 | default: |
| 954 | escaping = 0; |
| 955 | MB_COPY_CHAR(s, d); |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | // add terminating quote and finish with a NUL |
| 961 | if (has_spaces) |
| 962 | { |
| 963 | for (i = 0; i < escaping; i++) |
| 964 | *d++ = '\\'; |
| 965 | *d++ = '"'; |
| 966 | } |
| 967 | *d = NUL; |
| 968 | |
| 969 | return escaped_arg; |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * Build a command line from a list, taking care of escaping. |
| 974 | * The result is put in gap->ga_data. |
| 975 | * Returns FAIL when out of memory. |
| 976 | */ |
| 977 | int |
| 978 | win32_build_cmd(list_T *l, garray_T *gap) |
| 979 | { |
| 980 | listitem_T *li; |
| 981 | char_u *s; |
| 982 | |
| 983 | CHECK_LIST_MATERIALIZE(l); |
| 984 | FOR_ALL_LIST_ITEMS(l, li) |
| 985 | { |
| 986 | s = tv_get_string_chk(&li->li_tv); |
| 987 | if (s == NULL) |
| 988 | return FAIL; |
| 989 | s = win32_escape_arg(s); |
| 990 | if (s == NULL) |
| 991 | return FAIL; |
| 992 | ga_concat(gap, s); |
| 993 | vim_free(s); |
| 994 | if (li->li_next != NULL) |
| 995 | ga_append(gap, ' '); |
| 996 | } |
| 997 | return OK; |
| 998 | } |
| 999 | #endif |
| 1000 | |
| 1001 | /* |
| 1002 | * NOTE: Must call job_cleanup() only once right after the status of "job" |
| 1003 | * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or |
| 1004 | * mch_detect_ended_job() returned non-NULL). |
| 1005 | * If the job is no longer used it will be removed from the list of jobs, and |
| 1006 | * deleted a bit later. |
| 1007 | */ |
| 1008 | void |
| 1009 | job_cleanup(job_T *job) |
| 1010 | { |
| 1011 | if (job->jv_status != JOB_ENDED) |
| 1012 | return; |
| 1013 | |
| 1014 | // Ready to cleanup the job. |
| 1015 | job->jv_status = JOB_FINISHED; |
| 1016 | |
| 1017 | // When only channel-in is kept open, close explicitly. |
| 1018 | if (job->jv_channel != NULL) |
| 1019 | ch_close_part(job->jv_channel, PART_IN); |
| 1020 | |
| 1021 | if (job->jv_exit_cb.cb_name != NULL) |
| 1022 | { |
| 1023 | typval_T argv[3]; |
| 1024 | typval_T rettv; |
| 1025 | |
| 1026 | // Invoke the exit callback. Make sure the refcount is > 0. |
| 1027 | ch_log(job->jv_channel, "Invoking exit callback %s", |
| 1028 | job->jv_exit_cb.cb_name); |
| 1029 | ++job->jv_refcount; |
| 1030 | argv[0].v_type = VAR_JOB; |
| 1031 | argv[0].vval.v_job = job; |
| 1032 | argv[1].v_type = VAR_NUMBER; |
| 1033 | argv[1].vval.v_number = job->jv_exitval; |
| 1034 | call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv); |
| 1035 | clear_tv(&rettv); |
| 1036 | --job->jv_refcount; |
| 1037 | channel_need_redraw = TRUE; |
| 1038 | } |
| 1039 | |
| 1040 | if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe) |
| 1041 | job->jv_channel->ch_killing = TRUE; |
| 1042 | |
| 1043 | // Do not free the job in case the close callback of the associated channel |
| 1044 | // isn't invoked yet and may get information by job_info(). |
| 1045 | if (job->jv_refcount == 0 && !job_channel_still_useful(job)) |
| 1046 | // The job was already unreferenced and the associated channel was |
| 1047 | // detached, now that it ended it can be freed. However, a caller might |
| 1048 | // still use it, thus free it a bit later. |
| 1049 | job_free_later(job); |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * Mark references in jobs that are still useful. |
| 1054 | */ |
| 1055 | int |
| 1056 | set_ref_in_job(int copyID) |
| 1057 | { |
| 1058 | int abort = FALSE; |
| 1059 | job_T *job; |
| 1060 | typval_T tv; |
| 1061 | |
| 1062 | for (job = first_job; !abort && job != NULL; job = job->jv_next) |
| 1063 | if (job_still_useful(job)) |
| 1064 | { |
| 1065 | tv.v_type = VAR_JOB; |
| 1066 | tv.vval.v_job = job; |
| 1067 | abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL); |
| 1068 | } |
| 1069 | return abort; |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * Dereference "job". Note that after this "job" may have been freed. |
| 1074 | */ |
| 1075 | void |
| 1076 | job_unref(job_T *job) |
| 1077 | { |
| 1078 | if (job != NULL && --job->jv_refcount <= 0) |
| 1079 | { |
| 1080 | // Do not free the job if there is a channel where the close callback |
| 1081 | // may get the job info. |
| 1082 | if (!job_channel_still_useful(job)) |
| 1083 | { |
| 1084 | // Do not free the job when it has not ended yet and there is a |
| 1085 | // "stoponexit" flag or an exit callback. |
| 1086 | if (!job_need_end_check(job)) |
| 1087 | { |
| 1088 | job_free(job); |
| 1089 | } |
| 1090 | else if (job->jv_channel != NULL) |
| 1091 | { |
| 1092 | // Do remove the link to the channel, otherwise it hangs |
| 1093 | // around until Vim exits. See job_free() for refcount. |
| 1094 | ch_log(job->jv_channel, "detaching channel from job"); |
| 1095 | job->jv_channel->ch_job = NULL; |
| 1096 | channel_unref(job->jv_channel); |
| 1097 | job->jv_channel = NULL; |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | int |
| 1104 | free_unused_jobs_contents(int copyID, int mask) |
| 1105 | { |
| 1106 | int did_free = FALSE; |
| 1107 | job_T *job; |
| 1108 | |
| 1109 | FOR_ALL_JOBS(job) |
| 1110 | if ((job->jv_copyID & mask) != (copyID & mask) |
| 1111 | && !job_still_useful(job)) |
| 1112 | { |
| 1113 | // Free the channel and ordinary items it contains, but don't |
| 1114 | // recurse into Lists, Dictionaries etc. |
| 1115 | job_free_contents(job); |
| 1116 | did_free = TRUE; |
| 1117 | } |
| 1118 | return did_free; |
| 1119 | } |
| 1120 | |
| 1121 | void |
| 1122 | free_unused_jobs(int copyID, int mask) |
| 1123 | { |
| 1124 | job_T *job; |
| 1125 | job_T *job_next; |
| 1126 | |
| 1127 | for (job = first_job; job != NULL; job = job_next) |
| 1128 | { |
| 1129 | job_next = job->jv_next; |
| 1130 | if ((job->jv_copyID & mask) != (copyID & mask) |
| 1131 | && !job_still_useful(job)) |
| 1132 | { |
| 1133 | // Free the job struct itself. |
| 1134 | job_free_job(job); |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | * Allocate a job. Sets the refcount to one and sets options default. |
| 1141 | */ |
| 1142 | job_T * |
| 1143 | job_alloc(void) |
| 1144 | { |
| 1145 | job_T *job; |
| 1146 | |
| 1147 | job = ALLOC_CLEAR_ONE(job_T); |
| 1148 | if (job != NULL) |
| 1149 | { |
| 1150 | job->jv_refcount = 1; |
| 1151 | job->jv_stoponexit = vim_strsave((char_u *)"term"); |
| 1152 | |
| 1153 | if (first_job != NULL) |
| 1154 | { |
| 1155 | first_job->jv_prev = job; |
| 1156 | job->jv_next = first_job; |
| 1157 | } |
| 1158 | first_job = job; |
| 1159 | } |
| 1160 | return job; |
| 1161 | } |
| 1162 | |
| 1163 | void |
| 1164 | job_set_options(job_T *job, jobopt_T *opt) |
| 1165 | { |
| 1166 | if (opt->jo_set & JO_STOPONEXIT) |
| 1167 | { |
| 1168 | vim_free(job->jv_stoponexit); |
| 1169 | if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL) |
| 1170 | job->jv_stoponexit = NULL; |
| 1171 | else |
| 1172 | job->jv_stoponexit = vim_strsave(opt->jo_stoponexit); |
| 1173 | } |
| 1174 | if (opt->jo_set & JO_EXIT_CB) |
| 1175 | { |
| 1176 | free_callback(&job->jv_exit_cb); |
| 1177 | if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL) |
| 1178 | { |
| 1179 | job->jv_exit_cb.cb_name = NULL; |
| 1180 | job->jv_exit_cb.cb_partial = NULL; |
| 1181 | } |
| 1182 | else |
| 1183 | copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb); |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag. |
| 1189 | */ |
| 1190 | void |
| 1191 | job_stop_on_exit(void) |
| 1192 | { |
| 1193 | job_T *job; |
| 1194 | |
| 1195 | FOR_ALL_JOBS(job) |
| 1196 | if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL) |
| 1197 | mch_signal_job(job, job->jv_stoponexit); |
| 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * Return TRUE when there is any job that has an exit callback and might exit, |
| 1202 | * which means job_check_ended() should be called more often. |
| 1203 | */ |
| 1204 | int |
| 1205 | has_pending_job(void) |
| 1206 | { |
| 1207 | job_T *job; |
| 1208 | |
| 1209 | FOR_ALL_JOBS(job) |
| 1210 | // Only should check if the channel has been closed, if the channel is |
| 1211 | // open the job won't exit. |
| 1212 | if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job)) |
| 1213 | || (job->jv_status == JOB_FINISHED |
| 1214 | && job_channel_can_close(job))) |
| 1215 | return TRUE; |
| 1216 | return FALSE; |
| 1217 | } |
| 1218 | |
| 1219 | #define MAX_CHECK_ENDED 8 |
| 1220 | |
| 1221 | /* |
| 1222 | * Called once in a while: check if any jobs that seem useful have ended. |
| 1223 | * Returns TRUE if a job did end. |
| 1224 | */ |
| 1225 | int |
| 1226 | job_check_ended(void) |
| 1227 | { |
| 1228 | int i; |
| 1229 | int did_end = FALSE; |
| 1230 | |
| 1231 | // be quick if there are no jobs to check |
| 1232 | if (first_job == NULL) |
| 1233 | return did_end; |
| 1234 | |
| 1235 | for (i = 0; i < MAX_CHECK_ENDED; ++i) |
| 1236 | { |
| 1237 | // NOTE: mch_detect_ended_job() must only return a job of which the |
| 1238 | // status was just set to JOB_ENDED. |
| 1239 | job_T *job = mch_detect_ended_job(first_job); |
| 1240 | |
| 1241 | if (job == NULL) |
| 1242 | break; |
| 1243 | did_end = TRUE; |
| 1244 | job_cleanup(job); // may add "job" to jobs_to_free |
| 1245 | } |
| 1246 | |
| 1247 | // Actually free jobs that were cleaned up. |
| 1248 | free_jobs_to_free_later(); |
| 1249 | |
| 1250 | if (channel_need_redraw) |
| 1251 | { |
| 1252 | channel_need_redraw = FALSE; |
| 1253 | redraw_after_callback(TRUE); |
| 1254 | } |
| 1255 | return did_end; |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * Create a job and return it. Implements job_start(). |
| 1260 | * "argv_arg" is only for Unix. |
| 1261 | * When "argv_arg" is NULL then "argvars" is used. |
| 1262 | * The returned job has a refcount of one. |
| 1263 | * Returns NULL when out of memory. |
| 1264 | */ |
| 1265 | job_T * |
| 1266 | job_start( |
| 1267 | typval_T *argvars, |
| 1268 | char **argv_arg UNUSED, |
| 1269 | jobopt_T *opt_arg, |
| 1270 | job_T **term_job) |
| 1271 | { |
| 1272 | job_T *job; |
| 1273 | char_u *cmd = NULL; |
| 1274 | char **argv = NULL; |
| 1275 | int argc = 0; |
| 1276 | int i; |
Bram Moolenaar | f46bf52 | 2020-12-09 13:16:13 +0100 | [diff] [blame] | 1277 | #ifndef USE_ARGV |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1278 | garray_T ga; |
| 1279 | #endif |
| 1280 | jobopt_T opt; |
| 1281 | ch_part_T part; |
| 1282 | |
| 1283 | job = job_alloc(); |
| 1284 | if (job == NULL) |
| 1285 | return NULL; |
| 1286 | |
| 1287 | job->jv_status = JOB_FAILED; |
| 1288 | #ifndef USE_ARGV |
| 1289 | ga_init2(&ga, (int)sizeof(char*), 20); |
| 1290 | #endif |
| 1291 | |
| 1292 | if (opt_arg != NULL) |
| 1293 | opt = *opt_arg; |
| 1294 | else |
| 1295 | { |
| 1296 | // Default mode is NL. |
| 1297 | clear_job_options(&opt); |
| 1298 | opt.jo_mode = MODE_NL; |
| 1299 | if (get_job_options(&argvars[1], &opt, |
| 1300 | JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT |
| 1301 | + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE, |
| 1302 | JO2_ENV + JO2_CWD) == FAIL) |
| 1303 | goto theend; |
| 1304 | } |
| 1305 | |
| 1306 | // Check that when io is "file" that there is a file name. |
| 1307 | for (part = PART_OUT; part < PART_COUNT; ++part) |
| 1308 | if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT))) |
| 1309 | && opt.jo_io[part] == JIO_FILE |
| 1310 | && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT))) |
| 1311 | || *opt.jo_io_name[part] == NUL)) |
| 1312 | { |
| 1313 | emsg(_("E920: _io file requires _name to be set")); |
| 1314 | goto theend; |
| 1315 | } |
| 1316 | |
| 1317 | if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER) |
| 1318 | { |
| 1319 | buf_T *buf = NULL; |
| 1320 | |
| 1321 | // check that we can find the buffer before starting the job |
| 1322 | if (opt.jo_set & JO_IN_BUF) |
| 1323 | { |
| 1324 | buf = buflist_findnr(opt.jo_io_buf[PART_IN]); |
| 1325 | if (buf == NULL) |
| 1326 | semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]); |
| 1327 | } |
| 1328 | else if (!(opt.jo_set & JO_IN_NAME)) |
| 1329 | { |
| 1330 | emsg(_("E915: in_io buffer requires in_buf or in_name to be set")); |
| 1331 | } |
| 1332 | else |
| 1333 | buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE); |
| 1334 | if (buf == NULL) |
| 1335 | goto theend; |
| 1336 | if (buf->b_ml.ml_mfp == NULL) |
| 1337 | { |
| 1338 | char_u numbuf[NUMBUFLEN]; |
| 1339 | char_u *s; |
| 1340 | |
| 1341 | if (opt.jo_set & JO_IN_BUF) |
| 1342 | { |
| 1343 | sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]); |
| 1344 | s = numbuf; |
| 1345 | } |
| 1346 | else |
| 1347 | s = opt.jo_io_name[PART_IN]; |
| 1348 | semsg(_("E918: buffer must be loaded: %s"), s); |
| 1349 | goto theend; |
| 1350 | } |
| 1351 | job->jv_in_buf = buf; |
| 1352 | } |
| 1353 | |
| 1354 | job_set_options(job, &opt); |
| 1355 | |
| 1356 | #ifdef USE_ARGV |
| 1357 | if (argv_arg != NULL) |
| 1358 | { |
| 1359 | // Make a copy of argv_arg for job->jv_argv. |
| 1360 | for (i = 0; argv_arg[i] != NULL; i++) |
| 1361 | argc++; |
| 1362 | argv = ALLOC_MULT(char *, argc + 1); |
| 1363 | if (argv == NULL) |
| 1364 | goto theend; |
| 1365 | for (i = 0; i < argc; i++) |
| 1366 | argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]); |
| 1367 | argv[argc] = NULL; |
| 1368 | } |
| 1369 | else |
| 1370 | #endif |
| 1371 | if (argvars[0].v_type == VAR_STRING) |
| 1372 | { |
| 1373 | // Command is a string. |
| 1374 | cmd = argvars[0].vval.v_string; |
| 1375 | if (cmd == NULL || *skipwhite(cmd) == NUL) |
| 1376 | { |
| 1377 | emsg(_(e_invarg)); |
| 1378 | goto theend; |
| 1379 | } |
| 1380 | |
| 1381 | if (build_argv_from_string(cmd, &argv, &argc) == FAIL) |
| 1382 | goto theend; |
| 1383 | } |
| 1384 | else if (argvars[0].v_type != VAR_LIST |
| 1385 | || argvars[0].vval.v_list == NULL |
| 1386 | || argvars[0].vval.v_list->lv_len < 1) |
| 1387 | { |
| 1388 | emsg(_(e_invarg)); |
| 1389 | goto theend; |
| 1390 | } |
| 1391 | else |
| 1392 | { |
| 1393 | list_T *l = argvars[0].vval.v_list; |
| 1394 | |
| 1395 | if (build_argv_from_list(l, &argv, &argc) == FAIL) |
| 1396 | goto theend; |
| 1397 | |
| 1398 | // Empty command is invalid. |
| 1399 | if (argc == 0 || *skipwhite((char_u *)argv[0]) == NUL) |
| 1400 | { |
| 1401 | emsg(_(e_invarg)); |
| 1402 | goto theend; |
| 1403 | } |
| 1404 | #ifndef USE_ARGV |
| 1405 | if (win32_build_cmd(l, &ga) == FAIL) |
| 1406 | goto theend; |
| 1407 | cmd = ga.ga_data; |
| 1408 | if (cmd == NULL || *skipwhite(cmd) == NUL) |
| 1409 | { |
| 1410 | emsg(_(e_invarg)); |
| 1411 | goto theend; |
| 1412 | } |
| 1413 | #endif |
| 1414 | } |
| 1415 | |
| 1416 | // Save the command used to start the job. |
| 1417 | job->jv_argv = argv; |
| 1418 | |
| 1419 | if (term_job != NULL) |
| 1420 | *term_job = job; |
| 1421 | |
| 1422 | #ifdef USE_ARGV |
| 1423 | if (ch_log_active()) |
| 1424 | { |
| 1425 | garray_T ga; |
| 1426 | |
| 1427 | ga_init2(&ga, (int)sizeof(char), 200); |
| 1428 | for (i = 0; i < argc; ++i) |
| 1429 | { |
| 1430 | if (i > 0) |
| 1431 | ga_concat(&ga, (char_u *)" "); |
| 1432 | ga_concat(&ga, (char_u *)argv[i]); |
| 1433 | } |
| 1434 | ga_append(&ga, NUL); |
| 1435 | ch_log(NULL, "Starting job: %s", (char *)ga.ga_data); |
| 1436 | ga_clear(&ga); |
| 1437 | } |
| 1438 | mch_job_start(argv, job, &opt, term_job != NULL); |
| 1439 | #else |
| 1440 | ch_log(NULL, "Starting job: %s", (char *)cmd); |
| 1441 | mch_job_start((char *)cmd, job, &opt); |
| 1442 | #endif |
| 1443 | |
| 1444 | // If the channel is reading from a buffer, write lines now. |
| 1445 | if (job->jv_channel != NULL) |
| 1446 | channel_write_in(job->jv_channel); |
| 1447 | |
| 1448 | theend: |
| 1449 | #ifndef USE_ARGV |
| 1450 | vim_free(ga.ga_data); |
| 1451 | #endif |
| 1452 | if (argv != NULL && argv != job->jv_argv) |
| 1453 | { |
| 1454 | for (i = 0; argv[i] != NULL; i++) |
| 1455 | vim_free(argv[i]); |
| 1456 | vim_free(argv); |
| 1457 | } |
| 1458 | free_job_options(&opt); |
| 1459 | return job; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
| 1463 | * Get the status of "job" and invoke the exit callback when needed. |
| 1464 | * The returned string is not allocated. |
| 1465 | */ |
| 1466 | char * |
| 1467 | job_status(job_T *job) |
| 1468 | { |
| 1469 | char *result; |
| 1470 | |
| 1471 | if (job->jv_status >= JOB_ENDED) |
| 1472 | // No need to check, dead is dead. |
| 1473 | result = "dead"; |
| 1474 | else if (job->jv_status == JOB_FAILED) |
| 1475 | result = "fail"; |
| 1476 | else |
| 1477 | { |
| 1478 | result = mch_job_status(job); |
| 1479 | if (job->jv_status == JOB_ENDED) |
| 1480 | job_cleanup(job); |
| 1481 | } |
| 1482 | return result; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * Send a signal to "job". Implements job_stop(). |
| 1487 | * When "type" is not NULL use this for the type. |
| 1488 | * Otherwise use argvars[1] for the type. |
| 1489 | */ |
| 1490 | int |
| 1491 | job_stop(job_T *job, typval_T *argvars, char *type) |
| 1492 | { |
| 1493 | char_u *arg; |
| 1494 | |
| 1495 | if (type != NULL) |
| 1496 | arg = (char_u *)type; |
| 1497 | else if (argvars[1].v_type == VAR_UNKNOWN) |
| 1498 | arg = (char_u *)""; |
| 1499 | else |
| 1500 | { |
| 1501 | arg = tv_get_string_chk(&argvars[1]); |
| 1502 | if (arg == NULL) |
| 1503 | { |
| 1504 | emsg(_(e_invarg)); |
| 1505 | return 0; |
| 1506 | } |
| 1507 | } |
| 1508 | if (job->jv_status == JOB_FAILED) |
| 1509 | { |
| 1510 | ch_log(job->jv_channel, "Job failed to start, job_stop() skipped"); |
| 1511 | return 0; |
| 1512 | } |
| 1513 | if (job->jv_status == JOB_ENDED) |
| 1514 | { |
| 1515 | ch_log(job->jv_channel, "Job has already ended, job_stop() skipped"); |
| 1516 | return 0; |
| 1517 | } |
| 1518 | ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg); |
| 1519 | if (mch_signal_job(job, arg) == FAIL) |
| 1520 | return 0; |
| 1521 | |
| 1522 | // Assume that only "kill" will kill the job. |
| 1523 | if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0) |
| 1524 | job->jv_channel->ch_job_killed = TRUE; |
| 1525 | |
| 1526 | // We don't try freeing the job, obviously the caller still has a |
| 1527 | // reference to it. |
| 1528 | return 1; |
| 1529 | } |
| 1530 | |
| 1531 | void |
| 1532 | invoke_prompt_callback(void) |
| 1533 | { |
| 1534 | typval_T rettv; |
| 1535 | typval_T argv[2]; |
| 1536 | char_u *text; |
| 1537 | char_u *prompt; |
| 1538 | linenr_T lnum = curbuf->b_ml.ml_line_count; |
| 1539 | |
| 1540 | // Add a new line for the prompt before invoking the callback, so that |
| 1541 | // text can always be inserted above the last line. |
| 1542 | ml_append(lnum, (char_u *)"", 0, FALSE); |
| 1543 | curwin->w_cursor.lnum = lnum + 1; |
| 1544 | curwin->w_cursor.col = 0; |
| 1545 | |
| 1546 | if (curbuf->b_prompt_callback.cb_name == NULL |
| 1547 | || *curbuf->b_prompt_callback.cb_name == NUL) |
| 1548 | return; |
| 1549 | text = ml_get(lnum); |
| 1550 | prompt = prompt_text(); |
| 1551 | if (STRLEN(text) >= STRLEN(prompt)) |
| 1552 | text += STRLEN(prompt); |
| 1553 | argv[0].v_type = VAR_STRING; |
| 1554 | argv[0].vval.v_string = vim_strsave(text); |
| 1555 | argv[1].v_type = VAR_UNKNOWN; |
| 1556 | |
| 1557 | call_callback(&curbuf->b_prompt_callback, -1, &rettv, 1, argv); |
| 1558 | clear_tv(&argv[0]); |
| 1559 | clear_tv(&rettv); |
| 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * Return TRUE when the interrupt callback was invoked. |
| 1564 | */ |
| 1565 | int |
| 1566 | invoke_prompt_interrupt(void) |
| 1567 | { |
| 1568 | typval_T rettv; |
| 1569 | typval_T argv[1]; |
| 1570 | |
| 1571 | if (curbuf->b_prompt_interrupt.cb_name == NULL |
| 1572 | || *curbuf->b_prompt_interrupt.cb_name == NUL) |
| 1573 | return FALSE; |
| 1574 | argv[0].v_type = VAR_UNKNOWN; |
| 1575 | |
| 1576 | got_int = FALSE; // don't skip executing commands |
| 1577 | call_callback(&curbuf->b_prompt_interrupt, -1, &rettv, 0, argv); |
| 1578 | clear_tv(&rettv); |
| 1579 | return TRUE; |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * Return the effective prompt for the specified buffer. |
| 1584 | */ |
| 1585 | char_u * |
| 1586 | buf_prompt_text(buf_T* buf) |
| 1587 | { |
| 1588 | if (buf->b_prompt_text == NULL) |
| 1589 | return (char_u *)"% "; |
| 1590 | return buf->b_prompt_text; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * Return the effective prompt for the current buffer. |
| 1595 | */ |
| 1596 | char_u * |
| 1597 | prompt_text(void) |
| 1598 | { |
| 1599 | return buf_prompt_text(curbuf); |
| 1600 | } |
| 1601 | |
| 1602 | |
| 1603 | /* |
| 1604 | * Prepare for prompt mode: Make sure the last line has the prompt text. |
| 1605 | * Move the cursor to this line. |
| 1606 | */ |
| 1607 | void |
| 1608 | init_prompt(int cmdchar_todo) |
| 1609 | { |
| 1610 | char_u *prompt = prompt_text(); |
| 1611 | char_u *text; |
| 1612 | |
| 1613 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 1614 | text = ml_get_curline(); |
| 1615 | if (STRNCMP(text, prompt, STRLEN(prompt)) != 0) |
| 1616 | { |
| 1617 | // prompt is missing, insert it or append a line with it |
| 1618 | if (*text == NUL) |
| 1619 | ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE); |
| 1620 | else |
| 1621 | ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE); |
| 1622 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 1623 | coladvance((colnr_T)MAXCOL); |
| 1624 | changed_bytes(curbuf->b_ml.ml_line_count, 0); |
| 1625 | } |
| 1626 | |
| 1627 | // Insert always starts after the prompt, allow editing text after it. |
| 1628 | if (Insstart_orig.lnum != curwin->w_cursor.lnum |
| 1629 | || Insstart_orig.col != (int)STRLEN(prompt)) |
| 1630 | set_insstart(curwin->w_cursor.lnum, (int)STRLEN(prompt)); |
| 1631 | |
| 1632 | if (cmdchar_todo == 'A') |
| 1633 | coladvance((colnr_T)MAXCOL); |
Bram Moolenaar | ee8b787 | 2020-11-19 18:46:25 +0100 | [diff] [blame] | 1634 | if (curwin->w_cursor.col < (int)STRLEN(prompt)) |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1635 | curwin->w_cursor.col = (int)STRLEN(prompt); |
| 1636 | // Make sure the cursor is in a valid position. |
| 1637 | check_cursor(); |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Return TRUE if the cursor is in the editable position of the prompt line. |
| 1642 | */ |
| 1643 | int |
| 1644 | prompt_curpos_editable() |
| 1645 | { |
| 1646 | return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count |
| 1647 | && curwin->w_cursor.col >= (int)STRLEN(prompt_text()); |
| 1648 | } |
| 1649 | |
| 1650 | /* |
| 1651 | * "prompt_setcallback({buffer}, {callback})" function |
| 1652 | */ |
| 1653 | void |
| 1654 | f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED) |
| 1655 | { |
| 1656 | buf_T *buf; |
| 1657 | callback_T callback; |
| 1658 | |
| 1659 | if (check_secure()) |
| 1660 | return; |
| 1661 | buf = tv_get_buf(&argvars[0], FALSE); |
| 1662 | if (buf == NULL) |
| 1663 | return; |
| 1664 | |
| 1665 | callback = get_callback(&argvars[1]); |
| 1666 | if (callback.cb_name == NULL) |
| 1667 | return; |
| 1668 | |
| 1669 | free_callback(&buf->b_prompt_callback); |
| 1670 | set_callback(&buf->b_prompt_callback, &callback); |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * "prompt_setinterrupt({buffer}, {callback})" function |
| 1675 | */ |
| 1676 | void |
| 1677 | f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED) |
| 1678 | { |
| 1679 | buf_T *buf; |
| 1680 | callback_T callback; |
| 1681 | |
| 1682 | if (check_secure()) |
| 1683 | return; |
| 1684 | buf = tv_get_buf(&argvars[0], FALSE); |
| 1685 | if (buf == NULL) |
| 1686 | return; |
| 1687 | |
| 1688 | callback = get_callback(&argvars[1]); |
| 1689 | if (callback.cb_name == NULL) |
| 1690 | return; |
| 1691 | |
| 1692 | free_callback(&buf->b_prompt_interrupt); |
| 1693 | set_callback(&buf->b_prompt_interrupt, &callback); |
| 1694 | } |
| 1695 | |
| 1696 | |
| 1697 | /* |
| 1698 | * "prompt_getprompt({buffer})" function |
| 1699 | */ |
| 1700 | void |
| 1701 | f_prompt_getprompt(typval_T *argvars, typval_T *rettv) |
| 1702 | { |
| 1703 | buf_T *buf; |
| 1704 | |
| 1705 | // return an empty string by default, e.g. it's not a prompt buffer |
| 1706 | rettv->v_type = VAR_STRING; |
| 1707 | rettv->vval.v_string = NULL; |
| 1708 | |
| 1709 | buf = tv_get_buf_from_arg(&argvars[0]); |
| 1710 | if (buf == NULL) |
| 1711 | return; |
| 1712 | |
| 1713 | if (!bt_prompt(buf)) |
| 1714 | return; |
| 1715 | |
| 1716 | rettv->vval.v_string = vim_strsave(buf_prompt_text(buf)); |
| 1717 | } |
| 1718 | |
| 1719 | /* |
| 1720 | * "prompt_setprompt({buffer}, {text})" function |
| 1721 | */ |
| 1722 | void |
| 1723 | f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED) |
| 1724 | { |
| 1725 | buf_T *buf; |
| 1726 | char_u *text; |
| 1727 | |
Yegappan Lakshmanan | a9a7c0c | 2021-07-17 19:11:07 +0200 | [diff] [blame] | 1728 | if (in_vim9script() |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1729 | && (check_for_string_or_number_arg(argvars, 0) == FAIL |
Yegappan Lakshmanan | a9a7c0c | 2021-07-17 19:11:07 +0200 | [diff] [blame] | 1730 | || check_for_string_arg(argvars, 1) == FAIL)) |
| 1731 | return; |
| 1732 | |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1733 | if (check_secure()) |
| 1734 | return; |
| 1735 | buf = tv_get_buf(&argvars[0], FALSE); |
| 1736 | if (buf == NULL) |
| 1737 | return; |
| 1738 | |
| 1739 | text = tv_get_string(&argvars[1]); |
| 1740 | vim_free(buf->b_prompt_text); |
| 1741 | buf->b_prompt_text = vim_strsave(text); |
| 1742 | } |
| 1743 | |
| 1744 | /* |
| 1745 | * Get the job from the argument. |
| 1746 | * Returns NULL if the job is invalid. |
| 1747 | */ |
| 1748 | static job_T * |
| 1749 | get_job_arg(typval_T *tv) |
| 1750 | { |
| 1751 | job_T *job; |
| 1752 | |
| 1753 | if (tv->v_type != VAR_JOB) |
| 1754 | { |
| 1755 | semsg(_(e_invarg2), tv_get_string(tv)); |
| 1756 | return NULL; |
| 1757 | } |
| 1758 | job = tv->vval.v_job; |
| 1759 | |
| 1760 | if (job == NULL) |
| 1761 | emsg(_("E916: not a valid job")); |
| 1762 | return job; |
| 1763 | } |
| 1764 | |
| 1765 | /* |
| 1766 | * "job_getchannel()" function |
| 1767 | */ |
| 1768 | void |
| 1769 | f_job_getchannel(typval_T *argvars, typval_T *rettv) |
| 1770 | { |
| 1771 | job_T *job = get_job_arg(&argvars[0]); |
| 1772 | |
| 1773 | if (job != NULL) |
| 1774 | { |
| 1775 | rettv->v_type = VAR_CHANNEL; |
| 1776 | rettv->vval.v_channel = job->jv_channel; |
| 1777 | if (job->jv_channel != NULL) |
| 1778 | ++job->jv_channel->ch_refcount; |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | /* |
| 1783 | * Implementation of job_info(). |
| 1784 | */ |
| 1785 | static void |
| 1786 | job_info(job_T *job, dict_T *dict) |
| 1787 | { |
| 1788 | dictitem_T *item; |
| 1789 | varnumber_T nr; |
| 1790 | list_T *l; |
| 1791 | int i; |
| 1792 | |
| 1793 | dict_add_string(dict, "status", (char_u *)job_status(job)); |
| 1794 | |
| 1795 | item = dictitem_alloc((char_u *)"channel"); |
| 1796 | if (item == NULL) |
| 1797 | return; |
| 1798 | item->di_tv.v_type = VAR_CHANNEL; |
| 1799 | item->di_tv.vval.v_channel = job->jv_channel; |
| 1800 | if (job->jv_channel != NULL) |
| 1801 | ++job->jv_channel->ch_refcount; |
| 1802 | if (dict_add(dict, item) == FAIL) |
| 1803 | dictitem_free(item); |
| 1804 | |
| 1805 | #ifdef UNIX |
| 1806 | nr = job->jv_pid; |
| 1807 | #else |
| 1808 | nr = job->jv_proc_info.dwProcessId; |
| 1809 | #endif |
| 1810 | dict_add_number(dict, "process", nr); |
| 1811 | dict_add_string(dict, "tty_in", job->jv_tty_in); |
| 1812 | dict_add_string(dict, "tty_out", job->jv_tty_out); |
| 1813 | |
| 1814 | dict_add_number(dict, "exitval", job->jv_exitval); |
| 1815 | dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name); |
| 1816 | dict_add_string(dict, "stoponexit", job->jv_stoponexit); |
| 1817 | #ifdef UNIX |
| 1818 | dict_add_string(dict, "termsig", job->jv_termsig); |
| 1819 | #endif |
| 1820 | #ifdef MSWIN |
| 1821 | dict_add_string(dict, "tty_type", job->jv_tty_type); |
| 1822 | #endif |
| 1823 | |
| 1824 | l = list_alloc(); |
| 1825 | if (l != NULL) |
| 1826 | { |
| 1827 | dict_add_list(dict, "cmd", l); |
| 1828 | if (job->jv_argv != NULL) |
| 1829 | for (i = 0; job->jv_argv[i] != NULL; i++) |
| 1830 | list_append_string(l, (char_u *)job->jv_argv[i], -1); |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * Implementation of job_info() to return info for all jobs. |
| 1836 | */ |
| 1837 | static void |
| 1838 | job_info_all(list_T *l) |
| 1839 | { |
| 1840 | job_T *job; |
| 1841 | typval_T tv; |
| 1842 | |
| 1843 | FOR_ALL_JOBS(job) |
| 1844 | { |
| 1845 | tv.v_type = VAR_JOB; |
| 1846 | tv.vval.v_job = job; |
| 1847 | |
| 1848 | if (list_append_tv(l, &tv) != OK) |
| 1849 | return; |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | /* |
| 1854 | * "job_info()" function |
| 1855 | */ |
| 1856 | void |
| 1857 | f_job_info(typval_T *argvars, typval_T *rettv) |
| 1858 | { |
| 1859 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 1860 | { |
| 1861 | job_T *job = get_job_arg(&argvars[0]); |
| 1862 | |
| 1863 | if (job != NULL && rettv_dict_alloc(rettv) != FAIL) |
| 1864 | job_info(job, rettv->vval.v_dict); |
| 1865 | } |
| 1866 | else if (rettv_list_alloc(rettv) == OK) |
| 1867 | job_info_all(rettv->vval.v_list); |
| 1868 | } |
| 1869 | |
| 1870 | /* |
| 1871 | * "job_setoptions()" function |
| 1872 | */ |
| 1873 | void |
| 1874 | f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED) |
| 1875 | { |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1876 | job_T *job; |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1877 | jobopt_T opt; |
| 1878 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1879 | if (in_vim9script() |
| 1880 | && (check_for_job_arg(argvars, 0) == FAIL |
| 1881 | || check_for_dict_arg(argvars, 1) == FAIL)) |
| 1882 | return; |
| 1883 | |
| 1884 | job = get_job_arg(&argvars[0]); |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1885 | if (job == NULL) |
| 1886 | return; |
| 1887 | clear_job_options(&opt); |
| 1888 | if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK) |
| 1889 | job_set_options(job, &opt); |
| 1890 | free_job_options(&opt); |
| 1891 | } |
| 1892 | |
| 1893 | /* |
| 1894 | * "job_start()" function |
| 1895 | */ |
| 1896 | void |
| 1897 | f_job_start(typval_T *argvars, typval_T *rettv) |
| 1898 | { |
| 1899 | rettv->v_type = VAR_JOB; |
| 1900 | if (check_restricted() || check_secure()) |
| 1901 | return; |
| 1902 | rettv->vval.v_job = job_start(argvars, NULL, NULL, NULL); |
| 1903 | } |
| 1904 | |
| 1905 | /* |
| 1906 | * "job_status()" function |
| 1907 | */ |
| 1908 | void |
| 1909 | f_job_status(typval_T *argvars, typval_T *rettv) |
| 1910 | { |
Bram Moolenaar | 218450a | 2020-10-17 18:51:52 +0200 | [diff] [blame] | 1911 | if (argvars[0].v_type == VAR_JOB && argvars[0].vval.v_job == NULL) |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1912 | { |
Bram Moolenaar | 218450a | 2020-10-17 18:51:52 +0200 | [diff] [blame] | 1913 | // A job that never started returns "fail". |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1914 | rettv->v_type = VAR_STRING; |
Bram Moolenaar | 218450a | 2020-10-17 18:51:52 +0200 | [diff] [blame] | 1915 | rettv->vval.v_string = vim_strsave((char_u *)"fail"); |
| 1916 | } |
| 1917 | else |
| 1918 | { |
| 1919 | job_T *job = get_job_arg(&argvars[0]); |
| 1920 | |
| 1921 | if (job != NULL) |
| 1922 | { |
| 1923 | rettv->v_type = VAR_STRING; |
| 1924 | rettv->vval.v_string = vim_strsave((char_u *)job_status(job)); |
| 1925 | } |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | /* |
| 1930 | * "job_stop()" function |
| 1931 | */ |
| 1932 | void |
| 1933 | f_job_stop(typval_T *argvars, typval_T *rettv) |
| 1934 | { |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1935 | job_T *job; |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1936 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1937 | if (in_vim9script() |
| 1938 | && (check_for_job_arg(argvars, 0) == FAIL |
| 1939 | || check_for_opt_string_or_number_arg(argvars, 1) == FAIL)) |
| 1940 | return; |
| 1941 | |
| 1942 | job = get_job_arg(&argvars[0]); |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1943 | if (job != NULL) |
| 1944 | rettv->vval.v_number = job_stop(job, argvars, NULL); |
| 1945 | } |
| 1946 | |
Bram Moolenaar | 1328bde | 2021-06-05 20:51:38 +0200 | [diff] [blame] | 1947 | /* |
| 1948 | * Get a string with information about the job in "varp" in "buf". |
| 1949 | * "buf" must be at least NUMBUFLEN long. |
| 1950 | */ |
| 1951 | char_u * |
| 1952 | job_to_string_buf(typval_T *varp, char_u *buf) |
| 1953 | { |
| 1954 | job_T *job = varp->vval.v_job; |
| 1955 | char *status; |
| 1956 | |
| 1957 | if (job == NULL) |
| 1958 | return (char_u *)"no process"; |
| 1959 | status = job->jv_status == JOB_FAILED ? "fail" |
| 1960 | : job->jv_status >= JOB_ENDED ? "dead" |
| 1961 | : "run"; |
| 1962 | # ifdef UNIX |
| 1963 | vim_snprintf((char *)buf, NUMBUFLEN, |
| 1964 | "process %ld %s", (long)job->jv_pid, status); |
| 1965 | # elif defined(MSWIN) |
| 1966 | vim_snprintf((char *)buf, NUMBUFLEN, |
| 1967 | "process %ld %s", |
| 1968 | (long)job->jv_proc_info.dwProcessId, |
| 1969 | status); |
| 1970 | # else |
| 1971 | // fall-back |
| 1972 | vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status); |
| 1973 | # endif |
| 1974 | return buf; |
| 1975 | } |
| 1976 | |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1977 | #endif // FEAT_JOB_CHANNEL |