backquote work, still some segfaults, end of day commit

This commit is contained in:
Jack Halford 2017-01-12 18:09:16 +01:00
parent ef400aa225
commit dfdf245b1a
19 changed files with 142 additions and 68 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:59:52 by jhalford ### ########.fr */
/* Updated: 2017/01/12 13:18:27 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:08:20 by jhalford ### ########.fr */
/* Updated: 2017/01/12 14:57:41 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -82,7 +82,8 @@ void token_print(t_list *lst);
void token_expand_var(t_token *token);
int reduce_parens(t_list **alst, char *str);
int reduce_bquotes(t_list **alst, char *str);
int reduce_bquotes(t_list **alst, char **str);
char *command_getoutput(char *command);
int ft_is_delim(char c);

@ -1 +1 @@
Subproject commit 5cfe17901496443664e5b7be7fd87310f854030c
Subproject commit ab92f0e5d817c9d726a8ccf2f11c084ba446bbdf

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:51:08 by jhalford ### ########.fr */
/* Updated: 2017/01/12 13:14:35 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 15:10:20 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:53:45 by jhalford ### ########.fr */
/* Updated: 2017/01/12 15:15:21 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 12:51:08 by jhalford #+# #+# */
/* Updated: 2017/01/10 12:31:00 by jhalford ### ########.fr */
/* Updated: 2017/01/12 15:16:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,11 +19,11 @@ void job_remove(int id)
jobc = &data_singleton()->jobc;
if (job_is_completed(id))
{
/* DG("job_remove"); */
DG("job_remove");
if (id < data_singleton()->jobc.current_id)
data_singleton()->jobc.current_id = id;
ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free);
}
/* else */
/* DG("job_remove failed (not completed)"); */
else
DG("job_remove failed (not completed)");
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:55:30 by jhalford ### ########.fr */
/* Updated: 2017/01/12 15:14:38 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 13:05:55 by jhalford #+# #+# */
/* Updated: 2017/01/11 16:49:50 by jhalford ### ########.fr */
/* Updated: 2017/01/12 13:16:45 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -43,7 +43,14 @@ void process_format(t_list **plist, int firstp, int opts)
ft_putchar('\t');
if (opts & JOBS_OPTS_L)
{
ft_sstrprint(p->av, ' ');
if (p->attributes & PROCESS_SUBSHELL)
{
ft_putstr("( ");
ft_putstr(p->av[2]);
ft_putstr(" )");
}
else
ft_sstrprint(p->av, ' ');
if ((*plist)->next)
ft_putstr(" |");
(*plist) = (*plist)->next;
@ -61,7 +68,14 @@ void process_format(t_list **plist, int firstp, int opts)
p->attributes &= ~PROCESS_STATE_MASK;
p->attributes &= ~PROCESS_RUNNING;
}
ft_sstrprint(p->av, ' ');
if (p->attributes & PROCESS_SUBSHELL)
{
ft_putstr("( ");
ft_putstr(p->av[2]);
ft_putstr(" )");
}
else
ft_sstrprint(p->av, ' ');
if ((*plist)->next)
ft_putstr(" | ");
(*plist) = (*plist)->next;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:54:22 by jhalford ### ########.fr */
/* Updated: 2017/01/12 15:14:28 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,13 +23,13 @@ int process_mark_status(pid_t pid, int status)
p->status = status;
if (WIFSTOPPED(status))
{
/* DG("marking: pid=%i, status=%i (stopped sig %i)", pid, status, WTERMSIG(status)); */
DG("marking: pid=%i, status=%i (stopped sig %i)", pid, status, WTERMSIG(status));
p->attributes &= ~PROCESS_STATE_MASK;
p->attributes |= PROCESS_SUSPENDED;
}
else
{
/* DG("marking: pid=%i, status=%i (completed sig %i)", pid, status, WTERMSIG(status)); */
DG("marking: pid=%i, status=%i (completed sig %i)", pid, status, WTERMSIG(status));
p->attributes &= ~PROCESS_STATE_MASK;
p->attributes |= PROCESS_COMPLETED;
if (WIFSIGNALED(status))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:54:29 by jhalford ### ########.fr */
/* Updated: 2017/01/12 13:16:48 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* command_getoutput.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/12 14:01:59 by jhalford #+# #+# */
/* Updated: 2017/01/12 15:31:34 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#define BUF_SIZE 10
char *command_getoutput(char *command)
{
int fds[2];
t_btree *ast;
t_astnode item;
char *output;
char *buf;
int ret;
output = NULL;
buf = NULL;
item.type = TK_SUBSHELL;
item.data.sstr = malloc(4 * sizeof(char *));
item.data.sstr[0] = ft_strdup(data_singleton()->argv[0]);
item.data.sstr[1] = ft_strdup("-c");
item.data.sstr[2] = ft_strdup(command);
item.data.sstr[3] = NULL;
ast = btree_create_node(&item, sizeof(item));
pipe(fds);
dup2(fds[PIPE_WRITE], 1);
close(fds[PIPE_WRITE]);
ft_exec(&ast);
/* DG("gonna parse"); */
/* token_print(token); */
/* if (ft_parse(&ast, &token)) */
/* return (NULL); */
/* DG("gonna exec"); */
/* if (ft_exec(&ast)) */
/* return (NULL); */
DG("gonna read from pipe");
while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)))
{
buf[ret] = 0;
DG("read '%s' from pipe", buf);
ft_strappend(&output, buf);
}
DG("finished reading from pipe");
close(fds[PIPE_READ]);
return (output);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 16:11:11 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:07:39 by jhalford ### ########.fr */
/* Updated: 2017/01/12 13:58:20 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,9 +14,14 @@
int ft_post_tokenize(t_list **alst, char *str)
{
if (reduce_parens(alst, str))
return (1);
if (reduce_bquotes(alst, str))
return (1);
int ret;
while ((ret = reduce_parens(alst, str)))
if (ret == -1)
return (-1);
while ((ret = reduce_bquotes(alst, &str)))
if (ret == -1)
return (-1);
DG("new command from bquotes: '%s'", str);
return (0);
}

View file

@ -6,43 +6,44 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 16:46:27 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:56:40 by jhalford ### ########.fr */
/* Updated: 2017/01/12 15:10:16 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int reduce_bquotes(t_list **alst, char *str)
int reduce_bquotes(t_list **alst, char **str)
{
t_list *start;
t_list *end;
t_list **end;
t_token *token;
int tk;
char *new;
char *fit;
char *bq_start;
char *bq_end;
(void)str;
tk = TK_BQUOTE;
if ((start = ft_lst_find(*alst, &tk, token_cmp_type)))
{
end = start->next;
end = &start->next;
while (end)
{
token = end->content;
token = (*end)->content;
if (token->type == TK_BQUOTE)
break ;
end = end->next;
if (token->type & TK_NON_FREEABLE)
ft_lst_delif(alst, token, ft_addrcmp, ft_lst_cfree);
else
ft_lst_delif(alst, token, ft_addrcmp, token_free);
}
if (end)
{
token = start->content;
token->type = TK_SUBSHELL;
token->data = ft_strbetween(token->data + 1, ((t_token*)end->content)->data);
ft_lst_delif(alst, end->content, ft_addrcmp, ft_lst_cfree);
return (0);
end = &(*end)->next;
}
if (!*end)
return (-1);
bq_start = ((t_token*)start->content)->data;
bq_end = ((t_token*)(*end)->content)->data;
ft_lstdel(end, token_free);
fit = command_getoutput(ft_strbetween(bq_start + 1, bq_end));
new = ft_strreplace(str, bq_start, bq_end, fit);
ft_strdel(str);
*str = new;
ft_lstdel(alst, token_free);
return (1);
}
return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 16:13:31 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:06:51 by jhalford ### ########.fr */
/* Updated: 2017/01/12 13:50:10 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,19 +36,14 @@ int reduce_parens(t_list **alst, char *str)
if (num_p == 0)
break ;
end = end->next;
if (token->type & TK_NON_FREEABLE)
ft_lst_delif(alst, token, ft_addrcmp, ft_lst_cfree);
else
ft_lst_delif(alst, token, ft_addrcmp, token_free);
}
if (end)
{
token = start->content;
token->type = TK_SUBSHELL;
token->data = ft_strbetween(token->data + 1, ((t_token*)end->content)->data);
ft_lst_delif(alst, end->content, ft_addrcmp, ft_lst_cfree);
return (0);
ft_lst_delif(alst, token, ft_addrcmp, token_free);
}
if (!end)
return (-1);
token = start->content;
token->type = TK_SUBSHELL;
token->data = ft_strbetween(token->data + 1, ((t_token*)end->content)->data);
ft_lst_delif(alst, end->content, ft_addrcmp, token_free);
return (1);
}
return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */
/* Updated: 2016/12/05 13:17:56 by jhalford ### ########.fr */
/* Updated: 2017/01/12 13:18:46 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,6 +18,7 @@ void token_free(void *data, size_t size)
(void)size;
token = data;
ft_strdel(&token->data);
if (!(token->type & TK_NON_FREEABLE))
ft_strdel(&token->data);
free(token);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */
/* Updated: 2017/01/11 16:28:42 by jhalford ### ########.fr */
/* Updated: 2017/01/12 14:48:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,7 @@ void token_print(t_list *lst)
while (lst)
{
i = -1;
DG("lst at %p", lst);
token = lst->content;
type = token->type;
while (type >> (i++ + 2))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:48:10 by jhalford ### ########.fr */
/* Updated: 2017/01/12 14:02:30 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,13 +31,13 @@ int shell_single_command(char *command)
return (1);
DG("after post_tokenize");
token_print(token);
if (ft_parse(&ast, &token))
return (1);
btree_print(STDBUG, ast, &ft_putast);
/* ft_dprintf(STDBUG, "\n--- INFIX BREAKDOWN ---\n"); */
/* btree_apply_infix(ast, &ft_putast2); */
if (ft_exec(&ast))
return (1);
/* if (ft_parse(&ast, &token)) */
/* return (1); */
/* btree_print(STDBUG, ast, &ft_putast); */
/* /1* ft_dprintf(STDBUG, "\n--- INFIX BREAKDOWN ---\n"); *1/ */
/* /1* btree_apply_infix(ast, &ft_putast2); *1/ */
/* if (ft_exec(&ast)) */
/* return (1); */
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2017/01/11 16:52:25 by jhalford ### ########.fr */
/* Updated: 2017/01/12 15:07:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 16:52:44 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:50:54 by jhalford ### ########.fr */
/* Updated: 2017/01/12 15:10:24 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */