Blame src/regposerr.c

Packit b89d10
/**********************************************************************
Packit b89d10
  regposerr.c - Oniguruma (regular expression library)
Packit b89d10
**********************************************************************/
Packit b89d10
/*-
Packit b89d10
 * Copyright (c) 2002-2018  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
Packit b89d10
 * All rights reserved.
Packit b89d10
 *
Packit b89d10
 * Redistribution and use in source and binary forms, with or without
Packit b89d10
 * modification, are permitted provided that the following conditions
Packit b89d10
 * are met:
Packit b89d10
 * 1. Redistributions of source code must retain the above copyright
Packit b89d10
 *    notice, this list of conditions and the following disclaimer.
Packit b89d10
 * 2. Redistributions in binary form must reproduce the above copyright
Packit b89d10
 *    notice, this list of conditions and the following disclaimer in the
Packit b89d10
 *    documentation and/or other materials provided with the distribution.
Packit b89d10
 *
Packit b89d10
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
Packit b89d10
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit b89d10
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit b89d10
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
Packit b89d10
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit b89d10
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit b89d10
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit b89d10
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit b89d10
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit b89d10
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit b89d10
 * SUCH DAMAGE.
Packit b89d10
 */
Packit b89d10
Packit b89d10
/* Can't include regint.h etc.. for conflict of regex_t.
Packit b89d10
   Define ONIGURUMA_EXPORT here for onigposix.h.
Packit b89d10
 */
Packit b89d10
#ifndef ONIGURUMA_EXPORT
Packit b89d10
#define ONIGURUMA_EXPORT
Packit b89d10
#endif
Packit b89d10
Packit b89d10
#include "config.h"
Packit b89d10
#include "onigposix.h"
Packit b89d10
Packit b89d10
#ifdef HAVE_STRING_H
Packit b89d10
# include <string.h>
Packit b89d10
#else
Packit b89d10
# include <strings.h>
Packit b89d10
#endif
Packit b89d10
Packit b89d10
#if defined(__GNUC__)
Packit b89d10
#  define ARG_UNUSED  __attribute__ ((unused))
Packit b89d10
#else
Packit b89d10
#  define ARG_UNUSED
Packit b89d10
#endif
Packit b89d10
Packit b89d10
#if defined(_WIN32) && !defined(__GNUC__)
Packit b89d10
#define xsnprintf   sprintf_s
Packit b89d10
#define xstrncpy(dest,src,size)   strncpy_s(dest,size,src,_TRUNCATE)
Packit b89d10
#else
Packit b89d10
#define xsnprintf   snprintf
Packit b89d10
#define xstrncpy    strncpy
Packit b89d10
#endif
Packit b89d10
Packit b89d10
static char* ESTRING[] = {
Packit b89d10
  NULL,
Packit b89d10
  "failed to match",                         /* REG_NOMATCH    */
Packit b89d10
  "Invalid regular expression",              /* REG_BADPAT     */
Packit b89d10
  "invalid collating element referenced",    /* REG_ECOLLATE   */
Packit b89d10
  "invalid character class type referenced", /* REG_ECTYPE     */
Packit b89d10
  "bad backslash-escape sequence",           /* REG_EESCAPE    */
Packit b89d10
  "invalid back reference number",           /* REG_ESUBREG    */
Packit b89d10
  "imbalanced [ and ]",                      /* REG_EBRACK     */
Packit b89d10
  "imbalanced ( and )",                      /* REG_EPAREN     */
Packit b89d10
  "imbalanced { and }",                      /* REG_EBRACE     */
Packit b89d10
  "invalid repeat range {n,m}",              /* REG_BADBR      */
Packit b89d10
  "invalid range",                           /* REG_ERANGE     */
Packit b89d10
  "Out of memory",                           /* REG_ESPACE     */
Packit b89d10
  "? * + not preceded by valid regular expression", /* REG_BADRPT   */
Packit b89d10
Packit b89d10
  /* Extended errors */
Packit b89d10
  "internal error",                          /* REG_EONIG_INTERNAL */
Packit b89d10
  "invalid wide char value",                 /* REG_EONIG_BADWC    */
Packit b89d10
  "invalid argument"                         /* REG_EONIG_BADARG   */
Packit b89d10
};
Packit b89d10
Packit b89d10
#include <stdio.h>
Packit b89d10
Packit b89d10
Packit b89d10
extern size_t
Packit b89d10
regerror(int posix_ecode, const regex_t* reg ARG_UNUSED, char* buf,
Packit b89d10
	 size_t size)
Packit b89d10
{
Packit b89d10
  char* s;
Packit b89d10
  char tbuf[35];
Packit b89d10
  size_t len;
Packit b89d10
Packit b89d10
  if (posix_ecode > 0
Packit b89d10
      && posix_ecode < (int )(sizeof(ESTRING) / sizeof(ESTRING[0]))) {
Packit b89d10
    s = ESTRING[posix_ecode];
Packit b89d10
  }
Packit b89d10
  else if (posix_ecode == 0) {
Packit b89d10
    s = "";
Packit b89d10
  }
Packit b89d10
  else {
Packit b89d10
    xsnprintf(tbuf, sizeof(tbuf), "undefined error code (%d)", posix_ecode);
Packit b89d10
    s = tbuf;
Packit b89d10
  }
Packit b89d10
Packit b89d10
  len = strlen(s) + 1; /* use strlen() because s is ascii encoding. */
Packit b89d10
Packit b89d10
  if (buf != NULL && size > 0) {
Packit b89d10
    xstrncpy(buf, s, size - 1);
Packit b89d10
    buf[size - 1] = '\0';
Packit b89d10
  }
Packit b89d10
  return len;
Packit b89d10
}