1 /*  Copyright (C) 2001-2004  Kenichi Suto
2  *
3  *  This program is free software; you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License as published by
5  *  the Free Software Foundation; either version 2 of the License, or
6  *  (at your option) any later version.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 */
17 
18 #include "defs.h"
19 #include "reg.h"
20 
regex_prepare(guchar * pat,gboolean ignore_case)21 REG_TABLE *regex_prepare(guchar *pat, gboolean ignore_case)
22 {
23 	REG_TABLE *reg;
24 
25 	reg = g_new(REG_TABLE, 1);
26 
27 	if(ignore_case == TRUE){
28 		if(0 != regcomp(reg, pat, REG_ICASE|REG_EXTENDED)){
29 			LOG(LOG_CRITICAL, "regcomp: %s", strerror(errno));
30 			g_free(reg);
31 			return(NULL);
32 		}
33 	} else {
34 		if(0 != regcomp(reg, pat, REG_EXTENDED)){
35 			LOG(LOG_CRITICAL, "regcomp: %s", strerror(errno));
36 			g_free(reg);
37 			return(NULL);
38 		}
39 	}
40 
41 	return(reg);
42 }
43 
regex_free(REG_TABLE * reg)44 void regex_free(REG_TABLE *reg)
45 {
46 	regfree(reg);
47 	g_free(reg);
48 }
49 
regex_search(REG_TABLE * reg,guchar * text)50 guchar *regex_search(REG_TABLE *reg, guchar *text){
51 	regmatch_t pmatch[1];
52 
53 	if(REG_NOMATCH == regexec(reg, text, 1, pmatch, 0)){
54 		return(FALSE);
55 	} else {
56 		if(pmatch[0].rm_so == -1){
57 			return(NULL);
58 		}
59 		return(text+pmatch[0].rm_so);
60 	}
61 }
62 
63