Something went wrong. Try again.
Cider Isn't Darwin Emulation, Really
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534.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 DESCRIPTIONThe standard.Xr printf 3family of routines provides a convenient way to convert one or more argumentsto various forms for output, under the control of a format string.The format string may contain any number of conversion specifications, whichstart with the.Sq Li %character and end with a conversion specifier character (like.Sq Li dor.Sq Li f ) ,with conversion flag characters in-between..PpExtensible 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 theFreeBSD 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.PpThe main problem with the usual forms of extensible printf is thatchanges to.Xr printf 3are program-wide.But this is unsafe, since frameworks,libraries or some other thread could change printf behavior in waysunexpected by the main program, or the latter could unexpectedly affect theformer..PpSo instead, the implementation used in Mac OS X makeschanges 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 3variant along with the format string and arguments to generate output.The standard.Xr printf 3behavior is never affected..PpTo define a new conversion specifier, two function typedefs are defined, andthe user must provide two functions based on these typedefs.These functions will get called from extensible printf while processingthe corresponding conversion specification..PpDuring the first of three phases of extensible printf processing, the formatstring is parsed, and for each conversion specification, a.Vt struct printf_infois created, containing the option flags specified in theconversion specification as well as other settings.Important fields in.Vt struct printf_infoare:.Bl -tag -width ".Va is_long_double".It Va altBoolean value whether the.Sq Li #flag was specified..It Va contextA.Vt void *pointer to arbitrary data specified in the original call to.Xr register_printf_domain_function 3 ..It Va groupBoolean value whether the.Sq Li 'flag was specified..It Va is_charBoolean value whether the.Sq Li hhflag was specified..It Va is_intmaxBoolean value whether the.Sq Li jflag was specified..It Va is_longBoolean value whether the.Sq Li lflag was specified..It Va is_long_doubleBoolean value whether the.Sq Li Lor.Sq Li llflags were specified..It Va is_ptrdiffBoolean value whether the.Sq Li tflag was specified..It Va is_quadBoolean value whether the.Sq Li qflag was specified..It Va is_shortBoolean value whether the.Sq Li hflag was specified..It Va is_sizeBoolean value whether the.Sq Li zflag was specified..It Va is_vecBoolean value whether the.Sq Li vflag was specified..It Va leftBoolean value whether the.Sq Li -flag was specified..It Va locThe extended locale (see.Xr xlocale 3 )specified by the extensible printf caller (never.Dv NULL ) ..It Va padThe padding character; either.Sq Li 0or space..It Va precThe value of the optional precision.-1 means the precision was unspecified..It Va showsignBoolean value whether the.Sq Li +flag was specified..It Va signcharThe sign character, either.Sq Li + ,space or zero if none..It Va spaceBoolean value whether the space flag was specified..It Va specThe specifier character itself..It Va vsepThe separator character between vector items (using the.Sq Li vflag).Can be any one of the four characters.Dq Li ,:;_or.Sq Li Xif no separator character was specified (meaning that a space is used as theseparator, unless the specifier is.Sq Li c ,in which case no separator is used)..It Va widthThe value of the minimum field width (defaults to zero)..El.PpAll other structure fields are either unused or private (and shouldn't beused)..PpThis.Vt struct printf_infostructure is then passed to the corresponding.Nm printf_arginfo_functioncallback function.The callback function should return the number of consecutive arguments thespecifier handles, including zero (the maximum number of consecutive argumentsa single specifier can handle is.Dv __PRINTFMAXARG ,which is currently set to 2, but could be increased in the future if there isneed)..PpThe callback function is also passed an integer array and the length of thatarray; 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_CHARThe argument type is an.Vt intcast to a.Vt char ..It Dv PA_DOUBLEThe argument type is a.Vt double .OR-ing.Dv PA_DOUBLEwith.Dv PA_FLAG_LONG_DOUBLEspecifies a.Vt "long double"type..It Dv PA_FLOAT(Defined but unused; best to avoid, since.Vt floatis automatically promoted to.Vt doubleanyways.).It Dv PA_INTThe 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_INTMAXThe integer is the size of a.Vt intmax_t ..It Dv PA_FLAG_LONGThe integer is the size of a.Vt long ..It Dv PA_FLAG_LONG_LONGThe integer is the size of a.Vt "long long" ..It Dv PA_FLAG_PTRDIFFThe integer is the size of a.Vt ptrdiff_t ..It Dv PA_FLAG_QUADThe integer is the size of a.Vt quad_t(deprecated)..It Dv PA_FLAG_SHORTThe integer is the size of a.Vt short ..It Dv PA_FLAG_SIZEThe integer is the size of a.Vt size_t ..El.It Dv PA_POINTERThe argument type is a pointer type, cast to a.Vt "void *" ..It Dv PA_STRINGThe argument type is a null-terminated character string.Vt ( "char *" ) ..It Dv PA_VECTORThe argument type is an AltiVec or SSE vector (16 bytes)..It Dv PA_WCHARThe argument type is a.Vt wchar_t ..It Dv PA_WSTRINGThe argument type is a null-terminated wide character string.Vt ( "wchar_t *" ) ..El.PpAfter the.Nm printf_arginfo_functionreturns, phase 2 of extensible printf processing involves converting theargument according to the types specified by the returned type array.Note that positional arguments are dealt with here as well..PpThen in phase 3, output is generated, either from the text in-between theconversion specifications, or by calling the so-called rendering functionsassociated with each conversion specifier (with typedef.Nm printf_function ) .The rendering function is passed the same.Vt struct printf_infostructure, as well as an array of pointers to each of the arguments convertedin phase 2 that it is responsible for.The callback should write its output to the provided outputstdio stream, and then return the number of characters written..Sh EXAMPLEHere 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 intprint_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 intcoordinate_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;}
intmain (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.PpThis example defines a Coordinate type, that consists of a pair of doubles.We create a conversion specifier that displays a Coordinate type, either justas 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_lto do the actual output; this is using regular printf from within an extensibleprintf renderer callback.The use of.Nm printf_lalso insures correct handling of extended locales..PpThe 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.PpNotice:.Bl -bullet.ItField width, precision and left adjustment are applied to each of the numbers..ItThe alternate form, using parenthesized numbers separated by a comma..ItIn the second group of six, the thousands separator corresponds to theglobal locale setting.Pq Li en_US.UTF-8 ..ItThe second and third group have a label for each number, provide throughthe user-defined context argument..ItThe third group has the decimal point and thousands separator of the extendedlocale argument.Pq Li uk_UA.UTF-8 ..El.Sh PERFORMANCEBecause of the three phase processing of extensible printf, as well as theuse of two callbacks for each conversion specifier, performance isconsiderably slower than the one pass, highly optimized regular.Xr printf 3 .Recursive use of.Xr printf 3from within an extensible printf renderer callback(as in the.Sx EXAMPLEabove) adds additional overhead..PpTo ameliorate some of this slowness, the concept of separate compilationand execution phases has be added to extensible printf.The functions in.Xr xprintf_comp 3allow the creation of pre-compiled extensible printf structures (performingphase one of extensible printf processing).These pre-compiled structures can then be passed to the printf variants in.Xr xprintf_exec 3to produce the actual output (performing phases 2 and 3).The compilation phase need only be done once, while execution can be performedany number of times..PpA 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.PpHere,.Va coordsis a array containing.Vt Coordinatestructures that are to be printed and the.Va domainand.Va locvariables are as from.Sx EXAMPLEabove.(Error checking on the return value from.Fn new_printf_compis 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