aboutsummaryrefslogtreecommitdiff
path: root/src/lexer/utils.c
blob: 0b7baa31fc930677207f6036c0f066ae7aab17a5 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   utils.c                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: nahaddac <nahaddac@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/07/16 08:18:15 by nahaddac          #+#    #+#             */
/*   Updated: 2020/10/08 14:11:51 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "lexer.h"

// return token tag corresponding to string id
enum e_tok                tok_assign_tag(char *content)
{
	if (content[0] == ';')
		return (TAG_END);
	if (ft_strncmp(content, "&&", 2) == 0)
		return (TAG_AND);
	if (ft_strncmp(content, "||", 2) == 0)
		return (TAG_OR);
	if(content[0]  == '|')
		return (TAG_PIPE);
	if (content[0] == '>')
		return (TAG_REDIR_OUT);
	if (content[0] == '<')
		return (TAG_REDIR_IN);
	if (ft_strncmp(content, ">>", 2) == 0)
		return (TAG_REDIR_APPEND);
	if (content[0] == '(')
		return (TAG_PARENT_OPEN);
	if (content[0] == ')')
		return (TAG_PARENT_CLOSE);
	return (0);
}

/*
** return tag
** le tag stick est rajouter si
** a la fin (char *)tk->tok ne figure pas d'espace.
*/

enum e_tok tok_assign_stick(t_tok_lst *tok)
{
	int i;

	i = ft_strlen(tok->content);
	if (i > 0)
		if (ft_isblank(tok->content[i - 1]))
			return (tok->tag);
	return (tok->tag | TAG_STICK);
}

/*
** return tag si
** la chaine de character est un str où '' où ''
** \note the loop after ft_strpbrk is to search again if the quote was escaped
*/
enum e_tok tok_assign_str(t_tok_lst *tok)
{
	char	*found;

	found = ft_strpbrk(tok->content, "'\"");
	while (found != NULL && found != tok->content && found[-1] == '\\')
		found = ft_strpbrk(found + 1, "'\"");
	if (found == NULL)
		tok->tag = TAG_STR;
	else if (*found == '\'')
		tok->tag = TAG_STR_SINGLE;
	else if (*found == '"')
		tok->tag = TAG_STR_DOUBLE;
	return (tok_assign_stick(tok));
}


// check is char is separator
int                 	lexer_sep(char c)
{
	return (ft_strchr(";&|><()", c) != NULL);
}

// number of starting space character
int             		lexer_space(char *input)
{
	return (ft_strspn(input, " \t"));
}

int             		quote_len(char *input, int i)
{
	char	quote_type;

	quote_type = input[i];
	i++;
	while (input[i] != quote_type && input[i] != '\0')
	{
		if (quote_type == '"' && input[i] == '\\')
			i++;
		i++;
	}
	while (ft_isblank(input[i + 1]))
		i++;
	return (i + 1);
}