[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH 2/2] vi: support ~/.exrc
From:       Ron Yorston <rmy () pobox ! com>
Date:       2021-08-19 16:00:17
Message-ID: 611e8011.9VsILv9iww6MIz1W%rmy () pobox ! com
[Download RAW message or body]

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 <rmy@pobox.com>
---
 editors/vi.c | 40 ++++++++++++++++++++++++++++++++--------
 1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/editors/vi.c b/editors/vi.c
index cc4f6bde7..fd8e0d48e 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)
 	// :!<cmd>	// run <cmd> 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,36 @@ 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_uid == getuid() &&
+							(st.st_mode & (S_IWGRP|S_IWOTH)) == 0) {
+						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);
-- 
2.31.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic