From 1fc93fac240b22ca57df28bf51ae1f9be9417c71 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Fri, 26 Jun 2026 17:39:05 +0100 Subject: [PATCH] Rename shell commander --- src/os/shell/commander/commander.c | 62 +++++++++---------- src/os/shell/commander/commander.h | 6 +- src/os/shell/commander/commander_output.h | 30 ++++----- .../shell/commander/posix/commander_posix.c | 8 +-- src/os/shell/commander/win/commander_win.c | 10 +-- tests/shell_commander/test_shell_commander.c | 16 ++--- tests/shell_commander/test_shell_commander.cc | 16 ++--- 7 files changed, 74 insertions(+), 74 deletions(-) diff --git a/src/os/shell/commander/commander.c b/src/os/shell/commander/commander.c index 49a18d1..476c202 100644 --- a/src/os/shell/commander/commander.c +++ b/src/os/shell/commander/commander.c @@ -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; } diff --git a/src/os/shell/commander/commander.h b/src/os/shell/commander/commander.h index 1abde2f..891a4f4 100644 --- a/src/os/shell/commander/commander.h +++ b/src/os/shell/commander/commander.h @@ -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 diff --git a/src/os/shell/commander/commander_output.h b/src/os/shell/commander/commander_output.h index d43592c..4d18d48 100644 --- a/src/os/shell/commander/commander_output.h +++ b/src/os/shell/commander/commander_output.h @@ -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 diff --git a/src/os/shell/commander/posix/commander_posix.c b/src/os/shell/commander/posix/commander_posix.c index 7409f94..0b1069c 100644 --- a/src/os/shell/commander/posix/commander_posix.c +++ b/src/os/shell/commander/posix/commander_posix.c @@ -10,16 +10,16 @@ #include #include -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 diff --git a/src/os/shell/commander/win/commander_win.c b/src/os/shell/commander/win/commander_win.c index 7375ed8..daf764e 100644 --- a/src/os/shell/commander/win/commander_win.c +++ b/src/os/shell/commander/win/commander_win.c @@ -9,16 +9,16 @@ #include "../../utils/shell_utils.h" #include -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 diff --git a/tests/shell_commander/test_shell_commander.c b/tests/shell_commander/test_shell_commander.c index 26e3b03..93d82d8 100644 --- a/tests/shell_commander/test_shell_commander.c +++ b/tests/shell_commander/test_shell_commander.c @@ -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); } diff --git a/tests/shell_commander/test_shell_commander.cc b/tests/shell_commander/test_shell_commander.cc index 2555901..d22aa59 100644 --- a/tests/shell_commander/test_shell_commander.cc +++ b/tests/shell_commander/test_shell_commander.cc @@ -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); }