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)
diff --git a/src/terminal.c b/src/terminal.c
index f1d89ab..4f2b8bb 100644
--- a/src/terminal.c
+++ b/src/terminal.c
@@ -109,6 +109,7 @@
#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->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 @@
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 @@
semsg(_("E181: Invalid attribute: %s"), cmd);
goto theend;
}
+# undef OPTARG_HAS
cmd = skipwhite(p);
}
if (*cmd == NUL)
@@ -933,6 +954,7 @@
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);
@@ -3770,6 +3792,15 @@
}
/*
+ * 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 @@
}
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;
}
@@ -5546,6 +5577,27 @@
#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
*/
void
@@ -5608,7 +5660,7 @@
+ 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);