4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 1992, 1993, 1994 Henry Spencer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 1992, 1993, 1994
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * The Regents of the University of California. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This code is derived from software contributed to Berkeley by
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Henry Spencer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Redistribution and use in source and binary forms, with or without
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * modification, are permitted provided that the following conditions
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * are met:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * documentation and/or other materials provided with the distribution.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 4. Neither the name of the University nor the names of its contributors
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * may be used to endorse or promote products derived from this software
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * without specific prior written permission.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * SUCH DAMAGE.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "lint.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "file64.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <sys/types.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <stdio.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <string.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <limits.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <stdlib.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <regex.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "utils.h"
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore#include "../gen/_libc_gettext.h"
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore#define RERR(x, msg) { x, #x, msg }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorestatic struct rerr {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore int code;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore char *name;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore char *explain;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore} rerrs[] = {
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_NOMATCH, "regexec() failed to match"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_BADPAT, "invalid regular expression"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_ECOLLATE, "invalid collating element"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_ECTYPE, "invalid character class"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_EESCAPE, "trailing backslash (\\)"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_ESUBREG, "invalid backreference number"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_EBRACK, "brackets ([ ]) not balanced"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_EPAREN, "parentheses not balanced"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_EBRACE, "braces not balanced"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_BADBR, "invalid repetition count(s)"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_ERANGE, "invalid character range"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_ESPACE, "out of memory"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_BADRPT, "repetition-operator operand invalid"),
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#ifdef REG_EMPTY
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_EMPTY, "empty (sub)expression"),
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#endif
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_EFATAL, "fatal internal error"),
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#ifdef REG_INVARG
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_INVARG, "invalid argument to regex routine"),
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#endif
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_ECHAR, "illegal byte sequence"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_ENOSYS, "function not supported"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_STACK, "backtrack stack overflow"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_ENSUB, "more than 9 \\( \\) pairs"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore RERR(REG_ENEWLINE, "\n found before end of pattern"),
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore {0, "", "*** unknown regexp error code ***"}
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore};
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * regerror - the interface to error numbers
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/* ARGSUSED */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amoresize_t
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amoreregerror(int errcode, const regex_t *_RESTRICT_KYWD preg,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore char *_RESTRICT_KYWD errbuf, size_t errbuf_size)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore struct rerr *r;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore size_t len;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore char *s;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore for (r = rerrs; r->code != 0; r++)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (r->code == errcode)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40Garrett D'Amore s = _libc_gettext(r->explain);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore len = strlen(s) + 1;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (errbuf_size > 0) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (errbuf_size > len)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore (void) strcpy(errbuf, s);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore else {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore (void) strncpy(errbuf, s, errbuf_size-1);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore errbuf[errbuf_size-1] = '\0';
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (len);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}