-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_parse1.c
81 lines (71 loc) · 2.11 KB
/
lexer_parse1.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_parse1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: btaha <btaha@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/02 17:15:42 by btaha #+# #+# */
/* Updated: 2023/01/02 17:45:06 by btaha ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/minishell.h"
int ft_iscmd(char c)
{
if (c == ' ' || c == '\t' || c == '\r'
|| c == '\v' || c == '\n' || c == '\f')
return (0);
else if (c == '|' || c == '<' || c == '>'
|| c == '$' || c == '"' || c == '\'')
return (0);
else
return (1);
}
t_token *lexer_parse_command(t_lexer *lexer)
{
int j;
char *str;
j = 0;
while (ft_iscmd(lexer->c) && lexer->c != '\0')
{
lexer_advance(lexer);
j++;
}
str = ft_calloc(j + 1, sizeof(char));
ft_strlcpy(str, &lexer->src[(lexer->i) - j], j + 1);
return (init_token(str, COMMAND));
}
t_token *lexer_parse_sc(t_lexer *lexer)
{
char *str;
str = ft_calloc(2, sizeof(char));
*str = '\'';
lexer_advance(lexer);
return (init_token(str, SINGLE_QUOTE));
}
t_token *lexer_parse_dc(t_lexer *lexer)
{
char *str;
str = ft_calloc(2, sizeof(char));
*str = '"';
lexer_advance(lexer);
return (init_token(str, DOUBLE_QUOTE));
}
t_token *lexer_parse_dollar(t_lexer *lexer)
{
char *str;
if (lexer_peek(lexer) == ' ' || lexer_peek(lexer) == '\0')
{
str = ft_calloc(2, sizeof(char));
*str = '$';
lexer_advance(lexer);
return (init_token(str, COMMAND));
}
else
{
str = ft_calloc(2, sizeof(char));
*str = '$';
lexer_advance(lexer);
return (init_token(str, DOLLAR));
}
}