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