From 65268ba9dedd4750877dde27b6311f2a2411a070 Mon Sep 17 00:00:00 2001 From: Jamozed Date: Sat, 11 Sep 2021 23:52:04 +1200 Subject: [PATCH] Evaluate $XDG_CONFIG_HOME/esh/eshrc file on start --- CHANGELOG | 1 + src/main.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 022064b..5ea065b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,7 @@ * Implement command history * Implement configuration * Switch to AST parsing +* Evaluate $XDG_CONFIG_HOME/esh/eshrc file on start 0.4.0, 2021-04-01 * Fix SIGINT handling at prompt diff --git a/src/main.c b/src/main.c index e60db54..3b92251 100644 --- a/src/main.c +++ b/src/main.c @@ -37,6 +37,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #include "cll/error.h" #include "cll/optget.h" +#include + #include #include #include @@ -59,6 +61,7 @@ static jmp_buf jmp; static sig_atomic_t jmpflag = false; static void reset(int signo); +static void eshrc(void); static void hlp(void); static void ver(void); @@ -76,6 +79,8 @@ int main(int ac, char *av[]) { (void)(ac); A0 = av[0]; signal(SIGINT, &reset); signal(SIGTSTP, SIG_IGN); signal(SIGQUIT, SIG_IGN); if (errno) { warn("%s", serr()); } + eshrc(); + do { if (sigsetjmp(jmp, 1)) { fputc('\n', stdout); } jmpflag = true; @@ -92,6 +97,27 @@ static void reset(int signo) { (void)(signo); if (jmpflag) { siglongjmp(jmp, 1); } } +/* Evaluate $XDG_CONFIG_HOME/esh/eshrc file. */ +static void eshrc(void) { + char *dir = strdup(getenv("XDG_CONFIG_HOME")); + + /* If XDG_CONFIG_HOME is unset or empty, use the default */ + if (!dir || !dir[0]) { + char *home = getenv("HOME"); + if (!home || !home[0]) { warn("$HOME is unset or empty"); return; } + + dir = assert_malloc(strlen(home) + strlen("/.config") + 1); + strcpy(dir, home); strcat(dir, "/.config"); + } + + char *path, *base = "/esh/eshrc"; + + path = assert_malloc(strlen(dir) + strlen(base) + 1); + strcpy(path, dir); strcat(path, base); free(dir); + + eval_file((u8 *)path); free(path); +} + static void hlp(void) { puts("ESH - Executive Shell\n"); puts("Usage: esh\n");