From 4035b501285dc0e969eb3419f594147f8b288696 Mon Sep 17 00:00:00 2001 From: hkc Date: Tue, 30 Apr 2024 12:41:41 +0300 Subject: [PATCH] Added wordexp for `source` command Related issue: #1 --- src/sfxd.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/sfxd.c b/src/sfxd.c index 3589869..a65d1d2 100644 --- a/src/sfxd.c +++ b/src/sfxd.c @@ -13,6 +13,7 @@ #include #include #include +#include #define KEY_LENGTH 256 #define MAX_SOUNDS_PER_KEY 32 @@ -327,7 +328,23 @@ void execute_command(struct msg_target tgt, const char *command, const char *par send_txt(tgt, "ERR: argument required: PATH\n"); return; } - execute_file(tgt, params, 0); + wordexp_t p; + int retval = wordexp(params, &p, 0); +#define _WORDEXP_ERROR(ERR_CODE) case ERR_CODE: send_txt(tgt, "ERR: wordexp: " #ERR_CODE); return; + switch (retval) { + case 0: break; + _WORDEXP_ERROR(WRDE_BADCHAR); + _WORDEXP_ERROR(WRDE_BADVAL); + _WORDEXP_ERROR(WRDE_CMDSUB); + _WORDEXP_ERROR(WRDE_NOSPACE); + _WORDEXP_ERROR(WRDE_SYNTAX); + default: send_txt(tgt, "ERR: wordexp: Unknown error: %d\n", retval); return; + } +#undef _WORDEXP_ERROR + for (size_t i = 0; i < p.we_wordc; i++) { + execute_file(tgt, p.we_wordv[i], 0); + } + wordfree(&p); } else if (0 == strcmp(command, "help")) { send_help(tgt); } else if (0 == strcmp(command, "meow")) { @@ -425,7 +442,24 @@ bool execute_file(struct msg_target tgt, const char *path, int depth) { fclose(fp); return false; } - execute_file(tgt, args, depth + 1); + wordexp_t p; + int retval = wordexp(args, &p, 0); +#define _WORDEXP_ERROR(ERR_CODE) case ERR_CODE: send_txt(tgt, "ERR: wordexp: " #ERR_CODE); return false; + switch (retval) { + case 0: break; + _WORDEXP_ERROR(WRDE_BADCHAR); + _WORDEXP_ERROR(WRDE_BADVAL); + _WORDEXP_ERROR(WRDE_CMDSUB); + _WORDEXP_ERROR(WRDE_NOSPACE); + _WORDEXP_ERROR(WRDE_SYNTAX); + default: send_txt(tgt, "ERR: wordexp: Unknown error: %d\n", retval); return false; + } +#undef _WORDEXP_ERROR + for (size_t i = 0; i < p.we_wordc; i++) { + send_txt(tgt, "DBG: executing file %s\n", p.we_wordv[i]); + execute_file(tgt, p.we_wordv[i], depth + 1); + } + wordfree(&p); } else { execute_command(tgt, cmd, args); }