From 182e31b42ea5666f3fb522a08f681dfaff6332fa Mon Sep 17 00:00:00 2001 From: Jean <47366872+jean-voila@users.noreply.github.com> Date: Sat, 31 Jan 2026 15:40:46 +0100 Subject: [PATCH] fix(chaipa): Remove pretty print to avoid clang-tidy / added the options in echo --- src/execution/execution_helpers.c | 82 +++++++++++++++++++++++++++++-- src/main.c | 10 +--- src/utils/args/args.c | 13 ++--- src/utils/args/args.h | 2 +- src/utils/ast/ast.c | 3 +- src/utils/ast/ast.h | 5 -- tests/unit/utils/args.c | 25 ++++++---- 7 files changed, 106 insertions(+), 34 deletions(-) diff --git a/src/execution/execution_helpers.c b/src/execution/execution_helpers.c index e65c0d1..ebb833d 100644 --- a/src/execution/execution_helpers.c +++ b/src/execution/execution_helpers.c @@ -2,6 +2,7 @@ #include "execution_helpers.h" #include +#include #include #include #include @@ -285,20 +286,95 @@ int exec_ast_loop(struct ast_loop *loop_node, struct hash_map *vars) // --- Builtins --- +static void print_with_escapes(const char *str) +{ + while (*str) + { + if (*str == '\\') + { + str++; + if (*str == 'n') + putchar('\n'); + else if (*str == 't') + putchar('\t'); + else if (*str == '\\') + putchar('\\'); + else if (*str == 'a') + putchar('\a'); + else if (*str == 'b') + putchar('\b'); + else if (*str == 'f') + putchar('\f'); + else if (*str == 'r') + putchar('\r'); + else if (*str == 'v') + putchar('\v'); + else if (*str == 'c') + return; // stop printing + else + { + // unrecognized escape, print "\"" and the char + putchar('\\'); + if (*str) + putchar(*str); + } + } + else + { + putchar(*str); + } + str++; + } +} + static int builtin_echo(char **argv) { bool newline = true; + bool interpret_escapes = false; int i = 1; - if (argv[1] && strcmp(argv[1], "-n") == 0) + // Parse options + while (argv[i] && argv[i][0] == '-') { - newline = false; + char *opt = argv[i] + 1; // skip "-" + bool valid_option = false; + while (*opt) + { + if (*opt == 'n') + { + newline = false; + valid_option = true; + } + else if (*opt == 'e') + { + interpret_escapes = true; + valid_option = true; + } + else if (*opt == 'E') + { + interpret_escapes = false; + valid_option = true; + } + else + { + // invalid option so euh treat as regular argument + valid_option = false; + break; + } + opt++; + } + if (!valid_option) + break; // stop parsing options i++; } + // Print arguments for (; argv[i]; i++) { - printf("%s", argv[i]); + if (interpret_escapes) + print_with_escapes(argv[i]); + else + printf("%s", argv[i]); if (argv[i + 1]) printf(" "); } diff --git a/src/main.c b/src/main.c index 1b86854..02320b9 100644 --- a/src/main.c +++ b/src/main.c @@ -28,8 +28,7 @@ static int err_input(struct hash_map **vars, struct lexer_context *ctx) return ERR_INPUT_PROCESSING; } -static int main_loop(struct lexer_context *ctx, struct args_options *options, - struct hash_map *vars) +static int main_loop(struct lexer_context *ctx, struct hash_map *vars) { int return_code = SUCCESS; // init parser @@ -41,11 +40,6 @@ static int main_loop(struct lexer_context *ctx, struct args_options *options, // Retrieve and build first AST struct ast *command_ast = get_ast(ctx); - if (options->pretty_print) - { - ast_print_dot(command_ast); - } - // Main parse-execute loop while (command_ast != NULL && command_ast->type != AST_END) { @@ -123,7 +117,7 @@ int main(int argc, char **argv) // init lexer context struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context)); - return_code = main_loop(ctx, &options, vars); + return_code = main_loop(ctx, vars); return return_code; } diff --git a/src/utils/args/args.c b/src/utils/args/args.c index c34599f..69374e2 100644 --- a/src/utils/args/args.c +++ b/src/utils/args/args.c @@ -67,7 +67,7 @@ int args_handler(int argc, char **argv, struct args_options *options, { options->type = INPUT_UNDEFINED; options->input_source = NULL; - options->pretty_print = false; + // options->pretty_print = false; options->verbose = false; struct list *args_list = NULL; @@ -76,11 +76,11 @@ int args_handler(int argc, char **argv, struct args_options *options, for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "--pretty-print") == 0) + /* if (strcmp(argv[i], "--pretty-print") == 0) { options->pretty_print = true; - } - else if (strcmp(argv[i], "--verbose") == 0) + } */ + if (strcmp(argv[i], "--verbose") == 0) { options->verbose = true; } @@ -138,7 +138,7 @@ void args_print(struct args_options *options) : "UNDEFINED"); printf("Input source: %s\n", options->input_source ? options->input_source : "NULL"); - printf("Pretty print: %s\n", options->pretty_print ? "true" : "false"); + // printf("Pretty print: %s\n", options->pretty_print ? "true" : "false"); printf("Verbose: %s\n", options->verbose ? "true" : "false"); } @@ -147,7 +147,8 @@ void print_usage(FILE *std, const char *program_name) fprintf(std, "Usage: %s [OPTIONS] [SCRIPT] [ARGUMENTS...]\n", program_name); fprintf(std, "Options:\n"); fprintf(std, " -c [SCRIPT] Execute the given command string.\n"); - fprintf(std, " --pretty-print Enable pretty printing of outputs.\n"); + // fprintf(std, " --pretty-print Enable pretty printing of + // outputs.\n"); fprintf(std, " --verbose Enable verbose mode.\n"); fprintf(std, "If no SCRIPT is provided, input is read from standard input.\n"); diff --git a/src/utils/args/args.h b/src/utils/args/args.h index 6eac20c..3c02fc4 100644 --- a/src/utils/args/args.h +++ b/src/utils/args/args.h @@ -22,7 +22,7 @@ struct args_options /** Type of the input source */ enum input_type type; /** Enable or disable pretty printing of outputs */ - bool pretty_print; + // bool pretty_print; /** Enable or disable verbose mode */ bool verbose; }; diff --git a/src/utils/ast/ast.c b/src/utils/ast/ast.c index 5f5f65b..5baaf6a 100644 --- a/src/utils/ast/ast.c +++ b/src/utils/ast/ast.c @@ -79,7 +79,7 @@ struct ast *ast_create(enum ast_type type, void *data) return node; } -// TODO handle new types (AST_WORD, AST_PIPE, etc.) +/* // TODO handle new types (AST_WORD, AST_PIPE, etc.) static void ast_print_dot_recursive(struct ast *node, FILE *out) { if (!node) @@ -170,3 +170,4 @@ void ast_print_dot(struct ast *ast) fprintf(dot_pipe, "}\n"); pclose(dot_pipe); } + */ \ No newline at end of file diff --git a/src/utils/ast/ast.h b/src/utils/ast/ast.h index f0023f3..12abee5 100644 --- a/src/utils/ast/ast.h +++ b/src/utils/ast/ast.h @@ -16,9 +16,4 @@ #include "ast_void.h" #include "ast_word.h" -/** - * Prints the Graphviz DOT representation of the given AST to stdout. - */ -void ast_print_dot(struct ast *ast); - #endif /* ! AST_H */ diff --git a/tests/unit/utils/args.c b/tests/unit/utils/args.c index 672f6e9..9f601bf 100644 --- a/tests/unit/utils/args.c +++ b/tests/unit/utils/args.c @@ -19,7 +19,7 @@ Test(utils_args, basic_command) int r = args_handler(argc, input, &options, vars); cr_expect(r == 0); - cr_expect(options.pretty_print == false); + // cr_expect(options.pretty_print == false); cr_expect(options.verbose == false); cr_expect(options.type == INPUT_CMD); cr_expect(eq(options.input_source, "echo Hello, World!")); @@ -30,14 +30,16 @@ Test(utils_args, basic_command_with_flags) { int argc = 5; struct args_options options; - char *input[] = { "program", "--pretty-print", "-c", "echo Hello, World!", - "--verbose" }; + /* char *input[] = { "program", "--pretty-print", "-c", "echo Hello, + World!", + "--verbose" };*/ + char *input[] = { "program", "-c", "echo Hello, World!", "--verbose" }; struct hash_map *vars = vars_init(); int r = args_handler(argc, input, &options, vars); cr_expect(r == 0); - cr_expect(options.pretty_print == true); + // cr_expect(options.pretty_print == true); cr_expect(options.verbose == true); cr_expect(options.type == INPUT_CMD); cr_expect(eq(options.input_source, "echo Hello, World!")); @@ -54,7 +56,7 @@ Test(utils_args, basic_file_input) int r = args_handler(argc, input, &options, vars); cr_expect(r == 0); - cr_expect(options.pretty_print == false); + // cr_expect(options.pretty_print == false); cr_expect(options.verbose == false); cr_expect(options.type == INPUT_FILE); cr_expect(eq(options.input_source, "input.txt")); @@ -65,13 +67,15 @@ Test(utils_args, basic_file_input_with_flags) { int argc = 4; struct args_options options; - char *input[] = { "program", "--verbose", "input.txt", "--pretty-print" }; + // char *input[] = { "program", "--verbose", "input.txt", "--pretty-print" + // }; + char *input[] = { "program", "--verbose", "input.txt" }; struct hash_map *vars = vars_init(); int r = args_handler(argc, input, &options, vars); cr_expect(r == 0); - cr_expect(options.pretty_print == true); + // cr_expect(options.pretty_print == true); cr_expect(options.verbose == true); cr_expect(options.type == INPUT_FILE); cr_expect(eq(options.input_source, "input.txt")); @@ -88,7 +92,7 @@ Test(utils_args, basic_stdin_input) int r = args_handler(argc, input, &options, vars); cr_expect(r == 0); - cr_expect(options.pretty_print == false); + // cr_expect(options.pretty_print == false); cr_expect(options.verbose == false); cr_expect(options.type == INPUT_STDIN); cr_expect(options.input_source == NULL); @@ -99,13 +103,14 @@ Test(utils_args, pretty_print_and_verbose_flags) { int argc = 3; struct args_options options; - char *input[] = { "program", "--pretty-print", "--verbose" }; + // char *input[] = { "program", "--pretty-print", "--verbose" }; + char *input[] = { "program", "--verbose" }; struct hash_map *vars = vars_init(); int r = args_handler(argc, input, &options, vars); cr_expect(r == 0); - cr_expect(options.pretty_print == true); + // cr_expect(options.pretty_print == true); cr_expect(options.verbose == true); cr_expect(options.type == INPUT_STDIN); cr_expect(options.input_source == NULL);