first try at $ expansion, done on the fly in the lexer, not tested thoroughly

This commit is contained in:
Jack Halford 2017-01-10 15:20:45 +01:00
parent 0c99e8012c
commit f011ea258d
20 changed files with 100 additions and 26 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 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, QUOTE,
DQUOTE, DQUOTE,
BACKSLASH, BACKSLASH,
VAR,
}; };
struct s_token struct s_token
@ -71,6 +72,7 @@ int token_append(t_token *token, char c);
void token_free(void *data, size_t size); void token_free(void *data, size_t size);
int token_cmp_type(t_token *token, t_type *ref); int token_cmp_type(t_token *token, t_type *ref);
void token_print(t_list *lst); void token_print(t_list *lst);
void token_expand_var(t_token *token);
int ft_is_delim(char c); 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_quote(t_list **alst, char *str);
int lexer_dquote(t_list **alst, char *str); int lexer_dquote(t_list **alst, char *str);
int lexer_backslash(t_list **alst, char *str); int lexer_backslash(t_list **alst, char *str);
int lexer_var(t_list **alst, char *str);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 16:04:18 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) if (p->fdin != STDIN)
{ {
DG("redirect STDIN to %i", p->fdin); /* DG("redirect STDIN to %i", p->fdin); */
dup2(p->fdin, STDIN); dup2(p->fdin, STDIN);
close(p->fdin); close(p->fdin);
} }
if (p->fdout != STDOUT) if (p->fdout != STDOUT)
{ {
DG("redirect STDOUT to %i", p->fdout); /* DG("redirect STDOUT to %i", p->fdout); */
dup2(p->fdout, STDOUT); dup2(p->fdout, STDOUT);
close(p->fdout); close(p->fdout);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:37:11 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_quote,
&lexer_dquote, &lexer_dquote,
&lexer_backslash, &lexer_backslash,
&lexer_var,
}; };
int ft_is_delim(char c) int ft_is_delim(char c)
@ -37,6 +38,7 @@ int ft_tokenize(t_list **alst, char *str, t_lexstate state)
{ {
t_token *token; t_token *token;
DG("state=%i, *str=%c", state, *str);
if (!*str) if (!*str)
return (0); return (0);
if (!*alst) if (!*alst)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/10 13:45:46 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)) if (ft_is_delim(*str))
return (DELIM); return (DELIM);
else if (*str == '$')
return (VAR);
else if (*str == '&' || *str == ';' || *str == '|') else if (*str == '&' || *str == ';' || *str == '|')
return (SEP); return (SEP);
else if (*str == '\\') else if (*str == '\\')

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:36:21 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; state = WORD;
token = (*alst)->content; token = (*alst)->content;
token_append(token, *str); token_append(token, *str);
token->type = TK_WORD; token->type = TK_N_WORD;
return (ft_tokenize(alst, str + 1, state)); return (ft_tokenize(alst, str + 1, state));
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:36:58 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; token->type = TK_DQ_WORD;
str++; str++;
if (*str == '\"') if (*str == '\"')
return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT)); return (ft_tokenize(alst, str + 1, DEFAULT));
if (*str == '\\') if (*str == '\\')
{ {
token_append(token, *(str + 1)); token_append(token, *(str + 1));

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:06:35 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:06:53 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:06:45 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; t_lexstate state;
token = (*alst)->content; token = (*alst)->content;
DG("*str=%c", *str);
if ((state = get_lexer_state(str))) if ((state = get_lexer_state(str)))
return (ft_tokenize(alst, str, state)); return (ft_tokenize(alst, str, state));
if (*str == '>') if (*str == '>')

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:07:08 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; token->type = TK_Q_WORD;
str++; str++;
if (*str == '\'') if (*str == '\'')
return (ft_tokenize(&(*alst)->next, str + 1, WORD)); return (ft_tokenize(alst, str + 1, WORD));
token_append(token, *str); token_append(token, *str);
return (lexer_quote(alst, str)); return (lexer_quote(alst, str));
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 16:29:57 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_var.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:07:11 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token_expand_var.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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++);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:39:01 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) while (lst)
{ {
i = 1; i = 0;
token = lst->content; token = lst->content;
type = token->type; type = token->type;
while (type >> (i++ + 2)) while (type >> (i++ + 2))

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 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); return (1);
tok = (*lst)->content; tok = (*lst)->content;
next_tok = (*lst)->next->content; next_tok = (*lst)->next->content;
if (next_tok->type != TK_WORD) if (!(next_tok->type & TK_WORD))
return (1); return (1);
node->data.redir.n = ft_atoi(tok->data); node->data.redir.n = ft_atoi(tok->data);
node->data.redir.word.word = ft_strdup(next_tok->data); node->data.redir.word.word = ft_strdup(next_tok->data);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 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); return (1);
tok = (*lst)->content; tok = (*lst)->content;
next_tok = (*lst)->next->content; next_tok = (*lst)->next->content;
if (next_tok->type != TK_WORD) if (!(next_tok->type & TK_WORD))
return (1); return (1);
node->data.redir.n = ft_atoi(tok->data); node->data.redir.n = ft_atoi(tok->data);
node->data.redir.word.word = ft_strdup(next_tok->data); node->data.redir.word.word = ft_strdup(next_tok->data);