2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2016-2017, 2019 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
9 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /// $CompilerFlags: find_compiler_flags("tre")
25 /// $LinkerFlags: find_linker_flags("tre" "-ltre")
27 /// $PackageInfo: require_system("arch") pkgconf tre
28 /// $PackageInfo: require_system("darwin") pkg-config tre
29 /// $PackageInfo: require_system("debian") libtre-dev pkg-config
30 /// $PackageInfo: require_system("ubuntu") libtre-dev pkg-config
33 #include "modules/regex.h"
34 #include <sys/types.h>
35 #include <tre/regex.h>
37 class TRERegex : public Regex
42 TRERegex(const std::string& rx) : Regex(rx)
44 int flags = REG_EXTENDED | REG_NOSUB;
46 errcode = regcomp(®buf, rx.c_str(), flags);
49 // Get the error string into a std::string. YUCK this involves at least 2 string copies.
52 size_t sz = regerror(errcode, ®buf, NULL, 0);
53 errbuf = new char[sz + 1];
54 memset(errbuf, 0, sz + 1);
55 regerror(errcode, ®buf, errbuf, sz + 1);
59 throw RegexException(rx, error);
68 bool Matches(const std::string& text) CXX11_OVERRIDE
70 return (regexec(®buf, text.c_str(), 0, NULL, 0) == 0);
74 class TREFactory : public RegexFactory
77 TREFactory(Module* m) : RegexFactory(m, "regex/tre") {}
78 Regex* Create(const std::string& expr) CXX11_OVERRIDE
80 return new TRERegex(expr);
84 class ModuleRegexTRE : public Module
89 ModuleRegexTRE() : trf(this)
93 Version GetVersion() CXX11_OVERRIDE
95 return Version("Provides a regular expression engine which uses the TRE library.", VF_VENDOR);
99 MODULE_INIT(ModuleRegexTRE)