1 From fb7c59cafceb35628d40c727dbfa2990335b922a Mon Sep 17 00:00:00 2001
2 From: Victor Seva <linuxmaniac@torreviejawireless.org>
3 Date: Wed, 17 May 2023 16:37:10 +0200
4 Subject: [PATCH] regex: clang-format for coherent indentation and coding style
7 src/modules/regex/regex_mod.c | 313 ++++++++++++++++------------------
8 1 file changed, 150 insertions(+), 163 deletions(-)
10 --- a/src/modules/regex/regex_mod.c
11 +++ b/src/modules/regex/regex_mod.c
12 @@ -49,9 +49,9 @@ MODULE_VERSION
16 -#define FILE_MAX_LINE 500 /*!< Max line size in the file */
17 -#define MAX_GROUPS 20 /*!< Max number of groups */
18 -#define GROUP_MAX_SIZE 8192 /*!< Max size of a group */
19 +#define FILE_MAX_LINE 500 /*!< Max line size in the file */
20 +#define MAX_GROUPS 20 /*!< Max number of groups */
21 +#define GROUP_MAX_SIZE 8192 /*!< Max size of a group */
24 static int regex_init_rpc(void);
25 @@ -66,12 +66,12 @@ gen_lock_t *reload_lock;
26 * Module exported parameter variables
29 -static int max_groups = MAX_GROUPS;
30 -static int group_max_size = GROUP_MAX_SIZE;
31 -static int pcre_caseless = 0;
32 -static int pcre_multiline = 0;
33 -static int pcre_dotall = 0;
34 -static int pcre_extended = 0;
35 +static int max_groups = MAX_GROUPS;
36 +static int group_max_size = GROUP_MAX_SIZE;
37 +static int pcre_caseless = 0;
38 +static int pcre_multiline = 0;
39 +static int pcre_dotall = 0;
40 +static int pcre_extended = 0;
44 @@ -100,119 +100,117 @@ static void free_shared_memory(void);
48 -static int w_pcre_match(struct sip_msg* _msg, char* _s1, char* _s2);
49 -static int w_pcre_match_group(struct sip_msg* _msg, char* _s1, char* _s2);
50 +static int w_pcre_match(struct sip_msg *_msg, char *_s1, char *_s2);
51 +static int w_pcre_match_group(struct sip_msg *_msg, char *_s1, char *_s2);
57 -static cmd_export_t cmds[] =
59 - { "pcre_match", (cmd_function)w_pcre_match, 2, fixup_spve_spve, 0,
60 - REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
61 - { "pcre_match_group", (cmd_function)w_pcre_match_group, 2, fixup_spve_spve, 0,
62 - REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
63 - { "pcre_match_group", (cmd_function)w_pcre_match_group, 1, fixup_spve_null, 0,
64 - REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
65 - { 0, 0, 0, 0, 0, 0 }
67 +static cmd_export_t cmds[] = {
68 + {"pcre_match", (cmd_function)w_pcre_match, 2, fixup_spve_spve, 0,
69 + REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE | BRANCH_ROUTE
71 + {"pcre_match_group", (cmd_function)w_pcre_match_group, 2,
73 + REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE | BRANCH_ROUTE
75 + {"pcre_match_group", (cmd_function)w_pcre_match_group, 1,
77 + REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE | BRANCH_ROUTE
79 + {0, 0, 0, 0, 0, 0}};
85 -static param_export_t params[] = {
86 - {"file", PARAM_STRING, &file },
87 - {"max_groups", INT_PARAM, &max_groups },
88 - {"group_max_size", INT_PARAM, &group_max_size },
89 - {"pcre_caseless", INT_PARAM, &pcre_caseless },
90 - {"pcre_multiline", INT_PARAM, &pcre_multiline },
91 - {"pcre_dotall", INT_PARAM, &pcre_dotall },
92 - {"pcre_extended", INT_PARAM, &pcre_extended },
95 +static param_export_t params[] = {{"file", PARAM_STRING, &file},
96 + {"max_groups", INT_PARAM, &max_groups},
97 + {"group_max_size", INT_PARAM, &group_max_size},
98 + {"pcre_caseless", INT_PARAM, &pcre_caseless},
99 + {"pcre_multiline", INT_PARAM, &pcre_multiline},
100 + {"pcre_dotall", INT_PARAM, &pcre_dotall},
101 + {"pcre_extended", INT_PARAM, &pcre_extended}, {0, 0, 0}};
107 struct module_exports exports = {
108 - "regex", /*!< module name */
109 - DEFAULT_DLFLAGS, /*!< dlopen flags */
110 - cmds, /*!< exported functions */
111 - params, /*!< exported parameters */
112 - 0, /*!< exported RPC functions */
113 - 0, /*!< exported pseudo-variables */
114 - 0, /*!< response handling function */
115 - mod_init, /*!< module initialization function */
116 - 0, /*!< per-child init function */
117 - destroy /*!< destroy function */
118 + "regex", /*!< module name */
119 + DEFAULT_DLFLAGS, /*!< dlopen flags */
120 + cmds, /*!< exported functions */
121 + params, /*!< exported parameters */
122 + 0, /*!< exported RPC functions */
123 + 0, /*!< exported pseudo-variables */
124 + 0, /*!< response handling function */
125 + mod_init, /*!< module initialization function */
126 + 0, /*!< per-child init function */
127 + destroy /*!< destroy function */
133 * Init module function
135 static int mod_init(void)
137 - if(regex_init_rpc()<0)
139 + if(regex_init_rpc() < 0) {
140 LM_ERR("failed to register RPC commands\n");
144 /* Group matching feature */
145 - if (file == NULL) {
147 LM_NOTICE("'file' parameter is not set, group matching disabled\n");
149 /* Create and init the lock */
150 reload_lock = lock_alloc();
151 - if (reload_lock == NULL) {
152 + if(reload_lock == NULL) {
153 LM_ERR("cannot allocate reload_lock\n");
156 - if (lock_init(reload_lock) == NULL) {
157 + if(lock_init(reload_lock) == NULL) {
158 LM_ERR("cannot init the reload_lock\n");
159 lock_dealloc(reload_lock);
164 - if (pcre_caseless != 0) {
165 + if(pcre_caseless != 0) {
166 LM_DBG("PCRE CASELESS enabled\n");
167 pcre_options = pcre_options | PCRE_CASELESS;
169 - if (pcre_multiline != 0) {
170 + if(pcre_multiline != 0) {
171 LM_DBG("PCRE MULTILINE enabled\n");
172 pcre_options = pcre_options | PCRE_MULTILINE;
174 - if (pcre_dotall != 0) {
175 + if(pcre_dotall != 0) {
176 LM_DBG("PCRE DOTALL enabled\n");
177 pcre_options = pcre_options | PCRE_DOTALL;
179 - if (pcre_extended != 0) {
180 + if(pcre_extended != 0) {
181 LM_DBG("PCRE EXTENDED enabled\n");
182 pcre_options = pcre_options | PCRE_EXTENDED;
184 LM_DBG("PCRE options: %i\n", pcre_options);
186 /* Pointer to pcres */
187 - if ((pcres_addr = shm_malloc(sizeof(pcre **))) == 0) {
188 + if((pcres_addr = shm_malloc(sizeof(pcre **))) == 0) {
189 LM_ERR("no memory for pcres_addr\n");
193 /* Integer containing the number of pcres */
194 - if ((num_pcres = shm_malloc(sizeof(int))) == 0) {
195 + if((num_pcres = shm_malloc(sizeof(int))) == 0) {
196 LM_ERR("no memory for num_pcres\n");
201 LM_DBG("loading pcres...\n");
202 - if (load_pcres(START)) {
203 + if(load_pcres(START)) {
204 LM_ERR("failed to load pcres\n");
207 @@ -251,21 +249,21 @@ static int load_pcres(int action)
209 lock_get(reload_lock);
211 - if (!(f = fopen(file, "r"))) {
212 + if(!(f = fopen(file, "r"))) {
213 LM_ERR("could not open file '%s'\n", file);
217 /* Array containing each pattern in the file */
218 - if ((patterns = pkg_malloc(sizeof(char*) * max_groups)) == 0) {
219 + if((patterns = pkg_malloc(sizeof(char *) * max_groups)) == 0) {
220 LM_ERR("no more memory for patterns\n");
224 - memset(patterns, 0, sizeof(char*) * max_groups);
225 + memset(patterns, 0, sizeof(char *) * max_groups);
227 - for (i=0; i<max_groups; i++) {
228 - if ((patterns[i] = pkg_malloc(sizeof(char) * group_max_size)) == 0) {
229 + for(i = 0; i < max_groups; i++) {
230 + if((patterns[i] = pkg_malloc(sizeof(char) * group_max_size)) == 0) {
231 LM_ERR("no more memory for patterns[%d]\n", i);
234 @@ -276,26 +274,27 @@ static int load_pcres(int action)
235 /* Read the file and extract the patterns */
236 memset(line, 0, FILE_MAX_LINE);
238 - while (fgets(line, FILE_MAX_LINE-4, f) != NULL) {
239 + while(fgets(line, FILE_MAX_LINE - 4, f) != NULL) {
241 /* Ignore comments and lines starting by space, tab, CR, LF */
242 - if(isspace(line[0]) || line[0]=='#') {
243 + if(isspace(line[0]) || line[0] == '#') {
244 memset(line, 0, FILE_MAX_LINE);
249 - if (i == -1 && line[0] != '[') {
250 - LM_ERR("first group must be initialized with [0] before any regular expression\n");
251 + if(i == -1 && line[0] != '[') {
252 + LM_ERR("first group must be initialized with [0] before any "
253 + "regular expression\n");
259 - if (line[0] == '[') {
260 + if(line[0] == '[') {
262 /* Check if there are more patterns than the max value */
263 - if (i >= max_groups) {
264 + if(i >= max_groups) {
265 LM_ERR("max patterns exceeded\n");
268 @@ -309,14 +308,14 @@ static int load_pcres(int action)
271 /* Check if the patter size is too big (aprox) */
272 - if (strlen(patterns[i]) + llen >= group_max_size - 4) {
273 + if(strlen(patterns[i]) + llen >= group_max_size - 4) {
274 LM_ERR("pattern max file exceeded\n");
279 /* Append ')' at the end of the line */
280 - if (line[llen - 1] == '\n') {
281 + if(line[llen - 1] == '\n') {
282 line[llen - 1] = ')';
284 line[llen + 1] = '\0';
285 @@ -328,7 +327,7 @@ static int load_pcres(int action)
287 /* Append '(' at the beginning of the line */
288 llen = strlen(patterns[i]);
289 - memcpy(patterns[i]+llen, "(", 1);
290 + memcpy(patterns[i] + llen, "(", 1);
293 /* Append the line to the current pattern (including the ending 0) */
294 @@ -340,16 +339,16 @@ static int load_pcres(int action)
298 - if(num_pcres_tmp==0) {
299 + if(num_pcres_tmp == 0) {
300 LM_ERR("no expressions in the file\n");
304 /* Fix the patterns */
305 - for (i=0; i < num_pcres_tmp; i++) {
306 + for(i = 0; i < num_pcres_tmp; i++) {
308 /* Convert empty groups in unmatcheable regular expression ^$ */
309 - if (strlen(patterns[i]) == 1) {
310 + if(strlen(patterns[i]) == 1) {
311 patterns[i][0] = '^';
312 patterns[i][1] = '$';
313 patterns[i][2] = '\0';
314 @@ -357,13 +356,13 @@ static int load_pcres(int action)
317 /* Delete possible '\n' at the end of the pattern */
318 - if (patterns[i][strlen(patterns[i])-1] == '\n') {
319 - patterns[i][strlen(patterns[i])-1] = '\0';
320 + if(patterns[i][strlen(patterns[i]) - 1] == '\n') {
321 + patterns[i][strlen(patterns[i]) - 1] = '\0';
324 /* Replace '\n' with '|' (except at the end of the pattern) */
325 - for (j=0; j < strlen(patterns[i]); j++) {
326 - if (patterns[i][j] == '\n' && j != strlen(patterns[i])-1) {
327 + for(j = 0; j < strlen(patterns[i]); j++) {
328 + if(patterns[i][j] == '\n' && j != strlen(patterns[i]) - 1) {
329 patterns[i][j] = '|';
332 @@ -374,38 +373,38 @@ static int load_pcres(int action)
334 /* Log the group patterns */
335 LM_INFO("num groups = %d\n", num_pcres_tmp);
336 - for (i=0; i < num_pcres_tmp; i++) {
337 - LM_INFO("<group[%d]>%s</group[%d]> (size = %i)\n", i, patterns[i],
338 - i, (int)strlen(patterns[i]));
339 + for(i = 0; i < num_pcres_tmp; i++) {
340 + LM_INFO("<group[%d]>%s</group[%d]> (size = %i)\n", i, patterns[i], i,
341 + (int)strlen(patterns[i]));
344 /* Temporal pointer of pcres */
345 - if ((pcres_tmp = pkg_malloc(sizeof(pcre *) * num_pcres_tmp)) == 0) {
346 + if((pcres_tmp = pkg_malloc(sizeof(pcre *) * num_pcres_tmp)) == 0) {
347 LM_ERR("no more memory for pcres_tmp\n");
350 - for (i=0; i<num_pcres_tmp; i++) {
351 + for(i = 0; i < num_pcres_tmp; i++) {
355 /* Compile the patters */
356 - for (i=0; i<num_pcres_tmp; i++) {
357 + for(i = 0; i < num_pcres_tmp; i++) {
359 - pcre_tmp = pcre_compile(patterns[i], pcre_options, &pcre_error,
360 - &pcre_erroffset, NULL);
361 - if (pcre_tmp == NULL) {
362 + pcre_tmp = pcre_compile(
363 + patterns[i], pcre_options, &pcre_error, &pcre_erroffset, NULL);
364 + if(pcre_tmp == NULL) {
365 LM_ERR("pcre_tmp compilation of '%s' failed at offset %d: %s\n",
366 patterns[i], pcre_erroffset, pcre_error);
369 pcre_rc = pcre_fullinfo(pcre_tmp, NULL, PCRE_INFO_SIZE, &pcre_size);
372 printf("pcre_fullinfo on compiled pattern[%i] yielded error: %d\n",
377 - if ((pcres_tmp[i] = pkg_malloc(pcre_size)) == 0) {
378 + if((pcres_tmp[i] = pkg_malloc(pcre_size)) == 0) {
379 LM_ERR("no more memory for pcres_tmp[%i]\n", i);
382 @@ -417,22 +416,22 @@ static int load_pcres(int action)
385 /* Copy to shared memory */
386 - if (action == RELOAD) {
387 - for(i=0; i<*num_pcres; i++) { /* Use the previous num_pcres value */
389 + if(action == RELOAD) {
390 + for(i = 0; i < *num_pcres; i++) { /* Use the previous num_pcres value */
397 - if ((pcres = shm_malloc(sizeof(pcre *) * num_pcres_tmp)) == 0) {
398 + if((pcres = shm_malloc(sizeof(pcre *) * num_pcres_tmp)) == 0) {
399 LM_ERR("no more memory for pcres\n");
402 memset(pcres, 0, sizeof(pcre *) * num_pcres_tmp);
403 - for (i=0; i<num_pcres_tmp; i++) {
404 + for(i = 0; i < num_pcres_tmp; i++) {
405 pcre_rc = pcre_fullinfo(pcres_tmp[i], NULL, PCRE_INFO_SIZE, &pcre_size);
406 - if ((pcres[i] = shm_malloc(pcre_size)) == 0) {
407 + if((pcres[i] = shm_malloc(pcre_size)) == 0) {
408 LM_ERR("no more memory for pcres[%i]\n", i);
411 @@ -442,12 +441,12 @@ static int load_pcres(int action)
414 /* Free used memory */
415 - for (i=0; i<num_pcres_tmp; i++) {
416 + for(i = 0; i < num_pcres_tmp; i++) {
417 pkg_free(pcres_tmp[i]);
420 /* Free allocated slots for unused patterns */
421 - for (i = num_pcres_tmp; i < max_groups; i++) {
422 + for(i = num_pcres_tmp; i < max_groups; i++) {
423 pkg_free(patterns[i]);
426 @@ -456,26 +455,26 @@ static int load_pcres(int action)
431 - for(i=0; i<max_groups; i++) {
434 + for(i = 0; i < max_groups; i++) {
436 pkg_free(patterns[i]);
442 - for (i=0; i<num_pcres_tmp; i++) {
443 - if (pcres_tmp[i]) {
445 + for(i = 0; i < num_pcres_tmp; i++) {
447 pkg_free(pcres_tmp[i]);
454 lock_release(reload_lock);
456 - if (action == START) {
457 + if(action == START) {
458 free_shared_memory();
461 @@ -486,9 +485,9 @@ static void free_shared_memory(void)
466 - for(i=0; i<*num_pcres; i++) {
469 + for(i = 0; i < *num_pcres; i++) {
474 @@ -496,21 +495,21 @@ static void free_shared_memory(void)
486 shm_free(pcres_addr);
492 lock_destroy(reload_lock);
493 lock_dealloc(reload_lock);
500 @@ -519,32 +518,32 @@ static void free_shared_memory(void)
503 /*! \brief Return true if the argument matches the regular expression parameter */
504 -static int ki_pcre_match(sip_msg_t* msg, str* string, str* regex)
505 +static int ki_pcre_match(sip_msg_t *msg, str *string, str *regex)
507 pcre *pcre_re = NULL;
509 const char *pcre_error;
512 - pcre_re = pcre_compile(regex->s, pcre_options, &pcre_error, &pcre_erroffset, NULL);
513 - if (pcre_re == NULL) {
514 + pcre_re = pcre_compile(
515 + regex->s, pcre_options, &pcre_error, &pcre_erroffset, NULL);
516 + if(pcre_re == NULL) {
517 LM_ERR("pcre_re compilation of '%s' failed at offset %d: %s\n",
518 regex->s, pcre_erroffset, pcre_error);
522 - pcre_rc = pcre_exec(
523 - pcre_re, /* the compiled pattern */
524 - NULL, /* no extra data - we didn't study the pattern */
525 - string->s, /* the matching string */
526 - (int)(string->len), /* the length of the subject */
527 - 0, /* start at offset 0 in the string */
528 - 0, /* default options */
529 - NULL, /* output vector for substring information */
530 - 0); /* number of elements in the output vector */
531 + pcre_rc = pcre_exec(pcre_re, /* the compiled pattern */
532 + NULL, /* no extra data - we didn't study the pattern */
533 + string->s, /* the matching string */
534 + (int)(string->len), /* the length of the subject */
535 + 0, /* start at offset 0 in the string */
536 + 0, /* default options */
537 + NULL, /* output vector for substring information */
538 + 0); /* number of elements in the output vector */
540 /* Matching failed: handle error cases */
544 case PCRE_ERROR_NOMATCH:
545 LM_DBG("'%s' doesn't match '%s'\n", string->s, regex->s);
546 @@ -562,28 +561,26 @@ static int ki_pcre_match(sip_msg_t* msg,
549 /*! \brief Return true if the argument matches the regular expression parameter */
550 -static int w_pcre_match(struct sip_msg* _msg, char* _s1, char* _s2)
551 +static int w_pcre_match(struct sip_msg *_msg, char *_s1, char *_s2)
558 LM_ERR("bad parameters\n");
564 LM_ERR("bad parameters\n");
568 - if (fixup_get_svalue(_msg, (gparam_p)_s1, &string))
570 + if(fixup_get_svalue(_msg, (gparam_p)_s1, &string)) {
571 LM_ERR("cannot print the format for string\n");
574 - if (fixup_get_svalue(_msg, (gparam_p)_s2, ®ex))
576 + if(fixup_get_svalue(_msg, (gparam_p)_s2, ®ex)) {
577 LM_ERR("cannot print the format for regex\n");
580 @@ -592,17 +589,17 @@ static int w_pcre_match(struct sip_msg*
583 /*! \brief Return true if the string argument matches the pattern group parameter */
584 -static int ki_pcre_match_group(sip_msg_t* _msg, str* string, int num_pcre)
585 +static int ki_pcre_match_group(sip_msg_t *_msg, str *string, int num_pcre)
589 /* Check if group matching feature is enabled */
590 - if (file == NULL) {
592 LM_ERR("group matching is disabled\n");
596 - if (num_pcre >= *num_pcres) {
597 + if(num_pcre >= *num_pcres) {
598 LM_ERR("invalid pcre index '%i', there are %i pcres\n", num_pcre,
601 @@ -610,20 +607,19 @@ static int ki_pcre_match_group(sip_msg_t
603 lock_get(reload_lock);
605 - pcre_rc = pcre_exec(
606 - (*pcres_addr)[num_pcre], /* the compiled pattern */
607 - NULL, /* no extra data - we didn't study the pattern */
608 - string->s, /* the matching string */
609 - (int)(string->len), /* the length of the subject */
610 - 0, /* start at offset 0 in the string */
611 - 0, /* default options */
612 - NULL, /* output vector for substring information */
613 - 0); /* number of elements in the output vector */
614 + pcre_rc = pcre_exec((*pcres_addr)[num_pcre], /* the compiled pattern */
615 + NULL, /* no extra data - we didn't study the pattern */
616 + string->s, /* the matching string */
617 + (int)(string->len), /* the length of the subject */
618 + 0, /* start at offset 0 in the string */
619 + 0, /* default options */
620 + NULL, /* output vector for substring information */
621 + 0); /* number of elements in the output vector */
623 lock_release(reload_lock);
625 /* Matching failed: handle error cases */
629 case PCRE_ERROR_NOMATCH:
630 LM_DBG("'%s' doesn't match pcres[%i]\n", string->s, num_pcre);
631 @@ -640,29 +636,27 @@ static int ki_pcre_match_group(sip_msg_t
634 /*! \brief Return true if the string argument matches the pattern group parameter */
635 -static int w_pcre_match_group(struct sip_msg* _msg, char* _s1, char* _s2)
636 +static int w_pcre_match_group(struct sip_msg *_msg, char *_s1, char *_s2)
639 unsigned int num_pcre = 0;
643 LM_ERR("bad parameters\n");
651 - if (fixup_get_svalue(_msg, (gparam_p)_s2, &group))
653 + if(fixup_get_svalue(_msg, (gparam_p)_s2, &group)) {
654 LM_ERR("cannot print the format for second param\n");
657 str2int(&group, &num_pcre);
660 - if (fixup_get_svalue(_msg, (gparam_p)_s1, &string))
662 + if(fixup_get_svalue(_msg, (gparam_p)_s1, &string)) {
663 LM_ERR("cannot print the format for first param\n");
666 @@ -676,42 +670,35 @@ static int w_pcre_match_group(struct sip
669 /*! \brief Reload pcres by reading the file again */
670 -void regex_rpc_reload(rpc_t* rpc, void* ctx)
671 +void regex_rpc_reload(rpc_t *rpc, void *ctx)
673 /* Check if group matching feature is enabled */
674 - if (file == NULL) {
676 LM_NOTICE("'file' parameter is not set, group matching disabled\n");
677 rpc->fault(ctx, 500, "Group matching not enabled");
680 LM_INFO("reloading pcres...\n");
681 - if (load_pcres(RELOAD)) {
682 + if(load_pcres(RELOAD)) {
683 LM_ERR("failed to reload pcres\n");
684 rpc->fault(ctx, 500, "Failed to reload");
687 LM_INFO("reload success\n");
691 -static const char* regex_rpc_reload_doc[2] = {
692 - "Reload regex file",
695 +static const char *regex_rpc_reload_doc[2] = {"Reload regex file", 0};
697 rpc_export_t regex_rpc_cmds[] = {
698 - {"regex.reload", regex_rpc_reload,
699 - regex_rpc_reload_doc, 0},
702 + {"regex.reload", regex_rpc_reload, regex_rpc_reload_doc, 0},
706 * register RPC commands
708 static int regex_init_rpc(void)
710 - if (rpc_register_array(regex_rpc_cmds)!=0)
712 + if(rpc_register_array(regex_rpc_cmds) != 0) {
713 LM_ERR("failed to register RPC commands\n");