ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1990: * SourceMod Regular Expressions Extension
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include "extension.h"
ds@1952:
ds@1952: #include
ds@1952: #include "pcre.h"
ds@1952: #include "CRegEx.h"
ds@1952: using namespace SourceHook;
ds@1952:
ds@1952: /**
ds@1952: * @file extension.cpp
ds@1990: * @brief Implement Regex extension code here.
ds@1952: */
ds@1952:
ds@1952: RegexExtension g_RegexExtension; /**< Global singleton for extension's main interface */
ds@1952:
ds@1952: SMEXT_LINK(&g_RegexExtension);
ds@1952:
ds@1952: RegexHandler g_RegexHandler;
ds@1952: HandleType_t g_RegexHandle=0;
ds@1952:
ds@1952:
ds@1952:
ds@1952: bool RegexExtension::SDK_OnLoad(char *error, size_t err_max, bool late)
ds@1952: {
ds@1952: g_pShareSys->AddNatives(myself,regex_natives);
ds@1952: g_RegexHandle = g_pHandleSys->CreateType("Regex", &g_RegexHandler, 0, NULL, NULL, myself->GetIdentity(), NULL);
ds@1952: return true;
ds@1952: }
ds@1952:
ds@1952: void RegexExtension::SDK_OnUnload()
ds@1952: {
ds@1952: g_pHandleSys->RemoveType(g_RegexHandle, myself->GetIdentity());
ds@1952:
ds@1952: }
ds@1952:
ds@1952: static cell_t CompileRegex(IPluginContext *pCtx, const cell_t *params)
ds@1952: {
ds@1952: char *regex;
ds@1952: pCtx->LocalToString(params[1], ®ex);
ds@1952:
ds@1952: RegEx *x = new RegEx();
ds@1952:
ds@1952: if (x->Compile(regex, params[2]) == 0)
ds@1952: {
ds@1952: cell_t *eOff;
ds@1952: pCtx->LocalToPhysAddr(params[5], &eOff);
ds@1952: const char *err = x->mError;
ds@1952: *eOff = x->mErrorOffset;
ds@1952: pCtx->StringToLocal(params[3], params[4], err ? err:"unknown");
ds@1952: return 0;
ds@1952: }
ds@1952:
ds@1952: return g_pHandleSys->CreateHandle(g_RegexHandle, (void*)x, pCtx->GetIdentity(), myself->GetIdentity(), NULL);
ds@1952: }
ds@1952:
ds@1952:
ds@1952: static cell_t MatchRegex(IPluginContext *pCtx, const cell_t *params)
ds@1952: {
ds@1952: Handle_t hndl = static_cast(params[1]);
ds@1952: HandleError err;
ds@1952: HandleSecurity sec;
ds@1952: sec.pOwner = NULL;
ds@1952: sec.pIdentity = myself->GetIdentity();
ds@1952:
ds@1952: RegEx *x;
ds@1952:
ds@1952: if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
ds@1952: {
ds@1952: return pCtx->ThrowNativeError("Invalid file handle %x (error %d)", hndl, err);
ds@1952: }
ds@1952:
ds@1952: if (!x)
ds@1952: {
ds@1952: pCtx->ThrowNativeError("Regex data not found\n");
ds@1952:
ds@1952: return 0;
ds@1952: }
ds@1952:
ds@1952: char *str;
ds@1952: pCtx->LocalToString(params[2], &str);
ds@1952:
ds@1952: int e = x->Match(str);
ds@1952:
ds@1952: if (e == -1)
ds@1952: {
ds@1952: /* there was a match error. move on. */
ds@1952: cell_t *res;
ds@1952: pCtx->LocalToPhysAddr(params[3], &res);
ds@1952: *res = x->mErrorOffset;
ds@1952: /* only clear the match results, since the regex object
ds@1952: may still be referenced later */
ds@1952: x->ClearMatch();
ds@1952:
ds@1952: return -1;
ds@1952: }
ds@1952: else if (e == 0)
ds@1952: {
ds@1952: /* only clear the match results, since the regex object
ds@1952: may still be referenced later */
ds@1952: x->ClearMatch();
ds@1952:
ds@1952: return 0;
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: return x->mSubStrings;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: static cell_t GetRegexSubString(IPluginContext *pCtx, const cell_t *params)
ds@1952: {
ds@1952: Handle_t hndl=static_cast(params[1]);
ds@1952: HandleError err;
ds@1952: HandleSecurity sec;
ds@1952: sec.pOwner=NULL;
ds@1952: sec.pIdentity=myself->GetIdentity();
ds@1952:
ds@1952: RegEx *x;
ds@1952:
ds@1952: if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
ds@1952: {
ds@1952: return pCtx->ThrowNativeError("Invalid file handle %x (error %d)", hndl, err);
ds@1952: }
ds@1952:
ds@1952: if (!x)
ds@1952: {
ds@1952: pCtx->ThrowNativeError("Regex data not found\n");
ds@1952: return 0;
ds@1952: }
ds@1952:
ds@1952: static char buffer[4096];
ds@1952: const char *ret=x->GetSubstring(params[2], buffer, sizeof(buffer));
ds@1952:
ds@1952: if(!ret)
ds@1952: {
ds@1952: return 0;
ds@1952: }
ds@1952:
ds@1952: pCtx->StringToLocalUTF8(params[3], params[4], ret, NULL);
ds@1952:
ds@1952: return 1;
ds@1952: }
ds@1952:
ds@1952: void RegexHandler::OnHandleDestroy(HandleType_t type, void *object)
ds@1952: {
ds@1952: RegEx *x = (RegEx *)object;
ds@1952:
ds@1952: x->Clear();
ds@1952: delete x;
ds@1952: }
ds@1952:
ds@1952: const sp_nativeinfo_t regex_natives[] =
ds@1952: {
ds@1952: {"GetRegexSubString", GetRegexSubString},
ds@1952: {"MatchRegex", MatchRegex},
ds@1952: {"CompileRegex", CompileRegex},
ds@1952: {NULL, NULL},
ds@1952: };