Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# regerror.c

Go to the documentation of this file.

    00001 /**********************************************************************
    00002   regerror.c -  Oniguruma (regular expression library)
    00003 **********************************************************************/
    00004 /*-
    00005  * Copyright (c) 2002-2007  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   const 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 = "failed to allocate memory"; 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 = "invalid meta-code syntax"; break;
    00089   case ONIGERR_CONTROL_CODE_SYNTAX:
    00090     p = "invalid 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_CODE_POINT_VALUE:
    00146     p = "invalid code point 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 static void sprint_byte(char* s, unsigned int v)
    00186 {
    00187   sprintf(s, "%02x", (v & 0377));
    00188 }
    00189
    00190 static void sprint_byte_with_x(char* s, unsigned int v)
    00191 {
    00192   sprintf(s, "\\x%02x", (v & 0377));
    00193 }
    00194
    00195 static int to_ascii(OnigEncoding enc, UChar *s, UChar *end,
    00196                     UChar buf[], int buf_size, int *is_over)
    00197 {
    00198   int len;
    00199   UChar *p;
    00200   OnigCodePoint code;
    00201
    00202   if (ONIGENC_MBC_MINLEN(enc) > 1) {
    00203     p = s;
    00204     len = 0;
    00205     while (p < end) {
    00206       code = ONIGENC_MBC_TO_CODE(enc, p, end);
    00207       if (code >= 0x80) {
    00208         if (code > 0xffff && len + 10 <= buf_size) {
    00209           sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 24));
    00210           sprint_byte((char*)(&(buf[len+4])),      (unsigned int)(code >> 16));
    00211           sprint_byte((char*)(&(buf[len+6])),      (unsigned int)(code >>  8));
    00212           sprint_byte((char*)(&(buf[len+8])),      (unsigned int)code);
    00213           len += 10;
    00214         }
    00215         else if (len + 6 <= buf_size) {
    00216           sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 8));
    00217           sprint_byte((char*)(&(buf[len+4])),      (unsigned int)code);
    00218           len += 6;
    00219         }
    00220         else {
    00221           break;
    00222         }
    00223       }
    00224       else {
    00225         buf[len++] = (UChar )code;
    00226       }
    00227
    00228       p += enclen(enc, p, end);
    00229       if (len >= buf_size) break;
    00230     }
    00231
    00232     *is_over = ((p < end) ? 1 : 0);
    00233   }
    00234   else {
    00235     len = (int)MIN((end - s), buf_size);
    00236     xmemcpy(buf, s, (size_t )len);
    00237     *is_over = ((buf_size < (end - s)) ? 1 : 0);
    00238   }
    00239
    00240   return len;
    00241 }
    00242
    00243
    00244 /* for ONIG_MAX_ERROR_MESSAGE_LEN */
    00245 #define MAX_ERROR_PAR_LEN   30
    00246
    00247 extern int
    00248 #ifdef HAVE_STDARG_PROTOTYPES
    00249 onig_error_code_to_str(UChar* s, int code, ...)
    00250 #else
    00251 onig_error_code_to_str(s, code, va_alist)
    00252   UChar* s;
    00253   int code;
    00254   va_dcl
    00255 #endif
    00256 {
    00257   UChar *p, *q;
    00258   OnigErrorInfo* einfo;
    00259   size_t len;
    00260   int is_over;
    00261   UChar parbuf[MAX_ERROR_PAR_LEN];
    00262   va_list vargs;
    00263
    00264   va_init_list(vargs, code);
    00265
    00266   switch (code) {
    00267   case ONIGERR_UNDEFINED_NAME_REFERENCE:
    00268   case ONIGERR_UNDEFINED_GROUP_REFERENCE:
    00269   case ONIGERR_MULTIPLEX_DEFINED_NAME:
    00270   case ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL:
    00271   case ONIGERR_INVALID_GROUP_NAME:
    00272   case ONIGERR_INVALID_CHAR_IN_GROUP_NAME:
    00273   case ONIGERR_INVALID_CHAR_PROPERTY_NAME:
    00274     einfo = va_arg(vargs, OnigErrorInfo*);
    00275     len = to_ascii(einfo->enc, einfo->par, einfo->par_end,
    00276                    parbuf, MAX_ERROR_PAR_LEN - 3, &is_over);
    00277     q = onig_error_code_to_format(code);
    00278     p = s;
    00279     while (*q != '\0') {
    00280       if (*q == '%') {
    00281         q++;
    00282         if (*q == 'n') { /* '%n': name */
    00283           xmemcpy(p, parbuf, len);
    00284           p += len;
    00285           if (is_over != 0) {
    00286             xmemcpy(p, "...", 3);
    00287             p += 3;
    00288           }
    00289           q++;
    00290         }
    00291         else
    00292           goto normal_char;
    00293       }
    00294       else {
    00295       normal_char:
    00296         *p++ = *q++;
    00297       }
    00298     }
    00299     *p = '\0';
    00300     len = p - s;
    00301     break;
    00302
    00303   default:
    00304     q = onig_error_code_to_format(code);
    00305     len = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, q);
    00306     xmemcpy(s, q, len);
    00307     s[len] = '\0';
    00308     break;
    00309   }
    00310
    00311   va_end(vargs);
    00312   return (int)len;
    00313 }
    00314
    00315 void
    00316 onig_vsnprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc,
    00317                            UChar* pat, UChar* pat_end, const UChar *fmt, va_list args)
    00318 {
    00319   size_t need;
    00320   int n, len;
    00321   UChar *p, *s, *bp;
    00322   UChar bs[6];
    00323
    00324   n = xvsnprintf((char* )buf, bufsize, (const char* )fmt, args);
    00325
    00326   need = (pat_end - pat) * 4 + 4;
    00327
    00328   if (n + need < (size_t)bufsize) {
    00329     strcat((char* )buf, ": /");
    00330     s = buf + onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, buf);
    00331
    00332     p = pat;
    00333     while (p < pat_end) {
    00334       if (*p == '\\') {
    00335         *s++ = *p++;
    00336         len = enclen(enc, p, pat_end);
    00337         while (len-- > 0) *s++ = *p++;
    00338       }
    00339       else if (*p == '/') {
    00340         *s++ = (unsigned char )'\\';
    00341         *s++ = *p++;
    00342       }
    00343       else if (ONIGENC_IS_MBC_HEAD(enc, p, pat_end)) {
    00344         len = enclen(enc, p, pat_end);
    00345         if (ONIGENC_MBC_MINLEN(enc) == 1) {
    00346           while (len-- > 0) *s++ = *p++;
    00347         }
    00348         else { /* for UTF16 */
    00349           int blen;
    00350
    00351           while (len-- > 0) {
    00352             sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
    00353             blen = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, bs);
    00354             bp = bs;
    00355             while (blen-- > 0) *s++ = *bp++;
    00356           }
    00357         }
    00358       }
    00359       else if (!ONIGENC_IS_CODE_PRINT(enc, *p) &&
    00360                !ONIGENC_IS_CODE_SPACE(enc, *p)) {
    00361         sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
    00362         len = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, bs);
    00363         bp = bs;
    00364         while (len-- > 0) *s++ = *bp++;
    00365       }
    00366       else {
    00367         *s++ = *p++;
    00368       }
    00369     }
    00370
    00371     *s++ = '/';
    00372     *s   = '\0';
    00373   }
    00374 }
    00375
    00376 void
    00377 #ifdef HAVE_STDARG_PROTOTYPES
    00378 onig_snprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc,
    00379                            UChar* pat, UChar* pat_end, const UChar *fmt, ...)
    00380 #else
    00381 onig_snprintf_with_pattern(buf, bufsize, enc, pat, pat_end, fmt, va_alist)
    00382     UChar buf[];
    00383     int bufsize;
    00384     OnigEncoding enc;
    00385     UChar* pat;
    00386     UChar* pat_end;
    00387     const UChar *fmt;
    00388     va_dcl
    00389 #endif
    00390 {
    00391   va_list args;
    00392   va_init_list(args, fmt);
    00393   onig_vsnprintf_with_pattern(buf, bufsize, enc,
    00394           pat, pat_end, fmt, args);
    00395   va_end(args);
    00396 }
    00397
    00398