Back to index

php5  5.3.10
regerror.c
Go to the documentation of this file.
00001 /**********************************************************************
00002   regerror.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 #include "regint.h"
00031 #include <stdio.h> /* for vsnprintf() */
00032 
00033 #ifdef HAVE_STDARG_PROTOTYPES
00034 #include <stdarg.h>
00035 #define va_init_list(a,b) va_start(a,b)
00036 #else
00037 #include <varargs.h>
00038 #define va_init_list(a,b) va_start(a)
00039 #endif
00040 
00041 extern UChar*
00042 onig_error_code_to_format(int code)
00043 {
00044   char *p;
00045 
00046   if (code >= 0) return (UChar* )0;
00047 
00048   switch (code) {
00049   case ONIG_MISMATCH:
00050     p = "mismatch"; break;
00051   case ONIG_NO_SUPPORT_CONFIG:
00052     p = "no support in this configuration"; break;
00053   case ONIGERR_MEMORY:
00054     p = "fail to memory allocation"; break;
00055   case ONIGERR_MATCH_STACK_LIMIT_OVER:
00056     p = "match-stack limit over"; break;
00057   case ONIGERR_TYPE_BUG:
00058     p = "undefined type (bug)"; break;
00059   case ONIGERR_PARSER_BUG:
00060     p = "internal parser error (bug)"; break;
00061   case ONIGERR_STACK_BUG:
00062     p = "stack error (bug)"; break;
00063   case ONIGERR_UNDEFINED_BYTECODE:
00064     p = "undefined bytecode (bug)"; break;
00065   case ONIGERR_UNEXPECTED_BYTECODE:
00066     p = "unexpected bytecode (bug)"; break;
00067   case ONIGERR_DEFAULT_ENCODING_IS_NOT_SETTED:
00068     p = "default multibyte-encoding is not setted"; break;
00069   case ONIGERR_SPECIFIED_ENCODING_CANT_CONVERT_TO_WIDE_CHAR:
00070     p = "can't convert to wide-char on specified multibyte-encoding"; break;
00071   case ONIGERR_INVALID_ARGUMENT:
00072     p = "invalid argument"; break;
00073   case ONIGERR_END_PATTERN_AT_LEFT_BRACE:
00074     p = "end pattern at left brace"; break;
00075   case ONIGERR_END_PATTERN_AT_LEFT_BRACKET:
00076     p = "end pattern at left bracket"; break;
00077   case ONIGERR_EMPTY_CHAR_CLASS:
00078     p = "empty char-class"; break;
00079   case ONIGERR_PREMATURE_END_OF_CHAR_CLASS:
00080     p = "premature end of char-class"; break;
00081   case ONIGERR_END_PATTERN_AT_ESCAPE:
00082     p = "end pattern at escape"; break;
00083   case ONIGERR_END_PATTERN_AT_META:
00084     p = "end pattern at meta"; break;
00085   case ONIGERR_END_PATTERN_AT_CONTROL:
00086     p = "end pattern at control"; break;
00087   case ONIGERR_META_CODE_SYNTAX:
00088     p = "illegal meta-code syntax"; break;
00089   case ONIGERR_CONTROL_CODE_SYNTAX:
00090     p = "illegal control-code syntax"; break;
00091   case ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE:
00092     p = "char-class value at end of range"; break;
00093   case ONIGERR_CHAR_CLASS_VALUE_AT_START_OF_RANGE:
00094     p = "char-class value at start of range"; break;
00095   case ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS:
00096     p = "unmatched range specifier in char-class"; break;
00097   case ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED:
00098     p = "target of repeat operator is not specified"; break;
00099   case ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID:
00100     p = "target of repeat operator is invalid"; break;
00101   case ONIGERR_NESTED_REPEAT_OPERATOR:
00102     p = "nested repeat operator"; break;
00103   case ONIGERR_UNMATCHED_CLOSE_PARENTHESIS:
00104     p = "unmatched close parenthesis"; break;
00105   case ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS:
00106     p = "end pattern with unmatched parenthesis"; break;
00107   case ONIGERR_END_PATTERN_IN_GROUP:
00108     p = "end pattern in group"; break;
00109   case ONIGERR_UNDEFINED_GROUP_OPTION:
00110     p = "undefined group option"; break;
00111   case ONIGERR_INVALID_POSIX_BRACKET_TYPE:
00112     p = "invalid POSIX bracket type"; break;
00113   case ONIGERR_INVALID_LOOK_BEHIND_PATTERN:
00114     p = "invalid pattern in look-behind"; break;
00115   case ONIGERR_INVALID_REPEAT_RANGE_PATTERN:
00116     p = "invalid repeat range {lower,upper}"; break;
00117   case ONIGERR_TOO_BIG_NUMBER:
00118     p = "too big number"; break;
00119   case ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE:
00120     p = "too big number for repeat range"; break;
00121   case ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE:
00122     p = "upper is smaller than lower in repeat range"; break;
00123   case ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS:
00124     p = "empty range in char class"; break;
00125   case ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE:
00126     p = "mismatch multibyte code length in char-class range"; break;
00127   case ONIGERR_TOO_MANY_MULTI_BYTE_RANGES:
00128     p = "too many multibyte code ranges are specified"; break;
00129   case ONIGERR_TOO_SHORT_MULTI_BYTE_STRING:
00130     p = "too short multibyte code string"; break;
00131   case ONIGERR_TOO_BIG_BACKREF_NUMBER:
00132     p = "too big backref number"; break;
00133   case ONIGERR_INVALID_BACKREF:
00134 #ifdef USE_NAMED_GROUP
00135     p = "invalid backref number/name"; break;
00136 #else
00137     p = "invalid backref number"; break;
00138 #endif
00139   case ONIGERR_NUMBERED_BACKREF_OR_CALL_NOT_ALLOWED:
00140     p = "numbered backref/call is not allowed. (use name)"; break;
00141   case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
00142     p = "too big wide-char value"; break;
00143   case ONIGERR_TOO_LONG_WIDE_CHAR_VALUE:
00144     p = "too long wide-char value"; break;
00145   case ONIGERR_INVALID_WIDE_CHAR_VALUE:
00146     p = "invalid wide-char value"; break;
00147   case ONIGERR_EMPTY_GROUP_NAME:
00148     p = "group name is empty"; break;
00149   case ONIGERR_INVALID_GROUP_NAME:
00150     p = "invalid group name <%n>"; break;
00151   case ONIGERR_INVALID_CHAR_IN_GROUP_NAME:
00152 #ifdef USE_NAMED_GROUP
00153     p = "invalid char in group name <%n>"; break;
00154 #else
00155     p = "invalid char in group number <%n>"; break;
00156 #endif
00157   case ONIGERR_UNDEFINED_NAME_REFERENCE:
00158     p = "undefined name <%n> reference"; break;
00159   case ONIGERR_UNDEFINED_GROUP_REFERENCE:
00160     p = "undefined group <%n> reference"; break;
00161   case ONIGERR_MULTIPLEX_DEFINED_NAME:
00162     p = "multiplex defined name <%n>"; break;
00163   case ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL:
00164     p = "multiplex definition name <%n> call"; break;
00165   case ONIGERR_NEVER_ENDING_RECURSION:
00166     p = "never ending recursion"; break;
00167   case ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY:
00168     p = "group number is too big for capture history"; break;
00169   case ONIGERR_INVALID_CHAR_PROPERTY_NAME:
00170     p = "invalid character property name {%n}"; break;
00171   case ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION:
00172     p = "not supported encoding combination"; break;
00173   case ONIGERR_INVALID_COMBINATION_OF_OPTIONS:
00174     p = "invalid combination of options"; break;
00175   case ONIGERR_OVER_THREAD_PASS_LIMIT_COUNT:
00176     p = "over thread pass limit count"; break;
00177 
00178   default:
00179     p = "undefined error code"; break;
00180   }
00181 
00182   return (UChar* )p;
00183 }
00184 
00185 
00186 static int to_ascii(OnigEncoding enc, UChar *s, UChar *end,
00187                   UChar buf[], int buf_size, int *is_over)
00188 {
00189   int len;
00190   UChar *p;
00191   OnigCodePoint code;
00192 
00193   if (ONIGENC_MBC_MINLEN(enc) > 1) {
00194     p = s;
00195     len = 0;
00196     while (p < end) {
00197       code = ONIGENC_MBC_TO_CODE(enc, p, end);
00198       if (code >= 0x80) {
00199        if (len + 5 <= buf_size) {
00200          sprintf((char* )(&(buf[len])), "\\%03o",
00201                 (unsigned int)(code & 0377));
00202          len += 5;
00203        }
00204        else {
00205          break;
00206        }
00207       }
00208       else {
00209        buf[len++] = (UChar )code;
00210       }
00211 
00212       p += enc_len(enc, p);
00213       if (len >= buf_size) break;
00214     }
00215 
00216     *is_over = ((p < end) ? 1 : 0);
00217   }
00218   else {
00219     len = MIN((end - s), buf_size);
00220     xmemcpy(buf, s, (size_t )len);
00221     *is_over = ((buf_size < (end - s)) ? 1 : 0);
00222   }
00223 
00224   return len;
00225 }
00226 
00227 
00228 /* for ONIG_MAX_ERROR_MESSAGE_LEN */
00229 #define MAX_ERROR_PAR_LEN   30
00230 
00231 extern int
00232 #ifdef HAVE_STDARG_PROTOTYPES
00233 onig_error_code_to_str(UChar* s, int code, ...)
00234 #else
00235 onig_error_code_to_str(s, code, va_alist)
00236   UChar* s;
00237   int code;
00238   va_dcl 
00239 #endif
00240 {
00241   UChar *p, *q;
00242   OnigErrorInfo* einfo;
00243   int len, is_over;
00244   UChar parbuf[MAX_ERROR_PAR_LEN];
00245   va_list vargs;
00246 
00247   va_init_list(vargs, code);
00248 
00249   switch (code) {
00250   case ONIGERR_UNDEFINED_NAME_REFERENCE:
00251   case ONIGERR_UNDEFINED_GROUP_REFERENCE:
00252   case ONIGERR_MULTIPLEX_DEFINED_NAME:
00253   case ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL:
00254   case ONIGERR_INVALID_GROUP_NAME:
00255   case ONIGERR_INVALID_CHAR_IN_GROUP_NAME:
00256   case ONIGERR_INVALID_CHAR_PROPERTY_NAME:
00257     einfo = va_arg(vargs, OnigErrorInfo*);
00258     len = to_ascii(einfo->enc, einfo->par, einfo->par_end,
00259                  parbuf, MAX_ERROR_PAR_LEN - 3, &is_over);
00260     q = onig_error_code_to_format(code);
00261     p = s;
00262     while (*q != '\0') {
00263       if (*q == '%') {
00264        q++;
00265        if (*q == 'n') { /* '%n': name */
00266          xmemcpy(p, parbuf, len);
00267          p += len;
00268          if (is_over != 0) {
00269            xmemcpy(p, "...", 3);
00270            p += 3;
00271          }
00272          q++;
00273        }
00274        else
00275          goto normal_char;
00276       }
00277       else {
00278       normal_char:
00279        *p++ = *q++;
00280       }
00281     }
00282     *p = '\0';
00283     len = p - s;
00284     break;
00285 
00286   default:
00287     q = onig_error_code_to_format(code);
00288     len = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, q);
00289     xmemcpy(s, q, len);
00290     s[len] = '\0';
00291     break;
00292   }
00293 
00294   va_end(vargs);
00295   return len;
00296 }
00297 
00298 
00299 void
00300 #ifdef HAVE_STDARG_PROTOTYPES
00301 onig_snprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc,
00302                            UChar* pat, UChar* pat_end, const UChar *fmt, ...)
00303 #else
00304 onig_snprintf_with_pattern(buf, bufsize, enc, pat, pat_end, fmt, va_alist)
00305     UChar buf[];
00306     int bufsize;
00307     OnigEncoding enc;
00308     UChar* pat;
00309     UChar* pat_end;
00310     const UChar *fmt;
00311     va_dcl
00312 #endif
00313 {
00314   int n, need, len;
00315   UChar *p, *s, *bp;
00316   UChar bs[6];
00317   va_list args;
00318 
00319   va_init_list(args, fmt);
00320   n = vsnprintf((char* )buf, bufsize, (const char* )fmt, args);
00321   va_end(args);
00322 
00323   need = (pat_end - pat) * 4 + 4;
00324 
00325   if (n + need < bufsize) {
00326     strcat((char* )buf, ": /");
00327     s = buf + onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, buf);
00328 
00329     p = pat;
00330     while (p < pat_end) {
00331       if (*p == MC_ESC(enc)) {
00332        *s++ = *p++;
00333        len = enc_len(enc, p);
00334        while (len-- > 0) *s++ = *p++;
00335       }
00336       else if (*p == '/') {
00337        *s++ = (unsigned char )MC_ESC(enc);
00338        *s++ = *p++;
00339       }
00340       else if (ONIGENC_IS_MBC_HEAD(enc, p)) {
00341         len = enc_len(enc, p);
00342         if (ONIGENC_MBC_MINLEN(enc) == 1) {
00343           while (len-- > 0) *s++ = *p++;
00344         }
00345         else { /* for UTF16 */
00346           int blen;
00347 
00348           while (len-- > 0) {
00349             sprintf((char* )bs, "\\%03o", *p++ & 0377);
00350             blen = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, bs);
00351             bp = bs;
00352             while (blen-- > 0) *s++ = *bp++;
00353           }
00354         }
00355       }
00356       else if (!ONIGENC_IS_CODE_PRINT(enc, *p) &&
00357               !ONIGENC_IS_CODE_SPACE(enc, *p)) {
00358        sprintf((char* )bs, "\\%03o", *p++ & 0377);
00359        len = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, bs);
00360         bp = bs;
00361        while (len-- > 0) *s++ = *bp++;
00362       }
00363       else {
00364        *s++ = *p++;
00365       }
00366     }
00367 
00368     *s++ = '/';
00369     *s   = '\0';
00370   }
00371 }