aboutsummaryrefslogtreecommitdiff
path: root/dicod/regex.c
blob: 7535b827e21ddd01c4109000fd2e04e0b2f585f6 (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
/* This file is part of GNU Dico.
   Copyright (C) 2008-2018 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/>. */

#include <dicod.h>
#include <regex.h>

struct regex_flags {
    int flags;
};
    
struct regex_data {
    regex_t reg;
};

static int
regex_sel(int cmd, dico_key_t key, const char *dict_word)
{
    char const *word = key->word;
    struct regex_flags *fp = key->strat->closure;
    struct regex_data *rp = key->call_data;
    int rc;

    switch (cmd) {
    case DICO_SELECT_BEGIN:
	rp = malloc(sizeof(*rp));
	if (!rp)
	    return 1;
	key->call_data = rp;
	rc = regcomp(&rp->reg, word, fp->flags);
	if (rc) {
	    char errbuf[512];
	    regerror(rc, &rp->reg, errbuf, sizeof(errbuf));
	    dico_log(L_ERR, 0, _("Regex error: %s"), errbuf);
	}
	break;

    case DICO_SELECT_RUN:
	rc = regexec(&rp->reg, dict_word, 0, NULL, 0) == 0;
	break;
	
    case DICO_SELECT_END:
	rc = 0;
	regfree(&rp->reg);
	free(rp);
	break;
    }
    return rc;
}

static struct regex_flags ext_flags = {
    REG_EXTENDED|REG_ICASE
};

static struct regex_flags basic_flags = {
    REG_ICASE
};

static struct dico_strategy re_strat = {
    "re",
    "POSIX 1003.2 (modern) regular expressions",
    regex_sel,
    &ext_flags
};

static struct dico_strategy regex_strat = {
    "regexp",
    "Old (basic) regular expressions",
    regex_sel,
    &basic_flags
};

void
register_regex(void)
{
    dico_strategy_add(&re_strat);
    dico_strategy_add(&regex_strat);
}

    

Return to:

Send suggestions and report system problems to the System administrator.