40
40
#include "mem.h"
41
41
#include "vcard.h"
42
42
43
+ /**
44
+ * Compile regex, checking and handling errors.
45
+ *
46
+ * \parm[in] preg The compiled regex.
47
+ * \parm[in] regex The pattern to match.
48
+ * \parm[in] cflags The compilation flags according to regex(3).
49
+ *
50
+ * \retval 0 If there were no errors.
51
+ * \retval 1 If an error was encounted.
52
+ **/
53
+ static int
54
+ xregcomp (regex_t * preg , const char * regex , int cflags ) {
55
+ int rerr = 0 ; /* Regex error code */
56
+ size_t rlen = 0 ; /* Regex error string length */
57
+ char * rstr = NULL ; /* Regex error string */
58
+
59
+ rerr = regcomp (preg , regex , REG_EXTENDED | cflags );
60
+ if (rerr != 0 ) {
61
+ rlen = regerror (rerr , preg , NULL , 0 );
62
+ rstr = xmalloc ((rlen + 1 )* sizeof (char ));
63
+ regerror (rerr , preg , rstr , rlen );
64
+ warnx (_ ("Unable to compile regex '%s': %s\n" ), regex , rstr );
65
+ if (rstr ) {
66
+ free (rstr );
67
+ rstr = NULL ;
68
+ }
69
+ return 1 ;
70
+ }
71
+ return 0 ;
72
+ }
73
+
43
74
/**
44
75
* Search a query's result. This will run regexs over the result
45
76
* to filter the data.
@@ -63,8 +94,6 @@ search(const char *card)
63
94
int plen = 0 ; /* Length of snprintf()'s */
64
95
65
96
int rerr = 0 ; /* Regex error code */
66
- size_t rlen = 0 ; /* Regex error string length */
67
- char * rstr = NULL ; /* Regex error string */
68
97
69
98
size_t qlen = 0 ; /* Length of the query string */
70
99
char * q = NULL ; /* Regex pattern for query */
@@ -96,15 +125,7 @@ search(const char *card)
96
125
return (EXIT_FAILURE );
97
126
}
98
127
99
- if ((rerr = regcomp (& rq , q , REG_EXTENDED |REG_NEWLINE |REG_ICASE )) != 0 ) {
100
- rlen = regerror (rerr , & rq , NULL , 0 );
101
- rstr = xmalloc ((rlen + 1 )* sizeof (char ));
102
- regerror (rerr , & rq , rstr , rlen );
103
- warnx (_ ("Unable to compile regex '%s': %s\n" ), q , rstr );
104
- if (rstr ) {
105
- free (rstr );
106
- rstr = NULL ;
107
- }
128
+ if (xregcomp (& rq , q , REG_NEWLINE |REG_ICASE ) != 0 ) {
108
129
return (EXIT_FAILURE );
109
130
}
110
131
@@ -119,15 +140,7 @@ search(const char *card)
119
140
return (EXIT_FAILURE );
120
141
}
121
142
122
- if ((rerr = regcomp (& rs , s , REG_EXTENDED |REG_NEWLINE )) != 0 ) {
123
- rlen = regerror (rerr , & rs , NULL , 0 );
124
- rstr = xmalloc ((rlen + 1 )* sizeof (char ));
125
- regerror (rerr , & rs , rstr , rlen );
126
- warnx (_ ("Unable to compile regex '%s': %s\n" ), s , rstr );
127
- if (rstr ) {
128
- free (rstr );
129
- rstr = NULL ;
130
- }
143
+ if (xregcomp (& rs , s , REG_NEWLINE ) != 0 ) {
131
144
return (EXIT_FAILURE );
132
145
}
133
146
0 commit comments