-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter_cli_tok.c
More file actions
226 lines (188 loc) · 5.71 KB
/
splinter_cli_tok.c
File metadata and controls
226 lines (188 loc) · 5.71 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Copyright 2025 Tim Post
* License: Apache 2 (MIT available upon request to timthepost@protonmail.com)
*
* @file splinter_cli_tok.c
* @brief Splinter CLI input tokenizers
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "splinter_cli.h"
/**
* Dealing with potentially-quoted strings in C is always a pain.
*
* strtok() clobbers strings in-place, which is .. not ideal with dealing with
* potentially quoted SET commands, etc. So, a custom "unrolling" helper
* that deals with simple quotes.
*
* This is meant to deal with things like:
* > set foo_key "fooo \"bar baz boo\" bar foo foobar"
* It does NOT deal with single or nested quotes.
*/
char **cli_unroll_argv(const char *input, int *out_argc) {
int cap = 8, argc = 0, i;
char **argv;
if (!input) return NULL;
const char *p = input;
argv = malloc(cap * sizeof(char*));
if (!argv) return NULL;
while (*p) {
/* skip whitespace */
while (isspace((unsigned char)*p)) ++p;
if (!*p) break;
/* decide whether token is quoted */
char *token = NULL;
size_t tlen = 0;
if (*p == '"') {
++p; /* skip opening quote */
const char *start = p;
while (*p && *p != '"') {
if (*p == '\\' && *(p+1)) p += 2; /* allow escaped chars inside quotes */
else ++p;
}
tlen = p - start;
token = malloc(tlen + 1);
if (!token) goto fail;
/* simple copy handling backslash escapes for \" and \\ */
const char *q = start;
char *w = token;
while (q < p) {
// advance past escape
if (*q == '\\' && (q + 1) < p) ++q;
// now just a simple copy
*w++ = *q++;
}
*w = '\0';
if (*p == '"') ++p; /* skip closing quote */
} else {
const char *start = p;
while (*p && !isspace((unsigned char)*p)) {
++p;
}
tlen = p - start;
token = malloc(tlen + 1);
if (!token) goto fail;
memcpy(token, start, tlen);
token[tlen] = '\0';
}
if (argc >= cap) {
cap *= 2;
char **tmp = realloc(argv, cap * sizeof(char*));
if (!tmp) { free(token); goto fail; }
argv = tmp;
}
argv[argc++] = token;
}
/* NULL-terminate */
char **tmp = realloc(argv, (argc + 1) * sizeof(char*));
if (!tmp) goto fail;
argv = tmp;
argv[argc] = NULL;
*out_argc = argc;
return argv;
fail:
if (argv) {
for (i = 0; i < argc; ++i) free(argv[i]);
free(argv);
}
return NULL;
}
/**
* Slice the last n elements of an array (usually args) into
* a new array beginning at the given offset as [0].
*/
char **cli_slice_args(char *const src[], size_t n) {
size_t i = 0, start = 0, len = 0;
if (!src) return NULL;
while (src[len] != NULL) ++len;
/* clamp to available elements */
if (n > len) n = len;
char **dst = malloc((n + 1) * sizeof(char *));
if (!dst) return NULL;
start = len - n;
for (i = 0; i < n; ++i) {
dst[i] = src[start + i];
}
dst[n] = NULL;
return dst;
}
/**
* This is a helper that tries to re-construct a command line as it was before
* the calling shell stripped quotes as it broke down arguments into the argument
* array. It's not a perfect exact science, but it lets us (usually) keep interactive
* and non-interactive history the same.
*/
char *cli_rejoin_args(char *const src[]) {
char *empty = malloc(1);
if (empty == NULL) {
errno = ENOMEM;
return NULL;
}
empty[0] = '\0';
if (!src) return empty;
size_t total = 0, count = 0, i;
// First pass: count items and calculate total size needed
for (i = 0; src[i] != NULL; ++i) {
size_t item_len = strlen(src[i]);
int has_spaces = (strchr(src[i], ' ') != NULL);
int quote_count = 0;
// Count existing quotes that need escaping
const char *p_scan = src[i];
while ((p_scan = strchr(p_scan, '"')) != NULL) {
quote_count++;
p_scan++;
}
// Add base item length plus escapes for existing quotes
total += item_len + quote_count;
// If item has spaces, add 2 for wrapping quotes
if (has_spaces) {
total += 2;
}
++count;
}
if (count == 0) return empty;
free(empty);
// spaces between items = count - 1
total += (count > 1) ? (count - 1) : 0;
total += 1; // terminating NULL
char *out = malloc(total);
if (!out) return NULL;
char *p = out;
for (i = 0; i < count; ++i) {
const char *item = src[i];
int has_spaces = (strchr(item, ' ') != NULL);
// Add opening quote if item has spaces
if (has_spaces) {
*p++ = '"';
}
// Copy item character by character, escaping quotes
while (*item) {
if (*item == '"') {
*p++ = '\\'; // Add escape character
}
*p++ = *item++;
}
// Add closing quote if item has spaces
if (has_spaces) {
*p++ = '"';
}
// Add space separator if not the last item
if (i + 1 < count) {
*p++ = ' ';
}
}
*p = '\0';
return out;
}
/**
* Helper to free an allocated argument stack
*/
void cli_free_argv(char *argv[]) {
int i;
if (!argv) return;
for (i = 0; argv[i]; ++i) free(argv[i]);
free(argv);
}