113 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/* ************************************************************************** */
 | 
						|
/*                                                                            */
 | 
						|
/*                                                        :::      ::::::::   */
 | 
						|
/*   cmds.c                                             :+:      :+:    :+:   */
 | 
						|
/*                                                    +:+ +:+         +:+     */
 | 
						|
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
						|
/*                                                +#+#+#+#+#+   +#+           */
 | 
						|
/*   Created: 2023/02/15 14:17:26 by cchauvet          #+#    #+#             */
 | 
						|
/*   Updated: 2023/02/17 15:20:29 by cchauvet         ###   ########.fr       */
 | 
						|
/*                                                                            */
 | 
						|
/* ************************************************************************** */
 | 
						|
 | 
						|
#include "libftx/libftx.h"
 | 
						|
#include "minishell.h"
 | 
						|
 | 
						|
static int	ft_cmds_init(t_list **cmds, size_t len)
 | 
						|
{
 | 
						|
	t_cmd	*content;
 | 
						|
	t_list	*current;
 | 
						|
	size_t	i;
 | 
						|
 | 
						|
	*cmds = NULL;
 | 
						|
	if (len > 0)
 | 
						|
		*cmds = malloc(sizeof(t_list));
 | 
						|
	current = *cmds;
 | 
						|
	i = 0;
 | 
						|
	while (i < len)
 | 
						|
	{
 | 
						|
		content = malloc(sizeof(t_cmd));
 | 
						|
		if (content == NULL)
 | 
						|
		{
 | 
						|
			ft_lstclear(cmds, ft_cmddel);
 | 
						|
			return (1);
 | 
						|
		}
 | 
						|
		content->args = NULL;
 | 
						|
		content->executable = NULL;
 | 
						|
		content->fd_in = -1;
 | 
						|
		content->fd_out = -1;
 | 
						|
		current->content = content;
 | 
						|
		if (!((i + 1) < len))
 | 
						|
		{
 | 
						|
			current->next = NULL;
 | 
						|
			return (0);
 | 
						|
		}
 | 
						|
		current->next = malloc(sizeof(t_list));
 | 
						|
		if (current->next == NULL)
 | 
						|
			ft_lstclear(cmds, ft_cmddel);
 | 
						|
		current = current->next;
 | 
						|
		i++;
 | 
						|
	}
 | 
						|
	return (0);
 | 
						|
}
 | 
						|
 | 
						|
static int	ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
 | 
						|
{
 | 
						|
	t_cmd	*cmd;
 | 
						|
	t_list	*current;
 | 
						|
 | 
						|
	if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|')))
 | 
						|
	{
 | 
						|
		free(cmds);
 | 
						|
		return (1);
 | 
						|
	}
 | 
						|
	if (*cmds == NULL)
 | 
						|
		return (0);
 | 
						|
	current = *cmds;
 | 
						|
	cmd = current->content;
 | 
						|
	cmd->fd_in = infile;
 | 
						|
	cmd = ft_lstlast(*cmds)->content;
 | 
						|
	cmd->fd_out = outfile;
 | 
						|
	return (0);
 | 
						|
}
 | 
						|
 | 
						|
static int	ft_cmds_fill(t_data *data, t_list **cmds, const char *line)
 | 
						|
{
 | 
						|
	char	**tab;
 | 
						|
	char	**args;
 | 
						|
	t_list	*current;
 | 
						|
	size_t	i;
 | 
						|
 | 
						|
	tab = ft_split_quoted(line, '|');
 | 
						|
	if (tab == NULL)
 | 
						|
		return (1);
 | 
						|
	i = 0;
 | 
						|
	current = *cmds;
 | 
						|
	while (tab[i] != NULL)
 | 
						|
	{
 | 
						|
		args = ft_split_quoted(tab[i], ' ');
 | 
						|
		if (ft_cmd_filler(data, current, args) == 1)
 | 
						|
		{
 | 
						|
			ft_lstclear(cmds, ft_cmddel);
 | 
						|
			ft_freer_tab_ultimate(2, args, tab);
 | 
						|
			return (1);
 | 
						|
		}
 | 
						|
		current = current->next;
 | 
						|
		i++;
 | 
						|
	}
 | 
						|
	ft_freer_tab_ultimate(1, tab);
 | 
						|
	return (0);
 | 
						|
}
 | 
						|
 | 
						|
t_list	**ft_parse_cmds(t_data *data, char *line, int infile, int outfile)
 | 
						|
{
 | 
						|
	t_list	**cmds;
 | 
						|
 | 
						|
	cmds = malloc(sizeof(t_list *));
 | 
						|
	if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
 | 
						|
		return (NULL);
 | 
						|
	if (ft_cmds_fill(data, cmds, line) == 1)
 | 
						|
		return (NULL);
 | 
						|
	return (cmds);
 | 
						|
}
 |