Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
.Dd Aug 19, 2012
.Dt XPRINTF 5
.Os Darwin
.Sh NAME
.Nm xprintf
.Nd extensible printf
.Sh SYNOPSIS
.In printf.h
.Ft "typedef int"
.Fn printf_arginfo_function "const struct printf_info *info" "size_t n" "int *argtypes"
.Ft "typedef int"
.Fn printf_function "FILE *stream" "const struct printf_info *info" "const void *const *args"
.Sh DESCRIPTION
The standard
.Xr printf 3
family of routines provides a convenient way to convert one or more arguments
to various forms for output, under the control of a format string.
The format string may contain any number of conversion specifications, which
start with the
.Sq Li %
character and end with a conversion specifier character (like
.Sq Li d
or
.Sq Li f ) ,
with conversion flag characters in-between.
.Pp
Extensible printf is an enhancement that allows adding new (user-defined)
conversion specifiers, or modifying/removing existing ones.
The implementation of extensible printf in Mac OS X is derived from the
FreeBSD version, which is based on the one in GNU libc (GLIBC).
Documentation for the GLIBC version is available at:
.Pp
.Li http://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html
.Pp
The main problem with the usual forms of extensible printf is that
changes to
.Xr printf 3
are program-wide.
But this is unsafe, since frameworks,
libraries or some other thread could change printf behavior in ways
unexpected by the main program, or the latter could unexpectedly affect the
former.
.Pp
So instead, the implementation used in Mac OS X makes
changes to conversion specifiers within printf domains,
which are independent structures containing the specifier definitions.
These domains are created as described in
.Xr xprintf_domain 3 ,
and once set up, it can be passed to a
.Xr xprintf 3
variant along with the format string and arguments to generate output.
The standard
.Xr printf 3
behavior is never affected.
.Pp
To define a new conversion specifier, two function typedefs are defined, and
the user must provide two functions based on these typedefs.
These functions will get called from extensible printf while processing
the corresponding conversion specification.
.Pp
During the first of three phases of extensible printf processing, the format
string is parsed, and for each conversion specification, a
.Vt struct printf_info
is created, containing the option flags specified in the
conversion specification as well as other settings.
Important fields in
.Vt struct printf_info
are:
.Bl -tag -width ".Va is_long_double"
.It Va alt
Boolean value whether the
.Sq Li #
flag was specified.
.It Va context
A
.Vt void *
pointer to arbitrary data specified in the original call to
.Xr register_printf_domain_function 3 .
.It Va group
Boolean value whether the
.Sq Li '
flag was specified.
.It Va is_char
Boolean value whether the
.Sq Li hh
flag was specified.
.It Va is_intmax
Boolean value whether the
.Sq Li j
flag was specified.
.It Va is_long
Boolean value whether the
.Sq Li l
flag was specified.
.It Va is_long_double
Boolean value whether the
.Sq Li L
or
.Sq Li ll
flags were specified.
.It Va is_ptrdiff
Boolean value whether the
.Sq Li t
flag was specified.
.It Va is_quad
Boolean value whether the
.Sq Li q
flag was specified.
.It Va is_short
Boolean value whether the
.Sq Li h
flag was specified.
.It Va is_size
Boolean value whether the
.Sq Li z
flag was specified.
.It Va is_vec
Boolean value whether the
.Sq Li v
flag was specified.
.It Va left
Boolean value whether the
.Sq Li -
flag was specified.
.It Va loc
The extended locale (see
.Xr xlocale 3 )
specified by the extensible printf caller (never
.Dv NULL ) .
.It Va pad
The padding character; either
.Sq Li 0
or space.
.It Va prec
The value of the optional precision.
-1 means the precision was unspecified.
.It Va showsign
Boolean value whether the
.Sq Li +
flag was specified.
.It Va signchar
The sign character, either
.Sq Li + ,
space or zero if none.
.It Va space
Boolean value whether the space flag was specified.
.It Va spec
The specifier character itself.
.It Va vsep
The separator character between vector items (using the
.Sq Li v
flag).
Can be any one of the four characters
.Dq Li ,:;_
or
.Sq Li X
if no separator character was specified (meaning that a space is used as the
separator, unless the specifier is
.Sq Li c ,
in which case no separator is used).
.It Va width
The value of the minimum field width (defaults to zero).
.El
.Pp
All other structure fields are either unused or private (and shouldn't be
used).
.Pp
This
.Vt struct printf_info
structure is then passed to the corresponding
.Nm printf_arginfo_function
callback function.
The callback function should return the number of consecutive arguments the
specifier handles, including zero (the maximum number of consecutive arguments
a single specifier can handle is
.Dv __PRINTFMAXARG ,
which is currently set to 2, but could be increased in the future if there is
need).
.Pp
The callback function is also passed an integer array and the length of that
array; the length will typically be
.Dv __PRINTFMAXARG .
The function should fill out the array up to the number of arguments it expects,
using the following values:
.Bl -tag -width ".Dv PA_POINTER"
.It Dv PA_CHAR
The argument type is an
.Vt int
cast to a
.Vt char .
.It Dv PA_DOUBLE
The argument type is a
.Vt double .
OR-ing
.Dv PA_DOUBLE
with
.Dv PA_FLAG_LONG_DOUBLE
specifies a
.Vt "long double"
type.
.It Dv PA_FLOAT
(Defined but unused; best to avoid, since
.Vt float
is automatically promoted to
.Vt double
anyways.)
.It Dv PA_INT
The argument type is
.Vt int
(either signed or unsigned).
The size can be adjusted by OR-ing the following values to
.Dv PA_INT :
.Bl -tag -width ".Dv PA_FLAG_LONG_LONG"
.It Dv PA_FLAG_INTMAX
The integer is the size of a
.Vt intmax_t .
.It Dv PA_FLAG_LONG
The integer is the size of a
.Vt long .
.It Dv PA_FLAG_LONG_LONG
The integer is the size of a
.Vt "long long" .
.It Dv PA_FLAG_PTRDIFF
The integer is the size of a
.Vt ptrdiff_t .
.It Dv PA_FLAG_QUAD
The integer is the size of a
.Vt quad_t
(deprecated).
.It Dv PA_FLAG_SHORT
The integer is the size of a
.Vt short .
.It Dv PA_FLAG_SIZE
The integer is the size of a
.Vt size_t .
.El
.It Dv PA_POINTER
The argument type is a pointer type, cast to a
.Vt "void *" .
.It Dv PA_STRING
The argument type is a null-terminated character string
.Vt ( "char *" ) .
.It Dv PA_VECTOR
The argument type is an AltiVec or SSE vector (16 bytes).
.It Dv PA_WCHAR
The argument type is a
.Vt wchar_t .
.It Dv PA_WSTRING
The argument type is a null-terminated wide character string
.Vt ( "wchar_t *" ) .
.El
.Pp
After the
.Nm printf_arginfo_function
returns, phase 2 of extensible printf processing involves converting the
argument according to the types specified by the returned type array.
Note that positional arguments are dealt with here as well.
.Pp
Then in phase 3, output is generated, either from the text in-between the
conversion specifications, or by calling the so-called rendering functions
associated with each conversion specifier (with typedef
.Nm printf_function ) .
The rendering function is passed the same
.Vt struct printf_info
structure, as well as an array of pointers to each of the arguments converted
in phase 2 that it is responsible for.
The callback should write its output to the provided output
stdio stream, and then return the number of characters written.
.Sh EXAMPLE
Here is an example that demonstrates many of the features of extensible printf:
.Bd -literal
#include <stdio.h>
#include <stdlib.h>
#include <printf.h>
#include <locale.h>
#include <xlocale.h>
#include <err.h>

/* The Coordinate type */
typedef struct {
    double x;
    double y;
} Coordinate;

#define L	(1 << 0)
#define P	(1 << 1)

/* The renderer callback for Coordinate */
static int
print_coordinate (FILE *stream, const struct printf_info *info,
    const void *const *args)
{
    const Coordinate *c;
    int width, ret, which = 0;
    char fmt[32];
    char *bp, *cp, *ep;
    /* The optional coordinate labels */
    const char **labels = (const char **)info->context;

    /* Get the argument pointer to a Coordinate */
    c = *((const Coordinate **) (args[0]));

    /* Set up the format string */
    cp = fmt;
    if(info->alt) *cp++ = '(';
    bp = cp;
    if(labels) {
	which |= L;
	*cp++ = '%';
	*cp++ = 's';
    }
    *cp++ = '%';
    if(info->group) *cp++ = '\e'';
    *cp++ = '*';
    if(info->prec >= 0) {
	which |= P;
	*cp++ = '.';
	*cp++ = '*';
    }
    *cp++ = 'l';
    *cp++ = 'f';
    ep = cp;
    if(info->alt) *cp++ = ',';
    *cp++ = ' ';
    while(bp < ep) *cp++ = *bp++;
    if(info->alt) *cp++ = ')';
    *cp = 0;

    width = info->left ? -info->width : info->width;

    /* Output to the given stream */
    switch(which) {
    case 0:
	ret = fprintf_l(stream, info->loc, fmt, width, c->x, width, c->y);
	break;
    case L:
	ret = fprintf_l(stream, info->loc, fmt, labels[0], width, c->x,
			labels[1], width, c->y);
	break;
    case P:
	ret = fprintf_l(stream, info->loc, fmt, width, info->prec, c->x,
			width, info->prec, c->y);
	break;
    case (L | P):
	ret = fprintf_l(stream, info->loc, fmt, labels[0], width,
			info->prec, c->x, labels[1], width, info->prec,
			c->y);
	break;
    }

    return ret;
}

/* The arginfo callback for Coordinate */
static int
coordinate_arginfo (const struct printf_info *info, size_t n,
		    int *argtypes)
{
  /* We always take exactly one argument and this is a pointer to the
     structure.. */
  if (n > 0)
    argtypes[0] = PA_POINTER;
  return 1;
}

int
main (void)
{
    Coordinate mycoordinate = {12345.6789, 3.141593};
    printf_domain_t domain;
    locale_t loc;
    const char *labels[] = {"x=", "y="};

    /* Set up a domain to add support for Coordinate conversion */
    domain = new_printf_domain();
    if(!domain)
	err(1, "new_printf_domain");
    /* Set up an extended locale to test locale support */
    loc = newlocale(LC_ALL_MASK, "uk_UA.UTF-8", NULL);
    if(!loc)
	err(1, "newlocale");

    /* Register the callbacks for Coordinates in the domain */
    register_printf_domain_function (domain, 'C', print_coordinate,
				     coordinate_arginfo, NULL);

    /* Print the coordinate using the current locale (C). */
    xprintf(domain, NULL, "|%'C|\en", &mycoordinate);
    xprintf(domain, NULL, "|%'14C|\en", &mycoordinate);
    xprintf(domain, NULL, "|%'-14.2C|\en", &mycoordinate);
    xprintf(domain, NULL, "|%'#C|\en", &mycoordinate);
    xprintf(domain, NULL, "|%'#14C|\en", &mycoordinate);
    xprintf(domain, NULL, "|%'#-14.2C|\en", &mycoordinate);

    printf("-------------\en");
    /* Reregister the callbacks, specifying coordinate labels
     * and setting the global locale (notice thousands separator) */
    register_printf_domain_function (domain, 'C', print_coordinate,
				     coordinate_arginfo, labels);
    if(setlocale(LC_ALL, "en_US.UTF-8") == NULL)
	errx(1, "setlocale");

    /* Reprint with labels */
    xprintf(domain, NULL, "|%'C|\en", &mycoordinate);
    xprintf(domain, NULL, "|%'14C|\en", &mycoordinate);
    xprintf(domain, NULL, "|%'-14.2C|\en", &mycoordinate);
    xprintf(domain, NULL, "|%'#C|\en", &mycoordinate);
    xprintf(domain, NULL, "|%'#14C|\en", &mycoordinate);
    xprintf(domain, NULL, "|%'#-14.2C|\en", &mycoordinate);

    printf("-------------\en");
    /* Now print with the test locale (notice decimal point and
     * thousands separator) */
    xprintf(domain, loc, "|%'C|\en", &mycoordinate);
    xprintf(domain, loc, "|%'14C|\en", &mycoordinate);
    xprintf(domain, loc, "|%'-14.2C|\en", &mycoordinate);
    xprintf(domain, loc, "|%'#C|\en", &mycoordinate);
    xprintf(domain, loc, "|%'#14C|\en", &mycoordinate);
    xprintf(domain, loc, "|%'#-14.2C|\en", &mycoordinate);

    return 0;
}
.Ed
.Pp
This example defines a Coordinate type, that consists of a pair of doubles.
We create a conversion specifier that displays a Coordinate type, either just
as two floating point numbers, or with the
.Sq Li #
(alternate form) flag, as parenthesized numbers separated by a comma.
Note the use of
.Nm printf_l
to do the actual output; this is using regular printf from within an extensible
printf renderer callback.
The use of
.Nm printf_l
also insures correct handling of extended locales.
.Pp
The output of the programs looks like:
.Bd -literal
|12345.678900 3.141593|
|  12345.678900       3.141593|
|12345.68       3.14          |
|(12345.678900, 3.141593)|
|(  12345.678900,       3.141593)|
|(12345.68      , 3.14          )|
-------------
|x=12,345.678900 y=3.141593|
|x= 12,345.678900 y=      3.141593|
|x=12,345.68      y=3.14          |
|(x=12,345.678900, y=3.141593)|
|(x= 12,345.678900, y=      3.141593)|
|(x=12,345.68     , y=3.14          )|
-------------
|x=12 345,678900 y=3,141593|
|x= 12 345,678900 y=      3,141593|
|x=12 345,68      y=3,14          |
|(x=12 345,678900, y=3,141593)|
|(x= 12 345,678900, y=      3,141593)|
|(x=12 345,68     , y=3,14          )|
.Ed
.Pp
Notice:
.Bl -bullet
.It
Field width, precision and left adjustment are applied to each of the numbers.
.It
The alternate form, using parenthesized numbers separated by a comma.
.It
In the second group of six, the thousands separator corresponds to the
global locale setting
.Pq Li en_US.UTF-8 .
.It
The second and third group have a label for each number, provide through
the user-defined context argument.
.It
The third group has the decimal point and thousands separator of the extended
locale argument
.Pq Li uk_UA.UTF-8 .
.El
.Sh PERFORMANCE
Because of the three phase processing of extensible printf, as well as the
use of two callbacks for each conversion specifier, performance is
considerably slower than the one pass, highly optimized regular
.Xr printf 3 .
Recursive use of
.Xr printf 3
from within an extensible printf renderer callback
(as in the
.Sx EXAMPLE
above) adds additional overhead.
.Pp
To ameliorate some of this slowness, the concept of separate compilation
and execution phases has be added to extensible printf.
The functions in
.Xr xprintf_comp 3
allow the creation of pre-compiled extensible printf structures (performing
phase one of extensible printf processing).
These pre-compiled structures can then be passed to the printf variants in
.Xr xprintf_exec 3
to produce the actual output (performing phases 2 and 3).
The compilation phase need only be done once, while execution can be performed
any number of times.
.Pp
A simple example of use is:
.Bd -literal
    printf_comp_t pc = new_printf_comp(domain, loc, "%d: %C\en");
    for(i = 0; i = sizeof(coords) / sizeof(*coords); i++) {
	xprintf_exec(pc, i, &coords[i]);
    }
    free_printf_comp(pc);
.Ed
.Pp
Here,
.Va coords
is a array containing
.Vt Coordinate
structures that are to be printed and the
.Va domain
and
.Va loc
variables are as from
.Sx EXAMPLE
above.
(Error checking on the return value from
.Fn new_printf_comp
is not shown).
.Sh SEE ALSO
.Xr printf 3 ,
.Xr xlocale 3 ,
.Xr xprintf 3 ,
.Xr xprintf_comp 3 ,
.Xr xprintf_domain 3 ,
.Xr xprintf_exec 3