forked from glyif/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.c
108 lines (94 loc) · 2.26 KB
/
tokenizer.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
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
#include "header.h"
/**
* init_tokens - initializes token struct
* @tokens: tokens struct to initialize
* @length: length to malloc for
*/
void init_tokens(tokens_t *tokens, int length)
{
tokens->tokens = safe_malloc(length * sizeof(token_t));
/* Initialize the rest of the structure */
tokens->tokensN = 0;
}
/**
* is_redirection - checks if token id is a redirection
* @token_id: tokenid
*
* Return: 1 if yes , 0 if no
*/
int is_redirection(int token_id)
{
return (token_id == TOKEN_REWRITE || token_id == TOKEN_APPEND ||
token_id == TOKEN_CAT);
}
/**
* delete_tokens - freeing tokens
* @tokens: tokens_t struct with tokens
* Return: 0
*/
int delete_tokens(tokens_t *tokens)
{
unsigned int i;
for (i = 0; i < tokens->tokensN; i++)
free(tokens->tokens[i].str);
free(tokens->tokens);
tokens->tokens = NULL;
tokens->tokensN = 0;
return (0);
}
/**
* delete_dups - deletes duplicate semi-colons from data
* @tokens: tokens_t struct
*/
void delete_dups(tokens_t *tokens)
{
unsigned int i, tokens_to_move;
for (i = 0; i + 1 < tokens->tokensN;)
{
if ((tokens->tokens[i].id == tokens->tokens[i + 1].id) &&
(tokens->tokens[i].id == TOKEN_SEMICOLON))
{
tokens_to_move = tokens->tokensN - i - 1;
_memmove(tokens->tokens + i, tokens->tokens + i + 1,
tokens_to_move * sizeof(token_t));
tokens->tokensN--;
}
else
{
i++;
}
}
}
/**
* token_classify - classifies the tokens
* @tokens: tokens_t struct
*/
void token_classify(tokens_t *tokens)
{
unsigned int i;
int j;
token_types token_names[] = {
{ TOKEN_SEMICOLON, ";", "semicolon", 1 },
{ TOKEN_BACKGROUND, "&", "background", 1 },
{ TOKEN_AND, "&&", "and", 2 },
{ TOKEN_OR, "||", "or", 2 },
{ TOKEN_PIPE, "|", "pipe", 3 },
{ TOKEN_REWRITE, ">", "rewrite", 4 },
{ TOKEN_APPEND, ">>", "append", 4 },
{ TOKEN_CAT, "<", "cat", 4 },
{ 9, '\0', '\0', 9}
};
for (i = 0; i < tokens->tokensN; i++)
{
tokens->tokens[i].id = TOKEN_STRING;
for (j = 0; token_names[j].token_id != 9; j++)
{
if (_strcmp(token_names[j].token_str, tokens->tokens[i].str) == 0)
{
tokens->tokens[i].id = token_names[j].token_id;
tokens->tokens[i].prec = token_names[j].precedence;
break;
}
}
}
}