Back to index

php5  5.3.10
Functions | Variables
pcreposix.c File Reference
#include "config.h"
#include "pcre.h"
#include "pcre_internal.h"
#include "pcreposix.h"

Go to the source code of this file.

Functions

PCREPOSIX_EXP_DEFN size_t
PCRE_CALL_CONVENTION 
regerror (int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
PCREPOSIX_EXP_DEFN void
PCRE_CALL_CONVENTION 
regfree (regex_t *preg)
PCREPOSIX_EXP_DEFN int
PCRE_CALL_CONVENTION 
regcomp (regex_t *preg, const char *pattern, int cflags)
PCREPOSIX_EXP_DEFN int
PCRE_CALL_CONVENTION 
regexec (const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags)

Variables

static const int eint []
static const char *const pstring []

Function Documentation

PCREPOSIX_EXP_DEFN int PCRE_CALL_CONVENTION regcomp ( regex_t preg,
const char *  pattern,
int  cflags 
)

Definition at line 244 of file pcreposix.c.

{
const char *errorptr;
int erroffset;
int errorcode;
int options = 0;

if ((cflags & REG_ICASE) != 0)    options |= PCRE_CASELESS;
if ((cflags & REG_NEWLINE) != 0)  options |= PCRE_MULTILINE;
if ((cflags & REG_DOTALL) != 0)   options |= PCRE_DOTALL;
if ((cflags & REG_NOSUB) != 0)    options |= PCRE_NO_AUTO_CAPTURE;
if ((cflags & REG_UTF8) != 0)     options |= PCRE_UTF8;
if ((cflags & REG_UCP) != 0)      options |= PCRE_UCP;
if ((cflags & REG_UNGREEDY) != 0) options |= PCRE_UNGREEDY;

preg->re_pcre = pcre_compile2(pattern, options, &errorcode, &errorptr,
  &erroffset, NULL);
preg->re_erroffset = erroffset;

/* Safety: if the error code is too big for the translation vector (which
should not happen, but we all make mistakes), return REG_BADPAT. */

if (preg->re_pcre == NULL)
  {
  return (errorcode < sizeof(eint)/sizeof(const int))?
    eint[errorcode] : REG_BADPAT;
  }

preg->re_nsub = pcre_info((const pcre *)preg->re_pcre, NULL, NULL);
return 0;
}

Here is the call graph for this function:

PCREPOSIX_EXP_DEFN size_t PCRE_CALL_CONVENTION regerror ( int  errcode,
const regex_t preg,
char *  errbuf,
size_t  errbuf_size 
)

Definition at line 186 of file pcreposix.c.

{
const char *message, *addmessage;
size_t length, addlength;

message = (errcode >= (int)(sizeof(pstring)/sizeof(char *)))?
  "unknown error code" : pstring[errcode];
length = strlen(message) + 1;

addmessage = " at offset ";
addlength = (preg != NULL && (int)preg->re_erroffset != -1)?
  strlen(addmessage) + 6 : 0;

if (errbuf_size > 0)
  {
  if (addlength > 0 && errbuf_size >= length + addlength)
    sprintf(errbuf, "%s%s%-6d", message, addmessage, (int)preg->re_erroffset);
  else
    {
    strncpy(errbuf, message, errbuf_size - 1);
    errbuf[errbuf_size-1] = 0;
    }
  }

return length + addlength;
}

Here is the call graph for this function:

PCREPOSIX_EXP_DEFN int PCRE_CALL_CONVENTION regexec ( const regex_t preg,
const char *  string,
size_t  nmatch,
regmatch_t  pmatch[],
int  eflags 
)

Definition at line 295 of file pcreposix.c.

{
int rc, so, eo;
int options = 0;
int *ovector = NULL;
int small_ovector[POSIX_MALLOC_THRESHOLD * 3];
BOOL allocated_ovector = FALSE;
BOOL nosub =
  (((const pcre *)preg->re_pcre)->options & PCRE_NO_AUTO_CAPTURE) != 0;

if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL;
if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL;
if ((eflags & REG_NOTEMPTY) != 0) options |= PCRE_NOTEMPTY;

((regex_t *)preg)->re_erroffset = (size_t)(-1);  /* Only has meaning after compile */

/* When no string data is being returned, or no vector has been passed in which
to put it, ensure that nmatch is zero. Otherwise, ensure the vector for holding
the return data is large enough. */

if (nosub || pmatch == NULL) nmatch = 0;

else if (nmatch > 0)
  {
  if (nmatch <= POSIX_MALLOC_THRESHOLD)
    {
    ovector = &(small_ovector[0]);
    }
  else
    {
    if (nmatch > INT_MAX/(sizeof(int) * 3)) return REG_ESPACE;
    ovector = (int *)malloc(sizeof(int) * nmatch * 3);
    if (ovector == NULL) return REG_ESPACE;
    allocated_ovector = TRUE;
    }
  }

/* REG_STARTEND is a BSD extension, to allow for non-NUL-terminated strings.
The man page from OS X says "REG_STARTEND affects only the location of the
string, not how it is matched". That is why the "so" value is used to bump the
start location rather than being passed as a PCRE "starting offset". */

if ((eflags & REG_STARTEND) != 0)
  {
  so = pmatch[0].rm_so;
  eo = pmatch[0].rm_eo;
  }
else
  {
  so = 0;
  eo = (int)strlen(string);
  }

rc = pcre_exec((const pcre *)preg->re_pcre, NULL, string + so, (eo - so),
  0, options, ovector, (int)(nmatch * 3));

if (rc == 0) rc = (int)nmatch;    /* All captured slots were filled in */

/* Successful match */

if (rc >= 0)
  {
  size_t i;
  if (!nosub)
    {
    for (i = 0; i < (size_t)rc; i++)
      {
      pmatch[i].rm_so = ovector[i*2];
      pmatch[i].rm_eo = ovector[i*2+1];
      }
    if (allocated_ovector) free(ovector);
    for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
    }
  return 0;
  }

/* Unsuccessful match */

if (allocated_ovector) free(ovector);
switch(rc)
  {
/* ========================================================================== */
  /* These cases are never obeyed. This is a fudge that causes a compile-time
  error if the vector eint, which is indexed by compile-time error number, is
  not the correct length. It seems to be the only way to do such a check at
  compile time, as the sizeof() operator does not work in the C preprocessor.
  As all the PCRE_ERROR_xxx values are negative, we can use 0 and 1. */

  case 0:
  case (sizeof(eint)/sizeof(int) == ERRCOUNT):
  return REG_ASSERT;
/* ========================================================================== */

  case PCRE_ERROR_NOMATCH: return REG_NOMATCH;
  case PCRE_ERROR_NULL: return REG_INVARG;
  case PCRE_ERROR_BADOPTION: return REG_INVARG;
  case PCRE_ERROR_BADMAGIC: return REG_INVARG;
  case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT;
  case PCRE_ERROR_NOMEMORY: return REG_ESPACE;
  case PCRE_ERROR_MATCHLIMIT: return REG_ESPACE;
  case PCRE_ERROR_BADUTF8: return REG_INVARG;
  case PCRE_ERROR_BADUTF8_OFFSET: return REG_INVARG;
  default: return REG_ASSERT;
  }
}

Here is the call graph for this function:

Definition at line 221 of file pcreposix.c.

{
(pcre_free)(preg->re_pcre);
}

Here is the call graph for this function:


Variable Documentation

const int eint[] [static]

Definition at line 70 of file pcreposix.c.

const char* const pstring[] [static]
Initial value:
 {
  "",                                
  "internal error",                  
  "invalid repeat counts in {}",     
  "pattern error",                   
  "? * + invalid",                   
  "unbalanced {}",                   
  "unbalanced []",                   
  "collation error - not relevant",  
  "bad class",                       
  "bad escape sequence",             
  "empty expression",                
  "unbalanced ()",                   
  "bad range inside []",             
  "expression too big",              
  "failed to get memory",            
  "bad back reference",              
  "bad argument",                    
  "match failed"                     
}

Definition at line 157 of file pcreposix.c.