diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 129b3cfe..294ab517 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:59:06 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 15:04:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,6 +50,7 @@ enum e_lexstate QUOTE, DQUOTE, BACKSLASH, + VAR, }; struct s_token @@ -71,6 +72,7 @@ int token_append(t_token *token, char c); void token_free(void *data, size_t size); int token_cmp_type(t_token *token, t_type *ref); void token_print(t_list *lst); +void token_expand_var(t_token *token); int ft_is_delim(char c); @@ -87,5 +89,6 @@ int lexer_greatand(t_list **alst, char *str); int lexer_quote(t_list **alst, char *str); int lexer_dquote(t_list **alst, char *str); int lexer_backslash(t_list **alst, char *str); +int lexer_var(t_list **alst, char *str); #endif diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index 91357682..fed5209d 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2017/01/09 15:35:39 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:31:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,13 +16,13 @@ int process_redirect(t_process *p) { if (p->fdin != STDIN) { - DG("redirect STDIN to %i", p->fdin); + /* DG("redirect STDIN to %i", p->fdin); */ dup2(p->fdin, STDIN); close(p->fdin); } if (p->fdout != STDOUT) { - DG("redirect STDOUT to %i", p->fdout); + /* DG("redirect STDOUT to %i", p->fdout); */ dup2(p->fdout, STDOUT); close(p->fdout); } diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index b1f95a2a..4bb42418 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:51:22 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 15:15:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ int (*g_lexer[])(t_list **alst, char *str) = &lexer_quote, &lexer_dquote, &lexer_backslash, + &lexer_var, }; int ft_is_delim(char c) @@ -37,6 +38,7 @@ int ft_tokenize(t_list **alst, char *str, t_lexstate state) { t_token *token; + DG("state=%i, *str=%c", state, *str); if (!*str) return (0); if (!*alst) diff --git a/42sh/src/lexer/get_lexer_state.c b/42sh/src/lexer/get_lexer_state.c index e58a31d6..4464f969 100644 --- a/42sh/src/lexer/get_lexer_state.c +++ b/42sh/src/lexer/get_lexer_state.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 13:45:46 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:52:54 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 15:18:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ t_lexstate get_lexer_state(char *str) { if (ft_is_delim(*str)) return (DELIM); + else if (*str == '$') + return (VAR); else if (*str == '&' || *str == ';' || *str == '|') return (SEP); else if (*str == '\\') diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index e4fbb58d..bdc32a58 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:54:17 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:50:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,6 +30,6 @@ int lexer_default(t_list **alst, char *str) state = WORD; token = (*alst)->content; token_append(token, *str); - token->type = TK_WORD; + token->type = TK_N_WORD; return (ft_tokenize(alst, str + 1, state)); } diff --git a/42sh/src/lexer/lexer_dquote.c b/42sh/src/lexer/lexer_dquote.c index 128648b4..d5c1b2e3 100644 --- a/42sh/src/lexer/lexer_dquote.c +++ b/42sh/src/lexer/lexer_dquote.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:58 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:58:17 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 15:15:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ int lexer_dquote(t_list **alst, char *str) token->type = TK_DQ_WORD; str++; if (*str == '\"') - return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT)); + return (ft_tokenize(alst, str + 1, DEFAULT)); if (*str == '\\') { token_append(token, *(str + 1)); diff --git a/42sh/src/lexer/lexer_great.c b/42sh/src/lexer/lexer_great.c index 910f48b2..d02a183b 100644 --- a/42sh/src/lexer/lexer_great.c +++ b/42sh/src/lexer/lexer_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:35 by jhalford #+# #+# */ -/* Updated: 2016/12/03 12:51:11 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:48:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_less.c b/42sh/src/lexer/lexer_less.c index 45dd0613..bc3fa958 100644 --- a/42sh/src/lexer/lexer_less.c +++ b/42sh/src/lexer/lexer_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:53 by jhalford #+# #+# */ -/* Updated: 2016/12/03 12:06:53 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:27:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_number.c b/42sh/src/lexer/lexer_number.c index f2d3f558..b5cffe2d 100644 --- a/42sh/src/lexer/lexer_number.c +++ b/42sh/src/lexer/lexer_number.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:54:33 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:29:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ int lexer_number(t_list **alst, char *str) t_lexstate state; token = (*alst)->content; + DG("*str=%c", *str); if ((state = get_lexer_state(str))) return (ft_tokenize(alst, str, state)); if (*str == '>') diff --git a/42sh/src/lexer/lexer_quote.c b/42sh/src/lexer/lexer_quote.c index 458dd3aa..401fd07f 100644 --- a/42sh/src/lexer/lexer_quote.c +++ b/42sh/src/lexer/lexer_quote.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:08 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:57:41 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 15:13:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ int lexer_quote(t_list **alst, char *str) token->type = TK_Q_WORD; str++; if (*str == '\'') - return (ft_tokenize(&(*alst)->next, str + 1, WORD)); + return (ft_tokenize(alst, str + 1, WORD)); token_append(token, *str); return (lexer_quote(alst, str)); } diff --git a/42sh/src/lexer/lexer_sep.c b/42sh/src/lexer/lexer_sep.c index d44a62df..54f7c20e 100644 --- a/42sh/src/lexer/lexer_sep.c +++ b/42sh/src/lexer/lexer_sep.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:49:57 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:39:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_var.c b/42sh/src/lexer/lexer_var.c new file mode 100644 index 00000000..881cc70a --- /dev/null +++ b/42sh/src/lexer/lexer_var.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_var.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/10 14:54:57 by jhalford #+# #+# */ +/* Updated: 2017/01/10 15:19:44 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int lexer_var(t_list **alst, char *str) +{ + t_token *token; + t_lexstate state; + + token = (*alst)->content; + token->type = TK_N_WORD; + str++; + DG("check, *str=%c, data='%s'", *str, token->data); + if (!ft_strchr(token->data, '$')) + token_append(token, '$'); + if (!*str) + { + token_expand_var(token); + return (0); + } + if ((state = get_lexer_state(str))) + { + token_expand_var(token); + return (ft_tokenize(alst, str, state)); + } + token_append(token, *str); + return (lexer_var(alst, str)); +} diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 52c4c6e2..7ed43aff 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:59:38 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 15:09:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_expand_var.c b/42sh/src/lexer/token_expand_var.c new file mode 100644 index 00000000..9ce64813 --- /dev/null +++ b/42sh/src/lexer/token_expand_var.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* token_expand_var.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/10 14:57:53 by jhalford #+# #+# */ +/* Updated: 2017/01/10 15:19:47 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +void token_expand_var(t_token *token) +{ + char *dollar; + char *val; + + dollar = ft_strchr(token->data, '$'); + if (!dollar[1]) + return ; + val = ft_getenv(data_singleton()->env, dollar + 1); + *dollar = 0; + if (val) + while (*val) + token_append(token, *val++); +} diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index 9a8b27c1..949700b7 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2016/12/07 15:23:03 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:39:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ void token_print(t_list *lst) while (lst) { - i = 1; + i = 0; token = lst->content; type = token->type; while (type >> (i++ + 2)) diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index a720ea97..89e9f60d 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:02:06 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:18:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index c8fc311b..3a2401d0 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:00:17 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:20:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 329566fe..7b70ab63 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:00:09 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:20:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_dgreat.c b/42sh/src/parser/parse_dgreat.c index a27b7602..15d7b579 100644 --- a/42sh/src/parser/parse_dgreat.c +++ b/42sh/src/parser/parse_dgreat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/12/01 16:36:58 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:44:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ int parse_dgreat(t_btree **ast, t_list **start, t_list **lst) return (1); tok = (*lst)->content; next_tok = (*lst)->next->content; - if (next_tok->type != TK_WORD) + if (!(next_tok->type & TK_WORD)) return (1); node->data.redir.n = ft_atoi(tok->data); node->data.redir.word.word = ft_strdup(next_tok->data); diff --git a/42sh/src/parser/parse_great.c b/42sh/src/parser/parse_great.c index ac191c30..490965dc 100644 --- a/42sh/src/parser/parse_great.c +++ b/42sh/src/parser/parse_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:12:11 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 14:45:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ int parse_great(t_btree **ast, t_list **start, t_list **lst) return (1); tok = (*lst)->content; next_tok = (*lst)->next->content; - if (next_tok->type != TK_WORD) + if (!(next_tok->type & TK_WORD)) return (1); node->data.redir.n = ft_atoi(tok->data); node->data.redir.word.word = ft_strdup(next_tok->data);