diff --git a/src/utils/args/args.c b/src/utils/args/args.c index 89a00a3..a957382 100644 --- a/src/utils/args/args.c +++ b/src/utils/args/args.c @@ -1,12 +1,67 @@ +#define _POSIX_C_SOURCE 200809L #include "./args.h" #include #include +#include #include +#include "../lists/lists.h" #include "../string_utils/string_utils.h" #include "../vars/vars.h" +static void strlen_acc(void *acc, void *data) +{ + size_t *len = (size_t *)acc; + char *str = (char *)data; + *len += strlen(str); +} + +static char *concat_list_str(struct list *list) +{ + char *res = NULL; + size_t total_len = list_length(list) + 1; // + spaces + null terminator + + list_fold(list, &total_len, strlen_acc); + + res = malloc(total_len); + if (res != NULL) + { + res[0] = 0; + for (struct list *it = list; it != NULL; it = it->next) + { + strcat(res, (char *)it->data); + if (it->next != NULL) + strcat(res, " "); + } + res[total_len - 1] = 0; + } + + return res; +} + +static void args_in_var(struct hash_map *vars, struct list *args_list) +{ + int arg_index = 1; + char index_str[11]; + + for (struct list *it = args_list; it != NULL; it = it->next) + { + int_to_str(arg_index++, index_str); + set_var_copy(vars, index_str, (char *)it->data); + } + + char *concated_args = concat_list_str(args_list); + set_var_copy(vars, "*", concated_args); + + char *key = strdup("@"); + set_var(vars, key, concated_args, NULL); + // key and concated_args consumed by hash_map + + int_to_str(arg_index - 1, index_str); + set_var_copy(vars, "#", index_str); +} + int args_handler(int argc, char **argv, struct args_options *options, struct hash_map *vars) { @@ -15,8 +70,7 @@ int args_handler(int argc, char **argv, struct args_options *options, options->pretty_print = false; options->verbose = false; - int arg_index = 1; - char index_str[11]; + struct list *args_list = NULL; set_var_copy(vars, "0", argv[0]); @@ -61,14 +115,13 @@ int args_handler(int argc, char **argv, struct args_options *options, else { // All remaining arguments are treated as additional arguments - int_to_str(arg_index++, index_str); - set_var_copy(vars, index_str, argv[i]); + args_list = list_append(args_list, argv[i]); continue; } } - int_to_str(arg_index - 1, index_str); - set_var_copy(vars, "#", index_str); + args_in_var(vars, args_list); + list_destroy(args_list); if (options->type == INPUT_UNDEFINED) options->type = INPUT_STDIN; diff --git a/src/utils/lists/lists.h b/src/utils/lists/lists.h index a0fedf3..9b5d38e 100644 --- a/src/utils/lists/lists.h +++ b/src/utils/lists/lists.h @@ -107,4 +107,9 @@ int list_find(struct list *list, void *value); // struct list *list_split(struct list *list, size_t index); // END PROTO list_split +/** + * @brief: Folds the list from left to right using func and an accumulator. + */ +void list_fold(struct list *list, void *acc, void (*func)(void *, void *)); + #endif /* ! LISTS_H */ diff --git a/src/utils/lists/lists3.c b/src/utils/lists/lists3.c index e9cc545..c18c7be 100644 --- a/src/utils/lists/lists3.c +++ b/src/utils/lists/lists3.c @@ -115,3 +115,13 @@ void list_deep_destroy(struct list *l) elt = next_elt; } } + +void list_fold(struct list *list, void *acc, void (*func)(void *, void *)) +{ + struct list *elt = list; + while (elt != NULL) + { + func(acc, elt->data); + elt = elt->next; + } +}