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