ESH

Executive Shell
git clone http://git.omkov.net/ESH
Log | Tree | Refs | README | Download

AuthorJamozed <[email protected]>
Date2021-11-12 03:34:23
Commit18443a4aaf76b7d9deb66008bbe9b2b6bc74d53c
Parentaf976fa8f6699a9133f89c8fb14b0925040f432d

Deduplicate config handling functions

Diffstat

M src/conf.c | 39 +++++++++++++++++++++------------------
M src/conf.h | 10 +++++-----
M src/main.c | 26 ++------------------------

3 files changed, 28 insertions, 47 deletions

diff --git a/src/conf.c b/src/conf.c
index 671c647..5ad2f73 100644
--- a/src/conf.c
+++ b/src/conf.c
@@ -31,35 +31,38 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 */
 
 #include "conf.h"
+#include "esh.h"
 
 #include "cll/error.h"
 
+#include "util/util.h"
+
+#include <unistd.h>
+
 #include <errno.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-size_t conf_histsize = 8192;
+UINT conf_histsize = 8192;
 
-/* Currently the "eshrc" function in main.c is used instead of this. */
+/* Evaluate the eshrc config file. */
 void confread(void) {
-	char *bp, *fp; register size_t bl; FILE *fi;
-	
-	if (!(bp = getenv("XDG_CONFIG_HOME"))) {
-		if (!(bp = getenv("HOME"))) { warn("$HOME is undefined"); return; }
-		bl = strlen(bp); if (!(fp = calloc(bl + 19, sizeof (*fp)))) { return; }
-		memcpy(fp, bp, bl); memcpy(fp + bl, "/.config/esh/eshrc", 18);
-	}
-	else {
-		bl = strlen(bp); if (!(fp = calloc(bl + 11, sizeof (*fp)))) { return; }
-		memcpy(fp, bp, bl); memcpy(fp + bl, "/esh/eshrc", 10);
-	}
+	char *base = strdup(getenv("XDG_CONFIG_HOME")), *path;
 
-	if (!(fi = fopen(fp, "r")) && errno != ENOENT) {
-		perror(fp); free(fp); return;
+	/* If $XDG_CONFIG_HOME is unset or empty, use the default path */
+	if (!base || !base[0]) {
+		free(base); char *home = getenv("HOME");
+		if (!home || !home[0]) { warn("$HOME is unset or empty"); return; }
+		
+		base = assert_malloc(strlen(home) + strlen("/.config") + 1);
+		strcpy(base, home); strcat(base, "./config");
 	}
 
-	/* TODO execute rc file */
+	/* Allocate and construct the full eshrc path */
+	path = assert_malloc(strlen(base) + strlen("/esh/eshrc") + 1);
+	strcpy(path, base); strcat(path, "/esh/eshrc"); free(base);
 
-	free(fp); return;
+	/* If the eshrc file exists, then evaluate it */
+	if (access(path, F_OK) && errno == ENOENT) { errno = 0; }
+	else { eval_file(path); } free(path);
 }
diff --git a/src/conf.h b/src/conf.h
index e1f5e32..8bc805e 100644
--- a/src/conf.h
+++ b/src/conf.h
@@ -30,13 +30,13 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 */
 
-#ifndef OMKOV_ESH_CONF_H_SOQSQ8HL
-#define OMKOV_ESH_CONF_H_SOQSQ8HL
+#ifndef ESH_CONF_H_SOQSQ8HL
+#define ESH_CONF_H_SOQSQ8HL
 
-#include <stdlib.h>
+#include "util/util.h"
 
-extern size_t conf_histsize;
+extern UINT conf_histsize;
 
 extern void confread(void);
 
-#endif // OMKOV_ESH_CONF_H_SOQSQ8HL
+#endif // ESH_CONF_H_SOQSQ8HL
diff --git a/src/main.c b/src/main.c
index 2087ec7..f536fd9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -32,9 +32,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 
 /*
 	FIXME: Every second press of the delete key prints garbage to the terminal.
-	TODO: Deduplicate config handling functions, currently there are two.
 */
 
+#include "conf.h"
 #include "esh.h"
 #include "lineread.h"
 #include "util/util.h"
@@ -85,7 +85,7 @@ 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()); }
 
-	if (!Lflag) { eshrc(); }
+	if (!Lflag) { confread(); }
 
 	do {
 		if (sigsetjmp(jmp, 1)) { fputc('\n', stdout); } jmpflag = true;
@@ -103,28 +103,6 @@ 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);
-	
-	if (access(path, F_OK) && errno == ENOENT) { errno = 0; }
-	else { eval_file(path); } free(path);
-}
-
 static void hlp(void) {
 	puts("ESH - Executive Shell\n");
 	puts("Usage: esh\n");