blob: 0e73b1cdbf8b16774514058b2c1a29dc30b16047 [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 {
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;
451 opt->jo_curwin = tv_get_number(item);
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
722static job_T *first_job = NULL;
723
724 static void
725job_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
764job_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
775job_free_job(job_T *job)
776{
777 job_unlink(job);
778 vim_free(job);
779}
780
781 static void
782job_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
791static 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
797job_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
805free_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
820job_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
836job_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
846job_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
855job_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
866job_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
876job_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
890#if !defined(USE_ARGV) || defined(PROTO)
891/*
892 * Escape one argument for an external command.
893 * Returns the escaped string in allocated memory. NULL when out of memory.
894 */
895 static char_u *
896win32_escape_arg(char_u *arg)
897{
898 int slen, dlen;
899 int escaping = 0;
900 int i;
901 char_u *s, *d;
902 char_u *escaped_arg;
903 int has_spaces = FALSE;
904
905 // First count the number of extra bytes required.
906 slen = (int)STRLEN(arg);
907 dlen = slen;
908 for (s = arg; *s != NUL; MB_PTR_ADV(s))
909 {
910 if (*s == '"' || *s == '\\')
911 ++dlen;
912 if (*s == ' ' || *s == '\t')
913 has_spaces = TRUE;
914 }
915
916 if (has_spaces)
917 dlen += 2;
918
919 if (dlen == slen)
920 return vim_strsave(arg);
921
922 // Allocate memory for the result and fill it.
923 escaped_arg = alloc(dlen + 1);
924 if (escaped_arg == NULL)
925 return NULL;
926 memset(escaped_arg, 0, dlen+1);
927
928 d = escaped_arg;
929
930 if (has_spaces)
931 *d++ = '"';
932
933 for (s = arg; *s != NUL;)
934 {
935 switch (*s)
936 {
937 case '"':
938 for (i = 0; i < escaping; i++)
939 *d++ = '\\';
940 escaping = 0;
941 *d++ = '\\';
942 *d++ = *s++;
943 break;
944 case '\\':
945 escaping++;
946 *d++ = *s++;
947 break;
948 default:
949 escaping = 0;
950 MB_COPY_CHAR(s, d);
951 break;
952 }
953 }
954
955 // add terminating quote and finish with a NUL
956 if (has_spaces)
957 {
958 for (i = 0; i < escaping; i++)
959 *d++ = '\\';
960 *d++ = '"';
961 }
962 *d = NUL;
963
964 return escaped_arg;
965}
966
967/*
968 * Build a command line from a list, taking care of escaping.
969 * The result is put in gap->ga_data.
970 * Returns FAIL when out of memory.
971 */
972 int
973win32_build_cmd(list_T *l, garray_T *gap)
974{
975 listitem_T *li;
976 char_u *s;
977
978 CHECK_LIST_MATERIALIZE(l);
979 FOR_ALL_LIST_ITEMS(l, li)
980 {
981 s = tv_get_string_chk(&li->li_tv);
982 if (s == NULL)
983 return FAIL;
984 s = win32_escape_arg(s);
985 if (s == NULL)
986 return FAIL;
987 ga_concat(gap, s);
988 vim_free(s);
989 if (li->li_next != NULL)
990 ga_append(gap, ' ');
991 }
992 return OK;
993}
994#endif
995
996/*
997 * NOTE: Must call job_cleanup() only once right after the status of "job"
998 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
999 * mch_detect_ended_job() returned non-NULL).
1000 * If the job is no longer used it will be removed from the list of jobs, and
1001 * deleted a bit later.
1002 */
1003 void
1004job_cleanup(job_T *job)
1005{
1006 if (job->jv_status != JOB_ENDED)
1007 return;
1008
1009 // Ready to cleanup the job.
1010 job->jv_status = JOB_FINISHED;
1011
1012 // When only channel-in is kept open, close explicitly.
1013 if (job->jv_channel != NULL)
1014 ch_close_part(job->jv_channel, PART_IN);
1015
1016 if (job->jv_exit_cb.cb_name != NULL)
1017 {
1018 typval_T argv[3];
1019 typval_T rettv;
1020
1021 // Invoke the exit callback. Make sure the refcount is > 0.
1022 ch_log(job->jv_channel, "Invoking exit callback %s",
1023 job->jv_exit_cb.cb_name);
1024 ++job->jv_refcount;
1025 argv[0].v_type = VAR_JOB;
1026 argv[0].vval.v_job = job;
1027 argv[1].v_type = VAR_NUMBER;
1028 argv[1].vval.v_number = job->jv_exitval;
1029 call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv);
1030 clear_tv(&rettv);
1031 --job->jv_refcount;
1032 channel_need_redraw = TRUE;
1033 }
1034
1035 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
1036 job->jv_channel->ch_killing = TRUE;
1037
1038 // Do not free the job in case the close callback of the associated channel
1039 // isn't invoked yet and may get information by job_info().
1040 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
1041 // The job was already unreferenced and the associated channel was
1042 // detached, now that it ended it can be freed. However, a caller might
1043 // still use it, thus free it a bit later.
1044 job_free_later(job);
1045}
1046
1047/*
1048 * Mark references in jobs that are still useful.
1049 */
1050 int
1051set_ref_in_job(int copyID)
1052{
1053 int abort = FALSE;
1054 job_T *job;
1055 typval_T tv;
1056
1057 for (job = first_job; !abort && job != NULL; job = job->jv_next)
1058 if (job_still_useful(job))
1059 {
1060 tv.v_type = VAR_JOB;
1061 tv.vval.v_job = job;
1062 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1063 }
1064 return abort;
1065}
1066
1067/*
1068 * Dereference "job". Note that after this "job" may have been freed.
1069 */
1070 void
1071job_unref(job_T *job)
1072{
1073 if (job != NULL && --job->jv_refcount <= 0)
1074 {
1075 // Do not free the job if there is a channel where the close callback
1076 // may get the job info.
1077 if (!job_channel_still_useful(job))
1078 {
1079 // Do not free the job when it has not ended yet and there is a
1080 // "stoponexit" flag or an exit callback.
1081 if (!job_need_end_check(job))
1082 {
1083 job_free(job);
1084 }
1085 else if (job->jv_channel != NULL)
1086 {
1087 // Do remove the link to the channel, otherwise it hangs
1088 // around until Vim exits. See job_free() for refcount.
1089 ch_log(job->jv_channel, "detaching channel from job");
1090 job->jv_channel->ch_job = NULL;
1091 channel_unref(job->jv_channel);
1092 job->jv_channel = NULL;
1093 }
1094 }
1095 }
1096}
1097
1098 int
1099free_unused_jobs_contents(int copyID, int mask)
1100{
1101 int did_free = FALSE;
1102 job_T *job;
1103
1104 FOR_ALL_JOBS(job)
1105 if ((job->jv_copyID & mask) != (copyID & mask)
1106 && !job_still_useful(job))
1107 {
1108 // Free the channel and ordinary items it contains, but don't
1109 // recurse into Lists, Dictionaries etc.
1110 job_free_contents(job);
1111 did_free = TRUE;
1112 }
1113 return did_free;
1114}
1115
1116 void
1117free_unused_jobs(int copyID, int mask)
1118{
1119 job_T *job;
1120 job_T *job_next;
1121
1122 for (job = first_job; job != NULL; job = job_next)
1123 {
1124 job_next = job->jv_next;
1125 if ((job->jv_copyID & mask) != (copyID & mask)
1126 && !job_still_useful(job))
1127 {
1128 // Free the job struct itself.
1129 job_free_job(job);
1130 }
1131 }
1132}
1133
1134/*
1135 * Allocate a job. Sets the refcount to one and sets options default.
1136 */
1137 job_T *
1138job_alloc(void)
1139{
1140 job_T *job;
1141
1142 job = ALLOC_CLEAR_ONE(job_T);
1143 if (job != NULL)
1144 {
1145 job->jv_refcount = 1;
1146 job->jv_stoponexit = vim_strsave((char_u *)"term");
1147
1148 if (first_job != NULL)
1149 {
1150 first_job->jv_prev = job;
1151 job->jv_next = first_job;
1152 }
1153 first_job = job;
1154 }
1155 return job;
1156}
1157
1158 void
1159job_set_options(job_T *job, jobopt_T *opt)
1160{
1161 if (opt->jo_set & JO_STOPONEXIT)
1162 {
1163 vim_free(job->jv_stoponexit);
1164 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
1165 job->jv_stoponexit = NULL;
1166 else
1167 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
1168 }
1169 if (opt->jo_set & JO_EXIT_CB)
1170 {
1171 free_callback(&job->jv_exit_cb);
1172 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
1173 {
1174 job->jv_exit_cb.cb_name = NULL;
1175 job->jv_exit_cb.cb_partial = NULL;
1176 }
1177 else
1178 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
1179 }
1180}
1181
1182/*
1183 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
1184 */
1185 void
1186job_stop_on_exit(void)
1187{
1188 job_T *job;
1189
1190 FOR_ALL_JOBS(job)
1191 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
1192 mch_signal_job(job, job->jv_stoponexit);
1193}
1194
1195/*
1196 * Return TRUE when there is any job that has an exit callback and might exit,
1197 * which means job_check_ended() should be called more often.
1198 */
1199 int
1200has_pending_job(void)
1201{
1202 job_T *job;
1203
1204 FOR_ALL_JOBS(job)
1205 // Only should check if the channel has been closed, if the channel is
1206 // open the job won't exit.
1207 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
1208 || (job->jv_status == JOB_FINISHED
1209 && job_channel_can_close(job)))
1210 return TRUE;
1211 return FALSE;
1212}
1213
1214#define MAX_CHECK_ENDED 8
1215
1216/*
1217 * Called once in a while: check if any jobs that seem useful have ended.
1218 * Returns TRUE if a job did end.
1219 */
1220 int
1221job_check_ended(void)
1222{
1223 int i;
1224 int did_end = FALSE;
1225
1226 // be quick if there are no jobs to check
1227 if (first_job == NULL)
1228 return did_end;
1229
1230 for (i = 0; i < MAX_CHECK_ENDED; ++i)
1231 {
1232 // NOTE: mch_detect_ended_job() must only return a job of which the
1233 // status was just set to JOB_ENDED.
1234 job_T *job = mch_detect_ended_job(first_job);
1235
1236 if (job == NULL)
1237 break;
1238 did_end = TRUE;
1239 job_cleanup(job); // may add "job" to jobs_to_free
1240 }
1241
1242 // Actually free jobs that were cleaned up.
1243 free_jobs_to_free_later();
1244
1245 if (channel_need_redraw)
1246 {
1247 channel_need_redraw = FALSE;
1248 redraw_after_callback(TRUE);
1249 }
1250 return did_end;
1251}
1252
1253/*
1254 * Create a job and return it. Implements job_start().
1255 * "argv_arg" is only for Unix.
1256 * When "argv_arg" is NULL then "argvars" is used.
1257 * The returned job has a refcount of one.
1258 * Returns NULL when out of memory.
1259 */
1260 job_T *
1261job_start(
1262 typval_T *argvars,
1263 char **argv_arg UNUSED,
1264 jobopt_T *opt_arg,
1265 job_T **term_job)
1266{
1267 job_T *job;
1268 char_u *cmd = NULL;
1269 char **argv = NULL;
1270 int argc = 0;
1271 int i;
1272#if defined(UNIX)
1273# define USE_ARGV
1274#else
1275 garray_T ga;
1276#endif
1277 jobopt_T opt;
1278 ch_part_T part;
1279
1280 job = job_alloc();
1281 if (job == NULL)
1282 return NULL;
1283
1284 job->jv_status = JOB_FAILED;
1285#ifndef USE_ARGV
1286 ga_init2(&ga, (int)sizeof(char*), 20);
1287#endif
1288
1289 if (opt_arg != NULL)
1290 opt = *opt_arg;
1291 else
1292 {
1293 // Default mode is NL.
1294 clear_job_options(&opt);
1295 opt.jo_mode = MODE_NL;
1296 if (get_job_options(&argvars[1], &opt,
1297 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
1298 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
1299 JO2_ENV + JO2_CWD) == FAIL)
1300 goto theend;
1301 }
1302
1303 // Check that when io is "file" that there is a file name.
1304 for (part = PART_OUT; part < PART_COUNT; ++part)
1305 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
1306 && opt.jo_io[part] == JIO_FILE
1307 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
1308 || *opt.jo_io_name[part] == NUL))
1309 {
1310 emsg(_("E920: _io file requires _name to be set"));
1311 goto theend;
1312 }
1313
1314 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
1315 {
1316 buf_T *buf = NULL;
1317
1318 // check that we can find the buffer before starting the job
1319 if (opt.jo_set & JO_IN_BUF)
1320 {
1321 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
1322 if (buf == NULL)
1323 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
1324 }
1325 else if (!(opt.jo_set & JO_IN_NAME))
1326 {
1327 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
1328 }
1329 else
1330 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
1331 if (buf == NULL)
1332 goto theend;
1333 if (buf->b_ml.ml_mfp == NULL)
1334 {
1335 char_u numbuf[NUMBUFLEN];
1336 char_u *s;
1337
1338 if (opt.jo_set & JO_IN_BUF)
1339 {
1340 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
1341 s = numbuf;
1342 }
1343 else
1344 s = opt.jo_io_name[PART_IN];
1345 semsg(_("E918: buffer must be loaded: %s"), s);
1346 goto theend;
1347 }
1348 job->jv_in_buf = buf;
1349 }
1350
1351 job_set_options(job, &opt);
1352
1353#ifdef USE_ARGV
1354 if (argv_arg != NULL)
1355 {
1356 // Make a copy of argv_arg for job->jv_argv.
1357 for (i = 0; argv_arg[i] != NULL; i++)
1358 argc++;
1359 argv = ALLOC_MULT(char *, argc + 1);
1360 if (argv == NULL)
1361 goto theend;
1362 for (i = 0; i < argc; i++)
1363 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
1364 argv[argc] = NULL;
1365 }
1366 else
1367#endif
1368 if (argvars[0].v_type == VAR_STRING)
1369 {
1370 // Command is a string.
1371 cmd = argvars[0].vval.v_string;
1372 if (cmd == NULL || *skipwhite(cmd) == NUL)
1373 {
1374 emsg(_(e_invarg));
1375 goto theend;
1376 }
1377
1378 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
1379 goto theend;
1380 }
1381 else if (argvars[0].v_type != VAR_LIST
1382 || argvars[0].vval.v_list == NULL
1383 || argvars[0].vval.v_list->lv_len < 1)
1384 {
1385 emsg(_(e_invarg));
1386 goto theend;
1387 }
1388 else
1389 {
1390 list_T *l = argvars[0].vval.v_list;
1391
1392 if (build_argv_from_list(l, &argv, &argc) == FAIL)
1393 goto theend;
1394
1395 // Empty command is invalid.
1396 if (argc == 0 || *skipwhite((char_u *)argv[0]) == NUL)
1397 {
1398 emsg(_(e_invarg));
1399 goto theend;
1400 }
1401#ifndef USE_ARGV
1402 if (win32_build_cmd(l, &ga) == FAIL)
1403 goto theend;
1404 cmd = ga.ga_data;
1405 if (cmd == NULL || *skipwhite(cmd) == NUL)
1406 {
1407 emsg(_(e_invarg));
1408 goto theend;
1409 }
1410#endif
1411 }
1412
1413 // Save the command used to start the job.
1414 job->jv_argv = argv;
1415
1416 if (term_job != NULL)
1417 *term_job = job;
1418
1419#ifdef USE_ARGV
1420 if (ch_log_active())
1421 {
1422 garray_T ga;
1423
1424 ga_init2(&ga, (int)sizeof(char), 200);
1425 for (i = 0; i < argc; ++i)
1426 {
1427 if (i > 0)
1428 ga_concat(&ga, (char_u *)" ");
1429 ga_concat(&ga, (char_u *)argv[i]);
1430 }
1431 ga_append(&ga, NUL);
1432 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
1433 ga_clear(&ga);
1434 }
1435 mch_job_start(argv, job, &opt, term_job != NULL);
1436#else
1437 ch_log(NULL, "Starting job: %s", (char *)cmd);
1438 mch_job_start((char *)cmd, job, &opt);
1439#endif
1440
1441 // If the channel is reading from a buffer, write lines now.
1442 if (job->jv_channel != NULL)
1443 channel_write_in(job->jv_channel);
1444
1445theend:
1446#ifndef USE_ARGV
1447 vim_free(ga.ga_data);
1448#endif
1449 if (argv != NULL && argv != job->jv_argv)
1450 {
1451 for (i = 0; argv[i] != NULL; i++)
1452 vim_free(argv[i]);
1453 vim_free(argv);
1454 }
1455 free_job_options(&opt);
1456 return job;
1457}
1458
1459/*
1460 * Get the status of "job" and invoke the exit callback when needed.
1461 * The returned string is not allocated.
1462 */
1463 char *
1464job_status(job_T *job)
1465{
1466 char *result;
1467
1468 if (job->jv_status >= JOB_ENDED)
1469 // No need to check, dead is dead.
1470 result = "dead";
1471 else if (job->jv_status == JOB_FAILED)
1472 result = "fail";
1473 else
1474 {
1475 result = mch_job_status(job);
1476 if (job->jv_status == JOB_ENDED)
1477 job_cleanup(job);
1478 }
1479 return result;
1480}
1481
1482/*
1483 * Send a signal to "job". Implements job_stop().
1484 * When "type" is not NULL use this for the type.
1485 * Otherwise use argvars[1] for the type.
1486 */
1487 int
1488job_stop(job_T *job, typval_T *argvars, char *type)
1489{
1490 char_u *arg;
1491
1492 if (type != NULL)
1493 arg = (char_u *)type;
1494 else if (argvars[1].v_type == VAR_UNKNOWN)
1495 arg = (char_u *)"";
1496 else
1497 {
1498 arg = tv_get_string_chk(&argvars[1]);
1499 if (arg == NULL)
1500 {
1501 emsg(_(e_invarg));
1502 return 0;
1503 }
1504 }
1505 if (job->jv_status == JOB_FAILED)
1506 {
1507 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
1508 return 0;
1509 }
1510 if (job->jv_status == JOB_ENDED)
1511 {
1512 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
1513 return 0;
1514 }
1515 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
1516 if (mch_signal_job(job, arg) == FAIL)
1517 return 0;
1518
1519 // Assume that only "kill" will kill the job.
1520 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
1521 job->jv_channel->ch_job_killed = TRUE;
1522
1523 // We don't try freeing the job, obviously the caller still has a
1524 // reference to it.
1525 return 1;
1526}
1527
1528 void
1529invoke_prompt_callback(void)
1530{
1531 typval_T rettv;
1532 typval_T argv[2];
1533 char_u *text;
1534 char_u *prompt;
1535 linenr_T lnum = curbuf->b_ml.ml_line_count;
1536
1537 // Add a new line for the prompt before invoking the callback, so that
1538 // text can always be inserted above the last line.
1539 ml_append(lnum, (char_u *)"", 0, FALSE);
1540 curwin->w_cursor.lnum = lnum + 1;
1541 curwin->w_cursor.col = 0;
1542
1543 if (curbuf->b_prompt_callback.cb_name == NULL
1544 || *curbuf->b_prompt_callback.cb_name == NUL)
1545 return;
1546 text = ml_get(lnum);
1547 prompt = prompt_text();
1548 if (STRLEN(text) >= STRLEN(prompt))
1549 text += STRLEN(prompt);
1550 argv[0].v_type = VAR_STRING;
1551 argv[0].vval.v_string = vim_strsave(text);
1552 argv[1].v_type = VAR_UNKNOWN;
1553
1554 call_callback(&curbuf->b_prompt_callback, -1, &rettv, 1, argv);
1555 clear_tv(&argv[0]);
1556 clear_tv(&rettv);
1557}
1558
1559/*
1560 * Return TRUE when the interrupt callback was invoked.
1561 */
1562 int
1563invoke_prompt_interrupt(void)
1564{
1565 typval_T rettv;
1566 typval_T argv[1];
1567
1568 if (curbuf->b_prompt_interrupt.cb_name == NULL
1569 || *curbuf->b_prompt_interrupt.cb_name == NUL)
1570 return FALSE;
1571 argv[0].v_type = VAR_UNKNOWN;
1572
1573 got_int = FALSE; // don't skip executing commands
1574 call_callback(&curbuf->b_prompt_interrupt, -1, &rettv, 0, argv);
1575 clear_tv(&rettv);
1576 return TRUE;
1577}
1578
1579/*
1580 * Return the effective prompt for the specified buffer.
1581 */
1582 char_u *
1583buf_prompt_text(buf_T* buf)
1584{
1585 if (buf->b_prompt_text == NULL)
1586 return (char_u *)"% ";
1587 return buf->b_prompt_text;
1588}
1589
1590/*
1591 * Return the effective prompt for the current buffer.
1592 */
1593 char_u *
1594prompt_text(void)
1595{
1596 return buf_prompt_text(curbuf);
1597}
1598
1599
1600/*
1601 * Prepare for prompt mode: Make sure the last line has the prompt text.
1602 * Move the cursor to this line.
1603 */
1604 void
1605init_prompt(int cmdchar_todo)
1606{
1607 char_u *prompt = prompt_text();
1608 char_u *text;
1609
1610 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1611 text = ml_get_curline();
1612 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1613 {
1614 // prompt is missing, insert it or append a line with it
1615 if (*text == NUL)
1616 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1617 else
1618 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1619 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1620 coladvance((colnr_T)MAXCOL);
1621 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1622 }
1623
1624 // Insert always starts after the prompt, allow editing text after it.
1625 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1626 || Insstart_orig.col != (int)STRLEN(prompt))
1627 set_insstart(curwin->w_cursor.lnum, (int)STRLEN(prompt));
1628
1629 if (cmdchar_todo == 'A')
1630 coladvance((colnr_T)MAXCOL);
1631 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
1632 curwin->w_cursor.col = (int)STRLEN(prompt);
1633 // Make sure the cursor is in a valid position.
1634 check_cursor();
1635}
1636
1637/*
1638 * Return TRUE if the cursor is in the editable position of the prompt line.
1639 */
1640 int
1641prompt_curpos_editable()
1642{
1643 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1644 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1645}
1646
1647/*
1648 * "prompt_setcallback({buffer}, {callback})" function
1649 */
1650 void
1651f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
1652{
1653 buf_T *buf;
1654 callback_T callback;
1655
1656 if (check_secure())
1657 return;
1658 buf = tv_get_buf(&argvars[0], FALSE);
1659 if (buf == NULL)
1660 return;
1661
1662 callback = get_callback(&argvars[1]);
1663 if (callback.cb_name == NULL)
1664 return;
1665
1666 free_callback(&buf->b_prompt_callback);
1667 set_callback(&buf->b_prompt_callback, &callback);
1668}
1669
1670/*
1671 * "prompt_setinterrupt({buffer}, {callback})" function
1672 */
1673 void
1674f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
1675{
1676 buf_T *buf;
1677 callback_T callback;
1678
1679 if (check_secure())
1680 return;
1681 buf = tv_get_buf(&argvars[0], FALSE);
1682 if (buf == NULL)
1683 return;
1684
1685 callback = get_callback(&argvars[1]);
1686 if (callback.cb_name == NULL)
1687 return;
1688
1689 free_callback(&buf->b_prompt_interrupt);
1690 set_callback(&buf->b_prompt_interrupt, &callback);
1691}
1692
1693
1694/*
1695 * "prompt_getprompt({buffer})" function
1696 */
1697 void
1698f_prompt_getprompt(typval_T *argvars, typval_T *rettv)
1699{
1700 buf_T *buf;
1701
1702 // return an empty string by default, e.g. it's not a prompt buffer
1703 rettv->v_type = VAR_STRING;
1704 rettv->vval.v_string = NULL;
1705
1706 buf = tv_get_buf_from_arg(&argvars[0]);
1707 if (buf == NULL)
1708 return;
1709
1710 if (!bt_prompt(buf))
1711 return;
1712
1713 rettv->vval.v_string = vim_strsave(buf_prompt_text(buf));
1714}
1715
1716/*
1717 * "prompt_setprompt({buffer}, {text})" function
1718 */
1719 void
1720f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
1721{
1722 buf_T *buf;
1723 char_u *text;
1724
1725 if (check_secure())
1726 return;
1727 buf = tv_get_buf(&argvars[0], FALSE);
1728 if (buf == NULL)
1729 return;
1730
1731 text = tv_get_string(&argvars[1]);
1732 vim_free(buf->b_prompt_text);
1733 buf->b_prompt_text = vim_strsave(text);
1734}
1735
1736/*
1737 * Get the job from the argument.
1738 * Returns NULL if the job is invalid.
1739 */
1740 static job_T *
1741get_job_arg(typval_T *tv)
1742{
1743 job_T *job;
1744
1745 if (tv->v_type != VAR_JOB)
1746 {
1747 semsg(_(e_invarg2), tv_get_string(tv));
1748 return NULL;
1749 }
1750 job = tv->vval.v_job;
1751
1752 if (job == NULL)
1753 emsg(_("E916: not a valid job"));
1754 return job;
1755}
1756
1757/*
1758 * "job_getchannel()" function
1759 */
1760 void
1761f_job_getchannel(typval_T *argvars, typval_T *rettv)
1762{
1763 job_T *job = get_job_arg(&argvars[0]);
1764
1765 if (job != NULL)
1766 {
1767 rettv->v_type = VAR_CHANNEL;
1768 rettv->vval.v_channel = job->jv_channel;
1769 if (job->jv_channel != NULL)
1770 ++job->jv_channel->ch_refcount;
1771 }
1772}
1773
1774/*
1775 * Implementation of job_info().
1776 */
1777 static void
1778job_info(job_T *job, dict_T *dict)
1779{
1780 dictitem_T *item;
1781 varnumber_T nr;
1782 list_T *l;
1783 int i;
1784
1785 dict_add_string(dict, "status", (char_u *)job_status(job));
1786
1787 item = dictitem_alloc((char_u *)"channel");
1788 if (item == NULL)
1789 return;
1790 item->di_tv.v_type = VAR_CHANNEL;
1791 item->di_tv.vval.v_channel = job->jv_channel;
1792 if (job->jv_channel != NULL)
1793 ++job->jv_channel->ch_refcount;
1794 if (dict_add(dict, item) == FAIL)
1795 dictitem_free(item);
1796
1797#ifdef UNIX
1798 nr = job->jv_pid;
1799#else
1800 nr = job->jv_proc_info.dwProcessId;
1801#endif
1802 dict_add_number(dict, "process", nr);
1803 dict_add_string(dict, "tty_in", job->jv_tty_in);
1804 dict_add_string(dict, "tty_out", job->jv_tty_out);
1805
1806 dict_add_number(dict, "exitval", job->jv_exitval);
1807 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
1808 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
1809#ifdef UNIX
1810 dict_add_string(dict, "termsig", job->jv_termsig);
1811#endif
1812#ifdef MSWIN
1813 dict_add_string(dict, "tty_type", job->jv_tty_type);
1814#endif
1815
1816 l = list_alloc();
1817 if (l != NULL)
1818 {
1819 dict_add_list(dict, "cmd", l);
1820 if (job->jv_argv != NULL)
1821 for (i = 0; job->jv_argv[i] != NULL; i++)
1822 list_append_string(l, (char_u *)job->jv_argv[i], -1);
1823 }
1824}
1825
1826/*
1827 * Implementation of job_info() to return info for all jobs.
1828 */
1829 static void
1830job_info_all(list_T *l)
1831{
1832 job_T *job;
1833 typval_T tv;
1834
1835 FOR_ALL_JOBS(job)
1836 {
1837 tv.v_type = VAR_JOB;
1838 tv.vval.v_job = job;
1839
1840 if (list_append_tv(l, &tv) != OK)
1841 return;
1842 }
1843}
1844
1845/*
1846 * "job_info()" function
1847 */
1848 void
1849f_job_info(typval_T *argvars, typval_T *rettv)
1850{
1851 if (argvars[0].v_type != VAR_UNKNOWN)
1852 {
1853 job_T *job = get_job_arg(&argvars[0]);
1854
1855 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
1856 job_info(job, rettv->vval.v_dict);
1857 }
1858 else if (rettv_list_alloc(rettv) == OK)
1859 job_info_all(rettv->vval.v_list);
1860}
1861
1862/*
1863 * "job_setoptions()" function
1864 */
1865 void
1866f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1867{
1868 job_T *job = get_job_arg(&argvars[0]);
1869 jobopt_T opt;
1870
1871 if (job == NULL)
1872 return;
1873 clear_job_options(&opt);
1874 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
1875 job_set_options(job, &opt);
1876 free_job_options(&opt);
1877}
1878
1879/*
1880 * "job_start()" function
1881 */
1882 void
1883f_job_start(typval_T *argvars, typval_T *rettv)
1884{
1885 rettv->v_type = VAR_JOB;
1886 if (check_restricted() || check_secure())
1887 return;
1888 rettv->vval.v_job = job_start(argvars, NULL, NULL, NULL);
1889}
1890
1891/*
1892 * "job_status()" function
1893 */
1894 void
1895f_job_status(typval_T *argvars, typval_T *rettv)
1896{
1897 job_T *job = get_job_arg(&argvars[0]);
1898
1899 if (job != NULL)
1900 {
1901 rettv->v_type = VAR_STRING;
1902 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
1903 }
1904}
1905
1906/*
1907 * "job_stop()" function
1908 */
1909 void
1910f_job_stop(typval_T *argvars, typval_T *rettv)
1911{
1912 job_T *job = get_job_arg(&argvars[0]);
1913
1914 if (job != NULL)
1915 rettv->vval.v_number = job_stop(job, argvars, NULL);
1916}
1917
1918#endif // FEAT_JOB_CHANNEL