1 /*
2   regerror.c - POSIX regerror() implementation for TRE.
3 
4   Copyright (c) 2001-2006 Ville Laurikari <vl@iki.fi>.
5 
6   This library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10 
11   This library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15 
16   You should have received a copy of the GNU Lesser General Public
17   License along with this library; if not, write to the Free Software
18   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 
20 */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif /* HAVE_CONFIG_H */
25 
26 #include <string.h>
27 #ifdef HAVE_WCHAR_H
28 #include <wchar.h>
29 #endif /* HAVE_WCHAR_H */
30 #ifdef HAVE_WCTYPE_H
31 #include <wctype.h>
32 #endif /* HAVE_WCTYPE_H */
33 
34 #include "tre-internal.h"
35 #include "regex.h"
36 #include "gettext.h"
37 #define _(String) dgettext(PACKAGE, String)
38 #define gettext_noop(String) String
39 
40 /* Error message strings for error codes listed in `regex.h'.  This list
41    needs to be in sync with the codes listed there, naturally. */
42 static const char *tre_error_messages[] =
43   { gettext_noop("No error"),				 /* REG_OK */
44     gettext_noop("No match"),				 /* REG_NOMATCH */
45     gettext_noop("Invalid regexp"),			 /* REG_BADPAT */
46     gettext_noop("Unknown collating element"),		 /* REG_ECOLLATE */
47     gettext_noop("Unknown character class name"),	 /* REG_ECTYPE */
48     gettext_noop("Trailing backslash"),			 /* REG_EESCAPE */
49     gettext_noop("Invalid back reference"),		 /* REG_ESUBREG */
50     gettext_noop("Missing ']'"),			 /* REG_EBRACK */
51     gettext_noop("Missing ')'"),			 /* REG_EPAREN */
52     gettext_noop("Missing '}'"),			 /* REG_EBRACE */
53     gettext_noop("Invalid contents of {}"),		 /* REG_BADBR */
54     gettext_noop("Invalid character range"),		 /* REG_ERANGE */
55     gettext_noop("Out of memory"),			 /* REG_ESPACE */
56     gettext_noop("Invalid use of repetition operators")	 /* REG_BADRPT */
57   };
58 
59 size_t
regerror(int errcode,const regex_t * preg,char * errbuf,size_t errbuf_size)60 regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
61 {
62   const char *err;
63   size_t err_len;
64 
65   if (errcode >= 0
66       && errcode < (sizeof(tre_error_messages) / sizeof(*tre_error_messages)))
67     err = gettext(tre_error_messages[errcode]);
68   else
69     err = gettext("Unknown error");
70 
71   err_len = strlen(err) + 1;
72   if (errbuf_size > 0 && errbuf != NULL)
73     {
74       if (err_len > errbuf_size)
75 	{
76 	  strncpy(errbuf, err, errbuf_size - 1);
77 	  errbuf[errbuf_size - 1] = '\0';
78 	}
79       else
80 	{
81 	  strcpy(errbuf, err);
82 	}
83     }
84   return err_len;
85 }
86 
87 /* EOF */
88