aboutsummaryrefslogtreecommitdiff
path: root/modules/pcre/pcre.c
blob: 07a8b04fadbc60bf76ce6b6fe1fc4d4bbdc5cbf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* This file is part of GNU Dico.
   Copyright (C) 2008, 2010, 2012 Sergey Poznyakoff

   GNU Dico is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Dico is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <dico.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <appi18n.h>
#include <pcre.h>

struct dico_pcre_flag
{
    int c;
    int flag;
};

static struct dico_pcre_flag flagtab[] = {
    { 'a', PCRE_ANCHORED }, /* Force pattern anchoring */
    { 'e', PCRE_EXTENDED }, /* Ignore whitespace and # comments */
    { 'i', PCRE_CASELESS }, /* Do caseless matching */
    { 'G', PCRE_UNGREEDY }, /* Invert greediness of quantifiers */
    { 0 },
};

static int
pcre_flag(int c, int *pflags)
{
    struct dico_pcre_flag *p;
    
    for (p = flagtab; p->c; p++) {
	if (p->c == c) {
	    *pflags |= p->flag;
	    return 0;
	} else if (p->c == tolower(c) || p->c == toupper(c)) {
	    *pflags &= ~p->flag;
	    return 0;
	}
    }
    return 1;
}
    
static pcre *
compile_pattern(const char *pattern)
{
    int cflags = PCRE_UTF8|PCRE_NEWLINE_ANY;
    const char *error;
    int error_offset;
    char *tmp = NULL;
    pcre *pre;
	
    if (pattern[0] == '/') {
	size_t len;
	char *p;

	pattern++;
	p = strrchr(pattern, '/');
	if (!p) {
	    dico_log(L_ERR, 0, _("PCRE missing terminating /: %s"),
		     pattern - 1);
	    return NULL;
	}
	len = p - pattern;

	while (*++p) {
	    if (pcre_flag(*p, &cflags)) {
		dico_log(L_ERR, 0, _("PCRE error: invalid flag %c"), *p);
		return NULL;
	    }
	}
    
	tmp = malloc(len + 1);
	if (!tmp)
	    return NULL;
	memcpy(tmp, pattern, len);
	tmp[len] = 0;
	pattern = tmp;
    }
    pre = pcre_compile(pattern, cflags, &error, &error_offset, 0);
    if (!pre) {
	dico_log(L_ERR, 0, 
		 _("pcre_compile(\"%s\") failed at offset %d: %s"),
		 pattern, error_offset, error);
    }
    free(tmp);
    return pre;
}

static int
pcre_sel(int cmd, dico_key_t key, const char *dict_word)
{
    int rc = 0;
    char const *word = key->word;
    pcre *pre = key->call_data;

    switch (cmd) {
    case DICO_SELECT_BEGIN:
	pre = compile_pattern(word);
	if (!pre)
	    return 1;
	key->call_data = pre;
	break;

    case DICO_SELECT_RUN:
	rc = pcre_exec(pre, 0, dict_word, strlen(dict_word), 0, 0,
		       NULL, 0) >= 0;
	break;
	
    case DICO_SELECT_END:
	pcre_free(pre);
	break;
    }
    return rc;
}

static struct dico_strategy pcre_strat = {
    "pcre",
    "Match using Perl-compatible regular expressions",
    pcre_sel
};

static int
pcre_init(int argc, char **argv)
{
    dico_strategy_add(&pcre_strat);
    return 0;
}

struct dico_database_module DICO_EXPORT(pcre, module) = {
    DICO_MODULE_VERSION,
    DICO_CAPA_NODB,
    pcre_init,
};

Return to:

Send suggestions and report system problems to the System administrator.