aboutsummaryrefslogtreecommitdiff
path: root/src/parse/parse_error.c
blob: b721c2879b27b8b551b7c888501be994b9b866d6 (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
103
104
105
106
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parse_error.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: nahaddac <nahaddac@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/06/18 15:09:48 by nahaddac          #+#    #+#             */
/*   Updated: 2020/06/19 12:46:56 by nahaddac         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "parser.h"

t_token                 *error_syntax_parent(t_ftlst *in)
{
    int                 op;
    int                 cl;
    t_token             *tk;

    op = 0;
    cl = 0;
    while(in != NULL)
    {
        tk = in->data;
        if (tk->tag & TAG_PARENT_OPEN)
            op++;
        if(tk->tag & TAG_PARENT_CLOSE)
            cl++;
        if (cl && op == 0)
        {
            tk->content = ft_strjoin3(
                "minishell:  syntax error near unexpected token `",
                        tk->content, "'");
            return tk;
        }
        in = in->next;
    }
    return NULL;
}

int                     out_error_first(t_token *tk)
{
    int                 i;

    i = 0;
    if(tk->tag & TAG_IS_SEP)
            return(1);
    if (tk->tag & TAG_IS_REDIR)
    {
        while(tk->content[i])
            i++;
        if (tk->tag & TAG_REDIR_APPEND && i <= 2)
            return (0);
        else
            return(1);
    }
    return(0);
}

t_token                 *error_syntax_simple(t_ftlst *in)
{
    t_token             *tk;
    size_t              i;
    t_ftlst             *first;

    tk = in->data;
    first = in;
    if(tk->tag & TAG_IS_SEP || (tk->tag & TAG_IS_REDIR))
    {
        if (out_error_first(tk))
        {
            i = ft_strlen(tk->content);
            if (i >= 2)
                tk->content[2] = '\0';
            tk->content =
            ft_strjoin3("minishell:  syntax error near unexpected token `",
                        tk->content, "'");
            return(tk);
        }
    }
    while(in != NULL)
    {
        i = 0;
        tk = in->data;
        if(tk->tag & TAG_IS_SEP || (tk->tag & TAG_IS_REDIR))
        {
            if (((t_token *)in->next->data)->tag &
            ((t_token*)in->next->data)->tag & TAG_IS_SEP ||
            (((t_token*)in->next->data)->tag & TAG_IS_REDIR))
            {
                tk = in->next->data;
                i = ft_strlen(tk->content);
                if (i >= 3)
                    tk->content[3] = '\0';
                tk->content =
                ft_strjoin3("minishell:  syntax error near unexpected token `",
                            tk->content, "'");
                return(tk);
            }
        }
        in = in->next;
    }
    return error_syntax_parent(first);

}