fix(chaipa): Remove pretty print to avoid clang-tidy / added the options in echo
This commit is contained in:
parent
3cd231f031
commit
182e31b42e
7 changed files with 106 additions and 34 deletions
|
|
@ -2,6 +2,7 @@
|
|||
#include "execution_helpers.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
|
@ -285,19 +286,94 @@ 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] == '-')
|
||||
{
|
||||
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++)
|
||||
{
|
||||
if (interpret_escapes)
|
||||
print_with_escapes(argv[i]);
|
||||
else
|
||||
printf("%s", argv[i]);
|
||||
if (argv[i + 1])
|
||||
printf(" ");
|
||||
|
|
|
|||
10
src/main.c
10
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
*/
|
||||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue