new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_regex / examples / regex_str.c
1 /* Copyright by Daniel B. Cid (2005, 2006)
2  * Under the public domain. It is just an example.
3  * Some examples of usage for the os_regex library.
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 #include "os_regex.h"
11
12
13 int main(int argc, char **argv)
14 {
15     int r_code = 0;
16
17     /* OSRegex structure */
18     OSRegex reg;
19
20     /* Check for arguments */
21     if (argc != 3) {
22         printf("%s regex string\n", argv[0]);
23         exit(1);
24     }
25
26     /* If the compilation failed, we don't need to free anything.
27      * We are passing the OS_RETURN_SUBSTRING because we want the
28      * substrings back.
29      */
30     if (OSRegex_Compile(argv[1], &reg, OS_RETURN_SUBSTRING)) {
31         const char *retv;
32         /* If the execution succeeds, the substrings will be
33          * at reg.sub_strings
34          */
35         if ((retv = OSRegex_Execute(argv[2], &reg))) {
36             int sub_size = 0;
37             char **ret;
38             r_code = 1;
39
40             /* Next pt */
41             printf("next pt: '%s'\n", retv);
42             /* Assign reg.sub_strings to ret */
43             ret = reg.sub_strings;
44
45             printf("substrings:\n");
46             while (*ret) {
47                 printf("  %d: !%s!\n", sub_size, *ret);
48                 sub_size++;
49                 ret++;
50             }
51
52             /* We must free the substrings */
53             OSRegex_FreeSubStrings(&reg);
54         } else {
55             printf("Error: Didn't match.\n");
56         }
57
58         OSRegex_FreePattern(&reg);
59     }
60
61     /* Compilation error */
62     else {
63         printf("Error: Regex Compile Error: %d\n", reg.error);
64     }
65
66     return (r_code);
67 }
68