Rename shell commander
This commit is contained in:
@@ -17,85 +17,85 @@
|
||||
#define CMD_BUF_LEN 8192
|
||||
#define OUT_BUF_LEN 4096
|
||||
|
||||
wp_intern CMDResult execute_command(WpStr8RO *cmd, CMDOutHandling out_handling, WpStr8 *out_buf);
|
||||
wp_intern CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, WpStr8 *out_buf);
|
||||
wp_intern WpCmdResult executeCommand(WpStr8RO *cmd, WpCmdOutHandling out_handling, WpStr8 *out_buf);
|
||||
wp_intern WpCmdError getCommandOutput(FILE *fp, WpCmdOutHandling out_handling, WpStr8 *out_buf);
|
||||
|
||||
CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, WpStr8 *out_buf, const WpStr8List *cmd) {
|
||||
WpCmdResult wpShellCommanderExecute(WpCmdOutHandling out_handling, WpStr8 *out_buf, const WpStr8List *cmd) {
|
||||
if (!cmd) {
|
||||
return CMD_NO_EXIT(SHELL_ERR_INVALID_ARGS);
|
||||
return wpCmdNoExit(WP_SHELL_ERR_INVALID_ARGS);
|
||||
}
|
||||
|
||||
WpAllocator arena = wapp_mem_arena_allocator_init(KiB(500));
|
||||
WpAllocator arena = wpMemArenaAllocatorInit(KiB(500));
|
||||
|
||||
WpStr8 *cmd_str = wpStr8Join(&arena, cmd, &wpStr8LitRo(" "));
|
||||
if (!cmd_str) {
|
||||
wapp_mem_arena_allocator_destroy(&arena);
|
||||
return CMD_NO_EXIT(SHELL_ERR_ALLOCATION_FAIL);
|
||||
wpMemArenaAllocatorDestroy(&arena);
|
||||
return wpCmdNoExit(WP_SHELL_ERR_ALLOCATION_FAIL);
|
||||
}
|
||||
|
||||
// Redirect output
|
||||
cmd_str = wpStr8AllocConcat(&arena, cmd_str, &wpStr8LitRo(" 2>&1"));
|
||||
|
||||
CMDResult output = execute_command(cmd_str, out_handling, out_buf);
|
||||
WpCmdResult output = executeCommand(cmd_str, out_handling, out_buf);
|
||||
|
||||
wapp_mem_arena_allocator_destroy(&arena);
|
||||
wpMemArenaAllocatorDestroy(&arena);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
wp_intern CMDResult execute_command(WpStr8RO *cmd, CMDOutHandling out_handling, WpStr8 *out_buf) {
|
||||
wp_intern WpCmdResult executeCommand(WpStr8RO *cmd, WpCmdOutHandling out_handling, WpStr8 *out_buf) {
|
||||
char cmd_buf[CMD_BUF_LEN] = {0};
|
||||
wpStr8CopyToCstr(cmd_buf, cmd, CMD_BUF_LEN);
|
||||
|
||||
FILE *fp = wapp_shell_utils_popen(cmd_buf, "r");
|
||||
FILE *fp = wpShellUtilsPopen(cmd_buf, "r");
|
||||
if (!fp) {
|
||||
return CMD_NO_EXIT(SHELL_ERR_PROC_START_FAIL);
|
||||
return wpCmdNoExit(WP_SHELL_ERR_PROC_START_FAIL);
|
||||
}
|
||||
|
||||
CMDResult output;
|
||||
WpCmdResult output;
|
||||
|
||||
CMDError err = get_command_output(fp, out_handling, out_buf);
|
||||
if (err > SHELL_ERR_NO_ERROR) {
|
||||
output = CMD_NO_EXIT(err);
|
||||
goto EXECUTE_COMMAND_CLOSE;
|
||||
WpCmdError err = getCommandOutput(fp, out_handling, out_buf);
|
||||
if (err > WP_SHELL_ERR_NO_ERROR) {
|
||||
output = wpCmdNoExit(err);
|
||||
goto executeCommand_CLOSE;
|
||||
}
|
||||
|
||||
i32 st = EXIT_SUCCESS;
|
||||
err = get_output_status(fp, &st);
|
||||
if (err > SHELL_ERR_NO_ERROR) {
|
||||
output = CMD_NO_EXIT(err);
|
||||
goto EXECUTE_COMMAND_CLOSE;
|
||||
err = _getOutputStatus(fp, &st);
|
||||
if (err > WP_SHELL_ERR_NO_ERROR) {
|
||||
output = wpCmdNoExit(err);
|
||||
goto executeCommand_CLOSE;
|
||||
}
|
||||
|
||||
// Process is already closed in get_output_status
|
||||
// Process is already closed in _getOutputStatus
|
||||
fp = NULL;
|
||||
|
||||
output = (CMDResult){
|
||||
output = (WpCmdResult){
|
||||
.exited = true,
|
||||
.exit_code = st,
|
||||
.error = SHELL_ERR_NO_ERROR,
|
||||
.error = WP_SHELL_ERR_NO_ERROR,
|
||||
};
|
||||
|
||||
EXECUTE_COMMAND_CLOSE:
|
||||
executeCommand_CLOSE:
|
||||
if (fp) {
|
||||
wapp_shell_utils_pclose(fp);
|
||||
wpShellUtilsPclose(fp);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
wp_intern CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, WpStr8 *out_buf) {
|
||||
wp_intern WpCmdError getCommandOutput(FILE *fp, WpCmdOutHandling out_handling, WpStr8 *out_buf) {
|
||||
WpStr8 out = wpStr8Buf(OUT_BUF_LEN);
|
||||
|
||||
out.size = fread((void *)out.buf, sizeof(c8), out.capacity, fp);
|
||||
if (out_handling == SHELL_OUTPUT_CAPTURE && out_buf != NULL) {
|
||||
if (out_handling == WP_SHELL_OUTPUT_CAPTURE && out_buf != NULL) {
|
||||
if (out.size >= out_buf->capacity) {
|
||||
return SHELL_ERR_OUT_BUF_FULL;
|
||||
return WP_SHELL_ERR_OUT_BUF_FULL;
|
||||
}
|
||||
|
||||
wpStr8ConcatCapped(out_buf, &out);
|
||||
} else if (out_handling == SHELL_OUTPUT_PRINT) {
|
||||
} else if (out_handling == WP_SHELL_OUTPUT_PRINT) {
|
||||
printf(WP_STR8_SPEC, wpStr8Varg(out));
|
||||
}
|
||||
|
||||
return SHELL_ERR_NO_ERROR;
|
||||
return WP_SHELL_ERR_NO_ERROR;
|
||||
}
|
||||
|
||||
@@ -16,11 +16,11 @@
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // !WP_PLATFORM_CPP
|
||||
|
||||
#define CMD_NO_EXIT(ERR) ((CMDResult){.exited = false, .exit_code = EXIT_FAILURE, .error = ERR})
|
||||
#define wpCmdNoExit(ERR) ((WpCmdResult){.exited = false, .exit_code = EXIT_FAILURE, .error = ERR})
|
||||
|
||||
CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, WpStr8 *out_buf, const WpStr8List *cmd);
|
||||
WpCmdResult wpShellCommanderExecute(WpCmdOutHandling out_handling, WpStr8 *out_buf, const WpStr8List *cmd);
|
||||
|
||||
wp_extern CMDError get_output_status(FILE *fp, i32 *status_out);
|
||||
wp_extern WpCmdError _getOutputStatus(FILE *fp, i32 *status_out);
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
|
||||
@@ -12,27 +12,27 @@ BEGIN_C_LINKAGE
|
||||
#endif // !WP_PLATFORM_CPP
|
||||
|
||||
typedef enum {
|
||||
SHELL_OUTPUT_DISCARD,
|
||||
SHELL_OUTPUT_PRINT,
|
||||
SHELL_OUTPUT_CAPTURE,
|
||||
} CMDOutHandling;
|
||||
WP_SHELL_OUTPUT_DISCARD,
|
||||
WP_SHELL_OUTPUT_PRINT,
|
||||
WP_SHELL_OUTPUT_CAPTURE,
|
||||
} WpCmdOutHandling;
|
||||
|
||||
typedef enum {
|
||||
SHELL_ERR_NO_ERROR,
|
||||
SHELL_ERR_INVALID_ARGS,
|
||||
SHELL_ERR_ALLOCATION_FAIL,
|
||||
SHELL_ERR_PROC_START_FAIL,
|
||||
SHELL_ERR_OUT_BUF_FULL,
|
||||
SHELL_ERR_PROC_EXIT_FAIL,
|
||||
} CMDError;
|
||||
WP_SHELL_ERR_NO_ERROR,
|
||||
WP_SHELL_ERR_INVALID_ARGS,
|
||||
WP_SHELL_ERR_ALLOCATION_FAIL,
|
||||
WP_SHELL_ERR_PROC_START_FAIL,
|
||||
WP_SHELL_ERR_OUT_BUF_FULL,
|
||||
WP_SHELL_ERR_PROC_EXIT_FAIL,
|
||||
} WpCmdError;
|
||||
|
||||
typedef struct CMDResult CMDResult;
|
||||
struct CMDResult {
|
||||
typedef struct WpCmdResult WpCmdResult;
|
||||
struct WpCmdResult {
|
||||
i32 exit_code;
|
||||
CMDError error;
|
||||
WpCmdError error;
|
||||
b8 exited;
|
||||
|
||||
wpMiscUtilsReservePadding(sizeof(b8) + sizeof(i32) + sizeof(CMDError));
|
||||
wpMiscUtilsReservePadding(sizeof(b8) + sizeof(i32) + sizeof(WpCmdError));
|
||||
};
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
|
||||
@@ -10,16 +10,16 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
CMDError get_output_status(FILE *fp, i32 *status_out) {
|
||||
*status_out = wapp_shell_utils_pclose(fp);
|
||||
WpCmdError _getOutputStatus(FILE *fp, i32 *status_out) {
|
||||
*status_out = wpShellUtilsPclose(fp);
|
||||
|
||||
if (!WIFEXITED(*status_out)) {
|
||||
return SHELL_ERR_PROC_EXIT_FAIL;
|
||||
return WP_SHELL_ERR_PROC_EXIT_FAIL;
|
||||
}
|
||||
|
||||
*status_out = WEXITSTATUS(*status_out);
|
||||
|
||||
return SHELL_ERR_NO_ERROR;
|
||||
return WP_SHELL_ERR_NO_ERROR;
|
||||
}
|
||||
|
||||
#endif // !WP_PLATFORM_POSIX
|
||||
|
||||
@@ -9,16 +9,16 @@
|
||||
#include "../../utils/shell_utils.h"
|
||||
#include <stdio.h>
|
||||
|
||||
CMDError get_output_status(FILE *fp, i32 *status_out) {
|
||||
WpCmdError _getOutputStatus(FILE *fp, i32 *status_out) {
|
||||
if (!feof(fp)) {
|
||||
// Ensure process is closed on failure
|
||||
wapp_shell_utils_pclose(fp);
|
||||
return SHELL_ERR_PROC_EXIT_FAIL;
|
||||
wpShellUtilsPclose(fp);
|
||||
return WP_SHELL_ERR_PROC_EXIT_FAIL;
|
||||
}
|
||||
|
||||
*status_out = wapp_shell_utils_pclose(fp);
|
||||
*status_out = wpShellUtilsPclose(fp);
|
||||
|
||||
return SHELL_ERR_NO_ERROR;
|
||||
return WP_SHELL_ERR_NO_ERROR;
|
||||
}
|
||||
|
||||
#endif // !WP_PLATFORM_WINDOWS
|
||||
|
||||
@@ -9,9 +9,9 @@ WpTestFuncResult test_commander_cmd_success(void) {
|
||||
wpDblListPushBack(WpStr8, &cmd, &wpStr8Lit("echo"));
|
||||
wpDblListPushBack(WpStr8, &cmd, &wpStr8Lit("hello world"));
|
||||
|
||||
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_DISCARD, NULL, &cmd);
|
||||
WpCmdResult result = wpShellCommanderExecute(WP_SHELL_OUTPUT_DISCARD, NULL, &cmd);
|
||||
b8 succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
|
||||
result.error == SHELL_ERR_NO_ERROR;
|
||||
result.error == WP_SHELL_ERR_NO_ERROR;
|
||||
|
||||
return wpTesterResult(succeeded);
|
||||
}
|
||||
@@ -20,9 +20,9 @@ WpTestFuncResult test_commander_cmd_failure(void) {
|
||||
WpStr8List cmd = wpDblList(WpStr8);
|
||||
wpDblListPushBack(WpStr8, &cmd, &wpStr8Lit("grep"));
|
||||
|
||||
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_DISCARD, NULL, &cmd);
|
||||
WpCmdResult result = wpShellCommanderExecute(WP_SHELL_OUTPUT_DISCARD, NULL, &cmd);
|
||||
b8 failed = result.exited && result.exit_code != EXIT_SUCCESS &&
|
||||
result.error == SHELL_ERR_NO_ERROR;
|
||||
result.error == WP_SHELL_ERR_NO_ERROR;
|
||||
|
||||
return wpTesterResult(failed);
|
||||
}
|
||||
@@ -37,9 +37,9 @@ WpTestFuncResult test_commander_cmd_out_buf_success(void) {
|
||||
wpDblListPushBack(WpStr8, &cmd, &wpStr8Lit("echo"));
|
||||
wpDblListPushBack(WpStr8, &cmd, &wpStr8Lit(msg));
|
||||
|
||||
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
|
||||
WpCmdResult result = wpShellCommanderExecute(WP_SHELL_OUTPUT_CAPTURE, &buf, &cmd);
|
||||
b8 succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
|
||||
result.error == SHELL_ERR_NO_ERROR && wpStr8EqualToCount(&buf, &expected, strlen(msg));
|
||||
result.error == WP_SHELL_ERR_NO_ERROR && wpStr8EqualToCount(&buf, &expected, strlen(msg));
|
||||
|
||||
return wpTesterResult(succeeded);
|
||||
}
|
||||
@@ -54,9 +54,9 @@ WpTestFuncResult test_commander_cmd_out_buf_failure(void) {
|
||||
wpDblListPushBack(WpStr8, &cmd, &wpStr8Lit("echo"));
|
||||
wpDblListPushBack(WpStr8, &cmd, &wpStr8Lit(msg));
|
||||
|
||||
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
|
||||
WpCmdResult result = wpShellCommanderExecute(WP_SHELL_OUTPUT_CAPTURE, &buf, &cmd);
|
||||
b8 failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
|
||||
result.error == SHELL_ERR_OUT_BUF_FULL && !wpStr8Equal(&buf, &expected);
|
||||
result.error == WP_SHELL_ERR_OUT_BUF_FULL && !wpStr8Equal(&buf, &expected);
|
||||
|
||||
return wpTesterResult(failed);
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ WpTestFuncResult test_commander_cmd_success(void) {
|
||||
wpDblListPushBack(WpStr8, &cmd, &echo);
|
||||
wpDblListPushBack(WpStr8, &cmd, &msg);
|
||||
|
||||
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_DISCARD, nullptr, &cmd);
|
||||
WpCmdResult result = wpShellCommanderExecute(WP_SHELL_OUTPUT_DISCARD, nullptr, &cmd);
|
||||
b8 succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
|
||||
result.error == SHELL_ERR_NO_ERROR;
|
||||
result.error == WP_SHELL_ERR_NO_ERROR;
|
||||
|
||||
return wpTesterResult(succeeded);
|
||||
}
|
||||
@@ -23,9 +23,9 @@ WpTestFuncResult test_commander_cmd_failure(void) {
|
||||
WpStr8 grep = wpStr8Lit("grep");
|
||||
wpDblListPushBack(WpStr8, &cmd, &grep);
|
||||
|
||||
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_DISCARD, nullptr, &cmd);
|
||||
WpCmdResult result = wpShellCommanderExecute(WP_SHELL_OUTPUT_DISCARD, nullptr, &cmd);
|
||||
b8 failed = result.exited && result.exit_code != EXIT_SUCCESS &&
|
||||
result.error == SHELL_ERR_NO_ERROR;
|
||||
result.error == WP_SHELL_ERR_NO_ERROR;
|
||||
|
||||
return wpTesterResult(failed);
|
||||
}
|
||||
@@ -42,9 +42,9 @@ WpTestFuncResult test_commander_cmd_out_buf_success(void) {
|
||||
wpDblListPushBack(WpStr8, &cmd, &echo);
|
||||
wpDblListPushBack(WpStr8, &cmd, &arg);
|
||||
|
||||
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
|
||||
WpCmdResult result = wpShellCommanderExecute(WP_SHELL_OUTPUT_CAPTURE, &buf, &cmd);
|
||||
b8 succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
|
||||
result.error == SHELL_ERR_NO_ERROR && wpStr8EqualToCount(&buf, &expected, strlen(msg));
|
||||
result.error == WP_SHELL_ERR_NO_ERROR && wpStr8EqualToCount(&buf, &expected, strlen(msg));
|
||||
|
||||
return wpTesterResult(succeeded);
|
||||
}
|
||||
@@ -61,9 +61,9 @@ WpTestFuncResult test_commander_cmd_out_buf_failure(void) {
|
||||
wpDblListPushBack(WpStr8, &cmd, &echo);
|
||||
wpDblListPushBack(WpStr8, &cmd, &arg);
|
||||
|
||||
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
|
||||
WpCmdResult result = wpShellCommanderExecute(WP_SHELL_OUTPUT_CAPTURE, &buf, &cmd);
|
||||
b8 failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
|
||||
result.error == SHELL_ERR_OUT_BUF_FULL && !wpStr8Equal(&buf, &expected);
|
||||
result.error == WP_SHELL_ERR_OUT_BUF_FULL && !wpStr8Equal(&buf, &expected);
|
||||
|
||||
return wpTesterResult(failed);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user