#include #include #include #include #include #include "../ast/ast.h" // static size_t var_len(char *start) // { // char *iter = start; // while (*iter != ' ' && *iter != 0) // *iter++; // return iter - start; // } struct ast_cmd *expand(struct ast_cmd *cmd) { if (cmd == NULL) return NULL; bool in_quotes = false; char *str; size_t len; struct list *l = cmd->cmd; while (l != NULL) { in_quotes = false; str = (char *)l->data; len = strlen(str); for (size_t i = 0; str[i] != '\0'; i++) { if (in_quotes) { // do nothing } else if (str[i] == '\'') { in_quotes = !in_quotes; memmove(&str[i], &str[i + 1], strlen(&str[i + 1]) + 1); } else if (str[i] == '$' && str[i + 1] != 0 && !isspace(str[i + 1])) { // size_t len = var_len(str + i + 1); // char *end = str + i + len + 1; // char c = *end; // *end = 0; // printf("var: %s\n", str + i + 1); // *end = c; } } if (in_quotes) { // error: quote not closed } if (len != strlen(str)) { char *new_str = realloc(str, strlen(str) + 1); if (new_str == NULL) { // error: realloc fail } l->data = new_str; } l = l->next; } return cmd; } // int main() // { // printf("Expansion module test\n"); // struct ast_cmd ast_cmd; // // char str[] = "echo Hello $?"; // char str[] = "echo Hello $AE86"; // ast_cmd.cmd = list_append(NULL, str); // struct ast_cmd *cmd2 = expand(&ast_cmd); // printf("cmd2: %s\n", (char *)cmd2->cmd->data); // return 0; // }