xref: /titanic_50/usr/src/lib/libc/port/locale/regerror.c (revision 0e8139c7ae64247b4d7b4a98fdae6557f5ba0b40)
1 /*
2  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
3  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
4  * Copyright (c) 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Henry Spencer.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include "lint.h"
36 #include "file64.h"
37 #include <sys/types.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <limits.h>
41 #include <stdlib.h>
42 #include <regex.h>
43 
44 #include "utils.h"
45 #include "../gen/_libc_gettext.h"
46 
47 #define	RERR(x, msg)	{ x, #x, msg }
48 
49 static struct rerr {
50 	int code;
51 	char *name;
52 	char *explain;
53 } rerrs[] = {
54 	RERR(REG_NOMATCH,	"regexec() failed to match"),
55 	RERR(REG_BADPAT,	"invalid regular expression"),
56 	RERR(REG_ECOLLATE,	"invalid collating element"),
57 	RERR(REG_ECTYPE,	"invalid character class"),
58 	RERR(REG_EESCAPE,	"trailing backslash (\\)"),
59 	RERR(REG_ESUBREG,	"invalid backreference number"),
60 	RERR(REG_EBRACK,	"brackets ([ ]) not balanced"),
61 	RERR(REG_EPAREN,	"parentheses not balanced"),
62 	RERR(REG_EBRACE,	"braces not balanced"),
63 	RERR(REG_BADBR,		"invalid repetition count(s)"),
64 	RERR(REG_ERANGE,	"invalid character range"),
65 	RERR(REG_ESPACE,	"out of memory"),
66 	RERR(REG_BADRPT,	"repetition-operator operand invalid"),
67 #ifdef	REG_EMPTY
68 	RERR(REG_EMPTY,		"empty (sub)expression"),
69 #endif
70 	RERR(REG_EFATAL,	"fatal internal error"),
71 #ifdef	REG_INVARG
72 	RERR(REG_INVARG,	"invalid argument to regex routine"),
73 #endif
74 	RERR(REG_ECHAR,		"illegal byte sequence"),
75 	RERR(REG_ENOSYS,	"function not supported"),
76 	RERR(REG_STACK,		"backtrack stack overflow"),
77 	RERR(REG_ENSUB,		"more than 9 \\( \\) pairs"),
78 	RERR(REG_ENEWLINE,	"\n found before end of pattern"),
79 	{0,	"",		"*** unknown regexp error code ***"}
80 };
81 
82 
83 /*
84  * regerror - the interface to error numbers
85  */
86 /* ARGSUSED */
87 size_t
regerror(int errcode,const regex_t * _RESTRICT_KYWD preg,char * _RESTRICT_KYWD errbuf,size_t errbuf_size)88 regerror(int errcode, const regex_t *_RESTRICT_KYWD preg,
89     char *_RESTRICT_KYWD errbuf, size_t errbuf_size)
90 {
91 	struct rerr *r;
92 	size_t len;
93 	char *s;
94 
95 	for (r = rerrs; r->code != 0; r++)
96 		if (r->code == errcode)
97 			break;
98 
99 	s = _libc_gettext(r->explain);
100 
101 	len = strlen(s) + 1;
102 	if (errbuf_size > 0) {
103 		if (errbuf_size > len)
104 			(void) strcpy(errbuf, s);
105 		else {
106 			(void) strncpy(errbuf, s, errbuf_size-1);
107 			errbuf[errbuf_size-1] = '\0';
108 		}
109 	}
110 
111 	return (len);
112 }
113