Next: , Previous: Conversion Specifier Options, Up: Customizing Printf


12.13.3 Defining the Output Handler

Now let's look at how to define the handler and arginfo functions which are passed as arguments to register_printf_function.

Compatibility Note: The interface changed in GNU libc version 2.0. Previously the third argument was of type va_list *.

You should define your handler functions with a prototype like:

     int function (FILE *stream, const struct printf_info *info,
                         const void *const *args)

The stream argument passed to the handler function is the stream to which it should write output.

The info argument is a pointer to a structure that contains information about the various options that were included with the conversion in the template string. You should not modify this structure inside your handler function. See Conversion Specifier Options, for a description of this data structure.

The args is a vector of pointers to the arguments data. The number of arguments was determined by calling the argument information function provided by the user.

Your handler function should return a value just like printf does: it should return the number of characters it has written, or a negative value to indicate an error.

— Data Type: printf_function

This is the data type that a handler function should have.

If you are going to use parse_printf_format in your application, you must also define a function to pass as the arginfo-function argument for each new conversion you install with register_printf_function.

You have to define these functions with a prototype like:

     int function (const struct printf_info *info,
                         size_t n, int *argtypes)

The return value from the function should be the number of arguments the conversion expects. The function should also fill in no more than n elements of the argtypes array with information about the types of each of these arguments. This information is encoded using the various ‘PA_’ macros. (You will notice that this is the same calling convention parse_printf_format itself uses.)

— Data Type: printf_arginfo_function

This type is used to describe functions that return information about the number and type of arguments used by a conversion specifier.