40
40
#include "mender-troubleshoot.h"
41
41
#include <nvs_flash.h>
42
42
#include <protocol_examples_common.h>
43
+ #include <regex.h>
43
44
#include "sdkconfig.h"
44
45
45
46
/**
@@ -180,6 +181,105 @@ config_updated_cb(mender_keystore_t *configuration) {
180
181
181
182
#ifdef CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT
182
183
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
+
183
283
/**
184
284
* @brief Shell vprintf function used to route logs
185
285
* @param format Log format string
@@ -204,7 +304,7 @@ shell_vprintf(const char *format, va_list args) {
204
304
if (NULL == (buffer = strndup (data , length ))) {
205
305
goto END ;
206
306
}
207
- if (NULL == (tmp = mender_utils_str_replace (buffer , "\r|\n" , "\r\n" ))) {
307
+ if (NULL == (tmp = str_replace (buffer , "\r|\n" , "\r\n" ))) {
208
308
goto END ;
209
309
}
210
310
buffer = tmp ;
@@ -273,7 +373,7 @@ shell_write_cb(uint8_t *data, size_t length) {
273
373
ret = MENDER_FAIL ;
274
374
goto END ;
275
375
}
276
- if (NULL == (tmp = mender_utils_str_replace (buffer , "\r|\n" , "\r\n" ))) {
376
+ if (NULL == (tmp = str_replace (buffer , "\r|\n" , "\r\n" ))) {
277
377
ESP_LOGE (TAG , "Unable to allocate memory" );
278
378
ret = MENDER_FAIL ;
279
379
goto END ;
0 commit comments