diff options
author | Bram Moolenaar <Bram@vim.org> | 2020-09-05 15:48:51 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2020-09-05 15:48:51 +0200 |
commit | 8b5866ded6036f7adece26b6d16962bbd2d47842 (patch) | |
tree | a473e6759fd5a5dc6436a6502c0d51ddb077805a /src/channel.c | |
parent | 7dfc5ce7cf4a7f63370d7dce2e13f7410ca0230a (diff) |
patch 8.2.1597: the channel source file is too bigv8.2.1597
Problem: The channel source file is too big.
Solution: Move job related code to a new source file.
Diffstat (limited to 'src/channel.c')
-rw-r--r-- | src/channel.c | 1845 |
1 files changed, 4 insertions, 1841 deletions
diff --git a/src/channel.c b/src/channel.c index 5bf561e220..2959139f56 100644 --- a/src/channel.c +++ b/src/channel.c @@ -66,17 +66,10 @@ static ch_mode_T channel_get_mode(channel_T *channel, ch_part_T part); static int channel_get_timeout(channel_T *channel, ch_part_T part); static ch_part_T channel_part_send(channel_T *channel); static ch_part_T channel_part_read(channel_T *channel); -static void free_job_options(jobopt_T *opt); #define FOR_ALL_CHANNELS(ch) \ for ((ch) = first_channel; (ch) != NULL; (ch) = (ch)->ch_next) -#define FOR_ALL_JOBS(job) \ - for ((job) = first_job; (job) != NULL; (job) = (job)->jv_next) - -// Whether a redraw is needed for appending a line to a buffer. -static int channel_need_redraw = FALSE; - // Whether we are inside channel_parse_messages() or another situation where it // is safe to invoke callbacks. static int safe_to_invoke_callback = 0; @@ -361,7 +354,7 @@ has_any_channel(void) * Return TRUE if "channel" has a callback and the associated job wasn't * killed. */ - static int + int channel_still_useful(channel_T *channel) { int has_sock_msg; @@ -404,7 +397,7 @@ channel_still_useful(channel_T *channel) /* * Return TRUE if "channel" is closeable (i.e. all readable fds are closed). */ - static int + int channel_can_close(channel_T *channel) { return channel->ch_to_be_closed == 0; @@ -1386,7 +1379,7 @@ theend: return channel; } - static void + void ch_close_part(channel_T *channel, ch_part_T part) { sock_T *fd = &channel->ch_part[part].ch_fd; @@ -1625,7 +1618,7 @@ can_write_buf_line(channel_T *channel) /* * Write any buffer lines to the input channel. */ - static void + void channel_write_in(channel_T *channel) { chanpart_T *in_part = &channel->ch_part[PART_IN]; @@ -4763,1654 +4756,6 @@ channel_get_timeout(channel_T *channel, ch_part_T part) return channel->ch_part[part].ch_timeout; } - static int -handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo) -{ - char_u *val = tv_get_string(item); - - opt->jo_set |= jo; - if (STRCMP(val, "nl") == 0) - *modep = MODE_NL; - else if (STRCMP(val, "raw") == 0) - *modep = MODE_RAW; - else if (STRCMP(val, "js") == 0) - *modep = MODE_JS; - else if (STRCMP(val, "json") == 0) - *modep = MODE_JSON; - else - { - semsg(_(e_invarg2), val); - return FAIL; - } - return OK; -} - - static int -handle_io(typval_T *item, ch_part_T part, jobopt_T *opt) -{ - char_u *val = tv_get_string(item); - - opt->jo_set |= JO_OUT_IO << (part - PART_OUT); - if (STRCMP(val, "null") == 0) - opt->jo_io[part] = JIO_NULL; - else if (STRCMP(val, "pipe") == 0) - opt->jo_io[part] = JIO_PIPE; - else if (STRCMP(val, "file") == 0) - opt->jo_io[part] = JIO_FILE; - else if (STRCMP(val, "buffer") == 0) - opt->jo_io[part] = JIO_BUFFER; - else if (STRCMP(val, "out") == 0 && part == PART_ERR) - opt->jo_io[part] = JIO_OUT; - else - { - semsg(_(e_invarg2), val); - return FAIL; - } - return OK; -} - -/* - * Clear a jobopt_T before using it. - */ - void -clear_job_options(jobopt_T *opt) -{ - CLEAR_POINTER(opt); -} - -/* - * Free any members of a jobopt_T. - */ - static void -free_job_options(jobopt_T *opt) -{ - if (opt->jo_callback.cb_partial != NULL) - partial_unref(opt->jo_callback.cb_partial); - else if (opt->jo_callback.cb_name != NULL) - func_unref(opt->jo_callback.cb_name); - if (opt->jo_out_cb.cb_partial != NULL) - partial_unref(opt->jo_out_cb.cb_partial); - else if (opt->jo_out_cb.cb_name != NULL) - func_unref(opt->jo_out_cb.cb_name); - if (opt->jo_err_cb.cb_partial != NULL) - partial_unref(opt->jo_err_cb.cb_partial); - else if (opt->jo_err_cb.cb_name != NULL) - func_unref(opt->jo_err_cb.cb_name); - if (opt->jo_close_cb.cb_partial != NULL) - partial_unref(opt->jo_close_cb.cb_partial); - else if (opt->jo_close_cb.cb_name != NULL) - func_unref(opt->jo_close_cb.cb_name); - if (opt->jo_exit_cb.cb_partial != NULL) - partial_unref(opt->jo_exit_cb.cb_partial); - else if (opt->jo_exit_cb.cb_name != NULL) - func_unref(opt->jo_exit_cb.cb_name); - if (opt->jo_env != NULL) - dict_unref(opt->jo_env); -} - -/* - * Get the PART_ number from the first character of an option name. - */ - static int -part_from_char(int c) -{ - return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR; -} - -/* - * Get the option entries from the dict in "tv", parse them and put the result - * in "opt". - * Only accept JO_ options in "supported" and JO2_ options in "supported2". - * If an option value is invalid return FAIL. - */ - int -get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2) -{ - typval_T *item; - char_u *val; - dict_T *dict; - int todo; - hashitem_T *hi; - ch_part_T part; - - if (tv->v_type == VAR_UNKNOWN) - return OK; - if (tv->v_type != VAR_DICT) - { - emsg(_(e_dictreq)); - return FAIL; - } - dict = tv->vval.v_dict; - if (dict == NULL) - return OK; - - todo = (int)dict->dv_hashtab.ht_used; - for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) - if (!HASHITEM_EMPTY(hi)) - { - item = &dict_lookup(hi)->di_tv; - - if (STRCMP(hi->hi_key, "mode") == 0) - { - if (!(supported & JO_MODE)) - break; - if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL) - return FAIL; - } - else if (STRCMP(hi->hi_key, "in_mode") == 0) - { - if (!(supported & JO_IN_MODE)) - break; - if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE) - == FAIL) - return FAIL; - } - else if (STRCMP(hi->hi_key, "out_mode") == 0) - { - if (!(supported & JO_OUT_MODE)) - break; - if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE) - == FAIL) - return FAIL; - } - else if (STRCMP(hi->hi_key, "err_mode") == 0) - { - if (!(supported & JO_ERR_MODE)) - break; - if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE) - == FAIL) - return FAIL; - } - else if (STRCMP(hi->hi_key, "noblock") == 0) - { - if (!(supported & JO_MODE)) - break; - opt->jo_noblock = tv_get_bool(item); - } - else if (STRCMP(hi->hi_key, "in_io") == 0 - || STRCMP(hi->hi_key, "out_io") == 0 - || STRCMP(hi->hi_key, "err_io") == 0) - { - if (!(supported & JO_OUT_IO)) - break; - if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL) - return FAIL; - } - else if (STRCMP(hi->hi_key, "in_name") == 0 - || STRCMP(hi->hi_key, "out_name") == 0 - || STRCMP(hi->hi_key, "err_name") == 0) - { - part = part_from_char(*hi->hi_key); - - if (!(supported & JO_OUT_IO)) - break; - opt->jo_set |= JO_OUT_NAME << (part - PART_OUT); - opt->jo_io_name[part] = tv_get_string_buf_chk(item, - opt->jo_io_name_buf[part]); - } - else if (STRCMP(hi->hi_key, "pty") == 0) - { - if (!(supported & JO_MODE)) - break; - opt->jo_pty = tv_get_bool(item); - } - else if (STRCMP(hi->hi_key, "in_buf") == 0 - || STRCMP(hi->hi_key, "out_buf") == 0 - || STRCMP(hi->hi_key, "err_buf") == 0) - { - part = part_from_char(*hi->hi_key); - - if (!(supported & JO_OUT_IO)) - break; - opt->jo_set |= JO_OUT_BUF << (part - PART_OUT); - opt->jo_io_buf[part] = tv_get_number(item); - if (opt->jo_io_buf[part] <= 0) - { - semsg(_(e_invargNval), hi->hi_key, tv_get_string(item)); - return FAIL; - } - if (buflist_findnr(opt->jo_io_buf[part]) == NULL) - { - semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "out_modifiable") == 0 - || STRCMP(hi->hi_key, "err_modifiable") == 0) - { - part = part_from_char(*hi->hi_key); - - if (!(supported & JO_OUT_IO)) - break; - opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT); - opt->jo_modifiable[part] = tv_get_bool(item); - } - else if (STRCMP(hi->hi_key, "out_msg") == 0 - || STRCMP(hi->hi_key, "err_msg") == 0) - { - part = part_from_char(*hi->hi_key); - - if (!(supported & JO_OUT_IO)) - break; - opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT); - opt->jo_message[part] = tv_get_bool(item); - } - else if (STRCMP(hi->hi_key, "in_top") == 0 - || STRCMP(hi->hi_key, "in_bot") == 0) - { - linenr_T *lp; - - if (!(supported & JO_OUT_IO)) - break; - if (hi->hi_key[3] == 't') - { - lp = &opt->jo_in_top; - opt->jo_set |= JO_IN_TOP; - } - else - { - lp = &opt->jo_in_bot; - opt->jo_set |= JO_IN_BOT; - } - *lp = tv_get_number(item); - if (*lp < 0) - { - semsg(_(e_invargNval), hi->hi_key, tv_get_string(item)); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "channel") == 0) - { - if (!(supported & JO_OUT_IO)) - break; - opt->jo_set |= JO_CHANNEL; - if (item->v_type != VAR_CHANNEL) - { - semsg(_(e_invargval), "channel"); - return FAIL; - } - opt->jo_channel = item->vval.v_channel; - } - else if (STRCMP(hi->hi_key, "callback") == 0) - { - if (!(supported & JO_CALLBACK)) - break; - opt->jo_set |= JO_CALLBACK; - opt->jo_callback = get_callback(item); - if (opt->jo_callback.cb_name == NULL) - { - semsg(_(e_invargval), "callback"); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "out_cb") == 0) - { - if (!(supported & JO_OUT_CALLBACK)) - break; - opt->jo_set |= JO_OUT_CALLBACK; - opt->jo_out_cb = get_callback(item); - if (opt->jo_out_cb.cb_name == NULL) - { - semsg(_(e_invargval), "out_cb"); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "err_cb") == 0) - { - if (!(supported & JO_ERR_CALLBACK)) - break; - opt->jo_set |= JO_ERR_CALLBACK; - opt->jo_err_cb = get_callback(item); - if (opt->jo_err_cb.cb_name == NULL) - { - semsg(_(e_invargval), "err_cb"); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "close_cb") == 0) - { - if (!(supported & JO_CLOSE_CALLBACK)) - break; - opt->jo_set |= JO_CLOSE_CALLBACK; - opt->jo_close_cb = get_callback(item); - if (opt->jo_close_cb.cb_name == NULL) - { - semsg(_(e_invargval), "close_cb"); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "drop") == 0) - { - int never = FALSE; - val = tv_get_string(item); - - if (STRCMP(val, "never") == 0) - never = TRUE; - else if (STRCMP(val, "auto") != 0) - { - semsg(_(e_invargNval), "drop", val); - return FAIL; - } - opt->jo_drop_never = never; - } - else if (STRCMP(hi->hi_key, "exit_cb") == 0) - { - if (!(supported & JO_EXIT_CB)) - break; - opt->jo_set |= JO_EXIT_CB; - opt->jo_exit_cb = get_callback(item); - if (opt->jo_exit_cb.cb_name == NULL) - { - semsg(_(e_invargval), "exit_cb"); - return FAIL; - } - } -#ifdef FEAT_TERMINAL - else if (STRCMP(hi->hi_key, "term_name") == 0) - { - if (!(supported2 & JO2_TERM_NAME)) - break; - opt->jo_set2 |= JO2_TERM_NAME; - opt->jo_term_name = tv_get_string_buf_chk(item, - opt->jo_term_name_buf); - if (opt->jo_term_name == NULL) - { - semsg(_(e_invargval), "term_name"); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "term_finish") == 0) - { - if (!(supported2 & JO2_TERM_FINISH)) - break; - val = tv_get_string(item); - if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0) - { - semsg(_(e_invargNval), "term_finish", val); - return FAIL; - } - opt->jo_set2 |= JO2_TERM_FINISH; - opt->jo_term_finish = *val; - } - else if (STRCMP(hi->hi_key, "term_opencmd") == 0) - { - char_u *p; - - if (!(supported2 & JO2_TERM_OPENCMD)) - break; - opt->jo_set2 |= JO2_TERM_OPENCMD; - p = opt->jo_term_opencmd = tv_get_string_buf_chk(item, - opt->jo_term_opencmd_buf); - if (p != NULL) - { - // Must have %d and no other %. - p = vim_strchr(p, '%'); - if (p != NULL && (p[1] != 'd' - || vim_strchr(p + 2, '%') != NULL)) - p = NULL; - } - if (p == NULL) - { - semsg(_(e_invargval), "term_opencmd"); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "eof_chars") == 0) - { - if (!(supported2 & JO2_EOF_CHARS)) - break; - opt->jo_set2 |= JO2_EOF_CHARS; - opt->jo_eof_chars = tv_get_string_buf_chk(item, - opt->jo_eof_chars_buf); - if (opt->jo_eof_chars == NULL) - { - semsg(_(e_invargval), "eof_chars"); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "term_rows") == 0) - { - if (!(supported2 & JO2_TERM_ROWS)) - break; - opt->jo_set2 |= JO2_TERM_ROWS; - opt->jo_term_rows = tv_get_number(item); - } - else if (STRCMP(hi->hi_key, "term_cols") == 0) - { - if (!(supported2 & JO2_TERM_COLS)) - break; - opt->jo_set2 |= JO2_TERM_COLS; - opt->jo_term_cols = tv_get_number(item); - } - else if (STRCMP(hi->hi_key, "vertical") == 0) - { - if (!(supported2 & JO2_VERTICAL)) - break; - opt->jo_set2 |= JO2_VERTICAL; - opt->jo_vertical = tv_get_bool(item); - } - else if (STRCMP(hi->hi_key, "curwin") == 0) - { - if (!(supported2 & JO2_CURWIN)) - break; - opt->jo_set2 |= JO2_CURWIN; - opt->jo_curwin = tv_get_number(item); - } - else if (STRCMP(hi->hi_key, "bufnr") == 0) - { - int nr; - - if (!(supported2 & JO2_CURWIN)) - break; - opt->jo_set2 |= JO2_BUFNR; - nr = tv_get_number(item); - if (nr <= 0) - { - semsg(_(e_invargNval), hi->hi_key, tv_get_string(item)); - return FAIL; - } - opt->jo_bufnr_buf = buflist_findnr(nr); - if (opt->jo_bufnr_buf == NULL) - { - semsg(_(e_nobufnr), (long)nr); - return FAIL; - } - if (opt->jo_bufnr_buf->b_nwindows == 0 - || opt->jo_bufnr_buf->b_term == NULL) - { - semsg(_(e_invarg2), "bufnr"); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "hidden") == 0) - { - if (!(supported2 & JO2_HIDDEN)) - break; - opt->jo_set2 |= JO2_HIDDEN; - opt->jo_hidden = tv_get_bool(item); - } - else if (STRCMP(hi->hi_key, "norestore") == 0) - { - if (!(supported2 & JO2_NORESTORE)) - break; - opt->jo_set2 |= JO2_NORESTORE; - opt->jo_term_norestore = tv_get_bool(item); - } - else if (STRCMP(hi->hi_key, "term_kill") == 0) - { - if (!(supported2 & JO2_TERM_KILL)) - break; - opt->jo_set2 |= JO2_TERM_KILL; - opt->jo_term_kill = tv_get_string_buf_chk(item, - opt->jo_term_kill_buf); - if (opt->jo_term_kill == NULL) - { - semsg(_(e_invargval), "term_kill"); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "tty_type") == 0) - { - char_u *p; - - if (!(supported2 & JO2_TTY_TYPE)) - break; - opt->jo_set2 |= JO2_TTY_TYPE; - p = tv_get_string_chk(item); - if (p == NULL) - { - semsg(_(e_invargval), "tty_type"); - return FAIL; - } - // Allow empty string, "winpty", "conpty". - if (!(*p == NUL || STRCMP(p, "winpty") == 0 - || STRCMP(p, "conpty") == 0)) - { - semsg(_(e_invargval), "tty_type"); - return FAIL; - } - opt->jo_tty_type = p[0]; - } -# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) - else if (STRCMP(hi->hi_key, "ansi_colors") == 0) - { - int n = 0; - listitem_T *li; - long_u rgb[16]; - - if (!(supported2 & JO2_ANSI_COLORS)) - break; - - if (item == NULL || item->v_type != VAR_LIST - || item->vval.v_list == NULL) - { - semsg(_(e_invargval), "ansi_colors"); - return FAIL; - } - - CHECK_LIST_MATERIALIZE(item->vval.v_list); - li = item->vval.v_list->lv_first; - for (; li != NULL && n < 16; li = li->li_next, n++) - { - char_u *color_name; - guicolor_T guicolor; - int called_emsg_before = called_emsg; - - color_name = tv_get_string_chk(&li->li_tv); - if (color_name == NULL) - return FAIL; - - guicolor = GUI_GET_COLOR(color_name); - if (guicolor == INVALCOLOR) - { - if (called_emsg_before == called_emsg) - // may not get the error if the GUI didn't start - semsg(_(e_alloc_color), color_name); - return FAIL; - } - - rgb[n] = GUI_MCH_GET_RGB(guicolor); - } - - if (n != 16 || li != NULL) - { - semsg(_(e_invargval), "ansi_colors"); - return FAIL; - } - - opt->jo_set2 |= JO2_ANSI_COLORS; - memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb)); - } -# endif - else if (STRCMP(hi->hi_key, "term_highlight") == 0) - { - char_u *p; - - if (!(supported2 & JO2_TERM_HIGHLIGHT)) - break; - opt->jo_set2 |= JO2_TERM_HIGHLIGHT; - p = tv_get_string_buf_chk(item, opt->jo_term_highlight_buf); - if (p == NULL || *p == NUL) - { - semsg(_(e_invargval), "term_highlight"); - return FAIL; - } - opt->jo_term_highlight = p; - } - else if (STRCMP(hi->hi_key, "term_api") == 0) - { - if (!(supported2 & JO2_TERM_API)) - break; - opt->jo_set2 |= JO2_TERM_API; - opt->jo_term_api = tv_get_string_buf_chk(item, - opt->jo_term_api_buf); - if (opt->jo_term_api == NULL) - { - semsg(_(e_invargval), "term_api"); - return FAIL; - } - } -#endif - else if (STRCMP(hi->hi_key, "env") == 0) - { - if (!(supported2 & JO2_ENV)) - break; - if (item->v_type != VAR_DICT) - { - semsg(_(e_invargval), "env"); - return FAIL; - } - opt->jo_set2 |= JO2_ENV; - opt->jo_env = item->vval.v_dict; - if (opt->jo_env != NULL) - ++opt->jo_env->dv_refcount; - } - else if (STRCMP(hi->hi_key, "cwd") == 0) - { - if (!(supported2 & JO2_CWD)) - break; - opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf); - if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd) -#ifndef MSWIN // Win32 directories don't have the concept of "executable" - || mch_access((char *)opt->jo_cwd, X_OK) != 0 -#endif - ) - { - semsg(_(e_invargval), "cwd"); - return FAIL; - } - opt->jo_set2 |= JO2_CWD; - } - else if (STRCMP(hi->hi_key, "waittime") == 0) - { - if (!(supported & JO_WAITTIME)) - break; - opt->jo_set |= JO_WAITTIME; - opt->jo_waittime = tv_get_number(item); - } - else if (STRCMP(hi->hi_key, "timeout") == 0) - { - if (!(supported & JO_TIMEOUT)) - break; - opt->jo_set |= JO_TIMEOUT; - opt->jo_timeout = tv_get_number(item); - } - else if (STRCMP(hi->hi_key, "out_timeout") == 0) - { - if (!(supported & JO_OUT_TIMEOUT)) - break; - opt->jo_set |= JO_OUT_TIMEOUT; - opt->jo_out_timeout = tv_get_number(item); - } - else if (STRCMP(hi->hi_key, "err_timeout") == 0) - { - if (!(supported & JO_ERR_TIMEOUT)) - break; - opt->jo_set |= JO_ERR_TIMEOUT; - opt->jo_err_timeout = tv_get_number(item); - } - else if (STRCMP(hi->hi_key, "part") == 0) - { - if (!(supported & JO_PART)) - break; - opt->jo_set |= JO_PART; - val = tv_get_string(item); - if (STRCMP(val, "err") == 0) - opt->jo_part = PART_ERR; - else if (STRCMP(val, "out") == 0) - opt->jo_part = PART_OUT; - else - { - semsg(_(e_invargNval), "part", val); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "id") == 0) - { - if (!(supported & JO_ID)) - break; - opt->jo_set |= JO_ID; - opt->jo_id = tv_get_number(item); - } - else if (STRCMP(hi->hi_key, "stoponexit") == 0) - { - if (!(supported & JO_STOPONEXIT)) - break; - opt->jo_set |= JO_STOPONEXIT; - opt->jo_stoponexit = tv_get_string_buf_chk(item, - opt->jo_stoponexit_buf); - if (opt->jo_stoponexit == NULL) - { - semsg(_(e_invargval), "stoponexit"); - return FAIL; - } - } - else if (STRCMP(hi->hi_key, "block_write") == 0) - { - if (!(supported & JO_BLOCK_WRITE)) - break; - opt->jo_set |= JO_BLOCK_WRITE; - opt->jo_block_write = tv_get_number(item); - } - else - break; - --todo; - } - if (todo > 0) - { - semsg(_(e_invarg2), hi->hi_key); - return FAIL; - } - - return OK; -} - -static job_T *first_job = NULL; - - static void -job_free_contents(job_T *job) -{ - int i; - - ch_log(job->jv_channel, "Freeing job"); - if (job->jv_channel != NULL) - { - // The link from the channel to the job doesn't count as a reference, - // thus don't decrement the refcount of the job. The reference from - // the job to the channel does count the reference, decrement it and - // NULL the reference. We don't set ch_job_killed, unreferencing the - // job doesn't mean it stops running. - job->jv_channel->ch_job = NULL; - channel_unref(job->jv_channel); - } - mch_clear_job(job); - - vim_free(job->jv_tty_in); - vim_free(job->jv_tty_out); - vim_free(job->jv_stoponexit); -#ifdef UNIX - vim_free(job->jv_termsig); -#endif -#ifdef MSWIN - vim_free(job->jv_tty_type); -#endif - free_callback(&job->jv_exit_cb); - if (job->jv_argv != NULL) - { - for (i = 0; job->jv_argv[i] != NULL; i++) - vim_free(job->jv_argv[i]); - vim_free(job->jv_argv); - } -} - -/* - * Remove "job" from the list of jobs. - */ - static void -job_unlink(job_T *job) -{ - if (job->jv_next != NULL) - job->jv_next->jv_prev = job->jv_prev; - if (job->jv_prev == NULL) - first_job = job->jv_next; - else - job->jv_prev->jv_next = job->jv_next; -} - - static void -job_free_job(job_T *job) -{ - job_unlink(job); - vim_free(job); -} - - static void -job_free(job_T *job) -{ - if (!in_free_unref_items) - { - job_free_contents(job); - job_free_job(job); - } -} - -static job_T *jobs_to_free = NULL; - -/* - * Put "job" in a list to be freed later, when it's no longer referenced. - */ - static void -job_free_later(job_T *job) -{ - job_unlink(job); - job->jv_next = jobs_to_free; - jobs_to_free = job; -} - - static void -free_jobs_to_free_later(void) -{ - job_T *job; - - while (jobs_to_free != NULL) - { - job = jobs_to_free; - jobs_to_free = job->jv_next; - job_free_contents(job); - vim_free(job); - } -} - -#if defined(EXITFREE) || defined(PROTO) - void -job_free_all(void) -{ - while (first_job != NULL) - job_free(first_job); - free_jobs_to_free_later(); - -# ifdef FEAT_TERMINAL - free_unused_terminals(); -# endif -} -#endif - -/* - * Return TRUE if we need to check if the process of "job" has ended. - */ - static int -job_need_end_check(job_T *job) -{ - return job->jv_status == JOB_STARTED - && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL); -} - -/* - * Return TRUE if the channel of "job" is still useful. - */ - static int -job_channel_still_useful(job_T *job) -{ - return job->jv_channel != NULL && channel_still_useful(job->jv_channel); -} - -/* - * Return TRUE if the channel of "job" is closeable. - */ - static int -job_channel_can_close(job_T *job) -{ - return job->jv_channel != NULL && channel_can_close(job->jv_channel); -} - -/* - * Return TRUE if the job should not be freed yet. Do not free the job when - * it has not ended yet and there is a "stoponexit" flag, an exit callback - * or when the associated channel will do something with the job output. - */ - static int -job_still_useful(job_T *job) -{ - return job_need_end_check(job) || job_channel_still_useful(job); -} - -#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO) -/* - * Return TRUE when there is any running job that we care about. - */ - int -job_any_running() -{ - job_T *job; - - FOR_ALL_JOBS(job) - if (job_still_useful(job)) - { - ch_log(NULL, "GUI not forking because a job is running"); - return TRUE; - } - return FALSE; -} -#endif - -#if !defined(USE_ARGV) || defined(PROTO) -/* - * Escape one argument for an external command. - * Returns the escaped string in allocated memory. NULL when out of memory. - */ - static char_u * -win32_escape_arg(char_u *arg) -{ - int slen, dlen; - int escaping = 0; - int i; - char_u *s, *d; - char_u *escaped_arg; - int has_spaces = FALSE; - - // First count the number of extra bytes required. - slen = (int)STRLEN(arg); - dlen = slen; - for (s = arg; *s != NUL; MB_PTR_ADV(s)) - { - if (*s == '"' || *s == '\\') - ++dlen; - if (*s == ' ' || *s == '\t') - has_spaces = TRUE; - } - - if (has_spaces) - dlen += 2; - - if (dlen == slen) - return vim_strsave(arg); - - // Allocate memory for the result and fill it. - escaped_arg = alloc(dlen + 1); - if (escaped_arg == NULL) - return NULL; - memset(escaped_arg, 0, dlen+1); - - d = escaped_arg; - - if (has_spaces) - *d++ = '"'; - - for (s = arg; *s != NUL;) - { - switch (*s) - { - case '"': - for (i = 0; i < escaping; i++) - *d++ = '\\'; - escaping = 0; - *d++ = '\\'; - *d++ = *s++; - break; - case '\\': - escaping++; - *d++ = *s++; - break; - default: - escaping = 0; - MB_COPY_CHAR(s, d); - break; - } - } - - // add terminating quote and finish with a NUL - if (has_spaces) - { - for (i = 0; i < escaping; i++) - *d++ = '\\'; - *d++ = '"'; - } - *d = NUL; - - return escaped_arg; -} - -/* - * Build a command line from a list, taking care of escaping. - * The result is put in gap->ga_data. - * Returns FAIL when out of memory. - */ - int -win32_build_cmd(list_T *l, garray_T *gap) -{ - listitem_T *li; - char_u *s; - - CHECK_LIST_MATERIALIZE(l); - FOR_ALL_LIST_ITEMS(l, li) - { - s = tv_get_string_chk(&li->li_tv); - if (s == NULL) - return FAIL; - s = win32_escape_arg(s); - if (s == NULL) - return FAIL; - ga_concat(gap, s); - vim_free(s); - if (li->li_next != NULL) - ga_append(gap, ' '); - } - return OK; -} -#endif - -/* - * NOTE: Must call job_cleanup() only once right after the status of "job" - * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or - * mch_detect_ended_job() returned non-NULL). - * If the job is no longer used it will be removed from the list of jobs, and - * deleted a bit later. - */ - void -job_cleanup(job_T *job) -{ - if (job->jv_status != JOB_ENDED) - return; - - // Ready to cleanup the job. - job->jv_status = JOB_FINISHED; - - // When only channel-in is kept open, close explicitly. - if (job->jv_channel != NULL) - ch_close_part(job->jv_channel, PART_IN); - - if (job->jv_exit_cb.cb_name != NULL) - { - typval_T argv[3]; - typval_T rettv; - - // Invoke the exit callback. Make sure the refcount is > 0. - ch_log(job->jv_channel, "Invoking exit callback %s", - job->jv_exit_cb.cb_name); - ++job->jv_refcount; - argv[0].v_type = VAR_JOB; - argv[0].vval.v_job = job; - argv[1].v_type = VAR_NUMBER; - argv[1].vval.v_number = job->jv_exitval; - call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv); - clear_tv(&rettv); - --job->jv_refcount; - channel_need_redraw = TRUE; - } - - if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe) - job->jv_channel->ch_killing = TRUE; - - // Do not free the job in case the close callback of the associated channel - // isn't invoked yet and may get information by job_info(). - if (job->jv_refcount == 0 && !job_channel_still_useful(job)) - // The job was already unreferenced and the associated channel was - // detached, now that it ended it can be freed. However, a caller might - // still use it, thus free it a bit later. - job_free_later(job); -} - -/* - * Mark references in jobs that are still useful. - */ - int -set_ref_in_job(int copyID) -{ - int abort = FALSE; - job_T *job; - typval_T tv; - - for (job = first_job; !abort && job != NULL; job = job->jv_next) - if (job_still_useful(job)) - { - tv.v_type = VAR_JOB; - tv.vval.v_job = job; - abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL); - } - return abort; -} - -/* - * Dereference "job". Note that after this "job" may have been freed. - */ - void -job_unref(job_T *job) -{ - if (job != NULL && --job->jv_refcount <= 0) - { - // Do not free the job if there is a channel where the close callback - // may get the job info. - if (!job_channel_still_useful(job)) - { - // Do not free the job when it has not ended yet and there is a - // "stoponexit" flag or an exit callback. - if (!job_need_end_check(job)) - { - job_free(job); - } - else if (job->jv_channel != NULL) - { - // Do remove the link to the channel, otherwise it hangs - // around until Vim exits. See job_free() for refcount. - ch_log(job->jv_channel, "detaching channel from job"); - job->jv_channel->ch_job = NULL; - channel_unref(job->jv_channel); - job->jv_channel = NULL; - } - } - } -} - - int -free_unused_jobs_contents(int copyID, int mask) -{ - int did_free = FALSE; - job_T *job; - - FOR_ALL_JOBS(job) - if ((job->jv_copyID & mask) != (copyID & mask) - && !job_still_useful(job)) - { - // Free the channel and ordinary items it contains, but don't - // recurse into Lists, Dictionaries etc. - job_free_contents(job); - did_free = TRUE; - } - return did_free; -} - - void -free_unused_jobs(int copyID, int mask) -{ - job_T *job; - job_T *job_next; - - for (job = first_job; job != NULL; job = job_next) - { - job_next = job->jv_next; - if ((job->jv_copyID & mask) != (copyID & mask) - && !job_still_useful(job)) - { - // Free the job struct itself. - job_free_job(job); - } - } -} - -/* - * Allocate a job. Sets the refcount to one and sets options default. - */ - job_T * -job_alloc(void) -{ - job_T *job; - - job = ALLOC_CLEAR_ONE(job_T); - if (job != NULL) - { - job->jv_refcount = 1; - job->jv_stoponexit = vim_strsave((char_u *)"term"); - - if (first_job != NULL) - { - first_job->jv_prev = job; - job->jv_next = first_job; - } - first_job = job; - } - return job; -} - - void -job_set_options(job_T *job, jobopt_T *opt) -{ - if (opt->jo_set & JO_STOPONEXIT) - { - vim_free(job->jv_stoponexit); - if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL) - job->jv_stoponexit = NULL; - else - job->jv_stoponexit = vim_strsave(opt->jo_stoponexit); - } - if (opt->jo_set & JO_EXIT_CB) - { - free_callback(&job->jv_exit_cb); - if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL) - { - job->jv_exit_cb.cb_name = NULL; - job->jv_exit_cb.cb_partial = NULL; - } - else - copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb); - } -} - -/* - * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag. - */ - void -job_stop_on_exit(void) -{ |