/[pcre]/code/trunk/pcreposix.c
ViewVC logotype

Diff of /code/trunk/pcreposix.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3 by nigel, Sat Feb 24 21:38:01 2007 UTC revision 25 by nigel, Sat Feb 24 21:38:45 2007 UTC
# Line 12  functions. Line 12  functions.
12    
13  Written by: Philip Hazel <ph10@cam.ac.uk>  Written by: Philip Hazel <ph10@cam.ac.uk>
14    
15             Copyright (c) 1997 University of Cambridge             Copyright (c) 1998 University of Cambridge
16    
17  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
18  Permission is granted to anyone to use this software for any purpose on any  Permission is granted to anyone to use this software for any purpose on any
# Line 39  restrictions: Line 39  restrictions:
39    
40  /* Corresponding tables of PCRE error messages and POSIX error codes. */  /* Corresponding tables of PCRE error messages and POSIX error codes. */
41    
42  static char *estring[] = {  static const char *estring[] = {
43    ERR1,  ERR2,  ERR3,  ERR4,  ERR5,  ERR6,  ERR7,  ERR8,  ERR9,  ERR10,    ERR1,  ERR2,  ERR3,  ERR4,  ERR5,  ERR6,  ERR7,  ERR8,  ERR9,  ERR10,
44    ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20,    ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20,
45    ERR21, ERR22, ERR23 };    ERR21, ERR22, ERR23, ERR24, ERR25 };
46    
47  static int eint[] = {  static int eint[] = {
48    REG_EESCAPE, /* "\\ at end of pattern" */    REG_EESCAPE, /* "\\ at end of pattern" */
# Line 67  static int eint[] = { Line 67  static int eint[] = {
67    REG_ESIZE,   /* "regular expression too large" */    REG_ESIZE,   /* "regular expression too large" */
68    REG_ESPACE,  /* "failed to get memory" */    REG_ESPACE,  /* "failed to get memory" */
69    REG_EPAREN,  /* "unmatched brackets" */    REG_EPAREN,  /* "unmatched brackets" */
70    REG_ASSERT   /* "internal error: code overflow" */    REG_ASSERT,  /* "internal error: code overflow" */
71      REG_BADPAT,  /* "unrecognized character after (?<" */
72      REG_BADPAT,  /* "lookbehind assertion is not fixed length" */
73      REG_BADPAT,  /* "malformed number after (?(" */
74      REG_BADPAT,  /* "conditional group containe more than two branches" */
75      REG_BADPAT   /* "assertion expected after (?(" */
76  };  };
77    
78  /* Table of texts corresponding to POSIX error codes */  /* Table of texts corresponding to POSIX error codes */
79    
80  static char *pstring[] = {  static const char *pstring[] = {
81    "",                                /* Dummy for value 0 */    "",                                /* Dummy for value 0 */
82    "internal error",                  /* REG_ASSERT */    "internal error",                  /* REG_ASSERT */
83    "invalid repeat counts in {}",     /* BADBR      */    "invalid repeat counts in {}",     /* BADBR      */
# Line 106  look them up in a table to turn them int Line 111  look them up in a table to turn them int
111  static int  static int
112  pcre_posix_error_code(const char *s)  pcre_posix_error_code(const char *s)
113  {  {
114  int i;  size_t i;
115  for (i = 0; i < sizeof(estring)/sizeof(char *); i++)  for (i = 0; i < sizeof(estring)/sizeof(char *); i++)
116    if (strcmp(s, estring[i]) == 0) return eint[i];    if (strcmp(s, estring[i]) == 0) return eint[i];
117  return REG_ASSERT;  return REG_ASSERT;
# Line 121  return REG_ASSERT; Line 126  return REG_ASSERT;
126  size_t  size_t
127  regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)  regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
128  {  {
129  char *message, *addmessage;  const char *message, *addmessage;
130  int length, adlength;  size_t length, addlength;
131    
132  message = (errcode >= sizeof(pstring)/sizeof(char *))?  message = (errcode >= (int)(sizeof(pstring)/sizeof(char *)))?
133    "unknown error code" : pstring[errcode];    "unknown error code" : pstring[errcode];
134    length = strlen(message) + 1;
135    
136  length = (int)strlen(message) + 1;  addmessage = " at offset ";
137    addlength = (preg != NULL && (int)preg->re_erroffset != -1)?
138  if (preg != NULL && (int)preg->re_erroffset != -1)    strlen(addmessage) + 6 : 0;
   {  
   addmessage = " at offset ";  
   adlength = (int)strlen(addmessage) + 6;  
   }  
 else adlength = 0;  
139    
140  if (errbuf_size > 0)  if (errbuf_size > 0)
141    {    {
142    if (adlength > 0 && errbuf_size >= length + adlength)    if (addlength > 0 && errbuf_size >= length + addlength)
143      sprintf(errbuf, "%s%s%-6d", message, addmessage, preg->re_erroffset);      sprintf(errbuf, "%s%s%-6d", message, addmessage, (int)preg->re_erroffset);
144    else    else
145      {      {
146      strncpy(errbuf, message, errbuf_size - 1);      strncpy(errbuf, message, errbuf_size - 1);
# Line 147  if (errbuf_size > 0) Line 148  if (errbuf_size > 0)
148      }      }
149    }    }
150    
151  return length + adlength;  return length + addlength;
152  }  }
153    
154    
# Line 183  Returns:      0 on success Line 184  Returns:      0 on success
184  int  int
185  regcomp(regex_t *preg, const char *pattern, int cflags)  regcomp(regex_t *preg, const char *pattern, int cflags)
186  {  {
187  char *errorptr;  const char *errorptr;
188  int erroffset;  int erroffset;
189  int options = 0;  int options = 0;
190    
191  if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS;  if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS;
192  if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE;  if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE;
193    
194  preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset);  preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset, NULL);
195  preg->re_erroffset = erroffset;  preg->re_erroffset = erroffset;
196    
197  if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr);  if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr);
# Line 216  int options = 0; Line 217  int options = 0;
217  if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL;  if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL;
218  if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL;  if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL;
219    
220  preg->re_erroffset = -1;   /* Only has meaning after compile */  preg->re_erroffset = (size_t)(-1);   /* Only has meaning after compile */
221    
222  rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), options,  rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), options,
223    (int *)pmatch, nmatch * 2);    (int *)pmatch, nmatch * 2);
# Line 225  if (rc == 0) return 0;    /* All pmatch Line 226  if (rc == 0) return 0;    /* All pmatch
226    
227  if (rc > 0)  if (rc > 0)
228    {    {
229    int i;    size_t i;
230    for (i = rc; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;    for (i = rc; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
231    return 0;    return 0;
232    }    }
# Line 233  if (rc > 0) Line 234  if (rc > 0)
234  else switch(rc)  else switch(rc)
235    {    {
236    case PCRE_ERROR_NOMATCH: return REG_NOMATCH;    case PCRE_ERROR_NOMATCH: return REG_NOMATCH;
   case PCRE_ERROR_BADREF: return REG_ESUBREG;  
237    case PCRE_ERROR_NULL: return REG_INVARG;    case PCRE_ERROR_NULL: return REG_INVARG;
238    case PCRE_ERROR_BADOPTION: return REG_INVARG;    case PCRE_ERROR_BADOPTION: return REG_INVARG;
239    case PCRE_ERROR_BADMAGIC: return REG_INVARG;    case PCRE_ERROR_BADMAGIC: return REG_INVARG;

Legend:
Removed from v.3  
changed lines
  Added in v.25

  ViewVC Help
Powered by ViewVC 1.1.5