robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#include <lua.h> 2#include <stdbool.h> 3#include <string.h> 4 5#include "nvim/ascii_defs.h" 6#include "nvim/charset.h" 7#include "nvim/errors.h" 8#include "nvim/ex_cmds_defs.h" 9#include "nvim/gettext_defs.h" 10#include "nvim/lua/executor.h" 11#include "nvim/lua/secure.h" 12#include "nvim/memory.h" 13#include "nvim/message.h" 14 15#include "lua/secure.c.generated.h" 16 17char * nlua_read_secure (const char * path ) 18{ 19lua_State * const lstate = get_global_lstate (); 20const int top = lua_gettop (lstate ); 21 22lua_getglobal (lstate ,"vim" ); 23lua_getfield (lstate ,-1 ,"secure" ); 24lua_getfield (lstate ,-1 ,"read" ); 25lua_pushstring (lstate ,path ); 26if (nlua_pcall (lstate ,1 ,1 )) { 27nlua_error (lstate ,_ ("vim.secure.read: %.*s" )); 28lua_settop (lstate ,top ); 29return NULL ; 30 } 31 32size_t len = 0 ; 33const char * contents = lua_tolstring (lstate ,-1 ,& len ); 34char * buf = NULL ; 35if (contents != NULL ) { 36// Add one to include trailing null byte 37buf = xcalloc (len + 1 ,sizeof (char )); 38memcpy (buf ,contents ,len + 1 ); 39 } 40 41lua_settop (lstate ,top ); 42return buf ; 43} 44 45static bool nlua_trust (const char * action ,const char * path ) 46{ 47lua_State * const lstate = get_global_lstate (); 48const int top = lua_gettop (lstate ); 49 50lua_getglobal (lstate ,"vim" ); 51lua_getfield (lstate ,-1 ,"secure" ); 52lua_getfield (lstate ,-1 ,"trust" ); 53 54lua_newtable (lstate ); 55lua_pushstring (lstate ,"action" ); 56lua_pushstring (lstate ,action ); 57lua_settable (lstate ,-3 ); 58if (path == NULL ) { 59lua_pushstring (lstate ,"bufnr" ); 60lua_pushnumber (lstate ,0 ); 61lua_settable (lstate ,-3 ); 62 }else { 63lua_pushstring (lstate ,"path" ); 64lua_pushstring (lstate ,path ); 65lua_settable (lstate ,-3 ); 66 } 67 68if (nlua_pcall (lstate ,1 ,2 )) { 69nlua_error (lstate ,_ ("vim.secure.trust: %.*s" )); 70lua_settop (lstate ,top ); 71return false; 72 } 73 74bool success = lua_toboolean (lstate ,-2 ); 75const char * msg = lua_tostring (lstate ,-1 ); 76if (msg != NULL ) { 77if (success ) { 78if (strcmp (action ,"allow" )== 0 ) { 79smsg (0 ,"Allowed in trust database: \"%s\"" ,msg ); 80 }else if (strcmp (action ,"deny" )== 0 ) { 81smsg (0 ,"Denied in trust database: \"%s\"" ,msg ); 82 }else if (strcmp (action ,"remove" )== 0 ) { 83smsg (0 ,"Removed from trust database: \"%s\"" ,msg ); 84 } 85 }else { 86semsg (e_trustfile ,msg ); 87 } 88 } 89 90lua_settop (lstate ,top ); 91return success ; 92} 93 94void ex_trust (exarg_T * eap ) 95{ 96const char * const p = skiptowhite (eap -> arg ); 97char * arg1 = xmemdupz (eap -> arg , (size_t )(p - eap -> arg )); 98const char * action = "allow" ; 99const char * path = skipwhite (p ); 100 101if (strcmp (arg1 ,"++deny" )== 0 ) { 102action = "deny" ; 103 }else if (strcmp (arg1 ,"++remove" )== 0 ) { 104action = "remove" ; 105 }else if (* arg1 != NUL ) { 106semsg (e_invarg2 ,arg1 ); 107 gototheend ; 108 } 109 110if (path [0 ]== NUL ) { 111path = NULL ; 112 } 113 114nlua_trust (action ,path ); 115 116theend : 117xfree (arg1 ); 118}