00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <stdarg.h>
00017 #include <stdint.h>
00018 #include <string.h>
00019 #include <errno.h>
00020
00021 #include "util.h"
00022 #include "regexp.h"
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 const char *regexp_error_string(int error)
00067 {
00068 switch(error) {
00069 case PCRE_ERROR_NOMATCH: return "PCRE_ERROR_NOMATCH";
00070 case PCRE_ERROR_NULL: return "PCRE_ERROR_NULL";
00071 case PCRE_ERROR_BADOPTION: return "PCRE_ERROR_BADOPTION";
00072 case PCRE_ERROR_BADMAGIC: return "PCRE_ERROR_BADMAGIC";
00073 case PCRE_ERROR_UNKNOWN_OPCODE: return "PCRE_ERROR_UNKNOWN_OPCODE";
00074 case PCRE_ERROR_NOMEMORY: return "PCRE_ERROR_NOMEMORY";
00075 case PCRE_ERROR_NOSUBSTRING: return "PCRE_ERROR_NOSUBSTRING";
00076 case PCRE_ERROR_MATCHLIMIT: return "PCRE_ERROR_MATCHLIMIT";
00077 case PCRE_ERROR_CALLOUT: return "PCRE_ERROR_CALLOUT";
00078 case PCRE_ERROR_BADUTF8: return "PCRE_ERROR_BADUTF8";
00079 case PCRE_ERROR_BADUTF8_OFFSET: return "PCRE_ERROR_BADUTF8_OFFSET";
00080 case PCRE_ERROR_PARTIAL: return "PCRE_ERROR_PARTIAL";
00081 case PCRE_ERROR_BADPARTIAL: return "PCRE_ERROR_BADPARTIAL";
00082 case PCRE_ERROR_INTERNAL: return "PCRE_ERROR_INTERNAL";
00083 case PCRE_ERROR_BADCOUNT: return "PCRE_ERROR_BADCOUNT";
00084 case PCRE_ERROR_DFA_UITEM: return "PCRE_ERROR_DFA_UITEM";
00085 case PCRE_ERROR_DFA_UCOND: return "PCRE_ERROR_DFA_UCOND";
00086 case PCRE_ERROR_DFA_UMLIMIT: return "PCRE_ERROR_DFA_UMLIMIT";
00087 case PCRE_ERROR_DFA_WSSIZE: return "PCRE_ERROR_DFA_WSSIZE";
00088 case PCRE_ERROR_DFA_RECURSE: return "PCRE_ERROR_DFA_RECURSE";
00089 case PCRE_ERROR_RECURSIONLIMIT: return "PCRE_ERROR_RECURSIONLIMIT";
00090 case PCRE_ERROR_NULLWSLIMIT: return "PCRE_ERROR_NULLWSLIMIT";
00091 case PCRE_ERROR_BADNEWLINE: return "PCRE_ERROR_BADNEWLINE";
00092 default: return "unknown";
00093 }
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 int regexp_compile(struct regexp_t *regexp)
00103 {
00104 const char *error_str;
00105 int error_code, error_offset;
00106
00107 if ((regexp->re = pcre_compile2(regexp->pattern, 0, &error_code,
00108 &error_str, &error_offset, NULL)) == NULL) {
00109 int i;
00110 char *ruler = malloc(error_offset + 2);
00111
00112 if (ruler) {
00113 for (i = 0; i < error_offset; i++) ruler[i] = ' ';
00114 ruler[error_offset] = '^';
00115 ruler[error_offset + 1] = 0;
00116 }
00117
00118 log_msg(LOG_ERROR, _("regexp compilation failed at offset %d\n%s\n%s %s\n"),
00119 error_offset, regexp->pattern, ruler, error_str);
00120 free(ruler);
00121 return error_code;
00122 }
00123 return SUCCESS;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 int regexp_search(struct regexp_t *regexp, const char *subject)
00136 {
00137 int result;
00138
00139 regexp->subject = subject;
00140 result = pcre_exec(regexp->re, NULL,
00141 subject, strlen(subject), 0, 0,
00142 regexp->matches, sizeof(regexp->matches)/sizeof(int));
00143
00144 if (result < 0) {
00145 regexp->num_matches = 0;
00146 regexp->error = result;
00147 if (result != PCRE_ERROR_NOMATCH) {
00148 log_msg(LOG_ERROR, _("regexp_search failed, error=%s(%d) pattern=\"%s\" subject=\"%s\"\n"),
00149 regexp_error_string(regexp->error), regexp->error, regexp->pattern, subject);
00150 }
00151
00152 } else {
00153 regexp->num_matches = result;
00154 regexp->error = 0;
00155 }
00156 return regexp->num_matches;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 int regexp_substring(struct regexp_t *regexp, const int match_number, char* buffer, int buffer_size)
00169 {
00170 if (buffer && buffer_size > 0) buffer[0] = 0;
00171
00172 if (match_number >= regexp->num_matches) {
00173 return PCRE_ERROR_NOSUBSTRING;
00174 }
00175
00176 if ((regexp->error = pcre_copy_substring(regexp->subject, regexp->matches, regexp->num_matches,
00177 match_number, buffer, buffer_size)) < 0) {
00178 log_msg(LOG_ERROR, _("regexp_substring failed, match_number=%d num_matches=%d error=%s(%d) pattern=\"%s\" subject=\"%s\"\n"),
00179 match_number, regexp->num_matches,
00180 regexp_error_string(regexp->error), regexp->error, regexp->pattern, regexp->subject);
00181 return regexp->error;
00182 }
00183 return SUCCESS;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 int regexp_free(struct regexp_t *regexp)
00195 {
00196 if (regexp->re) pcre_free(regexp->re);
00197 return SUCCESS;
00198 }
00199