Back to index

php5  5.3.10
regposix.c
Go to the documentation of this file.
00001 /**********************************************************************
00002   regposix.c - Oniguruma (regular expression library)
00003 **********************************************************************/
00004 /*-
00005  * Copyright (c) 2002-2006  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #define regex_t   onig_regex_t
00031 #include "regint.h"
00032 #undef regex_t
00033 #include "onigposix.h"
00034 
00035 #define ONIG_C(reg)    ((onig_regex_t* )((reg)->onig))
00036 #define PONIG_C(reg)   ((onig_regex_t** )(&(reg)->onig))
00037 
00038 /* #define ENC_STRING_LEN(enc,s,len)    len = strlen(s) */
00039 #define ENC_STRING_LEN(enc,s,len) do { \
00040   if (ONIGENC_MBC_MINLEN(enc) == 1) { \
00041     UChar* tmps = (UChar* )(s); \
00042     while (*tmps != 0) tmps++; \
00043     len = tmps - (UChar* )(s); \
00044   } \
00045   else { \
00046     len = onigenc_str_bytelen_null(enc, (UChar* )s); \
00047   } \
00048 } while(0)
00049 
00050 typedef struct {
00051   int onig_err;
00052   int posix_err;
00053 } O2PERR;
00054 
00055 static int
00056 onig2posix_error_code(int code)
00057 {
00058   static const O2PERR o2p[] = {
00059     { ONIG_MISMATCH,                                      REG_NOMATCH },
00060     { ONIG_NO_SUPPORT_CONFIG,                             REG_EONIG_INTERNAL },
00061     { ONIGERR_MEMORY,                                     REG_ESPACE  },
00062     { ONIGERR_MATCH_STACK_LIMIT_OVER,                     REG_EONIG_INTERNAL },
00063     { ONIGERR_TYPE_BUG,                                   REG_EONIG_INTERNAL },
00064     { ONIGERR_PARSER_BUG,                                 REG_EONIG_INTERNAL },
00065     { ONIGERR_STACK_BUG,                                  REG_EONIG_INTERNAL },
00066     { ONIGERR_UNDEFINED_BYTECODE,                         REG_EONIG_INTERNAL },
00067     { ONIGERR_UNEXPECTED_BYTECODE,                        REG_EONIG_INTERNAL },
00068     { ONIGERR_DEFAULT_ENCODING_IS_NOT_SETTED,             REG_EONIG_BADARG },
00069     { ONIGERR_SPECIFIED_ENCODING_CANT_CONVERT_TO_WIDE_CHAR, REG_EONIG_BADARG },
00070     { ONIGERR_INVALID_ARGUMENT,                           REG_EONIG_BADARG },
00071     { ONIGERR_END_PATTERN_AT_LEFT_BRACE,                  REG_EBRACE  },
00072     { ONIGERR_END_PATTERN_AT_LEFT_BRACKET,                REG_EBRACK  },
00073     { ONIGERR_EMPTY_CHAR_CLASS,                           REG_ECTYPE  },
00074     { ONIGERR_PREMATURE_END_OF_CHAR_CLASS,                REG_ECTYPE  },
00075     { ONIGERR_END_PATTERN_AT_ESCAPE,                      REG_EESCAPE },
00076     { ONIGERR_END_PATTERN_AT_META,                        REG_EESCAPE },
00077     { ONIGERR_END_PATTERN_AT_CONTROL,                     REG_EESCAPE },
00078     { ONIGERR_META_CODE_SYNTAX,                           REG_BADPAT  },
00079     { ONIGERR_CONTROL_CODE_SYNTAX,                        REG_BADPAT  },
00080     { ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE,           REG_ECTYPE  },
00081     { ONIGERR_CHAR_CLASS_VALUE_AT_START_OF_RANGE,         REG_ECTYPE  },
00082     { ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS,    REG_ECTYPE  },
00083     { ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED,    REG_BADRPT  },
00084     { ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID,          REG_BADRPT  },
00085     { ONIGERR_NESTED_REPEAT_OPERATOR,                     REG_BADRPT  },
00086     { ONIGERR_UNMATCHED_CLOSE_PARENTHESIS,                REG_EPAREN  },
00087     { ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS,     REG_EPAREN  },
00088     { ONIGERR_END_PATTERN_IN_GROUP,                       REG_BADPAT  },
00089     { ONIGERR_UNDEFINED_GROUP_OPTION,                     REG_BADPAT  },
00090     { ONIGERR_INVALID_POSIX_BRACKET_TYPE,                 REG_BADPAT  },
00091     { ONIGERR_INVALID_LOOK_BEHIND_PATTERN,                REG_BADPAT  },
00092     { ONIGERR_INVALID_REPEAT_RANGE_PATTERN,               REG_BADPAT  },
00093     { ONIGERR_TOO_BIG_NUMBER,                             REG_BADPAT  },
00094     { ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE,            REG_BADBR   },
00095     { ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE,   REG_BADBR   },
00096     { ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS,                  REG_ECTYPE  },
00097     { ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE,        REG_ECTYPE  },
00098     { ONIGERR_TOO_MANY_MULTI_BYTE_RANGES,                 REG_ECTYPE  },
00099     { ONIGERR_TOO_SHORT_MULTI_BYTE_STRING,                REG_BADPAT  },
00100     { ONIGERR_TOO_BIG_BACKREF_NUMBER,                     REG_ESUBREG },
00101     { ONIGERR_INVALID_BACKREF,                            REG_ESUBREG },
00102     { ONIGERR_NUMBERED_BACKREF_OR_CALL_NOT_ALLOWED,       REG_BADPAT  },
00103     { ONIGERR_TOO_BIG_WIDE_CHAR_VALUE,                    REG_EONIG_BADWC },
00104     { ONIGERR_TOO_LONG_WIDE_CHAR_VALUE,                   REG_EONIG_BADWC },
00105     { ONIGERR_INVALID_WIDE_CHAR_VALUE,                    REG_EONIG_BADWC },
00106     { ONIGERR_EMPTY_GROUP_NAME,                           REG_BADPAT },
00107     { ONIGERR_INVALID_GROUP_NAME,                         REG_BADPAT },
00108     { ONIGERR_INVALID_CHAR_IN_GROUP_NAME,                 REG_BADPAT },
00109     { ONIGERR_UNDEFINED_NAME_REFERENCE,                   REG_BADPAT },
00110     { ONIGERR_UNDEFINED_GROUP_REFERENCE,                  REG_BADPAT },
00111     { ONIGERR_MULTIPLEX_DEFINED_NAME,                     REG_BADPAT },
00112     { ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL,             REG_BADPAT },
00113     { ONIGERR_NEVER_ENDING_RECURSION,                     REG_BADPAT },
00114     { ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY,      REG_BADPAT },
00115     { ONIGERR_INVALID_CHAR_PROPERTY_NAME,                 REG_BADPAT },
00116     { ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION,         REG_EONIG_BADARG },
00117     { ONIGERR_OVER_THREAD_PASS_LIMIT_COUNT,               REG_EONIG_THREAD }
00118 
00119   };
00120 
00121   int i;
00122 
00123   if (code >= 0) return 0;
00124 
00125   for (i = 0; i < sizeof(o2p) / sizeof(o2p[0]); i++) {
00126     if (code == o2p[i].onig_err)
00127       return o2p[i].posix_err;
00128   }
00129 
00130   return REG_EONIG_INTERNAL;  /* but, unknown error code */
00131 }
00132 
00133 extern int
00134 regcomp(regex_t* reg, const char* pattern, int posix_options)
00135 {
00136   int r, len;
00137   OnigSyntaxType* syntax = OnigDefaultSyntax;
00138   OnigOptionType options;
00139 
00140   if ((posix_options & REG_EXTENDED) == 0)
00141     syntax = ONIG_SYNTAX_POSIX_BASIC;
00142 
00143   options = syntax->options;
00144   if ((posix_options & REG_ICASE)   != 0)
00145     ONIG_OPTION_ON(options, ONIG_OPTION_IGNORECASE);
00146   if ((posix_options & REG_NEWLINE) != 0) {
00147     ONIG_OPTION_ON( options, ONIG_OPTION_NEGATE_SINGLELINE);
00148     ONIG_OPTION_OFF(options, ONIG_OPTION_SINGLELINE);
00149   }
00150 
00151   reg->comp_options = posix_options;
00152 
00153   ENC_STRING_LEN(OnigEncDefaultCharEncoding, pattern, len);
00154   r = onig_new(PONIG_C(reg), (UChar* )pattern, (UChar* )(pattern + len),
00155               options, OnigEncDefaultCharEncoding, syntax,
00156               (OnigErrorInfo* )NULL);
00157   if (r != ONIG_NORMAL) {
00158     return onig2posix_error_code(r);
00159   }
00160 
00161   reg->re_nsub = ONIG_C(reg)->num_mem;
00162   return 0;
00163 }
00164 
00165 extern int
00166 regexec(regex_t* reg, const char* str, size_t nmatch,
00167        regmatch_t pmatch[], int posix_options)
00168 {
00169   int r, i, len;
00170   UChar* end;
00171   regmatch_t* pm;
00172   OnigOptionType options;
00173 
00174   options = ONIG_OPTION_POSIX_REGION;
00175   if ((posix_options & REG_NOTBOL) != 0) options |= ONIG_OPTION_NOTBOL;
00176   if ((posix_options & REG_NOTEOL) != 0) options |= ONIG_OPTION_NOTEOL;
00177 
00178   if (nmatch == 0 || (reg->comp_options & REG_NOSUB) != 0) {
00179     pm = (regmatch_t* )NULL;
00180     nmatch = 0;
00181   }
00182   else if ((int )nmatch < ONIG_C(reg)->num_mem + 1) {
00183     pm = (regmatch_t* )xmalloc(sizeof(regmatch_t)
00184                                * (ONIG_C(reg)->num_mem + 1));
00185     if (pm == NULL)
00186       return REG_ESPACE;
00187   }
00188   else {
00189     pm = pmatch;
00190   }
00191 
00192   ENC_STRING_LEN(ONIG_C(reg)->enc, str, len);
00193   end = (UChar* )(str + len);
00194   r = onig_search(ONIG_C(reg), (UChar* )str, end, (UChar* )str, end,
00195                 (OnigRegion* )pm, options);
00196 
00197   if (r >= 0) {
00198     r = 0; /* Match */
00199     if (pm != pmatch && pm != NULL) {
00200       xmemcpy(pmatch, pm, sizeof(regmatch_t) * nmatch);
00201     }
00202   }
00203   else if (r == ONIG_MISMATCH) {
00204     r = REG_NOMATCH;
00205     for (i = 0; i < (int )nmatch; i++)
00206       pmatch[i].rm_so = pmatch[i].rm_eo = ONIG_REGION_NOTPOS;
00207   }
00208   else {
00209     r = onig2posix_error_code(r);
00210   }
00211 
00212   if (pm != pmatch && pm != NULL)
00213     xfree(pm);
00214 
00215 #if 0
00216   if (reg->re_nsub > nmatch - 1)
00217     reg->re_nsub = (nmatch <= 1 ? 0 : nmatch - 1);
00218 #endif
00219 
00220   return r;
00221 }
00222 
00223 extern void
00224 regfree(regex_t* reg)
00225 {
00226   onig_free(ONIG_C(reg));
00227 }
00228 
00229 
00230 extern void
00231 reg_set_encoding(int mb_code)
00232 {
00233   OnigEncoding enc;
00234 
00235   switch (mb_code) {
00236   case REG_POSIX_ENCODING_ASCII:
00237     enc = ONIG_ENCODING_ASCII;
00238     break;
00239   case REG_POSIX_ENCODING_EUC_JP:
00240     enc = ONIG_ENCODING_EUC_JP;
00241     break;
00242   case REG_POSIX_ENCODING_SJIS:
00243     enc = ONIG_ENCODING_SJIS;
00244     break;
00245   case REG_POSIX_ENCODING_UTF8:
00246     enc = ONIG_ENCODING_UTF8;
00247     break;
00248   case REG_POSIX_ENCODING_UTF16_BE:
00249     enc = ONIG_ENCODING_UTF16_BE;
00250     break;
00251   case REG_POSIX_ENCODING_UTF16_LE:
00252     enc = ONIG_ENCODING_UTF16_LE;
00253     break;
00254 
00255   default:
00256     return ;
00257     break;
00258   }
00259 
00260   onigenc_set_default_encoding(enc);
00261 }
00262 
00263 extern int
00264 reg_name_to_group_numbers(regex_t* reg,
00265   const unsigned char* name, const unsigned char* name_end, int** nums)
00266 {
00267   return onig_name_to_group_numbers(ONIG_C(reg), name, name_end, nums);
00268 }
00269 
00270 typedef struct {
00271   int (*func)(const unsigned char*, const unsigned char*,int,int*,regex_t*,void*);
00272   regex_t* reg;
00273   void* arg;
00274 } i_wrap;
00275 
00276 static int i_wrapper(const unsigned char* name, const unsigned char* name_end,
00277                    int ng, int* gs,
00278                    onig_regex_t* reg, void* arg)
00279 {
00280   i_wrap* warg = (i_wrap* )arg;
00281 
00282   return (*warg->func)(name, name_end, ng, gs, warg->reg, warg->arg);
00283 }
00284 
00285 extern int
00286 reg_foreach_name(regex_t* reg,
00287  int (*func)(const unsigned char*, const unsigned char*,int,int*,regex_t*,void*),
00288  void* arg)
00289 {
00290   i_wrap warg;
00291 
00292   warg.func = func;
00293   warg.reg  = reg;
00294   warg.arg  = arg;
00295 
00296   return onig_foreach_name(ONIG_C(reg), i_wrapper, &warg);
00297 }
00298 
00299 extern int
00300 reg_number_of_names(regex_t* reg)
00301 {
00302   return onig_number_of_names(ONIG_C(reg));
00303 }