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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* FRR string processing utilities.
* Copyright (C) 2018 Cumulus Networks, Inc.
* Quentin Young
* Copyright (c) 2023, LabN Consulting, L.L.C.
*/
#include "zebra.h"
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#ifdef HAVE_LIBPCRE2_POSIX
#ifndef _FRR_PCRE2_POSIX
#define _FRR_PCRE2_POSIX
#include <pcre2posix.h>
#endif /* _FRR_PCRE2_POSIX */
#elif defined(HAVE_LIBPCREPOSIX)
#include <pcreposix.h>
#else
#include <regex.h>
#endif /* HAVE_LIBPCRE2_POSIX */
#include "frrstr.h"
#include "memory.h"
#include "vector.h"
void frrstr_split(const char *string, const char *delimiter, char ***result,
int *argc)
{
if (!string)
return;
unsigned int sz = 4, idx = 0;
char *copy, *copystart;
*result = XCALLOC(MTYPE_TMP, sizeof(char *) * sz);
copystart = copy = XSTRDUP(MTYPE_TMP, string);
*argc = 0;
const char *tok = NULL;
while (copy) {
tok = strsep(©, delimiter);
(*result)[idx] = XSTRDUP(MTYPE_TMP, tok);
if (++idx == sz)
*result = XREALLOC(MTYPE_TMP, *result,
(sz *= 2) * sizeof(char *));
(*argc)++;
}
XFREE(MTYPE_TMP, copystart);
}
vector frrstr_split_vec(const char *string, const char *delimiter)
{
char **result;
int argc;
if (!string)
return NULL;
frrstr_split(string, delimiter, &result, &argc);
vector v = array_to_vector((void **)result, argc);
XFREE(MTYPE_TMP, result);
return v;
}
char *frrstr_join(const char **parts, int argc, const char *join)
{
int i;
char *str;
char *p;
size_t len = 0;
size_t joinlen = join ? strlen(join) : 0;
if (!argc)
return NULL;
for (i = 0; i < argc; i++)
len += strlen(parts[i]);
len += argc * joinlen + 1;
if (!len)
return NULL;
p = str = XMALLOC(MTYPE_TMP, len);
for (i = 0; i < argc; i++) {
size_t arglen = strlen(parts[i]);
memcpy(p, parts[i], arglen);
p += arglen;
if (i + 1 != argc && join) {
memcpy(p, join, joinlen);
p += joinlen;
}
}
*p = '\0';
return str;
}
char *frrstr_join_vec(vector v, const char *join)
{
char **argv;
int argc;
vector_to_array(v, (void ***)&argv, &argc);
char *ret = frrstr_join((const char **)argv, argc, join);
XFREE(MTYPE_TMP, argv);
return ret;
}
void frrstr_filter_vec(vector v, regex_t *filter)
{
regmatch_t ignored[1];
for (unsigned int i = 0; i < vector_active(v); i++) {
if (regexec(filter, vector_slot(v, i), 0, ignored, 0)) {
XFREE(MTYPE_TMP, vector_slot(v, i));
vector_unset(v, i);
}
}
}
void frrstr_strvec_free(vector v)
{
unsigned int i;
char *cp;
if (!v)
return;
for (i = 0; i < vector_active(v); i++) {
cp = vector_slot(v, i);
XFREE(MTYPE_TMP, cp);
}
vector_free(v);
}
char *frrstr_replace(const char *str, const char *find, const char *replace)
{
char *ch;
char *nustr = XSTRDUP(MTYPE_TMP, str);
size_t findlen = strlen(find);
size_t repllen = strlen(replace);
while ((ch = strstr(nustr, find))) {
if (repllen > findlen) {
size_t nusz = strlen(nustr) + repllen - findlen + 1;
nustr = XREALLOC(MTYPE_TMP, nustr, nusz);
ch = strstr(nustr, find);
}
size_t nustrlen = strlen(nustr);
size_t taillen = (nustr + nustrlen) - (ch + findlen);
memmove(ch + findlen + (repllen - findlen), ch + findlen,
taillen + 1);
memcpy(ch, replace, repllen);
}
return nustr;
}
bool frrstr_startswith(const char *str, const char *prefix)
{
if (!str || !prefix)
return false;
size_t lenstr = strlen(str);
size_t lenprefix = strlen(prefix);
if (lenprefix > lenstr)
return false;
return strncmp(str, prefix, lenprefix) == 0;
}
bool frrstr_endswith(const char *str, const char *suffix)
{
if (!str || !suffix)
return false;
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr)
return false;
return strncmp(&str[lenstr - lensuffix], suffix, lensuffix) == 0;
}
int all_digit(const char *str)
{
for (; *str != '\0'; str++)
if (!isdigit((unsigned char)*str))
return 0;
return 1;
}
char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num)
{
if (bufsiz == 0)
return buff;
char tmp[3];
buff[0] = '\0';
for (size_t i = 0; i < num; i++) {
snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]);
strlcat(buff, tmp, bufsiz);
}
return buff;
}
const char *frrstr_skip_over_char(const char *s, int skipc)
{
int c, quote = 0;
while ((c = *s++)) {
if (c == '\\') {
if (!*s++)
return NULL;
continue;
}
if (quote) {
if (c == quote)
quote = 0;
continue;
}
if (c == skipc)
return s;
if (c == '"' || c == '\'')
quote = c;
}
return NULL;
}
/*
* Advance backward in string until reaching the char `toc`
* if beginning of string is reached w/o finding char return NULL
*
* /foo/bar'baz/booz'/foo
*/
const char *frrstr_back_to_char(const char *s, int toc)
{
const char *next = s;
const char *prev = NULL;
if (s[0] == 0)
return NULL;
if (!strpbrk(s, "'\"\\"))
return strrchr(s, toc);
while ((next = frrstr_skip_over_char(next, toc)))
prev = next - 1;
return prev;
}
|