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