| 1 | /********************************************************************** |
| 2 | regposerr.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-2016 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 "config.h" |
| 32 | #include "onigmoposix.h" |
| 33 | #include <string.h> |
| 34 | |
| 35 | #if defined(__GNUC__) |
| 36 | # define ARG_UNUSED __attribute__ ((unused)) |
| 37 | #else |
| 38 | # define ARG_UNUSED |
| 39 | #endif |
| 40 | |
| 41 | #if defined(_WIN32) && !defined(__GNUC__) |
| 42 | # define xsnprintf sprintf_s |
| 43 | # define xstrncpy(dest,src,size) strncpy_s(dest,size,src,_TRUNCATE) |
| 44 | #else |
| 45 | # define xsnprintf snprintf |
| 46 | # define xstrncpy strncpy |
| 47 | #endif |
| 48 | |
| 49 | #define numberof(array) (int)(sizeof(array) / sizeof((array)[0])) |
| 50 | |
| 51 | static const char* ESTRING[] = { |
| 52 | NULL, |
| 53 | "failed to match", /* REG_NOMATCH */ |
| 54 | "Invalid regular expression", /* REG_BADPAT */ |
| 55 | "invalid collating element referenced", /* REG_ECOLLATE */ |
| 56 | "invalid character class type referenced", /* REG_ECTYPE */ |
| 57 | "bad backslash-escape sequence", /* REG_EESCAPE */ |
| 58 | "invalid back reference number", /* REG_ESUBREG */ |
| 59 | "imbalanced [ and ]", /* REG_EBRACK */ |
| 60 | "imbalanced ( and )", /* REG_EPAREN */ |
| 61 | "imbalanced { and }", /* REG_EBRACE */ |
| 62 | "invalid repeat range {n,m}", /* REG_BADBR */ |
| 63 | "invalid range", /* REG_ERANGE */ |
| 64 | "Out of memory", /* REG_ESPACE */ |
| 65 | "? * + not preceded by valid regular expression", /* REG_BADRPT */ |
| 66 | |
| 67 | /* Extended errors */ |
| 68 | "internal error", /* REG_EONIG_INTERNAL */ |
| 69 | "invalid wide char value", /* REG_EONIG_BADWC */ |
| 70 | "invalid argument", /* REG_EONIG_BADARG */ |
| 71 | }; |
| 72 | |
| 73 | #include <stdio.h> |
| 74 | |
| 75 | |
| 76 | extern size_t |
| 77 | regerror(int posix_ecode, const regex_t* reg ARG_UNUSED, char* buf, |
| 78 | size_t size) |
| 79 | { |
| 80 | const char* s; |
| 81 | char tbuf[35]; |
| 82 | size_t len; |
| 83 | |
| 84 | if (posix_ecode > 0 |
| 85 | && posix_ecode < numberof(ESTRING)) { |
| 86 | s = ESTRING[posix_ecode]; |
| 87 | } |
| 88 | else if (posix_ecode == 0) { |
| 89 | s = ""; |
| 90 | } |
| 91 | else { |
| 92 | xsnprintf(tbuf, sizeof(tbuf), "undefined error code (%d)", posix_ecode); |
| 93 | s = tbuf; |
| 94 | } |
| 95 | |
| 96 | len = strlen(s) + 1; /* use strlen() because s is ascii encoding. */ |
| 97 | |
| 98 | if (buf != NULL && size > 0) { |
| 99 | xstrncpy(buf, s, size - 1); |
| 100 | buf[size - 1] = '\0'; |
| 101 | } |
| 102 | return len; |
| 103 | } |