1 /* 2 tre_regerror.c - POSIX tre_regerror() implementation for TRE. 3 4 This software is released under a BSD-style license. 5 See the file LICENSE for details and copyright. 6 7 */ 8 9 #ifdef HAVE_CONFIG_H 10 #include <config.h> 11 #endif /* HAVE_CONFIG_H */ 12 13 #include <string.h> 14 #ifdef HAVE_WCHAR_H 15 #include <wchar.h> 16 #endif /* HAVE_WCHAR_H */ 17 #ifdef HAVE_WCTYPE_H 18 #include <wctype.h> 19 #endif /* HAVE_WCTYPE_H */ 20 21 #include "tre-internal.h" 22 #include "tre.h" 23 24 #ifdef __weak_alias 25 __weak_alias(regerror,_regerror) 26 #endif 27 28 #ifdef HAVE_GETTEXT 29 #include <libintl.h> 30 #else 31 #define dgettext(p, s) s 32 #define gettext(s) s 33 #endif 34 35 #define _(String) dgettext(PACKAGE, String) 36 #define gettext_noop(String) String 37 38 /* Error message strings for error codes listed in `tre.h'. This list 39 needs to be in sync with the codes listed there, naturally. */ 40 static const char *tre_error_messages[] = 41 { gettext_noop("No error"), /* REG_OK */ 42 gettext_noop("No match"), /* REG_NOMATCH */ 43 gettext_noop("Invalid regexp"), /* REG_BADPAT */ 44 gettext_noop("Unknown collating element"), /* REG_ECOLLATE */ 45 gettext_noop("Unknown character class name"), /* REG_ECTYPE */ 46 gettext_noop("Trailing backslash"), /* REG_EESCAPE */ 47 gettext_noop("Invalid back reference"), /* REG_ESUBREG */ 48 gettext_noop("Missing ']'"), /* REG_EBRACK */ 49 gettext_noop("Missing ')'"), /* REG_EPAREN */ 50 gettext_noop("Missing '}'"), /* REG_EBRACE */ 51 gettext_noop("Invalid contents of {}"), /* REG_BADBR */ 52 gettext_noop("Invalid character range"), /* REG_ERANGE */ 53 gettext_noop("Out of memory"), /* REG_ESPACE */ 54 gettext_noop("Invalid use of repetition operators") /* REG_BADRPT */ 55 }; 56 57 size_t 58 tre_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) 59 { 60 const char *err; 61 size_t err_len; 62 63 /*LINTED*/(void)&preg; 64 if (errcode >= 0 65 && errcode < (int)(sizeof(tre_error_messages) 66 / 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