| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 1999 */ |
| 6 |
/* All Rights Reserved. */ |
| 7 |
/* */ |
| 8 |
/* Permission is hereby granted, free of charge, to use and distribute */ |
| 9 |
/* this software and its documentation without restriction, including */ |
| 10 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 11 |
/* distribute, sublicense, and/or sell copies of this work, and to */ |
| 12 |
/* permit persons to whom this work is furnished to do so, subject to */ |
| 13 |
/* the following conditions: */ |
| 14 |
/* 1. The code must retain the above copyright notice, this list of */ |
| 15 |
/* conditions and the following disclaimer. */ |
| 16 |
/* 2. Any modifications must be clearly marked as such. */ |
| 17 |
/* 3. Original authors' names are not deleted. */ |
| 18 |
/* 4. The authors' names are not used to endorse or promote products */ |
| 19 |
/* derived from this software without specific prior written */ |
| 20 |
/* permission. */ |
| 21 |
/* */ |
| 22 |
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ |
| 23 |
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ |
| 24 |
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ |
| 25 |
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ |
| 26 |
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ |
| 27 |
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ |
| 28 |
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ |
| 29 |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 |
/* THIS SOFTWARE. */ |
| 31 |
/* */ |
| 32 |
/*************************************************************************/ |
| 33 |
/* Author: Alan W Black ([email protected]) */ |
| 34 |
/* Date: January 2000 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Regexes, this is just a front end to Henry Spencer's regex code */ |
| 38 |
/* Includes a mapping of fsf format regex's to hs format (escaping) */ |
| 39 |
/* */ |
| 40 |
/*************************************************************************/ |
| 41 |
#include "cst_alloc.h" |
| 42 |
#include "cst_regex.h" |
| 43 |
|
| 44 |
#include "cst_regex_defs.h" |
| 45 |
|
| 46 |
/* For access by const models */ |
| 47 |
const cst_regex *const cst_regex_table[] = { |
| 48 |
&cst_rx_dotted_abbrev_rx |
| 49 |
}; |
| 50 |
|
| 51 |
static char *regularize(const char *unregex,int match); |
| 52 |
|
| 53 |
void cst_regex_init() |
| 54 |
{ |
| 55 |
/* no need to initialize regexes anymore, they are pre-compiled */ |
| 56 |
|
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
int cst_regex_match(const cst_regex *r, const char *str) |
| 61 |
{ |
| 62 |
cst_regstate *s; |
| 63 |
|
| 64 |
if (r == NULL) return 0; |
| 65 |
s = hs_regexec(r, str); |
| 66 |
if (s) { |
| 67 |
cst_free(s); |
| 68 |
return 1; |
| 69 |
} else |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
cst_regstate *cst_regex_match_return(const cst_regex *r, const char *str) |
| 74 |
{ |
| 75 |
if (r == NULL) |
| 76 |
return 0; |
| 77 |
|
| 78 |
return hs_regexec(r, str); |
| 79 |
} |
| 80 |
|
| 81 |
cst_regex *new_cst_regex(const char *str) |
| 82 |
{ |
| 83 |
cst_regex *r; |
| 84 |
char *reg_str = regularize(str,1); |
| 85 |
|
| 86 |
r = hs_regcomp(reg_str); |
| 87 |
cst_free(reg_str); |
| 88 |
|
| 89 |
return r; |
| 90 |
} |
| 91 |
|
| 92 |
void delete_cst_regex(cst_regex *r) |
| 93 |
{ |
| 94 |
if (r) |
| 95 |
hs_regdelete(r); |
| 96 |
|
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
/* These define the different escape conventions for the FSF's */ |
| 101 |
/* regexp code and Henry Spencer's */ |
| 102 |
|
| 103 |
static const char * const fsf_magic="^$*+?[].\\"; |
| 104 |
static const char * const fsf_magic_backslashed="()|<>"; |
| 105 |
static const char * const spencer_magic="^$*+?[].()|\\\n"; |
| 106 |
static const char * const spencer_magic_backslashed="<>"; |
| 107 |
|
| 108 |
/* Adaptation of rjc's mapping of fsf format to henry spencer's format */ |
| 109 |
/* of escape sequences, as taken from EST_Regex.cc in EST */ |
| 110 |
static char *regularize(const char *unregex,int match) |
| 111 |
{ |
| 112 |
char *reg = cst_alloc(char, cst_strlen(unregex)*2+3); |
| 113 |
char *r=reg; |
| 114 |
const char *e; |
| 115 |
int magic=0,last_was_bs=0; |
| 116 |
const char * in_brackets=NULL; |
| 117 |
const char *ex = (unregex?unregex:""); |
| 118 |
|
| 119 |
if (match && *ex != '^') |
| 120 |
*(r++) = '^'; |
| 121 |
|
| 122 |
for(e=ex; *e ; e++) |
| 123 |
{ |
| 124 |
if (*e == '\\' && !last_was_bs) |
| 125 |
{ |
| 126 |
last_was_bs=1; |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
magic=strchr((last_was_bs?fsf_magic_backslashed:fsf_magic), *e)!=NULL; |
| 131 |
|
| 132 |
if (in_brackets) |
| 133 |
{ |
| 134 |
*(r++) = *e; |
| 135 |
if (*e == ']' && (e-in_brackets)>1) |
| 136 |
in_brackets=0; |
| 137 |
} |
| 138 |
else if (magic) |
| 139 |
{ |
| 140 |
if (strchr(spencer_magic_backslashed, *e)) |
| 141 |
*(r++) = '\\'; |
| 142 |
|
| 143 |
*(r++) = *e; |
| 144 |
if (*e == '[') |
| 145 |
in_brackets=e; |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
if (strchr(spencer_magic, *e)) |
| 150 |
*(r++) = '\\'; |
| 151 |
|
| 152 |
*(r++) = *e; |
| 153 |
} |
| 154 |
last_was_bs=0; |
| 155 |
} |
| 156 |
|
| 157 |
if (match && (e==ex || *(e-1) != '$')) |
| 158 |
{ |
| 159 |
if (last_was_bs) |
| 160 |
*(r++) = '\\'; |
| 161 |
*(r++) = '$'; |
| 162 |
} |
| 163 |
|
| 164 |
*r='\0'; |
| 165 |
|
| 166 |
return reg; |
| 167 |
} |