1 #include <sys/types.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <limits.h>
6 #include <stdlib.h>
7 /* local headers */
8 #include "regex.h"
9 #include "utils.h"
10 
11 /* #include "regerror.ih" // why generate this */
12 /* forward references */
13 static char *regatoi(const regex_t *preg, char *localbuf);
14 
15 /*
16  = #define	REG_OKAY	 0
17  = #define	REG_NOMATCH	 1
18  = #define	REG_BADPAT	 2
19  = #define	REG_ECOLLATE	 3
20  = #define	REG_ECTYPE	 4
21  = #define	REG_EESCAPE	 5
22  = #define	REG_ESUBREG	 6
23  = #define	REG_EBRACK	 7
24  = #define	REG_EPAREN	 8
25  = #define	REG_EBRACE	 9
26  = #define	REG_BADBR	10
27  = #define	REG_ERANGE	11
28  = #define	REG_ESPACE	12
29  = #define	REG_BADRPT	13
30  = #define	REG_EMPTY	14
31  = #define	REG_ASSERT	15
32  = #define	REG_INVARG	16
33  = #define	REG_ATOI	255	// convert name to number (!)
34  = #define	REG_ITOA	0400	// convert number to name (!)
35  */
36 static struct rerr {
37 	int code;
38 	char *name;
39 	char *explain;
40 } rerrs[] = {
41 	REG_OKAY,	"REG_OKAY",	"no errors detected",
42 	REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match",
43 	REG_BADPAT,	"REG_BADPAT",	"invalid regular expression",
44 	REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element",
45 	REG_ECTYPE,	"REG_ECTYPE",	"invalid character class",
46 	REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)",
47 	REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number",
48 	REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced",
49 	REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced",
50 	REG_EBRACE,	"REG_EBRACE",	"braces not balanced",
51 	REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)",
52 	REG_ERANGE,	"REG_ERANGE",	"invalid character range",
53 	REG_ESPACE,	"REG_ESPACE",	"out of memory",
54 	REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid",
55 	REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression",
56 	REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug",
57 	REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine",
58 	-1,		"",		"*** unknown regexp error code ***",
59 };
60 
61 /*
62  - regerror - the interface to error numbers
63  = extern size_t regerror(int, const regex_t *, char *, size_t);
64  */
65 /* ARGSUSED */
66 size_t
regerror(errcode,preg,errbuf,errbuf_size)67 regerror(errcode, preg, errbuf, errbuf_size)
68 int errcode;
69 const regex_t *preg;
70 char *errbuf;
71 size_t errbuf_size;
72 {
73 	register struct rerr *r;
74 	register size_t len;
75 	register int target = errcode &~ REG_ITOA;
76 	register char *s;
77 	char convbuf[50];
78 
79 	if (errcode == REG_ATOI)
80 		s = regatoi(preg, convbuf);
81 	else {
82 		for (r = rerrs; r->code >= 0; r++)
83 			if (r->code == target)
84 				break;
85 
86 		if (errcode&REG_ITOA) {
87 			if (r->code >= 0)
88 				(void) strcpy(convbuf, r->name);
89 			else
90 				sprintf(convbuf, "REG_0x%x", target);
91 			assert(strlen(convbuf) < sizeof(convbuf));
92 			s = convbuf;
93 		} else
94 			s = r->explain;
95 	}
96 
97 	len = strlen(s) + 1;
98 	if (errbuf_size > 0) {
99 		if (errbuf_size > len)
100 			(void) strcpy(errbuf, s);
101 		else {
102 			(void) strncpy(errbuf, s, errbuf_size-1);
103 			errbuf[errbuf_size-1] = '\0';
104 		}
105 	}
106 
107 	return(len);
108 }
109 
110 /*
111  - regatoi - internal routine to implement REG_ATOI
112  == static char *regatoi(const regex_t *preg, char *localbuf);
113  */
114 static char *
regatoi(preg,localbuf)115 regatoi(preg, localbuf)
116 const regex_t *preg;
117 char *localbuf;
118 {
119 	register struct rerr *r;
120 
121 	for (r = rerrs; r->code >= 0; r++)
122 		if (strcmp(r->name, preg->re_endp) == 0)
123 			break;
124 	if (r->code < 0)
125 		return("0");
126 
127 	sprintf(localbuf, "%d", r->code);
128 	return(localbuf);
129 }
130