~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to regex/regerror.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

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