0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

patch 8.1.2080: the terminal API is limited and can't be disabled

Problem:    The terminal API is limited and can't be disabled.
Solution:   Add term_setapi() to set the function prefix. (Ozaki Kiichi,
            closes #2907)
This commit is contained in:
Bram Moolenaar
2019-09-26 23:08:54 +02:00
parent d2c1fb476d
commit d2842ea60b
10 changed files with 220 additions and 35 deletions

View File

@@ -109,6 +109,7 @@ struct terminal_S {
#define TL_FINISH_OPEN 'o' /* ++open */
char_u *tl_opencmd;
char_u *tl_eof_chars;
char_u *tl_api; // prefix for terminal API function
char_u *tl_arg0_cmd; // To format the status bar
@@ -641,6 +642,11 @@ term_start(
term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
}
if (opt->jo_term_api != NULL)
term->tl_api = vim_strsave(opt->jo_term_api);
else
term->tl_api = vim_strsave((char_u *)"Tapi_");
/* System dependent: setup the vterm and maybe start the job in it. */
if (argv == NULL
&& argvar->v_type == VAR_STRING
@@ -708,44 +714,58 @@ ex_terminal(exarg_T *eap)
cmd += 2;
p = skiptowhite(cmd);
ep = vim_strchr(cmd, '=');
if (ep != NULL && ep < p)
p = ep;
if (ep != NULL)
{
if (ep < p)
p = ep;
else
ep = NULL;
}
if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
&& STRNICMP(cmd, name, sizeof(name) - 1) == 0)
if (OPTARG_HAS("close"))
opt.jo_term_finish = 'c';
else if ((int)(p - cmd) == 7 && STRNICMP(cmd, "noclose", 7) == 0)
else if (OPTARG_HAS("noclose"))
opt.jo_term_finish = 'n';
else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
else if (OPTARG_HAS("open"))
opt.jo_term_finish = 'o';
else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
else if (OPTARG_HAS("curwin"))
opt.jo_curwin = 1;
else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
else if (OPTARG_HAS("hidden"))
opt.jo_hidden = 1;
else if ((int)(p - cmd) == 9 && STRNICMP(cmd, "norestore", 9) == 0)
else if (OPTARG_HAS("norestore"))
opt.jo_term_norestore = 1;
else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "kill", 4) == 0
&& ep != NULL)
else if (OPTARG_HAS("kill") && ep != NULL)
{
opt.jo_set2 |= JO2_TERM_KILL;
opt.jo_term_kill = ep + 1;
p = skiptowhite(cmd);
}
else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
&& ep != NULL && isdigit(ep[1]))
else if (OPTARG_HAS("api"))
{
opt.jo_set2 |= JO2_TERM_API;
if (ep != NULL)
{
opt.jo_term_api = ep + 1;
p = skiptowhite(cmd);
}
else
opt.jo_term_api = NULL;
}
else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
{
opt.jo_set2 |= JO2_TERM_ROWS;
opt.jo_term_rows = atoi((char *)ep + 1);
p = skiptowhite(cmd);
}
else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
&& ep != NULL && isdigit(ep[1]))
else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
{
opt.jo_set2 |= JO2_TERM_COLS;
opt.jo_term_cols = atoi((char *)ep + 1);
p = skiptowhite(cmd);
}
else if ((int)(p - cmd) == 3 && STRNICMP(cmd, "eof", 3) == 0
&& ep != NULL)
else if (OPTARG_HAS("eof") && ep != NULL)
{
char_u *buf = NULL;
char_u *keys;
@@ -785,6 +805,7 @@ ex_terminal(exarg_T *eap)
semsg(_("E181: Invalid attribute: %s"), cmd);
goto theend;
}
# undef OPTARG_HAS
cmd = skipwhite(p);
}
if (*cmd == NUL)
@@ -933,6 +954,7 @@ free_unused_terminals()
free_scrollback(term);
term_free_vterm(term);
vim_free(term->tl_api);
vim_free(term->tl_title);
#ifdef FEAT_SESSION
vim_free(term->tl_command);
@@ -3769,6 +3791,15 @@ handle_drop_command(listitem_T *item)
vim_free(tofree);
}
/*
* Return TRUE if "func" starts with "pat" and "pat" isn't empty.
*/
static int
is_permitted_term_api(char_u *func, char_u *pat)
{
return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
}
/*
* Handles a function call from the job running in a terminal.
* "item" is the function name, "item->li_next" has the arguments.
@@ -3788,9 +3819,9 @@ handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
}
func = tv_get_string(&item->li_tv);
if (STRNCMP(func, "Tapi_", 5) != 0)
if (!is_permitted_term_api(func, term->tl_api))
{
ch_log(channel, "Invalid function name: %s", func);
ch_log(channel, "Unpermitted function: %s", func);
return;
}
@@ -5545,6 +5576,27 @@ f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
}
#endif
/*
* "term_setapi(buf, api)" function
*/
void
f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
{
buf_T *buf = term_get_buf(argvars, "term_setapi()");
term_T *term;
char_u *api;
if (buf == NULL)
return;
term = buf->b_term;
vim_free(term->tl_api);
api = tv_get_string_chk(&argvars[1]);
if (api != NULL)
term->tl_api = vim_strsave(api);
else
term->tl_api = NULL;
}
/*
* "term_setrestore(buf, command)" function
*/
@@ -5608,7 +5660,7 @@ f_term_start(typval_T *argvars, typval_T *rettv)
+ JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
+ JO2_CWD + JO2_ENV + JO2_EOF_CHARS
+ JO2_NORESTORE + JO2_TERM_KILL
+ JO2_ANSI_COLORS + JO2_TTY_TYPE) == FAIL)
+ JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
return;
buf = term_start(&argvars[0], NULL, &opt, 0);