xref: /original-bsd/lib/libc/regex/regerror.c (revision a6d8c59f)
1 /*-
2  * Copyright (c) 1992 Henry Spencer.
3  * Copyright (c) 1992 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Henry Spencer of the University of Toronto.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)regerror.c	5.3 (Berkeley) 09/30/92
12  */
13 
14 #if defined(LIBC_SCCS) && !defined(lint)
15 static char sccsid[] = "@(#)regerror.c	5.3 (Berkeley) 09/30/92";
16 #endif /* LIBC_SCCS and not lint */
17 
18 #include <sys/types.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <regex.h>
25 
26 #include "utils.h"
27 
28 static struct rerr {
29 	int code;
30 	char *name;
31 	char *explain;
32 } rerrs[] = {
33 	REG_NOMATCH,	"NOMATCH",	"regexec() failed to match",
34 	REG_BADPAT,	"BADPAT",	"invalid regular expression",
35 	REG_ECOLLATE,	"ECOLLATE",	"invalid collating element",
36 	REG_ECTYPE,	"ECTYPE",	"invalid character class",
37 	REG_EESCAPE,	"EESCAPE",	"trailing backslash (\\)",
38 	REG_ESUBREG,	"ESUBREG",	"invalid backreference number",
39 	REG_EBRACK,	"EBRACK",	"brackets ([ ]) not balanced",
40 	REG_EPAREN,	"EPAREN",	"parentheses not balanced",
41 	REG_EBRACE,	"EBRACE",	"braces not balanced",
42 	REG_BADBR,	"BADBR",	"invalid repetition count(s)",
43 	REG_ERANGE,	"ERANGE",	"invalid character range",
44 	REG_ESPACE,	"ESPACE",	"out of memory",
45 	REG_BADRPT,	"BADRPT",	"repetition-operator operand invalid",
46 	REG_EMPTY,	"EMPTY",	"empty (sub)expression",
47 	REG_ASSERT,	"ASSERT",	"\"can't happen\" -- you found a bug",
48 	0,		"",		"*** unknown regexp error code ***",
49 };
50 
51 /*
52  - regerror - the interface to error numbers
53  */
54 /* ARGSUSED */
55 size_t
56 regerror(errcode, preg, errbuf, errbuf_size)
57 int errcode;
58 const regex_t *preg;
59 char *errbuf;
60 size_t errbuf_size;
61 {
62 	register struct rerr *r;
63 	register size_t len;
64 
65 	for (r = rerrs; r->code != 0; r++)
66 		if (r->code == errcode)
67 			break;
68 
69 	len = strlen(r->explain) + 1;
70 	if (errbuf_size > 0) {
71 		if (errbuf_size > len)
72 			(void) strcpy(errbuf, r->explain);
73 		else {
74 			(void) strncpy(errbuf, r->explain, errbuf_size-1);
75 			errbuf[errbuf_size-1] = '\0';
76 		}
77 	}
78 
79 	return(len);
80 }
81 
82 #ifdef REDEBUG
83 /*
84  - eprint - express an error number as a string
85  */
86 char *
87 eprint(eno)
88 int eno;
89 {
90 	register struct rerr *r;
91 	static char eval[10];
92 
93 	for (r = rerrs; r->code != 0; r++)
94 		if (r->code == eno)
95 			return(r->name);
96 	sprintf(eval, "#%d", r->code);
97 	return(eval);
98 }
99 
100 /*
101  - efind - find an error name
102  */
103 int
104 efind(ename)
105 char *ename;
106 {
107 	register struct rerr *r;
108 
109 	for (r = rerrs; r->code != 0; r++)
110 		if (strcmp(r->name, ename) == 0)
111 			return(r->code);
112 	return(0);		/* it'll do */
113 }
114 #endif
115