Ruby  1.9.3p551(2014-11-13revision48407)
regerror.c
Go to the documentation of this file.
1 /**********************************************************************
2  regerror.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include "regint.h"
31 #include <stdio.h> /* for vsnprintf() */
32 
33 #ifdef HAVE_STDARG_PROTOTYPES
34 #include <stdarg.h>
35 #define va_init_list(a,b) va_start(a,b)
36 #else
37 #include <varargs.h>
38 #define va_init_list(a,b) va_start(a)
39 #endif
40 
41 extern UChar*
43 {
44  const char *p;
45 
46  if (code >= 0) return (UChar* )0;
47 
48  switch (code) {
49  case ONIG_MISMATCH:
50  p = "mismatch"; break;
52  p = "no support in this configuration"; break;
53  case ONIGERR_MEMORY:
54  p = "failed to allocate memory"; break;
56  p = "match-stack limit over"; break;
57  case ONIGERR_TYPE_BUG:
58  p = "undefined type (bug)"; break;
59  case ONIGERR_PARSER_BUG:
60  p = "internal parser error (bug)"; break;
61  case ONIGERR_STACK_BUG:
62  p = "stack error (bug)"; break;
64  p = "undefined bytecode (bug)"; break;
66  p = "unexpected bytecode (bug)"; break;
68  p = "default multibyte-encoding is not setted"; break;
70  p = "can't convert to wide-char on specified multibyte-encoding"; break;
72  p = "invalid argument"; break;
74  p = "end pattern at left brace"; break;
76  p = "end pattern at left bracket"; break;
78  p = "empty char-class"; break;
80  p = "premature end of char-class"; break;
82  p = "end pattern at escape"; break;
84  p = "end pattern at meta"; break;
86  p = "end pattern at control"; break;
88  p = "invalid meta-code syntax"; break;
90  p = "invalid control-code syntax"; break;
92  p = "char-class value at end of range"; break;
94  p = "char-class value at start of range"; break;
96  p = "unmatched range specifier in char-class"; break;
98  p = "target of repeat operator is not specified"; break;
100  p = "target of repeat operator is invalid"; break;
102  p = "nested repeat operator"; break;
104  p = "unmatched close parenthesis"; break;
106  p = "end pattern with unmatched parenthesis"; break;
108  p = "end pattern in group"; break;
110  p = "undefined group option"; break;
112  p = "invalid POSIX bracket type"; break;
114  p = "invalid pattern in look-behind"; break;
116  p = "invalid repeat range {lower,upper}"; break;
118  p = "too big number"; break;
120  p = "too big number for repeat range"; break;
122  p = "upper is smaller than lower in repeat range"; break;
124  p = "empty range in char class"; break;
126  p = "mismatch multibyte code length in char-class range"; break;
128  p = "too many multibyte code ranges are specified"; break;
130  p = "too short multibyte code string"; break;
132  p = "too big backref number"; break;
134 #ifdef USE_NAMED_GROUP
135  p = "invalid backref number/name"; break;
136 #else
137  p = "invalid backref number"; break;
138 #endif
140  p = "numbered backref/call is not allowed. (use name)"; break;
142  p = "too big wide-char value"; break;
144  p = "too long wide-char value"; break;
146  p = "invalid code point value"; break;
148  p = "group name is empty"; break;
150  p = "invalid group name <%n>"; break;
152 #ifdef USE_NAMED_GROUP
153  p = "invalid char in group name <%n>"; break;
154 #else
155  p = "invalid char in group number <%n>"; break;
156 #endif
158  p = "undefined name <%n> reference"; break;
160  p = "undefined group <%n> reference"; break;
162  p = "multiplex defined name <%n>"; break;
164  p = "multiplex definition name <%n> call"; break;
166  p = "never ending recursion"; break;
168  p = "group number is too big for capture history"; break;
170  p = "invalid character property name {%n}"; break;
172  p = "not supported encoding combination"; break;
174  p = "invalid combination of options"; break;
176  p = "over thread pass limit count"; break;
177 
178  default:
179  p = "undefined error code"; break;
180  }
181 
182  return (UChar* )p;
183 }
184 
185 static void sprint_byte(char* s, unsigned int v)
186 {
187  sprintf(s, "%02x", (v & 0377));
188 }
189 
190 static void sprint_byte_with_x(char* s, unsigned int v)
191 {
192  sprintf(s, "\\x%02x", (v & 0377));
193 }
194 
196  UChar buf[], int buf_size, int *is_over)
197 {
198  int len;
199  UChar *p;
201 
202  if (ONIGENC_MBC_MINLEN(enc) > 1) {
203  p = s;
204  len = 0;
205  while (p < end) {
206  code = ONIGENC_MBC_TO_CODE(enc, p, end);
207  if (code >= 0x80) {
208  if (code > 0xffff && len + 10 <= buf_size) {
209  sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 24));
210  sprint_byte((char*)(&(buf[len+4])), (unsigned int)(code >> 16));
211  sprint_byte((char*)(&(buf[len+6])), (unsigned int)(code >> 8));
212  sprint_byte((char*)(&(buf[len+8])), (unsigned int)code);
213  len += 10;
214  }
215  else if (len + 6 <= buf_size) {
216  sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 8));
217  sprint_byte((char*)(&(buf[len+4])), (unsigned int)code);
218  len += 6;
219  }
220  else {
221  break;
222  }
223  }
224  else {
225  buf[len++] = (UChar )code;
226  }
227 
228  p += enclen(enc, p, end);
229  if (len >= buf_size) break;
230  }
231 
232  *is_over = ((p < end) ? 1 : 0);
233  }
234  else {
235  len = (int)MIN((end - s), buf_size);
236  xmemcpy(buf, s, (size_t )len);
237  *is_over = ((buf_size < (end - s)) ? 1 : 0);
238  }
239 
240  return len;
241 }
242 
243 
244 /* for ONIG_MAX_ERROR_MESSAGE_LEN */
245 #define MAX_ERROR_PAR_LEN 30
246 
247 extern int
248 #ifdef HAVE_STDARG_PROTOTYPES
249 onig_error_code_to_str(UChar* s, int code, ...)
250 #else
251 onig_error_code_to_str(s, code, va_alist)
252  UChar* s;
253  int code;
254  va_dcl
255 #endif
256 {
257  UChar *p, *q;
259  size_t len;
260  int is_over;
262  va_list vargs;
263 
264  va_init_list(vargs, code);
265 
266  switch (code) {
274  einfo = va_arg(vargs, OnigErrorInfo*);
275  len = to_ascii(einfo->enc, einfo->par, einfo->par_end,
276  parbuf, MAX_ERROR_PAR_LEN - 3, &is_over);
277  q = onig_error_code_to_format(code);
278  p = s;
279  while (*q != '\0') {
280  if (*q == '%') {
281  q++;
282  if (*q == 'n') { /* '%n': name */
283  xmemcpy(p, parbuf, len);
284  p += len;
285  if (is_over != 0) {
286  xmemcpy(p, "...", 3);
287  p += 3;
288  }
289  q++;
290  }
291  else
292  goto normal_char;
293  }
294  else {
295  normal_char:
296  *p++ = *q++;
297  }
298  }
299  *p = '\0';
300  len = p - s;
301  break;
302 
303  default:
304  q = onig_error_code_to_format(code);
306  xmemcpy(s, q, len);
307  s[len] = '\0';
308  break;
309  }
310 
311  va_end(vargs);
312  return (int)len;
313 }
314 
315 void
317  UChar* pat, UChar* pat_end, const UChar *fmt, va_list args)
318 {
319  size_t need;
320  int n, len;
321  UChar *p, *s, *bp;
322  UChar bs[6];
323 
324  n = xvsnprintf((char* )buf, bufsize, (const char* )fmt, args);
325 
326  need = (pat_end - pat) * 4 + 4;
327 
328  if (n + need < (size_t)bufsize) {
329  strcat((char* )buf, ": /");
331 
332  p = pat;
333  while (p < pat_end) {
334  if (*p == '\\') {
335  *s++ = *p++;
336  len = enclen(enc, p, pat_end);
337  while (len-- > 0) *s++ = *p++;
338  }
339  else if (*p == '/') {
340  *s++ = (unsigned char )'\\';
341  *s++ = *p++;
342  }
343  else if (ONIGENC_IS_MBC_HEAD(enc, p, pat_end)) {
344  len = enclen(enc, p, pat_end);
345  if (ONIGENC_MBC_MINLEN(enc) == 1) {
346  while (len-- > 0) *s++ = *p++;
347  }
348  else { /* for UTF16 */
349  int blen;
350 
351  while (len-- > 0) {
352  sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
354  bp = bs;
355  while (blen-- > 0) *s++ = *bp++;
356  }
357  }
358  }
359  else if (!ONIGENC_IS_CODE_PRINT(enc, *p) &&
360  !ONIGENC_IS_CODE_SPACE(enc, *p)) {
361  sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
363  bp = bs;
364  while (len-- > 0) *s++ = *bp++;
365  }
366  else {
367  *s++ = *p++;
368  }
369  }
370 
371  *s++ = '/';
372  *s = '\0';
373  }
374 }
375 
376 void
377 #ifdef HAVE_STDARG_PROTOTYPES
378 onig_snprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc,
379  UChar* pat, UChar* pat_end, const UChar *fmt, ...)
380 #else
381 onig_snprintf_with_pattern(buf, bufsize, enc, pat, pat_end, fmt, va_alist)
382  UChar buf[];
383  int bufsize;
385  UChar* pat;
386  UChar* pat_end;
387  const UChar *fmt;
388  va_dcl
389 #endif
390 {
391  va_list args;
392  va_init_list(args, fmt);
393  onig_vsnprintf_with_pattern(buf, bufsize, enc,
394  pat, pat_end, fmt, args);
395  va_end(args);
396 }
397 
#define ONIGERR_UNDEFINED_GROUP_OPTION
Definition: oniguruma.h:531
unsigned int OnigCodePoint
Definition: oniguruma.h:111
ssize_t n
Definition: bigdecimal.c:5519
VP_EXPORT int
Definition: bigdecimal.c:4911
UChar * pat
Definition: regerror.c:385
#define ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL
Definition: oniguruma.h:553
#define ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
Definition: oniguruma.h:559
code
Definition: tcltklib.c:3375
#define ONIGERR_STACK_BUG
Definition: oniguruma.h:504
#define ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE
Definition: oniguruma.h:540
OnigUChar * par_end
Definition: oniguruma.h:605
#define ONIGERR_SPECIFIED_ENCODING_CANT_CONVERT_TO_WIDE_CHAR
Definition: oniguruma.h:509
SYMID SyckParser * p
Definition: yaml2byte.c:119
#define ONIGENC_IS_CODE_PRINT(enc, code)
Definition: oniguruma.h:276
#define bp()
Definition: debug.h:27
VALUE enc
Definition: tcltklib.c:10402
#define ONIGERR_END_PATTERN_AT_META
Definition: oniguruma.h:518
#define ONIGERR_INVALID_COMBINATION_OF_OPTIONS
Definition: oniguruma.h:561
#define ONIGERR_NEVER_ENDING_RECURSION
Definition: oniguruma.h:554
#define ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
Definition: oniguruma.h:537
#define ONIG_ENCODING_ASCII
Definition: oniguruma.h:180
#define ONIGERR_TYPE_BUG
Definition: oniguruma.h:502
#define ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
Definition: oniguruma.h:529
#define ONIGERR_INVALID_LOOK_BEHIND_PATTERN
Definition: oniguruma.h:533
#define ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION
Definition: oniguruma.h:560
VALUE VALUE args
Definition: tcltklib.c:2550
static void sprint_byte(char *s, unsigned int v)
Definition: regerror.c:185
#define ONIGERR_INVALID_CHAR_PROPERTY_NAME
Definition: oniguruma.h:556
VALUE einfo
Definition: tcltklib.c:843
const char * fmt
Definition: tcltklib.c:837
#define ONIGERR_TOO_MANY_MULTI_BYTE_RANGES
Definition: oniguruma.h:541
#define ONIGERR_NESTED_REPEAT_OPERATOR
Definition: oniguruma.h:527
#define ONIGERR_END_PATTERN_AT_LEFT_BRACE
Definition: oniguruma.h:513
#define enclen(enc, p, e)
int bufsize
Definition: regerror.c:383
int is_over
Definition: regerror.c:260
#define ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID
Definition: oniguruma.h:526
#define ONIGERR_MULTIPLEX_DEFINED_NAME
Definition: oniguruma.h:552
#define xvsnprintf
Definition: regint.h:177
#define ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
Definition: oniguruma.h:546
va_end(args)
#define ONIGERR_INVALID_BACKREF
Definition: oniguruma.h:544
#define ONIGERR_UNEXPECTED_BYTECODE
Definition: oniguruma.h:506
#define ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE
Definition: oniguruma.h:538
#define ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
Definition: oniguruma.h:542
#define xmemcpy
Definition: regint.h:169
#define ONIGENC_IS_MBC_HEAD(enc, p, e)
Definition: oniguruma.h:221
#define ONIGENC_IS_CODE_SPACE(enc, code)
Definition: oniguruma.h:290
va_dcl
Definition: tcltklib.c:840
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:3913
UChar * onig_error_code_to_format(int code)
Definition: regerror.c:42
#define ONIGERR_UNDEFINED_NAME_REFERENCE
Definition: oniguruma.h:550
static int to_ascii(OnigEncoding enc, UChar *s, UChar *end, UChar buf[], int buf_size, int *is_over)
Definition: regerror.c:195
UChar * pat_end
Definition: regerror.c:386
va_list vargs
Definition: regerror.c:262
#define const
Definition: strftime.c:101
register char * s
Definition: os2.c:56
#define ONIGERR_END_PATTERN_IN_GROUP
Definition: oniguruma.h:530
#define MAX_ERROR_PAR_LEN
Definition: regerror.c:245
#define ONIG_NO_SUPPORT_CONFIG
Definition: oniguruma.h:498
OnigEncoding enc
Definition: oniguruma.h:603
#define ONIGERR_PARSER_BUG
Definition: oniguruma.h:503
register unsigned int len
Definition: name2ctype.h:22210
int onigenc_str_bytelen_null(OnigEncoding enc, const UChar *s)
Definition: regenc.c:162
#define ONIGENC_MBC_TO_CODE(enc, p, end)
Definition: oniguruma.h:264
#define ONIGENC_MBC_MINLEN(enc)
Definition: oniguruma.h:262
#define MIN(a, b)
Definition: regint.h:237
#define UChar
Definition: oniguruma.h:107
#define ONIGERR_EMPTY_CHAR_CLASS
Definition: oniguruma.h:515
#define ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED
Definition: oniguruma.h:525
#define ONIGERR_NUMBERED_BACKREF_OR_CALL_NOT_ALLOWED
Definition: oniguruma.h:545
gz end
Definition: zlib.c:2033
#define ONIGERR_END_PATTERN_AT_LEFT_BRACKET
Definition: oniguruma.h:514
#define va_init_list(a, b)
Definition: tcltklib.c:57
#define ONIGERR_TOO_BIG_BACKREF_NUMBER
Definition: oniguruma.h:543
#define ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS
Definition: oniguruma.h:524
UChar parbuf[MAX_ERROR_PAR_LEN]
Definition: regerror.c:261
onig_vsnprintf_with_pattern(buf, bufsize, enc, pat, pat_end, fmt, args)
#define ONIGERR_END_PATTERN_AT_ESCAPE
Definition: oniguruma.h:517
#define ONIGERR_MEMORY
Definition: oniguruma.h:501
#define ONIGERR_CONTROL_CODE_SYNTAX
Definition: oniguruma.h:521
#define ONIG_MISMATCH
Definition: oniguruma.h:497
#define ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
Definition: oniguruma.h:539
#define ONIGERR_INVALID_POSIX_BRACKET_TYPE
Definition: oniguruma.h:532
#define ONIGERR_EMPTY_GROUP_NAME
Definition: oniguruma.h:547
#define ONIGERR_TOO_BIG_NUMBER
Definition: oniguruma.h:536
#define ONIGERR_INVALID_ARGUMENT
Definition: oniguruma.h:511
#define ONIGERR_UNDEFINED_BYTECODE
Definition: oniguruma.h:505
#define ONIGERR_MATCH_STACK_LIMIT_OVER
Definition: oniguruma.h:507
#define ONIGERR_END_PATTERN_AT_CONTROL
Definition: oniguruma.h:519
#define ONIGERR_CHAR_CLASS_VALUE_AT_START_OF_RANGE
Definition: oniguruma.h:523
#define ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE
Definition: oniguruma.h:522
#define ONIGERR_UNMATCHED_CLOSE_PARENTHESIS
Definition: oniguruma.h:528
#define ONIGERR_PREMATURE_END_OF_CHAR_CLASS
Definition: oniguruma.h:516
#define ONIGERR_INVALID_CODE_POINT_VALUE
Definition: oniguruma.h:557
#define ONIGERR_DEFAULT_ENCODING_IS_NOT_SETTED
Definition: oniguruma.h:508
BDIGIT v
Definition: bigdecimal.c:5520
#define ONIGERR_INVALID_REPEAT_RANGE_PATTERN
Definition: oniguruma.h:534
q
Definition: tcltklib.c:2957
static void sprint_byte_with_x(char *s, unsigned int v)
Definition: regerror.c:190
#define ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY
Definition: oniguruma.h:555
#define ONIGERR_INVALID_GROUP_NAME
Definition: oniguruma.h:548
#define ONIGERR_UNDEFINED_GROUP_REFERENCE
Definition: oniguruma.h:551
OnigUChar * par
Definition: oniguruma.h:604
#define ONIGERR_META_CODE_SYNTAX
Definition: oniguruma.h:520
#define ONIGERR_OVER_THREAD_PASS_LIMIT_COUNT
Definition: oniguruma.h:564
#define ONIGERR_INVALID_CHAR_IN_GROUP_NAME
Definition: oniguruma.h:549