]> git.cworth.org Git - vogl/blob - src/voglcore/regex/regerror.c
Initial vogl checkin
[vogl] / src / voglcore / regex / regerror.c
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 #ifndef D_FILE_OFFSET_BITS
28 #define _FILE_OFFSET_BITS 64
29 #endif
30 #ifndef _LARGEFILE64_SOURCE
31 #define _LARGEFILE64_SOURCE 1
32 #endif
33
34 #include <sys/types.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <limits.h>
39 #include <stdlib.h>
40
41 #include "regex.h"
42
43 #include "utils.h"
44 #include "regerror.ih"
45
46 /*
47  = #define      REG_OKAY         0
48  = #define      REG_NOMATCH      1
49  = #define      REG_BADPAT       2
50  = #define      REG_ECOLLATE     3
51  = #define      REG_ECTYPE       4
52  = #define      REG_EESCAPE      5
53  = #define      REG_ESUBREG      6
54  = #define      REG_EBRACK       7
55  = #define      REG_EPAREN       8
56  = #define      REG_EBRACE       9
57  = #define      REG_BADBR       10
58  = #define      REG_ERANGE      11
59  = #define      REG_ESPACE      12
60  = #define      REG_BADRPT      13
61  = #define      REG_EMPTY       14
62  = #define      REG_ASSERT      15
63  = #define      REG_INVARG      16
64  = #define      REG_ATOI        255     // convert name to number (!)
65  = #define      REG_ITOA        0400    // convert number to name (!)
66  */
67 static struct rerr
68 {
69     int code;
70     char *name;
71     char *explain;
72 } vogl_rerrs[] =
73       {
74           { REG_OKAY, "REG_OKAY", "no errors detected" },
75           { REG_NOMATCH, "REG_NOMATCH", "regexec() failed to match" },
76           { REG_BADPAT, "REG_BADPAT", "invalid regular expression" },
77           { REG_ECOLLATE, "REG_ECOLLATE", "invalid collating element" },
78           { REG_ECTYPE, "REG_ECTYPE", "invalid character class" },
79           { REG_EESCAPE, "REG_EESCAPE", "trailing backslash (\\)" },
80           { REG_ESUBREG, "REG_ESUBREG", "invalid backreference number" },
81           { REG_EBRACK, "REG_EBRACK", "brackets ([ ]) not balanced" },
82           { REG_EPAREN, "REG_EPAREN", "parentheses not balanced" },
83           { REG_EBRACE, "REG_EBRACE", "braces not balanced" },
84           { REG_BADBR, "REG_BADBR", "invalid repetition count(s)" },
85           { REG_ERANGE, "REG_ERANGE", "invalid character range" },
86           { REG_ESPACE, "REG_ESPACE", "out of memory" },
87           { REG_BADRPT, "REG_BADRPT", "repetition-operator operand invalid" },
88           { REG_EMPTY, "REG_EMPTY", "empty (sub)expression" },
89           { REG_ASSERT, "REG_ASSERT", "\"can't happen\" -- you found a bug" },
90           { REG_INVARG, "REG_INVARG", "invalid argument to regex routine" },
91           { -1, "", "*** unknown regexp error code ***" },
92       };
93
94 /*
95  - regerror - the interface to error numbers
96  = extern size_t regerror(int, const regex_t *, char *, size_t);
97  */
98 /* ARGSUSED */
99 size_t
100 vogl_regerror(errcode, preg, errbuf, errbuf_size) int errcode;
101 const regex_t *preg;
102 char *errbuf;
103 size_t errbuf_size;
104 {
105     register struct rerr *r;
106     register size_t len;
107     register int target = errcode & ~REG_ITOA;
108     register char *s;
109     char convbuf[50];
110
111     if (errcode == REG_ATOI)
112         s = vogl_regatoi(preg, convbuf);
113     else
114     {
115         for (r = vogl_rerrs; r->code >= 0; r++)
116             if (r->code == target)
117                 break;
118
119         if (errcode & REG_ITOA)
120         {
121             if (r->code >= 0)
122                 (void)strcpy(convbuf, r->name);
123             else
124                 sprintf(convbuf, "REG_0x%x", target);
125             assert(strlen(convbuf) < sizeof(convbuf));
126             s = convbuf;
127         }
128         else
129             s = r->explain;
130     }
131
132     len = strlen(s) + 1;
133     if (errbuf_size > 0)
134     {
135         if (errbuf_size > len)
136             (void)strcpy(errbuf, s);
137         else
138         {
139             (void)strncpy(errbuf, s, errbuf_size - 1);
140             errbuf[errbuf_size - 1] = '\0';
141         }
142     }
143
144     return (len);
145 }
146
147 /*
148  - vogl_regatoi - internal routine to implement REG_ATOI
149  == static char *vogl_regatoi(const regex_t *preg, char *localbuf);
150  */
151 static char *
152 vogl_regatoi(preg, localbuf)
153     const regex_t *preg;
154 char *localbuf;
155 {
156     register struct rerr *r;
157
158     for (r = vogl_rerrs; r->code >= 0; r++)
159         if (strcmp(r->name, preg->re_endp) == 0)
160             break;
161     if (r->code < 0)
162         return ("0");
163
164     sprintf(localbuf, "%d", r->code);
165     return (localbuf);
166 }