Skip to content

Commit 0bdc409

Browse files
committed
core: utils: remove str replace function from the client library
1 parent 2f2e1bb commit 0bdc409

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

main/main.c

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "mender-troubleshoot.h"
4141
#include <nvs_flash.h>
4242
#include <protocol_examples_common.h>
43+
#include <regex.h>
4344
#include "sdkconfig.h"
4445

4546
/**
@@ -180,6 +181,105 @@ config_updated_cb(mender_keystore_t *configuration) {
180181

181182
#ifdef CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT
182183

184+
/**
185+
* @brief Function used to replace a string in the input buffer
186+
* @param input Input buffer
187+
* @param search String to be replaced or regex expression
188+
* @param replace Replacement string
189+
* @return New string with replacements if the function succeeds, NULL otherwise
190+
*/
191+
static char *
192+
str_replace(char *input, char *search, char *replace) {
193+
194+
assert(NULL != input);
195+
assert(NULL != search);
196+
assert(NULL != replace);
197+
198+
regex_t regex;
199+
regmatch_t match;
200+
char * str = input;
201+
char * output = NULL;
202+
size_t index = 0;
203+
int previous_match_finish = 0;
204+
205+
/* Compile expression */
206+
if (0 != regcomp(&regex, search, REG_EXTENDED)) {
207+
/* Unable to compile expression */
208+
mender_log_error("Unable to compile expression '%s'", search);
209+
return NULL;
210+
}
211+
212+
/* Loop until all search string are replaced */
213+
bool loop = true;
214+
while (true == loop) {
215+
216+
/* Search wanted string */
217+
if (0 != regexec(&regex, str, 1, &match, 0)) {
218+
/* No more string to be replaced */
219+
loop = false;
220+
} else {
221+
if (match.rm_so != -1) {
222+
223+
/* Beginning and ending offset of the match */
224+
int current_match_start = (int)(match.rm_so + (str - input));
225+
int current_match_finish = (int)(match.rm_eo + (str - input));
226+
227+
/* Reallocate output memory */
228+
char *tmp = (char *)realloc(output, index + (current_match_start - previous_match_finish) + 1);
229+
if (NULL == tmp) {
230+
mender_log_error("Unable to allocate memory");
231+
regfree(&regex);
232+
free(output);
233+
return NULL;
234+
}
235+
output = tmp;
236+
237+
/* Copy string from previous match to the beginning of the current match */
238+
memcpy(&output[index], &input[previous_match_finish], current_match_start - previous_match_finish);
239+
index += (current_match_start - previous_match_finish);
240+
output[index] = 0;
241+
242+
/* Reallocate output memory */
243+
if (NULL == (tmp = (char *)realloc(output, index + strlen(replace) + 1))) {
244+
mender_log_error("Unable to allocate memory");
245+
regfree(&regex);
246+
free(output);
247+
return NULL;
248+
}
249+
output = tmp;
250+
251+
/* Copy replace string to the output */
252+
strcat(output, replace);
253+
index += strlen(replace);
254+
255+
/* Update previous match ending value */
256+
previous_match_finish = current_match_finish;
257+
}
258+
str += match.rm_eo;
259+
}
260+
}
261+
262+
/* Reallocate output memory */
263+
char *tmp = (char *)realloc(output, index + (strlen(input) - previous_match_finish) + 1);
264+
if (NULL == tmp) {
265+
mender_log_error("Unable to allocate memory");
266+
regfree(&regex);
267+
free(output);
268+
return NULL;
269+
}
270+
output = tmp;
271+
272+
/* Copy the end of the string after the latest match */
273+
memcpy(&output[index], &input[previous_match_finish], strlen(input) - previous_match_finish);
274+
index += (strlen(input) - previous_match_finish);
275+
output[index] = 0;
276+
277+
/* Release regex */
278+
regfree(&regex);
279+
280+
return output;
281+
}
282+
183283
/**
184284
* @brief Shell vprintf function used to route logs
185285
* @param format Log format string
@@ -204,7 +304,7 @@ shell_vprintf(const char *format, va_list args) {
204304
if (NULL == (buffer = strndup(data, length))) {
205305
goto END;
206306
}
207-
if (NULL == (tmp = mender_utils_str_replace(buffer, "\r|\n", "\r\n"))) {
307+
if (NULL == (tmp = str_replace(buffer, "\r|\n", "\r\n"))) {
208308
goto END;
209309
}
210310
buffer = tmp;
@@ -273,7 +373,7 @@ shell_write_cb(uint8_t *data, size_t length) {
273373
ret = MENDER_FAIL;
274374
goto END;
275375
}
276-
if (NULL == (tmp = mender_utils_str_replace(buffer, "\r|\n", "\r\n"))) {
376+
if (NULL == (tmp = str_replace(buffer, "\r|\n", "\r\n"))) {
277377
ESP_LOGE(TAG, "Unable to allocate memory");
278378
ret = MENDER_FAIL;
279379
goto END;

0 commit comments

Comments
 (0)