Skip to content

Commit 8a29b01

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

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

main/main.c

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,105 @@ config_updated_cb(mender_keystore_t *configuration) {
180180

181181
#ifdef CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT
182182

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

0 commit comments

Comments
 (0)