Imported Upstream version 2.7
[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 /* Must be included */
11 #include "os_regex.h"
12
13 int main(int argc,char **argv)
14 {
15     int r_code = 0;
16     char **ret;
17
18     /* OSRegex structure */
19     OSRegex reg;
20
21     /* checking for arguments */
22     if(argc != 3)
23     {
24         printf("%s regex string\n",argv[0]);
25         exit(1);
26     }
27
28
29     /* If the compilation failed, we don't need to free anything.
30      * We are passing the OS_RETURN_SUBSTRING because we wan't the
31      * substrings back.
32      */
33     if(OSRegex_Compile(argv[1], &reg, OS_RETURN_SUBSTRING))
34     {
35         char *retv;
36         /* If the execution succeeds, the substrings will be
37          * at reg.sub_strings
38          */
39         if((retv = OSRegex_Execute(argv[2], &reg)))
40         {
41             int sub_size = 0;
42             r_code = 1;
43
44             /* next pt */
45             printf("next pt: '%s'\n", retv);
46             /* Assigning reg.sub_strings to ret */
47             ret = reg.sub_strings;
48
49             printf("substrings:\n");
50             while(*ret)
51             {
52                 printf("  %d: !%s!\n", sub_size, *ret);
53                 sub_size++; ret++;
54             }
55
56             /* We must free the substrings */
57             OSRegex_FreeSubStrings(&reg);
58         }
59         else
60         {
61             printf("Error: Didn't match.\n");
62         }
63
64         OSRegex_FreePattern(&reg);
65     }
66
67     /* Compilation error */
68     else
69     {
70         printf("Error: Regex Compile Error: %d\n", reg.error);
71     }
72
73     return(r_code);
74 }
75 /* EOF */