blob: 80cb47eeb179d4aada39987f7e57d7f725134f1f [file] [log] [blame]
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001/* 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
21handle_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
43handle_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
70clear_job_options(jobopt_T *opt)
71{
72 CLEAR_POINTER(opt);
73}
74
75/*
76 * Free any members of a jobopt_T.
77 */
78 void
79free_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
109part_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
121get_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 {
Bram Moolenaar88137392021-11-12 16:01:15 +0000427 int error = FALSE;
428
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200429 if (!(supported2 & JO2_TERM_ROWS))
430 break;
431 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaar88137392021-11-12 16:01:15 +0000432 opt->jo_term_rows = tv_get_number_chk(item, &error);
433 if (error)
434 return FAIL;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200435 }
436 else if (STRCMP(hi->hi_key, "term_cols") == 0)
437 {
438 if (!(supported2 & JO2_TERM_COLS))
439 break;
440 opt->jo_set2 |= JO2_TERM_COLS;
441 opt->jo_term_cols = tv_get_number(item);
442 }
443 else if (STRCMP(hi->hi_key, "vertical") == 0)
444 {
445 if (!(supported2 & JO2_VERTICAL))
446 break;
447 opt->jo_set2 |= JO2_VERTICAL;
448 opt->jo_vertical = tv_get_bool(item);
449 }
450 else if (STRCMP(hi->hi_key, "curwin") == 0)
451 {
452 if (!(supported2 & JO2_CURWIN))
453 break;
454 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaarad304702020-09-06 18:22:53 +0200455 opt->jo_curwin = tv_get_bool(item);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200456 }
457 else if (STRCMP(hi->hi_key, "bufnr") == 0)
458 {
459 int nr;
460
461 if (!(supported2 & JO2_CURWIN))
462 break;
463 opt->jo_set2 |= JO2_BUFNR;
464 nr = tv_get_number(item);
465 if (nr <= 0)
466 {
467 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
468 return FAIL;
469 }
470 opt->jo_bufnr_buf = buflist_findnr(nr);
471 if (opt->jo_bufnr_buf == NULL)
472 {
473 semsg(_(e_nobufnr), (long)nr);
474 return FAIL;
475 }
476 if (opt->jo_bufnr_buf->b_nwindows == 0
477 || opt->jo_bufnr_buf->b_term == NULL)
478 {
479 semsg(_(e_invarg2), "bufnr");
480 return FAIL;
481 }
482 }
483 else if (STRCMP(hi->hi_key, "hidden") == 0)
484 {
485 if (!(supported2 & JO2_HIDDEN))
486 break;
487 opt->jo_set2 |= JO2_HIDDEN;
488 opt->jo_hidden = tv_get_bool(item);
489 }
490 else if (STRCMP(hi->hi_key, "norestore") == 0)
491 {
492 if (!(supported2 & JO2_NORESTORE))
493 break;
494 opt->jo_set2 |= JO2_NORESTORE;
495 opt->jo_term_norestore = tv_get_bool(item);
496 }
497 else if (STRCMP(hi->hi_key, "term_kill") == 0)
498 {
499 if (!(supported2 & JO2_TERM_KILL))
500 break;
501 opt->jo_set2 |= JO2_TERM_KILL;
502 opt->jo_term_kill = tv_get_string_buf_chk(item,
503 opt->jo_term_kill_buf);
504 if (opt->jo_term_kill == NULL)
505 {
506 semsg(_(e_invargval), "term_kill");
507 return FAIL;
508 }
509 }
510 else if (STRCMP(hi->hi_key, "tty_type") == 0)
511 {
512 char_u *p;
513
514 if (!(supported2 & JO2_TTY_TYPE))
515 break;
516 opt->jo_set2 |= JO2_TTY_TYPE;
517 p = tv_get_string_chk(item);
518 if (p == NULL)
519 {
520 semsg(_(e_invargval), "tty_type");
521 return FAIL;
522 }
523 // Allow empty string, "winpty", "conpty".
524 if (!(*p == NUL || STRCMP(p, "winpty") == 0
525 || STRCMP(p, "conpty") == 0))
526 {
527 semsg(_(e_invargval), "tty_type");
528 return FAIL;
529 }
530 opt->jo_tty_type = p[0];
531 }
532# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
533 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
534 {
535 int n = 0;
536 listitem_T *li;
537 long_u rgb[16];
538
539 if (!(supported2 & JO2_ANSI_COLORS))
540 break;
541
542 if (item == NULL || item->v_type != VAR_LIST
543 || item->vval.v_list == NULL)
544 {
545 semsg(_(e_invargval), "ansi_colors");
546 return FAIL;
547 }
548
549 CHECK_LIST_MATERIALIZE(item->vval.v_list);
550 li = item->vval.v_list->lv_first;
551 for (; li != NULL && n < 16; li = li->li_next, n++)
552 {
553 char_u *color_name;
554 guicolor_T guicolor;
555 int called_emsg_before = called_emsg;
556
557 color_name = tv_get_string_chk(&li->li_tv);
558 if (color_name == NULL)
559 return FAIL;
560
561 guicolor = GUI_GET_COLOR(color_name);
562 if (guicolor == INVALCOLOR)
563 {
564 if (called_emsg_before == called_emsg)
565 // may not get the error if the GUI didn't start
Drew Vogele30d1022021-10-24 20:35:07 +0100566 semsg(_(e_cannot_allocate_color_str), color_name);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200567 return FAIL;
568 }
569
570 rgb[n] = GUI_MCH_GET_RGB(guicolor);
571 }
572
573 if (n != 16 || li != NULL)
574 {
575 semsg(_(e_invargval), "ansi_colors");
576 return FAIL;
577 }
578
579 opt->jo_set2 |= JO2_ANSI_COLORS;
580 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
581 }
582# endif
583 else if (STRCMP(hi->hi_key, "term_highlight") == 0)
584 {
585 char_u *p;
586
587 if (!(supported2 & JO2_TERM_HIGHLIGHT))
588 break;
589 opt->jo_set2 |= JO2_TERM_HIGHLIGHT;
590 p = tv_get_string_buf_chk(item, opt->jo_term_highlight_buf);
591 if (p == NULL || *p == NUL)
592 {
593 semsg(_(e_invargval), "term_highlight");
594 return FAIL;
595 }
596 opt->jo_term_highlight = p;
597 }
598 else if (STRCMP(hi->hi_key, "term_api") == 0)
599 {
600 if (!(supported2 & JO2_TERM_API))
601 break;
602 opt->jo_set2 |= JO2_TERM_API;
603 opt->jo_term_api = tv_get_string_buf_chk(item,
604 opt->jo_term_api_buf);
605 if (opt->jo_term_api == NULL)
606 {
607 semsg(_(e_invargval), "term_api");
608 return FAIL;
609 }
610 }
611#endif
612 else if (STRCMP(hi->hi_key, "env") == 0)
613 {
614 if (!(supported2 & JO2_ENV))
615 break;
616 if (item->v_type != VAR_DICT)
617 {
618 semsg(_(e_invargval), "env");
619 return FAIL;
620 }
621 opt->jo_set2 |= JO2_ENV;
622 opt->jo_env = item->vval.v_dict;
623 if (opt->jo_env != NULL)
624 ++opt->jo_env->dv_refcount;
625 }
626 else if (STRCMP(hi->hi_key, "cwd") == 0)
627 {
628 if (!(supported2 & JO2_CWD))
629 break;
630 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
631 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
632#ifndef MSWIN // Win32 directories don't have the concept of "executable"
633 || mch_access((char *)opt->jo_cwd, X_OK) != 0
634#endif
635 )
636 {
637 semsg(_(e_invargval), "cwd");
638 return FAIL;
639 }
640 opt->jo_set2 |= JO2_CWD;
641 }
642 else if (STRCMP(hi->hi_key, "waittime") == 0)
643 {
644 if (!(supported & JO_WAITTIME))
645 break;
646 opt->jo_set |= JO_WAITTIME;
647 opt->jo_waittime = tv_get_number(item);
648 }
649 else if (STRCMP(hi->hi_key, "timeout") == 0)
650 {
651 if (!(supported & JO_TIMEOUT))
652 break;
653 opt->jo_set |= JO_TIMEOUT;
654 opt->jo_timeout = tv_get_number(item);
655 }
656 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
657 {
658 if (!(supported & JO_OUT_TIMEOUT))
659 break;
660 opt->jo_set |= JO_OUT_TIMEOUT;
661 opt->jo_out_timeout = tv_get_number(item);
662 }
663 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
664 {
665 if (!(supported & JO_ERR_TIMEOUT))
666 break;
667 opt->jo_set |= JO_ERR_TIMEOUT;
668 opt->jo_err_timeout = tv_get_number(item);
669 }
670 else if (STRCMP(hi->hi_key, "part") == 0)
671 {
672 if (!(supported & JO_PART))
673 break;
674 opt->jo_set |= JO_PART;
675 val = tv_get_string(item);
676 if (STRCMP(val, "err") == 0)
677 opt->jo_part = PART_ERR;
678 else if (STRCMP(val, "out") == 0)
679 opt->jo_part = PART_OUT;
680 else
681 {
682 semsg(_(e_invargNval), "part", val);
683 return FAIL;
684 }
685 }
686 else if (STRCMP(hi->hi_key, "id") == 0)
687 {
688 if (!(supported & JO_ID))
689 break;
690 opt->jo_set |= JO_ID;
691 opt->jo_id = tv_get_number(item);
692 }
693 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
694 {
695 if (!(supported & JO_STOPONEXIT))
696 break;
697 opt->jo_set |= JO_STOPONEXIT;
698 opt->jo_stoponexit = tv_get_string_buf_chk(item,
699 opt->jo_stoponexit_buf);
700 if (opt->jo_stoponexit == NULL)
701 {
702 semsg(_(e_invargval), "stoponexit");
703 return FAIL;
704 }
705 }
706 else if (STRCMP(hi->hi_key, "block_write") == 0)
707 {
708 if (!(supported & JO_BLOCK_WRITE))
709 break;
710 opt->jo_set |= JO_BLOCK_WRITE;
711 opt->jo_block_write = tv_get_number(item);
712 }
713 else
714 break;
715 --todo;
716 }
717 if (todo > 0)
718 {
719 semsg(_(e_invarg2), hi->hi_key);
720 return FAIL;
721 }
722
723 return OK;
724}
725
726static job_T *first_job = NULL;
727
728 static void
729job_free_contents(job_T *job)
730{
731 int i;
732
733 ch_log(job->jv_channel, "Freeing job");
734 if (job->jv_channel != NULL)
735 {
736 // The link from the channel to the job doesn't count as a reference,
737 // thus don't decrement the refcount of the job. The reference from
738 // the job to the channel does count the reference, decrement it and
739 // NULL the reference. We don't set ch_job_killed, unreferencing the
740 // job doesn't mean it stops running.
741 job->jv_channel->ch_job = NULL;
742 channel_unref(job->jv_channel);
743 }
744 mch_clear_job(job);
745
746 vim_free(job->jv_tty_in);
747 vim_free(job->jv_tty_out);
748 vim_free(job->jv_stoponexit);
749#ifdef UNIX
750 vim_free(job->jv_termsig);
751#endif
752#ifdef MSWIN
753 vim_free(job->jv_tty_type);
754#endif
755 free_callback(&job->jv_exit_cb);
756 if (job->jv_argv != NULL)
757 {
758 for (i = 0; job->jv_argv[i] != NULL; i++)
759 vim_free(job->jv_argv[i]);
760 vim_free(job->jv_argv);
761 }
762}
763
764/*
765 * Remove "job" from the list of jobs.
766 */
767 static void
768job_unlink(job_T *job)
769{
770 if (job->jv_next != NULL)
771 job->jv_next->jv_prev = job->jv_prev;
772 if (job->jv_prev == NULL)
773 first_job = job->jv_next;
774 else
775 job->jv_prev->jv_next = job->jv_next;
776}
777
778 static void
779job_free_job(job_T *job)
780{
781 job_unlink(job);
782 vim_free(job);
783}
784
785 static void
786job_free(job_T *job)
787{
788 if (!in_free_unref_items)
789 {
790 job_free_contents(job);
791 job_free_job(job);
792 }
793}
794
795static job_T *jobs_to_free = NULL;
796
797/*
798 * Put "job" in a list to be freed later, when it's no longer referenced.
799 */
800 static void
801job_free_later(job_T *job)
802{
803 job_unlink(job);
804 job->jv_next = jobs_to_free;
805 jobs_to_free = job;
806}
807
808 static void
809free_jobs_to_free_later(void)
810{
811 job_T *job;
812
813 while (jobs_to_free != NULL)
814 {
815 job = jobs_to_free;
816 jobs_to_free = job->jv_next;
817 job_free_contents(job);
818 vim_free(job);
819 }
820}
821
822#if defined(EXITFREE) || defined(PROTO)
823 void
824job_free_all(void)
825{
826 while (first_job != NULL)
827 job_free(first_job);
828 free_jobs_to_free_later();
829
830# ifdef FEAT_TERMINAL
831 free_unused_terminals();
832# endif
833}
834#endif
835
836/*
837 * Return TRUE if we need to check if the process of "job" has ended.
838 */
839 static int
840job_need_end_check(job_T *job)
841{
842 return job->jv_status == JOB_STARTED
843 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
844}
845
846/*
847 * Return TRUE if the channel of "job" is still useful.
848 */
849 static int
850job_channel_still_useful(job_T *job)
851{
852 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
853}
854
855/*
856 * Return TRUE if the channel of "job" is closeable.
857 */
858 static int
859job_channel_can_close(job_T *job)
860{
861 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
862}
863
864/*
865 * Return TRUE if the job should not be freed yet. Do not free the job when
866 * it has not ended yet and there is a "stoponexit" flag, an exit callback
867 * or when the associated channel will do something with the job output.
868 */
869 static int
870job_still_useful(job_T *job)
871{
872 return job_need_end_check(job) || job_channel_still_useful(job);
873}
874
875#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
876/*
877 * Return TRUE when there is any running job that we care about.
878 */
879 int
880job_any_running()
881{
882 job_T *job;
883
884 FOR_ALL_JOBS(job)
885 if (job_still_useful(job))
886 {
887 ch_log(NULL, "GUI not forking because a job is running");
888 return TRUE;
889 }
890 return FALSE;
891}
892#endif
893
Bram Moolenaarf46bf522020-12-09 13:16:13 +0100894// Unix uses argv[] for the command, other systems use a string.
895#if defined(UNIX)
896# define USE_ARGV
897#endif
898
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200899#if !defined(USE_ARGV) || defined(PROTO)
900/*
901 * Escape one argument for an external command.
902 * Returns the escaped string in allocated memory. NULL when out of memory.
903 */
904 static char_u *
905win32_escape_arg(char_u *arg)
906{
907 int slen, dlen;
908 int escaping = 0;
909 int i;
910 char_u *s, *d;
911 char_u *escaped_arg;
912 int has_spaces = FALSE;
913
914 // First count the number of extra bytes required.
915 slen = (int)STRLEN(arg);
916 dlen = slen;
917 for (s = arg; *s != NUL; MB_PTR_ADV(s))
918 {
919 if (*s == '"' || *s == '\\')
920 ++dlen;
921 if (*s == ' ' || *s == '\t')
922 has_spaces = TRUE;
923 }
924
925 if (has_spaces)
926 dlen += 2;
927
928 if (dlen == slen)
929 return vim_strsave(arg);
930
931 // Allocate memory for the result and fill it.
932 escaped_arg = alloc(dlen + 1);
933 if (escaped_arg == NULL)
934 return NULL;
935 memset(escaped_arg, 0, dlen+1);
936
937 d = escaped_arg;
938
939 if (has_spaces)
940 *d++ = '"';
941
942 for (s = arg; *s != NUL;)
943 {
944 switch (*s)
945 {
946 case '"':
947 for (i = 0; i < escaping; i++)
948 *d++ = '\\';
949 escaping = 0;
950 *d++ = '\\';
951 *d++ = *s++;
952 break;
953 case '\\':
954 escaping++;
955 *d++ = *s++;
956 break;
957 default:
958 escaping = 0;
959 MB_COPY_CHAR(s, d);
960 break;
961 }
962 }
963
964 // add terminating quote and finish with a NUL
965 if (has_spaces)
966 {
967 for (i = 0; i < escaping; i++)
968 *d++ = '\\';
969 *d++ = '"';
970 }
971 *d = NUL;
972
973 return escaped_arg;
974}
975
976/*
977 * Build a command line from a list, taking care of escaping.
978 * The result is put in gap->ga_data.
979 * Returns FAIL when out of memory.
980 */
981 int
982win32_build_cmd(list_T *l, garray_T *gap)
983{
984 listitem_T *li;
985 char_u *s;
986
987 CHECK_LIST_MATERIALIZE(l);
988 FOR_ALL_LIST_ITEMS(l, li)
989 {
990 s = tv_get_string_chk(&li->li_tv);
991 if (s == NULL)
992 return FAIL;
993 s = win32_escape_arg(s);
994 if (s == NULL)
995 return FAIL;
996 ga_concat(gap, s);
997 vim_free(s);
998 if (li->li_next != NULL)
999 ga_append(gap, ' ');
1000 }
1001 return OK;
1002}
1003#endif
1004
1005/*
1006 * NOTE: Must call job_cleanup() only once right after the status of "job"
1007 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
1008 * mch_detect_ended_job() returned non-NULL).
1009 * If the job is no longer used it will be removed from the list of jobs, and
1010 * deleted a bit later.
1011 */
1012 void
1013job_cleanup(job_T *job)
1014{
1015 if (job->jv_status != JOB_ENDED)
1016 return;
1017
1018 // Ready to cleanup the job.
1019 job->jv_status = JOB_FINISHED;
1020
1021 // When only channel-in is kept open, close explicitly.
1022 if (job->jv_channel != NULL)
1023 ch_close_part(job->jv_channel, PART_IN);
1024
1025 if (job->jv_exit_cb.cb_name != NULL)
1026 {
1027 typval_T argv[3];
1028 typval_T rettv;
1029
1030 // Invoke the exit callback. Make sure the refcount is > 0.
1031 ch_log(job->jv_channel, "Invoking exit callback %s",
1032 job->jv_exit_cb.cb_name);
1033 ++job->jv_refcount;
1034 argv[0].v_type = VAR_JOB;
1035 argv[0].vval.v_job = job;
1036 argv[1].v_type = VAR_NUMBER;
1037 argv[1].vval.v_number = job->jv_exitval;
1038 call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv);
1039 clear_tv(&rettv);
1040 --job->jv_refcount;
1041 channel_need_redraw = TRUE;
1042 }
1043
1044 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
1045 job->jv_channel->ch_killing = TRUE;
1046
1047 // Do not free the job in case the close callback of the associated channel
1048 // isn't invoked yet and may get information by job_info().
1049 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
1050 // The job was already unreferenced and the associated channel was
1051 // detached, now that it ended it can be freed. However, a caller might
1052 // still use it, thus free it a bit later.
1053 job_free_later(job);
1054}
1055
1056/*
1057 * Mark references in jobs that are still useful.
1058 */
1059 int
1060set_ref_in_job(int copyID)
1061{
1062 int abort = FALSE;
1063 job_T *job;
1064 typval_T tv;
1065
1066 for (job = first_job; !abort && job != NULL; job = job->jv_next)
1067 if (job_still_useful(job))
1068 {
1069 tv.v_type = VAR_JOB;
1070 tv.vval.v_job = job;
1071 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1072 }
1073 return abort;
1074}
1075
1076/*
1077 * Dereference "job". Note that after this "job" may have been freed.
1078 */
1079 void
1080job_unref(job_T *job)
1081{
1082 if (job != NULL && --job->jv_refcount <= 0)
1083 {
1084 // Do not free the job if there is a channel where the close callback
1085 // may get the job info.
1086 if (!job_channel_still_useful(job))
1087 {
1088 // Do not free the job when it has not ended yet and there is a
1089 // "stoponexit" flag or an exit callback.
1090 if (!job_need_end_check(job))
1091 {
1092 job_free(job);
1093 }
1094 else if (job->jv_channel != NULL)
1095 {
1096 // Do remove the link to the channel, otherwise it hangs
1097 // around until Vim exits. See job_free() for refcount.
1098 ch_log(job->jv_channel, "detaching channel from job");
1099 job->jv_channel->ch_job = NULL;
1100 channel_unref(job->jv_channel);
1101 job->jv_channel = NULL;
1102 }
1103 }
1104 }
1105}
1106
1107 int
1108free_unused_jobs_contents(int copyID, int mask)
1109{
1110 int did_free = FALSE;
1111 job_T *job;
1112
1113 FOR_ALL_JOBS(job)
1114 if ((job->jv_copyID & mask) != (copyID & mask)
1115 && !job_still_useful(job))
1116 {
1117 // Free the channel and ordinary items it contains, but don't
1118 // recurse into Lists, Dictionaries etc.
1119 job_free_contents(job);
1120 did_free = TRUE;
1121 }
1122 return did_free;
1123}
1124
1125 void
1126free_unused_jobs(int copyID, int mask)
1127{
1128 job_T *job;
1129 job_T *job_next;
1130
1131 for (job = first_job; job != NULL; job = job_next)
1132 {
1133 job_next = job->jv_next;
1134 if ((job->jv_copyID & mask) != (copyID & mask)
1135 && !job_still_useful(job))
1136 {
1137 // Free the job struct itself.
1138 job_free_job(job);
1139 }
1140 }
1141}
1142
1143/*
1144 * Allocate a job. Sets the refcount to one and sets options default.
1145 */
1146 job_T *
1147job_alloc(void)
1148{
1149 job_T *job;
1150
1151 job = ALLOC_CLEAR_ONE(job_T);
1152 if (job != NULL)
1153 {
1154 job->jv_refcount = 1;
1155 job->jv_stoponexit = vim_strsave((char_u *)"term");
1156
1157 if (first_job != NULL)
1158 {
1159 first_job->jv_prev = job;
1160 job->jv_next = first_job;
1161 }
1162 first_job = job;
1163 }
1164 return job;
1165}
1166
1167 void
1168job_set_options(job_T *job, jobopt_T *opt)
1169{
1170 if (opt->jo_set & JO_STOPONEXIT)
1171 {
1172 vim_free(job->jv_stoponexit);
1173 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
1174 job->jv_stoponexit = NULL;
1175 else
1176 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
1177 }
1178 if (opt->jo_set & JO_EXIT_CB)
1179 {
1180 free_callback(&job->jv_exit_cb);
1181 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
1182 {
1183 job->jv_exit_cb.cb_name = NULL;
1184 job->jv_exit_cb.cb_partial = NULL;
1185 }
1186 else
1187 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
1188 }
1189}
1190
1191/*
1192 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
1193 */
1194 void
1195job_stop_on_exit(void)
1196{
1197 job_T *job;
1198
1199 FOR_ALL_JOBS(job)
1200 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
1201 mch_signal_job(job, job->jv_stoponexit);
1202}
1203
1204/*
1205 * Return TRUE when there is any job that has an exit callback and might exit,
1206 * which means job_check_ended() should be called more often.
1207 */
1208 int
1209has_pending_job(void)
1210{
1211 job_T *job;
1212
1213 FOR_ALL_JOBS(job)
1214 // Only should check if the channel has been closed, if the channel is
1215 // open the job won't exit.
1216 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
1217 || (job->jv_status == JOB_FINISHED
1218 && job_channel_can_close(job)))
1219 return TRUE;
1220 return FALSE;
1221}
1222
1223#define MAX_CHECK_ENDED 8
1224
1225/*
1226 * Called once in a while: check if any jobs that seem useful have ended.
1227 * Returns TRUE if a job did end.
1228 */
1229 int
1230job_check_ended(void)
1231{
1232 int i;
1233 int did_end = FALSE;
1234
1235 // be quick if there are no jobs to check
1236 if (first_job == NULL)
1237 return did_end;
1238
1239 for (i = 0; i < MAX_CHECK_ENDED; ++i)
1240 {
1241 // NOTE: mch_detect_ended_job() must only return a job of which the
1242 // status was just set to JOB_ENDED.
1243 job_T *job = mch_detect_ended_job(first_job);
1244
1245 if (job == NULL)
1246 break;
1247 did_end = TRUE;
1248 job_cleanup(job); // may add "job" to jobs_to_free
1249 }
1250
1251 // Actually free jobs that were cleaned up.
1252 free_jobs_to_free_later();
1253
1254 if (channel_need_redraw)
1255 {
1256 channel_need_redraw = FALSE;
1257 redraw_after_callback(TRUE);
1258 }
1259 return did_end;
1260}
1261
1262/*
1263 * Create a job and return it. Implements job_start().
1264 * "argv_arg" is only for Unix.
1265 * When "argv_arg" is NULL then "argvars" is used.
1266 * The returned job has a refcount of one.
1267 * Returns NULL when out of memory.
1268 */
1269 job_T *
1270job_start(
1271 typval_T *argvars,
1272 char **argv_arg UNUSED,
1273 jobopt_T *opt_arg,
1274 job_T **term_job)
1275{
1276 job_T *job;
1277 char_u *cmd = NULL;
1278 char **argv = NULL;
1279 int argc = 0;
1280 int i;
Bram Moolenaarf46bf522020-12-09 13:16:13 +01001281#ifndef USE_ARGV
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001282 garray_T ga;
1283#endif
1284 jobopt_T opt;
1285 ch_part_T part;
1286
1287 job = job_alloc();
1288 if (job == NULL)
1289 return NULL;
1290
1291 job->jv_status = JOB_FAILED;
1292#ifndef USE_ARGV
1293 ga_init2(&ga, (int)sizeof(char*), 20);
1294#endif
1295
1296 if (opt_arg != NULL)
1297 opt = *opt_arg;
1298 else
1299 {
1300 // Default mode is NL.
1301 clear_job_options(&opt);
1302 opt.jo_mode = MODE_NL;
1303 if (get_job_options(&argvars[1], &opt,
1304 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
1305 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
1306 JO2_ENV + JO2_CWD) == FAIL)
1307 goto theend;
1308 }
1309
1310 // Check that when io is "file" that there is a file name.
1311 for (part = PART_OUT; part < PART_COUNT; ++part)
1312 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
1313 && opt.jo_io[part] == JIO_FILE
1314 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
1315 || *opt.jo_io_name[part] == NUL))
1316 {
1317 emsg(_("E920: _io file requires _name to be set"));
1318 goto theend;
1319 }
1320
1321 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
1322 {
1323 buf_T *buf = NULL;
1324
1325 // check that we can find the buffer before starting the job
1326 if (opt.jo_set & JO_IN_BUF)
1327 {
1328 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
1329 if (buf == NULL)
1330 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
1331 }
1332 else if (!(opt.jo_set & JO_IN_NAME))
1333 {
1334 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
1335 }
1336 else
1337 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
1338 if (buf == NULL)
1339 goto theend;
1340 if (buf->b_ml.ml_mfp == NULL)
1341 {
1342 char_u numbuf[NUMBUFLEN];
1343 char_u *s;
1344
1345 if (opt.jo_set & JO_IN_BUF)
1346 {
1347 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
1348 s = numbuf;
1349 }
1350 else
1351 s = opt.jo_io_name[PART_IN];
1352 semsg(_("E918: buffer must be loaded: %s"), s);
1353 goto theend;
1354 }
1355 job->jv_in_buf = buf;
1356 }
1357
1358 job_set_options(job, &opt);
1359
1360#ifdef USE_ARGV
1361 if (argv_arg != NULL)
1362 {
1363 // Make a copy of argv_arg for job->jv_argv.
1364 for (i = 0; argv_arg[i] != NULL; i++)
1365 argc++;
1366 argv = ALLOC_MULT(char *, argc + 1);
1367 if (argv == NULL)
1368 goto theend;
1369 for (i = 0; i < argc; i++)
1370 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
1371 argv[argc] = NULL;
1372 }
1373 else
1374#endif
1375 if (argvars[0].v_type == VAR_STRING)
1376 {
1377 // Command is a string.
1378 cmd = argvars[0].vval.v_string;
1379 if (cmd == NULL || *skipwhite(cmd) == NUL)
1380 {
1381 emsg(_(e_invarg));
1382 goto theend;
1383 }
1384
1385 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
1386 goto theend;
1387 }
1388 else if (argvars[0].v_type != VAR_LIST
1389 || argvars[0].vval.v_list == NULL
1390 || argvars[0].vval.v_list->lv_len < 1)
1391 {
1392 emsg(_(e_invarg));
1393 goto theend;
1394 }
1395 else
1396 {
1397 list_T *l = argvars[0].vval.v_list;
1398
1399 if (build_argv_from_list(l, &argv, &argc) == FAIL)
1400 goto theend;
1401
1402 // Empty command is invalid.
1403 if (argc == 0 || *skipwhite((char_u *)argv[0]) == NUL)
1404 {
1405 emsg(_(e_invarg));
1406 goto theend;
1407 }
1408#ifndef USE_ARGV
1409 if (win32_build_cmd(l, &ga) == FAIL)
1410 goto theend;
1411 cmd = ga.ga_data;
1412 if (cmd == NULL || *skipwhite(cmd) == NUL)
1413 {
1414 emsg(_(e_invarg));
1415 goto theend;
1416 }
1417#endif
1418 }
1419
1420 // Save the command used to start the job.
1421 job->jv_argv = argv;
1422
1423 if (term_job != NULL)
1424 *term_job = job;
1425
1426#ifdef USE_ARGV
1427 if (ch_log_active())
1428 {
1429 garray_T ga;
1430
1431 ga_init2(&ga, (int)sizeof(char), 200);
1432 for (i = 0; i < argc; ++i)
1433 {
1434 if (i > 0)
1435 ga_concat(&ga, (char_u *)" ");
1436 ga_concat(&ga, (char_u *)argv[i]);
1437 }
1438 ga_append(&ga, NUL);
1439 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
1440 ga_clear(&ga);
1441 }
1442 mch_job_start(argv, job, &opt, term_job != NULL);
1443#else
1444 ch_log(NULL, "Starting job: %s", (char *)cmd);
1445 mch_job_start((char *)cmd, job, &opt);
1446#endif
1447
1448 // If the channel is reading from a buffer, write lines now.
1449 if (job->jv_channel != NULL)
1450 channel_write_in(job->jv_channel);
1451
1452theend:
1453#ifndef USE_ARGV
1454 vim_free(ga.ga_data);
1455#endif
1456 if (argv != NULL && argv != job->jv_argv)
1457 {
1458 for (i = 0; argv[i] != NULL; i++)
1459 vim_free(argv[i]);
1460 vim_free(argv);
1461 }
1462 free_job_options(&opt);
1463 return job;
1464}
1465
1466/*
1467 * Get the status of "job" and invoke the exit callback when needed.
1468 * The returned string is not allocated.
1469 */
1470 char *
1471job_status(job_T *job)
1472{
1473 char *result;
1474
1475 if (job->jv_status >= JOB_ENDED)
1476 // No need to check, dead is dead.
1477 result = "dead";
1478 else if (job->jv_status == JOB_FAILED)
1479 result = "fail";
1480 else
1481 {
1482 result = mch_job_status(job);
1483 if (job->jv_status == JOB_ENDED)
1484 job_cleanup(job);
1485 }
1486 return result;
1487}
1488
1489/*
1490 * Send a signal to "job". Implements job_stop().
1491 * When "type" is not NULL use this for the type.
1492 * Otherwise use argvars[1] for the type.
1493 */
1494 int
1495job_stop(job_T *job, typval_T *argvars, char *type)
1496{
1497 char_u *arg;
1498
1499 if (type != NULL)
1500 arg = (char_u *)type;
1501 else if (argvars[1].v_type == VAR_UNKNOWN)
1502 arg = (char_u *)"";
1503 else
1504 {
1505 arg = tv_get_string_chk(&argvars[1]);
1506 if (arg == NULL)
1507 {
1508 emsg(_(e_invarg));
1509 return 0;
1510 }
1511 }
1512 if (job->jv_status == JOB_FAILED)
1513 {
1514 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
1515 return 0;
1516 }
1517 if (job->jv_status == JOB_ENDED)
1518 {
1519 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
1520 return 0;
1521 }
1522 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
1523 if (mch_signal_job(job, arg) == FAIL)
1524 return 0;
1525
1526 // Assume that only "kill" will kill the job.
1527 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
1528 job->jv_channel->ch_job_killed = TRUE;
1529
1530 // We don't try freeing the job, obviously the caller still has a
1531 // reference to it.
1532 return 1;
1533}
1534
1535 void
1536invoke_prompt_callback(void)
1537{
1538 typval_T rettv;
1539 typval_T argv[2];
1540 char_u *text;
1541 char_u *prompt;
1542 linenr_T lnum = curbuf->b_ml.ml_line_count;
1543
1544 // Add a new line for the prompt before invoking the callback, so that
1545 // text can always be inserted above the last line.
1546 ml_append(lnum, (char_u *)"", 0, FALSE);
1547 curwin->w_cursor.lnum = lnum + 1;
1548 curwin->w_cursor.col = 0;
1549
1550 if (curbuf->b_prompt_callback.cb_name == NULL
1551 || *curbuf->b_prompt_callback.cb_name == NUL)
1552 return;
1553 text = ml_get(lnum);
1554 prompt = prompt_text();
1555 if (STRLEN(text) >= STRLEN(prompt))
1556 text += STRLEN(prompt);
1557 argv[0].v_type = VAR_STRING;
1558 argv[0].vval.v_string = vim_strsave(text);
1559 argv[1].v_type = VAR_UNKNOWN;
1560
1561 call_callback(&curbuf->b_prompt_callback, -1, &rettv, 1, argv);
1562 clear_tv(&argv[0]);
1563 clear_tv(&rettv);
1564}
1565
1566/*
1567 * Return TRUE when the interrupt callback was invoked.
1568 */
1569 int
1570invoke_prompt_interrupt(void)
1571{
1572 typval_T rettv;
1573 typval_T argv[1];
1574
1575 if (curbuf->b_prompt_interrupt.cb_name == NULL
1576 || *curbuf->b_prompt_interrupt.cb_name == NUL)
1577 return FALSE;
1578 argv[0].v_type = VAR_UNKNOWN;
1579
1580 got_int = FALSE; // don't skip executing commands
1581 call_callback(&curbuf->b_prompt_interrupt, -1, &rettv, 0, argv);
1582 clear_tv(&rettv);
1583 return TRUE;
1584}
1585
1586/*
1587 * Return the effective prompt for the specified buffer.
1588 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001589 static char_u *
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001590buf_prompt_text(buf_T* buf)
1591{
1592 if (buf->b_prompt_text == NULL)
1593 return (char_u *)"% ";
1594 return buf->b_prompt_text;
1595}
1596
1597/*
1598 * Return the effective prompt for the current buffer.
1599 */
1600 char_u *
1601prompt_text(void)
1602{
1603 return buf_prompt_text(curbuf);
1604}
1605
1606
1607/*
1608 * Prepare for prompt mode: Make sure the last line has the prompt text.
1609 * Move the cursor to this line.
1610 */
1611 void
1612init_prompt(int cmdchar_todo)
1613{
1614 char_u *prompt = prompt_text();
1615 char_u *text;
1616
1617 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1618 text = ml_get_curline();
1619 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1620 {
1621 // prompt is missing, insert it or append a line with it
1622 if (*text == NUL)
1623 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1624 else
1625 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1626 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1627 coladvance((colnr_T)MAXCOL);
1628 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1629 }
1630
1631 // Insert always starts after the prompt, allow editing text after it.
1632 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1633 || Insstart_orig.col != (int)STRLEN(prompt))
1634 set_insstart(curwin->w_cursor.lnum, (int)STRLEN(prompt));
1635
1636 if (cmdchar_todo == 'A')
1637 coladvance((colnr_T)MAXCOL);
Bram Moolenaaree8b7872020-11-19 18:46:25 +01001638 if (curwin->w_cursor.col < (int)STRLEN(prompt))
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001639 curwin->w_cursor.col = (int)STRLEN(prompt);
1640 // Make sure the cursor is in a valid position.
1641 check_cursor();
1642}
1643
1644/*
1645 * Return TRUE if the cursor is in the editable position of the prompt line.
1646 */
1647 int
1648prompt_curpos_editable()
1649{
1650 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1651 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1652}
1653
1654/*
1655 * "prompt_setcallback({buffer}, {callback})" function
1656 */
1657 void
1658f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
1659{
1660 buf_T *buf;
1661 callback_T callback;
1662
1663 if (check_secure())
1664 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02001665
1666 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
1667 return;
1668
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001669 buf = tv_get_buf(&argvars[0], FALSE);
1670 if (buf == NULL)
1671 return;
1672
1673 callback = get_callback(&argvars[1]);
1674 if (callback.cb_name == NULL)
1675 return;
1676
1677 free_callback(&buf->b_prompt_callback);
1678 set_callback(&buf->b_prompt_callback, &callback);
1679}
1680
1681/*
1682 * "prompt_setinterrupt({buffer}, {callback})" function
1683 */
1684 void
1685f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
1686{
1687 buf_T *buf;
1688 callback_T callback;
1689
1690 if (check_secure())
1691 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02001692
1693 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
1694 return;
1695
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001696 buf = tv_get_buf(&argvars[0], FALSE);
1697 if (buf == NULL)
1698 return;
1699
1700 callback = get_callback(&argvars[1]);
1701 if (callback.cb_name == NULL)
1702 return;
1703
1704 free_callback(&buf->b_prompt_interrupt);
1705 set_callback(&buf->b_prompt_interrupt, &callback);
1706}
1707
1708
1709/*
1710 * "prompt_getprompt({buffer})" function
1711 */
1712 void
1713f_prompt_getprompt(typval_T *argvars, typval_T *rettv)
1714{
1715 buf_T *buf;
1716
1717 // return an empty string by default, e.g. it's not a prompt buffer
1718 rettv->v_type = VAR_STRING;
1719 rettv->vval.v_string = NULL;
1720
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001721 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
1722 return;
1723
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001724 buf = tv_get_buf_from_arg(&argvars[0]);
1725 if (buf == NULL)
1726 return;
1727
1728 if (!bt_prompt(buf))
1729 return;
1730
1731 rettv->vval.v_string = vim_strsave(buf_prompt_text(buf));
1732}
1733
1734/*
1735 * "prompt_setprompt({buffer}, {text})" function
1736 */
1737 void
1738f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
1739{
1740 buf_T *buf;
1741 char_u *text;
1742
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02001743 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02001744 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02001745 || check_for_string_arg(argvars, 1) == FAIL))
1746 return;
1747
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001748 if (check_secure())
1749 return;
1750 buf = tv_get_buf(&argvars[0], FALSE);
1751 if (buf == NULL)
1752 return;
1753
1754 text = tv_get_string(&argvars[1]);
1755 vim_free(buf->b_prompt_text);
1756 buf->b_prompt_text = vim_strsave(text);
1757}
1758
1759/*
1760 * Get the job from the argument.
1761 * Returns NULL if the job is invalid.
1762 */
1763 static job_T *
1764get_job_arg(typval_T *tv)
1765{
1766 job_T *job;
1767
1768 if (tv->v_type != VAR_JOB)
1769 {
1770 semsg(_(e_invarg2), tv_get_string(tv));
1771 return NULL;
1772 }
1773 job = tv->vval.v_job;
1774
1775 if (job == NULL)
1776 emsg(_("E916: not a valid job"));
1777 return job;
1778}
1779
1780/*
1781 * "job_getchannel()" function
1782 */
1783 void
1784f_job_getchannel(typval_T *argvars, typval_T *rettv)
1785{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001786 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001787
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001788 if (in_vim9script() && check_for_job_arg(argvars, 0) == FAIL)
1789 return;
1790
1791 job = get_job_arg(&argvars[0]);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001792 if (job != NULL)
1793 {
1794 rettv->v_type = VAR_CHANNEL;
1795 rettv->vval.v_channel = job->jv_channel;
1796 if (job->jv_channel != NULL)
1797 ++job->jv_channel->ch_refcount;
1798 }
1799}
1800
1801/*
1802 * Implementation of job_info().
1803 */
1804 static void
1805job_info(job_T *job, dict_T *dict)
1806{
1807 dictitem_T *item;
1808 varnumber_T nr;
1809 list_T *l;
1810 int i;
1811
1812 dict_add_string(dict, "status", (char_u *)job_status(job));
1813
1814 item = dictitem_alloc((char_u *)"channel");
1815 if (item == NULL)
1816 return;
1817 item->di_tv.v_type = VAR_CHANNEL;
1818 item->di_tv.vval.v_channel = job->jv_channel;
1819 if (job->jv_channel != NULL)
1820 ++job->jv_channel->ch_refcount;
1821 if (dict_add(dict, item) == FAIL)
1822 dictitem_free(item);
1823
1824#ifdef UNIX
1825 nr = job->jv_pid;
1826#else
1827 nr = job->jv_proc_info.dwProcessId;
1828#endif
1829 dict_add_number(dict, "process", nr);
1830 dict_add_string(dict, "tty_in", job->jv_tty_in);
1831 dict_add_string(dict, "tty_out", job->jv_tty_out);
1832
1833 dict_add_number(dict, "exitval", job->jv_exitval);
1834 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
1835 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
1836#ifdef UNIX
1837 dict_add_string(dict, "termsig", job->jv_termsig);
1838#endif
1839#ifdef MSWIN
1840 dict_add_string(dict, "tty_type", job->jv_tty_type);
1841#endif
1842
1843 l = list_alloc();
1844 if (l != NULL)
1845 {
1846 dict_add_list(dict, "cmd", l);
1847 if (job->jv_argv != NULL)
1848 for (i = 0; job->jv_argv[i] != NULL; i++)
1849 list_append_string(l, (char_u *)job->jv_argv[i], -1);
1850 }
1851}
1852
1853/*
1854 * Implementation of job_info() to return info for all jobs.
1855 */
1856 static void
1857job_info_all(list_T *l)
1858{
1859 job_T *job;
1860 typval_T tv;
1861
1862 FOR_ALL_JOBS(job)
1863 {
1864 tv.v_type = VAR_JOB;
1865 tv.vval.v_job = job;
1866
1867 if (list_append_tv(l, &tv) != OK)
1868 return;
1869 }
1870}
1871
1872/*
1873 * "job_info()" function
1874 */
1875 void
1876f_job_info(typval_T *argvars, typval_T *rettv)
1877{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001878 if (in_vim9script() && check_for_opt_job_arg(argvars, 0) == FAIL)
1879 return;
1880
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001881 if (argvars[0].v_type != VAR_UNKNOWN)
1882 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001883 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001884
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001885 job = get_job_arg(&argvars[0]);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001886 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
1887 job_info(job, rettv->vval.v_dict);
1888 }
1889 else if (rettv_list_alloc(rettv) == OK)
1890 job_info_all(rettv->vval.v_list);
1891}
1892
1893/*
1894 * "job_setoptions()" function
1895 */
1896 void
1897f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1898{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001899 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001900 jobopt_T opt;
1901
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001902 if (in_vim9script()
1903 && (check_for_job_arg(argvars, 0) == FAIL
1904 || check_for_dict_arg(argvars, 1) == FAIL))
1905 return;
1906
1907 job = get_job_arg(&argvars[0]);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001908 if (job == NULL)
1909 return;
1910 clear_job_options(&opt);
1911 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
1912 job_set_options(job, &opt);
1913 free_job_options(&opt);
1914}
1915
1916/*
1917 * "job_start()" function
1918 */
1919 void
1920f_job_start(typval_T *argvars, typval_T *rettv)
1921{
1922 rettv->v_type = VAR_JOB;
1923 if (check_restricted() || check_secure())
1924 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001925
1926 if (in_vim9script()
1927 && (check_for_string_or_list_arg(argvars, 0) == FAIL
1928 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1929 return;
1930
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001931 rettv->vval.v_job = job_start(argvars, NULL, NULL, NULL);
1932}
1933
1934/*
1935 * "job_status()" function
1936 */
1937 void
1938f_job_status(typval_T *argvars, typval_T *rettv)
1939{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001940 if (in_vim9script() && check_for_job_arg(argvars, 0) == FAIL)
1941 return;
1942
Bram Moolenaar218450a2020-10-17 18:51:52 +02001943 if (argvars[0].v_type == VAR_JOB && argvars[0].vval.v_job == NULL)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001944 {
Bram Moolenaar218450a2020-10-17 18:51:52 +02001945 // A job that never started returns "fail".
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001946 rettv->v_type = VAR_STRING;
Bram Moolenaar218450a2020-10-17 18:51:52 +02001947 rettv->vval.v_string = vim_strsave((char_u *)"fail");
1948 }
1949 else
1950 {
1951 job_T *job = get_job_arg(&argvars[0]);
1952
1953 if (job != NULL)
1954 {
1955 rettv->v_type = VAR_STRING;
1956 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
1957 }
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001958 }
1959}
1960
1961/*
1962 * "job_stop()" function
1963 */
1964 void
1965f_job_stop(typval_T *argvars, typval_T *rettv)
1966{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001967 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001968
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001969 if (in_vim9script()
1970 && (check_for_job_arg(argvars, 0) == FAIL
1971 || check_for_opt_string_or_number_arg(argvars, 1) == FAIL))
1972 return;
1973
1974 job = get_job_arg(&argvars[0]);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001975 if (job != NULL)
1976 rettv->vval.v_number = job_stop(job, argvars, NULL);
1977}
1978
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001979/*
1980 * Get a string with information about the job in "varp" in "buf".
1981 * "buf" must be at least NUMBUFLEN long.
1982 */
1983 char_u *
1984job_to_string_buf(typval_T *varp, char_u *buf)
1985{
1986 job_T *job = varp->vval.v_job;
1987 char *status;
1988
1989 if (job == NULL)
Bram Moolenaar271906b2021-08-28 12:30:12 +02001990 {
1991 vim_snprintf((char *)buf, NUMBUFLEN, "no process");
1992 return buf;
1993 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001994 status = job->jv_status == JOB_FAILED ? "fail"
1995 : job->jv_status >= JOB_ENDED ? "dead"
1996 : "run";
1997# ifdef UNIX
1998 vim_snprintf((char *)buf, NUMBUFLEN,
1999 "process %ld %s", (long)job->jv_pid, status);
2000# elif defined(MSWIN)
2001 vim_snprintf((char *)buf, NUMBUFLEN,
2002 "process %ld %s",
2003 (long)job->jv_proc_info.dwProcessId,
2004 status);
2005# else
2006 // fall-back
2007 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
2008# endif
2009 return buf;
2010}
2011
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02002012#endif // FEAT_JOB_CHANNEL