summaryrefslogtreecommitdiff
path: root/src/gnu/lgnu.c
blob: 0cc4b8c1fc2fd1aa57fdec70da23b73b531bae7b (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* lgnu.c - Lua binding of GNU regular expressions library */
/* See Copyright Notice in the file LICENSE */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "lua.h"
#include "lauxlib.h"
#include "../common.h"

#define __USE_GNU
#ifndef REX_GNU_INCLUDE
#  include <regex.h>
#else
#  include REX_GNU_INCLUDE
#endif

/* These 2 settings may be redefined from the command-line or the makefile.
 * They should be kept in sync between themselves and with the target name.
 */
#ifndef REX_LIBNAME
#  define REX_LIBNAME "rex_gnu"
#endif
#ifndef REX_OPENLIB
#  define REX_OPENLIB luaopen_rex_gnu
#endif

#define REX_TYPENAME REX_LIBNAME"_regex"

#define ALG_CFLAGS_DFLT 0
#define ALG_EFLAGS_DFLT 0

#define ALG_GETCFLAGS(L,pos)  ALG_CFLAGS_DFLT

static void opttranslate (TArgComp *argC, lua_State *L, int pos);
#define ALG_OPTTRANSLATE(a,b,c)  opttranslate(a,b,c)

static void optsyntax (TArgComp *argC, lua_State *L, int pos);
#define ALG_OPTSYNTAX(a,b,c)  optsyntax(a,b,c)

#define ALG_NOMATCH(res)   ((res) == -1 || (res) == -2)
#define ALG_ISMATCH(res)   ((res) >= 0)
#define ALG_SUBBEG(ud,n)   ud->match.start[n]
#define ALG_SUBEND(ud,n)   ud->match.end[n]
#define ALG_SUBLEN(ud,n)   (ALG_SUBEND(ud,n) - ALG_SUBBEG(ud,n))
#define ALG_SUBVALID(ud,n) (ALG_SUBBEG(ud,n) >= 0)
#define ALG_NSUB(ud)     ((int)ud->r.re_nsub)

#define ALG_PUSHSUB(L,ud,text,n) \
  lua_pushlstring (L, (text) + ALG_SUBBEG(ud,n), ALG_SUBLEN(ud,n))

#define ALG_PUSHSUB_OR_FALSE(L,ud,text,n) \
  (ALG_SUBVALID(ud,n) ? ALG_PUSHSUB (L,ud,text,n) : lua_pushboolean (L,0))

#define ALG_PUSHSTART(L,ud,offs,n)   lua_pushinteger(L, (offs) + ALG_SUBBEG(ud,n) + 1)
#define ALG_PUSHEND(L,ud,offs,n)     lua_pushinteger(L, (offs) + ALG_SUBEND(ud,n))
#define ALG_PUSHOFFSETS(L,ud,offs,n) \
  (ALG_PUSHSTART(L,ud,offs,n), ALG_PUSHEND(L,ud,offs,n))

#define ALG_BASE(st)                  (st)

typedef struct {
  struct re_pattern_buffer r;
  struct re_registers      match;
  int                      freed;
  const char *             errmsg;
} TGnu;

#define TUserdata TGnu

#include "../algo.h"

/*  Functions
 ******************************************************************************
 */

/* Execution flags, which we need to simulate as GNU does not use flags for this. */
#define GNU_NOTBOL  1
#define GNU_NOTEOL  2
#define GNU_REVERSE 4

static int generate_error  (lua_State *L, const TUserdata *ud, int errcode) {
  const char *errmsg;
  switch (errcode) {
  case 0:
    errmsg = ud->errmsg;
    break;
  case -1:
    errmsg = "no match";
    break;
  case -2:
    errmsg = "internal error in GNU regex";
    break;
  default:
    errmsg = "unknown error";
  }
  return luaL_error (L, "%s", errmsg);
}

#define ALG_TRANSLATE_SIZE (UCHAR_MAX + 1)
static void opttranslate (TArgComp *argC, lua_State *L, int pos) {
  if (!lua_isnoneornil (L, pos)) {
    unsigned i;

    argC->translate = (const unsigned char *) Lmalloc (L, ALG_TRANSLATE_SIZE);
    memset ((unsigned char *) argC->translate, 0, ALG_TRANSLATE_SIZE); /* initialize all members to 0 */
    for (i = 0; i <= UCHAR_MAX; i++) {
      lua_pushinteger (L, i);
      lua_gettable (L, pos);
      if (lua_tostring (L, -1))
        ((unsigned char *) argC->translate)[i] = *lua_tostring (L, -1);
      lua_pop (L, 1);
    }
  } else
    argC->translate = NULL;
}

typedef struct {
  const char * name;
  int value;
} EncPair;

/* ATTENTION:
   This array must always be kept alphabetically sorted, as it's used in the
   binary search, so take care when manually inserting new elements.
 */
static EncPair Syntaxes[] = {
  { "AWK",                    RE_SYNTAX_AWK },
  { "ED",                     RE_SYNTAX_ED },
  { "EGREP",                  RE_SYNTAX_EGREP },
  { "EMACS",                  RE_SYNTAX_EMACS },
  { "GNU_AWK",                RE_SYNTAX_GNU_AWK },
  { "GREP",                   RE_SYNTAX_GREP },
  { "POSIX_AWK",              RE_SYNTAX_POSIX_AWK },
  { "POSIX_BASIC",            RE_SYNTAX_POSIX_BASIC },
  { "POSIX_EGREP",            RE_SYNTAX_POSIX_EGREP },
  { "POSIX_EXTENDED",         RE_SYNTAX_POSIX_EXTENDED },
  { "POSIX_MINIMAL_BASIC",    RE_SYNTAX_POSIX_MINIMAL_BASIC },
  { "POSIX_MINIMAL_EXTENDED", RE_SYNTAX_POSIX_MINIMAL_EXTENDED },
  { "SED",                    RE_SYNTAX_SED },
};

static int fcmp (const void *p1, const void *p2) {
  return strcmp (((EncPair*) p1)->name, ((EncPair*) p2)->name);
}

static int getsyntax (lua_State *L, int pos) {
  EncPair key, *found;
  if ((key.name = luaL_optstring (L, pos, NULL)) == NULL)
    return -1;
  found = (EncPair*) bsearch (&key, Syntaxes, sizeof (Syntaxes) / sizeof (EncPair),
          sizeof (EncPair), fcmp);
  if (found == NULL)
    luaL_argerror (L, pos, "invalid or unsupported syntax string");
  return found->value;
}

static void optsyntax (TArgComp *argC, lua_State *L, int pos) {
  argC->gnusyn = getsyntax (L, pos);
}

static void seteflags (TGnu *ud, TArgExec *argE) {
  ud->r.not_bol = (argE->eflags & GNU_NOTBOL) != 0;
  ud->r.not_eol = (argE->eflags & GNU_NOTEOL) != 0;
}

/*
   rex.setsyntax (syntax)
   @param syntax: one of the predefined strings listed in array 'Syntaxes'
   @return: nothing
*/
static int LGnu_setsyntax (lua_State *L) {
  (void) luaL_checkstring (L, 1);
  re_set_syntax (getsyntax (L, 1));
  return 0;
}

static int compile_regex (lua_State *L, const TArgComp *argC, TGnu **pud) {
  const char *res;
  TGnu *ud;
  reg_syntax_t old_syntax;
  int ret;

  ud = (TGnu *)lua_newuserdata (L, sizeof (TGnu));
  memset (ud, 0, sizeof (TGnu));          /* initialize all members to 0 */

  if (argC->gnusyn >= 0)
    old_syntax = re_set_syntax (argC->gnusyn);

  /* translate table is never written to, so this cast is safe */
  ud->r.translate = (unsigned char *) argC->translate;

  res = re_compile_pattern (argC->pattern, argC->patlen, &ud->r);
  if (res != NULL) {
      ud->errmsg = res;
      ret = generate_error (L, ud, 0);
  } else {
    if (argC->cflags & REG_NOSUB)
      ud->r.no_sub = 1;

    lua_pushvalue (L, LUA_ENVIRONINDEX);
    lua_setmetatable (L, -2);

    if (pud) *pud = ud;
    ret = 1;
  }

  if (argC->gnusyn >= 0)
    re_set_syntax (old_syntax);
  return ret;
}

static int gmatch_exec (TUserdata *ud, TArgExec *argE) {
  seteflags (ud, argE);
  if (argE->startoffset > 0)
    ud->r.not_bol = 1;
  argE->text += argE->startoffset;
  argE->textlen -= argE->startoffset;
  if (argE->eflags & GNU_REVERSE)
    return re_search (&ud->r, argE->text, argE->textlen, argE->textlen, -argE->textlen, &ud->match);
  else
    return re_search (&ud->r, argE->text, argE->textlen, 0, argE->textlen, &ud->match);
}

static void gmatch_pushsubject (lua_State *L, TArgExec *argE) {
  lua_pushlstring (L, argE->text, argE->textlen);
}

static int findmatch_exec (TGnu *ud, TArgExec *argE) {
  argE->text += argE->startoffset;
  argE->textlen -= argE->startoffset;
  seteflags (ud, argE);
  if (argE->eflags & GNU_REVERSE)
    return re_search (&ud->r, argE->text, argE->textlen, argE->textlen, -argE->textlen, &ud->match);
  else
    return re_search (&ud->r, argE->text, argE->textlen, 0, argE->textlen, &ud->match);
}

static int gsub_exec (TGnu *ud, TArgExec *argE, int st) {
  seteflags (ud, argE);
  if (st > 0)
    ud->r.not_bol = 1;
  if (argE->eflags & GNU_REVERSE)
    return re_search (&ud->r, argE->text + st, argE->textlen - st, argE->textlen - st, -(argE->textlen - st), &ud->match);
  else
    return re_search (&ud->r, argE->text + st, argE->textlen - st, 0, argE->textlen - st, &ud->match);
}

static int split_exec (TGnu *ud, TArgExec *argE, int offset) {
  seteflags (ud, argE);
  if (offset > 0)
    ud->r.not_bol = 1;
  if (argE->eflags & GNU_REVERSE)
    return re_search (&ud->r, argE->text + offset, argE->textlen - offset, argE->textlen - offset, -(argE->textlen - offset), &ud->match);
  else
    return re_search (&ud->r, argE->text + offset, argE->textlen - offset, 0, argE->textlen - offset, &ud->match);
}

static int Gnu_gc (lua_State *L) {
  TGnu *ud = check_ud (L);
  if (ud->freed == 0) {           /* precaution against "manual" __gc calling */
    ud->freed = 1;
    if (ud->r.regs_allocated != REGS_UNALLOCATED) {
      free (ud->match.start);
      free (ud->match.end);
    }
  }
  return 0;
}

static int Gnu_tostring (lua_State *L) {
  TGnu *ud = check_ud (L);
  if (ud->freed == 0)
    lua_pushfstring (L, "%s (%p)", REX_TYPENAME, (void*)ud);
  else
    lua_pushfstring (L, "%s (deleted)", REX_TYPENAME);
  return 1;
}

static flag_pair gnu_flags[] =
{
  { "not_bol", GNU_NOTBOL },
  { "not_eol", GNU_NOTEOL },
  { "reverse", GNU_REVERSE },
/*---------------------------------------------------------------------------*/
  { NULL, 0 }
};

static int Gnu_get_flags (lua_State *L) {
  const flag_pair* fps[] = { gnu_flags, NULL };
  return get_flags (L, fps);
}

static const luaL_reg gnumeta[] = {
  { "exec",       ud_exec },
  { "tfind",      ud_tfind },    /* old match */
  { "find",       ud_find },
  { "match",      ud_match },
  { "__gc",       Gnu_gc },
  { "__tostring", Gnu_tostring },
  { NULL, NULL}
};

static const luaL_reg rexlib[] = {
  { "match",      match },
  { "find",       find },
  { "gmatch",     gmatch },
  { "gsub",       gsub },
  { "split",      split },
  { "new",        ud_new },
  { "flags",      Gnu_get_flags },
  { "plainfind",  plainfind_func },
  { "setsyntax",  LGnu_setsyntax },
  { NULL, NULL }
};

/* Open the library */
REX_API int REX_OPENLIB (lua_State *L)
{
  re_set_syntax (RE_SYNTAX_POSIX_EXTENDED);

  /* create a new function environment to serve as a metatable for methods */
  lua_newtable (L);
  lua_pushvalue (L, -1);
  lua_replace (L, LUA_ENVIRONINDEX);
  lua_pushvalue(L, -1); /* mt.__index = mt */
  lua_setfield(L, -2, "__index");
  luaL_register (L, NULL, gnumeta);

  /* register functions */
  luaL_register (L, REX_LIBNAME, rexlib);
  lua_pushliteral (L, REX_VERSION" (for GNU regexes)");
  lua_setfield (L, -2, "_VERSION");
  return 1;
}