fix(chaipa): Remove pretty print to avoid clang-tidy / added the options in echo

This commit is contained in:
Jean 2026-01-31 15:40:46 +01:00
parent 3cd231f031
commit 182e31b42e
7 changed files with 106 additions and 34 deletions

View file

@ -2,6 +2,7 @@
#include "execution_helpers.h"
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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(" ");
}

View file

@ -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;
}

View file

@ -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");

View file

@ -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;
};

View file

@ -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);
}
*/

View file

@ -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 */