-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
51 lines (48 loc) · 1.12 KB
/
parser.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
#include "shell.h"
/**
* parse_args - parse arguments
* @cmd: tokenized string with arguments
* @del: delimeter
* @args: argument vector
* @mod: delimeter method 0-character 1-string
*
* Return: none
*/
void parse_args(char *cmd, const char *del, char ***args, int mod)
{
char *tokenized = NULL, **tmp, *trimmed_arg;
int index = 0, i;
*args = NULL;
tokenized = _strtok(cmd, del, mod);
while (tokenized != NULL)
{
tmp = (char **)_malloc((_arlen(*args) + 2) * sizeof(char *));
if (!*args)
*args = tmp;
else
{
i = 0;
while ((*args)[i])
{
tmp[i] = _strdup((*args)[i]);
i++;
}
free_pp(*args);
*args = tmp;
}
(*args)[index] = NULL;
trimmed_arg = tokenized;
if ((tokenized[0] == '"' && tokenized[_strlen(tokenized) - 1] == '"') ||
(tokenized[0] == '\'' && tokenized[_strlen(tokenized) - 1] == '\''))
{
trimmed_arg = _strdup(tokenized + 1);
trimmed_arg[_strlen(trimmed_arg) - 1] = '\0';
}
trim_spaces(&((*args)[index]), trimmed_arg);
(*args)[index + 1] = NULL;
if (trimmed_arg != tokenized)
free(trimmed_arg);
tokenized = _strtok(NULL, del, mod);
index++;
}
}