blob: 9d6c8628ba50156578e6e1284df3221656fd0c39 [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 {
Kenta Satoc28e7a22023-05-08 18:26:03 +0100275 semsg(_(e_invalid_value_for_argument_str_str),
276 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 Moolenaarf39d9e92023-04-22 22:54:40 +0100387 semsg(_(e_invalid_value_for_argument_str_str),
388 "term_finish", val);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200389 return FAIL;
390 }
391 opt->jo_set2 |= JO2_TERM_FINISH;
392 opt->jo_term_finish = *val;
393 }
394 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
395 {
396 char_u *p;
397
398 if (!(supported2 & JO2_TERM_OPENCMD))
399 break;
400 opt->jo_set2 |= JO2_TERM_OPENCMD;
401 p = opt->jo_term_opencmd = tv_get_string_buf_chk(item,
402 opt->jo_term_opencmd_buf);
403 if (p != NULL)
404 {
405 // Must have %d and no other %.
406 p = vim_strchr(p, '%');
407 if (p != NULL && (p[1] != 'd'
408 || vim_strchr(p + 2, '%') != NULL))
409 p = NULL;
410 }
411 if (p == NULL)
412 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000413 semsg(_(e_invalid_value_for_argument_str), "term_opencmd");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200414 return FAIL;
415 }
416 }
417 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
418 {
419 if (!(supported2 & JO2_EOF_CHARS))
420 break;
421 opt->jo_set2 |= JO2_EOF_CHARS;
422 opt->jo_eof_chars = tv_get_string_buf_chk(item,
423 opt->jo_eof_chars_buf);
424 if (opt->jo_eof_chars == NULL)
425 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000426 semsg(_(e_invalid_value_for_argument_str), "eof_chars");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200427 return FAIL;
428 }
429 }
430 else if (STRCMP(hi->hi_key, "term_rows") == 0)
431 {
Bram Moolenaar88137392021-11-12 16:01:15 +0000432 int error = FALSE;
433
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200434 if (!(supported2 & JO2_TERM_ROWS))
435 break;
436 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaar88137392021-11-12 16:01:15 +0000437 opt->jo_term_rows = tv_get_number_chk(item, &error);
438 if (error)
439 return FAIL;
Bram Moolenaar5300be62021-11-13 10:27:40 +0000440 if (opt->jo_term_rows < 0 || opt->jo_term_rows > 1000)
441 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000442 semsg(_(e_invalid_value_for_argument_str), "term_rows");
Bram Moolenaar5300be62021-11-13 10:27:40 +0000443 return FAIL;
444 }
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200445 }
446 else if (STRCMP(hi->hi_key, "term_cols") == 0)
447 {
Kenta Satoc28e7a22023-05-08 18:26:03 +0100448 int error = FALSE;
449
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200450 if (!(supported2 & JO2_TERM_COLS))
451 break;
452 opt->jo_set2 |= JO2_TERM_COLS;
Kenta Satoc28e7a22023-05-08 18:26:03 +0100453 opt->jo_term_cols = tv_get_number_chk(item, &error);
454 if (error)
455 return FAIL;
456 if (opt->jo_term_cols < 0 || opt->jo_term_cols > 1000)
457 {
458 semsg(_(e_invalid_value_for_argument_str), "term_cols");
459 return FAIL;
460 }
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200461 }
462 else if (STRCMP(hi->hi_key, "vertical") == 0)
463 {
464 if (!(supported2 & JO2_VERTICAL))
465 break;
466 opt->jo_set2 |= JO2_VERTICAL;
467 opt->jo_vertical = tv_get_bool(item);
468 }
469 else if (STRCMP(hi->hi_key, "curwin") == 0)
470 {
471 if (!(supported2 & JO2_CURWIN))
472 break;
473 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaarad304702020-09-06 18:22:53 +0200474 opt->jo_curwin = tv_get_bool(item);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200475 }
476 else if (STRCMP(hi->hi_key, "bufnr") == 0)
477 {
478 int nr;
479
480 if (!(supported2 & JO2_CURWIN))
481 break;
482 opt->jo_set2 |= JO2_BUFNR;
483 nr = tv_get_number(item);
484 if (nr <= 0)
485 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000486 semsg(_(e_invalid_value_for_argument_str_str), hi->hi_key, tv_get_string(item));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200487 return FAIL;
488 }
489 opt->jo_bufnr_buf = buflist_findnr(nr);
490 if (opt->jo_bufnr_buf == NULL)
491 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000492 semsg(_(e_buffer_nr_does_not_exist), (long)nr);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200493 return FAIL;
494 }
495 if (opt->jo_bufnr_buf->b_nwindows == 0
496 || opt->jo_bufnr_buf->b_term == NULL)
497 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000498 semsg(_(e_invalid_argument_str), "bufnr");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200499 return FAIL;
500 }
501 }
502 else if (STRCMP(hi->hi_key, "hidden") == 0)
503 {
504 if (!(supported2 & JO2_HIDDEN))
505 break;
506 opt->jo_set2 |= JO2_HIDDEN;
507 opt->jo_hidden = tv_get_bool(item);
508 }
509 else if (STRCMP(hi->hi_key, "norestore") == 0)
510 {
511 if (!(supported2 & JO2_NORESTORE))
512 break;
513 opt->jo_set2 |= JO2_NORESTORE;
514 opt->jo_term_norestore = tv_get_bool(item);
515 }
516 else if (STRCMP(hi->hi_key, "term_kill") == 0)
517 {
518 if (!(supported2 & JO2_TERM_KILL))
519 break;
520 opt->jo_set2 |= JO2_TERM_KILL;
521 opt->jo_term_kill = tv_get_string_buf_chk(item,
522 opt->jo_term_kill_buf);
523 if (opt->jo_term_kill == NULL)
524 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000525 semsg(_(e_invalid_value_for_argument_str), "term_kill");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200526 return FAIL;
527 }
528 }
529 else if (STRCMP(hi->hi_key, "tty_type") == 0)
530 {
531 char_u *p;
532
533 if (!(supported2 & JO2_TTY_TYPE))
534 break;
535 opt->jo_set2 |= JO2_TTY_TYPE;
536 p = tv_get_string_chk(item);
537 if (p == NULL)
538 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000539 semsg(_(e_invalid_value_for_argument_str), "tty_type");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200540 return FAIL;
541 }
542 // Allow empty string, "winpty", "conpty".
543 if (!(*p == NUL || STRCMP(p, "winpty") == 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100544 || STRCMP(p, "conpty") == 0))
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200545 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000546 semsg(_(e_invalid_value_for_argument_str), "tty_type");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200547 return FAIL;
548 }
549 opt->jo_tty_type = p[0];
550 }
551# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
552 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
553 {
554 int n = 0;
555 listitem_T *li;
556 long_u rgb[16];
557
558 if (!(supported2 & JO2_ANSI_COLORS))
559 break;
560
561 if (item == NULL || item->v_type != VAR_LIST
LemonBoyb2b3acb2022-05-20 10:10:34 +0100562 || item->vval.v_list == NULL
563 || item->vval.v_list->lv_first == &range_list_item)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200564 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000565 semsg(_(e_invalid_value_for_argument_str), "ansi_colors");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200566 return FAIL;
567 }
568
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200569 li = item->vval.v_list->lv_first;
570 for (; li != NULL && n < 16; li = li->li_next, n++)
571 {
572 char_u *color_name;
573 guicolor_T guicolor;
574 int called_emsg_before = called_emsg;
575
576 color_name = tv_get_string_chk(&li->li_tv);
577 if (color_name == NULL)
578 return FAIL;
579
580 guicolor = GUI_GET_COLOR(color_name);
581 if (guicolor == INVALCOLOR)
582 {
583 if (called_emsg_before == called_emsg)
584 // may not get the error if the GUI didn't start
Drew Vogele30d1022021-10-24 20:35:07 +0100585 semsg(_(e_cannot_allocate_color_str), color_name);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200586 return FAIL;
587 }
588
589 rgb[n] = GUI_MCH_GET_RGB(guicolor);
590 }
591
592 if (n != 16 || li != NULL)
593 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000594 semsg(_(e_invalid_value_for_argument_str), "ansi_colors");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200595 return FAIL;
596 }
597
598 opt->jo_set2 |= JO2_ANSI_COLORS;
599 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
600 }
601# endif
602 else if (STRCMP(hi->hi_key, "term_highlight") == 0)
603 {
604 char_u *p;
605
606 if (!(supported2 & JO2_TERM_HIGHLIGHT))
607 break;
608 opt->jo_set2 |= JO2_TERM_HIGHLIGHT;
609 p = tv_get_string_buf_chk(item, opt->jo_term_highlight_buf);
610 if (p == NULL || *p == NUL)
611 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000612 semsg(_(e_invalid_value_for_argument_str), "term_highlight");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200613 return FAIL;
614 }
615 opt->jo_term_highlight = p;
616 }
617 else if (STRCMP(hi->hi_key, "term_api") == 0)
618 {
619 if (!(supported2 & JO2_TERM_API))
620 break;
621 opt->jo_set2 |= JO2_TERM_API;
622 opt->jo_term_api = tv_get_string_buf_chk(item,
623 opt->jo_term_api_buf);
624 if (opt->jo_term_api == NULL)
625 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000626 semsg(_(e_invalid_value_for_argument_str), "term_api");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200627 return FAIL;
628 }
629 }
630#endif
631 else if (STRCMP(hi->hi_key, "env") == 0)
632 {
633 if (!(supported2 & JO2_ENV))
634 break;
635 if (item->v_type != VAR_DICT)
636 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000637 semsg(_(e_invalid_value_for_argument_str), "env");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200638 return FAIL;
639 }
640 opt->jo_set2 |= JO2_ENV;
641 opt->jo_env = item->vval.v_dict;
642 if (opt->jo_env != NULL)
643 ++opt->jo_env->dv_refcount;
644 }
645 else if (STRCMP(hi->hi_key, "cwd") == 0)
646 {
647 if (!(supported2 & JO2_CWD))
648 break;
649 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
650 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
651#ifndef MSWIN // Win32 directories don't have the concept of "executable"
652 || mch_access((char *)opt->jo_cwd, X_OK) != 0
653#endif
654 )
655 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000656 semsg(_(e_invalid_value_for_argument_str), "cwd");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200657 return FAIL;
658 }
659 opt->jo_set2 |= JO2_CWD;
660 }
661 else if (STRCMP(hi->hi_key, "waittime") == 0)
662 {
663 if (!(supported & JO_WAITTIME))
664 break;
665 opt->jo_set |= JO_WAITTIME;
666 opt->jo_waittime = tv_get_number(item);
667 }
668 else if (STRCMP(hi->hi_key, "timeout") == 0)
669 {
670 if (!(supported & JO_TIMEOUT))
671 break;
672 opt->jo_set |= JO_TIMEOUT;
673 opt->jo_timeout = tv_get_number(item);
674 }
675 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
676 {
677 if (!(supported & JO_OUT_TIMEOUT))
678 break;
679 opt->jo_set |= JO_OUT_TIMEOUT;
680 opt->jo_out_timeout = tv_get_number(item);
681 }
682 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
683 {
684 if (!(supported & JO_ERR_TIMEOUT))
685 break;
686 opt->jo_set |= JO_ERR_TIMEOUT;
687 opt->jo_err_timeout = tv_get_number(item);
688 }
689 else if (STRCMP(hi->hi_key, "part") == 0)
690 {
691 if (!(supported & JO_PART))
692 break;
693 opt->jo_set |= JO_PART;
694 val = tv_get_string(item);
695 if (STRCMP(val, "err") == 0)
696 opt->jo_part = PART_ERR;
697 else if (STRCMP(val, "out") == 0)
698 opt->jo_part = PART_OUT;
699 else
700 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000701 semsg(_(e_invalid_value_for_argument_str_str), "part", val);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200702 return FAIL;
703 }
704 }
705 else if (STRCMP(hi->hi_key, "id") == 0)
706 {
707 if (!(supported & JO_ID))
708 break;
709 opt->jo_set |= JO_ID;
710 opt->jo_id = tv_get_number(item);
711 }
712 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
713 {
714 if (!(supported & JO_STOPONEXIT))
715 break;
716 opt->jo_set |= JO_STOPONEXIT;
717 opt->jo_stoponexit = tv_get_string_buf_chk(item,
718 opt->jo_stoponexit_buf);
719 if (opt->jo_stoponexit == NULL)
720 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000721 semsg(_(e_invalid_value_for_argument_str), "stoponexit");
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200722 return FAIL;
723 }
724 }
725 else if (STRCMP(hi->hi_key, "block_write") == 0)
726 {
727 if (!(supported & JO_BLOCK_WRITE))
728 break;
729 opt->jo_set |= JO_BLOCK_WRITE;
730 opt->jo_block_write = tv_get_number(item);
731 }
732 else
733 break;
734 --todo;
735 }
736 if (todo > 0)
737 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000738 semsg(_(e_invalid_argument_str), hi->hi_key);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200739 return FAIL;
740 }
741
742 return OK;
743}
744
745static job_T *first_job = NULL;
746
747 static void
748job_free_contents(job_T *job)
749{
750 int i;
751
752 ch_log(job->jv_channel, "Freeing job");
753 if (job->jv_channel != NULL)
754 {
755 // The link from the channel to the job doesn't count as a reference,
756 // thus don't decrement the refcount of the job. The reference from
757 // the job to the channel does count the reference, decrement it and
758 // NULL the reference. We don't set ch_job_killed, unreferencing the
759 // job doesn't mean it stops running.
760 job->jv_channel->ch_job = NULL;
761 channel_unref(job->jv_channel);
762 }
763 mch_clear_job(job);
764
765 vim_free(job->jv_tty_in);
766 vim_free(job->jv_tty_out);
767 vim_free(job->jv_stoponexit);
768#ifdef UNIX
769 vim_free(job->jv_termsig);
770#endif
771#ifdef MSWIN
772 vim_free(job->jv_tty_type);
773#endif
774 free_callback(&job->jv_exit_cb);
775 if (job->jv_argv != NULL)
776 {
777 for (i = 0; job->jv_argv[i] != NULL; i++)
778 vim_free(job->jv_argv[i]);
779 vim_free(job->jv_argv);
780 }
781}
782
783/*
784 * Remove "job" from the list of jobs.
785 */
786 static void
787job_unlink(job_T *job)
788{
789 if (job->jv_next != NULL)
790 job->jv_next->jv_prev = job->jv_prev;
791 if (job->jv_prev == NULL)
792 first_job = job->jv_next;
793 else
794 job->jv_prev->jv_next = job->jv_next;
795}
796
797 static void
798job_free_job(job_T *job)
799{
800 job_unlink(job);
801 vim_free(job);
802}
803
804 static void
805job_free(job_T *job)
806{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000807 if (in_free_unref_items)
808 return;
809
810 job_free_contents(job);
811 job_free_job(job);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200812}
813
814static job_T *jobs_to_free = NULL;
815
816/*
817 * Put "job" in a list to be freed later, when it's no longer referenced.
818 */
819 static void
820job_free_later(job_T *job)
821{
822 job_unlink(job);
823 job->jv_next = jobs_to_free;
824 jobs_to_free = job;
825}
826
827 static void
828free_jobs_to_free_later(void)
829{
830 job_T *job;
831
832 while (jobs_to_free != NULL)
833 {
834 job = jobs_to_free;
835 jobs_to_free = job->jv_next;
836 job_free_contents(job);
837 vim_free(job);
838 }
839}
840
841#if defined(EXITFREE) || defined(PROTO)
842 void
843job_free_all(void)
844{
845 while (first_job != NULL)
846 job_free(first_job);
847 free_jobs_to_free_later();
848
849# ifdef FEAT_TERMINAL
850 free_unused_terminals();
851# endif
852}
853#endif
854
855/*
856 * Return TRUE if we need to check if the process of "job" has ended.
857 */
858 static int
859job_need_end_check(job_T *job)
860{
861 return job->jv_status == JOB_STARTED
862 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
863}
864
865/*
866 * Return TRUE if the channel of "job" is still useful.
867 */
868 static int
869job_channel_still_useful(job_T *job)
870{
871 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
872}
873
874/*
875 * Return TRUE if the channel of "job" is closeable.
876 */
877 static int
878job_channel_can_close(job_T *job)
879{
880 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
881}
882
883/*
884 * Return TRUE if the job should not be freed yet. Do not free the job when
885 * it has not ended yet and there is a "stoponexit" flag, an exit callback
886 * or when the associated channel will do something with the job output.
887 */
888 static int
889job_still_useful(job_T *job)
890{
891 return job_need_end_check(job) || job_channel_still_useful(job);
892}
893
894#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
895/*
896 * Return TRUE when there is any running job that we care about.
897 */
898 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000899job_any_running(void)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200900{
901 job_T *job;
902
903 FOR_ALL_JOBS(job)
904 if (job_still_useful(job))
905 {
906 ch_log(NULL, "GUI not forking because a job is running");
907 return TRUE;
908 }
909 return FALSE;
910}
911#endif
912
Bram Moolenaarf46bf522020-12-09 13:16:13 +0100913// Unix uses argv[] for the command, other systems use a string.
914#if defined(UNIX)
915# define USE_ARGV
916#endif
917
Bram Moolenaar8b5866d2020-09-05 15:48:51 +0200918#if !defined(USE_ARGV) || defined(PROTO)
919/*
920 * Escape one argument for an external command.
921 * Returns the escaped string in allocated memory. NULL when out of memory.
922 */
923 static char_u *
924win32_escape_arg(char_u *arg)
925{
926 int slen, dlen;
927 int escaping = 0;
928 int i;
929 char_u *s, *d;
930 char_u *escaped_arg;
931 int has_spaces = FALSE;
932
933 // First count the number of extra bytes required.
934 slen = (int)STRLEN(arg);
935 dlen = slen;
936 for (s = arg; *s != NUL; MB_PTR_ADV(s))
937 {
938 if (*s == '"' || *s == '\\')
939 ++dlen;
940 if (*s == ' ' || *s == '\t')
941 has_spaces = TRUE;
942 }
943
944 if (has_spaces)
945 dlen += 2;
946
947 if (dlen == slen)
948 return vim_strsave(arg);
949
950 // Allocate memory for the result and fill it.
951 escaped_arg = alloc(dlen + 1);
952 if (escaped_arg == NULL)
953 return NULL;
954 memset(escaped_arg, 0, dlen+1);
955
956 d = escaped_arg;
957
958 if (has_spaces)
959 *d++ = '"';
960
961 for (s = arg; *s != NUL;)
962 {
963 switch (*s)
964 {
965 case '"':
966 for (i = 0; i < escaping; i++)
967 *d++ = '\\';
968 escaping = 0;
969 *d++ = '\\';
970 *d++ = *s++;
971 break;
972 case '\\':
973 escaping++;
974 *d++ = *s++;
975 break;
976 default:
977 escaping = 0;
978 MB_COPY_CHAR(s, d);
979 break;
980 }
981 }
982
983 // add terminating quote and finish with a NUL
984 if (has_spaces)
985 {
986 for (i = 0; i < escaping; i++)
987 *d++ = '\\';
988 *d++ = '"';
989 }
990 *d = NUL;
991
992 return escaped_arg;
993}
994
995/*
996 * Build a command line from a list, taking care of escaping.
997 * The result is put in gap->ga_data.
998 * Returns FAIL when out of memory.
999 */
1000 int
1001win32_build_cmd(list_T *l, garray_T *gap)
1002{
1003 listitem_T *li;
1004 char_u *s;
1005
1006 CHECK_LIST_MATERIALIZE(l);
1007 FOR_ALL_LIST_ITEMS(l, li)
1008 {
1009 s = tv_get_string_chk(&li->li_tv);
1010 if (s == NULL)
1011 return FAIL;
1012 s = win32_escape_arg(s);
1013 if (s == NULL)
1014 return FAIL;
1015 ga_concat(gap, s);
1016 vim_free(s);
1017 if (li->li_next != NULL)
1018 ga_append(gap, ' ');
1019 }
1020 return OK;
1021}
1022#endif
1023
1024/*
1025 * NOTE: Must call job_cleanup() only once right after the status of "job"
1026 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
1027 * mch_detect_ended_job() returned non-NULL).
1028 * If the job is no longer used it will be removed from the list of jobs, and
1029 * deleted a bit later.
1030 */
1031 void
1032job_cleanup(job_T *job)
1033{
1034 if (job->jv_status != JOB_ENDED)
1035 return;
1036
1037 // Ready to cleanup the job.
1038 job->jv_status = JOB_FINISHED;
1039
1040 // When only channel-in is kept open, close explicitly.
1041 if (job->jv_channel != NULL)
1042 ch_close_part(job->jv_channel, PART_IN);
1043
1044 if (job->jv_exit_cb.cb_name != NULL)
1045 {
1046 typval_T argv[3];
1047 typval_T rettv;
1048
1049 // Invoke the exit callback. Make sure the refcount is > 0.
1050 ch_log(job->jv_channel, "Invoking exit callback %s",
1051 job->jv_exit_cb.cb_name);
1052 ++job->jv_refcount;
1053 argv[0].v_type = VAR_JOB;
1054 argv[0].vval.v_job = job;
1055 argv[1].v_type = VAR_NUMBER;
1056 argv[1].vval.v_number = job->jv_exitval;
1057 call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv);
1058 clear_tv(&rettv);
1059 --job->jv_refcount;
1060 channel_need_redraw = TRUE;
1061 }
1062
1063 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
1064 job->jv_channel->ch_killing = TRUE;
1065
1066 // Do not free the job in case the close callback of the associated channel
1067 // isn't invoked yet and may get information by job_info().
1068 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
1069 // The job was already unreferenced and the associated channel was
1070 // detached, now that it ended it can be freed. However, a caller might
1071 // still use it, thus free it a bit later.
1072 job_free_later(job);
1073}
1074
1075/*
1076 * Mark references in jobs that are still useful.
1077 */
1078 int
1079set_ref_in_job(int copyID)
1080{
1081 int abort = FALSE;
1082 job_T *job;
1083 typval_T tv;
1084
1085 for (job = first_job; !abort && job != NULL; job = job->jv_next)
1086 if (job_still_useful(job))
1087 {
1088 tv.v_type = VAR_JOB;
1089 tv.vval.v_job = job;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001090 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL, NULL);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001091 }
1092 return abort;
1093}
1094
1095/*
1096 * Dereference "job". Note that after this "job" may have been freed.
1097 */
1098 void
1099job_unref(job_T *job)
1100{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001101 if (job == NULL || --job->jv_refcount > 0)
1102 return;
1103
1104 // Do not free the job if there is a channel where the close callback
1105 // may get the job info.
1106 if (job_channel_still_useful(job))
1107 return;
1108
1109 // Do not free the job when it has not ended yet and there is a
1110 // "stoponexit" flag or an exit callback.
1111 if (!job_need_end_check(job))
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001112 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001113 job_free(job);
1114 }
1115 else if (job->jv_channel != NULL)
1116 {
1117 // Do remove the link to the channel, otherwise it hangs
1118 // around until Vim exits. See job_free() for refcount.
1119 ch_log(job->jv_channel, "detaching channel from job");
1120 job->jv_channel->ch_job = NULL;
1121 channel_unref(job->jv_channel);
1122 job->jv_channel = NULL;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001123 }
1124}
1125
1126 int
1127free_unused_jobs_contents(int copyID, int mask)
1128{
1129 int did_free = FALSE;
1130 job_T *job;
1131
1132 FOR_ALL_JOBS(job)
1133 if ((job->jv_copyID & mask) != (copyID & mask)
1134 && !job_still_useful(job))
1135 {
1136 // Free the channel and ordinary items it contains, but don't
1137 // recurse into Lists, Dictionaries etc.
1138 job_free_contents(job);
1139 did_free = TRUE;
1140 }
1141 return did_free;
1142}
1143
1144 void
1145free_unused_jobs(int copyID, int mask)
1146{
1147 job_T *job;
1148 job_T *job_next;
1149
1150 for (job = first_job; job != NULL; job = job_next)
1151 {
1152 job_next = job->jv_next;
1153 if ((job->jv_copyID & mask) != (copyID & mask)
1154 && !job_still_useful(job))
1155 {
1156 // Free the job struct itself.
1157 job_free_job(job);
1158 }
1159 }
1160}
1161
1162/*
1163 * Allocate a job. Sets the refcount to one and sets options default.
1164 */
1165 job_T *
1166job_alloc(void)
1167{
1168 job_T *job;
1169
1170 job = ALLOC_CLEAR_ONE(job_T);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001171 if (job == NULL)
1172 return NULL;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001173
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001174 job->jv_refcount = 1;
1175 job->jv_stoponexit = vim_strsave((char_u *)"term");
1176
1177 if (first_job != NULL)
1178 {
1179 first_job->jv_prev = job;
1180 job->jv_next = first_job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001181 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001182 first_job = job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001183 return job;
1184}
1185
1186 void
1187job_set_options(job_T *job, jobopt_T *opt)
1188{
1189 if (opt->jo_set & JO_STOPONEXIT)
1190 {
1191 vim_free(job->jv_stoponexit);
1192 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
1193 job->jv_stoponexit = NULL;
1194 else
1195 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
1196 }
1197 if (opt->jo_set & JO_EXIT_CB)
1198 {
1199 free_callback(&job->jv_exit_cb);
1200 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
1201 {
1202 job->jv_exit_cb.cb_name = NULL;
1203 job->jv_exit_cb.cb_partial = NULL;
1204 }
1205 else
1206 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
1207 }
1208}
1209
1210/*
1211 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
1212 */
1213 void
1214job_stop_on_exit(void)
1215{
1216 job_T *job;
1217
1218 FOR_ALL_JOBS(job)
1219 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
1220 mch_signal_job(job, job->jv_stoponexit);
1221}
1222
1223/*
1224 * Return TRUE when there is any job that has an exit callback and might exit,
1225 * which means job_check_ended() should be called more often.
1226 */
1227 int
1228has_pending_job(void)
1229{
1230 job_T *job;
1231
1232 FOR_ALL_JOBS(job)
1233 // Only should check if the channel has been closed, if the channel is
1234 // open the job won't exit.
1235 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
1236 || (job->jv_status == JOB_FINISHED
1237 && job_channel_can_close(job)))
1238 return TRUE;
1239 return FALSE;
1240}
1241
1242#define MAX_CHECK_ENDED 8
1243
1244/*
1245 * Called once in a while: check if any jobs that seem useful have ended.
1246 * Returns TRUE if a job did end.
1247 */
1248 int
1249job_check_ended(void)
1250{
1251 int i;
1252 int did_end = FALSE;
1253
1254 // be quick if there are no jobs to check
1255 if (first_job == NULL)
1256 return did_end;
1257
1258 for (i = 0; i < MAX_CHECK_ENDED; ++i)
1259 {
1260 // NOTE: mch_detect_ended_job() must only return a job of which the
1261 // status was just set to JOB_ENDED.
1262 job_T *job = mch_detect_ended_job(first_job);
1263
1264 if (job == NULL)
1265 break;
1266 did_end = TRUE;
1267 job_cleanup(job); // may add "job" to jobs_to_free
1268 }
1269
1270 // Actually free jobs that were cleaned up.
1271 free_jobs_to_free_later();
1272
1273 if (channel_need_redraw)
1274 {
1275 channel_need_redraw = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00001276 redraw_after_callback(TRUE, FALSE);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001277 }
1278 return did_end;
1279}
1280
1281/*
1282 * Create a job and return it. Implements job_start().
1283 * "argv_arg" is only for Unix.
1284 * When "argv_arg" is NULL then "argvars" is used.
1285 * The returned job has a refcount of one.
1286 * Returns NULL when out of memory.
1287 */
1288 job_T *
1289job_start(
1290 typval_T *argvars,
1291 char **argv_arg UNUSED,
1292 jobopt_T *opt_arg,
1293 job_T **term_job)
1294{
1295 job_T *job;
1296 char_u *cmd = NULL;
1297 char **argv = NULL;
1298 int argc = 0;
1299 int i;
Bram Moolenaarf46bf522020-12-09 13:16:13 +01001300#ifndef USE_ARGV
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001301 garray_T ga;
1302#endif
1303 jobopt_T opt;
1304 ch_part_T part;
1305
1306 job = job_alloc();
1307 if (job == NULL)
1308 return NULL;
1309
1310 job->jv_status = JOB_FAILED;
1311#ifndef USE_ARGV
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001312 ga_init2(&ga, sizeof(char*), 20);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001313#endif
1314
1315 if (opt_arg != NULL)
1316 opt = *opt_arg;
1317 else
1318 {
1319 // Default mode is NL.
1320 clear_job_options(&opt);
Bram Moolenaarac4174e2022-05-07 16:38:24 +01001321 opt.jo_mode = CH_MODE_NL;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001322 if (get_job_options(&argvars[1], &opt,
1323 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
1324 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
1325 JO2_ENV + JO2_CWD) == FAIL)
1326 goto theend;
1327 }
1328
1329 // Check that when io is "file" that there is a file name.
1330 for (part = PART_OUT; part < PART_COUNT; ++part)
1331 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
1332 && opt.jo_io[part] == JIO_FILE
1333 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
1334 || *opt.jo_io_name[part] == NUL))
1335 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001336 emsg(_(e_io_file_requires_name_to_be_set));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001337 goto theend;
1338 }
1339
1340 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
1341 {
1342 buf_T *buf = NULL;
1343
1344 // check that we can find the buffer before starting the job
1345 if (opt.jo_set & JO_IN_BUF)
1346 {
1347 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
1348 if (buf == NULL)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001349 semsg(_(e_buffer_nr_does_not_exist),
1350 (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001351 }
1352 else if (!(opt.jo_set & JO_IN_NAME))
1353 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001354 emsg(_(e_in_io_buffer_requires_in_buf_or_in_name_to_be_set));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001355 }
1356 else
1357 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
1358 if (buf == NULL)
1359 goto theend;
1360 if (buf->b_ml.ml_mfp == NULL)
1361 {
1362 char_u numbuf[NUMBUFLEN];
1363 char_u *s;
1364
1365 if (opt.jo_set & JO_IN_BUF)
1366 {
1367 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
1368 s = numbuf;
1369 }
1370 else
1371 s = opt.jo_io_name[PART_IN];
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001372 semsg(_(e_buffer_must_be_loaded_str), s);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001373 goto theend;
1374 }
1375 job->jv_in_buf = buf;
1376 }
1377
1378 job_set_options(job, &opt);
1379
1380#ifdef USE_ARGV
1381 if (argv_arg != NULL)
1382 {
1383 // Make a copy of argv_arg for job->jv_argv.
1384 for (i = 0; argv_arg[i] != NULL; i++)
1385 argc++;
1386 argv = ALLOC_MULT(char *, argc + 1);
1387 if (argv == NULL)
1388 goto theend;
1389 for (i = 0; i < argc; i++)
1390 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
1391 argv[argc] = NULL;
1392 }
1393 else
1394#endif
1395 if (argvars[0].v_type == VAR_STRING)
1396 {
1397 // Command is a string.
1398 cmd = argvars[0].vval.v_string;
1399 if (cmd == NULL || *skipwhite(cmd) == NUL)
1400 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001401 emsg(_(e_invalid_argument));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001402 goto theend;
1403 }
1404
1405 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
1406 goto theend;
1407 }
1408 else if (argvars[0].v_type != VAR_LIST
1409 || argvars[0].vval.v_list == NULL
1410 || argvars[0].vval.v_list->lv_len < 1)
1411 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001412 emsg(_(e_invalid_argument));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001413 goto theend;
1414 }
1415 else
1416 {
1417 list_T *l = argvars[0].vval.v_list;
1418
1419 if (build_argv_from_list(l, &argv, &argc) == FAIL)
1420 goto theend;
1421
1422 // Empty command is invalid.
1423 if (argc == 0 || *skipwhite((char_u *)argv[0]) == NUL)
1424 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001425 emsg(_(e_invalid_argument));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001426 goto theend;
1427 }
1428#ifndef USE_ARGV
1429 if (win32_build_cmd(l, &ga) == FAIL)
1430 goto theend;
1431 cmd = ga.ga_data;
1432 if (cmd == NULL || *skipwhite(cmd) == NUL)
1433 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001434 emsg(_(e_invalid_argument));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001435 goto theend;
1436 }
1437#endif
1438 }
1439
1440 // Save the command used to start the job.
1441 job->jv_argv = argv;
1442
1443 if (term_job != NULL)
1444 *term_job = job;
1445
1446#ifdef USE_ARGV
1447 if (ch_log_active())
1448 {
1449 garray_T ga;
1450
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001451 ga_init2(&ga, sizeof(char), 200);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001452 for (i = 0; i < argc; ++i)
1453 {
1454 if (i > 0)
1455 ga_concat(&ga, (char_u *)" ");
1456 ga_concat(&ga, (char_u *)argv[i]);
1457 }
1458 ga_append(&ga, NUL);
1459 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
1460 ga_clear(&ga);
1461 }
1462 mch_job_start(argv, job, &opt, term_job != NULL);
1463#else
1464 ch_log(NULL, "Starting job: %s", (char *)cmd);
1465 mch_job_start((char *)cmd, job, &opt);
1466#endif
1467
1468 // If the channel is reading from a buffer, write lines now.
1469 if (job->jv_channel != NULL)
1470 channel_write_in(job->jv_channel);
1471
1472theend:
1473#ifndef USE_ARGV
1474 vim_free(ga.ga_data);
1475#endif
1476 if (argv != NULL && argv != job->jv_argv)
1477 {
1478 for (i = 0; argv[i] != NULL; i++)
1479 vim_free(argv[i]);
1480 vim_free(argv);
1481 }
1482 free_job_options(&opt);
1483 return job;
1484}
1485
1486/*
1487 * Get the status of "job" and invoke the exit callback when needed.
1488 * The returned string is not allocated.
1489 */
1490 char *
1491job_status(job_T *job)
1492{
1493 char *result;
1494
1495 if (job->jv_status >= JOB_ENDED)
1496 // No need to check, dead is dead.
1497 result = "dead";
1498 else if (job->jv_status == JOB_FAILED)
1499 result = "fail";
1500 else
1501 {
1502 result = mch_job_status(job);
1503 if (job->jv_status == JOB_ENDED)
1504 job_cleanup(job);
1505 }
1506 return result;
1507}
1508
1509/*
1510 * Send a signal to "job". Implements job_stop().
1511 * When "type" is not NULL use this for the type.
1512 * Otherwise use argvars[1] for the type.
1513 */
1514 int
1515job_stop(job_T *job, typval_T *argvars, char *type)
1516{
1517 char_u *arg;
1518
1519 if (type != NULL)
1520 arg = (char_u *)type;
1521 else if (argvars[1].v_type == VAR_UNKNOWN)
1522 arg = (char_u *)"";
1523 else
1524 {
1525 arg = tv_get_string_chk(&argvars[1]);
1526 if (arg == NULL)
1527 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001528 emsg(_(e_invalid_argument));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001529 return 0;
1530 }
1531 }
1532 if (job->jv_status == JOB_FAILED)
1533 {
1534 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
1535 return 0;
1536 }
1537 if (job->jv_status == JOB_ENDED)
1538 {
1539 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
1540 return 0;
1541 }
1542 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
1543 if (mch_signal_job(job, arg) == FAIL)
1544 return 0;
1545
1546 // Assume that only "kill" will kill the job.
1547 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
1548 job->jv_channel->ch_job_killed = TRUE;
1549
1550 // We don't try freeing the job, obviously the caller still has a
1551 // reference to it.
1552 return 1;
1553}
1554
1555 void
1556invoke_prompt_callback(void)
1557{
1558 typval_T rettv;
1559 typval_T argv[2];
1560 char_u *text;
1561 char_u *prompt;
1562 linenr_T lnum = curbuf->b_ml.ml_line_count;
1563
1564 // Add a new line for the prompt before invoking the callback, so that
1565 // text can always be inserted above the last line.
1566 ml_append(lnum, (char_u *)"", 0, FALSE);
1567 curwin->w_cursor.lnum = lnum + 1;
1568 curwin->w_cursor.col = 0;
1569
1570 if (curbuf->b_prompt_callback.cb_name == NULL
1571 || *curbuf->b_prompt_callback.cb_name == NUL)
1572 return;
1573 text = ml_get(lnum);
1574 prompt = prompt_text();
1575 if (STRLEN(text) >= STRLEN(prompt))
1576 text += STRLEN(prompt);
1577 argv[0].v_type = VAR_STRING;
1578 argv[0].vval.v_string = vim_strsave(text);
1579 argv[1].v_type = VAR_UNKNOWN;
1580
1581 call_callback(&curbuf->b_prompt_callback, -1, &rettv, 1, argv);
1582 clear_tv(&argv[0]);
1583 clear_tv(&rettv);
1584}
1585
1586/*
1587 * Return TRUE when the interrupt callback was invoked.
1588 */
1589 int
1590invoke_prompt_interrupt(void)
1591{
1592 typval_T rettv;
1593 typval_T argv[1];
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001594 int ret;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001595
1596 if (curbuf->b_prompt_interrupt.cb_name == NULL
1597 || *curbuf->b_prompt_interrupt.cb_name == NUL)
1598 return FALSE;
1599 argv[0].v_type = VAR_UNKNOWN;
1600
1601 got_int = FALSE; // don't skip executing commands
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001602 ret = call_callback(&curbuf->b_prompt_interrupt, -1, &rettv, 0, argv);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001603 clear_tv(&rettv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001604 return ret == FAIL ? FALSE : TRUE;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001605}
1606
1607/*
1608 * Return the effective prompt for the specified buffer.
1609 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001610 static char_u *
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001611buf_prompt_text(buf_T* buf)
1612{
1613 if (buf->b_prompt_text == NULL)
1614 return (char_u *)"% ";
1615 return buf->b_prompt_text;
1616}
1617
1618/*
1619 * Return the effective prompt for the current buffer.
1620 */
1621 char_u *
1622prompt_text(void)
1623{
1624 return buf_prompt_text(curbuf);
1625}
1626
1627
1628/*
1629 * Prepare for prompt mode: Make sure the last line has the prompt text.
1630 * Move the cursor to this line.
1631 */
1632 void
1633init_prompt(int cmdchar_todo)
1634{
1635 char_u *prompt = prompt_text();
1636 char_u *text;
1637
1638 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1639 text = ml_get_curline();
1640 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1641 {
1642 // prompt is missing, insert it or append a line with it
1643 if (*text == NUL)
1644 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1645 else
1646 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1647 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1648 coladvance((colnr_T)MAXCOL);
1649 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1650 }
1651
1652 // Insert always starts after the prompt, allow editing text after it.
1653 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1654 || Insstart_orig.col != (int)STRLEN(prompt))
1655 set_insstart(curwin->w_cursor.lnum, (int)STRLEN(prompt));
1656
1657 if (cmdchar_todo == 'A')
1658 coladvance((colnr_T)MAXCOL);
Bram Moolenaaree8b7872020-11-19 18:46:25 +01001659 if (curwin->w_cursor.col < (int)STRLEN(prompt))
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001660 curwin->w_cursor.col = (int)STRLEN(prompt);
1661 // Make sure the cursor is in a valid position.
1662 check_cursor();
1663}
1664
1665/*
1666 * Return TRUE if the cursor is in the editable position of the prompt line.
1667 */
1668 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001669prompt_curpos_editable(void)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001670{
1671 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1672 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1673}
1674
1675/*
1676 * "prompt_setcallback({buffer}, {callback})" function
1677 */
1678 void
1679f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
1680{
1681 buf_T *buf;
1682 callback_T callback;
1683
1684 if (check_secure())
1685 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02001686
1687 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
1688 return;
1689
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001690 buf = tv_get_buf(&argvars[0], FALSE);
1691 if (buf == NULL)
1692 return;
1693
1694 callback = get_callback(&argvars[1]);
1695 if (callback.cb_name == NULL)
1696 return;
1697
1698 free_callback(&buf->b_prompt_callback);
1699 set_callback(&buf->b_prompt_callback, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00001700 if (callback.cb_free_name)
1701 vim_free(callback.cb_name);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001702}
1703
1704/*
1705 * "prompt_setinterrupt({buffer}, {callback})" function
1706 */
1707 void
1708f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
1709{
1710 buf_T *buf;
1711 callback_T callback;
1712
1713 if (check_secure())
1714 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02001715
1716 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
1717 return;
1718
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001719 buf = tv_get_buf(&argvars[0], FALSE);
1720 if (buf == NULL)
1721 return;
1722
1723 callback = get_callback(&argvars[1]);
1724 if (callback.cb_name == NULL)
1725 return;
1726
1727 free_callback(&buf->b_prompt_interrupt);
1728 set_callback(&buf->b_prompt_interrupt, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00001729 if (callback.cb_free_name)
1730 vim_free(callback.cb_name);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001731}
1732
1733
1734/*
1735 * "prompt_getprompt({buffer})" function
1736 */
1737 void
1738f_prompt_getprompt(typval_T *argvars, typval_T *rettv)
1739{
1740 buf_T *buf;
1741
1742 // return an empty string by default, e.g. it's not a prompt buffer
1743 rettv->v_type = VAR_STRING;
1744 rettv->vval.v_string = NULL;
1745
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001746 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
1747 return;
1748
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001749 buf = tv_get_buf_from_arg(&argvars[0]);
1750 if (buf == NULL)
1751 return;
1752
1753 if (!bt_prompt(buf))
1754 return;
1755
1756 rettv->vval.v_string = vim_strsave(buf_prompt_text(buf));
1757}
1758
1759/*
1760 * "prompt_setprompt({buffer}, {text})" function
1761 */
1762 void
1763f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
1764{
1765 buf_T *buf;
1766 char_u *text;
1767
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02001768 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02001769 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02001770 || check_for_string_arg(argvars, 1) == FAIL))
1771 return;
1772
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001773 if (check_secure())
1774 return;
1775 buf = tv_get_buf(&argvars[0], FALSE);
1776 if (buf == NULL)
1777 return;
1778
1779 text = tv_get_string(&argvars[1]);
1780 vim_free(buf->b_prompt_text);
1781 buf->b_prompt_text = vim_strsave(text);
1782}
1783
1784/*
1785 * Get the job from the argument.
1786 * Returns NULL if the job is invalid.
1787 */
1788 static job_T *
1789get_job_arg(typval_T *tv)
1790{
1791 job_T *job;
1792
1793 if (tv->v_type != VAR_JOB)
1794 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001795 semsg(_(e_invalid_argument_str), tv_get_string(tv));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001796 return NULL;
1797 }
1798 job = tv->vval.v_job;
1799
1800 if (job == NULL)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001801 emsg(_(e_not_valid_job));
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001802 return job;
1803}
1804
1805/*
1806 * "job_getchannel()" function
1807 */
1808 void
1809f_job_getchannel(typval_T *argvars, typval_T *rettv)
1810{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001811 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001812
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001813 if (in_vim9script() && check_for_job_arg(argvars, 0) == FAIL)
1814 return;
1815
1816 job = get_job_arg(&argvars[0]);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001817 if (job == NULL)
1818 return;
1819
1820 rettv->v_type = VAR_CHANNEL;
1821 rettv->vval.v_channel = job->jv_channel;
1822 if (job->jv_channel != NULL)
1823 ++job->jv_channel->ch_refcount;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001824}
1825
1826/*
1827 * Implementation of job_info().
1828 */
1829 static void
1830job_info(job_T *job, dict_T *dict)
1831{
1832 dictitem_T *item;
1833 varnumber_T nr;
1834 list_T *l;
1835 int i;
1836
1837 dict_add_string(dict, "status", (char_u *)job_status(job));
1838
1839 item = dictitem_alloc((char_u *)"channel");
1840 if (item == NULL)
1841 return;
1842 item->di_tv.v_type = VAR_CHANNEL;
1843 item->di_tv.vval.v_channel = job->jv_channel;
1844 if (job->jv_channel != NULL)
1845 ++job->jv_channel->ch_refcount;
1846 if (dict_add(dict, item) == FAIL)
1847 dictitem_free(item);
1848
1849#ifdef UNIX
1850 nr = job->jv_pid;
1851#else
1852 nr = job->jv_proc_info.dwProcessId;
1853#endif
1854 dict_add_number(dict, "process", nr);
1855 dict_add_string(dict, "tty_in", job->jv_tty_in);
1856 dict_add_string(dict, "tty_out", job->jv_tty_out);
1857
1858 dict_add_number(dict, "exitval", job->jv_exitval);
1859 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
1860 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
1861#ifdef UNIX
1862 dict_add_string(dict, "termsig", job->jv_termsig);
1863#endif
1864#ifdef MSWIN
1865 dict_add_string(dict, "tty_type", job->jv_tty_type);
1866#endif
1867
1868 l = list_alloc();
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001869 if (l == NULL)
1870 return;
1871
1872 dict_add_list(dict, "cmd", l);
1873 if (job->jv_argv != NULL)
1874 for (i = 0; job->jv_argv[i] != NULL; i++)
1875 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001876}
1877
1878/*
1879 * Implementation of job_info() to return info for all jobs.
1880 */
1881 static void
1882job_info_all(list_T *l)
1883{
1884 job_T *job;
1885 typval_T tv;
1886
1887 FOR_ALL_JOBS(job)
1888 {
1889 tv.v_type = VAR_JOB;
1890 tv.vval.v_job = job;
1891
1892 if (list_append_tv(l, &tv) != OK)
1893 return;
1894 }
1895}
1896
1897/*
1898 * "job_info()" function
1899 */
1900 void
1901f_job_info(typval_T *argvars, typval_T *rettv)
1902{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001903 if (in_vim9script() && check_for_opt_job_arg(argvars, 0) == FAIL)
1904 return;
1905
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001906 if (argvars[0].v_type != VAR_UNKNOWN)
1907 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001908 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001909
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001910 job = get_job_arg(&argvars[0]);
Bram Moolenaar93a10962022-06-16 11:42:09 +01001911 if (job != NULL && rettv_dict_alloc(rettv) == OK)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001912 job_info(job, rettv->vval.v_dict);
1913 }
1914 else if (rettv_list_alloc(rettv) == OK)
1915 job_info_all(rettv->vval.v_list);
1916}
1917
1918/*
1919 * "job_setoptions()" function
1920 */
1921 void
1922f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1923{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001924 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001925 jobopt_T opt;
1926
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001927 if (in_vim9script()
1928 && (check_for_job_arg(argvars, 0) == FAIL
1929 || check_for_dict_arg(argvars, 1) == FAIL))
1930 return;
1931
1932 job = get_job_arg(&argvars[0]);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001933 if (job == NULL)
1934 return;
1935 clear_job_options(&opt);
1936 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
1937 job_set_options(job, &opt);
1938 free_job_options(&opt);
1939}
1940
1941/*
1942 * "job_start()" function
1943 */
1944 void
1945f_job_start(typval_T *argvars, typval_T *rettv)
1946{
1947 rettv->v_type = VAR_JOB;
1948 if (check_restricted() || check_secure())
1949 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001950
1951 if (in_vim9script()
1952 && (check_for_string_or_list_arg(argvars, 0) == FAIL
1953 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1954 return;
1955
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001956 rettv->vval.v_job = job_start(argvars, NULL, NULL, NULL);
1957}
1958
1959/*
1960 * "job_status()" function
1961 */
1962 void
1963f_job_status(typval_T *argvars, typval_T *rettv)
1964{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001965 if (in_vim9script() && check_for_job_arg(argvars, 0) == FAIL)
1966 return;
1967
Bram Moolenaar218450a2020-10-17 18:51:52 +02001968 if (argvars[0].v_type == VAR_JOB && argvars[0].vval.v_job == NULL)
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001969 {
Bram Moolenaar218450a2020-10-17 18:51:52 +02001970 // A job that never started returns "fail".
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001971 rettv->v_type = VAR_STRING;
Bram Moolenaar218450a2020-10-17 18:51:52 +02001972 rettv->vval.v_string = vim_strsave((char_u *)"fail");
1973 }
1974 else
1975 {
1976 job_T *job = get_job_arg(&argvars[0]);
1977
1978 if (job != NULL)
1979 {
1980 rettv->v_type = VAR_STRING;
1981 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
1982 }
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001983 }
1984}
1985
1986/*
1987 * "job_stop()" function
1988 */
1989 void
1990f_job_stop(typval_T *argvars, typval_T *rettv)
1991{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001992 job_T *job;
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001993
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001994 if (in_vim9script()
1995 && (check_for_job_arg(argvars, 0) == FAIL
1996 || check_for_opt_string_or_number_arg(argvars, 1) == FAIL))
1997 return;
1998
1999 job = get_job_arg(&argvars[0]);
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02002000 if (job != NULL)
2001 rettv->vval.v_number = job_stop(job, argvars, NULL);
2002}
2003
Bram Moolenaar1328bde2021-06-05 20:51:38 +02002004/*
2005 * Get a string with information about the job in "varp" in "buf".
2006 * "buf" must be at least NUMBUFLEN long.
2007 */
2008 char_u *
2009job_to_string_buf(typval_T *varp, char_u *buf)
2010{
2011 job_T *job = varp->vval.v_job;
2012 char *status;
2013
2014 if (job == NULL)
Bram Moolenaar271906b2021-08-28 12:30:12 +02002015 {
2016 vim_snprintf((char *)buf, NUMBUFLEN, "no process");
2017 return buf;
2018 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02002019 status = job->jv_status == JOB_FAILED ? "fail"
2020 : job->jv_status >= JOB_ENDED ? "dead"
2021 : "run";
2022# ifdef UNIX
2023 vim_snprintf((char *)buf, NUMBUFLEN,
2024 "process %ld %s", (long)job->jv_pid, status);
2025# elif defined(MSWIN)
2026 vim_snprintf((char *)buf, NUMBUFLEN,
2027 "process %ld %s",
2028 (long)job->jv_proc_info.dwProcessId,
2029 status);
2030# else
2031 // fall-back
2032 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
2033# endif
2034 return buf;
2035}
2036
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02002037#endif // FEAT_JOB_CHANNEL