Ruby  2.0.0p247(2013-06-27revision41674)
regerror.c
Go to the documentation of this file.
1 /**********************************************************************
2  regerror.c - Onigmo (Oniguruma-mod) (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6  * Copyright (c) 2011 K.Takata <kentkt AT csc DOT jp>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include "regint.h"
32 #include <stdio.h> /* for vsnprintf() */
33 
34 #ifdef HAVE_STDARG_PROTOTYPES
35 #include <stdarg.h>
36 #define va_init_list(a,b) va_start(a,b)
37 #else
38 #include <varargs.h>
39 #define va_init_list(a,b) va_start(a)
40 #endif
41 
42 extern UChar*
44 {
45  const char *p;
46 
47  if (code >= 0) return (UChar* )0;
48 
49  switch (code) {
50  case ONIG_MISMATCH:
51  p = "mismatch"; break;
53  p = "no support in this configuration"; break;
54  case ONIGERR_MEMORY:
55  p = "failed to allocate memory"; break;
57  p = "match-stack limit over"; break;
58  case ONIGERR_TYPE_BUG:
59  p = "undefined type (bug)"; break;
60  case ONIGERR_PARSER_BUG:
61  p = "internal parser error (bug)"; break;
62  case ONIGERR_STACK_BUG:
63  p = "stack error (bug)"; break;
65  p = "undefined bytecode (bug)"; break;
67  p = "unexpected bytecode (bug)"; break;
69  p = "default multibyte-encoding is not set"; break;
71  p = "can't convert to wide-char on specified multibyte-encoding"; break;
73  p = "invalid argument"; break;
75  p = "end pattern at left brace"; break;
77  p = "end pattern at left bracket"; break;
79  p = "empty char-class"; break;
81  p = "premature end of char-class"; break;
83  p = "end pattern at escape"; break;
85  p = "end pattern at meta"; break;
87  p = "end pattern at control"; break;
89  p = "invalid meta-code syntax"; break;
91  p = "invalid control-code syntax"; break;
93  p = "char-class value at end of range"; break;
95  p = "char-class value at start of range"; break;
97  p = "unmatched range specifier in char-class"; break;
99  p = "target of repeat operator is not specified"; break;
101  p = "target of repeat operator is invalid"; break;
103  p = "nested repeat operator"; break;
105  p = "unmatched close parenthesis"; break;
107  p = "end pattern with unmatched parenthesis"; break;
109  p = "end pattern in group"; break;
111  p = "undefined group option"; break;
113  p = "invalid POSIX bracket type"; break;
115  p = "invalid pattern in look-behind"; break;
117  p = "invalid repeat range {lower,upper}"; break;
119  p = "invalid conditional pattern"; break;
121  p = "too big number"; break;
123  p = "too big number for repeat range"; break;
125  p = "upper is smaller than lower in repeat range"; break;
127  p = "empty range in char class"; break;
129  p = "mismatch multibyte code length in char-class range"; break;
131  p = "too many multibyte code ranges are specified"; break;
133  p = "too short multibyte code string"; break;
135  p = "too big backref number"; break;
137 #ifdef USE_NAMED_GROUP
138  p = "invalid backref number/name"; break;
139 #else
140  p = "invalid backref number"; break;
141 #endif
143  p = "numbered backref/call is not allowed. (use name)"; break;
145  p = "too big wide-char value"; break;
147  p = "too short digits"; break;
149  p = "too long wide-char value"; break;
151  p = "invalid code point value"; break;
153  p = "group name is empty"; break;
155  p = "invalid group name <%n>"; break;
157 #ifdef USE_NAMED_GROUP
158  p = "invalid char in group name <%n>"; break;
159 #else
160  p = "invalid char in group number <%n>"; break;
161 #endif
163  p = "undefined name <%n> reference"; break;
165  p = "undefined group <%n> reference"; break;
167  p = "multiplex defined name <%n>"; break;
169  p = "multiplex definition name <%n> call"; break;
171  p = "never ending recursion"; break;
173  p = "group number is too big for capture history"; break;
175  p = "invalid character property name {%n}"; break;
177  p = "not supported encoding combination"; break;
179  p = "invalid combination of options"; break;
181  p = "over thread pass limit count"; break;
182 
183  default:
184  p = "undefined error code"; break;
185  }
186 
187  return (UChar* )p;
188 }
189 
190 static void sprint_byte(char* s, unsigned int v)
191 {
192  sprintf(s, "%02x", (v & 0377));
193 }
194 
195 static void sprint_byte_with_x(char* s, unsigned int v)
196 {
197  sprintf(s, "\\x%02x", (v & 0377));
198 }
199 
201  UChar buf[], int buf_size, int *is_over)
202 {
203  int len;
204  UChar *p;
206 
207  if (ONIGENC_MBC_MINLEN(enc) > 1) {
208  p = s;
209  len = 0;
210  while (p < end) {
211  code = ONIGENC_MBC_TO_CODE(enc, p, end);
212  if (code >= 0x80) {
213  if (code > 0xffff && len + 10 <= buf_size) {
214  sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 24));
215  sprint_byte((char*)(&(buf[len+4])), (unsigned int)(code >> 16));
216  sprint_byte((char*)(&(buf[len+6])), (unsigned int)(code >> 8));
217  sprint_byte((char*)(&(buf[len+8])), (unsigned int)code);
218  len += 10;
219  }
220  else if (len + 6 <= buf_size) {
221  sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 8));
222  sprint_byte((char*)(&(buf[len+4])), (unsigned int)code);
223  len += 6;
224  }
225  else {
226  break;
227  }
228  }
229  else {
230  buf[len++] = (UChar )code;
231  }
232 
233  p += enclen(enc, p, end);
234  if (len >= buf_size) break;
235  }
236 
237  *is_over = ((p < end) ? 1 : 0);
238  }
239  else {
240  len = (int )MIN((end - s), buf_size);
241  xmemcpy(buf, s, (size_t )len);
242  *is_over = ((buf_size < (end - s)) ? 1 : 0);
243  }
244 
245  return len;
246 }
247 
248 
249 /* for ONIG_MAX_ERROR_MESSAGE_LEN */
250 #define MAX_ERROR_PAR_LEN 30
251 
252 extern int
253 #ifdef HAVE_STDARG_PROTOTYPES
254 onig_error_code_to_str(UChar* s, OnigPosition code, ...)
255 #else
256 onig_error_code_to_str(s, code, va_alist)
257  UChar* s;
259  va_dcl
260 #endif
261 {
262  UChar *p, *q;
264  size_t len;
265  int is_over;
267  va_list vargs;
268 
269  va_init_list(vargs, code);
270 
271  switch (code) {
279  einfo = va_arg(vargs, OnigErrorInfo*);
280  len = to_ascii(einfo->enc, einfo->par, einfo->par_end,
281  parbuf, MAX_ERROR_PAR_LEN - 3, &is_over);
282  q = onig_error_code_to_format(code);
283  p = s;
284  while (*q != '\0') {
285  if (*q == '%') {
286  q++;
287  if (*q == 'n') { /* '%n': name */
288  xmemcpy(p, parbuf, len);
289  p += len;
290  if (is_over != 0) {
291  xmemcpy(p, "...", 3);
292  p += 3;
293  }
294  q++;
295  }
296  else
297  goto normal_char;
298  }
299  else {
300  normal_char:
301  *p++ = *q++;
302  }
303  }
304  *p = '\0';
305  len = p - s;
306  break;
307 
308  default:
309  q = onig_error_code_to_format(code);
311  xmemcpy(s, q, len);
312  s[len] = '\0';
313  break;
314  }
315 
316  va_end(vargs);
317  return (int )len;
318 }
319 
320 void
322  UChar* pat, UChar* pat_end, const UChar *fmt, va_list args)
323 {
324  size_t need;
325  int n, len;
326  UChar *p, *s, *bp;
327  UChar bs[6];
328 
329  n = xvsnprintf((char* )buf, bufsize, (const char* )fmt, args);
330 
331  need = (pat_end - pat) * 4 + 4;
332 
333  if (n + need < (size_t )bufsize) {
334  strcat((char* )buf, ": /");
336 
337  p = pat;
338  while (p < pat_end) {
339  if (*p == '\\') {
340  *s++ = *p++;
341  len = enclen(enc, p, pat_end);
342  while (len-- > 0) *s++ = *p++;
343  }
344  else if (*p == '/') {
345  *s++ = (unsigned char )'\\';
346  *s++ = *p++;
347  }
348  else if (ONIGENC_IS_MBC_HEAD(enc, p, pat_end)) {
349  len = enclen(enc, p, pat_end);
350  if (ONIGENC_MBC_MINLEN(enc) == 1) {
351  while (len-- > 0) *s++ = *p++;
352  }
353  else { /* for UTF16 */
354  int blen;
355 
356  while (len-- > 0) {
357  sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
359  bp = bs;
360  while (blen-- > 0) *s++ = *bp++;
361  }
362  }
363  }
364  else if (!ONIGENC_IS_CODE_PRINT(enc, *p) &&
365  !ONIGENC_IS_CODE_SPACE(enc, *p)) {
366  sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
368  bp = bs;
369  while (len-- > 0) *s++ = *bp++;
370  }
371  else {
372  *s++ = *p++;
373  }
374  }
375 
376  *s++ = '/';
377  *s = '\0';
378  }
379 }
380 
381 void
382 #ifdef HAVE_STDARG_PROTOTYPES
383 onig_snprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc,
384  UChar* pat, UChar* pat_end, const UChar *fmt, ...)
385 #else
386 onig_snprintf_with_pattern(buf, bufsize, enc, pat, pat_end, fmt, va_alist)
387  UChar buf[];
388  int bufsize;
390  UChar* pat;
391  UChar* pat_end;
392  const UChar *fmt;
393  va_dcl
394 #endif
395 {
396  va_list args;
397  va_init_list(args, fmt);
398  onig_vsnprintf_with_pattern(buf, bufsize, enc,
399  pat, pat_end, fmt, args);
400  va_end(args);
401 }
402 
#define ONIGERR_INVALID_COMBINATION_OF_OPTIONS
#define ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
ssize_t n
Definition: bigdecimal.c:5655
UChar * onig_error_code_to_format(OnigPosition code)
Definition: regerror.c:43
#define ONIGERR_NEVER_ENDING_RECURSION
VP_EXPORT int
Definition: bigdecimal.c:5050
#define ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION
UChar * pat
Definition: regerror.c:390
#define ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
code
Definition: tcltklib.c:3381
#define ONIG_ENCODING_ASCII
Win32OLEIDispatch * p
Definition: win32ole.c:786
#define ONIGENC_MBC_MINLEN(enc)
#define ONIGERR_MATCH_STACK_LIMIT_OVER
#define ONIGERR_INVALID_POSIX_BRACKET_TYPE
unsigned int OnigCodePoint
Definition: ripper.y:115
#define ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS
OnigUChar * par
Definition: ripper.y:635
#define UChar
#define ONIGENC_IS_CODE_SPACE(enc, code)
#define ONIGENC_IS_MBC_HEAD(enc, p, e)
VALUE enc
Definition: tcltklib.c:10311
#define ONIGERR_CONTROL_CODE_SYNTAX
#define ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY
#define ONIGERR_INVALID_ARGUMENT
#define ONIGERR_END_PATTERN_AT_CONTROL
#define ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED
OnigUChar * par_end
Definition: ripper.y:636
VALUE VALUE args
Definition: tcltklib.c:2561
static void sprint_byte(char *s, unsigned int v)
Definition: regerror.c:190
#define ONIGERR_TOO_BIG_NUMBER
#define ONIGERR_PREMATURE_END_OF_CHAR_CLASS
#define ONIGERR_TOO_SHORT_DIGITS
VALUE einfo
Definition: tcltklib.c:847
const char * fmt
Definition: tcltklib.c:841
#define ONIGERR_UNDEFINED_GROUP_OPTION
#define ONIGERR_STACK_BUG
#define enclen(enc, p, e)
#define ONIGERR_INVALID_CONDITION_PATTERN
#define ONIGERR_DEFAULT_ENCODING_IS_NOT_SET
int bufsize
Definition: regerror.c:388
#define ONIGERR_NUMBERED_BACKREF_OR_CALL_NOT_ALLOWED
int is_over
Definition: regerror.c:265
#define ONIGERR_INVALID_LOOK_BEHIND_PATTERN
#define xvsnprintf
Definition: regint.h:190
#define ONIGERR_END_PATTERN_AT_ESCAPE
va_end(args)
#define xmemcpy
Definition: regint.h:182
#define ONIGERR_EMPTY_CHAR_CLASS
#define ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE
#define ONIGERR_UNMATCHED_CLOSE_PARENTHESIS
va_dcl
Definition: tcltklib.c:844
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
#define ONIGERR_EMPTY_GROUP_NAME
#define ONIGERR_TYPE_BUG
#define ONIGERR_INVALID_BACKREF
#define ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID
#define ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
static int to_ascii(OnigEncoding enc, UChar *s, UChar *end, UChar buf[], int buf_size, int *is_over)
Definition: regerror.c:200
UChar * pat_end
Definition: regerror.c:391
va_list vargs
Definition: regerror.c:267
#define ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
#define const
Definition: strftime.c:102
register char * s
Definition: os2.c:56
#define ONIGERR_END_PATTERN_IN_GROUP
#define MAX_ERROR_PAR_LEN
Definition: regerror.c:250
#define ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
OnigEncoding enc
Definition: ripper.y:634
#define ONIGERR_END_PATTERN_AT_LEFT_BRACKET
#define ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
#define ONIG_NO_SUPPORT_CONFIG
#define ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL
#define ONIGERR_UNDEFINED_GROUP_REFERENCE
#define ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE
int onigenc_str_bytelen_null(OnigEncoding enc, const UChar *s)
Definition: regenc.c:163
#define ONIGERR_INVALID_REPEAT_RANGE_PATTERN
#define MIN(a, b)
Definition: regint.h:275
#define ONIGERR_TOO_BIG_BACKREF_NUMBER
gz end
Definition: zlib.c:2270
#define ONIGERR_INVALID_GROUP_NAME
#define va_init_list(a, b)
Definition: tcltklib.c:61
#define ONIG_MISMATCH
#define ONIGENC_IS_CODE_PRINT(enc, code)
#define ONIGENC_MBC_TO_CODE(enc, p, end)
#define ONIGERR_END_PATTERN_AT_LEFT_BRACE
UChar parbuf[MAX_ERROR_PAR_LEN]
Definition: regerror.c:266
#define ONIGERR_OVER_THREAD_PASS_LIMIT_COUNT
onig_vsnprintf_with_pattern(buf, bufsize, enc, pat, pat_end, fmt, args)
#define ONIGERR_MEMORY
#define ONIGERR_MULTIPLEX_DEFINED_NAME
#define ONIGERR_CHAR_CLASS_VALUE_AT_START_OF_RANGE
#define ONIGERR_UNEXPECTED_BYTECODE
#define ONIGERR_UNDEFINED_NAME_REFERENCE
#define ONIGERR_END_PATTERN_AT_META
#define ONIGERR_SPECIFIED_ENCODING_CANT_CONVERT_TO_WIDE_CHAR
#define ONIGERR_INVALID_CODE_POINT_VALUE
#define ONIGERR_INVALID_CHAR_IN_GROUP_NAME
#define ONIGERR_META_CODE_SYNTAX
#define ONIGERR_TOO_MANY_MULTI_BYTE_RANGES
BDIGIT v
Definition: bigdecimal.c:5656
q
Definition: tcltklib.c:2968
#define ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE
static void sprint_byte_with_x(char *s, unsigned int v)
Definition: regerror.c:195
#define bp()
Definition: vm_debug.h:27
#define ONIGERR_UNDEFINED_BYTECODE
#define ONIGERR_NESTED_REPEAT_OPERATOR
#define ONIGERR_INVALID_CHAR_PROPERTY_NAME
ptrdiff_t OnigPosition
Definition: ripper.y:118
#define ONIGERR_PARSER_BUG
size_t len
Definition: tcltklib.c:3568