aboutsummaryrefslogtreecommitdiff
path: root/src/tokenizer.c
blob: bb76b9e340d228c01fda98aaa40d82f87a53b13c (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   tokenizer.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/06/28 20:55:50 by chuhlig           #+#    #+#             */
/*   Updated: 2024/08/05 13:43:39 by chuhlig          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"
#include "token.h"

void	print_token(t_token *token)
{
	if (token->type == STRING_TOKEN)
	{
		printf("STRING_TOKEN: %s\n", token->content.string);
	}
	else if (token->type == REDIR_TOKEN)
	{
		printf("REDIR_TOKEN: %d\n", token->content.redir_type);
	}
	else if (token->type == PIPE_TOKEN)
	{
		printf("PIPE_TOKEN\n");
	}
	else if (token->type == NEWLINE_TOKEN)
	{
		printf("NEWLINE_TOKEN\n");
	}
}

void	conditional_print(char *string, int start_of_string, int i,
	t_token **token_list)
{
	char	*line;
	int		len;

	len = i - start_of_string + 1;
	if (len > 0)
	{
		line = (char *)malloc(len);
		if (!line)
		{
			exit(EXIT_FAILURE);
		}
		ft_strncpy(line, string + start_of_string, len);
		line[len - 1] = '\0';
		while (*line == ' ' || *line == '\t')
			line++;
		if (*line != '\0')
		{
			*token_list = new_str_token(line, *token_list, NULL);
			print_token(*token_list);
		}
	}
}

int	symbol_checker(char *s, int i, t_token **token_list)
{
	if ((s[i] == '<' || s[i] == '>') && s[i + 1] == s[i])
	{
		if (s[i] == '<')
			*token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL);
		else
			*token_list = new_redir_token(OUTPUT_APPEND, *token_list, NULL);
		print_token(*token_list);
		i++;
	}
	else
	{
		if (s[i] == '<')
			*token_list = new_redir_token(INPUT_FILE, *token_list, NULL);
		else if (s[i] == '|')
			*token_list = new_token(PIPE_TOKEN, *token_list, NULL);
		else if (s[i] == '\n')
			*token_list = new_token(NEWLINE_TOKEN, *token_list, NULL);
		else
			*token_list = new_redir_token(OUTPUT_OVERRIDE, *token_list, NULL);
		print_token(*token_list);
	}
	return (i);
}

int	check_for_string(char *s, int i, int *start_of_string, t_token **token_list)
{
	char	quote_check;
	int		ignore_space;

	ignore_space = 0;
	quote_check = '\0';
	if (ignore_space && s[i] == quote_check)
	{
		quote_check = '\0';
		ignore_space = 0;
	}
	else if (!ignore_space && ft_strchr("\"\'", s[i]))
	{
		quote_check = s[i];
		ignore_space = 1;
	}
	if ((!ignore_space && (s[i] == '\0' || s[i] == ' '
				|| s[i] == '\t')) || i == ft_strlen(s) - 1)
	{
		if (s[i + 1] == '\0')
			i++;
		conditional_print(s, *start_of_string, i - 1, token_list);
		*start_of_string = i + 1;
	}
	return (i);
}

void	tokenizer(char *s, t_token **token_list)
{
	int		start_of_string;
	int		ignore_space;
	int		i;
	char	quote_check;

	quote_check = '\0';
	start_of_string = 0;
	ignore_space = 0;
	i = 0;
	if (!s || !*s)
		return ;
	while (s && s[i])
	{
		if (!ignore_space && (s[i] == '|' || s[i] == '\n'
				|| s[i] == '<' || s[i] == '>'))
		{
			i = symbol_checker(s, i, token_list);
			start_of_string = i + 1;
		}
		else
		{
			i = check_for_string(s, i, &start_of_string, token_list);
		}
		i++;
	}
}