Skip to content

Commit

Permalink
[TEST FIX] parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
rclanget committed Dec 16, 2016
1 parent 0048872 commit 763ba56
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 52 deletions.
4 changes: 2 additions & 2 deletions includes/execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ulefebvr <ulefebvr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/26 15:30:49 by ulefebvr #+# #+# */
/* Updated: 2016/12/15 21:49:14 by ulefebvr ### ########.fr */
/* Updated: 2016/12/16 18:04:41 by rclanget ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,7 +25,7 @@ t_tree *self_node(t_info *info);

t_word *get_magicquotelist(char *s);
char *make_sentence(t_word *words);
char *apply_magicquote(t_info *info, char *str);
char *apply_magicquote(t_info *info, char *str, int type);
char *process_magicquote(t_info *info, char *cmd);
t_word *treat_magicquote(t_info *info, t_word *words);

Expand Down
3 changes: 2 additions & 1 deletion includes/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ulefebvr <ulefebvr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/23 13:12:01 by ulefebvr #+# #+# */
/* Updated: 2016/12/15 20:49:48 by ulefebvr ### ########.fr */
/* Updated: 2016/12/16 12:03:31 by rclanget ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -87,6 +87,7 @@ char **parse_op_rightfd(char *str, int pos);

void *parser_free_cmd(t_tree *cmd);

int get_close_parenthese(char *str, int i);
t_tree *clean_parentheses(t_tree *node, char *str);

int is_operator(char cmd);
Expand Down
7 changes: 3 additions & 4 deletions srcs/execution/execution_motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ulefebvr <ulefebvr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/10/23 14:18:49 by ulefebvr #+# #+# */
/* Updated: 2016/12/15 21:21:22 by ulefebvr ### ########.fr */
/* Updated: 2016/12/16 18:29:17 by rclanget ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -54,15 +54,14 @@ int execution_motor(t_info *info, t_tree *cmd, int wait)

cmd->elem = parser_tild(info, cmd->elem);
tmp = parse_var(info, ft_strdup(cmd->elem));
tmp1 = apply_magicquote(info, tmp);
tmp1 = apply_magicquote(info, tmp, cmd->type);
cmd->cmd = redirection_agreg(cust_split(tmp1));
ft_free_them_all(2, &tmp, &tmp1);
if (cmd && cmd->cmd && cmd->cmd[0])
{
if (!cmd->type)
return (execution_command(info, cmd, wait));
else
return (g_p[cmd->type](info, cmd));
return (g_p[cmd->type](info, cmd));
}
return (0);
}
8 changes: 4 additions & 4 deletions srcs/execution/process_magicquote.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ulefebvr <ulefebvr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/10/25 22:09:32 by ulefebvr #+# #+# */
/* Updated: 2016/12/02 00:20:04 by ulefebvr ### ########.fr */
/* Updated: 2016/12/16 18:09:16 by rclanget ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -84,21 +84,21 @@ char *process_magicquote(t_info *info, char *cmd)
return (get_file_content());
}

char *apply_magicquote(t_info *info, char *str)
char *apply_magicquote(t_info *info, char *str, int type)
{
t_word *words;
char *tmp;
char *tmp2;

tmp = NULL;
tmp2 = NULL;
if ((words = get_magicquotelist(str)))
if (0 == type && (words = get_magicquotelist(str)))
{
treat_magicquote(info, words);
tmp = make_sentence(words);
tmp2 = ft_strtrim(tmp);
ft_free_them_all(1, &tmp);
free_list(words);
}
return (tmp2);
return (type ? ft_strdup(str) : tmp2);
}
34 changes: 9 additions & 25 deletions srcs/termcaps_readline/parser_clean_parentheses.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rclanget <rclanget@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/10/18 16:49:27 by rclanget #+# #+# */
/* Updated: 2016/12/13 18:08:48 by rclanget ### ########.fr */
/* Updated: 2016/12/16 18:50:58 by rclanget ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,37 +37,21 @@ int get_close_parenthese(char *str, int i)
int open_parenthese;

open_parenthese = 0;
while (str[i++])
while (str[i])
{
if (!isquote(str[i]))
{
if (str[i] == '(')
open_parenthese++;
else if (str[i] == ')')
{
if (open_parenthese)
open_parenthese--;
else
if (!--open_parenthese)
break ;
}
}
i++;
}
return (i);
}

t_tree *get_new_line(t_tree *node, char *str)
{
char *tmp;
char *line;

line = NULL;
ft_putstr("> ");
get_next_line(1, &line);
tmp = ft_strjoin(str, line);
ft_free_them_all(2, &line, &str);
return (clean_parentheses(node, tmp));
}

t_tree *clean_parentheses(t_tree *node, char *str)
{
int i;
Expand All @@ -80,15 +64,15 @@ t_tree *clean_parentheses(t_tree *node, char *str)
i = get_close_parenthese(str, i);
if (str[i] == ')' && str[i - 1] != '\\')
{
str_new = ft_memalloc(i);
node->elem = ft_memcpy(str_new, str + 1, i - 1);
str_new = ft_strndup(str + 1, i - 1);
// node->elem = ft_memcpy(str_new, str + 1, i - 1);
node->elem = ft_strtrim(str_new);
node->type = PARENTHESES_VAL;
free(str);
// free(str);
ft_free_them_all(2, &str, &str_new);
node->left = parser_cmd(ft_strtrim(node->elem));
return (node);
}
else if (!str[i])
return (get_new_line(node, str));
}
else
node->elem = str;
Expand Down
37 changes: 21 additions & 16 deletions srcs/termcaps_readline/termcaps_readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ulefebvr <ulefebvr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/01/26 00:16:21 by ulefebvr #+# #+# */
/* Updated: 2016/12/15 22:48:01 by rclanget ### ########.fr */
/* Updated: 2016/12/16 17:42:56 by rclanget ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,24 +16,29 @@

#include <unistd.h>

char check_quote(char *cmd, char quote)
char check_quote_parenthese(char *cmd, char *quote)
{
int i;
static int parenthese;
int i;

i = 0;
while (cmd[i])
{
if (cmd[i] == '\\' && quote == '\'')
if (cmd[i] == '\\' && *quote == '\'')
;
else if (cmd[i] == '\\' && cmd[i + 1])
++i;
else if (quote && cmd[i] == quote)
quote = 0;
else if (!quote && (cmd[i] == '\'' || cmd[i] == '\"'))
quote = cmd[i];
else if (*quote && cmd[i] == *quote)
*quote = 0;
else if (!*quote && (cmd[i] == '\'' || cmd[i] == '\"' || cmd[i] == '`'))
*quote = cmd[i];
else if (!*quote && cmd[i] == '(')
++parenthese;
else if (!*quote && cmd[i] == ')' && parenthese)
--parenthese;
++i;
}
return (quote);
return (parenthese);
}

char *getnextline(t_info *info, char quote)
Expand All @@ -47,9 +52,9 @@ char *getnextline(t_info *info, char quote)
cmd = NULL;
(void)info;
ret = get_next_line(0, &cmd);
if (ret <= 0 && quote)
if (ret < 0 && quote)
ft_fdprint(2, "42sh: Syntax error: unexpected end of file\n");
if (cmd && (quote = check_quote(cmd, quote)))
if (cmd && (check_quote_parenthese(cmd, &quote) || quote))
{
tmp = getnextline(info, quote);
tmp2 = ft_strjoin(cmd, "\n");
Expand All @@ -71,19 +76,19 @@ char *getnextlineb(t_info *info)
return (ret ? cmd : NULL);
}

char *get_currentline(t_info *info, int term, char quote)
char *get_currentline(t_info *info, int term, char quote, char parenthese)
{
char *cmd;
char *tmp;
char *tmp2;
char *tmp3;

ft_print(!quote ? "%s" : "> ", get_prompt(info));
ft_print(!quote && !parenthese ? "%s" : "> ", get_prompt(info));
cmd = ((term) ? termcaps_loop(info) : getnextlineb(info));
if (cmd && (quote = check_quote(cmd, quote)))
if (cmd && ((parenthese = check_quote_parenthese(cmd, &quote)) || quote))
{
info->term->pos_c = 0;
tmp = get_currentline(info, term, quote);
tmp = get_currentline(info, term, quote, parenthese);
tmp2 = ft_strjoin(cmd, "\n");
tmp3 = ft_strjoin(tmp2, tmp);
ft_free_them_all(3, &cmd, &tmp, &tmp2);
Expand All @@ -101,7 +106,7 @@ char *termcaps_readline(t_info *info)
{
termcaps_save(1);
term = termcaps_activation(info->term->is_term);
str = get_currentline(info, term, 0);
str = get_currentline(info, term, 0, 0);
termcaps_save(0);
return (str);
}
Expand Down

0 comments on commit 763ba56

Please sign in to comment.