Author | Jamozed <[email protected]> |
Date | 2021-09-11 11:52:04 |
Commit | 65268ba9dedd4750877dde27b6311f2a2411a070 |
Parent | de472f3d6ef1bb1c9594755d2c900bc95d959912 |
Evaluate $XDG_CONFIG_HOME/esh/eshrc file on start
Diffstat
M | CHANGELOG | | | 1 | + |
M | src/main.c | | | 26 | ++++++++++++++++++++++++++ |
2 files changed, 27 insertions, 0 deletions
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 <sys/stat.h> + #include <errno.h> #include <setjmp.h> #include <signal.h> @@ -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");