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