mba/msgno.c

filter: Statementstmt 0.0% 0 / 119 Branchbran 0.0% 0 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 169 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary mba/suba.c »
1 /* msgno - managing error codes and associated messages across
2 * separate C libraries
3 * Copyright (c) 2001 Michael B. Allen <mba2000 ioplex.com>
4 *
5 * The MIT License
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in 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
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25  
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <errno.h>
31  
32 #include "mba/msgno.h"
33  
34 #ifndef MSGNO_NUM_LISTS
35 #define MSGNO_NUM_LISTS 16
36 #endif
37 #ifndef MSGNO_BUFSIZ
38 #define MSGNO_BUFSIZ 1024
39 #endif
40  
41 struct msgno_entry msgno_builtin_codes[2] = {
42 { (1 << 16), "A parameter was NULL" },
43 { 0, NULL }
44 };
45  
46 static struct tbl_entry {
47 struct msgno_entry *list;
48 unsigned int num_msgs;
49 } list_tbl[MSGNO_NUM_LISTS] = {
50 { msgno_builtin_codes, 1 }
51 };
52  
53 static unsigned int next_tbl_idx = 1;
54  
55 int msgno_buf_idx = 0;
56 char msgno_buf[MSGNO_BUFSIZ] = { 0 };
57  
58 int
59 0 msgno_add_codes(struct msgno_entry *list)
60 {
61 struct tbl_entry *te;
62 0 int next_msgno = 0, hi_bits;
63  
64 0 ▶ if (list == NULL || list->msg == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (list == NULL || list->msg == NULL) { 0 0 0
if (list == NULL || list->msg == NULL) { 0 0 0
65 0 errno = EINVAL;
66 0 return -1;
67 }
68 0 ▶ if (next_tbl_idx == MSGNO_NUM_LISTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (next_tbl_idx == MSGNO_NUM_LISTS) { 0 0 0
69 0 errno = ERANGE;
70 0 return -1;
71 }
72  
73 0 ▶ for (te = list_tbl + 1; te->list; te++) {
Branch: 1 branch
BranchTrueFalseTotal
for (te = list_tbl + 1; te->list; te++) { 0 0 0
74 0 ▶ if (te->list == list) {
Branch: 1 branch
BranchTrueFalseTotal
if (te->list == list) { 0 0 0
75 0 return 0; /* already in list_tbl */
76 }
77 }
78  
79 0 hi_bits = (next_tbl_idx + 1) << 16;
80 0 te->list = list;
81 0 ▶ while (list->msg) {
Branch: 1 branch
BranchTrueFalseTotal
while (list->msg) { 0 0 0
82 0 ▶ if ((list->msgno & 0xFFFF0000)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((list->msgno & 0xFFFF0000)) { 0 0 0
83 0 te->list = NULL;
84 0 errno = ERANGE;
85 0 return -1;
86 }
87 0 ▶ if (list->msgno == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (list->msgno == 0) { 0 0 0
88 0 list->msgno = hi_bits | next_msgno++;
89 0 ▶ } else if (list->msgno >= next_msgno) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (list->msgno >= next_msgno) { 0 0 0
90 0 next_msgno = list->msgno + 1;
91 0 list->msgno = hi_bits | list->msgno;
92 } else {
93 0 te->list = NULL;
94 0 errno = ERANGE;
95 0 return -1;
96 }
97 0 te->num_msgs++;
98 0 list++;
99 }
100 0 next_tbl_idx++;
101  
102 0 return 0;
103 }
104  
105 const char *
106 0 msgno_msg(int msgno)
107 {
108 struct tbl_entry *te;
109 unsigned int i;
110  
111 0 i = msgno >> 16;
112 0 ▶ if (i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) { 0 0 0
113 0 return strerror(msgno);
114 0 ▶ } else if (i >= MSGNO_NUM_LISTS || (te = list_tbl + (i - 1)) == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (i >= MSGNO_NUM_LISTS || (te = list_tbl + (i - 1)) == NULL) { 0 0 0
} else if (i >= MSGNO_NUM_LISTS || (te = list_tbl + (i - 1)) == NULL) { 0 0 0
115 0 return "No such msgno list";
116 }
117  
118 0 ▶ for (i = 0; i < te->num_msgs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < te->num_msgs; i++) { 0 0 0
119 0 ▶ if (te->list[i].msgno == msgno) {
Branch: 1 branch
BranchTrueFalseTotal
if (te->list[i].msgno == msgno) { 0 0 0
120 0 return te->list[i].msg;
121 }
122 }
123  
124 0 return "No such message in msgno list";
125 }
126  
127 int
128 0 msgno_hdlr_stderr(const char *fmt, ...)
129 {
130 va_list ap;
131 0 va_start(ap, fmt);
132 0 vfprintf(stderr, fmt, ap);
133 0 va_end(ap);
134 0 fputc('\n', stderr);
135 0 fflush(stderr);
136 0 return 0;
137 }
138  
139 int (*msgno_hdlr)(const char *fmt, ...) = msgno_hdlr_stderr;
140  
141 int
142 0 msgno_append(const char *src, int n)
143 {
144 0 unsigned char *dst = (unsigned char *)msgno_buf + msgno_buf_idx;
145 0 unsigned char *dlim = (unsigned char *)msgno_buf + MSGNO_BUFSIZ;
146 0 unsigned char *start = dst;
147  
148 0 ▶ if (src == NULL || n < 1 || dst >= dlim) {
Branch: 3 branches
BranchTrueFalseTotal
if (src == NULL || n < 1 || dst >= dlim) { 0 0 0
if (src == NULL || n < 1 || dst >= dlim) { 0 0 0
if (src == NULL || n < 1 || dst >= dlim) { 0 0 0
149 0 return 0;
150 }
151 0 ▶ while (n-- && *src) {
Branch: 2 branches
BranchTrueFalseTotal
while (n-- && *src) { 0 0 0
while (n-- && *src) { 0 0 0
152 0 *dst++ = *src++;
153 0 ▶ if (dst == dlim) {
Branch: 1 branch
BranchTrueFalseTotal
if (dst == dlim) { 0 0 0
154 0 dst--;
155 0 break;
156 }
157 }
158 0 *dst = '\0';
159 0 msgno_buf_idx += dst - start;
160  
161 0 return dst - start;
162 }
163 int
164 0 msgno_vsprintf(const char *fmt, va_list ap)
165 {
166 0 size_t size = MSGNO_BUFSIZ - msgno_buf_idx;
167 int n;
168  
169 #if (__STDC_VERSION__ >= 199901L)
170 0 ▶ if ((n = vsnprintf(msgno_buf + msgno_buf_idx, size, fmt, ap)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if ((n = vsnprintf(msgno_buf + msgno_buf_idx, size, fmt, ap)) < 0 || 0 0 0
171 #else
172 if ((n = vsprintf(msgno_buf + msgno_buf_idx, fmt, ap)) < 0 ||
173 #endif
174 0 ▶ (size_t)n >= size || msgno_buf_idx > MSGNO_BUFSIZ) {
Branch: 2 branches
BranchTrueFalseTotal
(size_t)n >= size || msgno_buf_idx > MSGNO_BUFSIZ) { 0 0 0
(size_t)n >= size || msgno_buf_idx > MSGNO_BUFSIZ) { 0 0 0
175 0 *msgno_buf = '\0';
176 0 msgno_buf_idx = 0;
177 0 n = msgno_append("vsnprintf error", 15);
178 }
179 0 msgno_buf_idx += n;
180  
181 0 return n;
182 }
183 int
184 0 msgno_loc0(const char *loc0, const char *loc1)
185 {
186 0 ▶ if (*loc0 == '!') {
Branch: 1 branch
BranchTrueFalseTotal
if (*loc0 == '!') { 0 0 0
187 0 loc0++;
188 0 *msgno_buf = '\0';
189 0 msgno_buf_idx = 0;
190 0 ▶ } else if (*msgno_buf) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*msgno_buf) { 0 0 0
191 0 msgno_buf[msgno_buf_idx++] = ' ';
192 0 msgno_buf[msgno_buf_idx++] = ' ';
193 }
194 0 return msgno_append(loc0, 128) + msgno_append(loc1, 128) + msgno_append(": ", 2);
195 }
196 int
197 0 msgno_mmsg0(const char *fmt, ...)
198 {
199 va_list ap;
200  
201 0 va_start(ap, fmt);
202 0 msgno_vsprintf(fmt, ap);
203 0 msgno_hdlr("%s", msgno_buf);
204 0 *msgno_buf = '\0';
205 0 msgno_buf_idx = 0;
206 0 va_end(ap);
207  
208 0 return 0;
209 }
210 int
211 0 msgno_mmno0(int msgno)
212 {
213 0 msgno_append(msgno_msg(msgno), 255);
214 0 msgno_hdlr(msgno_buf);
215 0 *msgno_buf = '\0';
216 0 msgno_buf_idx = 0;
217  
218 0 return 0;
219 }
220 int
221 0 msgno_mmnf0(int msgno, const char *fmt, ...)
222 {
223 va_list ap;
224  
225 0 msgno_append(msgno_msg(msgno), 255);
226 0 va_start(ap, fmt);
227 0 msgno_vsprintf(fmt, ap);
228 0 msgno_hdlr("%s", msgno_buf);
229 0 *msgno_buf = '\0';
230 0 msgno_buf_idx = 0;
231 0 va_end(ap);
232  
233 0 return 0;
234 }
235 int
236 0 msgno_amsg0(const char *fmt, ...)
237 {
238 va_list ap;
239  
240 0 va_start(ap, fmt);
241 0 msgno_vsprintf(fmt, ap);
242 0 msgno_buf[msgno_buf_idx++] = '\n';
243 0 va_end(ap);
244  
245 0 return 0;
246 }
247 int
248 0 msgno_amno0(int msgno)
249 {
250 0 msgno_append(msgno_msg(msgno), 255);
251 0 msgno_buf[msgno_buf_idx++] = '\n';
252 0 return 0;
253 }
254 int
255 0 msgno_amnf0(int msgno, const char *fmt, ...)
256 {
257 va_list ap;
258  
259 0 msgno_append(msgno_msg(msgno), 255);
260 0 va_start(ap, fmt);
261 0 msgno_vsprintf(fmt, ap);
262 0 msgno_buf[msgno_buf_idx++] = '\n';
263 0 va_end(ap);
264  
265 0 return 0;
266 }
267  
↑ Summary mba/suba.c »