Compare commits
No commits in common. "781a46713b2525b4028deecee0be3fa3ee49301f" and "25ab75f74f06a5bbb64efa6e8809c13b6faeeb4a" have entirely different histories.
781a46713b
...
25ab75f74f
@ -1,127 +0,0 @@
|
|||||||
#include "commander.h"
|
|
||||||
#include "aliases.h"
|
|
||||||
#include "shell_utils.h"
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define CMD_BUF_LEN 8192
|
|
||||||
#define OUT_BUF_LEN 4096
|
|
||||||
|
|
||||||
internal inline CMDError build_command_from_args(char *cmd, u64 buf_len,
|
|
||||||
va_list args);
|
|
||||||
internal inline CMDResult execute_command(const char *cmd,
|
|
||||||
CMDOutHandling out_handling,
|
|
||||||
char *out_buf, u64 buf_size);
|
|
||||||
internal inline CMDError get_command_output(FILE *fp,
|
|
||||||
CMDOutHandling out_handling,
|
|
||||||
char *out_buf, u64 buf_size);
|
|
||||||
internal inline CMDError get_output_status(FILE *fp, i32 *status_out);
|
|
||||||
|
|
||||||
// clang-format off
|
|
||||||
CMDResult run_command(CMDOutHandling out_handling, char *out_buf, u64 buf_size, ...) { // clang-format on
|
|
||||||
va_list args;
|
|
||||||
va_start(args, buf_size);
|
|
||||||
|
|
||||||
char cmd[CMD_BUF_LEN] = {0};
|
|
||||||
CMDError err = build_command_from_args(cmd, CMD_BUF_LEN, args);
|
|
||||||
if (err > SHELL_ERR_NO_ERROR) {
|
|
||||||
return CMD_NO_EXIT(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
return execute_command(cmd, out_handling, out_buf, buf_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal inline CMDError build_command_from_args(char *cmd, u64 buf_len,
|
|
||||||
va_list args) {
|
|
||||||
u64 size = 0;
|
|
||||||
u64 arg_len = 0;
|
|
||||||
|
|
||||||
const char *arg;
|
|
||||||
while ((arg = va_arg(args, const char *))) {
|
|
||||||
arg_len = strlen(arg);
|
|
||||||
if (arg_len >= buf_len - size) {
|
|
||||||
return SHELL_ERR_CMD_BUF_FULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
strcat(cmd, arg);
|
|
||||||
cmd[size + arg_len] = ' ';
|
|
||||||
|
|
||||||
size += arg_len + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return SHELL_ERR_NO_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal inline CMDResult execute_command(const char *cmd,
|
|
||||||
CMDOutHandling out_handling,
|
|
||||||
char *out_buf, u64 buf_size) {
|
|
||||||
FILE *fp = wapp_shell_utils_popen(cmd, "r");
|
|
||||||
if (!fp) {
|
|
||||||
return CMD_NO_EXIT(SHELL_ERR_PROC_START_FAIL);
|
|
||||||
}
|
|
||||||
|
|
||||||
CMDError err = get_command_output(fp, out_handling, out_buf, buf_size);
|
|
||||||
if (err > SHELL_ERR_NO_ERROR) {
|
|
||||||
return CMD_NO_EXIT(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 st = EXIT_SUCCESS;
|
|
||||||
err = get_output_status(fp, &st);
|
|
||||||
if (err > SHELL_ERR_NO_ERROR) {
|
|
||||||
return CMD_NO_EXIT(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (CMDResult){
|
|
||||||
.exited = true,
|
|
||||||
.exit_code = st,
|
|
||||||
.error = SHELL_ERR_NO_ERROR,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
internal inline CMDError get_command_output(FILE *fp,
|
|
||||||
CMDOutHandling out_handling,
|
|
||||||
char *out_buf, u64 buf_size) {
|
|
||||||
char out[OUT_BUF_LEN] = {0};
|
|
||||||
u64 max_out_length = OUT_BUF_LEN - 1;
|
|
||||||
|
|
||||||
u64 buf_filled = 0;
|
|
||||||
while (fgets(out, max_out_length, fp)) {
|
|
||||||
if (out_handling == SHELL_OUTPUT_CAPTURE && out_buf != NULL) {
|
|
||||||
buf_filled += strlen(out);
|
|
||||||
if (buf_filled >= buf_size) {
|
|
||||||
return SHELL_ERR_OUT_BUF_FULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
strcat(out_buf, out);
|
|
||||||
} else {
|
|
||||||
printf("%s", out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return SHELL_ERR_NO_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal inline CMDError get_output_status(FILE *fp, i32 *status_out) {
|
|
||||||
#ifdef WAPP_PLATFORM_WINDOWS
|
|
||||||
if (!feof(fp)) {
|
|
||||||
return SHELL_ERR_PROC_EXIT_FAIL;
|
|
||||||
}
|
|
||||||
#endif /* ifdef WAPP_PLATFORM_WINDOWS */
|
|
||||||
|
|
||||||
*status_out = wapp_shell_utils_pclose(fp);
|
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_POSIX
|
|
||||||
if (!WIFEXITED(*status_out)) {
|
|
||||||
return SHELL_ERR_PROC_EXIT_FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*status_out = WEXITSTATUS(*status_out);
|
|
||||||
#endif /* ifdef WAPP_PLATFORM_WINDOWS */
|
|
||||||
|
|
||||||
return SHELL_ERR_NO_ERROR;
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
#ifndef COMMANDER_H
|
|
||||||
#define COMMANDER_H
|
|
||||||
|
|
||||||
#include "aliases.h"
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#define CMD_NO_EXIT(ERR) \
|
|
||||||
((CMDResult){.exited = false, .exit_code = EXIT_FAILURE})
|
|
||||||
#define wapp_shell_commander_execute(HANDLE_OUTPUT, OUT_BUF, BUF_SIZE, ...) \
|
|
||||||
run_command(HANDLE_OUTPUT, OUT_BUF, BUF_SIZE, __VA_ARGS__, NULL)
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
SHELL_OUTPUT_PRINT,
|
|
||||||
SHELL_OUTPUT_CAPTURE,
|
|
||||||
} CMDOutHandling;
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
SHELL_ERR_NO_ERROR,
|
|
||||||
SHELL_ERR_CMD_BUF_FULL,
|
|
||||||
SHELL_ERR_PROC_START_FAIL,
|
|
||||||
SHELL_ERR_OUT_BUF_FULL,
|
|
||||||
SHELL_ERR_PROC_EXIT_FAIL,
|
|
||||||
} CMDError;
|
|
||||||
|
|
||||||
typedef struct commander_result CMDResult;
|
|
||||||
struct commander_result {
|
|
||||||
bool exited;
|
|
||||||
int exit_code;
|
|
||||||
CMDError error;
|
|
||||||
};
|
|
||||||
|
|
||||||
// clang-format off
|
|
||||||
CMDResult run_command(CMDOutHandling out_handling, char *out_buf, u64 buf_size, ...);
|
|
||||||
// clang-format on
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif // !COMMANDER_H
|
|
@ -1,16 +0,0 @@
|
|||||||
#ifndef SHELL_UTILS_H
|
|
||||||
#define SHELL_UTILS_H
|
|
||||||
|
|
||||||
#include "platform.h"
|
|
||||||
|
|
||||||
// clang-format off
|
|
||||||
#ifdef WAPP_PLATFORM_WINDOWS
|
|
||||||
#define wapp_shell_utils_popen _popen
|
|
||||||
#define wapp_shell_utils_pclose _pclose
|
|
||||||
#else
|
|
||||||
#define wapp_shell_utils_popen popen
|
|
||||||
#define wapp_shell_utils_pclose pclose
|
|
||||||
#endif /* ifdef WAPP_PLATFORM_WINDOWS */
|
|
||||||
// clang-format on
|
|
||||||
|
|
||||||
#endif // !SHELL_UTILS_H
|
|
@ -54,7 +54,7 @@ internal const char *colours[COUNT_TERM_COLOUR] = {
|
|||||||
};
|
};
|
||||||
#endif /* ifdef WAPP_PLATFORM_WINDOWS */
|
#endif /* ifdef WAPP_PLATFORM_WINDOWS */
|
||||||
|
|
||||||
TerminalColourist wapp_shell_termcolour_get_colourist(void) {
|
TerminalColourist wapp_termcolour_get_colourist(void) {
|
||||||
TerminalColourist colourist;
|
TerminalColourist colourist;
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_WINDOWS
|
#ifdef WAPP_PLATFORM_WINDOWS
|
||||||
@ -74,8 +74,8 @@ TerminalColourist wapp_shell_termcolour_get_colourist(void) {
|
|||||||
return colourist;
|
return colourist;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_shell_termcolour_print_text(TerminalColourist *colourist,
|
void wapp_termcolour_print_text(TerminalColourist *colourist, const char *text,
|
||||||
const char *text, TerminalColour colour) {
|
TerminalColour colour) {
|
||||||
if (colour < WAPP_TERM_COLOUR_FG_BLACK ||
|
if (colour < WAPP_TERM_COLOUR_FG_BLACK ||
|
||||||
colour > WAPP_TERM_COLOUR_FG_BR_WHITE) {
|
colour > WAPP_TERM_COLOUR_FG_BR_WHITE) {
|
||||||
return;
|
return;
|
||||||
@ -85,7 +85,7 @@ void wapp_shell_termcolour_print_text(TerminalColourist *colourist,
|
|||||||
print_coloured_text(colourist, text);
|
print_coloured_text(colourist, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_shell_termcolour_clear_colour(TerminalColourist *colourist) {
|
void wapp_termcolour_clear_colour(TerminalColourist *colourist) {
|
||||||
colourist->current_colour = colourist->default_colour;
|
colourist->current_colour = colourist->default_colour;
|
||||||
print_coloured_text(colourist, "");
|
print_coloured_text(colourist, "");
|
||||||
}
|
}
|
@ -5,10 +5,6 @@
|
|||||||
#include "misc_utils.h"
|
#include "misc_utils.h"
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
WAPP_TERM_COLOUR_FG_BLACK,
|
WAPP_TERM_COLOUR_FG_BLACK,
|
||||||
WAPP_TERM_COLOUR_FG_RED,
|
WAPP_TERM_COLOUR_FG_RED,
|
||||||
@ -52,13 +48,9 @@ struct terminal_colourist {
|
|||||||
#endif // WAPP_PLATFORM_WINDOWS
|
#endif // WAPP_PLATFORM_WINDOWS
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
TerminalColourist wapp_shell_termcolour_get_colourist(void);
|
TerminalColourist wapp_termcolour_get_colourist(void);
|
||||||
void wapp_shell_termcolour_print_text(TerminalColourist *colourist,
|
void wapp_termcolour_print_text(TerminalColourist *colourist, const char *text,
|
||||||
const char *text, TerminalColour colour);
|
TerminalColour colour);
|
||||||
void wapp_shell_termcolour_clear_colour(TerminalColourist *colourist);
|
void wapp_termcolour_clear_colour(TerminalColourist *colourist);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif // !TERM_COLOUR_H
|
#endif // !TERM_COLOUR_H
|
@ -10,7 +10,7 @@ internal void handle_test_result(TerminalColourist *colourist,
|
|||||||
TestFuncResult result);
|
TestFuncResult result);
|
||||||
|
|
||||||
void run_tests(TestFunc *func1, ...) {
|
void run_tests(TestFunc *func1, ...) {
|
||||||
TerminalColourist colourist = wapp_shell_termcolour_get_colourist();
|
TerminalColourist colourist = wapp_termcolour_get_colourist();
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
@ -47,8 +47,8 @@ internal void handle_test_result(TerminalColourist *colourist,
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("[");
|
printf("[");
|
||||||
wapp_shell_termcolour_print_text(colourist, result_text, colour);
|
wapp_termcolour_print_text(colourist, result_text, colour);
|
||||||
wapp_shell_termcolour_clear_colour(colourist);
|
wapp_termcolour_clear_colour(colourist);
|
||||||
printf("] %s\n", result.name);
|
printf("] %s\n", result.name);
|
||||||
|
|
||||||
if (!result.passed) {
|
if (!result.passed) {
|
||||||
|
Loading…
Reference in New Issue
Block a user