"Fossies" - the Fresh Open Source Software Archive

Member "imapfilter-2.8.2/src/pcre.c" (26 Dec 2023, 3231 Bytes) of package /linux/privat/imapfilter-2.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pcre.c" see the Fossies "Dox" file reference documentation.

    1 #include <stdio.h>
    2 #include <string.h>
    3 
    4 #include <lua.h>
    5 #include <lauxlib.h>
    6 #include <lualib.h>
    7 
    8 #define PCRE2_CODE_UNIT_WIDTH 8
    9 
   10 #include <pcre2.h>
   11 
   12 #include "imapfilter.h"
   13 
   14 
   15 static int ifre_compile(lua_State *lua);
   16 static int ifre_exec(lua_State *lua);
   17 static int ifre_free(lua_State *lua);
   18 
   19 /* Lua imapfilter library of PCRE related functions. */
   20 static const luaL_Reg ifrelib[] = {
   21     { "compile", ifre_compile },
   22     { "exec", ifre_exec },
   23     { "free", ifre_free },
   24     { NULL, NULL }
   25 };
   26 
   27 
   28 /*
   29  * Lua implementation of the PCRE compile function.
   30  */
   31 static int
   32 ifre_compile(lua_State *lua)
   33 {
   34     pcre2_code **re;
   35     PCRE2_SPTR pattern;
   36     int errornumber;
   37     PCRE2_SIZE erroroffset;
   38 
   39     if (lua_gettop(lua) != 1)
   40         luaL_error(lua, "wrong number of arguments");
   41     luaL_checktype(lua, 1, LUA_TSTRING);
   42 
   43     pattern = (PCRE2_SPTR)lua_tostring(lua, 1);
   44 
   45 #if LUA_VERSION_NUM < 504
   46     re = (pcre2_code **)(lua_newuserdata(lua, sizeof(pcre2_code *)));
   47 #else
   48     re = (pcre2_code **)(lua_newuserdatauv(lua, sizeof(pcre2_code *), 1));
   49 #endif
   50 
   51     *re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &errornumber,
   52         &erroroffset, NULL);
   53 
   54     if (*re == NULL) {
   55         PCRE2_UCHAR buffer[256];
   56         pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
   57         fprintf(stderr, "RE failed at offset %d: %s\n",
   58             (int)erroroffset, buffer);
   59         lua_pop(lua, 1);
   60     }
   61 
   62     lua_remove(lua, 1);
   63 
   64     lua_pushboolean(lua, (*re != NULL));
   65     lua_insert(lua, 1);
   66     
   67     return (*re != NULL ? 2 : 1);
   68 }
   69 
   70 
   71 /*
   72  * Lua implementation of the PCRE exec function.
   73  */
   74 static int
   75 ifre_exec(lua_State *lua)
   76 {
   77     int i, rc;
   78     pcre2_code *re;
   79     pcre2_match_data *match_data;
   80     PCRE2_SPTR subject;
   81     size_t subject_length;
   82     PCRE2_SIZE *ovector;
   83 
   84     if (lua_gettop(lua) != 2)
   85         luaL_error(lua, "wrong number of arguments");
   86     luaL_checktype(lua, 1, LUA_TUSERDATA);
   87     luaL_checktype(lua, 2, LUA_TSTRING);
   88 
   89     re = *(pcre2_code **)(lua_touserdata(lua, 1));
   90     subject = (PCRE2_SPTR)lua_tostring(lua, 2);
   91     subject_length = strlen((char *)subject);
   92 
   93     match_data = pcre2_match_data_create_from_pattern(re, NULL);
   94 
   95     rc = pcre2_match(re, subject, subject_length, 0, 0, match_data, NULL);
   96 
   97     if (rc > 0) {
   98         ovector = pcre2_get_ovector_pointer(match_data);
   99         for (i = 0; i < rc; i++) {
  100             if (ovector[2 * i] != PCRE2_UNSET &&
  101                 ovector[2 * i + 1] != PCRE2_UNSET) {
  102                 PCRE2_SPTR start = subject + ovector[2 * i];
  103                 size_t len = ovector[2 * i + 1] - ovector[2 * i];
  104                 lua_pushlstring(lua, (const char *)start, len);
  105             } else {
  106                 lua_pushnil(lua);
  107             }
  108         }
  109     }
  110 
  111     pcre2_match_data_free(match_data);
  112 
  113     lua_remove(lua, 1);
  114     lua_remove(lua, 1);
  115 
  116     lua_pushboolean(lua, (rc > 0));
  117     lua_insert(lua, 1);
  118 
  119     return (rc > 0 ? rc : 1);
  120 }
  121 
  122 
  123 /*
  124  * Lua implementation of a PCRE free function.
  125  */
  126 static int
  127 ifre_free(lua_State *lua)
  128 {
  129     pcre2_code *re;
  130 
  131     if (lua_gettop(lua) != 1)
  132         luaL_error(lua, "wrong number of arguments");
  133     luaL_checktype(lua, 1, LUA_TUSERDATA);
  134 
  135     re = *(pcre2_code **)(lua_touserdata(lua, 1));
  136 
  137     pcre2_code_free(re);
  138 
  139     lua_remove(lua, 1);
  140 
  141     return 0;
  142 }
  143 
  144 
  145 /*
  146  * Open imapfilter library of PCRE related functions.
  147  */
  148 LUALIB_API int
  149 luaopen_ifre(lua_State *lua)
  150 {
  151 
  152 #if LUA_VERSION_NUM < 502
  153     luaL_register(lua, "ifre", ifrelib);
  154 #else
  155     luaL_newlib(lua, ifrelib);
  156     lua_setglobal(lua, "ifre");
  157 #endif
  158 
  159     return 1;
  160 }