feat: single quotes expansion
This commit is contained in:
parent
6f2aefa82e
commit
e8d6ac902c
2 changed files with 80 additions and 10 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef AST_H
|
#ifndef AST_H
|
||||||
#define AST_H
|
#define AST_H
|
||||||
|
|
||||||
#include "lists.h"
|
#include "../utils/lists/lists.h"
|
||||||
|
|
||||||
enum ast_type
|
enum ast_type
|
||||||
{
|
{
|
||||||
|
|
@ -10,16 +10,17 @@ enum ast_type
|
||||||
AST_CMD
|
AST_CMD
|
||||||
};
|
};
|
||||||
|
|
||||||
union ast_node
|
struct ast_cmd
|
||||||
{
|
{
|
||||||
struct ast_if;
|
struct list *cmd;
|
||||||
struct ast_cmd;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
union ast_node;
|
||||||
|
|
||||||
struct ast
|
struct ast
|
||||||
{
|
{
|
||||||
enum ast_type type;
|
enum ast_type type;
|
||||||
union ast_node data;
|
union ast_node *data;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ast_if
|
struct ast_if
|
||||||
|
|
@ -29,9 +30,10 @@ struct ast_if
|
||||||
struct ast *else_clause;
|
struct ast *else_clause;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ast_cmd
|
union ast_node
|
||||||
{
|
{
|
||||||
struct list* cmd;
|
struct ast_if ast_if;
|
||||||
|
struct ast_cmd ast_cmd;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* ! AST_H */
|
#endif /* ! AST_H */
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "../ast/ast.h"
|
||||||
|
|
||||||
|
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' && str[i + 1] != '$'
|
||||||
|
// && str[i + 1] != ' ')
|
||||||
|
else if (str[i] == '$' && isalnum(str[i + 1]))
|
||||||
|
{
|
||||||
|
// variable expansion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue