2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file lib/printf.c
19 * @brief UTF-8 *printf workalike (core)
22 #define NO_MEMORY_ALLOCATION
23 /* because byte_snprintf used from log.c */
35 /** @brief Flags from a converstion specification
50 /** @brief Possible lengths of a conversion specification */
64 /** @brief Formatter state */
66 /** @brief Output stream */
69 /** @brief Number of bytes written */
72 /** @brief Argument list */
76 /** @brief Definition of a conversion specifier */
78 /** @brief Defining character ('d', 's' etc) */
81 /** @brief Consistency check
82 * @param c Conversion being processed
83 * @return 0 if OK, -1 on error
85 int (*check)(const struct conversion *c);
87 /** @brief Generate output
88 * @param s Formatter state
89 * @param c Conversion being processed
90 * @return 0 on success, -1 on error
92 int (*output)(struct state *s, struct conversion *c);
94 /** @brief Number base */
97 /** @brief Digit set */
100 /** @brief Alternative-form prefix */
104 /** @brief One conversion specified as it's handled */
106 /** @brief Flags in this conversion */
109 /** @brief Field width (if @ref f_width) */
112 /** @brief Precision (if @ref f_precision) */
115 /** @brief Length modifier or 0 */
118 /** @brief Specifier used */
119 const struct specifier *specifier;
122 /** @brief Flag characters (order significant!) */
123 static const char flags[] = "'-+ #0";
125 /* write @nbytes@ to the output. Return -1 on error, 0 on success.
126 * Keeps track of the number of bytes written. */
127 static int do_write(struct state *s,
130 if(s->bytes > INT_MAX - nbytes) {
136 if(s->output->write(s->output, buffer, nbytes) < 0)
142 /* write character @ch@ @n@ times, reasonably efficiently */
143 static int do_pad(struct state *s, int ch, unsigned n) {
148 case ' ': padding = " "; break;
149 case '0': padding = "00000000000000000000000000000000"; break;
155 if(do_write(s, padding, 32) < 0)
158 if(do_write(s, padding, n) < 0)
163 /* pick up the integer at @ptr@, returning it via @intp@. Return the
164 * number of characters consumed. Return 0 if there is no integer
165 * there and -1 if an error occurred (e.g. too big) */
166 static int get_integer(int *intp, const char *ptr) {
171 n = strtol(ptr, &e, 10);
172 if(errno || n > INT_MAX || n < INT_MIN || e == ptr)
178 /* consistency checks for various conversion specifications */
180 static int check_integer(const struct conversion *c) {
197 static int check_string(const struct conversion *c) {
200 /* XXX don't support %ls, %lc */
207 static int check_pointer(const struct conversion *c) {
213 static int check_percent(const struct conversion *c) {
214 if(c->flags || c->width || c->precision || c->length)
219 /* output functions for various conversion specifications */
221 static int output_percent(struct state *s,
222 struct conversion attribute((unused)) *c) {
223 return do_write(s, "%", 1);
226 static int output_integer(struct state *s, struct conversion *c) {
230 int base, dp, iszero, ndigits, prec, xform, sign_bytes, pad;
231 char digits[CHAR_BIT * sizeof (uintmax_t)]; /* overestimate */
233 switch(c->specifier->ch) {
235 if(c->specifier->base < 0) {
237 case 0: l = va_arg(s->ap, int); break;
238 case l_char: l = (signed char)va_arg(s->ap, int); break;
239 case l_short: l = (short)va_arg(s->ap, int); break;
240 case l_long: l = va_arg(s->ap, long); break;
241 case l_longlong: l = va_arg(s->ap, long_long); break;
242 case l_intmax_t: l = va_arg(s->ap, intmax_t); break;
243 case l_size_t: l = va_arg(s->ap, ssize_t); break;
244 case l_ptrdiff_t: l = va_arg(s->ap, ptrdiff_t); break;
247 base = -c->specifier->base;
257 case 0: u = va_arg(s->ap, unsigned int); break;
258 case l_char: u = (unsigned char)va_arg(s->ap, unsigned int); break;
259 case l_short: u = (unsigned short)va_arg(s->ap, unsigned int); break;
260 case l_long: u = va_arg(s->ap, unsigned long); break;
261 case l_longlong: u = va_arg(s->ap, u_long_long); break;
262 case l_intmax_t: u = va_arg(s->ap, uintmax_t); break;
263 case l_size_t: u = va_arg(s->ap, size_t); break;
264 case l_ptrdiff_t: u = va_arg(s->ap, ptrdiff_t); break;
267 base = c->specifier->base;
272 u = (uintptr_t)va_arg(s->ap, void *);
274 base = c->specifier->base;
278 /* default precision */
279 if(!(c->flags & f_precision))
282 if((c->flags & f_sign) && !sign)
284 /* compute the digits */
288 digits[--dp] = c->specifier->digits[u % base];
291 ndigits = sizeof digits - dp;
292 /* alternative form */
293 if(c->flags & f_hash) {
296 if((dp == sizeof digits || digits[dp] != '0')
297 && c->precision <= ndigits)
298 c->precision = ndigits + 1;
301 if(!iszero && c->specifier->xform)
302 xform = strlen(c->specifier->xform);
307 /* calculate number of 0s to add for precision */
308 if(ndigits < c->precision)
309 prec = c->precision - ndigits;
312 /* bytes occupied by the sign */
317 /* XXX implement the ' ' flag */
318 /* calculate number of bytes of padding */
319 if(c->flags & f_width) {
320 if((pad = c->width - (ndigits + prec + xform + sign_bytes)) < 0)
324 /* now we are ready to output. Possibilities are:
325 * [space pad][sign][xform][0 prec]digits
326 * [sign][xform][0 pad][0 prec]digits
327 * [sign][xform][0 prec]digits[space pad]
331 if(c->flags & f_left) {
332 if(sign && do_write(s, &sign, 1))
334 if(xform && do_write(s, c->specifier->xform, xform))
336 if(prec && do_pad(s, '0', prec) < 0)
338 if(ndigits && do_write(s, digits + dp, ndigits))
340 if(pad && do_pad(s, ' ', pad) < 0)
342 } else if(c->flags & f_zero) {
343 if(sign && do_write(s, &sign, 1))
345 if(xform && do_write(s, c->specifier->xform, xform))
347 if(pad && do_pad(s, '0', pad) < 0)
349 if(prec && do_pad(s, '0', prec) < 0)
351 if(ndigits && do_write(s, digits + dp, ndigits))
354 if(pad && do_pad(s, ' ', pad) < 0)
356 if(sign && do_write(s, &sign, 1))
358 if(xform && do_write(s, c->specifier->xform, xform))
360 if(prec && do_pad(s, '0', prec) < 0)
362 if(ndigits && do_write(s, digits + dp, ndigits))
368 static int output_string(struct state *s, struct conversion *c) {
372 str = va_arg(s->ap, const char *);
373 if(c->flags & f_precision) {
374 if((n = memchr(str, 0, c->precision)))
380 if(c->flags & f_width) {
381 if((pad = c->width - len) < 0)
385 if(c->flags & f_left) {
386 if(do_write(s, str, len) < 0)
388 if(pad && do_pad(s, ' ', pad) < 0)
391 if(pad && do_pad(s, ' ', pad) < 0)
393 if(do_write(s, str, len) < 0)
400 static int output_char(struct state *s, struct conversion *c) {
404 ch = va_arg(s->ap, int);
405 if(c->flags & f_width) {
406 if((pad = c->width - 1) < 0)
410 if(c->flags & f_left) {
411 if(do_write(s, &ch, 1) < 0)
413 if(pad && do_pad(s, ' ', pad) < 0)
416 if(pad && do_pad(s, ' ', pad) < 0)
418 if(do_write(s, &ch, 1) < 0)
424 static int output_count(struct state *s, struct conversion *c) {
426 case 0: *va_arg(s->ap, int *) = s->bytes; break;
427 case l_char: *va_arg(s->ap, signed char *) = s->bytes; break;
428 case l_short: *va_arg(s->ap, short *) = s->bytes; break;
429 case l_long: *va_arg(s->ap, long *) = s->bytes; break;
430 case l_longlong: *va_arg(s->ap, long_long *) = s->bytes; break;
431 case l_intmax_t: *va_arg(s->ap, intmax_t *) = s->bytes; break;
432 case l_size_t: *va_arg(s->ap, ssize_t *) = s->bytes; break;
433 case l_ptrdiff_t: *va_arg(s->ap, ptrdiff_t *) = s->bytes; break;
439 /* table of conversion specifiers */
440 static const struct specifier specifiers[] = {
441 /* XXX don't support floating point conversions */
442 { '%', check_percent, output_percent, 0, 0, 0 },
443 { 'X', check_integer, output_integer, 16, "0123456789ABCDEF", "0X" },
444 { 'c', check_string, output_char, 0, 0, 0 },
445 { 'd', check_integer, output_integer, -10, "0123456789", 0 },
446 { 'i', check_integer, output_integer, -10, "0123456789", 0 },
447 { 'n', check_integer, output_count, 0, 0, 0 },
448 { 'o', check_integer, output_integer, 8, "01234567", 0 },
449 { 'p', check_pointer, output_integer, 16, "0123456789abcdef", "0x" },
450 { 's', check_string, output_string, 0, 0, 0 },
451 { 'u', check_integer, output_integer, 10, "0123456789", 0 },
452 { 'x', check_integer, output_integer, 16, "0123456789abcdef", "0x" },
455 /* collect and check information about a conversion specification */
456 static int parse_conversion(struct conversion *c, const char *ptr) {
458 const char *q, *start = ptr;
460 memset(c, 0, sizeof *c);
462 while(*ptr && (q = strchr(flags, *ptr))) {
463 c->flags |= (1 << (q - flags));
466 /* minimum field width */
467 if(*ptr >= '0' && *ptr <= '9') {
468 if((n = get_integer(&c->width, ptr)) < 0)
472 } else if(*ptr == '*') {
480 if(*ptr >= '0' && *ptr <= '9') {
481 if((n = get_integer(&c->precision, ptr)) < 0)
484 } else if(*ptr == '*') {
489 c->flags |= f_precision;
491 /* length modifier */
492 switch(ch = *ptr++) {
494 if((ch = *ptr++) == 'h') {
502 if((ch = *ptr++) == 'l') {
503 c->length = l_longlong;
509 case 'q': c->length = l_longlong; ch = *ptr++; break;
510 case 'j': c->length = l_intmax_t; ch = *ptr++; break;
511 case 'z': c->length = l_size_t; ch = *ptr++; break;
512 case 't': c->length = l_ptrdiff_t; ch = *ptr++; break;
513 case 'L': c->length = l_longdouble; ch = *ptr++; break;
515 /* conversion specifier */
517 r = sizeof specifiers / sizeof *specifiers;
518 while(l <= r && (specifiers[m = (l + r) / 2].ch != ch))
519 if(ch < specifiers[m].ch)
523 if(specifiers[m].ch != ch)
525 if(specifiers[m].check(c))
527 c->specifier = &specifiers[m];
531 /* ISO/IEC 9899:1999 7.19.6.1 */
532 /* http://www.opengroup.org/onlinepubs/009695399/functions/fprintf.html */
534 int byte_vsinkprintf(struct sink *output,
542 memset(&s, 0, sizeof s);
546 /* output text up to next conversion specification */
547 for(ptr = fmt; *fmt && *fmt != '%'; ++fmt)
550 if(do_write(&s, ptr, n) < 0)
555 /* parse conversion */
556 if((n = parse_conversion(&c, fmt)) < 0)
559 /* fill in width and precision */
560 if((c.flags & f_width) && c.width == -1)
561 if((c.width = va_arg(s.ap, int)) < 0) {
565 if((c.flags & f_precision) && c.precision == -1)
566 if((c.precision = va_arg(s.ap, int)) < 0)
567 c.flags ^= f_precision;
568 /* generate the output */
569 if(c.specifier->output(&s, &c) < 0)
579 int byte_sinkprintf(struct sink *output, const char *fmt, ...) {
584 n = byte_vsinkprintf(output, fmt, ap);