-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.c
131 lines (116 loc) · 2.57 KB
/
parsing.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "main.h"
/**
* parsing_entry - Parse the given entry into an array based on the delimiter.
* @entry: The entry string to parse.
* @delim: The delimiter used to split the entry into tokens.
* Return: An array of strings representing the tokens, or NULL on failure.
*/
char **parsing_entry(char *entry, char *delim)
{
char *token, *entry_copy;
size_t n_tokens;
char **av;
int i;
entry_copy = copy_entry_string(entry);
if (entry_copy == NULL)
return (NULL);
n_tokens = count_tokens(entry, delim);
av = allocate_token_array(n_tokens);
if (av == NULL)
{
free(entry_copy);
return (NULL);
}
token = tokenize_entry(entry_copy, delim);
for (i = 0; token != NULL; i++)
{
av[i] = copy_token_string(token);
if (av[i] == NULL)
{
free_token_array(av, i);
free(entry_copy);
return (NULL);
}
token = tokenize_entry(NULL, delim);
}
av[i] = NULL;
free(entry_copy);
check_builtin_command(av);
return (av);
}
/**
* copy_entry_string - Create a copy of the entry string.
* @entry: The entry string to copy.
* Return: A pointer to the newly allocated copy, or NULL on failure.
*/
char *copy_entry_string(char *entry)
{
char *copy;
size_t len;
if (entry == NULL)
return (NULL);
len = _strlen(entry) + 1;
copy = malloc(sizeof(char) * len);
if (copy == NULL)
{
perror("./shell");
return (NULL);
}
_strcpy(copy, entry);
return (copy);
}
/**
* count_tokens - Count the number of tokens in the entry string.
* @entry: The entry string to count tokens in.
* @delim: The delimiter used to split the entry.
* Return: The number of tokens.
*/
size_t count_tokens(char *entry, char *delim)
{
char *token;
size_t count = 0;
token = _strtok(entry, delim);
while (token != NULL)
{
count++;
token = _strtok(NULL, delim);
}
return (count);
}
/**
* allocate_token_array - Allocate memory for the token array.
* @n_tokens: The number of tokens in the array.
* Return: A pointer to the allocated token array, or NULL on failure.
*/
char **allocate_token_array(size_t n_tokens)
{
char **av;
av = malloc(sizeof(char *) * (n_tokens + 1));
if (av == NULL)
{
perror("./shell");
return (NULL);
}
return (av);
}
/**
* copy_token_string - Create a copy of the token string.
* @token: The token string to copy.
* Return: A pointer to the newly allocated copy, or NULL on failure.
*/
char *copy_token_string(char *token)
{
char *copy;
size_t len;
if (token == NULL)
return (NULL);
len = _strlen(token) + 1;
copy = malloc(sizeof(char) * len);
if (copy == NULL)
{
perror("./shell");
return (NULL);
}
_strcpy(copy, token);
return (copy);
}