robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#include <stdbool.h> // for true 2 3#include "nvim/channel.h" 4#include "nvim/errors.h" 5#include "nvim/eval.h" 6#include "nvim/eval/deprecated.h" 7#include "nvim/eval/funcs.h" 8#include "nvim/eval/typval.h" 9#include "nvim/eval/typval_defs.h" 10#include "nvim/ex_cmds.h" 11#include "nvim/gettext_defs.h" // for _ 12#include "nvim/globals.h" 13#include "nvim/macros_defs.h" // for S_LEN 14#include "nvim/message.h" // for semsg 15#include "nvim/types_defs.h" 16 17#include "eval/deprecated.c.generated.h" // IWYU pragma: keep 18 19/// "rpcstart()" function (DEPRECATED) 20void f_rpcstart (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 21{ 22rettv -> v_type = VAR_NUMBER ; 23rettv -> vval .v_number = 0 ; 24 25if (check_secure ()) { 26return ; 27 } 28 29if (argvars [0 ].v_type != VAR_STRING 30|| (argvars [1 ].v_type != VAR_LIST && argvars [1 ].v_type != VAR_UNKNOWN )) { 31// Wrong argument types 32emsg (_ (e_invarg )); 33return ; 34 } 35 36list_T * args = NULL ; 37int argsl = 0 ; 38if (argvars [1 ].v_type == VAR_LIST ) { 39args = argvars [1 ].vval .v_list ; 40argsl = tv_list_len (args ); 41// Assert that all list items are strings 42int i = 0 ; 43TV_LIST_ITER_CONST (args ,arg , { 44if (TV_LIST_ITEM_TV (arg )-> v_type != VAR_STRING ) { 45semsg (_ ("E5010: List item %d of the second argument is not a string" ), 46i ); 47return ; 48 } 49i ++ ; 50 }); 51 } 52 53if (argvars [0 ].vval .v_string == NULL || argvars [0 ].vval .v_string [0 ]== NUL ) { 54emsg (_ (e_api_spawn_failed )); 55return ; 56 } 57 58// Allocate extra memory for the argument vector and the NULL pointer 59int argvl = argsl + 2 ; 60char ** argv = xmalloc (sizeof (char * )* (size_t )argvl ); 61 62// Copy program name 63argv [0 ]= xstrdup (argvars [0 ].vval .v_string ); 64 65int i = 1 ; 66// Copy arguments to the vector 67if (argsl > 0 ) { 68TV_LIST_ITER_CONST (args ,arg , { 69argv [i ++ ]= xstrdup (tv_get_string (TV_LIST_ITEM_TV (arg ))); 70 }); 71 } 72 73// The last item of argv must be NULL 74argv [i ]= NULL ; 75 76Channel * chan = channel_job_start (argv ,NULL ,CALLBACK_READER_INIT , 77CALLBACK_READER_INIT ,CALLBACK_NONE , 78 false, true, false, false, 79kChannelStdinPipe ,NULL ,0 ,0 ,NULL , 80& rettv -> vval .v_number ); 81if (chan ) { 82channel_create_event (chan ,NULL ); 83 } 84} 85 86/// "rpcstop()" function 87void f_rpcstop (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 88{ 89rettv -> v_type = VAR_NUMBER ; 90rettv -> vval .v_number = 0 ; 91 92if (check_secure ()) { 93return ; 94 } 95 96if (argvars [0 ].v_type != VAR_NUMBER ) { 97// Wrong argument types 98emsg (_ (e_invarg )); 99return ; 100 } 101 102// if called with a job, stop it, else closes the channel 103uint64_t id = (uint64_t )argvars [0 ].vval .v_number ; 104if (find_job (id , false)) { 105f_jobstop (argvars ,rettv ,fptr ); 106 }else { 107const char * error ; 108rettv -> vval .v_number = 109channel_close ((uint64_t )argvars [0 ].vval .v_number ,kChannelPartRpc ,& error ); 110if (!rettv -> vval .v_number ) { 111emsg (error ); 112 } 113 } 114} 115 116/// "last_buffer_nr()" function. 117void f_last_buffer_nr (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 118{ 119int n = 0 ; 120 121FOR_ALL_BUFFERS (buf ) { 122if (n < buf -> b_fnum ) { 123n = buf -> b_fnum ; 124 } 125 } 126 127rettv -> vval .v_number = n ; 128} 129 130/// "termopen(cmd[, cwd])" function 131void f_termopen (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 132{ 133if (check_secure ()) { 134return ; 135 } 136 137bool must_free = false; 138 139if (argvars [1 ].v_type == VAR_UNKNOWN ) { 140must_free = true; 141argvars [1 ].v_type = VAR_DICT ; 142argvars [1 ].vval .v_dict = tv_dict_alloc (); 143 } 144 145if (argvars [1 ].v_type != VAR_DICT ) { 146// Wrong argument types 147semsg (_ (e_invarg2 ),"expected dictionary" ); 148return ; 149 } 150 151tv_dict_add_bool (argvars [1 ].vval .v_dict ,S_LEN ("term" ), true); 152f_jobstart (argvars ,rettv ,fptr ); 153if (must_free ) { 154tv_dict_free (argvars [1 ].vval .v_dict ); 155 } 156}