Commit | Line | Data |
---|---|---|
b64eb60f MW |
1 | /* -*-c-*- |
2 | * | |
3 | * Types for the test-vector framework | |
4 | * | |
5 | * (c) 2023 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of the mLib utilities library. | |
11 | * | |
12 | * mLib is free software: you can redistribute it and/or modify it under | |
13 | * the terms of the GNU Library General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or (at | |
15 | * your option) any later version. | |
16 | * | |
17 | * mLib is distributed in the hope that it will be useful, but WITHOUT | |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | |
20 | * License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Library General Public | |
23 | * License along with mLib. If not, write to the Free Software | |
24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
25 | * USA. | |
26 | */ | |
27 | ||
28 | /*----- Header files ------------------------------------------------------*/ | |
29 | ||
30 | #include <assert.h> | |
31 | #include <ctype.h> | |
32 | #include <errno.h> | |
e63124bc | 33 | #include <float.h> |
b64eb60f | 34 | #include <limits.h> |
e63124bc | 35 | #include <math.h> |
b64eb60f MW |
36 | #include <stdio.h> |
37 | #include <string.h> | |
38 | ||
39 | #include "buf.h" | |
40 | #include "codec.h" | |
41 | # include "base32.h" | |
42 | # include "base64.h" | |
43 | # include "hex.h" | |
44 | #include "dstr.h" | |
67b5031e | 45 | #include "maths.h" |
b64eb60f MW |
46 | #include "tvec.h" |
47 | ||
48 | /*----- Preliminary utilities ---------------------------------------------*/ | |
49 | ||
67b5031e MW |
50 | /* --- @trivial_release@ --- * |
51 | * | |
52 | * Arguments: @union tvec_regval *rv@ = a register value | |
53 | * @const struct tvec_regdef@ = the register definition | |
54 | * | |
55 | * Returns: --- | |
56 | * | |
57 | * Use: Does nothing. Used for register values which don't retain | |
58 | * resources. | |
59 | */ | |
3efcfd2d MW |
60 | |
61 | static void trivial_release(union tvec_regval *rv, | |
62 | const struct tvec_regdef *rd) | |
63 | { ; } | |
64 | ||
67b5031e MW |
65 | /*----- Integer utilities -------------------------------------------------*/ |
66 | ||
67 | /* --- @unsigned_to_buf@, @signed_to_buf@ --- * | |
68 | * | |
69 | * Arguments: @buf *b@ = buffer to write on | |
70 | * @unsigned long u@ or @long i@ = integer to write | |
71 | * | |
72 | * Returns: Zero on success, @-1@ on failure. | |
73 | * | |
74 | * Use: Write @i@ to the buffer, in big-endian (two's-complement, it | |
75 | * signed) format. | |
76 | */ | |
77 | ||
78 | static int unsigned_to_buf(buf *b, unsigned long u) | |
79 | { kludge64 k; ASSIGN64(k, u); return (buf_putk64l(b, k)); } | |
80 | ||
b64eb60f MW |
81 | static int signed_to_buf(buf *b, long i) |
82 | { | |
83 | kludge64 k; | |
84 | unsigned long u; | |
85 | ||
86 | u = i; | |
87 | if (i >= 0) ASSIGN64(k, u); | |
88 | else { ASSIGN64(k, ~u); CPL64(k, k); } | |
89 | return (buf_putk64l(b, k)); | |
90 | } | |
91 | ||
67b5031e MW |
92 | /* --- @unsigned_from_buf@, @signed_from_buf@ --- * |
93 | * | |
94 | * Arguments: @buf *b@ = buffer to write on | |
95 | * @unsigned long *u_out@ or @long *i_out@ = where to put the | |
96 | * result | |
97 | * | |
98 | * Returns: Zero on success, @-1@ on failure. | |
99 | * | |
100 | * Use: Read an integer, in big-endian (two's-complement, if signed) | |
101 | * format, from the buffer. | |
102 | */ | |
b64eb60f MW |
103 | |
104 | static int unsigned_from_buf(buf *b, unsigned long *u_out) | |
105 | { | |
106 | kludge64 k, ulmax; | |
107 | ||
108 | ASSIGN64(ulmax, ULONG_MAX); | |
109 | if (buf_getk64l(b, &k)) return (-1); | |
110 | if (CMP64(k, >, ulmax)) return (-1); | |
111 | *u_out = GET64(unsigned long, k); return (0); | |
112 | } | |
113 | ||
67b5031e MW |
114 | /* --- @hex_width@ --- * |
115 | * | |
116 | * Arguments: @unsigned long u@ = an integer | |
117 | * | |
118 | * Returns: A suitable number of digits to use in order to display @u@ in | |
119 | * hex. Currently, we select a power of two sufficient to show | |
120 | * the value, but at least 2. | |
121 | */ | |
122 | ||
b64eb60f MW |
123 | static int hex_width(unsigned long u) |
124 | { | |
125 | int wd; | |
126 | unsigned long t; | |
127 | ||
128 | for (t = u >> 4, wd = 4; t >>= wd, wd *= 2, t; ); | |
129 | return (wd/4); | |
130 | } | |
131 | ||
67b5031e MW |
132 | /* --- @format_unsigned_hex@, @format_signed_hex@ --- * |
133 | * | |
134 | * Arguments: @const struct gprintf_ops *gops@ = print operations | |
135 | * @void *go@ = print destination | |
136 | * @unsigned long u@ or @long i@ = integer to print | |
137 | * | |
138 | * Returns: --- | |
139 | * | |
140 | * Use: Print an unsigned or signed integer in hexadecimal. | |
141 | */ | |
142 | ||
143 | static void format_unsigned_hex(const struct gprintf_ops *gops, void *go, | |
144 | unsigned long u) | |
145 | { gprintf(gops, go, "0x%0*lx", hex_width(u), u); } | |
146 | ||
147 | static void format_signed_hex(const struct gprintf_ops *gops, void *go, | |
148 | long i) | |
149 | { | |
150 | unsigned long u = i >= 0 ? i : -(unsigned long)i; | |
151 | gprintf(gops, go, "%s0x%0*lx", i < 0 ? "-" : "", hex_width(u), u); | |
152 | } | |
153 | ||
154 | static int signed_from_buf(buf *b, long *i_out) | |
155 | { | |
156 | kludge64 k, lmax, not_lmin; | |
157 | ||
158 | ASSIGN64(lmax, LONG_MAX); ASSIGN64(not_lmin, ~(unsigned long)LONG_MIN); | |
159 | if (buf_getk64l(b, &k)) return (-1); | |
160 | if (CMP64(k, <=, lmax)) *i_out = (long)GET64(unsigned long, k); | |
161 | else { | |
162 | CPL64(k, k); | |
163 | if (CMP64(k, <=, not_lmin)) *i_out = -(long)GET64(unsigned long, k) - 1; | |
164 | else return (-1); | |
165 | } | |
166 | return (0); | |
167 | } | |
168 | ||
169 | /* --- @check_unsigned_range@, @check_signed_range@ --- * | |
170 | * | |
171 | * Arguments: @unsigned long u@ or @long i@ = an integer | |
172 | * @const struct tvec_urange *ur@ or | |
173 | * @const struct tvec_irange *ir@ = range specification, | |
174 | * or null | |
175 | * @struct tvec_state *tv@ = test vector state | |
176 | * | |
177 | * Returns: Zero on success, or @-1@ on error. | |
178 | * | |
179 | * Use: Check that the integer is within bounds. If not, report a | |
180 | * suitable error and return a failure indication. | |
181 | */ | |
182 | ||
882a39c1 MW |
183 | static int check_signed_range(long i, |
184 | const struct tvec_irange *ir, | |
185 | struct tvec_state *tv) | |
b64eb60f | 186 | { |
882a39c1 | 187 | if (ir && (ir->min > i || i > ir->max)) { |
b64eb60f MW |
188 | tvec_error(tv, "integer %ld out of range (must be in [%ld .. %ld])", |
189 | i, ir->min, ir->max); | |
882a39c1 MW |
190 | return (-1); |
191 | } | |
192 | return (0); | |
b64eb60f MW |
193 | } |
194 | ||
882a39c1 MW |
195 | static int check_unsigned_range(unsigned long u, |
196 | const struct tvec_urange *ur, | |
197 | struct tvec_state *tv) | |
b64eb60f | 198 | { |
882a39c1 | 199 | if (ur && (ur->min > u || u > ur->max)) { |
b64eb60f MW |
200 | tvec_error(tv, "integer %lu out of range (must be in [%lu .. %lu])", |
201 | u, ur->min, ur->max); | |
882a39c1 MW |
202 | return (-1); |
203 | } | |
204 | return (0); | |
b64eb60f MW |
205 | } |
206 | ||
67b5031e MW |
207 | /* --- @chtodig@ --- * |
208 | * | |
209 | * Arguments: @int ch@ = a character | |
210 | * | |
211 | * Returns: The numeric value of the character as a digit, or @-1@ if | |
212 | * it's not a digit. Letters count as extended digits starting | |
213 | * with value 10; case is not significant. | |
214 | */ | |
215 | ||
3efcfd2d MW |
216 | static int chtodig(int ch) |
217 | { | |
218 | if ('0' <= ch && ch <= '9') return (ch - '0'); | |
219 | else if ('a' <= ch && ch <= 'z') return (ch - 'a' + 10); | |
220 | else if ('A' <= ch && ch <= 'Z') return (ch - 'A' + 10); | |
221 | else return (-1); | |
222 | } | |
223 | ||
67b5031e MW |
224 | /* --- @parse_unsigned_integer@, @parse_signed_integer@ --- * |
225 | * | |
226 | * Arguments: @unsigned long *u_out@, @long *i_out@ = where to put the | |
227 | * result | |
228 | * @const char **q_out@ = where to put the end position | |
229 | * @const char *p@ = pointer to the string to parse | |
230 | * | |
231 | * Returns: Zero on success, @-1@ on error. | |
232 | * | |
233 | * Use: Parse an integer from a string in the test-vector format. | |
234 | * This is mostly extension of the traditional C @strtoul@ | |
235 | * format: supported inputs include: | |
236 | * | |
237 | * * NNN -- a decimal number (even if it starts with `0'); | |
238 | * * 0xNNN -- hexadecimal; | |
239 | * * 0oNNN -- octal; | |
240 | * * 0bNNN -- binary; | |
241 | * * NNrNNN -- base NN. | |
242 | * | |
243 | * Furthermore, single underscores are permitted internally as | |
244 | * an insignificant digit separator. | |
245 | */ | |
246 | ||
3efcfd2d MW |
247 | static int parse_unsigned_integer(unsigned long *u_out, const char **q_out, |
248 | const char *p) | |
249 | { | |
250 | unsigned long u; | |
251 | int ch, d, r; | |
252 | const char *q; | |
253 | unsigned f = 0; | |
67b5031e MW |
254 | #define f_implicit 1u /* implicitly reading base 10 */ |
255 | #define f_digit 2u /* read a real digit */ | |
256 | #define f_uscore 4u /* found an underscore */ | |
257 | ||
258 | /* Initial setup | |
259 | * | |
260 | * This will deal with the traditional `0[box]...' prefixes. We'll leave | |
261 | * our new `NNr...' syntax for later. | |
262 | */ | |
3efcfd2d MW |
263 | if (p[0] != '0' || !p[1]) { |
264 | d = chtodig(*p); if (0 > d || d >= 10) return (-1); | |
265 | r = 10; u = d; p++; f |= f_implicit | f_digit; | |
266 | } else { | |
267 | u = 0; d = chtodig(p[2]); | |
268 | if (d < 0) { r = 10; f |= f_implicit | f_digit; p++; } | |
269 | else if ((p[1] == 'x' || p[1] == 'X') && d < 16) { r = 16; p += 2; } | |
270 | else if ((p[1] == 'o' || p[1] == 'O') && d < 8) { r = 8; p += 2; } | |
271 | else if ((p[1] == 'b' || p[1] == 'B') && d < 2) { r = 2; p += 2; } | |
272 | else { r = 10; f |= f_digit; p++; } | |
273 | } | |
274 | ||
275 | q = p; | |
276 | for (;;) { | |
67b5031e MW |
277 | /* Work through the string a character at a time. */ |
278 | ||
279 | ch = *p; switch (ch) { | |
280 | ||
281 | case '_': | |
282 | /* An underscore is OK if we haven't just seen one. */ | |
283 | ||
284 | if (f&f_uscore) goto done; | |
285 | p++; f = (f&~f_implicit) | f_uscore; | |
286 | break; | |
287 | ||
288 | case 'r': case 'R': | |
289 | /* An `r' is OK if the number so far is small enough to be a sensible | |
290 | * base, and we're scanning decimal implicitly. | |
291 | */ | |
292 | ||
293 | if (!(f&f_implicit) || !u || u >= 36) goto done; | |
294 | d = chtodig(p[1]); if (0 > d || d >= u) goto done; | |
295 | r = u; u = d; f = (f&~f_implicit) | f_digit; p += 2; q = p; | |
296 | break; | |
297 | ||
298 | default: | |
299 | /* Otherwise we expect a valid digit and accumulate it. */ | |
300 | d = chtodig(ch); if (d < 0 || d >= r) goto done; | |
301 | if (u > ULONG_MAX/r) return (-1); | |
302 | u *= r; if (u > ULONG_MAX - d) return (-1); | |
303 | u += d; f = (f&~f_uscore) | f_digit; p++; q = p; | |
304 | break; | |
3efcfd2d MW |
305 | } |
306 | } | |
307 | ||
67b5031e | 308 | done: |
3efcfd2d MW |
309 | if (!(f&f_digit)) return (-1); |
310 | *u_out = u; *q_out = q; return (0); | |
311 | ||
312 | #undef f_implicit | |
313 | #undef f_digit | |
314 | #undef f_uscore | |
315 | } | |
316 | ||
317 | static int parse_signed_integer(long *i_out, const char **q_out, | |
318 | const char *p) | |
319 | { | |
320 | unsigned long u; | |
321 | unsigned f = 0; | |
322 | #define f_neg 1u | |
323 | ||
67b5031e | 324 | /* Read an initial sign. */ |
3efcfd2d MW |
325 | if (*p == '+') p++; |
326 | else if (*p == '-') { f |= f_neg; p++; } | |
327 | ||
67b5031e | 328 | /* Scan an unsigned number. */ |
3efcfd2d MW |
329 | if (parse_unsigned_integer(&u, q_out, p)) return (-1); |
330 | ||
67b5031e | 331 | /* Check for signed overflow and apply the sign. */ |
3efcfd2d MW |
332 | if (!(f&f_neg)) { |
333 | if (u > LONG_MAX) return (-1); | |
334 | *i_out = u; | |
335 | } else { | |
336 | if (u && u - 1 > -(LONG_MIN + 1)) return (-1); | |
337 | *i_out = u ? -(long)(u - 1) - 1 : 0; | |
338 | } | |
339 | ||
340 | return (0); | |
341 | ||
342 | #undef f_neg | |
343 | } | |
344 | ||
67b5031e MW |
345 | /* --- @parse_unsigned@, @parse_signed@ --- * |
346 | * | |
347 | * Arguments: @unsigned long *u_out@ or @long *i_out@ = where to put the | |
348 | * result | |
349 | * @const char *p@ = string to parse | |
350 | * @const struct tvec_urange *ur@ or | |
351 | * @const struct tvec_irange *ir@ = range specification, | |
352 | * or null | |
353 | * @struct tvec_state *tv@ = test vector state | |
354 | * | |
355 | * Returns: Zero on success, @-1@ on error. | |
356 | * | |
357 | * Use: Parse and range-check an integer. Unlike @parse_(un)signed_ | |
358 | * integer@, these functions check that there's no cruft | |
359 | * following the final digit, and report errors as they find | |
360 | * them rather than leaving that to the caller. | |
361 | */ | |
362 | ||
363 | static int parse_unsigned(unsigned long *u_out, const char *p, | |
364 | const struct tvec_urange *ur, | |
365 | struct tvec_state *tv) | |
366 | { | |
367 | unsigned long u; | |
368 | const char *q; | |
369 | ||
370 | if (parse_unsigned_integer(&u, &q, p)) | |
371 | return (tvec_error(tv, "invalid unsigned integer `%s'", p)); | |
372 | if (*q) return (tvec_syntax(tv, *q, "end-of-line")); | |
373 | if (check_unsigned_range(u, ur, tv)) return (-1); | |
374 | *u_out = u; return (0); | |
375 | } | |
376 | ||
882a39c1 MW |
377 | static int parse_signed(long *i_out, const char *p, |
378 | const struct tvec_irange *ir, | |
379 | struct tvec_state *tv) | |
b64eb60f | 380 | { |
b64eb60f | 381 | long i; |
3efcfd2d | 382 | const char *q; |
b64eb60f | 383 | |
3efcfd2d MW |
384 | if (parse_signed_integer(&i, &q, p)) |
385 | return (tvec_error(tv, "invalid signed integer `%s'", p)); | |
386 | if (*q) return (tvec_syntax(tv, *q, "end-of-line")); | |
e63124bc | 387 | if (check_signed_range(i, ir, tv)) return (-1); |
3efcfd2d | 388 | *i_out = i; return (0); |
b64eb60f MW |
389 | } |
390 | ||
67b5031e | 391 | /*----- Floating-point utilities ------------------------------------------*/ |
b64eb60f | 392 | |
67b5031e MW |
393 | /* --- @eqish_floating_p@ --- * |
394 | * | |
395 | * Arguments: @double x, y@ = two numbers to compare | |
396 | * @const struct tvec_floatinfo *fi@ = floating-point info | |
397 | * | |
398 | * Returns: Nonzero if the comparand @y@ is sufficiently close to the | |
399 | * reference @x@, or zero if it's definitely different. | |
400 | */ | |
3efcfd2d | 401 | |
67b5031e MW |
402 | static int eqish_floating_p(double x, double y, |
403 | const struct tvec_floatinfo *fi) | |
3efcfd2d | 404 | { |
67b5031e MW |
405 | double t; |
406 | ||
407 | if (NANP(x)) return (NANP(y)); else if (NANP(y)) return (0); | |
408 | if (INFP(x)) return (x == y); else if (INFP(y)) return (0); | |
409 | ||
410 | switch (fi ? fi->f&TVFF_EQMASK : TVFF_EXACT) { | |
411 | case TVFF_EXACT: | |
412 | return (x == y && NEGP(x) == NEGP(y)); | |
413 | case TVFF_ABSDELTA: | |
414 | t = x - y; if (t < 0) t = -t; return (t < fi->delta); | |
415 | case TVFF_RELDELTA: | |
416 | t = 1.0 - y/x; if (t < 0) t = -t; return (t < fi->delta); | |
417 | default: | |
418 | abort(); | |
419 | } | |
b64eb60f MW |
420 | } |
421 | ||
67b5031e MW |
422 | /* --- @format_floating@ --- * |
423 | * | |
424 | * Arguments: @const struct gprintf_ops *gops@ = print operations | |
425 | * @void *go@ = print destination | |
426 | * @double x@ = number to print | |
427 | * | |
428 | * Returns: --- | |
429 | * | |
430 | * Use: Print a floating-point number, accurately. | |
431 | */ | |
3efcfd2d | 432 | |
e63124bc MW |
433 | static void format_floating(const struct gprintf_ops *gops, void *go, |
434 | double x) | |
435 | { | |
436 | int prec; | |
437 | ||
438 | if (NANP(x)) | |
439 | gprintf(gops, go, "#nan"); | |
440 | else if (INFP(x)) | |
441 | gprintf(gops, go, x > 0 ? "#+inf" : "#-inf"); | |
442 | else { | |
443 | /* Ugh. C doesn't provide any function for just printing a | |
444 | * floating-point number /correctly/, i.e., so that you can read the | |
445 | * result back and recover the number you first thought of. There are | |
446 | * complicated algorithms published for doing this, but I really don't | |
447 | * want to get into that here. So we have this. | |
448 | * | |
449 | * The sign doesn't cause significant difficulty so we're going to ignore | |
450 | * it for now. So suppose we're given a number %$x = f b^e$%, in | |
451 | * base-%$b$% format, so %$f b^n$% and %$e$% are integers, with | |
452 | * %$0 \le f < 1$%. We're going to convert it into the nearest integer | |
453 | * of the form %$X = F B^E$%, with similar conditions, only with the | |
454 | * additional requirement that %$X$% is normalized, i.e., that %$X = 0$% | |
455 | * or %$F \ge B^{-N}$%. | |
456 | * | |
457 | * We're rounding to the nearest such %$X$%. If there is to be ambiguity | |
458 | * in the conversion, then some %$x = f b^e$% and the next smallest | |
459 | * representable number %$x' = x + b^{e-n}$% must both map to the same | |
460 | * %$X$%, which means both %$x$% and %$x'$% must be nearer to %$X$% than | |
461 | * any other number representable in the target system. The nest larger | |
462 | * number is %$X' = X + B^{E-N}$%; the next smaller number will normally | |
463 | * be %$W = X - B^{E-N}$%, but if %$F = 1/B$ then the next smaller number | |
464 | * is actually %$X - B^{E-N-1}$%. We ignore this latter possibility in | |
465 | * the pursuit of a conservative estimate (though actually it doesn't | |
466 | * matter). | |
467 | * | |
468 | * If both %$x$% and %$x'$% map to %$X$% then we must have | |
469 | * %$L = X - B^{E-N}/2 \le x$% and %$x + b^{e-n} \le R = X + B^{E-N}/2$%; | |
470 | * so firstly %$f b^e = x \ge L = W + B^{E-N}/2 > W = (F - B^{-N}) B^E$%, | |
471 | * and secondly %$b^{e-n} \le B^{E-N}$%. Since these inequalities are in | |
472 | * opposite senses, we can divide, giving | |
473 | * | |
474 | * %$f b^e/b^{e-n} > (F - B^{-N}) B^E/B^{E-N}$% , | |
475 | * | |
476 | * whence | |
477 | * | |
478 | * %$f b^n > (F - B^{-N}) B^N = F B^N - 1$% . | |
479 | * | |
480 | * Now %$f \le 1 - b^{-n}$%, and %$F \ge B^{-1}$%, so, for this to be | |
481 | * possible, it must be the case that | |
482 | * | |
483 | * %$(1 - b^{-n}) b^n = b^n - 1 > B^{N-1} - 1$% . | |
484 | * | |
485 | * Then rearrange and take logarithms, obtaining | |
486 | * | |
487 | * %$(N - 1) \log B < n \log b$% , | |
488 | * | |
489 | * and so | |
490 | * | |
491 | * %$N < n \log b/\log B + 1$% . | |
492 | * | |
493 | * Recall that this is a necessary condition for a collision to occur; we | |
494 | * are therefore safe whenever | |
495 | * | |
496 | * %$N \ge n \log b/\log B + 1$% ; | |
497 | * | |
498 | * so, taking ceilings, | |
499 | * | |
500 | * %$N \ge \lceil n \log b/\log B \rceil + 1$% . | |
501 | * | |
502 | * So that's why we have this. | |
503 | * | |
504 | * I'm going to assume that @n = DBL_MANT_DIG@ is sufficiently small that | |
505 | * we can calculate this without ending up on the wrong side of an | |
506 | * integer boundary. | |
507 | * | |
508 | * In C11, we have @DBL_DECIMAL_DIG@, which should be the same value only | |
509 | * as a constant. Except that modern compilers are more than clever | |
510 | * enough to work out that this is a constant anyway. | |
511 | * | |
512 | * This is sometimes an overestimate: we'll print out meaningless digits | |
513 | * that don't represent anything we actually know about the number in | |
514 | * question. To fix that, we'd need a complicated algorithm like Steele | |
515 | * and White's Dragon4, Gay's @dtoa@, or Burger and Dybvig's algorithm | |
516 | * (note that Loitsch's Grisu2 is conservative, and Grisu3 hands off to | |
517 | * something else in difficult situations). | |
518 | */ | |
519 | ||
520 | prec = ceil(DBL_MANT_DIG*log(FLT_RADIX)/log(10)) + 1; | |
521 | gprintf(gops, go, "%.*g", prec, x); | |
522 | } | |
523 | } | |
524 | ||
67b5031e MW |
525 | /* --- @parse_floating@ --- * |
526 | * | |
527 | * Arguments: @double *x_out@ = where to put the result | |
528 | * @const char *p@ = string to parse | |
529 | * @const struct tvec_floatinfo *fi@ = floating-point info | |
530 | * @struct tvec_state *tv@ = test vector state | |
531 | * | |
532 | * Returns: Zero on success, @-1@ on error. | |
533 | * | |
534 | * Use: Parse a floating-point number from a string. Reports any | |
535 | * necessary errors. | |
536 | */ | |
e63124bc MW |
537 | |
538 | static int parse_floating(double *x_out, const char *p, | |
539 | const struct tvec_floatinfo *fi, | |
540 | struct tvec_state *tv) | |
541 | { | |
542 | const char *pp; char *q; | |
543 | dstr d = DSTR_INIT; | |
544 | double x; | |
545 | int olderr, rc; | |
546 | ||
67b5031e | 547 | /* Check for special tokens. */ |
e63124bc MW |
548 | if (STRCMP(p, ==, "#nan")) { |
549 | #ifdef NAN | |
550 | x = NAN; rc = 0; | |
551 | #else | |
552 | tvec_error(tv, "NaN not supported on this system"); | |
553 | rc = -1; goto end; | |
554 | #endif | |
67b5031e MW |
555 | } |
556 | ||
557 | else if (STRCMP(p, ==, "#inf") || | |
558 | STRCMP(p, ==, "#+inf") || STRCMP(p, ==, "+#inf")) { | |
3efcfd2d | 559 | #ifdef INFINITY |
e63124bc MW |
560 | x = INFINITY; rc = 0; |
561 | #else | |
562 | tvec_error(tv, "infinity not supported on this system"); | |
563 | rc = -1; goto end; | |
564 | #endif | |
67b5031e MW |
565 | } |
566 | ||
567 | else if (STRCMP(p, ==, "#-inf") || STRCMP(p, ==, "-#inf")) { | |
3efcfd2d | 568 | #ifdef INFINITY |
e63124bc MW |
569 | x = -INFINITY; rc = 0; |
570 | #else | |
571 | tvec_error(tv, "infinity not supported on this system"); | |
572 | rc = -1; goto end; | |
573 | #endif | |
67b5031e MW |
574 | } |
575 | ||
576 | /* Check that this looks like a number, so we can exclude `strtod' | |
577 | * recognizing its own non-finite number tokens. | |
578 | */ | |
579 | else { | |
e63124bc MW |
580 | pp = p; |
581 | if (*pp == '+' || *pp == '-') pp++; | |
582 | if (*pp == '.') pp++; | |
583 | if (!ISDIGIT(*pp)) { | |
3efcfd2d | 584 | tvec_syntax(tv, *p ? *p : fgetc(tv->fp), "floating-point number"); |
e63124bc MW |
585 | rc = -1; goto end; |
586 | } | |
67b5031e MW |
587 | |
588 | /* Parse the number using the system parser. */ | |
e63124bc MW |
589 | olderr = errno; errno = 0; |
590 | x = strtod(p, &q); | |
591 | if (*q) { | |
592 | tvec_syntax(tv, *q, "end-of-line"); | |
593 | rc = -1; goto end; | |
594 | } | |
595 | if (errno && (errno != ERANGE || (x > 0 ? -x : x) == HUGE_VAL)) { | |
596 | tvec_error(tv, "invalid floating-point number `%s': %s", | |
597 | p, strerror(errno)); | |
598 | rc = -1; goto end; | |
599 | } | |
600 | errno = olderr; | |
601 | } | |
602 | ||
67b5031e | 603 | /* Check that the number is acceptable. */ |
e63124bc MW |
604 | if (NANP(x) && fi && !(fi->f&TVFF_NANOK)) { |
605 | tvec_error(tv, "#nan not allowed here"); | |
606 | rc = -1; goto end; | |
607 | } | |
67b5031e | 608 | |
e63124bc MW |
609 | if (fi && ((!(fi->f&TVFF_NOMIN) && x < fi->min) || |
610 | (!(fi->f&TVFF_NOMAX) && x > fi->max))) { | |
611 | dstr_puts(&d, "floating-point number "); | |
612 | format_floating(&dstr_printops, &d, x); | |
613 | dstr_puts(&d, " out of range (must be in "); | |
614 | if (fi->f&TVFF_NOMIN) | |
615 | dstr_puts(&d, "(#-inf"); | |
616 | else | |
617 | { dstr_putc(&d, '['); format_floating(&dstr_printops, &d, fi->min); } | |
618 | dstr_puts(&d, " .. "); | |
619 | if (fi->f&TVFF_NOMAX) | |
620 | dstr_puts(&d, "#+inf)"); | |
621 | else | |
622 | { format_floating(&dstr_printops, &d, fi->max); dstr_putc(&d, ']'); } | |
623 | dstr_putc(&d, ')'); dstr_putz(&d); | |
624 | tvec_error(tv, "%s", d.buf); rc = -1; goto end; | |
625 | } | |
626 | ||
67b5031e MW |
627 | /* All done. */ |
628 | *x_out = x; rc = 0; | |
629 | end: | |
630 | dstr_destroy(&d); | |
631 | return (rc); | |
632 | } | |
633 | ||
634 | /*----- String utilities --------------------------------------------------*/ | |
635 | ||
636 | /* Special character name table. */ | |
637 | static const struct chartab { | |
638 | const char *name; /* character name */ | |
639 | int ch; /* character value */ | |
640 | unsigned f; /* flags: */ | |
641 | #define CTF_PREFER 1u /* preferred name */ | |
642 | #define CTF_SHORT 2u /* short name (compact style) */ | |
643 | } chartab[] = { | |
644 | { "#eof", EOF, CTF_PREFER | CTF_SHORT }, | |
645 | { "#nul", '\0', CTF_PREFER }, | |
646 | { "#bell", '\a', CTF_PREFER }, | |
647 | { "#ding", '\a', 0 }, | |
648 | { "#bel", '\a', CTF_SHORT }, | |
649 | { "#backspace", '\b', CTF_PREFER }, | |
650 | { "#bs", '\b', CTF_SHORT }, | |
651 | { "#escape", '\x1b', CTF_PREFER }, | |
652 | { "#esc", '\x1b', CTF_SHORT }, | |
653 | { "#formfeed", '\f', CTF_PREFER }, | |
654 | { "#ff", '\f', CTF_SHORT }, | |
655 | { "#newline", '\n', CTF_PREFER }, | |
656 | { "#linefeed", '\n', 0 }, | |
657 | { "#lf", '\n', CTF_SHORT }, | |
658 | { "#nl", '\n', 0 }, | |
659 | { "#return", '\r', CTF_PREFER }, | |
660 | { "#carriage-return", '\r', 0 }, | |
661 | { "#cr", '\r', CTF_SHORT }, | |
662 | { "#tab", '\t', CTF_PREFER | CTF_SHORT }, | |
663 | { "#horizontal-tab", '\t', 0 }, | |
664 | { "#ht", '\t', 0 }, | |
665 | { "#vertical-tab", '\v', CTF_PREFER }, | |
666 | { "#vt", '\v', CTF_SHORT }, | |
667 | { "#space", ' ', 0 }, | |
668 | { "#spc", ' ', CTF_SHORT }, | |
669 | { "#delete", '\x7f', CTF_PREFER }, | |
670 | { "#del", '\x7f', CTF_SHORT }, | |
671 | { 0, 0, 0 } | |
672 | }; | |
673 | ||
674 | /* --- @find_charname@ --- * | |
675 | * | |
676 | * Arguments: @int ch@ = character to match | |
677 | * @unsigned f@ = flags (@CTF_...@) to match | |
678 | * | |
679 | * Returns: The name of the character, or null if no match is found. | |
680 | * | |
681 | * Use: Looks up a name for a character. Specifically, it returns | |
682 | * the first entry in the @chartab@ table which matches @ch@ and | |
683 | * which has one of the flags @f@ set. | |
684 | */ | |
685 | ||
686 | static const char *find_charname(int ch, unsigned f) | |
687 | { | |
688 | const struct chartab *ct; | |
689 | ||
690 | for (ct = chartab; ct->name; ct++) | |
691 | if (ct->ch == ch && (ct->f&f)) return (ct->name); | |
692 | return (0); | |
693 | } | |
694 | ||
695 | /* --- @read_charname@ --- * | |
696 | * | |
697 | * Arguments: @int *ch_out@ = where to put the character | |
698 | * @const char *p@ = character name | |
699 | * @unsigned f@ = flags (@TCF_...@) | |
700 | * | |
701 | * Returns: Zero if a match was found, @-1@ if not. | |
702 | * | |
703 | * Use: Looks up a character by name. If @RCF_EOFOK@ is set in @f@, | |
704 | * then the @EOF@ marker can be matched; otherwise it can't. | |
705 | */ | |
706 | ||
707 | #define RCF_EOFOK 1u | |
708 | static int read_charname(int *ch_out, const char *p, unsigned f) | |
709 | { | |
710 | const struct chartab *ct; | |
711 | ||
712 | for (ct = chartab; ct->name; ct++) | |
713 | if (STRCMP(p, ==, ct->name) && ((f&RCF_EOFOK) || ct->ch >= 0)) | |
714 | { *ch_out = ct->ch; return (0); } | |
715 | return (-1); | |
716 | } | |
717 | ||
718 | /* --- @format_charesc@ --- * | |
719 | * | |
720 | * Arguments: @const struct gprintf_ops *gops@ = print operations | |
721 | * @void *go@ = print destination | |
722 | * @int ch@ = character to format | |
723 | * @unsigned f@ = flags (@FCF_...@) | |
724 | * | |
725 | * Returns: --- | |
726 | * | |
727 | * Use: Format a character as an escape sequence, possibly as part of | |
728 | * a larger string. If @FCF_BRACE@ is set in @f@, then put | |
729 | * braces around a `\x...' code, so that it's suitable for use | |
730 | * in a longer string. | |
731 | */ | |
732 | ||
733 | #define FCF_BRACE 1u | |
734 | static void format_charesc(const struct gprintf_ops *gops, void *go, | |
735 | int ch, unsigned f) | |
736 | { | |
737 | switch (ch) { | |
738 | case '\a': gprintf(gops, go, "\\a"); break; | |
739 | case '\b': gprintf(gops, go, "\\b"); break; | |
740 | case '\x1b': gprintf(gops, go, "\\e"); break; | |
741 | case '\f': gprintf(gops, go, "\\f"); break; | |
742 | case '\r': gprintf(gops, go, "\\r"); break; | |
743 | case '\n': gprintf(gops, go, "\\n"); break; | |
744 | case '\t': gprintf(gops, go, "\\t"); break; | |
745 | case '\v': gprintf(gops, go, "\\v"); break; | |
746 | case '\\': gprintf(gops, go, "\\\\"); break; | |
747 | case '\'': gprintf(gops, go, "\\'"); break; | |
748 | case '\0': | |
749 | if (f&FCF_BRACE) gprintf(gops, go, "\\{0}"); | |
750 | else gprintf(gops, go, "\\0"); | |
751 | break; | |
752 | default: | |
753 | if (f&FCF_BRACE) | |
754 | gprintf(gops, go, "\\x{%0*x}", hex_width(UCHAR_MAX), ch); | |
755 | else | |
756 | gprintf(gops, go, "\\x%0*x", hex_width(UCHAR_MAX), ch); | |
757 | break; | |
758 | } | |
759 | } | |
760 | ||
761 | /* --- @format_char@ --- * | |
762 | * | |
763 | * Arguments: @const struct gprintf_ops *gops@ = print operations | |
764 | * @void *go@ = print destination | |
765 | * @int ch@ = character to format | |
766 | * | |
767 | * Returns: --- | |
768 | * | |
769 | * Use: Format a single character. | |
770 | */ | |
771 | ||
772 | static void format_char(const struct gprintf_ops *gops, void *go, int ch) | |
773 | { | |
774 | switch (ch) { | |
775 | case '\\': case '\'': escape: | |
776 | gprintf(gops, go, "'"); | |
777 | format_charesc(gops, go, ch, 0); | |
778 | gprintf(gops, go, "'"); | |
779 | break; | |
780 | default: | |
781 | if (!isprint(ch)) goto escape; | |
782 | gprintf(gops, go, "'%c'", ch); | |
783 | break; | |
784 | } | |
785 | } | |
786 | ||
787 | /* --- @maybe_format_unsigned_char@, @maybe_format_signed_char@ --- * | |
788 | * | |
789 | * Arguments: @const struct gprintf_ops *gops@ = print operations | |
790 | * @void *go@ = print destination | |
791 | * @unsigned long u@ or @long i@ = an integer | |
792 | * | |
793 | * Returns: --- | |
794 | * | |
795 | * Use: Format a (signed or unsigned) integer as a character, if it's | |
796 | * in range, printing something like `= 'q''. It's assumed that | |
797 | * a comment marker has already been output. | |
798 | */ | |
799 | ||
800 | static void maybe_format_unsigned_char | |
801 | (const struct gprintf_ops *gops, void *go, unsigned long u) | |
802 | { | |
803 | const char *p; | |
804 | ||
805 | p = find_charname(u, CTF_PREFER); | |
806 | if (p) gprintf(gops, go, " = %s", p); | |
807 | if (u < UCHAR_MAX) | |
808 | { gprintf(gops, go, " = "); format_char(gops, go, u); } | |
e63124bc MW |
809 | } |
810 | ||
67b5031e MW |
811 | static void maybe_format_signed_char |
812 | (const struct gprintf_ops *gops, void *go, long i) | |
b64eb60f | 813 | { |
67b5031e MW |
814 | const char *p; |
815 | ||
816 | p = find_charname(i, CTF_PREFER); | |
817 | if (p) gprintf(gops, go, " = %s", p); | |
818 | if (0 <= i && i < UCHAR_MAX) | |
819 | { gprintf(gops, go, " = "); format_char(gops, go, i); } | |
b64eb60f MW |
820 | } |
821 | ||
67b5031e MW |
822 | /* --- @read_charesc@ --- * |
823 | * | |
824 | * Arguments: @int *ch_out@ = where to put the result | |
825 | * @struct tvec_state *tv@ = test vector state | |
826 | * | |
827 | * Returns: Zero on success, @-1@ on error. | |
828 | * | |
829 | * Use: Parse and convert an escape sequence from @tv@'s input | |
830 | * stream, assuming that the initial `\' has already been read. | |
831 | * Reports errors as appropriate. | |
832 | */ | |
833 | ||
834 | static int read_charesc(int *ch_out, struct tvec_state *tv) | |
b64eb60f | 835 | { |
b64eb60f MW |
836 | int ch, i, esc; |
837 | unsigned f = 0; | |
838 | #define f_brace 1u | |
839 | ||
e63124bc MW |
840 | ch = getc(tv->fp); |
841 | switch (ch) { | |
67b5031e MW |
842 | |
843 | /* Things we shouldn't find. */ | |
844 | case EOF: case '\n': return (tvec_syntax(tv, ch, "string escape")); | |
845 | ||
846 | /* Single-character escapes. */ | |
e63124bc MW |
847 | case '\'': *ch_out = '\''; break; |
848 | case '\\': *ch_out = '\\'; break; | |
849 | case '"': *ch_out = '"'; break; | |
850 | case 'a': *ch_out = '\a'; break; | |
851 | case 'b': *ch_out = '\b'; break; | |
852 | case 'e': *ch_out = '\x1b'; break; | |
853 | case 'f': *ch_out = '\f'; break; | |
854 | case 'n': *ch_out = '\n'; break; | |
855 | case 'r': *ch_out = '\r'; break; | |
856 | case 't': *ch_out = '\t'; break; | |
857 | case 'v': *ch_out = '\v'; break; | |
858 | ||
67b5031e | 859 | /* Hex escapes, with and without braces. */ |
e63124bc MW |
860 | case 'x': |
861 | ch = getc(tv->fp); | |
862 | if (ch == '{') { f |= f_brace; ch = getc(tv->fp); } | |
863 | else f &= ~f_brace; | |
67b5031e MW |
864 | esc = chtodig(ch); |
865 | if (esc < 0 || esc >= 16) return (tvec_syntax(tv, ch, "hex digit")); | |
e63124bc | 866 | for (;;) { |
67b5031e MW |
867 | ch = getc(tv->fp); i = chtodig(ch); if (i < 0 || i >= 16) break; |
868 | esc = 16*esc + i; | |
e63124bc MW |
869 | if (esc > UCHAR_MAX) |
870 | return (tvec_error(tv, | |
871 | "character code %d out of range", esc)); | |
872 | } | |
873 | if (!(f&f_brace)) ungetc(ch, tv->fp); | |
874 | else if (ch != '}') return (tvec_syntax(tv, ch, "`}'")); | |
875 | *ch_out = esc; | |
876 | break; | |
877 | ||
67b5031e MW |
878 | /* Other things, primarily octal escapes. */ |
879 | case '{': | |
880 | f |= f_brace; ch = getc(tv->fp); | |
881 | /* fall through */ | |
e63124bc MW |
882 | default: |
883 | if ('0' <= ch && ch < '8') { | |
884 | i = 1; esc = ch - '0'; | |
885 | for (;;) { | |
886 | ch = getc(tv->fp); | |
887 | if ('0' > ch || ch >= '8') { ungetc(ch, tv->fp); break; } | |
888 | esc = 8*esc + ch - '0'; | |
889 | i++; if (i >= 3) break; | |
890 | } | |
67b5031e MW |
891 | if (f&f_brace) { |
892 | ch = getc(tv->fp); | |
893 | if (ch != '}') return (tvec_syntax(tv, ch, "`}'")); | |
894 | } | |
e63124bc MW |
895 | if (esc > UCHAR_MAX) |
896 | return (tvec_error(tv, | |
897 | "character code %d out of range", esc)); | |
67b5031e | 898 | *ch_out = esc; break; |
e63124bc MW |
899 | } else |
900 | return (tvec_syntax(tv, ch, "string escape")); | |
901 | } | |
902 | ||
67b5031e | 903 | /* Done. */ |
e63124bc MW |
904 | return (0); |
905 | ||
906 | #undef f_brace | |
907 | } | |
908 | ||
67b5031e MW |
909 | /* --- @read_quoted_string@ --- * |
910 | * | |
911 | * Arguments: @dstr *d@ = string to write to | |
912 | * @int quote@ = initial quote, `'' or `"' | |
913 | * @struct tvec_state *tv@ = test vector state | |
914 | * | |
915 | * Returns: Zero on success, @-1@ on error. | |
916 | * | |
917 | * Use: Read the rest of a quoted string into @d@, reporting errors | |
918 | * as appropriate. | |
919 | * | |
920 | * A single-quoted string is entirely literal. A double-quoted | |
921 | * string may contain C-like escapes. | |
922 | */ | |
923 | ||
e63124bc MW |
924 | static int read_quoted_string(dstr *d, int quote, struct tvec_state *tv) |
925 | { | |
926 | int ch; | |
b64eb60f MW |
927 | |
928 | for (;;) { | |
929 | ch = getc(tv->fp); | |
b64eb60f MW |
930 | switch (ch) { |
931 | case EOF: case '\n': | |
e63124bc | 932 | return (tvec_syntax(tv, ch, "`%c'", quote)); |
b64eb60f MW |
933 | case '\\': |
934 | if (quote == '\'') goto ordinary; | |
e63124bc | 935 | ch = getc(tv->fp); if (ch == '\n') { tv->lno++; break; } |
67b5031e | 936 | ungetc(ch, tv->fp); if (read_charesc(&ch, tv)) return (-1); |
e63124bc | 937 | goto ordinary; |
b64eb60f MW |
938 | default: |
939 | if (ch == quote) goto end; | |
940 | ordinary: | |
941 | DPUTC(d, ch); | |
942 | break; | |
943 | } | |
944 | } | |
945 | ||
946 | end: | |
947 | DPUTZ(d); | |
882a39c1 | 948 | return (0); |
e63124bc | 949 | } |
b64eb60f | 950 | |
67b5031e MW |
951 | /* --- @collect_bare@ --- * |
952 | * | |
953 | * Arguments: @dstr *d@ = string to write to | |
954 | * @struct tvec_state *tv@ = test vector state | |
955 | * | |
956 | * Returns: Zero on success, @-1@ on error. | |
957 | * | |
958 | * Use: Read barewords and the whitespace between them. Stop when we | |
959 | * encounter something which can't start a bareword. | |
960 | */ | |
b64eb60f MW |
961 | |
962 | static int collect_bare(dstr *d, struct tvec_state *tv) | |
963 | { | |
964 | size_t pos = d->len; | |
965 | enum { WORD, SPACE, ESCAPE }; unsigned s = WORD; | |
966 | int ch, rc; | |
967 | ||
968 | for (;;) { | |
969 | ch = getc(tv->fp); | |
970 | switch (ch) { | |
971 | case EOF: | |
882a39c1 MW |
972 | tvec_syntax(tv, ch, "bareword"); |
973 | rc = -1; goto end; | |
b64eb60f MW |
974 | case '\n': |
975 | if (s == ESCAPE) { tv->lno++; goto addch; } | |
976 | if (s == WORD) pos = d->len; | |
882a39c1 | 977 | ungetc(ch, tv->fp); if (tvec_nexttoken(tv)) { rc = -1; goto end; } |
b64eb60f MW |
978 | DPUTC(d, ' '); s = SPACE; |
979 | break; | |
67b5031e | 980 | case '"': case '\'': case '!': case '#': case ')': case '}': case ']': |
882a39c1 | 981 | if (s == SPACE) { ungetc(ch, tv->fp); goto done; } |
b64eb60f MW |
982 | goto addch; |
983 | case '\\': | |
984 | s = ESCAPE; | |
985 | break; | |
986 | default: | |
987 | if (s != ESCAPE && isspace(ch)) { | |
988 | if (s == WORD) pos = d->len; | |
989 | DPUTC(d, ch); s = SPACE; | |
990 | break; | |
991 | } | |
992 | addch: | |
993 | DPUTC(d, ch); s = WORD; | |
994 | } | |
995 | } | |
996 | ||
997 | done: | |
998 | if (s == SPACE) d->len = pos; | |
882a39c1 MW |
999 | DPUTZ(d); rc = 0; |
1000 | end: | |
1001 | return (rc); | |
b64eb60f MW |
1002 | } |
1003 | ||
67b5031e MW |
1004 | /* --- @set_up_encoding@ --- * |
1005 | * | |
1006 | * Arguments: @const codec_class **ccl_out@ = where to put the class | |
1007 | * @unsigned *f_out@ = where to put the flags | |
1008 | * @unsigned code@ = the coding scheme to use (@TVEC_...@) | |
1009 | * | |
1010 | * Returns: --- | |
1011 | * | |
1012 | * Use: Helper for @read_compound_string@ below. | |
1013 | * | |
1014 | * Return the appropriate codec class and flags for @code@. | |
1015 | * Leaves @*ccl_out@ null if the coding scheme doesn't have a | |
1016 | * backing codec class (e.g., @TVCODE_BARE@). | |
1017 | */ | |
1018 | ||
1019 | enum { TVCODE_BARE, TVCODE_HEX, TVCODE_BASE64, TVCODE_BASE32 }; | |
b64eb60f MW |
1020 | static void set_up_encoding(const codec_class **ccl_out, unsigned *f_out, |
1021 | unsigned code) | |
1022 | { | |
1023 | switch (code) { | |
1024 | case TVCODE_BARE: | |
1025 | *ccl_out = 0; *f_out = 0; | |
1026 | break; | |
1027 | case TVCODE_HEX: | |
1028 | *ccl_out = &hex_class; *f_out = CDCF_IGNCASE; | |
1029 | break; | |
1030 | case TVCODE_BASE32: | |
1031 | *ccl_out = &base32_class; *f_out = CDCF_IGNCASE | CDCF_IGNEQPAD; | |
1032 | break; | |
1033 | case TVCODE_BASE64: | |
1034 | *ccl_out = &base64_class; *f_out = CDCF_IGNEQPAD; | |
1035 | break; | |
1036 | default: | |
1037 | abort(); | |
1038 | } | |
1039 | } | |
1040 | ||
67b5031e MW |
1041 | /* --- @flush_codec@ --- * |
1042 | * | |
1043 | * Arguments: @codec *cdc@ = a codec, or null | |
1044 | * @dstr *d@ = output string | |
1045 | * @struct tvec_state *tv@ = test vector state | |
1046 | * | |
1047 | * Returns: Zero on success, @-1@ on error. | |
1048 | * | |
1049 | * Use: Helper for @read_compound_string@ below. | |
1050 | * | |
1051 | * Flush out any final buffered material from @cdc@, and check | |
1052 | * that it's in a good state. Frees the codec on success. Does | |
1053 | * nothing if @cdc@ is null. | |
1054 | */ | |
1055 | ||
1056 | static int flush_codec(codec *cdc, dstr *d, struct tvec_state *tv) | |
1057 | { | |
1058 | int err; | |
1059 | ||
1060 | if (cdc) { | |
1061 | err = cdc->ops->code(cdc, 0, 0, d); | |
1062 | if (err) | |
1063 | return (tvec_error(tv, "invalid %s sequence end: %s", | |
1064 | cdc->ops->c->name, codec_strerror(err))); | |
1065 | cdc->ops->destroy(cdc); | |
1066 | } | |
1067 | return (0); | |
1068 | } | |
1069 | ||
1070 | /* --- @read_compound_string@ --- * | |
1071 | * | |
1072 | * Arguments: @void **p_inout@ = address of output buffer pointer | |
1073 | * @size_t *sz_inout@ = address of buffer size | |
1074 | * @unsigned code@ = initial interpretation of barewords | |
1075 | * @unsigned f@ = other flags (@RCSF_...@) | |
1076 | * @struct tvec_state *tv@ = test vector state | |
1077 | * | |
1078 | * Returns: Zero on success, @-1@ on error. | |
1079 | * | |
1080 | * Use: Parse a compound string, i.e., a sequence of stringish pieces | |
1081 | * which might be quoted strings, character names, or barewords | |
1082 | * to be decoded accoding to @code@, interspersed with | |
1083 | * additional directives. | |
1084 | * | |
1085 | * If the initial buffer pointer is non-null and sufficiently | |
1086 | * large, then it will be reused; otherwise, it is freed and a | |
1087 | * fresh, sufficiently large buffer is allocated and returned. | |
1088 | */ | |
1089 | ||
1090 | #define RCSF_NESTED 1u | |
882a39c1 | 1091 | static int read_compound_string(void **p_inout, size_t *sz_inout, |
67b5031e MW |
1092 | unsigned code, unsigned f, |
1093 | struct tvec_state *tv) | |
b64eb60f | 1094 | { |
67b5031e | 1095 | const codec_class *ccl; unsigned cdf; |
b64eb60f MW |
1096 | codec *cdc; |
1097 | dstr d = DSTR_INIT, w = DSTR_INIT; | |
1098 | char *p; | |
67b5031e MW |
1099 | const char *q; |
1100 | void *pp = 0; size_t sz; | |
1101 | unsigned long n; | |
882a39c1 | 1102 | int ch, err, rc; |
b64eb60f | 1103 | |
67b5031e MW |
1104 | set_up_encoding(&ccl, &cdf, code); cdc = 0; |
1105 | ||
1106 | if (tvec_nexttoken(tv)) return (tvec_syntax(tv, fgetc(tv->fp), "string")); | |
b64eb60f MW |
1107 | do { |
1108 | ch = getc(tv->fp); | |
67b5031e MW |
1109 | switch (ch) { |
1110 | ||
1111 | case ')': case ']': case '}': | |
1112 | /* Close brackets. Leave these for recursive caller if there is one, | |
1113 | * or just complain. | |
1114 | */ | |
1115 | ||
1116 | if (!(f&RCSF_NESTED)) | |
1117 | { rc = tvec_syntax(tv, ch, "string"); goto end; } | |
1118 | ungetc(ch, tv->fp); goto done; | |
1119 | ||
1120 | case '"': case '\'': | |
1121 | /* Quotes. Read a quoted string. */ | |
1122 | ||
1123 | if (cdc && flush_codec(cdc, &d, tv)) { rc = -1; goto end; } | |
1124 | cdc = 0; | |
1125 | if (read_quoted_string(&d, ch, tv)) { rc = -1; goto end; } | |
1126 | break; | |
1127 | ||
1128 | case '#': | |
1129 | /* A named character. */ | |
1130 | ||
1131 | ungetc(ch, tv->fp); | |
1132 | if (cdc && flush_codec(cdc, &d, tv)) { rc = -1; goto end; } | |
1133 | cdc = 0; | |
1134 | DRESET(&w); tvec_readword(tv, &w, ";", "character name"); | |
1135 | if (read_charname(&ch, w.buf, RCF_EOFOK)) { | |
1136 | rc = tvec_error(tv, "unknown character name `%s'", d.buf); | |
1137 | goto end; | |
1138 | } | |
1139 | DPUTC(&d, ch); break; | |
1140 | ||
1141 | case '!': | |
1142 | /* A magic keyword. */ | |
1143 | ||
1144 | if (cdc && flush_codec(cdc, &d, tv)) { rc = -1; goto end; } | |
1145 | cdc = 0; | |
b64eb60f | 1146 | ungetc(ch, tv->fp); |
67b5031e MW |
1147 | DRESET(&w); tvec_readword(tv, &w, ";", "`!'-keyword"); |
1148 | ||
1149 | /* Change bareword coding system. */ | |
1150 | if (STRCMP(w.buf, ==, "!bare")) | |
1151 | { code = TVCODE_BARE; set_up_encoding(&ccl, &cdf, code); } | |
1152 | else if (STRCMP(w.buf, ==, "!hex")) | |
1153 | { code = TVCODE_HEX; set_up_encoding(&ccl, &cdf, code); } | |
1154 | else if (STRCMP(w.buf, ==, "!base32")) | |
1155 | { code = TVCODE_BASE32; set_up_encoding(&ccl, &cdf, code); } | |
1156 | else if (STRCMP(w.buf, ==, "!base64")) | |
1157 | { code = TVCODE_BASE64; set_up_encoding(&ccl, &cdf, code); } | |
1158 | ||
1159 | /* Repeated substrings. */ | |
1160 | else if (STRCMP(w.buf, ==, "!repeat")) { | |
1161 | if (tvec_nexttoken(tv)) { | |
1162 | rc = tvec_syntax(tv, fgetc(tv->fp), "repeat count"); | |
1163 | goto end; | |
1164 | } | |
1165 | DRESET(&w); | |
1166 | if (tvec_readword(tv, &w, ";{", "repeat count")) | |
1167 | { rc = -1; goto end; } | |
1168 | if (parse_unsigned_integer(&n, &q, w.buf)) { | |
1169 | rc = tvec_error(tv, "invalid repeat count `%s'", w.buf); | |
1170 | goto end; | |
1171 | } | |
1172 | if (*q) { rc = tvec_syntax(tv, *q, "`{'"); goto end; } | |
1173 | if (tvec_nexttoken(tv)) | |
1174 | { rc = tvec_syntax(tv, fgetc(tv->fp), "`{'"); goto end; } | |
1175 | ch = getc(tv->fp); if (ch != '{') | |
1176 | { rc = tvec_syntax(tv, ch, "`{'"); goto end; } | |
1177 | sz = 0; | |
1178 | if (read_compound_string(&pp, &sz, code, f | RCSF_NESTED, tv)) | |
1179 | { rc = -1; goto end; } | |
1180 | ch = getc(tv->fp); if (ch != '}') | |
1181 | { rc = tvec_syntax(tv, ch, "`}'"); goto end; } | |
1182 | if (sz) { | |
1183 | if (n > (size_t)-1/sz) | |
1184 | { rc = tvec_error(tv, "repeat size out of range"); goto end; } | |
1185 | dstr_ensure(&d, n*sz); | |
1186 | if (sz == 1) | |
1187 | { memset(d.buf + d.len, *(unsigned char *)pp, n); d.len += n; } | |
1188 | else | |
1189 | for (; n--; d.len += sz) memcpy(d.buf + d.len, pp, sz); | |
1190 | } | |
1191 | xfree(pp); pp = 0; | |
1192 | } | |
1193 | ||
1194 | /* Anything else is an error. */ | |
1195 | else { | |
1196 | tvec_error(tv, "unknown string keyword `%s'", w.buf); | |
1197 | rc = -1; goto end; | |
1198 | } | |
b64eb60f | 1199 | break; |
67b5031e | 1200 | |
b64eb60f | 1201 | default: |
67b5031e MW |
1202 | /* A bareword. Process it according to the current coding system. */ |
1203 | ||
1204 | switch (code) { | |
1205 | case TVCODE_BARE: | |
1206 | ungetc(ch, tv->fp); | |
1207 | if (collect_bare(&d, tv)) goto done; | |
1208 | break; | |
1209 | default: | |
1210 | assert(ccl); | |
1211 | ungetc(ch, tv->fp); DRESET(&w); | |
1212 | if (tvec_readword(tv, &w, ";", "%s-encoded fragment", ccl->name)) | |
1213 | { rc = -1; goto end; } | |
1214 | if (!cdc) cdc = ccl->decoder(cdf); | |
1215 | err = cdc->ops->code(cdc, w.buf, w.len, &d); | |
1216 | if (err) { | |
1217 | tvec_error(tv, "invalid %s fragment `%s': %s", | |
1218 | ccl->name, w.buf, codec_strerror(err)); | |
1219 | rc = -1; goto end; | |
1220 | } | |
1221 | break; | |
1222 | } | |
1223 | break; | |
b64eb60f MW |
1224 | } |
1225 | } while (!tvec_nexttoken(tv)); | |
1226 | ||
1227 | done: | |
67b5031e MW |
1228 | /* Wrap things up. */ |
1229 | if (cdc && flush_codec(cdc, &d, tv)) { rc = -1; goto end; } | |
1230 | cdc = 0; | |
b64eb60f MW |
1231 | if (*sz_inout <= d.len) |
1232 | { xfree(*p_inout); *p_inout = xmalloc(d.len + 1); } | |
1233 | p = *p_inout; memcpy(p, d.buf, d.len); p[d.len] = 0; *sz_inout = d.len; | |
882a39c1 | 1234 | rc = 0; |
67b5031e | 1235 | |
882a39c1 | 1236 | end: |
67b5031e MW |
1237 | /* Clean up any debris. */ |
1238 | if (cdc) cdc->ops->destroy(cdc); | |
1239 | if (pp) xfree(pp); | |
b64eb60f | 1240 | dstr_destroy(&d); dstr_destroy(&w); |
882a39c1 | 1241 | return (rc); |
b64eb60f MW |
1242 | } |
1243 | ||
1244 | /*----- Skeleton ----------------------------------------------------------*/ | |
1245 | /* | |
1246 | static void init_...(union tvec_regval *rv, const struct tvec_regdef *rd) | |
1247 | static void release_...(union tvec_regval *rv, const struct tvec_regdef *rd) | |
1248 | static int eq_...(const union tvec_regval *rv0, const union tvec_regval *rv1, | |
1249 | const struct tvec_regdef *rd) | |
b64eb60f MW |
1250 | static int tobuf_...(buf *b, const union tvec_regval *rv, |
1251 | const struct tvec_regdef *rd) | |
1252 | static int frombuf_...(buf *b, union tvec_regval *rv, | |
1253 | const struct tvec_regdef *rd) | |
e63124bc MW |
1254 | static int parse_...(union tvec_regval *rv, const struct tvec_regdef *rd, |
1255 | struct tvec_state *tv) | |
b64eb60f MW |
1256 | static void dump_...(const union tvec_regval *rv, |
1257 | const struct tvec_regdef *rd, | |
1258 | struct tvec_state *tv, unsigned style) | |
1259 | ||
1260 | const struct tvec_regty tvty_... = { | |
e63124bc | 1261 | init_..., release_..., eq_..., |
b64eb60f MW |
1262 | tobuf_..., frombuf_..., |
1263 | parse_..., dump_... | |
1264 | }; | |
1265 | */ | |
1266 | /*----- Signed and unsigned integer types ---------------------------------*/ | |
1267 | ||
1268 | static void init_int(union tvec_regval *rv, const struct tvec_regdef *rd) | |
1269 | { rv->i = 0; } | |
1270 | ||
1271 | static void init_uint(union tvec_regval *rv, const struct tvec_regdef *rd) | |
1272 | { rv->u = 0; } | |
1273 | ||
b64eb60f MW |
1274 | static int eq_int(const union tvec_regval *rv0, const union tvec_regval *rv1, |
1275 | const struct tvec_regdef *rd) | |
1276 | { return (rv0->i == rv1->i); } | |
1277 | ||
1278 | static int eq_uint(const union tvec_regval *rv0, | |
1279 | const union tvec_regval *rv1, | |
1280 | const struct tvec_regdef *rd) | |
1281 | { return (rv0->u == rv1->u); } | |
1282 | ||
b64eb60f MW |
1283 | static int tobuf_int(buf *b, const union tvec_regval *rv, |
1284 | const struct tvec_regdef *rd) | |
1285 | { return (signed_to_buf(b, rv->i)); } | |
1286 | ||
1287 | static int tobuf_uint(buf *b, const union tvec_regval *rv, | |
1288 | const struct tvec_regdef *rd) | |
1289 | { return (unsigned_to_buf(b, rv->u)); } | |
1290 | ||
1291 | static int frombuf_int(buf *b, union tvec_regval *rv, | |
1292 | const struct tvec_regdef *rd) | |
882a39c1 | 1293 | { return (signed_from_buf(b, &rv->i)); } |
b64eb60f MW |
1294 | |
1295 | static int frombuf_uint(buf *b, union tvec_regval *rv, | |
1296 | const struct tvec_regdef *rd) | |
1297 | { return (unsigned_from_buf(b, &rv->u)); } | |
1298 | ||
882a39c1 MW |
1299 | static int parse_int(union tvec_regval *rv, const struct tvec_regdef *rd, |
1300 | struct tvec_state *tv) | |
b64eb60f MW |
1301 | { |
1302 | dstr d = DSTR_INIT; | |
882a39c1 | 1303 | int rc; |
b64eb60f | 1304 | |
e63124bc MW |
1305 | if (tvec_readword(tv, &d, ";", "signed integer")) |
1306 | { rc = -1; goto end; } | |
1307 | if (parse_signed(&rv->i, d.buf, rd->arg.p, tv)) | |
1308 | { rc = -1; goto end; } | |
1309 | if (tvec_flushtoeol(tv, 0)) | |
1310 | { rc = -1; goto end; } | |
882a39c1 MW |
1311 | rc = 0; |
1312 | end: | |
b64eb60f | 1313 | dstr_destroy(&d); |
882a39c1 | 1314 | return (rc); |
b64eb60f MW |
1315 | } |
1316 | ||
882a39c1 MW |
1317 | static int parse_uint(union tvec_regval *rv, const struct tvec_regdef *rd, |
1318 | struct tvec_state *tv) | |
b64eb60f MW |
1319 | { |
1320 | dstr d = DSTR_INIT; | |
882a39c1 | 1321 | int rc; |
b64eb60f | 1322 | |
e63124bc MW |
1323 | if (tvec_readword(tv, &d, ";", "unsigned integer")) |
1324 | { rc = -1; goto end; } | |
1325 | if (parse_unsigned(&rv->u, d.buf, rd->arg.p, tv)) | |
1326 | { rc = -1; goto end; } | |
1327 | if (tvec_flushtoeol(tv, 0)) | |
1328 | { rc = -1; goto end; } | |
882a39c1 MW |
1329 | rc = 0; |
1330 | end: | |
b64eb60f | 1331 | dstr_destroy(&d); |
882a39c1 | 1332 | return (rc); |
b64eb60f MW |
1333 | } |
1334 | ||
1335 | static void dump_int(const union tvec_regval *rv, | |
1336 | const struct tvec_regdef *rd, | |
e63124bc MW |
1337 | unsigned style, |
1338 | const struct gprintf_ops *gops, void *go) | |
b64eb60f | 1339 | { |
b64eb60f | 1340 | |
e63124bc | 1341 | gprintf(gops, go, "%ld", rv->i); |
b64eb60f | 1342 | if (!(style&TVSF_COMPACT)) { |
3efcfd2d MW |
1343 | gprintf(gops, go, " ; = "); |
1344 | format_signed_hex(gops, go, rv->i); | |
1345 | maybe_format_signed_char(gops, go, rv->i); | |
b64eb60f MW |
1346 | } |
1347 | } | |
1348 | ||
1349 | static void dump_uint(const union tvec_regval *rv, | |
1350 | const struct tvec_regdef *rd, | |
e63124bc MW |
1351 | unsigned style, |
1352 | const struct gprintf_ops *gops, void *go) | |
b64eb60f | 1353 | { |
e63124bc MW |
1354 | gprintf(gops, go, "%lu", rv->u); |
1355 | if (!(style&TVSF_COMPACT)) { | |
3efcfd2d MW |
1356 | gprintf(gops, go, " ; = "); |
1357 | format_unsigned_hex(gops, go, rv->u); | |
1358 | maybe_format_unsigned_char(gops, go, rv->u); | |
e63124bc | 1359 | } |
b64eb60f MW |
1360 | } |
1361 | ||
1362 | const struct tvec_regty tvty_int = { | |
3efcfd2d | 1363 | init_int, trivial_release, eq_int, |
b64eb60f MW |
1364 | tobuf_int, frombuf_int, |
1365 | parse_int, dump_int | |
1366 | }; | |
1367 | ||
1368 | const struct tvec_irange | |
1369 | tvrange_schar = { SCHAR_MIN, SCHAR_MAX }, | |
1370 | tvrange_short = { SHRT_MIN, SHRT_MAX }, | |
1371 | tvrange_int = { INT_MIN, INT_MAX }, | |
1372 | tvrange_long = { LONG_MIN, LONG_MAX }, | |
1373 | tvrange_sbyte = { -128, 127 }, | |
1374 | tvrange_i16 = { -32768, +32767 }, | |
1375 | tvrange_i32 = { -2147483648, 2147483647 }; | |
1376 | ||
1377 | const struct tvec_regty tvty_uint = { | |
3efcfd2d | 1378 | init_uint, trivial_release, eq_uint, |
b64eb60f MW |
1379 | tobuf_uint, frombuf_uint, |
1380 | parse_uint, dump_uint | |
1381 | }; | |
1382 | ||
1383 | const struct tvec_urange | |
1384 | tvrange_uchar = { 0, UCHAR_MAX }, | |
1385 | tvrange_ushort = { 0, USHRT_MAX }, | |
1386 | tvrange_uint = { 0, UINT_MAX }, | |
1387 | tvrange_ulong = { 0, ULONG_MAX }, | |
1388 | tvrange_size = { 0, (size_t)-1 }, | |
1389 | tvrange_byte = { 0, 255 }, | |
1390 | tvrange_u16 = { 0, 65535 }, | |
1391 | tvrange_u32 = { 0, 4294967296 }; | |
1392 | ||
67b5031e MW |
1393 | /* --- @tvec_claimeq_int@ --- * |
1394 | * | |
1395 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1396 | * @long i0, i1@ = two signed integers | |
1397 | * @const char *file@, @unsigned @lno@ = calling file and line | |
1398 | * @const char *expr@ = the expression to quote on failure | |
1399 | * | |
1400 | * Returns: Nonzero if @i0@ and @i1@ are equal, otherwise zero. | |
1401 | * | |
1402 | * Use: Check that values of @i0@ and @i1@ are equal. As for | |
1403 | * @tvec_claim@ above, a test case is automatically begun and | |
1404 | * ended if none is already underway. If the values are | |
1405 | * unequal, then @tvec_fail@ is called, quoting @expr@, and the | |
1406 | * mismatched values are dumped: @i0@ is printed as the output | |
1407 | * value and @i1@ is printed as the input reference. | |
1408 | */ | |
1409 | ||
b64eb60f MW |
1410 | int tvec_claimeq_int(struct tvec_state *tv, long i0, long i1, |
1411 | const char *file, unsigned lno, const char *expr) | |
1412 | { | |
3efcfd2d | 1413 | tv->out[0].v.i = i0; tv->in[0].v.i = i1; |
b64eb60f MW |
1414 | return (tvec_claimeq(tv, &tvty_int, 0, file, lno, expr)); |
1415 | } | |
1416 | ||
67b5031e MW |
1417 | /* --- @tvec_claimeq_uint@ --- * |
1418 | * | |
1419 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1420 | * @unsigned long u0, u1@ = two unsigned integers | |
1421 | * @const char *file@, @unsigned @lno@ = calling file and line | |
1422 | * @const char *expr@ = the expression to quote on failure | |
1423 | * | |
1424 | * Returns: Nonzero if @u0@ and @u1@ are equal, otherwise zero. | |
1425 | * | |
1426 | * Use: Check that values of @u0@ and @u1@ are equal. As for | |
1427 | * @tvec_claim@ above, a test case is automatically begun and | |
1428 | * ended if none is already underway. If the values are | |
1429 | * unequal, then @tvec_fail@ is called, quoting @expr@, and the | |
1430 | * mismatched values are dumped: @u0@ is printed as the output | |
1431 | * value and @u1@ is printed as the input reference. | |
1432 | */ | |
1433 | ||
b64eb60f MW |
1434 | int tvec_claimeq_uint(struct tvec_state *tv, |
1435 | unsigned long u0, unsigned long u1, | |
1436 | const char *file, unsigned lno, const char *expr) | |
1437 | { | |
3efcfd2d | 1438 | tv->out[0].v.u = u0; tv->in[0].v.u = u1; |
b64eb60f MW |
1439 | return (tvec_claimeq(tv, &tvty_uint, 0, file, lno, expr)); |
1440 | } | |
1441 | ||
3efcfd2d | 1442 | /*----- Floating-point type -----------------------------------------------*/ |
e63124bc MW |
1443 | |
1444 | static void init_float(union tvec_regval *rv, const struct tvec_regdef *rd) | |
1445 | { rv->f = 0.0; } | |
e63124bc MW |
1446 | |
1447 | static int eq_float(const union tvec_regval *rv0, | |
1448 | const union tvec_regval *rv1, | |
1449 | const struct tvec_regdef *rd) | |
1450 | { return (eqish_floating_p(rv0->f, rv1->f, rd->arg.p)); } | |
1451 | ||
1452 | static int tobuf_float(buf *b, const union tvec_regval *rv, | |
1453 | const struct tvec_regdef *rd) | |
1454 | { return (buf_putf64l(b, rv->f)); } | |
1455 | static int frombuf_float(buf *b, union tvec_regval *rv, | |
1456 | const struct tvec_regdef *rd) | |
1457 | { return (buf_getf64l(b, &rv->f)); } | |
1458 | ||
1459 | static int parse_float(union tvec_regval *rv, const struct tvec_regdef *rd, | |
1460 | struct tvec_state *tv) | |
1461 | { | |
1462 | dstr d = DSTR_INIT; | |
1463 | int rc; | |
1464 | ||
1465 | if (tvec_readword(tv, &d, ";", "floating-point number")) | |
1466 | { rc = -1; goto end; } | |
1467 | if (parse_floating(&rv->f, d.buf, rd->arg.p, tv)) | |
1468 | { rc = -1; goto end; } | |
1469 | if (tvec_flushtoeol(tv, 0)) | |
1470 | { rc = -1; goto end; } | |
1471 | rc = 0; | |
1472 | end: | |
1473 | dstr_destroy(&d); | |
1474 | return (rc); | |
1475 | } | |
1476 | ||
1477 | static void dump_float(const union tvec_regval *rv, | |
1478 | const struct tvec_regdef *rd, | |
1479 | unsigned style, | |
1480 | const struct gprintf_ops *gops, void *go) | |
1481 | { format_floating(gops, go, rv->f); } | |
1482 | ||
1483 | const struct tvec_regty tvty_float = { | |
3efcfd2d | 1484 | init_float, trivial_release, eq_float, |
e63124bc MW |
1485 | tobuf_float, frombuf_float, |
1486 | parse_float, dump_float | |
1487 | }; | |
1488 | ||
67b5031e MW |
1489 | /* --- @tvec_claimeqish_float@ --- * |
1490 | * | |
1491 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1492 | * @double f0, f1@ = two floating-point numbers | |
1493 | * @unsigned f@ = flags (@TVFF_...@) | |
1494 | * @double delta@ = maximum tolerable difference | |
1495 | * @const char *file@, @unsigned @lno@ = calling file and line | |
1496 | * @const char *expr@ = the expression to quote on failure | |
1497 | * | |
1498 | * Returns: Nonzero if @f0@ and @u1@ are sufficiently close, otherwise | |
1499 | * zero. | |
1500 | * | |
1501 | * Use: Check that values of @f0@ and @f1@ are sufficiently close. | |
1502 | * As for @tvec_claim@ above, a test case is automatically begun | |
1503 | * and ended if none is already underway. If the values are | |
1504 | * too far apart, then @tvec_fail@ is called, quoting @expr@, | |
1505 | * and the mismatched values are dumped: @f0@ is printed as the | |
1506 | * output value and @f1@ is printed as the input reference. | |
1507 | * | |
1508 | * The details for the comparison are as follows. | |
1509 | * | |
1510 | * * A NaN value matches any other NaN, and nothing else. | |
1511 | * | |
1512 | * * An infinity matches another infinity of the same sign, | |
1513 | * and nothing else. | |
1514 | * | |
1515 | * * If @f&TVFF_EQMASK@ is @TVFF_EXACT@, then any | |
1516 | * representable number matches only itself: in particular, | |
1517 | * positive and negative zero are considered distinct. | |
1518 | * (This allows tests to check that they land on the correct | |
1519 | * side of branch cuts, for example.) | |
1520 | * | |
1521 | * * If @f&TVFF_EQMASK@ is @TVFF_ABSDELTA@, then %$x$% matches | |
1522 | * %$y$% when %$|x - y| < \delta$%. | |
1523 | * | |
1524 | * * If @f&TVFF_EQMASK@ is @TVFF_RELDELTA@, then %$x$% matches | |
1525 | * %$y$% when %$|1 - y/x| < \delta$%. (Note that this | |
1526 | * criterion is asymmetric FIXME | |
1527 | */ | |
1528 | ||
e63124bc MW |
1529 | int tvec_claimeqish_float(struct tvec_state *tv, |
1530 | double f0, double f1, unsigned f, double delta, | |
1531 | const char *file, unsigned lno, | |
1532 | const char *expr) | |
1533 | { | |
1534 | struct tvec_floatinfo fi; | |
1535 | union tvec_misc arg; | |
1536 | ||
1537 | fi.f = f; fi.min = fi.max = 0.0; fi.delta = delta; arg.p = &fi; | |
3efcfd2d | 1538 | tv->out[0].v.f = f0; tv->in[0].v.f = f1; |
e63124bc MW |
1539 | return (tvec_claimeq(tv, &tvty_float, &arg, file, lno, expr)); |
1540 | } | |
e63124bc | 1541 | |
67b5031e MW |
1542 | /* --- @tvec_claimeq_float@ --- * |
1543 | * | |
1544 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1545 | * @double f0, f1@ = two floating-point numbers | |
1546 | * @const char *file@, @unsigned @lno@ = calling file and line | |
1547 | * @const char *expr@ = the expression to quote on failure | |
1548 | * | |
1549 | * Returns: Nonzero if @f0@ and @u1@ are identical, otherwise zero. | |
1550 | * | |
1551 | * Use: Check that values of @f0@ and @f1@ are identical. The | |
1552 | * function is exactly equivalent to @tvec_claimeqish_float@ | |
1553 | * with @f == TVFF_EXACT@. | |
1554 | */ | |
1555 | ||
1556 | int tvec_claimeq_float(struct tvec_state *tv, | |
1557 | double f0, double f1, | |
1558 | const char *file, unsigned lno, | |
1559 | const char *expr) | |
1560 | { | |
1561 | return (tvec_claimeqish_float(tv, f0, f1, TVFF_EXACT, 0.0, | |
1562 | file, lno, expr)); | |
1563 | } | |
1564 | ||
b64eb60f MW |
1565 | /*----- Enumerations ------------------------------------------------------*/ |
1566 | ||
3efcfd2d MW |
1567 | #define init_ienum init_int |
1568 | #define init_uenum init_uint | |
1569 | #define init_fenum init_float | |
1570 | static void init_penum(union tvec_regval *rv, const struct tvec_regdef *rd) | |
1571 | { rv->p = 0; } | |
b64eb60f | 1572 | |
3efcfd2d MW |
1573 | #define eq_ienum eq_int |
1574 | #define eq_uenum eq_uint | |
1575 | static int eq_fenum(const union tvec_regval *rv0, | |
1576 | const union tvec_regval *rv1, | |
1577 | const struct tvec_regdef *rd) | |
b64eb60f | 1578 | { |
3efcfd2d MW |
1579 | const struct tvec_fenuminfo *ei = rd->arg.p; |
1580 | return (eqish_floating_p(rv0->f, rv1->f, ei->fi)); | |
b64eb60f | 1581 | } |
3efcfd2d MW |
1582 | static int eq_penum(const union tvec_regval *rv0, |
1583 | const union tvec_regval *rv1, | |
1584 | const struct tvec_regdef *rd) | |
1585 | { return (rv0->p == rv1->p); } | |
b64eb60f | 1586 | |
3efcfd2d MW |
1587 | #define tobuf_ienum tobuf_int |
1588 | #define tobuf_uenum tobuf_uint | |
1589 | #define tobuf_fenum tobuf_float | |
1590 | static int tobuf_penum(buf *b, const union tvec_regval *rv, | |
1591 | const struct tvec_regdef *rd) | |
b64eb60f | 1592 | { |
3efcfd2d | 1593 | const struct tvec_penuminfo *pei = rd->arg.p; |
e63124bc MW |
1594 | const struct tvec_passoc *pa; |
1595 | long i; | |
b64eb60f | 1596 | |
3efcfd2d MW |
1597 | for (pa = pei->av, i = 0; pa->tag; pa++, i++) |
1598 | if (pa->p == rv->p) goto found; | |
1599 | if (!rv->p) i = -1; | |
1600 | else return (-1); | |
1601 | found: | |
1602 | return (signed_to_buf(b, i)); | |
b64eb60f MW |
1603 | } |
1604 | ||
3efcfd2d MW |
1605 | #define frombuf_ienum frombuf_int |
1606 | #define frombuf_uenum frombuf_uint | |
1607 | #define frombuf_fenum frombuf_float | |
1608 | static int frombuf_penum(buf *b, union tvec_regval *rv, | |
b64eb60f MW |
1609 | const struct tvec_regdef *rd) |
1610 | { | |
3efcfd2d | 1611 | const struct tvec_penuminfo *pei = rd->arg.p; |
e63124bc MW |
1612 | const struct tvec_passoc *pa; |
1613 | long i, n; | |
b64eb60f | 1614 | |
3efcfd2d MW |
1615 | for (pa = pei->av, n = 0; pa->tag; pa++, n++); |
1616 | if (signed_from_buf(b, &i)) return (-1); | |
1617 | if (0 <= i && i < n) rv->p = (/*unconst*/ void *)pei->av[i].p; | |
1618 | else if (i == -1) rv->p = 0; | |
1619 | else return (-1); | |
1620 | return (0); | |
b64eb60f MW |
1621 | } |
1622 | ||
3efcfd2d MW |
1623 | #define DEFPARSE_ENUM(tag_, ty, slot) \ |
1624 | static int parse_##slot##enum(union tvec_regval *rv, \ | |
1625 | const struct tvec_regdef *rd, \ | |
1626 | struct tvec_state *tv) \ | |
1627 | { \ | |
1628 | const struct tvec_##slot##enuminfo *ei = rd->arg.p; \ | |
1629 | const struct tvec_##slot##assoc *a; \ | |
1630 | dstr d = DSTR_INIT; \ | |
1631 | int rc; \ | |
1632 | \ | |
1633 | if (tvec_readword(tv, &d, ";", "enumeration tag or " LITSTR_##tag_)) \ | |
1634 | { rc = -1; goto end; } \ | |
1635 | for (a = ei->av; a->tag; a++) \ | |
1636 | if (STRCMP(a->tag, ==, d.buf)) { FOUND_##tag_ goto done; } \ | |
1637 | MISSING_##tag_ \ | |
1638 | done: \ | |
1639 | if (tvec_flushtoeol(tv, 0)) { rc = -1; goto end; } \ | |
1640 | rc = 0; \ | |
1641 | end: \ | |
1642 | dstr_destroy(&d); \ | |
1643 | return (rc); \ | |
1644 | } | |
b64eb60f | 1645 | |
3efcfd2d MW |
1646 | #define LITSTR_INT "literal signed integer" |
1647 | #define FOUND_INT rv->i = a->i; | |
1648 | #define MISSING_INT if (parse_signed(&rv->i, d.buf, ei->ir, tv)) \ | |
1649 | { rc = -1; goto end; } | |
1650 | ||
1651 | #define LITSTR_UINT "literal unsigned integer" | |
1652 | #define FOUND_UINT rv->u = a->u; | |
1653 | #define MISSING_UINT if (parse_unsigned(&rv->u, d.buf, ei->ur, tv)) \ | |
1654 | { rc = -1; goto end; } | |
1655 | ||
1656 | #define LITSTR_FLT "literal floating-point number, " \ | |
1657 | "`#-inf', `#+inf', or `#nan'" | |
1658 | #define FOUND_FLT rv->f = a->f; | |
1659 | #define MISSING_FLT if (parse_floating(&rv->f, d.buf, ei->fi, tv)) \ | |
1660 | { rc = -1; goto end; } | |
1661 | ||
1662 | #define LITSTR_PTR "`#nil'" | |
1663 | #define FOUND_PTR rv->p = (/*unconst*/ void *)a->p; | |
1664 | #define MISSING_PTR if (STRCMP(d.buf, ==, "#nil")) \ | |
1665 | rv->p = 0; \ | |
1666 | else { \ | |
1667 | tvec_error(tv, "unknown `%s' value `%s'", \ | |
1668 | ei->name, d.buf); \ | |
1669 | rc = -1; goto end; \ | |
1670 | } | |
1671 | ||
1672 | TVEC_MISCSLOTS(DEFPARSE_ENUM) | |
1673 | ||
1674 | #undef LITSTR_INT | |
1675 | #undef FOUND_INT | |
1676 | #undef MISSING_INT | |
1677 | ||
1678 | #undef LITSTR_UINT | |
1679 | #undef FOUND_UINT | |
1680 | #undef MISSING_UINT | |
1681 | ||
1682 | #undef LITSTR_FLT | |
1683 | #undef FOUND_FLT | |
1684 | #undef MISSING_FLT | |
1685 | ||
1686 | #undef LITSTR_PTR | |
1687 | #undef FOUND_PTR | |
1688 | #undef MISSING_PTR | |
1689 | ||
1690 | #undef DEFPARSE_ENUM | |
1691 | ||
1692 | #define DEFDUMP_ENUM(tag_, ty, slot) \ | |
1693 | static void dump_##slot##enum(const union tvec_regval *rv, \ | |
1694 | const struct tvec_regdef *rd, \ | |
1695 | unsigned style, \ | |
1696 | const struct gprintf_ops *gops, void *go) \ | |
1697 | { \ | |
1698 | const struct tvec_##slot##enuminfo *ei = rd->arg.p; \ | |
1699 | const struct tvec_##slot##assoc *a; \ | |
1700 | \ | |
1701 | for (a = ei->av; a->tag; a++) \ | |
1702 | if (rv->slot == a->slot) { \ | |
1703 | gprintf(gops, go, "%s", a->tag); \ | |
1704 | if (style&TVSF_COMPACT) return; \ | |
1705 | gprintf(gops, go, " ; = "); break; \ | |
1706 | } \ | |
1707 | \ | |
1708 | PRINTRAW_##tag_ \ | |
b64eb60f MW |
1709 | } |
1710 | ||
3efcfd2d MW |
1711 | #define MAYBE_PRINT_EXTRA \ |
1712 | if (style&TVSF_COMPACT) ; \ | |
1713 | else if (!a->tag) { gprintf(gops, go, " ; = "); goto _extra; } \ | |
1714 | else if (1) { gprintf(gops, go, " = "); goto _extra; } \ | |
1715 | else _extra: | |
b64eb60f | 1716 | |
3efcfd2d MW |
1717 | #define PRINTRAW_INT gprintf(gops, go, "%ld", rv->i); \ |
1718 | MAYBE_PRINT_EXTRA { \ | |
1719 | format_signed_hex(gops, go, rv->i); \ | |
1720 | maybe_format_signed_char(gops, go, rv->i); \ | |
1721 | } | |
b64eb60f | 1722 | |
3efcfd2d MW |
1723 | #define PRINTRAW_UINT gprintf(gops, go, "%lu", rv->u); \ |
1724 | MAYBE_PRINT_EXTRA { \ | |
1725 | format_unsigned_hex(gops, go, rv->u); \ | |
1726 | maybe_format_unsigned_char(gops, go, rv->u); \ | |
1727 | } | |
1728 | ||
1729 | #define PRINTRAW_FLT format_floating(gops, go, rv->f); | |
1730 | ||
1731 | #define PRINTRAW_PTR if (!rv->p) gprintf(gops, go, "#nil"); \ | |
e63124bc | 1732 | else gprintf(gops, go, "#<%s %p>", ei->name, rv->p); |
b64eb60f | 1733 | |
3efcfd2d | 1734 | TVEC_MISCSLOTS(DEFDUMP_ENUM) |
b64eb60f | 1735 | |
3efcfd2d MW |
1736 | #undef PRINTRAW_INT |
1737 | #undef PRINTRAW_UINT | |
1738 | #undef PRINTRAW_FLT | |
1739 | #undef PRINTRAW_PTR | |
1740 | ||
1741 | #undef MAYBE_PRINT_EXTRA | |
1742 | #undef DEFDUMP_ENUM | |
1743 | ||
1744 | #define DEFTY_ENUM(tag, ty, slot) \ | |
1745 | const struct tvec_regty tvty_##slot##enum = { \ | |
1746 | init_##slot##enum, trivial_release, eq_##slot##enum, \ | |
1747 | tobuf_##slot##enum, frombuf_##slot##enum, \ | |
1748 | parse_##slot##enum, dump_##slot##enum \ | |
1749 | }; | |
1750 | TVEC_MISCSLOTS(DEFTY_ENUM) | |
1751 | #undef DEFTY_ENUM | |
b64eb60f | 1752 | |
e63124bc MW |
1753 | static const struct tvec_iassoc bool_assoc[] = { |
1754 | { "nil", 0 }, | |
1755 | { "false", 0 }, | |
1756 | { "f", 0 }, | |
1757 | { "no", 0 }, | |
1758 | { "n", 0 }, | |
1759 | { "off", 0 }, | |
1760 | ||
1761 | { "t", 1 }, | |
1762 | { "true", 1 }, | |
1763 | { "yes", 1 }, | |
1764 | { "y", 1 }, | |
1765 | { "on", 1 }, | |
1766 | ||
20ba6b0b | 1767 | TVEC_ENDENUM |
e63124bc MW |
1768 | }; |
1769 | ||
1770 | const struct tvec_ienuminfo tvenum_bool = | |
3efcfd2d | 1771 | { "bool", bool_assoc, &tvrange_int }; |
e63124bc | 1772 | |
20ba6b0b MW |
1773 | static const struct tvec_iassoc cmp_assoc[] = { |
1774 | { "<", -1 }, | |
1775 | { "less", -1 }, | |
1776 | { "lt", -1 }, | |
1777 | ||
1778 | { "=", 0 }, | |
1779 | { "equal", 0 }, | |
1780 | { "eq", 0 }, | |
1781 | ||
1782 | { ">", +1 }, | |
1783 | { "greater", +1 }, | |
1784 | { "gt", +1 }, | |
1785 | ||
1786 | TVEC_ENDENUM | |
1787 | }; | |
1788 | ||
1789 | const struct tvec_ienuminfo tvenum_cmp = | |
1790 | { "cmp", cmp_assoc, &tvrange_int }; | |
1791 | ||
67b5031e MW |
1792 | /* --- @tvec_claimeq_tenum@ --- * |
1793 | * | |
1794 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1795 | * @const struct tvec_typeenuminfo *ei@ = enumeration type info | |
1796 | * @ty t0, t1@ = two values | |
1797 | * @const char *file@, @unsigned @lno@ = calling file and line | |
1798 | * @const char *expr@ = the expression to quote on failure | |
1799 | * | |
1800 | * Returns: Nonzero if @t0@ and @t1@ are equal, otherwise zero. | |
1801 | * | |
1802 | * Use: Check that values of @t0@ and @t1@ are equal. As for | |
1803 | * @tvec_claim@ above, a test case is automatically begun and | |
1804 | * ended if none is already underway. If the values are | |
1805 | * unequal, then @tvec_fail@ is called, quoting @expr@, and the | |
1806 | * mismatched values are dumped: @t0@ is printed as the output | |
1807 | * value and @t1@ is printed as the input reference. | |
1808 | */ | |
1809 | ||
b64eb60f | 1810 | #define DEFCLAIM(tag, ty, slot) \ |
e63124bc MW |
1811 | int tvec_claimeq_##slot##enum \ |
1812 | (struct tvec_state *tv, \ | |
1813 | const struct tvec_##slot##enuminfo *ei, ty e0, ty e1, \ | |
1814 | const char *file, unsigned lno, const char *expr) \ | |
b64eb60f MW |
1815 | { \ |
1816 | union tvec_misc arg; \ | |
1817 | \ | |
b64eb60f | 1818 | arg.p = ei; \ |
3efcfd2d MW |
1819 | tv->out[0].v.slot = GET_##tag(e0); \ |
1820 | tv->in[0].v.slot = GET_##tag(e1); \ | |
1821 | return (tvec_claimeq(tv, &tvty_##slot##enum, &arg, \ | |
1822 | file, lno, expr)); \ | |
b64eb60f MW |
1823 | } |
1824 | #define GET_INT(e) (e) | |
1825 | #define GET_UINT(e) (e) | |
e63124bc | 1826 | #define GET_FLT(e) (e) |
b64eb60f MW |
1827 | #define GET_PTR(e) ((/*unconst*/ void *)(e)) |
1828 | TVEC_MISCSLOTS(DEFCLAIM) | |
1829 | #undef DEFCLAIM | |
1830 | #undef GET_INT | |
1831 | #undef GET_UINT | |
e63124bc | 1832 | #undef GET_FLT |
b64eb60f MW |
1833 | #undef GET_PTR |
1834 | ||
1835 | /*----- Flag types --------------------------------------------------------*/ | |
1836 | ||
882a39c1 MW |
1837 | static int parse_flags(union tvec_regval *rv, const struct tvec_regdef *rd, |
1838 | struct tvec_state *tv) | |
b64eb60f MW |
1839 | { |
1840 | const struct tvec_flaginfo *fi = rd->arg.p; | |
1841 | const struct tvec_flag *f; | |
1842 | unsigned long m = 0, v = 0, t; | |
1843 | dstr d = DSTR_INIT; | |
882a39c1 | 1844 | int ch, rc; |
b64eb60f MW |
1845 | |
1846 | for (;;) { | |
882a39c1 MW |
1847 | DRESET(&d); |
1848 | if (tvec_readword(tv, &d, "|;", "flag name or integer")) | |
1849 | { rc = -1; goto end; } | |
b64eb60f MW |
1850 | |
1851 | for (f = fi->fv; f->tag; f++) | |
1852 | if (STRCMP(f->tag, ==, d.buf)) { | |
882a39c1 MW |
1853 | if (m&f->m) |
1854 | { tvec_error(tv, "colliding flag setting"); rc = -1; goto end; } | |
1855 | else | |
1856 | { m |= f->m; v |= f->v; goto next; } | |
b64eb60f MW |
1857 | } |
1858 | ||
e63124bc MW |
1859 | if (parse_unsigned(&t, d.buf, fi->range, tv)) |
1860 | { rc = -1; goto end; } | |
882a39c1 | 1861 | v |= t; |
b64eb60f MW |
1862 | next: |
1863 | if (tvec_nexttoken(tv)) break; | |
882a39c1 MW |
1864 | ch = getc(tv->fp); |
1865 | if (ch != '|') { tvec_syntax(tv, ch, "`|'"); rc = -1; goto end; } | |
1866 | if (tvec_nexttoken(tv)) | |
1867 | { tvec_syntax(tv, '\n', "flag name or integer"); rc = -1; goto end; } | |
b64eb60f MW |
1868 | } |
1869 | rv->u = v; | |
882a39c1 MW |
1870 | rc = 0; |
1871 | end: | |
1872 | dstr_destroy(&d); | |
1873 | return (rc); | |
b64eb60f MW |
1874 | } |
1875 | ||
1876 | static void dump_flags(const union tvec_regval *rv, | |
1877 | const struct tvec_regdef *rd, | |
e63124bc MW |
1878 | unsigned style, |
1879 | const struct gprintf_ops *gops, void *go) | |
b64eb60f MW |
1880 | { |
1881 | const struct tvec_flaginfo *fi = rd->arg.p; | |
1882 | const struct tvec_flag *f; | |
1883 | unsigned long m = ~(unsigned long)0, v = rv->u; | |
1884 | const char *sep; | |
1885 | ||
1886 | for (f = fi->fv, sep = ""; f->tag; f++) | |
1887 | if ((m&f->m) && (v&f->m) == f->v) { | |
e63124bc | 1888 | gprintf(gops, go, "%s%s", sep, f->tag); m &= ~f->m; |
b64eb60f MW |
1889 | sep = style&TVSF_COMPACT ? "|" : " | "; |
1890 | } | |
1891 | ||
e63124bc | 1892 | if (v&m) gprintf(gops, go, "%s0x%0*lx", sep, hex_width(v), v&m); |
b64eb60f MW |
1893 | |
1894 | if (!(style&TVSF_COMPACT)) | |
e63124bc | 1895 | gprintf(gops, go, " ; = 0x%0*lx", hex_width(rv->u), rv->u); |
b64eb60f MW |
1896 | } |
1897 | ||
1898 | const struct tvec_regty tvty_flags = { | |
3efcfd2d | 1899 | init_uint, trivial_release, eq_uint, |
b64eb60f MW |
1900 | tobuf_uint, frombuf_uint, |
1901 | parse_flags, dump_flags | |
1902 | }; | |
1903 | ||
67b5031e MW |
1904 | /* --- @tvec_claimeq_flags@ --- * |
1905 | * | |
1906 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1907 | * @const struct tvec_flaginfo *fi@ = flags type info | |
1908 | * @unsigned long f0, f1@ = two values | |
1909 | * @const char *file@, @unsigned @lno@ = calling file and line | |
1910 | * @const char *expr@ = the expression to quote on failure | |
1911 | * | |
1912 | * Returns: Nonzero if @f0@ and @f1@ are equal, otherwise zero. | |
1913 | * | |
1914 | * Use: Check that values of @f0@ and @f1@ are equal. As for | |
1915 | * @tvec_claim@ above, a test case is automatically begun and | |
1916 | * ended if none is already underway. If the values are | |
1917 | * unequal, then @tvec_fail@ is called, quoting @expr@, and the | |
1918 | * mismatched values are dumped: @f0@ is printed as the output | |
1919 | * value and @f1@ is printed as the input reference. | |
1920 | */ | |
1921 | ||
b64eb60f MW |
1922 | int tvec_claimeq_flags(struct tvec_state *tv, |
1923 | const struct tvec_flaginfo *fi, | |
1924 | unsigned long f0, unsigned long f1, | |
1925 | const char *file, unsigned lno, const char *expr) | |
1926 | { | |
1927 | union tvec_misc arg; | |
1928 | ||
3efcfd2d | 1929 | arg.p = fi; tv->out[0].v.u = f0; tv->in[0].v.u = f1; |
b64eb60f MW |
1930 | return (tvec_claimeq(tv, &tvty_flags, &arg, file, lno, expr)); |
1931 | } | |
1932 | ||
e63124bc MW |
1933 | /*----- Characters --------------------------------------------------------*/ |
1934 | ||
1935 | static int tobuf_char(buf *b, const union tvec_regval *rv, | |
67b5031e | 1936 | const struct tvec_regdef *rd) |
e63124bc MW |
1937 | { |
1938 | uint32 u; | |
1939 | if (0 <= rv->i && rv->i <= UCHAR_MAX) u = rv->i; | |
1940 | else if (rv->i == EOF) u = MASK32; | |
1941 | else return (-1); | |
1942 | return (buf_putu32l(b, u)); | |
1943 | } | |
1944 | ||
1945 | static int frombuf_char(buf *b, union tvec_regval *rv, | |
67b5031e | 1946 | const struct tvec_regdef *rd) |
e63124bc MW |
1947 | { |
1948 | uint32 u; | |
1949 | ||
1950 | if (buf_getu32l(b, &u)) return (-1); | |
1951 | if (0 <= u && u <= UCHAR_MAX) rv->i = u; | |
1952 | else if (u == MASK32) rv->i = EOF; | |
1953 | else return (-1); | |
1954 | return (0); | |
1955 | } | |
1956 | ||
1957 | static int parse_char(union tvec_regval *rv, const struct tvec_regdef *rd, | |
1958 | struct tvec_state *tv) | |
1959 | { | |
1960 | dstr d = DSTR_INIT; | |
1961 | int ch, rc; | |
1962 | unsigned f = 0; | |
1963 | #define f_quote 1u | |
1964 | ||
1965 | ch = getc(tv->fp); | |
1966 | if (ch == '#') { | |
1967 | ungetc(ch, tv->fp); | |
1968 | if (tvec_readword(tv, &d, ";", "character name")) { rc = -1; goto end; } | |
67b5031e | 1969 | if (read_charname(&ch, d.buf, RCF_EOFOK)) { |
e63124bc MW |
1970 | rc = tvec_error(tv, "unknown character name `%s'", d.buf); |
1971 | goto end; | |
1972 | } | |
67b5031e MW |
1973 | if (tvec_flushtoeol(tv, 0)) { rc = -1; goto end; } |
1974 | rv->i = ch; rc = 0; goto end; | |
e63124bc MW |
1975 | } |
1976 | ||
1977 | if (ch == '\'') { f |= f_quote; ch = getc(tv->fp); } | |
1978 | switch (ch) { | |
67b5031e MW |
1979 | case ';': |
1980 | if (!(f&f_quote)) { rc = tvec_syntax(tv, ch, "character"); goto end; } | |
1981 | goto plain; | |
1982 | case '\n': | |
1983 | if (f&f_quote) | |
1984 | { f &= ~f_quote; ungetc(ch, tv->fp); ch = '\''; goto plain; } | |
1985 | case EOF: | |
1986 | if (f&f_quote) { f &= ~f_quote; ch = '\''; goto plain; } | |
1987 | /* fall through */ | |
e63124bc | 1988 | case '\'': |
e63124bc MW |
1989 | rc = tvec_syntax(tv, ch, "character"); goto end; |
1990 | case '\\': | |
67b5031e | 1991 | if (read_charesc(&ch, tv)) return (-1); |
e63124bc MW |
1992 | default: plain: |
1993 | rv->i = ch; break; | |
1994 | } | |
1995 | if (f&f_quote) { | |
1996 | ch = getc(tv->fp); | |
1997 | if (ch != '\'') { rc = tvec_syntax(tv, ch, "`''"); goto end; } | |
1998 | } | |
1999 | if (tvec_flushtoeol(tv, 0)) { rc = -1; goto end; } | |
2000 | rc = 0; | |
2001 | end: | |
2002 | dstr_destroy(&d); | |
2003 | return (rc); | |
2004 | ||
2005 | #undef f_quote | |
2006 | } | |
2007 | ||
2008 | static void dump_char(const union tvec_regval *rv, | |
2009 | const struct tvec_regdef *rd, | |
2010 | unsigned style, | |
2011 | const struct gprintf_ops *gops, void *go) | |
2012 | { | |
67b5031e MW |
2013 | const char *p; |
2014 | unsigned f = 0; | |
2015 | #define f_semi 1u | |
2016 | ||
2017 | p = find_charname(rv->i, (style&TVSF_COMPACT) ? CTF_SHORT : CTF_PREFER); | |
2018 | if (p) { | |
2019 | gprintf(gops, go, "%s", p); | |
2020 | if (style&TVSF_COMPACT) return; | |
2021 | else { gprintf(gops, go, " ;"); f |= f_semi; } | |
2022 | } | |
2023 | ||
2024 | if (rv->i >= 0) { | |
2025 | if (f&f_semi) gprintf(gops, go, " = "); | |
2026 | switch (rv->i) { | |
2027 | case ' ': case '\\': case '\'': quote: | |
2028 | format_char(gops, go, rv->i); | |
2029 | break; | |
2030 | default: | |
2031 | if (!(style&TVSF_COMPACT) || !isprint(rv->i)) goto quote; | |
2032 | gprintf(gops, go, "%c", (int)rv->i); | |
2033 | return; | |
2034 | } | |
2035 | } | |
e63124bc MW |
2036 | |
2037 | if (!(style&TVSF_COMPACT)) { | |
67b5031e MW |
2038 | if (!(f&f_semi)) gprintf(gops, go, " ;"); |
2039 | gprintf(gops, go, " = %ld = ", rv->i); | |
3efcfd2d | 2040 | format_signed_hex(gops, go, rv->i); |
e63124bc | 2041 | } |
67b5031e MW |
2042 | |
2043 | #undef f_semi | |
e63124bc MW |
2044 | } |
2045 | ||
2046 | const struct tvec_regty tvty_char = { | |
3efcfd2d | 2047 | init_int, trivial_release, eq_int, |
e63124bc MW |
2048 | tobuf_char, frombuf_char, |
2049 | parse_char, dump_char | |
2050 | }; | |
2051 | ||
67b5031e MW |
2052 | /* --- @tvec_claimeq_char@ --- * |
2053 | * | |
2054 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
2055 | * @int ch0, ch1@ = two character codes | |
2056 | * @const char *file@, @unsigned @lno@ = calling file and line | |
2057 | * @const char *expr@ = the expression to quote on failure | |
2058 | * | |
2059 | * Returns: Nonzero if @ch0@ and @ch1@ are equal, otherwise zero. | |
2060 | * | |
2061 | * Use: Check that values of @ch0@ and @ch1@ are equal. As for | |
2062 | * @tvec_claim@ above, a test case is automatically begun and | |
2063 | * ended if none is already underway. If the values are | |
2064 | * unequal, then @tvec_fail@ is called, quoting @expr@, and the | |
2065 | * mismatched values are dumped: @ch0@ is printed as the output | |
2066 | * value and @ch1@ is printed as the input reference. | |
2067 | */ | |
2068 | ||
e63124bc MW |
2069 | int tvec_claimeq_char(struct tvec_state *tv, int c0, int c1, |
2070 | const char *file, unsigned lno, const char *expr) | |
2071 | { | |
3efcfd2d | 2072 | tv->out[0].v.i = c0; tv->in[0].v.i = c1; |
e63124bc MW |
2073 | return (tvec_claimeq(tv, &tvty_char, 0, file, lno, expr)); |
2074 | } | |
2075 | ||
b64eb60f MW |
2076 | /*----- Text and byte strings ---------------------------------------------*/ |
2077 | ||
b64eb60f MW |
2078 | static void init_string(union tvec_regval *rv, const struct tvec_regdef *rd) |
2079 | { rv->str.p = 0; rv->str.sz = 0; } | |
2080 | ||
2081 | static void init_bytes(union tvec_regval *rv, const struct tvec_regdef *rd) | |
2082 | { rv->bytes.p = 0; rv->bytes.sz = 0; } | |
2083 | ||
2084 | static void release_string(union tvec_regval *rv, | |
2085 | const struct tvec_regdef *rd) | |
2086 | { xfree(rv->str.p); } | |
2087 | ||
2088 | static void release_bytes(union tvec_regval *rv, | |
2089 | const struct tvec_regdef *rd) | |
2090 | { xfree(rv->bytes.p); } | |
2091 | ||
2092 | static int eq_string(const union tvec_regval *rv0, | |
2093 | const union tvec_regval *rv1, | |
2094 | const struct tvec_regdef *rd) | |
2095 | { | |
2096 | return (rv0->str.sz == rv1->str.sz && | |
2097 | (!rv0->bytes.sz || | |
2098 | MEMCMP(rv0->str.p, ==, rv1->str.p, rv1->str.sz))); | |
2099 | } | |
2100 | ||
2101 | static int eq_bytes(const union tvec_regval *rv0, | |
2102 | const union tvec_regval *rv1, | |
2103 | const struct tvec_regdef *rd) | |
2104 | { | |
2105 | return (rv0->bytes.sz == rv1->bytes.sz && | |
2106 | (!rv0->bytes.sz || | |
2107 | MEMCMP(rv0->bytes.p, ==, rv1->bytes.p, rv1->bytes.sz))); | |
2108 | } | |
2109 | ||
b64eb60f MW |
2110 | static int tobuf_string(buf *b, const union tvec_regval *rv, |
2111 | const struct tvec_regdef *rd) | |
2112 | { return (buf_putmem32l(b, rv->str.p, rv->str.sz)); } | |
2113 | ||
2114 | static int tobuf_bytes(buf *b, const union tvec_regval *rv, | |
2115 | const struct tvec_regdef *rd) | |
2116 | { return (buf_putmem32l(b, rv->bytes.p, rv->bytes.sz)); } | |
2117 | ||
2118 | static int frombuf_string(buf *b, union tvec_regval *rv, | |
2119 | const struct tvec_regdef *rd) | |
2120 | { | |
2121 | const void *p; | |
2122 | size_t sz; | |
2123 | ||
2124 | p = buf_getmem32l(b, &sz); if (!p) return (-1); | |
67b5031e | 2125 | tvec_allocstring(rv, sz); memcpy(rv->str.p, p, sz); rv->str.p[sz] = 0; |
b64eb60f MW |
2126 | return (0); |
2127 | } | |
2128 | ||
2129 | static int frombuf_bytes(buf *b, union tvec_regval *rv, | |
2130 | const struct tvec_regdef *rd) | |
2131 | { | |
2132 | const void *p; | |
2133 | size_t sz; | |
2134 | ||
2135 | p = buf_getmem32l(b, &sz); if (!p) return (-1); | |
2136 | tvec_allocbytes(rv, sz); memcpy(rv->bytes.p, p, sz); | |
2137 | return (0); | |
2138 | } | |
2139 | ||
882a39c1 MW |
2140 | static int check_string_length(size_t sz, const struct tvec_urange *ur, |
2141 | struct tvec_state *tv) | |
b64eb60f MW |
2142 | { |
2143 | if (ur && (ur->min > sz || sz > ur->max)) | |
882a39c1 | 2144 | return (tvec_error(tv, |
67b5031e | 2145 | "invalid string length %lu; must be in [%lu .. %lu]", |
882a39c1 MW |
2146 | (unsigned long)sz, ur->min, ur->max)); |
2147 | return (0); | |
b64eb60f MW |
2148 | } |
2149 | ||
882a39c1 MW |
2150 | static int parse_string(union tvec_regval *rv, const struct tvec_regdef *rd, |
2151 | struct tvec_state *tv) | |
b64eb60f MW |
2152 | { |
2153 | void *p = rv->str.p; | |
2154 | ||
67b5031e MW |
2155 | if (read_compound_string(&p, &rv->str.sz, TVCODE_BARE, 0, tv)) |
2156 | return (-1); | |
882a39c1 MW |
2157 | rv->str.p = p; |
2158 | if (check_string_length(rv->str.sz, rd->arg.p, tv)) return (-1); | |
2159 | return (0); | |
b64eb60f MW |
2160 | } |
2161 | ||
882a39c1 MW |
2162 | static int parse_bytes(union tvec_regval *rv, const struct tvec_regdef *rd, |
2163 | struct tvec_state *tv) | |
b64eb60f MW |
2164 | { |
2165 | void *p = rv->bytes.p; | |
2166 | ||
67b5031e MW |
2167 | if (read_compound_string(&p, &rv->bytes.sz, TVCODE_HEX, 0, tv)) |
2168 | return (-1); | |
882a39c1 MW |
2169 | rv->bytes.p = p; |
2170 | if (check_string_length(rv->bytes.sz, rd->arg.p, tv)) return (-1); | |
2171 | return (0); | |
b64eb60f MW |
2172 | } |
2173 | ||
2174 | static void dump_string(const union tvec_regval *rv, | |
2175 | const struct tvec_regdef *rd, | |
e63124bc MW |
2176 | unsigned style, |
2177 | const struct gprintf_ops *gops, void *go) | |
b64eb60f MW |
2178 | { |
2179 | const unsigned char *p, *q, *l; | |
b64eb60f MW |
2180 | unsigned f = 0; |
2181 | #define f_nonword 1u | |
2182 | #define f_newline 2u | |
2183 | ||
e63124bc | 2184 | if (!rv->str.sz) { gprintf(gops, go, "\"\""); return; } |
b64eb60f MW |
2185 | |
2186 | p = (const unsigned char *)rv->str.p; l = p + rv->str.sz; | |
67b5031e MW |
2187 | switch (*p) { |
2188 | case '!': case '#': case ';': case '"': case '\'': | |
2189 | case '(': case '{': case '[': case ']': case '}': case ')': | |
2190 | f |= f_nonword; break; | |
2191 | } | |
b64eb60f MW |
2192 | for (q = p; q < l; q++) |
2193 | if (*q == '\n' && q != l - 1) f |= f_newline; | |
2194 | else if (!*q || !isgraph(*q) || *q == '\\') f |= f_nonword; | |
e63124bc | 2195 | if (f&f_newline) { gprintf(gops, go, "\n\t"); goto quote; } |
b64eb60f | 2196 | else if (f&f_nonword) goto quote; |
67b5031e MW |
2197 | |
2198 | gops->putm(go, (const char *)p, rv->str.sz); | |
2199 | return; | |
b64eb60f MW |
2200 | |
2201 | quote: | |
e63124bc | 2202 | gprintf(gops, go, "\""); |
b64eb60f | 2203 | for (q = p; q < l; q++) |
e63124bc MW |
2204 | if (!isprint(*q) || *q == '"') { |
2205 | if (p < q) gops->putm(go, (const char *)p, q - p); | |
67b5031e | 2206 | if (*q != '\n' || (style&TVSF_COMPACT)) |
3efcfd2d | 2207 | format_charesc(gops, go, *q, FCF_BRACE); |
67b5031e MW |
2208 | else { |
2209 | if (q + 1 == l) { gprintf(gops, go, "\\n\""); return; } | |
2210 | else gprintf(gops, go, "\\n\"\n\t\""); | |
2211 | } | |
2212 | p = q + 1; | |
b64eb60f | 2213 | } |
e63124bc MW |
2214 | if (p < q) gops->putm(go, (const char *)p, q - p); |
2215 | gprintf(gops, go, "\""); | |
b64eb60f MW |
2216 | |
2217 | #undef f_nonword | |
2218 | #undef f_newline | |
2219 | } | |
2220 | ||
2221 | static void dump_bytes(const union tvec_regval *rv, | |
2222 | const struct tvec_regdef *rd, | |
e63124bc MW |
2223 | unsigned style, |
2224 | const struct gprintf_ops *gops, void *go) | |
b64eb60f MW |
2225 | { |
2226 | const unsigned char *p = rv->bytes.p, *l = p + rv->bytes.sz; | |
2227 | size_t off, sz = rv->bytes.sz; | |
2228 | unsigned i, n; | |
2229 | int wd; | |
2230 | ||
2231 | if (!sz) { | |
e63124bc | 2232 | gprintf(gops, go, style&TVSF_COMPACT ? "\"\"" : "\"\" ; empty"); |
b64eb60f MW |
2233 | return; |
2234 | } | |
2235 | ||
2236 | if (style&TVSF_COMPACT) { | |
e63124bc | 2237 | while (p < l) gprintf(gops, go, "%02x", *p++); |
b64eb60f MW |
2238 | return; |
2239 | } | |
2240 | ||
e63124bc | 2241 | if (sz > 16) gprintf(gops, go, "\n\t"); |
b64eb60f MW |
2242 | |
2243 | off = 0; wd = hex_width(sz); | |
2244 | while (p < l) { | |
2245 | if (l - p < 16) n = l - p; | |
2246 | else n = 16; | |
2247 | ||
67b5031e | 2248 | for (i = 0; i < n; i++) { |
e63124bc MW |
2249 | if (i < n) gprintf(gops, go, "%02x", p[i]); |
2250 | else gprintf(gops, go, " "); | |
67b5031e | 2251 | if (i < n - 1 && i%4 == 3) gprintf(gops, go, " "); |
b64eb60f | 2252 | } |
e63124bc MW |
2253 | gprintf(gops, go, " ; "); |
2254 | if (sz > 16) gprintf(gops, go, "[%0*lx] ", wd, (unsigned long)off); | |
b64eb60f | 2255 | for (i = 0; i < n; i++) |
e63124bc | 2256 | gprintf(gops, go, "%c", isprint(p[i]) ? p[i] : '.'); |
b64eb60f | 2257 | p += n; off += n; |
e63124bc | 2258 | if (p < l) gprintf(gops, go, "\n\t"); |
b64eb60f MW |
2259 | } |
2260 | } | |
2261 | ||
2262 | const struct tvec_regty tvty_string = { | |
e63124bc | 2263 | init_string, release_string, eq_string, |
b64eb60f MW |
2264 | tobuf_string, frombuf_string, |
2265 | parse_string, dump_string | |
2266 | }; | |
2267 | ||
2268 | const struct tvec_regty tvty_bytes = { | |
e63124bc | 2269 | init_bytes, release_bytes, eq_bytes, |
b64eb60f MW |
2270 | tobuf_bytes, frombuf_bytes, |
2271 | parse_bytes, dump_bytes | |
2272 | }; | |
2273 | ||
67b5031e MW |
2274 | /* --- @tvec_claimeq_string@ --- * |
2275 | * | |
2276 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
2277 | * @const char *p0@, @size_t sz0@ = first string with length | |
2278 | * @const char *p1@, @size_t sz1@ = second string with length | |
2279 | * @const char *file@, @unsigned @lno@ = calling file and line | |
2280 | * @const char *expr@ = the expression to quote on failure | |
2281 | * | |
2282 | * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise | |
2283 | * zero. | |
2284 | * | |
2285 | * Use: Check that strings at @p0@ and @p1@ are equal. As for | |
2286 | * @tvec_claim@ above, a test case is automatically begun and | |
2287 | * ended if none is already underway. If the values are | |
2288 | * unequal, then @tvec_fail@ is called, quoting @expr@, and the | |
2289 | * mismatched values are dumped: @p0@ is printed as the output | |
2290 | * value and @p1@ is printed as the input reference. | |
2291 | */ | |
2292 | ||
b64eb60f MW |
2293 | int tvec_claimeq_string(struct tvec_state *tv, |
2294 | const char *p0, size_t sz0, | |
2295 | const char *p1, size_t sz1, | |
2296 | const char *file, unsigned lno, const char *expr) | |
2297 | { | |
3efcfd2d MW |
2298 | tv->out[0].v.str.p = (/*unconst*/ char *)p0; tv->out[0].v.str.sz = sz0; |
2299 | tv->in[0].v.str.p =(/*unconst*/ char *) p1; tv->in[0].v.str.sz = sz1; | |
b64eb60f MW |
2300 | return (tvec_claimeq(tv, &tvty_string, 0, file, lno, expr)); |
2301 | } | |
2302 | ||
67b5031e MW |
2303 | /* --- @tvec_claimeq_strz@ --- * |
2304 | * | |
2305 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
2306 | * @const char *p0, *p1@ = two strings to compare | |
2307 | * @const char *file@, @unsigned @lno@ = calling file and line | |
2308 | * @const char *expr@ = the expression to quote on failure | |
2309 | * | |
2310 | * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise | |
2311 | * zero. | |
2312 | * | |
2313 | * Use: Check that strings at @p0@ and @p1@ are equal, as for | |
2314 | * @tvec_claimeq_string@, except that the strings are assumed | |
2315 | * null-terminated, so their lengths don't need to be supplied | |
2316 | * explicitly. | |
2317 | */ | |
2318 | ||
b64eb60f MW |
2319 | int tvec_claimeq_strz(struct tvec_state *tv, |
2320 | const char *p0, const char *p1, | |
2321 | const char *file, unsigned lno, const char *expr) | |
2322 | { | |
3efcfd2d MW |
2323 | tv->out[0].v.str.p = (/*unconst*/ char *)p0; |
2324 | tv->out[0].v.str.sz = strlen(p0); | |
2325 | tv->in[0].v.str.p = (/*unconst*/ char *)p1; | |
2326 | tv->in[0].v.str.sz = strlen(p1); | |
b64eb60f MW |
2327 | return (tvec_claimeq(tv, &tvty_string, 0, file, lno, expr)); |
2328 | } | |
2329 | ||
67b5031e MW |
2330 | /* --- @tvec_claimeq_bytes@ --- * |
2331 | * | |
2332 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
2333 | * @const void *p0@, @size_t sz0@ = first string with length | |
2334 | * @const void *p1@, @size_t sz1@ = second string with length | |
2335 | * @const char *file@, @unsigned @lno@ = calling file and line | |
2336 | * @const char *expr@ = the expression to quote on failure | |
2337 | * | |
2338 | * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise | |
2339 | * zero. | |
2340 | * | |
2341 | * Use: Check that binary strings at @p0@ and @p1@ are equal. As for | |
2342 | * @tvec_claim@ above, a test case is automatically begun and | |
2343 | * ended if none is already underway. If the values are | |
2344 | * unequal, then @tvec_fail@ is called, quoting @expr@, and the | |
2345 | * mismatched values are dumped: @p0@ is printed as the output | |
2346 | * value and @p1@ is printed as the input reference. | |
2347 | */ | |
2348 | ||
b64eb60f MW |
2349 | int tvec_claimeq_bytes(struct tvec_state *tv, |
2350 | const void *p0, size_t sz0, | |
2351 | const void *p1, size_t sz1, | |
2352 | const char *file, unsigned lno, const char *expr) | |
2353 | { | |
3efcfd2d MW |
2354 | tv->out[0].v.bytes.p = (/*unconst*/ void *)p0; |
2355 | tv->out[0].v.bytes.sz = sz0; | |
2356 | tv->in[0].v.bytes.p = (/*unconst*/ void *)p1; | |
2357 | tv->in[0].v.bytes.sz = sz1; | |
b64eb60f MW |
2358 | return (tvec_claimeq(tv, &tvty_bytes, 0, file, lno, expr)); |
2359 | } | |
2360 | ||
67b5031e MW |
2361 | /* --- @tvec_allocstring@, @tvec_allocbytes@ --- * |
2362 | * | |
2363 | * Arguments: @union tvec_regval *rv@ = register value | |
2364 | * @size_t sz@ = required size | |
2365 | * | |
2366 | * Returns: --- | |
2367 | * | |
2368 | * Use: Allocated space in a text or binary string register. If the | |
2369 | * current register size is sufficient, its buffer is left | |
2370 | * alone; otherwise, the old buffer, if any, is freed and a | |
2371 | * fresh buffer allocated. These functions are not intended to | |
2372 | * be used to adjust a buffer repeatedly, e.g., while building | |
2373 | * output incrementally: (a) they will perform badly, and (b) | |
2374 | * the old buffer contents are simply discarded if reallocation | |
2375 | * is necessary. Instead, use a @dbuf@ or @dstr@. | |
2376 | * | |
2377 | * The @tvec_allocstring@ function sneakily allocates an extra | |
2378 | * byte for a terminating zero. The @tvec_allocbytes@ function | |
2379 | * doesn't do this. | |
2380 | */ | |
2381 | ||
2382 | void tvec_allocstring(union tvec_regval *rv, size_t sz) | |
2383 | { | |
2384 | if (rv->str.sz <= sz) { xfree(rv->str.p); rv->str.p = xmalloc(sz + 1); } | |
2385 | rv->str.sz = sz; | |
2386 | } | |
2387 | ||
2388 | void tvec_allocbytes(union tvec_regval *rv, size_t sz) | |
2389 | { | |
2390 | if (rv->bytes.sz < sz) { xfree(rv->bytes.p); rv->bytes.p = xmalloc(sz); } | |
2391 | rv->bytes.sz = sz; | |
2392 | } | |
2393 | ||
b64eb60f MW |
2394 | /*----- Buffer type -------------------------------------------------------*/ |
2395 | ||
2396 | static int eq_buffer(const union tvec_regval *rv0, | |
2397 | const union tvec_regval *rv1, | |
2398 | const struct tvec_regdef *rd) | |
2399 | { return (rv0->bytes.sz == rv1->bytes.sz); } | |
2400 | ||
2401 | static int tobuf_buffer(buf *b, const union tvec_regval *rv, | |
2402 | const struct tvec_regdef *rd) | |
2403 | { return (unsigned_to_buf(b, rv->bytes.sz)); } | |
2404 | ||
2405 | static int frombuf_buffer(buf *b, union tvec_regval *rv, | |
2406 | const struct tvec_regdef *rd) | |
2407 | { | |
2408 | unsigned long u; | |
2409 | ||
2410 | if (unsigned_from_buf(b, &u)) return (-1); | |
2411 | if (u > (size_t)-1) return (-1); | |
2412 | tvec_allocbytes(rv, u); memset(rv->bytes.p, '!', u); | |
2413 | return (0); | |
2414 | } | |
2415 | ||
2416 | static const char units[] = "kMGTPEZY"; | |
2417 | ||
882a39c1 MW |
2418 | static int parse_buffer(union tvec_regval *rv, |
2419 | const struct tvec_regdef *rd, | |
2420 | struct tvec_state *tv) | |
b64eb60f MW |
2421 | { |
2422 | dstr d = DSTR_INIT; | |
3efcfd2d | 2423 | const char *q, *unit; |
b64eb60f MW |
2424 | size_t pos; |
2425 | unsigned long u, t; | |
882a39c1 | 2426 | int rc; |
b64eb60f MW |
2427 | unsigned f = 0; |
2428 | #define f_range 1u | |
2429 | ||
882a39c1 | 2430 | if (tvec_readword(tv, &d, ";", "buffer length")) { rc = -1; goto end; } |
3efcfd2d | 2431 | if (parse_unsigned_integer(&u, &q, d.buf)) goto bad; |
b64eb60f MW |
2432 | if (!*q) { |
2433 | tvec_skipspc(tv); pos = d.len; | |
2434 | if (!tvec_readword(tv, &d, ";", 0)) pos++; | |
2435 | q = d.buf + pos; | |
2436 | } | |
2437 | ||
2438 | if (u > (size_t)-1) goto rangerr; | |
2439 | for (t = u, unit = units; *unit; unit++) { | |
2440 | if (t > (size_t)-1/1024) f |= f_range; | |
2441 | else t *= 1024; | |
67b5031e | 2442 | if (*q == *unit) { |
b64eb60f | 2443 | if (f&f_range) goto rangerr; |
67b5031e | 2444 | u = t; q++; break; |
b64eb60f MW |
2445 | } |
2446 | } | |
67b5031e MW |
2447 | if (*q == 'B') q++; |
2448 | if (*q) goto bad; | |
882a39c1 | 2449 | if (check_string_length(u, rd->arg.p, tv)) { rc = -1; goto end; } |
b64eb60f | 2450 | |
882a39c1 MW |
2451 | if (tvec_flushtoeol(tv, 0)) { rc = -1; goto end; } |
2452 | tvec_allocbytes(rv, u); memset(rv->bytes.p, '?', u); | |
2453 | rc = 0; | |
2454 | end: | |
2455 | DDESTROY(&d); return (rc); | |
b64eb60f MW |
2456 | |
2457 | bad: | |
2458 | tvec_error(tv, "invalid buffer length `%s'", d.buf); | |
882a39c1 | 2459 | rc = -1; goto end; |
b64eb60f MW |
2460 | |
2461 | rangerr: | |
2462 | tvec_error(tv, "buffer length `%s' out of range", d.buf); | |
882a39c1 | 2463 | rc = -1; goto end; |
b64eb60f MW |
2464 | |
2465 | #undef f_range | |
2466 | } | |
2467 | ||
2468 | static void dump_buffer(const union tvec_regval *rv, | |
2469 | const struct tvec_regdef *rd, | |
e63124bc MW |
2470 | unsigned style, |
2471 | const struct gprintf_ops *gops, void *go) | |
b64eb60f MW |
2472 | { |
2473 | const char *unit; | |
2474 | unsigned long u = rv->bytes.sz; | |
2475 | ||
2476 | if (!u || u%1024) | |
e63124bc | 2477 | gprintf(gops, go, "%lu B", u); |
b64eb60f MW |
2478 | else { |
2479 | for (unit = units, u /= 1024; !(u%1024) && unit[1]; u /= 1024, unit++); | |
e63124bc | 2480 | gprintf(gops, go, "%lu %cB", u, *unit); |
b64eb60f MW |
2481 | } |
2482 | } | |
2483 | ||
2484 | const struct tvec_regty tvty_buffer = { | |
e63124bc | 2485 | init_bytes, release_bytes, eq_buffer, |
b64eb60f MW |
2486 | tobuf_buffer, frombuf_buffer, |
2487 | parse_buffer, dump_buffer | |
2488 | }; | |
2489 | ||
2490 | /*----- That's all, folks -------------------------------------------------*/ |