|
ds@1952
|
1 |
/** |
|
ds@1952
|
2 |
* vim: set ts=4 : |
|
ds@1952
|
3 |
* ============================================================================= |
|
ds@1990
|
4 |
* SourceMod Regular Expressions Extension |
|
ds@1952
|
5 |
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. |
|
ds@1952
|
6 |
* ============================================================================= |
|
ds@1952
|
7 |
* |
|
ds@1952
|
8 |
* This program is free software; you can redistribute it and/or modify it under |
|
ds@1952
|
9 |
* the terms of the GNU General Public License, version 3.0, as published by the |
|
ds@1952
|
10 |
* Free Software Foundation. |
|
ds@1952
|
11 |
* |
|
ds@1952
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
ds@1952
|
13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
ds@1952
|
14 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
ds@1952
|
15 |
* details. |
|
ds@1952
|
16 |
* |
|
ds@1952
|
17 |
* You should have received a copy of the GNU General Public License along with |
|
ds@1952
|
18 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
|
ds@1952
|
19 |
* |
|
ds@1952
|
20 |
* As a special exception, AlliedModders LLC gives you permission to link the |
|
ds@1952
|
21 |
* code of this program (as well as its derivative works) to "Half-Life 2," the |
|
ds@1952
|
22 |
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software |
|
ds@1952
|
23 |
* by the Valve Corporation. You must obey the GNU General Public License in |
|
ds@1952
|
24 |
* all respects for all other code used. Additionally, AlliedModders LLC grants |
|
ds@1952
|
25 |
* this exception to all derivative works. AlliedModders LLC defines further |
|
ds@1952
|
26 |
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), |
|
ds@1952
|
27 |
* or <http://www.sourcemod.net/license.php>. |
|
ds@1952
|
28 |
* |
|
ds@1952
|
29 |
* Version: $Id$ |
|
ds@1952
|
30 |
*/ |
|
ds@1952
|
31 |
|
|
ds@1952
|
32 |
#include "extension.h" |
|
ds@1952
|
33 |
|
|
ds@1952
|
34 |
#include <sh_string.h> |
|
ds@1952
|
35 |
#include "pcre.h" |
|
ds@1952
|
36 |
#include "CRegEx.h" |
|
ds@1952
|
37 |
using namespace SourceHook; |
|
ds@1952
|
38 |
|
|
ds@1952
|
39 |
/** |
|
ds@1952
|
40 |
* @file extension.cpp |
|
ds@1990
|
41 |
* @brief Implement Regex extension code here. |
|
ds@1952
|
42 |
*/ |
|
ds@1952
|
43 |
|
|
ds@1952
|
44 |
RegexExtension g_RegexExtension; /**< Global singleton for extension's main interface */ |
|
ds@1952
|
45 |
|
|
ds@1952
|
46 |
SMEXT_LINK(&g_RegexExtension); |
|
ds@1952
|
47 |
|
|
ds@1952
|
48 |
RegexHandler g_RegexHandler; |
|
ds@1952
|
49 |
HandleType_t g_RegexHandle=0; |
|
ds@1952
|
50 |
|
|
ds@1952
|
51 |
|
|
ds@1952
|
52 |
|
|
ds@1952
|
53 |
bool RegexExtension::SDK_OnLoad(char *error, size_t err_max, bool late) |
|
ds@1952
|
54 |
{ |
|
ds@1952
|
55 |
g_pShareSys->AddNatives(myself,regex_natives); |
|
ds@1952
|
56 |
g_RegexHandle = g_pHandleSys->CreateType("Regex", &g_RegexHandler, 0, NULL, NULL, myself->GetIdentity(), NULL); |
|
ds@1952
|
57 |
return true; |
|
ds@1952
|
58 |
} |
|
ds@1952
|
59 |
|
|
ds@1952
|
60 |
void RegexExtension::SDK_OnUnload() |
|
ds@1952
|
61 |
{ |
|
ds@1952
|
62 |
g_pHandleSys->RemoveType(g_RegexHandle, myself->GetIdentity()); |
|
ds@1952
|
63 |
|
|
ds@1952
|
64 |
} |
|
ds@1952
|
65 |
|
|
ds@1952
|
66 |
static cell_t CompileRegex(IPluginContext *pCtx, const cell_t *params) |
|
ds@1952
|
67 |
{ |
|
ds@1952
|
68 |
char *regex; |
|
ds@1952
|
69 |
pCtx->LocalToString(params[1], ®ex); |
|
ds@1952
|
70 |
|
|
ds@1952
|
71 |
RegEx *x = new RegEx(); |
|
ds@1952
|
72 |
|
|
ds@1952
|
73 |
if (x->Compile(regex, params[2]) == 0) |
|
ds@1952
|
74 |
{ |
|
ds@1952
|
75 |
cell_t *eOff; |
|
ds@1952
|
76 |
pCtx->LocalToPhysAddr(params[5], &eOff); |
|
ds@1952
|
77 |
const char *err = x->mError; |
|
ds@1952
|
78 |
*eOff = x->mErrorOffset; |
|
ds@1952
|
79 |
pCtx->StringToLocal(params[3], params[4], err ? err:"unknown"); |
|
ds@1952
|
80 |
return 0; |
|
ds@1952
|
81 |
} |
|
ds@1952
|
82 |
|
|
ds@1952
|
83 |
return g_pHandleSys->CreateHandle(g_RegexHandle, (void*)x, pCtx->GetIdentity(), myself->GetIdentity(), NULL); |
|
ds@1952
|
84 |
} |
|
ds@1952
|
85 |
|
|
ds@1952
|
86 |
|
|
ds@1952
|
87 |
static cell_t MatchRegex(IPluginContext *pCtx, const cell_t *params) |
|
ds@1952
|
88 |
{ |
|
ds@1952
|
89 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@1952
|
90 |
HandleError err; |
|
ds@1952
|
91 |
HandleSecurity sec; |
|
ds@1952
|
92 |
sec.pOwner = NULL; |
|
ds@1952
|
93 |
sec.pIdentity = myself->GetIdentity(); |
|
ds@1952
|
94 |
|
|
ds@1952
|
95 |
RegEx *x; |
|
ds@1952
|
96 |
|
|
ds@1952
|
97 |
if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None) |
|
ds@1952
|
98 |
{ |
|
ds@1952
|
99 |
return pCtx->ThrowNativeError("Invalid file handle %x (error %d)", hndl, err); |
|
ds@1952
|
100 |
} |
|
ds@1952
|
101 |
|
|
ds@1952
|
102 |
if (!x) |
|
ds@1952
|
103 |
{ |
|
ds@1952
|
104 |
pCtx->ThrowNativeError("Regex data not found\n"); |
|
ds@1952
|
105 |
|
|
ds@1952
|
106 |
return 0; |
|
ds@1952
|
107 |
} |
|
ds@1952
|
108 |
|
|
ds@1952
|
109 |
char *str; |
|
ds@1952
|
110 |
pCtx->LocalToString(params[2], &str); |
|
ds@1952
|
111 |
|
|
ds@1952
|
112 |
int e = x->Match(str); |
|
ds@1952
|
113 |
|
|
ds@1952
|
114 |
if (e == -1) |
|
ds@1952
|
115 |
{ |
|
ds@1952
|
116 |
/* there was a match error. move on. */ |
|
ds@1952
|
117 |
cell_t *res; |
|
ds@1952
|
118 |
pCtx->LocalToPhysAddr(params[3], &res); |
|
ds@1952
|
119 |
*res = x->mErrorOffset; |
|
ds@1952
|
120 |
/* only clear the match results, since the regex object |
|
ds@1952
|
121 |
may still be referenced later */ |
|
ds@1952
|
122 |
x->ClearMatch(); |
|
ds@1952
|
123 |
|
|
ds@1952
|
124 |
return -1; |
|
ds@1952
|
125 |
} |
|
ds@1952
|
126 |
else if (e == 0) |
|
ds@1952
|
127 |
{ |
|
ds@1952
|
128 |
/* only clear the match results, since the regex object |
|
ds@1952
|
129 |
may still be referenced later */ |
|
ds@1952
|
130 |
x->ClearMatch(); |
|
ds@1952
|
131 |
|
|
ds@1952
|
132 |
return 0; |
|
ds@1952
|
133 |
} |
|
ds@1952
|
134 |
else |
|
ds@1952
|
135 |
{ |
|
ds@1952
|
136 |
return x->mSubStrings; |
|
ds@1952
|
137 |
} |
|
ds@1952
|
138 |
} |
|
ds@1952
|
139 |
|
|
ds@1952
|
140 |
static cell_t GetRegexSubString(IPluginContext *pCtx, const cell_t *params) |
|
ds@1952
|
141 |
{ |
|
ds@1952
|
142 |
Handle_t hndl=static_cast<Handle_t>(params[1]); |
|
ds@1952
|
143 |
HandleError err; |
|
ds@1952
|
144 |
HandleSecurity sec; |
|
ds@1952
|
145 |
sec.pOwner=NULL; |
|
ds@1952
|
146 |
sec.pIdentity=myself->GetIdentity(); |
|
ds@1952
|
147 |
|
|
ds@1952
|
148 |
RegEx *x; |
|
ds@1952
|
149 |
|
|
ds@1952
|
150 |
if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None) |
|
ds@1952
|
151 |
{ |
|
ds@1952
|
152 |
return pCtx->ThrowNativeError("Invalid file handle %x (error %d)", hndl, err); |
|
ds@1952
|
153 |
} |
|
ds@1952
|
154 |
|
|
ds@1952
|
155 |
if (!x) |
|
ds@1952
|
156 |
{ |
|
ds@1952
|
157 |
pCtx->ThrowNativeError("Regex data not found\n"); |
|
ds@1952
|
158 |
return 0; |
|
ds@1952
|
159 |
} |
|
ds@1952
|
160 |
|
|
ds@1952
|
161 |
static char buffer[4096]; |
|
ds@1952
|
162 |
const char *ret=x->GetSubstring(params[2], buffer, sizeof(buffer)); |
|
ds@1952
|
163 |
|
|
ds@1952
|
164 |
if(!ret) |
|
ds@1952
|
165 |
{ |
|
ds@1952
|
166 |
return 0; |
|
ds@1952
|
167 |
} |
|
ds@1952
|
168 |
|
|
ds@1952
|
169 |
pCtx->StringToLocalUTF8(params[3], params[4], ret, NULL); |
|
ds@1952
|
170 |
|
|
ds@1952
|
171 |
return 1; |
|
ds@1952
|
172 |
} |
|
ds@1952
|
173 |
|
|
ds@1952
|
174 |
void RegexHandler::OnHandleDestroy(HandleType_t type, void *object) |
|
ds@1952
|
175 |
{ |
|
ds@1952
|
176 |
RegEx *x = (RegEx *)object; |
|
ds@1952
|
177 |
|
|
ds@1952
|
178 |
x->Clear(); |
|
ds@1952
|
179 |
delete x; |
|
ds@1952
|
180 |
} |
|
ds@1952
|
181 |
|
|
ds@1952
|
182 |
const sp_nativeinfo_t regex_natives[] = |
|
ds@1952
|
183 |
{ |
|
ds@1952
|
184 |
{"GetRegexSubString", GetRegexSubString}, |
|
ds@1952
|
185 |
{"MatchRegex", MatchRegex}, |
|
ds@1952
|
186 |
{"CompileRegex", CompileRegex}, |
|
ds@1952
|
187 |
{NULL, NULL}, |
|
ds@1952
|
188 |
}; |