a998f6b981
Release / release (push) Successful in 8s
## Summary Standardize naming conventions across the entire wizapp-stdlib codebase by replacing inconsistent prefixes and snake_case with a unified `wp` prefix + CamelCase scheme. ## Changes ### Naming convention applied | Pattern | Before | After | |---|---|---| | Public functions | `wapp_module_function` | `wpModuleFunction` | | Public types | `GenericXxx`, bare `Xxx` | `WpXxx` | | Constants / enum values | `WAPP_XXX`, `SHELL_XXX` | `WP_XXX`, `WP_SHELL_XXX` | | Internal functions | `_module_function` | `_moduleFunction` | | Storage-class macros | `wapp_extern`, `wapp_intern` | `wp_extern`, `wp_intern` | ### Modules affected All 20 modules were renamed: `arena`, `array`, `dbl_list`, `queue`, `str8`, `mem_allocator`, `mem_utils`, `mem_os`, `file`, `cpath`, `log`, `shell_commander`, `shell_termcolour`, `shell_utils`, `prng/xorshift`, `uuid`, `tester`, `aliases`, `assert`, `misc_utils`, `platform` — plus their test files. ### Backward compatibility Added `src/oldnames.h` with `#define OLD_NAME NEW_NAME` for every renamed symbol, organized by module under Constants → Types → Functions sections. Existing code that includes this file will compile without changes. Reviewed-on: #12 Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com> Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
102 lines
2.7 KiB
C
102 lines
2.7 KiB
C
// vim:fileencoding=utf-8:foldmethod=marker
|
|
|
|
#include "commander.h"
|
|
#include "commander_output.h"
|
|
#include "../utils/shell_utils.h"
|
|
#include "../../allocators/arena/mem_arena_allocator.h"
|
|
#include "../../../common/aliases/aliases.h"
|
|
#include "../../../common/misc/misc_utils.h"
|
|
#include "../../../base/dbl_list/dbl_list.h"
|
|
#include "../../../base/mem/allocator/mem_allocator.h"
|
|
#include "../../../base/strings/str8/str8.h"
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define CMD_BUF_LEN 8192
|
|
#define OUT_BUF_LEN 4096
|
|
|
|
wp_intern WpCmdResult executeCommand(WpStr8RO *cmd, WpCmdOutHandling out_handling, WpStr8 *out_buf);
|
|
wp_intern WpCmdError getCommandOutput(FILE *fp, WpCmdOutHandling out_handling, WpStr8 *out_buf);
|
|
|
|
WpCmdResult wpShellCommanderExecute(WpCmdOutHandling out_handling, WpStr8 *out_buf, const WpStr8List *cmd) {
|
|
if (!cmd) {
|
|
return wpCmdNoExit(WP_SHELL_ERR_INVALID_ARGS);
|
|
}
|
|
|
|
WpAllocator arena = wpMemArenaAllocatorInit(KiB(500));
|
|
|
|
WpStr8 *cmd_str = wpStr8Join(&arena, cmd, &wpStr8LitRo(" "));
|
|
if (!cmd_str) {
|
|
wpMemArenaAllocatorDestroy(&arena);
|
|
return wpCmdNoExit(WP_SHELL_ERR_ALLOCATION_FAIL);
|
|
}
|
|
|
|
// Redirect output
|
|
cmd_str = wpStr8AllocConcat(&arena, cmd_str, &wpStr8LitRo(" 2>&1"));
|
|
|
|
WpCmdResult output = executeCommand(cmd_str, out_handling, out_buf);
|
|
|
|
wpMemArenaAllocatorDestroy(&arena);
|
|
|
|
return output;
|
|
}
|
|
|
|
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 = wpShellUtilsPopen(cmd_buf, "r");
|
|
if (!fp) {
|
|
return wpCmdNoExit(WP_SHELL_ERR_PROC_START_FAIL);
|
|
}
|
|
|
|
WpCmdResult output;
|
|
|
|
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 = _getOutputStatus(fp, &st);
|
|
if (err > WP_SHELL_ERR_NO_ERROR) {
|
|
output = wpCmdNoExit(err);
|
|
goto executeCommand_CLOSE;
|
|
}
|
|
|
|
// Process is already closed in _getOutputStatus
|
|
fp = NULL;
|
|
|
|
output = (WpCmdResult){
|
|
.exited = true,
|
|
.exit_code = st,
|
|
.error = WP_SHELL_ERR_NO_ERROR,
|
|
};
|
|
|
|
executeCommand_CLOSE:
|
|
if (fp) {
|
|
wpShellUtilsPclose(fp);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
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 == WP_SHELL_OUTPUT_CAPTURE && out_buf != NULL) {
|
|
if (out.size >= out_buf->capacity) {
|
|
return WP_SHELL_ERR_OUT_BUF_FULL;
|
|
}
|
|
|
|
wpStr8ConcatCapped(out_buf, &out);
|
|
} else if (out_handling == WP_SHELL_OUTPUT_PRINT) {
|
|
printf(WP_STR8_SPEC, wpStr8Varg(out));
|
|
}
|
|
|
|
return WP_SHELL_ERR_NO_ERROR;
|
|
}
|