blob: 4e64ca3c789c92f19b480be461a9d711088f4adc [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
Bram Moolenaarc96b7f52022-12-02 15:58:38 +000077 static void
78unref_job_callback(callback_T *cb)
79{
80 if (cb->cb_partial != NULL)
81 partial_unref(cb->cb_partial);
82 else if (cb->cb_name != NULL)
83 {
84 func_unref(cb->cb_name);
85 if (cb->cb_free_name)
86 vim_free(cb->cb_name);
87 }
88}
89
Bram Moolenaar8b5866d2020-09-05 15:48:51 +020090/*
91 * Free any members of a jobopt_T.
92 */
93 void
94free_job_options(jobopt_T *opt)
95{
Bram Moolenaarc96b7f52022-12-02 15:58:38 +000096 unref_job_callback(&opt->jo_callback);
97 unref_job_callback(&opt->jo_out_cb);
98 unref_job_callback(&opt->jo_err_cb);
99 unref_job_callback(&opt->jo_close_cb);
100 unref_job_callback(&opt->jo_exit_cb);
101
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200102 if (opt->jo_env != NULL)
103 dict_unref(opt->jo_env);
104}
105
106/*
107 * Get the PART_ number from the first character of an option name.
108 */
109 static int
110part_from_char(int c)
111{
112 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
113}
114
115/*
116 * Get the option entries from the dict in "tv", parse them and put the result
117 * in "opt".
118 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
119 * If an option value is invalid return FAIL.
120 */
121 int
122get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
123{
124 typval_T *item;
125 char_u *val;
126 dict_T *dict;
127 int todo;
128 hashitem_T *hi;
129 ch_part_T part;
130
131 if (tv->v_type == VAR_UNKNOWN)
132 return OK;
133 if (tv->v_type != VAR_DICT)
134 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000135 emsg(_(e_dictionary_required));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200136 return FAIL;
137 }
138 dict = tv->vval.v_dict;
139 if (dict == NULL)
140 return OK;
141
142 todo = (int)dict->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000143 FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200144 if (!HASHITEM_EMPTY(hi))
145 {
146 item = &dict_lookup(hi)->di_tv;
147
148 if (STRCMP(hi->hi_key, "mode") == 0)
149 {
150 if (!(supported & JO_MODE))
151 break;
152 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
153 return FAIL;
154 }
155 else if (STRCMP(hi->hi_key, "in_mode") == 0)
156 {
157 if (!(supported & JO_IN_MODE))
158 break;
159 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
160 == FAIL)
161 return FAIL;
162 }
163 else if (STRCMP(hi->hi_key, "out_mode") == 0)
164 {
165 if (!(supported & JO_OUT_MODE))
166 break;
167 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
168 == FAIL)
169 return FAIL;
170 }
171 else if (STRCMP(hi->hi_key, "err_mode") == 0)
172 {
173 if (!(supported & JO_ERR_MODE))
174 break;
175 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
176 == FAIL)
177 return FAIL;
178 }
179 else if (STRCMP(hi->hi_key, "noblock") == 0)
180 {
181 if (!(supported & JO_MODE))
182 break;
183 opt->jo_noblock = tv_get_bool(item);
184 }
185 else if (STRCMP(hi->hi_key, "in_io") == 0
186 || STRCMP(hi->hi_key, "out_io") == 0
187 || STRCMP(hi->hi_key, "err_io") == 0)
188 {
189 if (!(supported & JO_OUT_IO))
190 break;
191 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
192 return FAIL;
193 }
194 else if (STRCMP(hi->hi_key, "in_name") == 0
195 || STRCMP(hi->hi_key, "out_name") == 0
196 || STRCMP(hi->hi_key, "err_name") == 0)
197 {
198 part = part_from_char(*hi->hi_key);
199
200 if (!(supported & JO_OUT_IO))
201 break;
202 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
203 opt->jo_io_name[part] = tv_get_string_buf_chk(item,
204 opt->jo_io_name_buf[part]);
205 }
206 else if (STRCMP(hi->hi_key, "pty") == 0)
207 {
208 if (!(supported & JO_MODE))
209 break;
210 opt->jo_pty = tv_get_bool(item);
211 }
212 else if (STRCMP(hi->hi_key, "in_buf") == 0
213 || STRCMP(hi->hi_key, "out_buf") == 0
214 || STRCMP(hi->hi_key, "err_buf") == 0)
215 {
216 part = part_from_char(*hi->hi_key);
217
218 if (!(supported & JO_OUT_IO))
219 break;
220 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
221 opt->jo_io_buf[part] = tv_get_number(item);
222 if (opt->jo_io_buf[part] <= 0)
223 {
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100224 semsg(_(e_invalid_value_for_argument_str_str),
225 hi->hi_key, tv_get_string(item));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200226 return FAIL;
227 }
228 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
229 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000230 semsg(_(e_buffer_nr_does_not_exist),
231 (long)opt->jo_io_buf[part]);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200232 return FAIL;
233 }
234 }
235 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
236 || STRCMP(hi->hi_key, "err_modifiable") == 0)
237 {
238 part = part_from_char(*hi->hi_key);
239
240 if (!(supported & JO_OUT_IO))
241 break;
242 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
243 opt->jo_modifiable[part] = tv_get_bool(item);
244 }
245 else if (STRCMP(hi->hi_key, "out_msg") == 0
246 || STRCMP(hi->hi_key, "err_msg") == 0)
247 {
248 part = part_from_char(*hi->hi_key);
249
250 if (!(supported & JO_OUT_IO))
251 break;
252 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
253 opt->jo_message[part] = tv_get_bool(item);
254 }
255 else if (STRCMP(hi->hi_key, "in_top") == 0
256 || STRCMP(hi->hi_key, "in_bot") == 0)
257 {
258 linenr_T *lp;
259
260 if (!(supported & JO_OUT_IO))
261 break;
262 if (hi->hi_key[3] == 't')
263 {
264 lp = &opt->jo_in_top;
265 opt->jo_set |= JO_IN_TOP;
266 }
267 else
268 {
269 lp = &opt->jo_in_bot;
270 opt->jo_set |= JO_IN_BOT;
271 }
272 *lp = tv_get_number(item);
273 if (*lp < 0)
274 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000275 semsg(_(e_invalid_value_for_argument_str_str), hi->hi_key, tv_get_string(item));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200276 return FAIL;
277 }
278 }
279 else if (STRCMP(hi->hi_key, "channel") == 0)
280 {
281 if (!(supported & JO_OUT_IO))
282 break;
283 opt->jo_set |= JO_CHANNEL;
284 if (item->v_type != VAR_CHANNEL)
285 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000286 semsg(_(e_invalid_value_for_argument_str), "channel");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200287 return FAIL;
288 }
289 opt->jo_channel = item->vval.v_channel;
290 }
291 else if (STRCMP(hi->hi_key, "callback") == 0)
292 {
293 if (!(supported & JO_CALLBACK))
294 break;
295 opt->jo_set |= JO_CALLBACK;
296 opt->jo_callback = get_callback(item);
297 if (opt->jo_callback.cb_name == NULL)
298 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000299 semsg(_(e_invalid_value_for_argument_str), "callback");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200300 return FAIL;
301 }
302 }
303 else if (STRCMP(hi->hi_key, "out_cb") == 0)
304 {
305 if (!(supported & JO_OUT_CALLBACK))
306 break;
307 opt->jo_set |= JO_OUT_CALLBACK;
308 opt->jo_out_cb = get_callback(item);
309 if (opt->jo_out_cb.cb_name == NULL)
310 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000311 semsg(_(e_invalid_value_for_argument_str), "out_cb");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200312 return FAIL;
313 }
314 }
315 else if (STRCMP(hi->hi_key, "err_cb") == 0)
316 {
317 if (!(supported & JO_ERR_CALLBACK))
318 break;
319 opt->jo_set |= JO_ERR_CALLBACK;
320 opt->jo_err_cb = get_callback(item);
321 if (opt->jo_err_cb.cb_name == NULL)
322 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000323 semsg(_(e_invalid_value_for_argument_str), "err_cb");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200324 return FAIL;
325 }
326 }
327 else if (STRCMP(hi->hi_key, "close_cb") == 0)
328 {
329 if (!(supported & JO_CLOSE_CALLBACK))
330 break;
331 opt->jo_set |= JO_CLOSE_CALLBACK;
332 opt->jo_close_cb = get_callback(item);
333 if (opt->jo_close_cb.cb_name == NULL)
334 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000335 semsg(_(e_invalid_value_for_argument_str), "close_cb");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200336 return FAIL;
337 }
338 }
339 else if (STRCMP(hi->hi_key, "drop") == 0)
340 {
341 int never = FALSE;
342 val = tv_get_string(item);
343
344 if (STRCMP(val, "never") == 0)
345 never = TRUE;
346 else if (STRCMP(val, "auto") != 0)
347 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000348 semsg(_(e_invalid_value_for_argument_str_str), "drop", val);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200349 return FAIL;
350 }
351 opt->jo_drop_never = never;
352 }
353 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
354 {
355 if (!(supported & JO_EXIT_CB))
356 break;
357 opt->jo_set |= JO_EXIT_CB;
358 opt->jo_exit_cb = get_callback(item);
359 if (opt->jo_exit_cb.cb_name == NULL)
360 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000361 semsg(_(e_invalid_value_for_argument_str), "exit_cb");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200362 return FAIL;
363 }
364 }
365#ifdef FEAT_TERMINAL
366 else if (STRCMP(hi->hi_key, "term_name") == 0)
367 {
368 if (!(supported2 & JO2_TERM_NAME))
369 break;
370 opt->jo_set2 |= JO2_TERM_NAME;
371 opt->jo_term_name = tv_get_string_buf_chk(item,
372 opt->jo_term_name_buf);
373 if (opt->jo_term_name == NULL)
374 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000375 semsg(_(e_invalid_value_for_argument_str), "term_name");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200376 return FAIL;
377 }
378 }
379 else if (STRCMP(hi->hi_key, "term_finish") == 0)
380 {
381 if (!(supported2 & JO2_TERM_FINISH))
382 break;
383 val = tv_get_string(item);
384 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
385 {
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100386 semsg(_(e_invalid_value_for_argument_str_str),
387 "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{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000797 if (in_free_unref_items)
798 return;
799
800 job_free_contents(job);
801 job_free_job(job);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200802}
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
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000889job_any_running(void)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200890{
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{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001091 if (job == NULL || --job->jv_refcount > 0)
1092 return;
1093
1094 // Do not free the job if there is a channel where the close callback
1095 // may get the job info.
1096 if (job_channel_still_useful(job))
1097 return;
1098
1099 // Do not free the job when it has not ended yet and there is a
1100 // "stoponexit" flag or an exit callback.
1101 if (!job_need_end_check(job))
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001102 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001103 job_free(job);
1104 }
1105 else if (job->jv_channel != NULL)
1106 {
1107 // Do remove the link to the channel, otherwise it hangs
1108 // around until Vim exits. See job_free() for refcount.
1109 ch_log(job->jv_channel, "detaching channel from job");
1110 job->jv_channel->ch_job = NULL;
1111 channel_unref(job->jv_channel);
1112 job->jv_channel = NULL;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001113 }
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);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001161 if (job == NULL)
1162 return NULL;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001163
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001164 job->jv_refcount = 1;
1165 job->jv_stoponexit = vim_strsave((char_u *)"term");
1166
1167 if (first_job != NULL)
1168 {
1169 first_job->jv_prev = job;
1170 job->jv_next = first_job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001171 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001172 first_job = job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001173 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
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001659prompt_curpos_editable(void)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001660{
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);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00001690 if (callback.cb_free_name)
1691 vim_free(callback.cb_name);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001692}
1693
1694/*
1695 * "prompt_setinterrupt({buffer}, {callback})" function
1696 */
1697 void
1698f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
1699{
1700 buf_T *buf;
1701 callback_T callback;
1702
1703 if (check_secure())
1704 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02001705
1706 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
1707 return;
1708
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001709 buf = tv_get_buf(&argvars[0], FALSE);
1710 if (buf == NULL)
1711 return;
1712
1713 callback = get_callback(&argvars[1]);
1714 if (callback.cb_name == NULL)
1715 return;
1716
1717 free_callback(&buf->b_prompt_interrupt);
1718 set_callback(&buf->b_prompt_interrupt, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00001719 if (callback.cb_free_name)
1720 vim_free(callback.cb_name);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001721}
1722
1723
1724/*
1725 * "prompt_getprompt({buffer})" function
1726 */
1727 void
1728f_prompt_getprompt(typval_T *argvars, typval_T *rettv)
1729{
1730 buf_T *buf;
1731
1732 // return an empty string by default, e.g. it's not a prompt buffer
1733 rettv->v_type = VAR_STRING;
1734 rettv->vval.v_string = NULL;
1735
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001736 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
1737 return;
1738
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001739 buf = tv_get_buf_from_arg(&argvars[0]);
1740 if (buf == NULL)
1741 return;
1742
1743 if (!bt_prompt(buf))
1744 return;
1745
1746 rettv->vval.v_string = vim_strsave(buf_prompt_text(buf));
1747}
1748
1749/*
1750 * "prompt_setprompt({buffer}, {text})" function
1751 */
1752 void
1753f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
1754{
1755 buf_T *buf;
1756 char_u *text;
1757
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02001758 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02001759 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02001760 || check_for_string_arg(argvars, 1) == FAIL))
1761 return;
1762
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001763 if (check_secure())
1764 return;
1765 buf = tv_get_buf(&argvars[0], FALSE);
1766 if (buf == NULL)
1767 return;
1768
1769 text = tv_get_string(&argvars[1]);
1770 vim_free(buf->b_prompt_text);
1771 buf->b_prompt_text = vim_strsave(text);
1772}
1773
1774/*
1775 * Get the job from the argument.
1776 * Returns NULL if the job is invalid.
1777 */
1778 static job_T *
1779get_job_arg(typval_T *tv)
1780{
1781 job_T *job;
1782
1783 if (tv->v_type != VAR_JOB)
1784 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001785 semsg(_(e_invalid_argument_str), tv_get_string(tv));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001786 return NULL;
1787 }
1788 job = tv->vval.v_job;
1789
1790 if (job == NULL)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001791 emsg(_(e_not_valid_job));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001792 return job;
1793}
1794
1795/*
1796 * "job_getchannel()" function
1797 */
1798 void
1799f_job_getchannel(typval_T *argvars, typval_T *rettv)
1800{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001801 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001802
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001803 if (in_vim9script() && check_for_job_arg(argvars, 0) == FAIL)
1804 return;
1805
1806 job = get_job_arg(&argvars[0]);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001807 if (job == NULL)
1808 return;
1809
1810 rettv->v_type = VAR_CHANNEL;
1811 rettv->vval.v_channel = job->jv_channel;
1812 if (job->jv_channel != NULL)
1813 ++job->jv_channel->ch_refcount;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001814}
1815
1816/*
1817 * Implementation of job_info().
1818 */
1819 static void
1820job_info(job_T *job, dict_T *dict)
1821{
1822 dictitem_T *item;
1823 varnumber_T nr;
1824 list_T *l;
1825 int i;
1826
1827 dict_add_string(dict, "status", (char_u *)job_status(job));
1828
1829 item = dictitem_alloc((char_u *)"channel");
1830 if (item == NULL)
1831 return;
1832 item->di_tv.v_type = VAR_CHANNEL;
1833 item->di_tv.vval.v_channel = job->jv_channel;
1834 if (job->jv_channel != NULL)
1835 ++job->jv_channel->ch_refcount;
1836 if (dict_add(dict, item) == FAIL)
1837 dictitem_free(item);
1838
1839#ifdef UNIX
1840 nr = job->jv_pid;
1841#else
1842 nr = job->jv_proc_info.dwProcessId;
1843#endif
1844 dict_add_number(dict, "process", nr);
1845 dict_add_string(dict, "tty_in", job->jv_tty_in);
1846 dict_add_string(dict, "tty_out", job->jv_tty_out);
1847
1848 dict_add_number(dict, "exitval", job->jv_exitval);
1849 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
1850 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
1851#ifdef UNIX
1852 dict_add_string(dict, "termsig", job->jv_termsig);
1853#endif
1854#ifdef MSWIN
1855 dict_add_string(dict, "tty_type", job->jv_tty_type);
1856#endif
1857
1858 l = list_alloc();
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001859 if (l == NULL)
1860 return;
1861
1862 dict_add_list(dict, "cmd", l);
1863 if (job->jv_argv != NULL)
1864 for (i = 0; job->jv_argv[i] != NULL; i++)
1865 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001866}
1867
1868/*
1869 * Implementation of job_info() to return info for all jobs.
1870 */
1871 static void
1872job_info_all(list_T *l)
1873{
1874 job_T *job;
1875 typval_T tv;
1876
1877 FOR_ALL_JOBS(job)
1878 {
1879 tv.v_type = VAR_JOB;
1880 tv.vval.v_job = job;
1881
1882 if (list_append_tv(l, &tv) != OK)
1883 return;
1884 }
1885}
1886
1887/*
1888 * "job_info()" function
1889 */
1890 void
1891f_job_info(typval_T *argvars, typval_T *rettv)
1892{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001893 if (in_vim9script() && check_for_opt_job_arg(argvars, 0) == FAIL)
1894 return;
1895
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001896 if (argvars[0].v_type != VAR_UNKNOWN)
1897 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001898 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001899
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001900 job = get_job_arg(&argvars[0]);
Bram Moolenaar93a10962022-06-16 11:42:09 +01001901 if (job != NULL && rettv_dict_alloc(rettv) == OK)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001902 job_info(job, rettv->vval.v_dict);
1903 }
1904 else if (rettv_list_alloc(rettv) == OK)
1905 job_info_all(rettv->vval.v_list);
1906}
1907
1908/*
1909 * "job_setoptions()" function
1910 */
1911 void
1912f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1913{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001914 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001915 jobopt_T opt;
1916
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001917 if (in_vim9script()
1918 && (check_for_job_arg(argvars, 0) == FAIL
1919 || check_for_dict_arg(argvars, 1) == FAIL))
1920 return;
1921
1922 job = get_job_arg(&argvars[0]);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001923 if (job == NULL)
1924 return;
1925 clear_job_options(&opt);
1926 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
1927 job_set_options(job, &opt);
1928 free_job_options(&opt);
1929}
1930
1931/*
1932 * "job_start()" function
1933 */
1934 void
1935f_job_start(typval_T *argvars, typval_T *rettv)
1936{
1937 rettv->v_type = VAR_JOB;
1938 if (check_restricted() || check_secure())
1939 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001940
1941 if (in_vim9script()
1942 && (check_for_string_or_list_arg(argvars, 0) == FAIL
1943 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1944 return;
1945
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001946 rettv->vval.v_job = job_start(argvars, NULL, NULL, NULL);
1947}
1948
1949/*
1950 * "job_status()" function
1951 */
1952 void
1953f_job_status(typval_T *argvars, typval_T *rettv)
1954{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001955 if (in_vim9script() && check_for_job_arg(argvars, 0) == FAIL)
1956 return;
1957
Bram Moolenaar218450a2020-10-17 18:51:52 +02001958 if (argvars[0].v_type == VAR_JOB && argvars[0].vval.v_job == NULL)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001959 {
Bram Moolenaar218450a2020-10-17 18:51:52 +02001960 // A job that never started returns "fail".
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001961 rettv->v_type = VAR_STRING;
Bram Moolenaar218450a2020-10-17 18:51:52 +02001962 rettv->vval.v_string = vim_strsave((char_u *)"fail");
1963 }
1964 else
1965 {
1966 job_T *job = get_job_arg(&argvars[0]);
1967
1968 if (job != NULL)
1969 {
1970 rettv->v_type = VAR_STRING;
1971 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
1972 }
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001973 }
1974}
1975
1976/*
1977 * "job_stop()" function
1978 */
1979 void
1980f_job_stop(typval_T *argvars, typval_T *rettv)
1981{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001982 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001983
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001984 if (in_vim9script()
1985 && (check_for_job_arg(argvars, 0) == FAIL
1986 || check_for_opt_string_or_number_arg(argvars, 1) == FAIL))
1987 return;
1988
1989 job = get_job_arg(&argvars[0]);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001990 if (job != NULL)
1991 rettv->vval.v_number = job_stop(job, argvars, NULL);
1992}
1993
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001994/*
1995 * Get a string with information about the job in "varp" in "buf".
1996 * "buf" must be at least NUMBUFLEN long.
1997 */
1998 char_u *
1999job_to_string_buf(typval_T *varp, char_u *buf)
2000{
2001 job_T *job = varp->vval.v_job;
2002 char *status;
2003
2004 if (job == NULL)
Bram Moolenaar271906b2021-08-28 12:30:12 +02002005 {
2006 vim_snprintf((char *)buf, NUMBUFLEN, "no process");
2007 return buf;
2008 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02002009 status = job->jv_status == JOB_FAILED ? "fail"
2010 : job->jv_status >= JOB_ENDED ? "dead"
2011 : "run";
2012# ifdef UNIX
2013 vim_snprintf((char *)buf, NUMBUFLEN,
2014 "process %ld %s", (long)job->jv_pid, status);
2015# elif defined(MSWIN)
2016 vim_snprintf((char *)buf, NUMBUFLEN,
2017 "process %ld %s",
2018 (long)job->jv_proc_info.dwProcessId,
2019 status);
2020# else
2021 // fall-back
2022 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
2023# endif
2024 return buf;
2025}
2026
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02002027#endif // FEAT_JOB_CHANNEL