le retour des BQUOTE

This commit is contained in:
wescande 2017-03-14 20:20:19 +01:00
parent 0d4f441670
commit 30556cdba2
3 changed files with 50 additions and 83 deletions

View file

@ -3,93 +3,53 @@
/* ::: :::::::: */
/* command_getoutput.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/12 14:01:59 by jhalford #+# #+# */
/* Updated: 2017/03/13 23:32:52 by jhalford ### ########.fr */
/* Created: 2017/03/14 19:44:25 by wescande #+# #+# */
/* Updated: 2017/03/14 19:44:37 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#define BUF_SIZE 1024
/*static char *manage_output(int *fds)
{
int ret;
char buf[BUF_SIZE + 1];
char *output;
output = NULL;
while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)) > 0)
{
buf[ret] = 0;
ft_strappend(&output, buf);
}
close(fds[PIPE_READ]);
return (output);
}
*/
static t_btree *gen_t_btree(const char *command)
static char *manage_output(int *fds)
{
t_btree *ast;
t_astnode item;
int len;
unsigned char *esc;
ft_bzero(&item, sizeof(t_astnode));
item.type = SUBSHELL;
ast = btree_create_node(&item, sizeof(t_astnode));
ft_bzero(&item, sizeof(t_astnode));
item.type = CMD;
len = ft_strlen(command);
esc = ft_memalloc((len >> 3) + 1);
ft_ld_pushfront(&item.data.cmd.token, gen_tab(command, esc, esc, 1));
ast->right = btree_create_node(&item, sizeof(t_astnode));
return (ast);
}
char *command_getoutput(char *command)
{
t_btree *ast;
char *output;
char buf[BUF_SIZE + 1];
t_exec *exec;
t_process p;
int ret;
int pid;
char buf[BUF_SIZE + 1];
char *output;
return (NULL);
output = NULL;
exec = &data_singleton()->exec;
DG("%p exec ?", exec);
DG("\033[38;5;46mbefore");
ast = gen_t_btree(command);
DG();
if (process_set(&p, ast))
return (NULL);
DG();
if (!(pid = plaunch_subshell(&p)))
return (NULL);
waitpid(pid, &ret, WUNTRACED);
DG();
// exec_leaf(&ast);
/*
DG();
set_process(&p, ast);
DG();
launch_process(&p);
DG();
close(p.fdout);*/
DG();
while ((ret = read(exec->fdin, buf, BUF_SIZE)) > 0)
close(fds[PIPE_WRITE]);
while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)) > 0)
{
buf[ret] = 0;
ft_strappend(&output, buf);
}
DG();
close(exec->fdin);
DG("done with %s\033[0m", output);
close(fds[PIPE_READ]);
return (output);
}
char *command_getoutput(char *command)
{
char *output;
int ret;
int pid;
int fds[2];
if (!command)
return (NULL);
pipe(fds);
if (!(pid = fork()))
{
close(fds[PIPE_READ]);
dup2_close(fds[PIPE_WRITE], STDOUT);
execle(data_singleton()->argv[0],data_singleton()->argv[0],
"-c", command, NULL,
data_singleton()->env);
exit(1);
}
waitpid(pid, &ret, WUNTRACED);
output = manage_output(fds);
return (output);
}

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 17:47:53 by wescande #+# #+# */
/* Updated: 2017/02/24 22:04:59 by ariard ### ########.fr */
/* Updated: 2017/03/14 20:07:47 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -75,7 +75,7 @@ static char *get_output(char *command)
int len;
if (!(output = command_getoutput(command)))
return (ft_strnew(0));
return (NULL);//ft_strnew(0));
len = ft_strlen(output);
while (output[--len] == '\n')
output[len] = '\0';
@ -98,10 +98,8 @@ static int search_bquote(t_bquote *me)
me->mid = ft_strsub(sta, 1, me->str - sta - 1);
me->s1 = ft_strsub(CH(*me->wk)[0], 0, sta - CH(*me->wk)[0]);
me->s2 = ft_strdup(me->str + 1);
content = get_output(me->mid);
DG();
init_expand(me, content, is_char_esc(me->esc, CH(*me->wk)[0], sta));
DG();
if ((content = get_output(me->mid)))
init_expand(me, content, is_char_esc(me->esc, CH(*me->wk)[0], sta));
ft_strdel(&me->mid);
ft_strdel(&me->s1);
ft_strdel(&me->s2);
@ -113,6 +111,13 @@ static int search_bquote(t_bquote *me)
return (0);
}
void delete(t_ld **tmp, t_ld **src)
{
if (*tmp == *src)
ft_ld_del(src, &ft_tabdel);
else
ft_ld_del(tmp, &ft_tabdel);
}
void expand_bquote(t_glob *gl)
{
t_ld *tmp;
@ -133,8 +138,9 @@ void expand_bquote(t_glob *gl)
me.str = CH(gl->m_pat)[0];
if ((tmp = gl->m_pat) &&
(do_it = search_bquote(&me)) == 1)
ft_ld_del(&tmp, &ft_tabdel);
if (!gl->m_pat->next)
delete(&tmp, &gl->m_pat);
// ft_ld_del(&tmp, &ft_tabdel);
if (!gl->m_pat || !gl->m_pat->next)
break ;
gl->m_pat = gl->m_pat->next;
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */
/* Updated: 2017/03/13 22:19:53 by jhalford ### ########.fr */
/* Updated: 2017/03/14 20:19:22 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,7 +36,8 @@ void process_free(void *content, size_t content_size)
/* return ; */
/* if (g_freemap[p->type].f) */
/* (g_freemap[p->type].f)(p); */
(p->map.free)(p);
if (p->map.free)
(p->map.free)(p);
ft_lstdel(&p->redirs, redir_free);
free(p);
}