aboutsummaryrefslogtreecommitdiff
path: root/src/parse_cmd.c
blob: a978c9922c02faf8194fe6d2246c78c5f0e9a2e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parse_cmd.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/07/08 15:06:25 by dkaiser           #+#    #+#             */
/*   Updated: 2025/01/23 18:04:50 by chuhlig          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

static char	**collect_args(t_token **tokens, t_env **env);

t_node	*parse_cmd(t_token *tokens, t_env **env)
{
	char			**args;
	t_redirection	*redirs;
	t_list			*create_files;

	create_files = NULL;
	printf("parse_cmd\n");
	print_token_list(tokens);
	redirs = collect_redirs(&tokens, *env, &create_files);// takes pos next to pipe or first token
	printf("parse_cmdafter collect redir\n");
	print_token_list(tokens);// still existing tokenlist
	if (redirs == NULL)
		return (NULL);
	args = collect_args(&tokens, env);
	printf("parse_cmdafter collect args\n");
	// print_token_list(tokens);//here it gone
	if (args == NULL)
	{
		free(redirs);
		return (NULL);
	}
	return (new_cmd_node(args, redirs, create_files));
}

// static char	**collect_args(t_token **tokens, t_env **env)
// {
// 	t_token	*cur;
// 	char	**result;
// 	int		i;
// 	t_token	*next;

// 	cur = *tokens;
// 	i = 0;
// 	while (cur != NULL && ++i)
// 		cur = cur->next;
// 	result = malloc(sizeof(char *) * (i + 1));
// 	if (result == NULL)
// 		return (free_tokens(*tokens), NULL);
// 	cur = *tokens;
// 	i = 0;
// 	while (cur != NULL && cur->type == STRING_TOKEN)
// 	{
// 		next = cur->next;
// 		if (cur->previous)
// 			free_token(cur->previous);
// 		result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
// 		i++;
// 		if (cur->next == NULL)
// 			free_token(cur);
// 		cur = next;
// 	}
// 	result[i] = NULL;
// 	return (result);
// }

static char **collect_args(t_token **tokens, t_env **env) {
    t_token *cur;
    char **result;
    int i;
    t_token *next;

    cur = *tokens;
    i = 0;
    while (cur != NULL && ++i)
        cur = cur->next;
    result = malloc(sizeof(char *) * (i + 1));
    if (result == NULL)
        return (free_tokens(*tokens), NULL);
    cur = *tokens;
    i = 0;
    while (cur != NULL && cur->type == STRING_TOKEN) {
        next = cur->next;
        result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
        i++;
        if (cur->previous)
            cur->previous->next = cur->next;
        if (cur->next)
            cur->next->previous = cur->previous;
        free_token(cur);
        cur = next;
    }
    result[i] = NULL;
    *tokens = cur; // Update the head of the token list
    return (result);
}// need to later the rest of the tokenlist