This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenize.c
99 lines (78 loc) · 2.24 KB
/
tokenize.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
#include "tokenize.h"
#include <stdlib.h>
#include <string.h>
#define LOG_MODULE "tokenize"
#define LOG_ENABLE_DBG 0
#include "log.h"
static bool
push_argv(char ***argv, size_t *size, char *arg, size_t *argc)
{
if (arg != NULL && arg[0] == '%')
return true;
if (*argc >= *size) {
size_t new_size = *size > 0 ? 2 * *size : 10;
char **new_argv = realloc(*argv, new_size * sizeof(new_argv[0]));
if (new_argv == NULL)
return false;
*argv = new_argv;
*size = new_size;
}
(*argv)[(*argc)++] = arg;
return true;
}
bool
tokenize_cmdline(char *cmdline, char ***argv)
{
*argv = NULL;
size_t argv_size = 0;
bool first_token_is_quoted = cmdline[0] == '"' || cmdline[0] == '\'';
char delim = first_token_is_quoted ? cmdline[0] : ' ';
char *p = first_token_is_quoted ? &cmdline[1] : &cmdline[0];
char *search_start = p;
size_t idx = 0;
while (*p != '\0') {
char *end = strchr(search_start, delim);
if (end == NULL) {
if (delim != ' ') {
LOG_ERR("unterminated %s quote", delim == '"' ? "double" : "single");
free(*argv);
*argv = NULL;
return false;
}
if (!push_argv(argv, &argv_size, p, &idx) ||
!push_argv(argv, &argv_size, NULL, &idx))
{
goto err;
} else
return true;
}
if (end > p && *(end - 1) == '\\') {
/* Escaped quote, remove one level of escaping and
* continue searching for "our" closing quote */
memmove(end - 1, end, strlen(end));
end[strlen(end) - 1] = '\0';
search_start = end;
continue;
}
*end = '\0';
if (!push_argv(argv, &argv_size, p, &idx))
goto err;
p = end + 1;
while (*p == delim)
p++;
while (*p == ' ')
p++;
if (*p == '"' || *p == '\'') {
delim = *p;
p++;
} else
delim = ' ';
search_start = p;
}
if (!push_argv(argv, &argv_size, NULL, &idx))
goto err;
return true;
err:
free(*argv);
return false;
}