Added wordexp for `source` command

Related issue: #1
This commit is contained in:
Casey 2024-04-30 12:41:41 +03:00
parent 6ca32e8d49
commit 4035b50128
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 36 additions and 2 deletions

View File

@ -13,6 +13,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include <wordexp.h>
#define KEY_LENGTH 256 #define KEY_LENGTH 256
#define MAX_SOUNDS_PER_KEY 32 #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"); send_txt(tgt, "ERR: argument required: PATH\n");
return; 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")) { } else if (0 == strcmp(command, "help")) {
send_help(tgt); send_help(tgt);
} else if (0 == strcmp(command, "meow")) { } else if (0 == strcmp(command, "meow")) {
@ -425,7 +442,24 @@ bool execute_file(struct msg_target tgt, const char *path, int depth) {
fclose(fp); fclose(fp);
return false; 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 { } else {
execute_command(tgt, cmd, args); execute_command(tgt, cmd, args);
} }