From f9217cd235c2a139ae22cf549c7614724f1fc6cf Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 19 Aug 2021 17:00:17 +0100 Subject: [PATCH] vi: support ~/.exrc Run initialisation commands from ~/.exrc. As with EXINIT these commands are processed before the first file is loaded. Commands starting with double quotes are ignored. This is how comments are often included in .exrc. function old new delta vi_main 268 406 +138 colon 4033 4071 +38 .rodata 108411 108442 +31 packed_usage 34128 34118 -10 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/1 up/down: 207/-10) Total: 197 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- editors/vi.c | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/editors/vi.c b/editors/vi.c index cc4f6bde7..e6527e36b 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -7,7 +7,7 @@ */ // //Things To Do: -// $HOME/.exrc and ./.exrc +// ./.exrc // add magic to search /foo.*bar // add :help command // :map macros @@ -185,7 +185,7 @@ //usage:#define vi_full_usage "\n\n" //usage: "Edit FILE\n" //usage: IF_FEATURE_VI_COLON( -//usage: "\n -c CMD Initial command to run ($EXINIT also available)" +//usage: "\n -c CMD Initial command to run ($EXINIT and ~/.exrc also available)" //usage: ) //usage: IF_FEATURE_VI_READONLY( //usage: "\n -R Read-only" @@ -2829,10 +2829,12 @@ static void colon(char *buf) // :! // run then return // - if (!buf[0]) - goto ret; - if (*buf == ':') - buf++; // move past the ':' + while (*buf == ':') + buf++; // move past leading colons + while (isblank(*buf)) + buf++; // move past leading blanks + if (!buf[0] || buf[0] == '"') + goto ret; // ignore empty lines or those starting with '"' li = i = 0; b = e = -1; @@ -4923,14 +4925,37 @@ int vi_main(int argc, char **argv) cmdline_filecnt = argc - optind; // 1- process EXINIT variable from environment - // 2- if EXINIT is unset process $HOME/.exrc file (not implemented yet) + // 2- if EXINIT is unset process $HOME/.exrc file // 3- process command line args #if ENABLE_FEATURE_VI_COLON { const char *exinit = getenv("EXINIT"); + char *cmds = NULL; if (exinit) { - char *cmds = xstrdup(exinit); + cmds = xstrdup(exinit); + } else { + const char *home = getenv("HOME"); + + if (home && *home) { + char *exrc = concat_path_file(home, ".exrc"); + struct stat st; + + // .exrc must belong to and only be writable by user + if (stat(exrc, &st) == 0) { + if ((st.st_mode & (S_IWGRP|S_IWOTH)) == 0 + && st.st_uid == getuid() + ) { + cmds = xmalloc_open_read_close(exrc, NULL); + } else { + status_line_bold(".exrc: permission denied"); + } + } + free(exrc); + } + } + + if (cmds) { init_text_buffer(NULL); run_cmds(cmds); free(cmds);