15 #define min(a,b) ((a)<(b) ? (a) : (b))
17 /* callbacks specified by the calling function to extract
18 substrings to values. */
19 void regulo_atoi(void *dest_int
, const char *source
)
21 /* skip any leading non-digit */
22 while (*source
!= '\0' && !isdigit(*source
))
25 *(int *) dest_int
= atoi(source
);
28 void regulo_strcpy(void *dest
, const char *source
)
30 strcpy((char *) dest
, source
);
33 void regulo_strcpy_tolower(void *dest
, const char *source
)
36 for (i
= 0; i
< strlen(source
); i
++) {
37 ((char *) dest
)[i
] = tolower(source
[i
]);
39 ((char *) dest
)[i
] = '\0';
42 void regulo_strcpy_skip1(void *dest
, const char *source
)
44 strcpy((char *) dest
, source
+ 1);
48 /* deprecated as unportable */
51 regulo_match(const char *regex
,
52 const char *string
, const struct regulo
*instructions
)
54 struct re_registers regs
;
58 memset(®s
, 0, sizeof(struct re_registers
));
59 matchedchars
= compile_and_match_regex(regex
, string
, ®s
);
60 if (matchedchars
<= 0)
62 if (instructions
== NULL
)
64 for (i
= 0; instructions
[i
].match_handler
!= NULL
; i
++) {
66 int j
= instructions
[i
].match_index
;
67 int len
= min(254, regs
.end
[j
] - regs
.start
[j
]);
68 if (regs
.start
[j
] >= 0) {
69 strncpy(buf
, string
+ regs
.start
[j
], len
);
71 instructions
[i
].match_handler(instructions
[i
].destination
,
76 free(regs
.end
); // added 3 jul 02, appeasing valgrind
77 free(regs
.start
); // added 3 jul 02, appeasing valgrind
84 int compile_and_match_regex_posix(const char *regex
, const char *str
, /*@out@ */
85 regmatch_t
* regs
, size_t regs_len
)
89 if ((errcode
= regcomp(®
, regex
, REG_EXTENDED
)) != 0) {
91 regerror(errcode
, ®
, errbuf
, 256);
92 fprintf(stderr
, "error in compiling regular expression: %s\n",
97 errcode
= regexec(®
, str
, regs_len
, regs
, 0);
107 regulo_match(const char *regex
,
108 const char *string
, const struct regulo
*instructions
)
114 matchedchars
= compile_and_match_regex_posix(regex
, string
, regs
, 20);
115 if (matchedchars
<= 0)
117 if (instructions
== NULL
)
119 for (i
= 0; instructions
[i
].match_handler
!= NULL
; i
++) {
121 int j
= instructions
[i
].match_index
;
122 int len
= min(254, regs
[j
].rm_eo
- regs
[j
].rm_so
);
123 if (regs
[j
].rm_so
>= 0) {
124 strncpy(buf
, string
+ regs
[j
].rm_so
, len
);
126 instructions
[i
].match_handler(instructions
[i
].destination
,