patch 8.0.1647: terminal API may call any user function

Problem:    Terminal API may call a function not meant to be called by this
            API.
Solution:   Require the function to start with Tapi_.
diff --git a/src/terminal.c b/src/terminal.c
index 50e87c9..5875af6 100644
--- a/src/terminal.c
+++ b/src/terminal.c
@@ -3193,7 +3193,7 @@
     }
     func = get_tv_string(&item->li_tv);
 
-    if (!ASCII_ISUPPER(*func))
+    if (STRNCMP(func, "Tapi_", 5) != 0)
     {
 	ch_log(channel, "Invalid function name: %s", func);
 	return;
diff --git a/src/testdir/test_terminal.vim b/src/testdir/test_terminal.vim
index ff50568..892da50 100644
--- a/src/testdir/test_terminal.vim
+++ b/src/testdir/test_terminal.vim
@@ -1072,24 +1072,28 @@
   bwipe Xtextfile
 endfunc
 
-func TryThis(bufnum, arg)
+func Tapi_TryThis(bufnum, arg)
   let g:called_bufnum = a:bufnum
   let g:called_arg = a:arg
 endfunc
 
+func WriteApiCall(funcname)
+  " Use the title termcap entries to output the escape sequence.
+  call writefile([
+	\ 'set title',
+	\ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
+	\ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
+	\ 'redraw',
+	\ "set t_ts=",
+	\ ], 'Xscript')
+endfunc
+
 func Test_terminal_api_call()
   if !CanRunVimInTerminal()
     return
   endif
 
-  " Use the title termcap entries to output the escape sequence.
-  call writefile([
-	\ 'set title',
-	\ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
-	\ 'let &titlestring = ''["call","TryThis",["hello",123]]''',
-	\ 'redraw',
-	\ "set t_ts=",
-	\ ], 'Xscript')
+  call WriteApiCall('Tapi_TryThis')
   let buf = RunVimInTerminal('-S Xscript', {})
   call WaitFor({-> exists('g:called_bufnum')})
   call assert_equal(buf, g:called_bufnum)
@@ -1100,3 +1104,19 @@
   unlet g:called_bufnum
   unlet g:called_arg
 endfunc
+
+func Test_terminal_api_call_fails()
+  if !CanRunVimInTerminal()
+    return
+  endif
+
+  call WriteApiCall('TryThis')
+  call ch_logfile('Xlog', 'w')
+  let buf = RunVimInTerminal('-S Xscript', {})
+  call WaitFor({-> string(readfile('Xlog')) =~ 'Invalid function name: TryThis'})
+
+  call StopVimInTerminal(buf)
+  call delete('Xscript')
+  call ch_logfile('', '')
+  call delete('Xlog')
+endfunc
diff --git a/src/version.c b/src/version.c
index 14bf136..df56277 100644
--- a/src/version.c
+++ b/src/version.c
@@ -767,6 +767,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1647,
+/**/
     1646,
 /**/
     1645,