merged heredoc support; moved history/ into src/ because of makefile
problems
This commit is contained in:
commit
73f12e7e96
23 changed files with 141 additions and 38 deletions
|
|
@ -73,6 +73,7 @@ exec/process_setexec.c\
|
|||
exec/process_setgroup.c\
|
||||
exec/process_setsig.c\
|
||||
exec/redirect_dgreat.c\
|
||||
exec/redirect_dless.c\
|
||||
exec/redirect_great.c\
|
||||
exec/redirect_greatand.c\
|
||||
exec/redirect_less.c\
|
||||
|
|
@ -104,6 +105,13 @@ glob/lib_perso/ft_strsubf.c\
|
|||
glob/lib_perso/ft_tabdel.c\
|
||||
glob/lib_perso/ft_tablen.c\
|
||||
glob/match_pattern.c\
|
||||
history/add_str_in_history.c\
|
||||
history/history.c\
|
||||
history/history_parsing.c\
|
||||
history/history_parsing_toolz.c\
|
||||
history/history_parsing_toolz_2.c\
|
||||
history/list_toolz.c\
|
||||
history/surch_in_history.c\
|
||||
job-control/builtin_bg.c\
|
||||
job-control/builtin_fg.c\
|
||||
job-control/builtin_jobs.c\
|
||||
|
|
@ -139,6 +147,7 @@ job-control/sigttin_handler.c\
|
|||
job-control/sigttou_handler.c\
|
||||
lexer/command_getoutput.c\
|
||||
lexer/expand_bquotes.c\
|
||||
lexer/get_lexer_stack.c\
|
||||
lexer/get_state_global.c\
|
||||
lexer/get_state_redir.c\
|
||||
lexer/lexer_backslash.c\
|
||||
|
|
@ -146,10 +155,12 @@ lexer/lexer_bquote.c\
|
|||
lexer/lexer_comment.c\
|
||||
lexer/lexer_default.c\
|
||||
lexer/lexer_delim.c\
|
||||
lexer/lexer_dless.c\
|
||||
lexer/lexer_dquote.c\
|
||||
lexer/lexer_end.c\
|
||||
lexer/lexer_great.c\
|
||||
lexer/lexer_greatand.c\
|
||||
lexer/lexer_init.c\
|
||||
lexer/lexer_less.c\
|
||||
lexer/lexer_lessand.c\
|
||||
lexer/lexer_lex.c\
|
||||
|
|
@ -172,13 +183,6 @@ line-editing/copy_cut_paste.c\
|
|||
line-editing/ft_prompt.c\
|
||||
line-editing/get_key.c\
|
||||
line-editing/heredoc.c\
|
||||
line-editing/history/add_str_in_history.c\
|
||||
line-editing/history/history.c\
|
||||
line-editing/history/history_parsing.c\
|
||||
line-editing/history/history_parsing_toolz.c\
|
||||
line-editing/history/history_parsing_toolz_2.c\
|
||||
line-editing/history/list_toolz.c\
|
||||
line-editing/history/surch_in_history.c\
|
||||
line-editing/home_end.c\
|
||||
line-editing/lib_line_editing/tool_line.c\
|
||||
line-editing/lib_line_editing/tool_line_2.c\
|
||||
|
|
|
|||
|
|
@ -45,10 +45,11 @@ enum e_lexstate
|
|||
SEP,
|
||||
WORD,
|
||||
NUMBER,
|
||||
GREAT,
|
||||
LESS,
|
||||
GREATAND,
|
||||
GREAT,
|
||||
LESSAND,
|
||||
GREATAND,
|
||||
DLESS,
|
||||
QUOTE,
|
||||
DQUOTE,
|
||||
BQUOTE,
|
||||
|
|
@ -74,6 +75,7 @@ struct s_lexer
|
|||
int pos;
|
||||
t_lexstate state;
|
||||
t_list *stack;
|
||||
t_list *heredoc_stack;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -101,6 +103,8 @@ char *stack_to_prompt(t_list *stack);
|
|||
|
||||
t_lexstate get_state_global(t_lexer *lexer);
|
||||
t_lexstate get_state_redir(t_lexer *lexer);
|
||||
int get_lexer_stack(t_lexer lexer);
|
||||
void lexer_init(t_lexer *lexer);
|
||||
int lexer_lex(t_list **alst, t_lexer *lexer);
|
||||
int lexer_default(t_list **alst, t_lexer *lexer);
|
||||
int lexer_newline(t_list **alst, t_lexer *lexer);
|
||||
|
|
@ -110,8 +114,9 @@ int lexer_word(t_list **alst, t_lexer *lexer);
|
|||
int lexer_number(t_list **alst, t_lexer *lexer);
|
||||
int lexer_less(t_list **alst, t_lexer *lexer);
|
||||
int lexer_great(t_list **alst, t_lexer *lexer);
|
||||
int lexer_lessand(t_list **alst, t_lexer *lexer);
|
||||
int lexer_greatand(t_list **alst, t_lexer *lexer);
|
||||
int lexer_lessand(t_list **alst, t_lexer *lexer);
|
||||
int lexer_dless(t_list **alst, t_lexer *lexer);
|
||||
int lexer_quote(t_list **alst, t_lexer *lexer);
|
||||
int lexer_dquote(t_list **alst, t_lexer *lexer);
|
||||
int lexer_bquote(t_list **alst, t_lexer *lexer);
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ t_redirmap g_redirmap[] =
|
|||
{
|
||||
{TK_LESS, redirect_less},
|
||||
{TK_GREAT, redirect_great},
|
||||
/* {TK_DLESS, redirect_dless}, */
|
||||
{TK_DLESS, redirect_dless},
|
||||
{TK_DGREAT, redirect_dgreat},
|
||||
{TK_LESSAND, redirect_lessand},
|
||||
{TK_GREATAND, redirect_greatand},
|
||||
|
|
@ -34,10 +34,7 @@ int process_redirect(t_process *p)
|
|||
{
|
||||
redir = redirs->content;
|
||||
if (redir->n > 9)
|
||||
{
|
||||
bad_fd(redir->n);
|
||||
exit(1);
|
||||
}
|
||||
return (bad_fd(redir->n));
|
||||
i = 0;
|
||||
while (g_redirmap[i].type)
|
||||
{
|
||||
|
|
|
|||
27
42sh/src/exec/redirect_dless.c
Normal file
27
42sh/src/exec/redirect_dless.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* redirect_dless.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/06 22:09:53 by jhalford #+# #+# */
|
||||
/* Updated: 2017/02/07 16:05:09 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "exec.h"
|
||||
|
||||
int redirect_dless(t_redir *redir)
|
||||
{
|
||||
int fds[2];
|
||||
char *str;
|
||||
|
||||
pipe(fds);
|
||||
str = redir->word.word;
|
||||
write(fds[PIPE_WRITE], str, ft_strlen(str));
|
||||
close(fds[PIPE_WRITE]);
|
||||
dup2(fds[PIPE_READ], 0);
|
||||
close(fds[PIPE_READ]);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@ int redirect_greatand(t_redir *redir)
|
|||
int fdold;
|
||||
int fdnew;
|
||||
|
||||
DG("redir greatand");
|
||||
if (redir->close)
|
||||
{
|
||||
close(redir->n);
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ int job_wait(int id)
|
|||
pid_t pid;
|
||||
int status;
|
||||
|
||||
DG("gonna wait [%i]", id);
|
||||
if (job_is_stopped(id))
|
||||
return (0);
|
||||
job_update_status();
|
||||
|
|
|
|||
6
42sh/src/lexer/get_lexer_stack.c
Normal file
6
42sh/src/lexer/get_lexer_stack.c
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include "lexer.h"
|
||||
|
||||
int get_lexer_stack(t_lexer lexer)
|
||||
{
|
||||
return (lexer.stack ? *(int*)lexer.stack->content : 0);
|
||||
}
|
||||
41
42sh/src/lexer/lexer_dless.c
Normal file
41
42sh/src/lexer/lexer_dless.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include "lexer.h"
|
||||
|
||||
int lexer_dless(t_list **alst, t_lexer *lexer)
|
||||
{
|
||||
t_list *heredoc_lst;
|
||||
t_token *eof_tok;
|
||||
t_token *heredoc_tok;
|
||||
|
||||
(void)alst;
|
||||
(void)lexer;
|
||||
heredoc_lst = *(t_list**)lexer->heredoc_stack->content;
|
||||
heredoc_tok = heredoc_lst->content;
|
||||
if (!(heredoc_lst->next))
|
||||
{
|
||||
ft_dprintf(2, "{red}%s: parse error near `\\n'{eoc}\n", SHELL_NAME);
|
||||
return (1);
|
||||
}
|
||||
eof_tok = heredoc_lst->next->content;
|
||||
if (!(eof_tok->type == TK_WORD))
|
||||
{
|
||||
ft_dprintf(2, "{red}%s: expected word token after <<{eoc}\n", SHELL_NAME);
|
||||
return (1);
|
||||
}
|
||||
DG("heredoc contains [%s]", heredoc_tok->data);
|
||||
DG("heredoc ends at [%s]", eof_tok->data);
|
||||
DG("input is [%s]", lexer->str + lexer->pos);
|
||||
if (ft_strcmp(eof_tok->data, lexer->str + lexer->pos) == 0)
|
||||
{
|
||||
pop(&lexer->stack);
|
||||
pop(&lexer->heredoc_stack);
|
||||
while (lexer->str[++lexer->pos])
|
||||
;
|
||||
ft_strappend(&lexer->str, (char[]){'\n', 0});
|
||||
lexer->pos++;
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
while (lexer->str[lexer->pos])
|
||||
token_append_char(heredoc_tok, lexer->str[lexer->pos++], 0, 0);
|
||||
return (lexer_end(alst, lexer));
|
||||
}
|
||||
|
|
@ -4,15 +4,23 @@ int lexer_end(t_list **alst, t_lexer *lexer)
|
|||
{
|
||||
t_token *token;
|
||||
|
||||
DG("check");
|
||||
if (*alst && (lexer->state == QUOTE
|
||||
if ((*alst && (lexer->state == QUOTE
|
||||
|| lexer->state == DQUOTE
|
||||
|| lexer->state == BQUOTE))
|
||||
|| get_lexer_stack(*lexer) == DLESS)
|
||||
{
|
||||
token = (*alst)->content;
|
||||
ft_strappend(&lexer->str, (char[]){'\n', 0});
|
||||
token_append(token, lexer, 1, 0);
|
||||
lexer->pos++;
|
||||
if (get_lexer_stack(*lexer) == DLESS)
|
||||
{
|
||||
token = (*(t_list**)lexer->heredoc_stack->content)->content;
|
||||
token_append_char(token, '\n', 0, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
token = (*alst)->content;
|
||||
token_append_char(token, '\n', 1, 0);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
return (0);
|
||||
|
|
|
|||
10
42sh/src/lexer/lexer_init.c
Normal file
10
42sh/src/lexer/lexer_init.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include "lexer.h"
|
||||
|
||||
void lexer_init(t_lexer *lexer)
|
||||
{
|
||||
lexer->str = NULL;
|
||||
lexer->pos = 0;
|
||||
lexer->state = DEFAULT;
|
||||
lexer->stack = NULL;
|
||||
lexer->heredoc_stack = NULL;
|
||||
}
|
||||
|
|
@ -28,11 +28,13 @@ int lexer_less(t_list **alst, t_lexer *lexer)
|
|||
}
|
||||
if (lexer->str[lexer->pos + 1] == '<')
|
||||
{
|
||||
token_free(token, 0);
|
||||
(*alst)->content = token_init();
|
||||
token->type = TK_DLESS;
|
||||
lexer->pos++;
|
||||
token_append(token, lexer, 0, 0);
|
||||
lexer->pos++;
|
||||
lexer->pos += 2;
|
||||
push(&lexer->stack, DLESS);
|
||||
lexer->state = DEFAULT;
|
||||
ft_lsteadd(&lexer->heredoc_stack, ft_lstnew(alst, sizeof(alst)));
|
||||
return (lexer_lex(&(*alst)->next, lexer));
|
||||
}
|
||||
else
|
||||
|
|
|
|||
|
|
@ -20,10 +20,11 @@ int (*g_lexer[])(t_list **alst, t_lexer *lexer) =
|
|||
&lexer_sep,
|
||||
&lexer_word,
|
||||
&lexer_number,
|
||||
&lexer_great,
|
||||
&lexer_less,
|
||||
&lexer_greatand,
|
||||
&lexer_great,
|
||||
&lexer_lessand,
|
||||
&lexer_greatand,
|
||||
&lexer_dless,
|
||||
&lexer_quote,
|
||||
&lexer_dquote,
|
||||
&lexer_bquote,
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ int lexer_sep(t_list **alst, t_lexer *lexer)
|
|||
char c;
|
||||
char cn;
|
||||
|
||||
lexer->state = DEFAULT;
|
||||
if (*alst)
|
||||
{
|
||||
token = (*alst)->content;
|
||||
|
|
|
|||
|
|
@ -31,6 +31,8 @@ char *stack_to_prompt(t_list *stack)
|
|||
return ("dquote bquote> ");
|
||||
else if (top == PAREN)
|
||||
return ("subsh> ");
|
||||
else if (top == DLESS)
|
||||
return ("heredoc> ");
|
||||
else
|
||||
return ("error> ");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,10 +18,8 @@ int non_interactive_shell(char *command)
|
|||
t_lexer lexer;
|
||||
t_btree *ast;
|
||||
|
||||
lexer.pos = 0;
|
||||
lexer.state = DEFAULT;
|
||||
lexer_init(&lexer);
|
||||
lexer.str = command;
|
||||
lexer.stack = NULL;
|
||||
token = NULL;
|
||||
ast = NULL;
|
||||
while (lexer.str[lexer.pos])
|
||||
|
|
@ -43,6 +41,7 @@ int non_interactive_shell(char *command)
|
|||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int interactive_shell()
|
||||
{
|
||||
t_list *token;
|
||||
|
|
@ -50,27 +49,26 @@ int interactive_shell()
|
|||
t_lexer lexer;
|
||||
t_btree *ast;
|
||||
|
||||
lexer.pos = 0;
|
||||
lexer.state = DEFAULT;
|
||||
lexer.str = NULL;
|
||||
lexer_init(&lexer);
|
||||
token = NULL;
|
||||
lexer.stack = NULL;
|
||||
ast = NULL;
|
||||
do {
|
||||
ft_strappend(&lexer.str, readline(stack_to_prompt(lexer.stack)));
|
||||
if (lexer.stack && *(int*)lexer.stack->content == BACKSLASH)
|
||||
if (get_lexer_stack(lexer) == BACKSLASH)
|
||||
pop(&lexer.stack);
|
||||
else if (get_lexer_stack(lexer) == DLESS)
|
||||
lexer.state = DLESS;
|
||||
ltoken = ft_lstlast(token);
|
||||
lexer_lex((token ? <oken : &token), &lexer);
|
||||
if (lexer_lex((token ? <oken : &token), &lexer))
|
||||
return (1);
|
||||
DG("[{mag}%s{eoc}] stack=[%i] state=[%i]", lexer.str, lexer.stack ? *(int*)lexer.stack->content : 0, lexer.state);
|
||||
token_print(token);
|
||||
} while (lexer.stack);
|
||||
} while (get_lexer_stack(lexer));
|
||||
if (bquotes_expand(&token))
|
||||
return (1);
|
||||
if (!token)
|
||||
return (0);
|
||||
ft_add_str_in_history(lexer.str);
|
||||
token_print(token);
|
||||
if (ft_parse(&ast, &token))
|
||||
return (1);
|
||||
btree_print(STDBUG, ast, &ft_putast);
|
||||
|
|
@ -87,7 +85,7 @@ int main(int ac, char **av)
|
|||
data = data_singleton();
|
||||
setlocale(LC_ALL, "");
|
||||
shell_init(ac, av);
|
||||
DG("{inv}{bol}{gre}start of shell{eoc} pid=%i pgrp=%i job_control is %s", getpid(), getpgrp(), SH_HAS_JOBC(data->opts) ? "ON" : "OFF");
|
||||
DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s", SH_HAS_JOBC(data->opts)?"ON":"OFF");
|
||||
if (SH_IS_INTERACTIVE(data->opts))
|
||||
{
|
||||
while (1)
|
||||
|
|
|
|||
|
|
@ -26,7 +26,8 @@ int parse_dless(t_btree **ast, t_list **start, t_list **lst)
|
|||
next_tok = (*lst)->next->content;
|
||||
if (next_tok->type != TK_WORD)
|
||||
return (1);
|
||||
node->data.redir.word.word = ft_strdup(next_tok->data);
|
||||
node->data.redir.word.word = ft_strdup(tok->data);
|
||||
node->data.redir.n = 0;
|
||||
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
|
||||
ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free);
|
||||
ft_parse(&(*ast)->left, start);
|
||||
|
|
|
|||
Loading…
Reference in a new issue