Imported Upstream version 2.5.1
[ossec-hids.git] / src / util / ossec-regex.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12
13 /* This tool will clear the project statistics */
14
15 #include "shared.h"
16
17 #undef ARGV0
18 #define ARGV0 "ossec-regex"
19
20
21 /** help **/
22 void helpmsg()
23 {
24     printf("\nOSSEC HIDS %s: ossec-regex pattern\n", ARGV0);
25     exit(1);
26 }
27
28
29 /** main **/
30 int main(int argc, char **argv)
31 {
32     char *pattern;
33     
34     char msg[OS_MAXSTR +1];
35     memset(msg, '\0', OS_MAXSTR +1);
36     OSRegex regex; 
37     OSMatch matcher; 
38
39     OS_SetName(ARGV0);
40         
41     
42     /* user arguments */
43     if(argc != 2)
44     {
45         helpmsg();
46         return(-1);
47     }
48     
49     /* User options */
50     if(strcmp(argv[1], "-h") == 0)
51     {
52         helpmsg();
53         return(-1);
54     }
55
56     os_strdup(argv[1], pattern);
57     if(!OSRegex_Compile(pattern, &regex, 0)) 
58     { 
59         printf("pattern does not compile with OSRegex_Compile\n");
60         return(-1); 
61     }
62     if(!OSMatch_Compile(pattern, &matcher, 0))
63     {
64         printf("pattern does not compile with OSMatch_Compile\n");
65         return(-1);
66     }
67
68
69     while((fgets(msg, OS_MAXSTR, stdin)) != NULL)
70     {        
71         /* Removing new line. */                                                                                                                                       
72         if(msg[strlen(msg) -1] == '\n')
73             msg[strlen(msg) -1] = '\0';
74
75         /* Make sure we ignore blank lines. */                                                                                                                         
76         if(strlen(msg) < 2) { continue; }                                                            
77
78         if(OSRegex_Execute(msg, &regex))
79             printf("+OSRegex_Execute: %s\n",msg); 
80         /*
81         else
82             printf("-OSRegex_Execute: \n"); 
83             */
84
85         if(OS_Regex(pattern, msg)) 
86             printf("+OS_Regex       : %s\n", msg);
87         /*
88         else
89             printf("-OS_Regex: \n"); 
90             */
91
92         if(OSMatch_Execute(msg, strlen(msg), &matcher)) 
93             printf("+OSMatch_Compile: %s\n", msg); 
94         
95         if(OS_Match2(pattern, msg)) 
96             printf("+OS_Match2      : %s\n", msg); 
97     }
98     return(0);
99 }
100
101
102 /* EOF */