aboutsummaryrefslogtreecommitdiff
path: root/src/tokenizer.c
blob: 086d4b0253b80bbf776b3d64ff1fbf6a7bb11028 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   tokenizer.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/06/28 20:55:50 by chuhlig           #+#    #+#             */
/*   Updated: 2024/06/28 21:58:38 by chuhlig          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

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

char	*ft_strncpy(char *dst, char *src, size_t n)
{
	char *start;

	start = dst;
	while (n-- > 0 && *src)
		*dst++ = *src++;
	if (n > 0)
		ft_memset(dst, '\0', n);
	return (start);
}

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");
	}
}



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

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

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

	quotes = "\"\'";
	quote_check = '\0';
	start_of_string = 0;
	ignore_space = 0;
	flag = 0;
	i = 0;
	skip = 0;
	while (s[i])
	{
		c = s[i];
		if (skip)
		{
			skip = 0;
			i++;
			continue ;
		}
		if (!ignore_space && (c == '|' || c == ';' || c == '<' || c == '>'))
		{
			conditional_print(s, start_of_string, i, 0, token_list);
			if ((c == '<' || c == '>') && s[i + 1] == c)
			{
				if (c == '<')
				{
					*token_list = new_redir_token(INPUT_FILE,
							*token_list, NULL);
					print_token(*token_list);
				}
				else
				{
					*token_list = new_redir_token(OUTPUT_APPEND,
							*token_list, NULL);
					print_token(*token_list);
				}
				i++;
			}
			else
			{
				if (c == '<')
				{
					*token_list = new_redir_token(INPUT_FILE,
							*token_list, NULL);
				}
				else
				{
					*token_list = new_redir_token(OUTPUT_OVERRIDE,
							*token_list, NULL);
				}
				print_token(*token_list);
			}
			start_of_string = i + 1;
		}
		else if (ignore_space && c == quote_check)
		{
			quote_check = '\0';
			ignore_space = 0;
		}
		else if (!ignore_space && ft_strchr(quotes, c))
		{
			quote_check = c;
			ignore_space = 1;
		}
		else if ((!ignore_space && (s[i] != ' ' || s[i] != '\t')) || s[i + 1] == '\0')
		{
			if (s[i + 1])
				conditional_print(s, start_of_string, i, 1, token_list);
			else
				conditional_print(s, start_of_string, i, 0, token_list);
			start_of_string = i + 1;
		}
		i++;
	}
}