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