176afb20cSPhil Shafer.. index:: API 2983afe33SPhil Shafer 3983afe33SPhil ShaferThe libxo API 4983afe33SPhil Shafer============= 5983afe33SPhil Shafer 6983afe33SPhil ShaferThis section gives details about the functions in libxo, how to call 7983afe33SPhil Shaferthem, and the actions they perform. 8983afe33SPhil Shafer 9983afe33SPhil Shafer.. index:: Handles 10983afe33SPhil Shafer.. _handles: 11983afe33SPhil Shafer 12983afe33SPhil ShaferHandles 13983afe33SPhil Shafer------- 14983afe33SPhil Shafer 15983afe33SPhil Shaferlibxo uses "handles" to control its rendering functionality. The 16983afe33SPhil Shaferhandle contains state and buffered data, as well as callback functions 17983afe33SPhil Shaferto process data. 18983afe33SPhil Shafer 19983afe33SPhil ShaferHandles give an abstraction for libxo that encapsulates the state of a 20983afe33SPhil Shaferstream of output. Handles have the data type "`xo_handle_t`" and are 21983afe33SPhil Shaferopaque to the caller. 22983afe33SPhil Shafer 23983afe33SPhil ShaferThe library has a default handle that is automatically initialized. 24983afe33SPhil ShaferBy default, this handle will send text style output (`XO_STYLE_TEXT`) to 25983afe33SPhil Shaferstandard output. The xo_set_style and xo_set_flags functions can be 26983afe33SPhil Shaferused to change this behavior. 27983afe33SPhil Shafer 28983afe33SPhil ShaferFor the typical command that is generating output on standard output, 29983afe33SPhil Shaferthere is no need to create an explicit handle, but they are available 30983afe33SPhil Shaferwhen needed, e.g., for daemons that generate multiple streams of 31983afe33SPhil Shaferoutput. 32983afe33SPhil Shafer 33983afe33SPhil ShaferMany libxo functions take a handle as their first parameter; most that 34983afe33SPhil Shaferdo not use the default handle. Any function taking a handle can be 35983afe33SPhil Shaferpassed NULL to access the default handle. For the convenience of 36983afe33SPhil Shafercallers, the libxo library includes handle-less functions that 37983afe33SPhil Shaferimplicitly use the default handle. 38983afe33SPhil Shafer 39983afe33SPhil ShaferFor example, the following are equivalent:: 40983afe33SPhil Shafer 41983afe33SPhil Shafer xo_emit("test"); 42983afe33SPhil Shafer xo_emit_h(NULL, "test"); 43983afe33SPhil Shafer 44983afe33SPhil ShaferHandles are created using `xo_create` and destroy using 45983afe33SPhil Shafer`xo_destroy`. 46983afe33SPhil Shafer 47983afe33SPhil Shafer.. index:: xo_create 48983afe33SPhil Shafer 49983afe33SPhil Shaferxo_create 50983afe33SPhil Shafer~~~~~~~~~ 51983afe33SPhil Shafer 52983afe33SPhil Shafer.. c:function:: xo_handle_t *xo_create (xo_style_t style, xo_xof_flags_t flags) 53983afe33SPhil Shafer 54983afe33SPhil Shafer The `xo_create` function allocates a new handle which can be passed 55983afe33SPhil Shafer to further libxo function calls. The `xo_handle_t` structure is 56983afe33SPhil Shafer opaque. 57983afe33SPhil Shafer 58983afe33SPhil Shafer :param xo_style_t style: Output style (XO_STYLE\_*) 59983afe33SPhil Shafer :param xo_xof_flags_t flags: Flags for this handle (XOF\_*) 60983afe33SPhil Shafer :return: New libxo handle 61983afe33SPhil Shafer :rtype: xo_handle_t \* 62983afe33SPhil Shafer 63983afe33SPhil Shafer :: 64983afe33SPhil Shafer 65983afe33SPhil Shafer EXAMPLE: 66983afe33SPhil Shafer xo_handle_t *xop = xo_create(XO_STYLE_JSON, XOF_WARN | XOF_PRETTY); 67983afe33SPhil Shafer .... 68983afe33SPhil Shafer xo_emit_h(xop, "testing\n"); 69983afe33SPhil Shafer 70983afe33SPhil Shafer See also :ref:`output-styles` and :ref:`flags`. 71983afe33SPhil Shafer 72983afe33SPhil Shafer.. index:: xo_create_to_file 73983afe33SPhil Shafer.. index:: XOF_CLOSE_FP 74983afe33SPhil Shafer 75983afe33SPhil Shaferxo_create_to_file 76983afe33SPhil Shafer~~~~~~~~~~~~~~~~~ 77983afe33SPhil Shafer 78983afe33SPhil Shafer.. c:function:: 79983afe33SPhil Shafer xo_handle_t *xo_create_to_file (FILE *fp, unsigned style, unsigned flags) 80983afe33SPhil Shafer 81983afe33SPhil Shafer The `xo_create_to_file` function is aconvenience function is 82983afe33SPhil Shafer provided for situations when output should be written to a different 83983afe33SPhil Shafer file, rather than the default of standard output. 84983afe33SPhil Shafer 85983afe33SPhil Shafer The `XOF_CLOSE_FP` flag can be set on the returned handle to trigger a 86983afe33SPhil Shafer call to fclose() for the FILE pointer when the handle is destroyed, 87983afe33SPhil Shafer avoiding the need for the caller to perform this task. 88983afe33SPhil Shafer 89983afe33SPhil Shafer :param fp: FILE to use as base for this handle 90983afe33SPhil Shafer :type fp: FILE * 91983afe33SPhil Shafer :param xo_style_t style: Output style (XO_STYLE\_*) 92983afe33SPhil Shafer :param xo_xof_flags_t flags: Flags for this handle (XOF\_*) 93983afe33SPhil Shafer :return: New libxo handle 94983afe33SPhil Shafer :rtype: xo_handle_t \* 95983afe33SPhil Shafer 96983afe33SPhil Shafer.. index:: xo_set_writer 97983afe33SPhil Shafer.. index:: xo_write_func_t 98983afe33SPhil Shafer.. index:: xo_close_func_t 99983afe33SPhil Shafer.. index:: xo_flush_func_t 100983afe33SPhil Shafer 101983afe33SPhil Shaferxo_set_writer 102983afe33SPhil Shafer~~~~~~~~~~~~~ 103983afe33SPhil Shafer 104983afe33SPhil Shafer.. c:function:: 105983afe33SPhil Shafer void xo_set_writer (xo_handle_t *xop, void *opaque, \ 106983afe33SPhil Shafer xo_write_func_t write_func, xo_close_func_t close_func, \ 107983afe33SPhil Shafer xo_flush_func_t flush_func) 108983afe33SPhil Shafer 109983afe33SPhil Shafer The `xo_set_writer` function allows custom functions which can 110983afe33SPhil Shafer tailor how libxo writes data. The `opaque` argument is recorded and 111983afe33SPhil Shafer passed back to the functions, allowing the function to acquire 112983afe33SPhil Shafer context information. The *write_func* function writes data to the 113983afe33SPhil Shafer output stream. The *close_func* function can release this opaque 114983afe33SPhil Shafer data and any other resources as needed. The *flush_func* function 115983afe33SPhil Shafer is called to flush buffered data associated with the opaque object. 116983afe33SPhil Shafer 117983afe33SPhil Shafer :param xop: Handle to modify (or NULL for default handle) 118983afe33SPhil Shafer :type xop: xo_handle_t * 119983afe33SPhil Shafer :param opaque: Pointer to opaque data passed to the given functions 120983afe33SPhil Shafer :type opaque: void * 121983afe33SPhil Shafer :param xo_write_func_t write_func: New write function 122983afe33SPhil Shafer :param xo_close_func_t close_func: New close function 123983afe33SPhil Shafer :param xo_flush_func_t flush_func: New flush function 124983afe33SPhil Shafer :returns: void 125983afe33SPhil Shafer 126983afe33SPhil Shafer.. index:: xo_get_style 127983afe33SPhil Shafer 128983afe33SPhil Shaferxo_get_style 129983afe33SPhil Shafer~~~~~~~~~~~~ 130983afe33SPhil Shafer 131983afe33SPhil Shafer.. c:function:: xo_style_t xo_get_style(xo_handle_t *xop) 132983afe33SPhil Shafer 133983afe33SPhil Shafer Use the `xo_get_style` function to find the current output style for 134983afe33SPhil Shafer a given handle. To use the default handle, pass a `NULL` handle. 135983afe33SPhil Shafer 136983afe33SPhil Shafer :param xop: Handle to interrogate (or NULL for default handle) 137983afe33SPhil Shafer :type xop: xo_handle_t * 138983afe33SPhil Shafer :returns: Output style (XO_STYLE\_*) 139983afe33SPhil Shafer :rtype: xo_style_t 140983afe33SPhil Shafer 141983afe33SPhil Shafer :: 142983afe33SPhil Shafer 143983afe33SPhil Shafer EXAMPLE:: 144983afe33SPhil Shafer style = xo_get_style(NULL); 145983afe33SPhil Shafer 146983afe33SPhil Shafer.. index:: XO_STYLE_TEXT 147983afe33SPhil Shafer.. index:: XO_STYLE_XML 148983afe33SPhil Shafer.. index:: XO_STYLE_JSON 149983afe33SPhil Shafer.. index:: XO_STYLE_HTML 150983afe33SPhil Shafer 151983afe33SPhil Shafer.. _output-styles: 152983afe33SPhil Shafer 153983afe33SPhil ShaferOutput Styles (XO_STYLE\_\*) 154983afe33SPhil Shafer++++++++++++++++++++++++++++ 155983afe33SPhil Shafer 156983afe33SPhil ShaferThe libxo functions accept a set of output styles: 157983afe33SPhil Shafer 158983afe33SPhil Shafer =============== ========================= 159983afe33SPhil Shafer Flag Description 160983afe33SPhil Shafer =============== ========================= 161983afe33SPhil Shafer XO_STYLE_TEXT Traditional text output 162983afe33SPhil Shafer XO_STYLE_XML XML encoded data 163983afe33SPhil Shafer XO_STYLE_JSON JSON encoded data 164983afe33SPhil Shafer XO_STYLE_HTML HTML encoded data 165983afe33SPhil Shafer =============== ========================= 166983afe33SPhil Shafer 167983afe33SPhil ShaferThe "XML", "JSON", and "HTML" output styles all use the UTF-8 168983afe33SPhil Shafercharacter encoding. "TEXT" using locale-based encoding. 169983afe33SPhil Shafer 170983afe33SPhil Shafer.. index:: xo_set_style 171983afe33SPhil Shafer 172983afe33SPhil Shaferxo_set_style 173983afe33SPhil Shafer~~~~~~~~~~~~ 174983afe33SPhil Shafer 175983afe33SPhil Shafer.. c:function:: void xo_set_style(xo_handle_t *xop, xo_style_t style) 176983afe33SPhil Shafer 177983afe33SPhil Shafer The `xo_set_style` function is used to change the output style 178983afe33SPhil Shafer setting for a handle. To use the default handle, pass a `NULL` 179983afe33SPhil Shafer handle. 180983afe33SPhil Shafer 181983afe33SPhil Shafer :param xop: Handle to modify 182983afe33SPhil Shafer :type xop: xo_handle_t * 183983afe33SPhil Shafer :param xo_style_t style: Output style (XO_STYLE\_*) 184983afe33SPhil Shafer :returns: void 185983afe33SPhil Shafer 186983afe33SPhil Shafer :: 187983afe33SPhil Shafer 188983afe33SPhil Shafer EXAMPLE: 189983afe33SPhil Shafer xo_set_style(NULL, XO_STYLE_XML); 190983afe33SPhil Shafer 191983afe33SPhil Shafer.. index:: xo_set_style_name 192983afe33SPhil Shafer 193983afe33SPhil Shaferxo_set_style_name 194983afe33SPhil Shafer~~~~~~~~~~~~~~~~~ 195983afe33SPhil Shafer 196983afe33SPhil Shafer.. c:function:: int xo_set_style_name (xo_handle_t *xop, const char *style) 197983afe33SPhil Shafer 198983afe33SPhil Shafer The `xo_set_style_name` function can be used to set the style based 199983afe33SPhil Shafer on a name encoded as a string: The name can be any of the supported 200983afe33SPhil Shafer styles: "text", "xml", "json", or "html". 201983afe33SPhil Shafer 202983afe33SPhil Shafer :param xop: Handle for modify (or NULL for default handle) 203983afe33SPhil Shafer :type xop: xo_handle_t \* 204983afe33SPhil Shafer :param style: Text name of the style 205983afe33SPhil Shafer :type style: const char \* 206983afe33SPhil Shafer :returns: zero for success, non-zero for error 207983afe33SPhil Shafer :rtype: int 208983afe33SPhil Shafer 209983afe33SPhil Shafer :: 210983afe33SPhil Shafer 211983afe33SPhil Shafer EXAMPLE: 212983afe33SPhil Shafer xo_set_style_name(NULL, "html"); 213983afe33SPhil Shafer 214983afe33SPhil Shafer.. index:: xo_set_flags 215983afe33SPhil Shafer 216983afe33SPhil Shaferxo_set_flags 217983afe33SPhil Shafer~~~~~~~~~~~~ 218983afe33SPhil Shafer 219983afe33SPhil Shafer.. c:function:: void xo_set_flags(xo_handle_t *xop, xo_xof_flags_t flags) 220983afe33SPhil Shafer 221983afe33SPhil Shafer :param xop: Handle for modify (or NULL for default handle) 222983afe33SPhil Shafer :type xop: xo_handle_t \* 223983afe33SPhil Shafer :param xo_xof_flags_t flags: Flags to add for the handle 224983afe33SPhil Shafer :returns: void 225983afe33SPhil Shafer 226983afe33SPhil Shafer Use the `xo_set_flags` function to turn on flags for a given libxo 227983afe33SPhil Shafer handle. To use the default handle, pass a `NULL` handle. 228983afe33SPhil Shafer 229983afe33SPhil Shafer :: 230983afe33SPhil Shafer 231983afe33SPhil Shafer EXAMPLE: 232983afe33SPhil Shafer xo_set_flags(NULL, XOF_PRETTY | XOF_WARN); 233983afe33SPhil Shafer 234983afe33SPhil Shafer.. index:: Flags; XOF_* 235983afe33SPhil Shafer.. index:: XOF_CLOSE_FP 236983afe33SPhil Shafer.. index:: XOF_COLOR 237983afe33SPhil Shafer.. index:: XOF_COLOR_ALLOWED 238983afe33SPhil Shafer.. index:: XOF_DTRT 239983afe33SPhil Shafer.. index:: XOF_INFO 240983afe33SPhil Shafer.. index:: XOF_KEYS 241983afe33SPhil Shafer.. index:: XOF_NO_ENV 242983afe33SPhil Shafer.. index:: XOF_NO_HUMANIZE 243983afe33SPhil Shafer.. index:: XOF_PRETTY 244983afe33SPhil Shafer.. index:: XOF_UNDERSCORES 245983afe33SPhil Shafer.. index:: XOF_UNITS 246983afe33SPhil Shafer.. index:: XOF_WARN 247983afe33SPhil Shafer.. index:: XOF_WARN_XML 248983afe33SPhil Shafer.. index:: XOF_XPATH 249983afe33SPhil Shafer.. index:: XOF_COLUMNS 250983afe33SPhil Shafer.. index:: XOF_FLUSH 251983afe33SPhil Shafer 252983afe33SPhil Shafer.. _flags: 253983afe33SPhil Shafer 254983afe33SPhil ShaferFlags (XOF\_\*) 255983afe33SPhil Shafer+++++++++++++++ 256983afe33SPhil Shafer 257983afe33SPhil ShaferThe set of valid flags include: 258983afe33SPhil Shafer 259983afe33SPhil Shafer =================== ========================================= 260983afe33SPhil Shafer Flag Description 261983afe33SPhil Shafer =================== ========================================= 262983afe33SPhil Shafer XOF_CLOSE_FP Close file pointer on `xo_destroy` 263983afe33SPhil Shafer XOF_COLOR Enable color and effects in output 264983afe33SPhil Shafer XOF_COLOR_ALLOWED Allow color/effect for terminal output 265983afe33SPhil Shafer XOF_DTRT Enable "do the right thing" mode 266983afe33SPhil Shafer XOF_INFO Display info data attributes (HTML) 267983afe33SPhil Shafer XOF_KEYS Emit the key attribute (XML) 268983afe33SPhil Shafer XOF_NO_ENV Do not use the :ref:`libxo-options` env var 269983afe33SPhil Shafer XOF_NO_HUMANIZE Display humanization (TEXT, HTML) 270983afe33SPhil Shafer XOF_PRETTY Make "pretty printed" output 271983afe33SPhil Shafer XOF_UNDERSCORES Replaces hyphens with underscores 272983afe33SPhil Shafer XOF_UNITS Display units (XML, HMTL) 273983afe33SPhil Shafer XOF_WARN Generate warnings for broken calls 274983afe33SPhil Shafer XOF_WARN_XML Generate warnings in XML on stdout 275983afe33SPhil Shafer XOF_XPATH Emit XPath expressions (HTML) 276983afe33SPhil Shafer XOF_COLUMNS Force xo_emit to return columns used 277983afe33SPhil Shafer XOF_FLUSH Flush output after each `xo_emit` call 278983afe33SPhil Shafer =================== ========================================= 279983afe33SPhil Shafer 280983afe33SPhil ShaferThe `XOF_CLOSE_FP` flag will trigger the call of the *close_func* 281983afe33SPhil Shafer(provided via `xo_set_writer`) when the handle is destroyed. 282983afe33SPhil Shafer 283983afe33SPhil ShaferThe `XOF_COLOR` flag enables color and effects in output regardless 284983afe33SPhil Shaferof output device, while the `XOF_COLOR_ALLOWED` flag allows color 285983afe33SPhil Shaferand effects only if the output device is a terminal. 286983afe33SPhil Shafer 287983afe33SPhil ShaferThe `XOF_PRETTY` flag requests "pretty printing", which will trigger 288983afe33SPhil Shaferthe addition of indentation and newlines to enhance the readability of 289983afe33SPhil ShaferXML, JSON, and HTML output. Text output is not affected. 290983afe33SPhil Shafer 291983afe33SPhil ShaferThe `XOF_WARN` flag requests that warnings will trigger diagnostic 292983afe33SPhil Shaferoutput (on standard error) when the library notices errors during 293983afe33SPhil Shaferoperations, or with arguments to functions. Without warnings enabled, 294983afe33SPhil Shafersuch conditions are ignored. 295983afe33SPhil Shafer 296983afe33SPhil ShaferWarnings allow developers to debug their interaction with libxo. 297983afe33SPhil ShaferThe function `xo_failure` can used as a breakpoint for a debugger, 298983afe33SPhil Shaferregardless of whether warnings are enabled. 299983afe33SPhil Shafer 300983afe33SPhil ShaferIf the style is `XO_STYLE_HTML`, the following additional flags can be 301983afe33SPhil Shaferused: 302983afe33SPhil Shafer 303983afe33SPhil Shafer =============== ========================================= 304983afe33SPhil Shafer Flag Description 305983afe33SPhil Shafer =============== ========================================= 306983afe33SPhil Shafer XOF_XPATH Emit "data-xpath" attributes 307983afe33SPhil Shafer XOF_INFO Emit additional info fields 308983afe33SPhil Shafer =============== ========================================= 309983afe33SPhil Shafer 310983afe33SPhil ShaferThe `XOF_XPATH` flag enables the emission of XPath expressions detailing 311983afe33SPhil Shaferthe hierarchy of XML elements used to encode the data field, if the 312983afe33SPhil ShaferXPATH style of output were requested. 313983afe33SPhil Shafer 314983afe33SPhil ShaferThe `XOF_INFO` flag encodes additional informational fields for HTML 315983afe33SPhil Shaferoutput. See :ref:`field-information` for details. 316983afe33SPhil Shafer 317983afe33SPhil ShaferIf the style is `XO_STYLE_XML`, the following additional flags can be 318983afe33SPhil Shaferused: 319983afe33SPhil Shafer 320983afe33SPhil Shafer =============== ========================================= 321983afe33SPhil Shafer Flag Description 322983afe33SPhil Shafer =============== ========================================= 323983afe33SPhil Shafer XOF_KEYS Flag "key" fields for XML 324983afe33SPhil Shafer =============== ========================================= 325983afe33SPhil Shafer 326983afe33SPhil ShaferThe `XOF_KEYS` flag adds "key" attribute to the XML encoding for 327983afe33SPhil Shaferfield definitions that use the "k" modifier. The key attribute has 328983afe33SPhil Shaferthe value "key":: 329983afe33SPhil Shafer 330983afe33SPhil Shafer xo_emit("{k:name}", item); 331983afe33SPhil Shafer 332983afe33SPhil Shafer XML: 333983afe33SPhil Shafer <name key="key">truck</name> 334983afe33SPhil Shafer 335983afe33SPhil Shafer.. index:: xo_clear_flags 336983afe33SPhil Shafer 337983afe33SPhil Shaferxo_clear_flags 338983afe33SPhil Shafer++++++++++++++ 339983afe33SPhil Shafer 340983afe33SPhil Shafer.. c:function:: void xo_clear_flags (xo_handle_t *xop, xo_xof_flags_t flags) 341983afe33SPhil Shafer 342983afe33SPhil Shafer :param xop: Handle for modify (or NULL for default handle) 343983afe33SPhil Shafer :type xop: xo_handle_t \* 344983afe33SPhil Shafer :param xo_xof_flags_t flags: Flags to clear for the handle 345983afe33SPhil Shafer :returns: void 346983afe33SPhil Shafer 347983afe33SPhil Shafer Use the `xo_clear_flags` function to turn off the given flags in a 348983afe33SPhil Shafer specific handle. To use the default handle, pass a `NULL` handle. 349983afe33SPhil Shafer 350983afe33SPhil Shafer.. index:: xo_set_options 351983afe33SPhil Shafer 352983afe33SPhil Shaferxo_set_options 353983afe33SPhil Shafer++++++++++++++ 354983afe33SPhil Shafer 355983afe33SPhil Shafer.. c:function:: int xo_set_options (xo_handle_t *xop, const char *input) 356983afe33SPhil Shafer 357983afe33SPhil Shafer :param xop: Handle for modify (or NULL for default handle) 358983afe33SPhil Shafer :type xop: xo_handle_t \* 359983afe33SPhil Shafer :param input: string containing options to set 360983afe33SPhil Shafer :type input: const char * 361983afe33SPhil Shafer :returns: zero for success, non-zero for error 362983afe33SPhil Shafer :rtype: int 363983afe33SPhil Shafer 364983afe33SPhil Shafer The `xo_set_options` function accepts a comma-separated list of 365983afe33SPhil Shafer output styles and modifier flags and enables them for a specific 366983afe33SPhil Shafer handle. The options are identical to those listed in 367983afe33SPhil Shafer :ref:`options`. To use the default handle, pass a `NULL` handle. 368983afe33SPhil Shafer 369983afe33SPhil Shafer.. index:: xo_destroy 370983afe33SPhil Shafer 371983afe33SPhil Shaferxo_destroy 372983afe33SPhil Shafer++++++++++ 373983afe33SPhil Shafer 374983afe33SPhil Shafer.. c:function:: void xo_destroy(xo_handle_t *xop) 375983afe33SPhil Shafer 376983afe33SPhil Shafer :param xop: Handle for modify (or NULL for default handle) 377983afe33SPhil Shafer :type xop: xo_handle_t \* 378983afe33SPhil Shafer :returns: void 379983afe33SPhil Shafer 380983afe33SPhil Shafer The `xo_destroy` function releases a handle and any resources it is 381983afe33SPhil Shafer using. Calling `xo_destroy` with a `NULL` handle will release any 382983afe33SPhil Shafer resources associated with the default handle. 383983afe33SPhil Shafer 384983afe33SPhil Shafer.. index:: xo_emit 385983afe33SPhil Shafer 386983afe33SPhil ShaferEmitting Content (xo_emit) 387983afe33SPhil Shafer-------------------------- 388983afe33SPhil Shafer 389*34b867caSPhil ShaferThe functions in this section are used to emit output. They use a 390*34b867caSPhil Shafer`format` string containing field descriptors as specified in 391*34b867caSPhil Shafer:ref:`format-strings`. The use of a handle is optional and `NULL` can 392*34b867caSPhil Shaferbe passed to access the internal "default" handle. See 393983afe33SPhil Shafer:ref:`handles`. 394983afe33SPhil Shafer 395983afe33SPhil ShaferThe remaining arguments to `xo_emit` and `xo_emit_h` are a set of 396983afe33SPhil Shaferarguments corresponding to the fields in the format string. Care must 397983afe33SPhil Shaferbe taken to ensure the argument types match the fields in the format 398*34b867caSPhil Shaferstring, since an inappropriate or missing argument can ruin your day. 399*34b867caSPhil ShaferThe `vap` argument to `xo_emit_hv` points to a variable argument list 400*34b867caSPhil Shaferthat can be used to retrieve arguments via `va_arg`. 401983afe33SPhil Shafer 402406a584dSPhil Shafer.. c:function:: xo_ssize_t xo_emit (const char *fmt, ...) 403983afe33SPhil Shafer 404983afe33SPhil Shafer :param fmt: The format string, followed by zero or more arguments 405983afe33SPhil Shafer :returns: If XOF_COLUMNS is set, the number of columns used; otherwise the number of bytes emitted 406406a584dSPhil Shafer :rtype: xo_ssize_t 407983afe33SPhil Shafer 408406a584dSPhil Shafer.. c:function:: xo_ssize_t xo_emit_h (xo_handle_t *xop, const char *fmt, ...) 409983afe33SPhil Shafer 410983afe33SPhil Shafer :param xop: Handle for modify (or NULL for default handle) 411983afe33SPhil Shafer :type xop: xo_handle_t \* 412983afe33SPhil Shafer :param fmt: The format string, followed by zero or more arguments 413983afe33SPhil Shafer :returns: If XOF_COLUMNS is set, the number of columns used; otherwise the number of bytes emitted 414406a584dSPhil Shafer :rtype: xo_ssize_t 415983afe33SPhil Shafer 416406a584dSPhil Shafer.. c:function:: xo_ssize_t xo_emit_hv (xo_handle_t *xop, const char *fmt, va_list vap) 417983afe33SPhil Shafer 418983afe33SPhil Shafer :param xop: Handle for modify (or NULL for default handle) 419983afe33SPhil Shafer :type xop: xo_handle_t \* 420983afe33SPhil Shafer :param fmt: The format string 421983afe33SPhil Shafer :param va_list vap: A set of variadic arguments 422983afe33SPhil Shafer :returns: If XOF_COLUMNS is set, the number of columns used; otherwise the number of bytes emitted 423406a584dSPhil Shafer :rtype: xo_ssize_t 424983afe33SPhil Shafer 425983afe33SPhil Shafer.. index:: xo_emit_field 426983afe33SPhil Shafer 427983afe33SPhil ShaferSingle Field Emitting Functions (xo_emit_field) 428983afe33SPhil Shafer~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 429983afe33SPhil Shafer 430*34b867caSPhil ShaferThe functions in this section emit formatted output similar to 431*34b867caSPhil Shafer`xo_emit` but where `xo_emit` uses a single string argument containing 432*34b867caSPhil Shaferthe description for multiple fields, `xo_emit_field` emits a single 433*34b867caSPhil Shaferfield using multiple ar- guments to contain the field description. 434*34b867caSPhil Shafer`xo_emit_field_h` adds an ex- plicit handle to use instead of the 435*34b867caSPhil Shaferdefault handle, while `xo_emit_field_hv` accepts a va_list for 436*34b867caSPhil Shaferadditional flexibility. 437983afe33SPhil Shafer 438*34b867caSPhil ShaferThe arguments `rolmod`, `content`, `fmt`, and `efmt` are detailed in 439*34b867caSPhil Shafer:ref:`field-formatting`. Using distinct arguments allows callers to 440*34b867caSPhil Shaferpass the field description in pieces, rather than having to use 441*34b867caSPhil Shafersomething like `snprintf` to build the format string required by 442*34b867caSPhil Shafer`xo_emit`. The arguments are each NUL-terminated strings. The `rolmod` 443*34b867caSPhil Shaferargument contains the `role` and `modifier` portions of the field 444*34b867caSPhil Shaferdescription, the `content` argument contains the `content` portion, and 445*34b867caSPhil Shaferthe `fmt` and `efmt` contain the `field-format` and `encoding-format` por- 446*34b867caSPhil Shafertions, respectively. 447*34b867caSPhil Shafer 448*34b867caSPhil ShaferAs with `xo_emit`, the `fmt` and `efmt` values are both optional, 449*34b867caSPhil Shafersince the `field-format` string defaults to "%s", and the 450*34b867caSPhil Shafer`encoding-format`'s default value is derived from the `field-format` 451*34b867caSPhil Shaferper :ref:`field-formatting`. However, care must be taken to avoid 452*34b867caSPhil Shaferusing a value directly as the format, since characters like '{', '%', 453*34b867caSPhil Shaferand '}' will be interpreted as formatting directives, and may cause 454*34b867caSPhil Shaferxo_emit_field to dereference arbitrary values off the stack, leading 455*34b867caSPhil Shaferto bugs, core files, and gnashing of teeth. 456*34b867caSPhil Shafer 457*34b867caSPhil Shafer.. c:function:: xo_ssize_t xo_emit_field (const char *rolmod, const char *content, const char *fmt, const char *efmt, ...) 458983afe33SPhil Shafer 459983afe33SPhil Shafer :param rolmod: A comma-separated list of field roles and field modifiers 460983afe33SPhil Shafer :type rolmod: const char * 461*34b867caSPhil Shafer :param content: The "content" portion of the field description string 462*34b867caSPhil Shafer :type content: const char * 463*34b867caSPhil Shafer :param fmt: Contents format string 464983afe33SPhil Shafer :type fmt: const char * 465983afe33SPhil Shafer :param efmt: Encoding format string, followed by additional arguments 466983afe33SPhil Shafer :type efmt: const char * 467983afe33SPhil Shafer :returns: If XOF_COLUMNS is set, the number of columns used; otherwise the number of bytes emitted 468406a584dSPhil Shafer :rtype: xo_ssize_t 469983afe33SPhil Shafer 470983afe33SPhil Shafer :: 471983afe33SPhil Shafer 472983afe33SPhil Shafer EXAMPLE:: 473*34b867caSPhil Shafer xo_emit_field("T", title, NULL, NULL, NULL); 474983afe33SPhil Shafer xo_emit_field("T", "Host name is ", NULL, NULL); 475983afe33SPhil Shafer xo_emit_field("V", "host-name", NULL, NULL, host-name); 476*34b867caSPhil Shafer xo_emit_field(",leaf-list,quotes", "sku", "%s-%u", "%s-000-%u", 477*34b867caSPhil Shafer "gum", 1412); 478983afe33SPhil Shafer 479406a584dSPhil Shafer.. c:function:: xo_ssize_t xo_emit_field_h (xo_handle_t *xop, const char *rolmod, const char *contents, const char *fmt, const char *efmt, ...) 480983afe33SPhil Shafer 481983afe33SPhil Shafer :param xop: Handle for modify (or NULL for default handle) 482983afe33SPhil Shafer :type xop: xo_handle_t \* 483983afe33SPhil Shafer :param rolmod: A comma-separated list of field roles and field modifiers 484983afe33SPhil Shafer :type rolmod: const char * 485983afe33SPhil Shafer :param contents: The "contents" portion of the field description string 486983afe33SPhil Shafer :type contents: const char * 487983afe33SPhil Shafer :param fmt: Content format string 488983afe33SPhil Shafer :type fmt: const char * 489983afe33SPhil Shafer :param efmt: Encoding format string, followed by additional arguments 490983afe33SPhil Shafer :type efmt: const char * 491983afe33SPhil Shafer :returns: If XOF_COLUMNS is set, the number of columns used; otherwise the number of bytes emitted 492406a584dSPhil Shafer :rtype: xo_ssize_t 493983afe33SPhil Shafer 494406a584dSPhil Shafer.. c:function:: xo_ssize_t xo_emit_field_hv (xo_handle_t *xop, const char *rolmod, const char *contents, const char *fmt, const char *efmt, va_list vap) 495983afe33SPhil Shafer 496983afe33SPhil Shafer :param xop: Handle for modify (or NULL for default handle) 497983afe33SPhil Shafer :type xop: xo_handle_t \* 498983afe33SPhil Shafer :param rolmod: A comma-separated list of field roles and field modifiers 499983afe33SPhil Shafer :type rolmod: const char * 500983afe33SPhil Shafer :param contents: The "contents" portion of the field description string 501983afe33SPhil Shafer :type contents: const char * 502983afe33SPhil Shafer :param fmt: Content format string 503983afe33SPhil Shafer :type fmt: const char * 504983afe33SPhil Shafer :param efmt: Encoding format string 505983afe33SPhil Shafer :type efmt: const char * 506983afe33SPhil Shafer :param va_list vap: A set of variadic arguments 507983afe33SPhil Shafer :returns: If XOF_COLUMNS is set, the number of columns used; otherwise the number of bytes emitted 508406a584dSPhil Shafer :rtype: xo_ssize_t 509983afe33SPhil Shafer 510983afe33SPhil Shafer.. index:: xo_attr 511983afe33SPhil Shafer.. _xo_attr: 512983afe33SPhil Shafer 513983afe33SPhil ShaferAttributes (xo_attr) 514983afe33SPhil Shafer~~~~~~~~~~~~~~~~~~~~ 515983afe33SPhil Shafer 516983afe33SPhil ShaferThe functions in this section emit an XML attribute with the given name 517983afe33SPhil Shaferand value. This only affects the XML output style. 518983afe33SPhil Shafer 519983afe33SPhil ShaferThe `name` parameter give the name of the attribute to be encoded. The 520983afe33SPhil Shafer`fmt` parameter gives a printf-style format string used to format the 521983afe33SPhil Shafervalue of the attribute using any remaining arguments, or the vap 522983afe33SPhil Shaferparameter passed to `xo_attr_hv`. 523983afe33SPhil Shafer 524983afe33SPhil ShaferAll attributes recorded via `xo_attr` are placed on the next 525983afe33SPhil Shafercontainer, instance, leaf, or leaf list that is emitted. 526983afe33SPhil Shafer 527983afe33SPhil ShaferSince attributes are only emitted in XML, their use should be limited 528983afe33SPhil Shaferto meta-data and additional or redundant representations of data 529983afe33SPhil Shaferalready emitted in other form. 530983afe33SPhil Shafer 531406a584dSPhil Shafer.. c:function:: xo_ssize_t xo_attr (const char *name, const char *fmt, ...) 532983afe33SPhil Shafer 533983afe33SPhil Shafer :param name: Attribute name 534983afe33SPhil Shafer :type name: const char * 535983afe33SPhil Shafer :param fmt: Attribute value, as variadic arguments 536983afe33SPhil Shafer :type fmt: const char * 537983afe33SPhil Shafer :returns: -1 for error, or the number of bytes in the formatted attribute value 538406a584dSPhil Shafer :rtype: xo_ssize_t 539983afe33SPhil Shafer 540983afe33SPhil Shafer :: 541983afe33SPhil Shafer 542983afe33SPhil Shafer EXAMPLE: 543983afe33SPhil Shafer xo_attr("seconds", "%ld", (unsigned long) login_time); 544983afe33SPhil Shafer struct tm *tmp = localtime(login_time); 545983afe33SPhil Shafer strftime(buf, sizeof(buf), "%R", tmp); 546983afe33SPhil Shafer xo_emit("Logged in at {:login-time}\n", buf); 547983afe33SPhil Shafer XML: 548983afe33SPhil Shafer <login-time seconds="1408336270">00:14</login-time> 549983afe33SPhil Shafer 550983afe33SPhil Shafer 551406a584dSPhil Shafer.. c:function:: xo_ssize_t xo_attr_h (xo_handle_t *xop, const char *name, const char *fmt, ...) 552983afe33SPhil Shafer 553983afe33SPhil Shafer :param xop: Handle for modify (or NULL for default handle) 554983afe33SPhil Shafer :type xop: xo_handle_t \* 555983afe33SPhil Shafer 556983afe33SPhil Shafer The `xo_attr_h` function follows the conventions of `xo_attr` but 557983afe33SPhil Shafer adds an explicit libxo handle. 558983afe33SPhil Shafer 559406a584dSPhil Shafer.. c:function:: xo_ssize_t xo_attr_hv (xo_handle_t *xop, const char *name, const char *fmt, va_list vap) 560983afe33SPhil Shafer 561983afe33SPhil Shafer The `xo_attr_h` function follows the conventions of `xo_attr_h` 562983afe33SPhil Shafer but replaced the variadic list with a variadic pointer. 563983afe33SPhil Shafer 564983afe33SPhil Shafer.. index:: xo_flush 565983afe33SPhil Shafer 566983afe33SPhil ShaferFlushing Output (xo_flush) 567983afe33SPhil Shafer~~~~~~~~~~~~~~~~~~~~~~~~~~ 568983afe33SPhil Shafer 569983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_flush (void) 570983afe33SPhil Shafer 571983afe33SPhil Shafer :returns: -1 for error, or the number of bytes generated 572983afe33SPhil Shafer :rtype: xo_ssize_t 573983afe33SPhil Shafer 574983afe33SPhil Shafer libxo buffers data, both for performance and consistency, but also 575983afe33SPhil Shafer to allow for the proper function of various advanced features. At 576983afe33SPhil Shafer various times, the caller may wish to flush any data buffered within 577983afe33SPhil Shafer the library. The `xo_flush` call is used for this. 578983afe33SPhil Shafer 579983afe33SPhil Shafer Calling `xo_flush` also triggers the flush function associated with 580983afe33SPhil Shafer the handle. For the default handle, this is equivalent to 581983afe33SPhil Shafer "fflush(stdio);". 582983afe33SPhil Shafer 583983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_flush_h (xo_handle_t *xop) 584983afe33SPhil Shafer 585983afe33SPhil Shafer :param xop: Handle for flush (or NULL for default handle) 586983afe33SPhil Shafer :type xop: xo_handle_t \* 587983afe33SPhil Shafer :returns: -1 for error, or the number of bytes generated 588983afe33SPhil Shafer :rtype: xo_ssize_t 589983afe33SPhil Shafer 590983afe33SPhil Shafer The `xo_flush_h` function follows the conventions of `xo_flush`, 591983afe33SPhil Shafer but adds an explicit libxo handle. 592983afe33SPhil Shafer 593983afe33SPhil Shafer.. index:: xo_finish 594983afe33SPhil Shafer.. index:: xo_finish_atexit 595983afe33SPhil Shafer.. index:: atexit 596983afe33SPhil Shafer 597983afe33SPhil ShaferFinishing Output (xo_finish) 598983afe33SPhil Shafer~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 599983afe33SPhil Shafer 600983afe33SPhil ShaferWhen the program is ready to exit or close a handle, a call to 601983afe33SPhil Shafer`xo_finish` or `xo_finish_h` is required. This flushes any buffered 602983afe33SPhil Shaferdata, closes open libxo constructs, and completes any pending 603983afe33SPhil Shaferoperations. 604983afe33SPhil Shafer 605983afe33SPhil ShaferCalling this function is vital to the proper operation of libxo, 606983afe33SPhil Shaferespecially for the non-TEXT output styles. 607983afe33SPhil Shafer 608983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_finish (void) 609983afe33SPhil Shafer 610983afe33SPhil Shafer :returns: -1 on error, or the number of bytes flushed 611983afe33SPhil Shafer :rtype: xo_ssize_t 612983afe33SPhil Shafer 613983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_finish_h (xo_handle_t *xop) 614983afe33SPhil Shafer 615983afe33SPhil Shafer :param xop: Handle for finish (or NULL for default handle) 616983afe33SPhil Shafer :type xop: xo_handle_t \* 617983afe33SPhil Shafer :returns: -1 on error, or the number of bytes flushed 618983afe33SPhil Shafer :rtype: xo_ssize_t 619983afe33SPhil Shafer 620983afe33SPhil Shafer.. c:function:: void xo_finish_atexit (void) 621983afe33SPhil Shafer 622983afe33SPhil Shafer The `xo_finish_atexit` function is suitable for use with 623983afe33SPhil Shafer :manpage:`atexit(3)` to ensure that `xo_finish` is called 624983afe33SPhil Shafer on the default handle when the application exits. 625983afe33SPhil Shafer 626983afe33SPhil Shafer.. index:: UTF-8 627983afe33SPhil Shafer.. index:: xo_open_container 628983afe33SPhil Shafer.. index:: xo_close_container 629983afe33SPhil Shafer 630983afe33SPhil ShaferEmitting Hierarchy 631983afe33SPhil Shafer------------------ 632983afe33SPhil Shafer 633983afe33SPhil Shaferlibxo represents two types of hierarchy: containers and lists. A 634983afe33SPhil Shafercontainer appears once under a given parent where a list consists of 635983afe33SPhil Shaferinstances that can appear multiple times. A container is used to hold 636983afe33SPhil Shaferrelated fields and to give the data organization and scope. 637983afe33SPhil Shafer 638983afe33SPhil Shafer.. index:: YANG 639983afe33SPhil Shafer 640983afe33SPhil Shafer.. admonition:: YANG Terminology 641983afe33SPhil Shafer 642983afe33SPhil Shafer libxo uses terminology from YANG (:RFC:`7950`), the data modeling 643983afe33SPhil Shafer language for NETCONF: container, list, leaf, and leaf-list. 644983afe33SPhil Shafer 645983afe33SPhil ShaferFor XML and JSON, individual fields appear inside hierarchies which 646983afe33SPhil Shaferprovide context and meaning to the fields. Unfortunately, these 647983afe33SPhil Shaferencoding have a basic disconnect between how lists is similar objects 648983afe33SPhil Shaferare represented. 649983afe33SPhil Shafer 650983afe33SPhil ShaferXML encodes lists as set of sequential elements:: 651983afe33SPhil Shafer 652983afe33SPhil Shafer <user>phil</user> 653983afe33SPhil Shafer <user>pallavi</user> 654983afe33SPhil Shafer <user>sjg</user> 655983afe33SPhil Shafer 656983afe33SPhil ShaferJSON encodes lists using a single name and square brackets:: 657983afe33SPhil Shafer 658983afe33SPhil Shafer "user": [ "phil", "pallavi", "sjg" ] 659983afe33SPhil Shafer 660983afe33SPhil ShaferThis means libxo needs three distinct indications of hierarchy: one 661983afe33SPhil Shaferfor containers of hierarchy appear only once for any specific parent, 662983afe33SPhil Shaferone for lists, and one for each item in a list. 663983afe33SPhil Shafer 664983afe33SPhil Shafer.. index:: Containers 665983afe33SPhil Shafer 666983afe33SPhil ShaferContainers 667983afe33SPhil Shafer~~~~~~~~~~ 668983afe33SPhil Shafer 669983afe33SPhil ShaferA "*container*" is an element of a hierarchy that appears only once 670983afe33SPhil Shaferunder any specific parent. The container has no value, but serves to 671983afe33SPhil Shafercontain and organize other nodes. 672983afe33SPhil Shafer 673983afe33SPhil ShaferTo open a container, call xo_open_container() or 674983afe33SPhil Shaferxo_open_container_h(). The former uses the default handle and the 675983afe33SPhil Shaferlatter accepts a specific handle. To close a level, use the 676983afe33SPhil Shaferxo_close_container() or xo_close_container_h() functions. 677983afe33SPhil Shafer 678983afe33SPhil ShaferEach open call must have a matching close call. If the XOF_WARN flag 679983afe33SPhil Shaferis set and the name given does not match the name of the currently open 680983afe33SPhil Shafercontainer, a warning will be generated. 681983afe33SPhil Shafer 682983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_open_container (const char *name) 683983afe33SPhil Shafer 684983afe33SPhil Shafer :param name: Name of the container 685983afe33SPhil Shafer :type name: const char * 686983afe33SPhil Shafer :returns: -1 on error, or the number of bytes generated 687983afe33SPhil Shafer :rtype: xo_ssize_t 688983afe33SPhil Shafer 689983afe33SPhil Shafer The `name` parameter gives the name of the container, encoded in 690983afe33SPhil Shafer UTF-8. Since ASCII is a proper subset of UTF-8, traditional C 691983afe33SPhil Shafer strings can be used directly. 692983afe33SPhil Shafer 693983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_open_container_h (xo_handle_t *xop, const char *name) 694983afe33SPhil Shafer 695983afe33SPhil Shafer :param xop: Handle to use (or NULL for default handle) 696983afe33SPhil Shafer :type xop: xo_handle_t * 697983afe33SPhil Shafer 698983afe33SPhil Shafer The `xo_open_container_h` function adds a `handle` parameter. 699983afe33SPhil Shafer 700983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_close_container (const char *name) 701983afe33SPhil Shafer 702983afe33SPhil Shafer :param name: Name of the container 703983afe33SPhil Shafer :type name: const char * 704983afe33SPhil Shafer :returns: -1 on error, or the number of bytes generated 705983afe33SPhil Shafer :rtype: xo_ssize_t 706983afe33SPhil Shafer 707983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_close_container_h (xo_handle_t *xop, const char *name) 708983afe33SPhil Shafer 709983afe33SPhil Shafer :param xop: Handle to use (or NULL for default handle) 710983afe33SPhil Shafer :type xop: xo_handle_t * 711983afe33SPhil Shafer 712983afe33SPhil Shafer The `xo_close_container_h` function adds a `handle` parameter. 713983afe33SPhil Shafer 714983afe33SPhil ShaferUse the :index:`XOF_WARN` flag to generate a warning if the name given 715983afe33SPhil Shaferon the close does not match the current open container. 716983afe33SPhil Shafer 717983afe33SPhil ShaferFor TEXT and HTML output, containers are not rendered into output 718983afe33SPhil Shafertext, though for HTML they are used to record an XPath value when the 719983afe33SPhil Shafer:index:`XOF_XPATH` flag is set. 720983afe33SPhil Shafer 721983afe33SPhil Shafer:: 722983afe33SPhil Shafer 723983afe33SPhil Shafer EXAMPLE: 724983afe33SPhil Shafer xo_open_container("top"); 725983afe33SPhil Shafer xo_open_container("system"); 726983afe33SPhil Shafer xo_emit("{:host-name/%s%s%s}", hostname, 727983afe33SPhil Shafer domainname ? "." : "", domainname ?: ""); 728983afe33SPhil Shafer xo_close_container("system"); 729983afe33SPhil Shafer xo_close_container("top"); 730983afe33SPhil Shafer TEXT: 731983afe33SPhil Shafer my-host.example.org 732983afe33SPhil Shafer XML: 733983afe33SPhil Shafer <top> 734983afe33SPhil Shafer <system> 735983afe33SPhil Shafer <host-name>my-host.example.org</host-name> 736983afe33SPhil Shafer </system> 737983afe33SPhil Shafer </top> 738983afe33SPhil Shafer JSON: 739983afe33SPhil Shafer "top" : { 740983afe33SPhil Shafer "system" : { 741983afe33SPhil Shafer "host-name": "my-host.example.org" 742983afe33SPhil Shafer } 743983afe33SPhil Shafer } 744983afe33SPhil Shafer HTML: 745983afe33SPhil Shafer <div class="data" 746983afe33SPhil Shafer data-tag="host-name">my-host.example.org</div> 747983afe33SPhil Shafer 748983afe33SPhil Shafer.. index:: xo_open_instance 749983afe33SPhil Shafer.. index:: xo_close_instance 750983afe33SPhil Shafer.. index:: xo_open_list 751983afe33SPhil Shafer.. index:: xo_close_list 752983afe33SPhil Shafer 753983afe33SPhil ShaferLists and Instances 754983afe33SPhil Shafer~~~~~~~~~~~~~~~~~~~ 755983afe33SPhil Shafer 756983afe33SPhil ShaferA "*list*" is set of one or more instances that appear under the same 757983afe33SPhil Shaferparent. The instances contain details about a specific object. One 758983afe33SPhil Shafercan think of instances as objects or records. A call is needed to 759983afe33SPhil Shaferopen and close the list, while a distinct call is needed to open and 760983afe33SPhil Shaferclose each instance of the list. 761983afe33SPhil Shafer 762983afe33SPhil ShaferThe name given to all calls must be identical, and it is strongly 763983afe33SPhil Shafersuggested that the name be singular, not plural, as a matter of 764983afe33SPhil Shaferstyle and usage expectations:: 765983afe33SPhil Shafer 766983afe33SPhil Shafer EXAMPLE: 767983afe33SPhil Shafer xo_open_list("item"); 768983afe33SPhil Shafer 769983afe33SPhil Shafer for (ip = list; ip->i_title; ip++) { 770983afe33SPhil Shafer xo_open_instance("item"); 771983afe33SPhil Shafer xo_emit("{L:Item} '{:name/%s}':\n", ip->i_title); 772983afe33SPhil Shafer xo_close_instance("item"); 773983afe33SPhil Shafer } 774983afe33SPhil Shafer 775983afe33SPhil Shafer xo_close_list("item"); 776983afe33SPhil Shafer 777983afe33SPhil ShaferGetting the list and instance calls correct is critical to the proper 778983afe33SPhil Shafergeneration of XML and JSON data. 779983afe33SPhil Shafer 780983afe33SPhil ShaferOpening Lists 781983afe33SPhil Shafer+++++++++++++ 782983afe33SPhil Shafer 783983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_open_list (const char *name) 784983afe33SPhil Shafer 785983afe33SPhil Shafer :param name: Name of the list 786983afe33SPhil Shafer :type name: const char * 787983afe33SPhil Shafer :returns: -1 on error, or the number of bytes generated 788983afe33SPhil Shafer :rtype: xo_ssize_t 789983afe33SPhil Shafer 790983afe33SPhil Shafer The `xo_open_list` function open a list of instances. 791983afe33SPhil Shafer 792983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_open_list_h (xo_handle_t *xop, const char *name) 793983afe33SPhil Shafer 794983afe33SPhil Shafer :param xop: Handle to use (or NULL for default handle) 795983afe33SPhil Shafer :type xop: xo_handle_t * 796983afe33SPhil Shafer 797983afe33SPhil ShaferClosing Lists 798983afe33SPhil Shafer+++++++++++++ 799983afe33SPhil Shafer 800983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_close_list (const char *name) 801983afe33SPhil Shafer 802983afe33SPhil Shafer :param name: Name of the list 803983afe33SPhil Shafer :type name: const char * 804983afe33SPhil Shafer :returns: -1 on error, or the number of bytes generated 805983afe33SPhil Shafer :rtype: xo_ssize_t 806983afe33SPhil Shafer 807983afe33SPhil Shafer The `xo_close_list` function closes a list of instances. 808983afe33SPhil Shafer 809983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_close_list_h (xo_handle_t *xop, const char *name) 810983afe33SPhil Shafer 811983afe33SPhil Shafer :param xop: Handle to use (or NULL for default handle) 812983afe33SPhil Shafer :type xop: xo_handle_t * 813983afe33SPhil Shafer 814983afe33SPhil Shafer The `xo_close_container_h` function adds a `handle` parameter. 815983afe33SPhil Shafer 816983afe33SPhil ShaferOpening Instances 817983afe33SPhil Shafer+++++++++++++++++ 818983afe33SPhil Shafer 819983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_open_instance (const char *name) 820983afe33SPhil Shafer 821983afe33SPhil Shafer :param name: Name of the instance (same as the list name) 822983afe33SPhil Shafer :type name: const char * 823983afe33SPhil Shafer :returns: -1 on error, or the number of bytes generated 824983afe33SPhil Shafer :rtype: xo_ssize_t 825983afe33SPhil Shafer 826983afe33SPhil Shafer The `xo_open_instance` function open a single instance. 827983afe33SPhil Shafer 828983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_open_instance_h (xo_handle_t *xop, const char *name) 829983afe33SPhil Shafer 830983afe33SPhil Shafer :param xop: Handle to use (or NULL for default handle) 831983afe33SPhil Shafer :type xop: xo_handle_t * 832983afe33SPhil Shafer 833983afe33SPhil Shafer The `xo_open_instance_h` function adds a `handle` parameter. 834983afe33SPhil Shafer 835983afe33SPhil ShaferClosing Instances 836983afe33SPhil Shafer+++++++++++++++++ 837983afe33SPhil Shafer 838983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_close_instance (const char *name) 839983afe33SPhil Shafer 840983afe33SPhil Shafer :param name: Name of the instance 841983afe33SPhil Shafer :type name: const char * 842983afe33SPhil Shafer :returns: -1 on error, or the number of bytes generated 843983afe33SPhil Shafer :rtype: xo_ssize_t 844983afe33SPhil Shafer 845983afe33SPhil Shafer The `xo_close_instance` function closes an open instance. 846983afe33SPhil Shafer 847983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_close_instance_h (xo_handle_t *xop, const char *name) 848983afe33SPhil Shafer 849983afe33SPhil Shafer :param xop: Handle to use (or NULL for default handle) 850983afe33SPhil Shafer :type xop: xo_handle_t * 851983afe33SPhil Shafer 852983afe33SPhil Shafer The `xo_close_instance_h` function adds a `handle` parameter. 853983afe33SPhil Shafer 854983afe33SPhil Shafer :: 855983afe33SPhil Shafer 856983afe33SPhil Shafer EXAMPLE: 857983afe33SPhil Shafer xo_open_list("user"); 858983afe33SPhil Shafer for (i = 0; i < num_users; i++) { 859983afe33SPhil Shafer xo_open_instance("user"); 860983afe33SPhil Shafer xo_emit("{k:name}:{:uid/%u}:{:gid/%u}:{:home}\n", 861983afe33SPhil Shafer pw[i].pw_name, pw[i].pw_uid, 862983afe33SPhil Shafer pw[i].pw_gid, pw[i].pw_dir); 863983afe33SPhil Shafer xo_close_instance("user"); 864983afe33SPhil Shafer } 865983afe33SPhil Shafer xo_close_list("user"); 866983afe33SPhil Shafer TEXT: 867983afe33SPhil Shafer phil:1001:1001:/home/phil 868983afe33SPhil Shafer pallavi:1002:1002:/home/pallavi 869983afe33SPhil Shafer XML: 870983afe33SPhil Shafer <user> 871983afe33SPhil Shafer <name>phil</name> 872983afe33SPhil Shafer <uid>1001</uid> 873983afe33SPhil Shafer <gid>1001</gid> 874983afe33SPhil Shafer <home>/home/phil</home> 875983afe33SPhil Shafer </user> 876983afe33SPhil Shafer <user> 877983afe33SPhil Shafer <name>pallavi</name> 878983afe33SPhil Shafer <uid>1002</uid> 879983afe33SPhil Shafer <gid>1002</gid> 880983afe33SPhil Shafer <home>/home/pallavi</home> 881983afe33SPhil Shafer </user> 882983afe33SPhil Shafer JSON: 883983afe33SPhil Shafer user: [ 884983afe33SPhil Shafer { 885983afe33SPhil Shafer "name": "phil", 886983afe33SPhil Shafer "uid": 1001, 887983afe33SPhil Shafer "gid": 1001, 888983afe33SPhil Shafer "home": "/home/phil", 889983afe33SPhil Shafer }, 890983afe33SPhil Shafer { 891983afe33SPhil Shafer "name": "pallavi", 892983afe33SPhil Shafer "uid": 1002, 893983afe33SPhil Shafer "gid": 1002, 894983afe33SPhil Shafer "home": "/home/pallavi", 895983afe33SPhil Shafer } 896983afe33SPhil Shafer ] 897983afe33SPhil Shafer 898983afe33SPhil ShaferMarkers 899983afe33SPhil Shafer~~~~~~~ 900983afe33SPhil Shafer 901983afe33SPhil ShaferMarkers are used to protect and restore the state of open hierarchy 902983afe33SPhil Shaferconstructs (containers, lists, or instances). While a marker is open, 903983afe33SPhil Shaferno other open constructs can be closed. When a marker is closed, all 904983afe33SPhil Shaferconstructs open since the marker was opened will be closed. 905983afe33SPhil Shafer 906983afe33SPhil ShaferMarkers use names which are not user-visible, allowing the caller to 907983afe33SPhil Shaferchoose appropriate internal names. 908983afe33SPhil Shafer 909983afe33SPhil ShaferIn this example, the code whiffles through a list of fish, calling a 910983afe33SPhil Shaferfunction to emit details about each fish. The marker "fish-guts" is 911983afe33SPhil Shaferused to ensure that any constructs opened by the function are closed 912983afe33SPhil Shaferproperly:: 913983afe33SPhil Shafer 914983afe33SPhil Shafer EXAMPLE: 915983afe33SPhil Shafer for (i = 0; fish[i]; i++) { 916983afe33SPhil Shafer xo_open_instance("fish"); 917983afe33SPhil Shafer xo_open_marker("fish-guts"); 918983afe33SPhil Shafer dump_fish_details(i); 919983afe33SPhil Shafer xo_close_marker("fish-guts"); 920983afe33SPhil Shafer } 921983afe33SPhil Shafer 922983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_open_marker(const char *name) 923983afe33SPhil Shafer 924983afe33SPhil Shafer :param name: Name of the instance 925983afe33SPhil Shafer :type name: const char * 926983afe33SPhil Shafer :returns: -1 on error, or the number of bytes generated 927983afe33SPhil Shafer :rtype: xo_ssize_t 928983afe33SPhil Shafer 929983afe33SPhil Shafer The `xo_open_marker` function records the current state of open tags 930983afe33SPhil Shafer in order for `xo_close_marker` to close them at some later point. 931983afe33SPhil Shafer 932983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_open_marker_h(const char *name) 933983afe33SPhil Shafer 934983afe33SPhil Shafer :param xop: Handle to use (or NULL for default handle) 935983afe33SPhil Shafer :type xop: xo_handle_t * 936983afe33SPhil Shafer 937983afe33SPhil Shafer The `xo_open_marker_h` function adds a `handle` parameter. 938983afe33SPhil Shafer 939983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_close_marker(const char *name) 940983afe33SPhil Shafer 941983afe33SPhil Shafer :param name: Name of the instance 942983afe33SPhil Shafer :type name: const char * 943983afe33SPhil Shafer :returns: -1 on error, or the number of bytes generated 944983afe33SPhil Shafer :rtype: xo_ssize_t 945983afe33SPhil Shafer 946983afe33SPhil Shafer The `xo_close_marker` function closes any open containers, lists, or 947983afe33SPhil Shafer instances as needed to return to the state recorded when 948983afe33SPhil Shafer `xo_open_marker` was called with the matching name. 949983afe33SPhil Shafer 950983afe33SPhil Shafer.. c:function:: xo_ssize_t xo_close_marker(const char *name) 951983afe33SPhil Shafer 952983afe33SPhil Shafer :param xop: Handle to use (or NULL for default handle) 953983afe33SPhil Shafer :type xop: xo_handle_t * 954983afe33SPhil Shafer 955983afe33SPhil Shafer The `xo_close_marker_h` function adds a `handle` parameter. 956983afe33SPhil Shafer 957983afe33SPhil ShaferDTRT Mode 958983afe33SPhil Shafer~~~~~~~~~ 959983afe33SPhil Shafer 960983afe33SPhil ShaferSome users may find tracking the names of open containers, lists, and 961983afe33SPhil Shaferinstances inconvenient. libxo offers a "Do The Right Thing" mode, where 962983afe33SPhil Shaferlibxo will track the names of open containers, lists, and instances so 963983afe33SPhil Shaferthe close function can be called without a name. To enable DTRT mode, 964983afe33SPhil Shaferturn on the XOF_DTRT flag prior to making any other libxo output:: 965983afe33SPhil Shafer 966983afe33SPhil Shafer xo_set_flags(NULL, XOF_DTRT); 967983afe33SPhil Shafer 968983afe33SPhil Shafer.. index:: XOF_DTRT 969983afe33SPhil Shafer 970983afe33SPhil ShaferEach open and close function has a version with the suffix "_d", which 971983afe33SPhil Shaferwill close the open container, list, or instance:: 972983afe33SPhil Shafer 973983afe33SPhil Shafer xo_open_container_d("top"); 974983afe33SPhil Shafer ... 975983afe33SPhil Shafer xo_close_container_d(); 976983afe33SPhil Shafer 977983afe33SPhil ShaferThis also works for lists and instances:: 978983afe33SPhil Shafer 979983afe33SPhil Shafer xo_open_list_d("item"); 980983afe33SPhil Shafer for (...) { 981983afe33SPhil Shafer xo_open_instance_d("item"); 982983afe33SPhil Shafer xo_emit(...); 983983afe33SPhil Shafer xo_close_instance_d(); 984983afe33SPhil Shafer } 985983afe33SPhil Shafer xo_close_list_d(); 986983afe33SPhil Shafer 987983afe33SPhil Shafer.. index:: XOF_WARN 988983afe33SPhil Shafer 989983afe33SPhil ShaferNote that the XOF_WARN flag will also cause libxo to track open 990983afe33SPhil Shafercontainers, lists, and instances. A warning is generated when the 991983afe33SPhil Shafername given to the close function and the name recorded do not match. 992983afe33SPhil Shafer 993983afe33SPhil ShaferSupport Functions 994983afe33SPhil Shafer----------------- 995983afe33SPhil Shafer 996983afe33SPhil Shafer.. index:: xo_parse_args 997983afe33SPhil Shafer.. _xo_parse_args: 998983afe33SPhil Shafer 999983afe33SPhil ShaferParsing Command-line Arguments (xo_parse_args) 1000983afe33SPhil Shafer~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1001983afe33SPhil Shafer 1002983afe33SPhil Shafer.. c:function:: int xo_parse_args (int argc, char **argv) 1003983afe33SPhil Shafer 1004983afe33SPhil Shafer :param int argc: Number of arguments 1005983afe33SPhil Shafer :param argv: Array of argument strings 1006983afe33SPhil Shafer :return: -1 on error, or the number of remaining arguments 1007983afe33SPhil Shafer :rtype: int 1008983afe33SPhil Shafer 1009983afe33SPhil Shafer The `xo_parse_args` function is used to process a program's 1010983afe33SPhil Shafer arguments. libxo-specific options are processed and removed from 1011983afe33SPhil Shafer the argument list so the calling application does not need to 1012983afe33SPhil Shafer process them. If successful, a new value for argc is returned. On 1013983afe33SPhil Shafer failure, a message is emitted and -1 is returned:: 1014983afe33SPhil Shafer 1015983afe33SPhil Shafer argc = xo_parse_args(argc, argv); 1016983afe33SPhil Shafer if (argc < 0) 1017983afe33SPhil Shafer exit(EXIT_FAILURE); 1018983afe33SPhil Shafer 1019983afe33SPhil Shafer Following the call to xo_parse_args, the application can process the 1020983afe33SPhil Shafer remaining arguments in a normal manner. See :ref:`options` for a 1021983afe33SPhil Shafer description of valid arguments. 1022983afe33SPhil Shafer 1023983afe33SPhil Shafer.. index:: xo_set_program 1024983afe33SPhil Shafer 1025983afe33SPhil Shaferxo_set_program 1026983afe33SPhil Shafer~~~~~~~~~~~~~~ 1027983afe33SPhil Shafer 1028983afe33SPhil Shafer.. c:function:: void xo_set_program (const char *name) 1029983afe33SPhil Shafer 1030983afe33SPhil Shafer :param name: Name to use as the program name 1031983afe33SPhil Shafer :type name: const char * 1032983afe33SPhil Shafer :returns: void 1033983afe33SPhil Shafer 1034983afe33SPhil Shafer The `xo_set_program` function sets the name of the program as 1035983afe33SPhil Shafer reported by functions like `xo_failure`, `xo_warn`, `xo_err`, etc. 1036983afe33SPhil Shafer The program name is initialized by `xo_parse_args`, but subsequent 1037983afe33SPhil Shafer calls to `xo_set_program` can override this value:: 1038983afe33SPhil Shafer 1039983afe33SPhil Shafer EXAMPLE: 1040983afe33SPhil Shafer xo_set_program(argv[0]); 1041983afe33SPhil Shafer 1042983afe33SPhil Shafer Note that the value is not copied, so the memory passed to 1043983afe33SPhil Shafer `xo_set_program` (and `xo_parse_args`) must be maintained by the 1044983afe33SPhil Shafer caller. 1045983afe33SPhil Shafer 1046983afe33SPhil Shafer.. index:: xo_set_version 1047983afe33SPhil Shafer 1048983afe33SPhil Shaferxo_set_version 1049983afe33SPhil Shafer~~~~~~~~~~~~~~ 1050983afe33SPhil Shafer 1051983afe33SPhil Shafer.. c:function:: void xo_set_version (const char *version) 1052983afe33SPhil Shafer 1053983afe33SPhil Shafer :param name: Value to use as the version string 1054983afe33SPhil Shafer :type name: const char * 1055983afe33SPhil Shafer :returns: void 1056983afe33SPhil Shafer 1057983afe33SPhil Shafer The `xo_set_version` function records a version number to be emitted 1058983afe33SPhil Shafer as part of the data for encoding styles (XML and JSON). This 1059983afe33SPhil Shafer version number is suitable for tracking changes in the content, 1060983afe33SPhil Shafer allowing a user of the data to discern which version of the data 1061983afe33SPhil Shafer model is in use. 1062983afe33SPhil Shafer 1063983afe33SPhil Shafer.. c:function:: void xo_set_version_h (xo_handle_t *xop, const char *version) 1064983afe33SPhil Shafer 1065983afe33SPhil Shafer :param xop: Handle to use (or NULL for default handle) 1066983afe33SPhil Shafer :type xop: xo_handle_t * 1067983afe33SPhil Shafer 1068983afe33SPhil Shafer The `xo_set_version` function adds a `handle` parameter. 1069983afe33SPhil Shafer 1070983afe33SPhil Shafer.. index:: --libxo 1071983afe33SPhil Shafer.. index:: XOF_INFO 1072983afe33SPhil Shafer.. index:: xo_info_t 1073983afe33SPhil Shafer 1074983afe33SPhil Shafer.. _field-information: 1075983afe33SPhil Shafer 1076983afe33SPhil ShaferField Information (xo_info_t) 1077983afe33SPhil Shafer~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1078983afe33SPhil Shafer 1079983afe33SPhil ShaferHTML data can include additional information in attributes that 1080983afe33SPhil Shaferbegin with "data-". To enable this, three things must occur: 1081983afe33SPhil Shafer 1082983afe33SPhil ShaferFirst the application must build an array of xo_info_t structures, 1083983afe33SPhil Shaferone per tag. The array must be sorted by name, since libxo uses a 1084983afe33SPhil Shaferbinary search to find the entry that matches names from format 1085983afe33SPhil Shaferinstructions. 1086983afe33SPhil Shafer 1087983afe33SPhil ShaferSecond, the application must inform libxo about this information using 1088983afe33SPhil Shaferthe `xo_set_info` call:: 1089983afe33SPhil Shafer 1090983afe33SPhil Shafer typedef struct xo_info_s { 1091983afe33SPhil Shafer const char *xi_name; /* Name of the element */ 1092983afe33SPhil Shafer const char *xi_type; /* Type of field */ 1093983afe33SPhil Shafer const char *xi_help; /* Description of field */ 1094983afe33SPhil Shafer } xo_info_t; 1095983afe33SPhil Shafer 1096983afe33SPhil Shafer void xo_set_info (xo_handle_t *xop, xo_info_t *infop, int count); 1097983afe33SPhil Shafer 1098983afe33SPhil ShaferLike other libxo calls, passing `NULL` for the handle tells libxo to 1099983afe33SPhil Shaferuse the default handle. 1100983afe33SPhil Shafer 1101983afe33SPhil ShaferIf the count is -1, libxo will count the elements of infop, but there 1102983afe33SPhil Shafermust be an empty element at the end. More typically, the number is 1103983afe33SPhil Shaferknown to the application:: 1104983afe33SPhil Shafer 1105983afe33SPhil Shafer xo_info_t info[] = { 1106983afe33SPhil Shafer { "in-stock", "number", "Number of items in stock" }, 1107983afe33SPhil Shafer { "name", "string", "Name of the item" }, 1108983afe33SPhil Shafer { "on-order", "number", "Number of items on order" }, 1109983afe33SPhil Shafer { "sku", "string", "Stock Keeping Unit" }, 1110983afe33SPhil Shafer { "sold", "number", "Number of items sold" }, 1111983afe33SPhil Shafer }; 1112983afe33SPhil Shafer int info_count = (sizeof(info) / sizeof(info[0])); 1113983afe33SPhil Shafer ... 1114983afe33SPhil Shafer xo_set_info(NULL, info, info_count); 1115983afe33SPhil Shafer 1116983afe33SPhil ShaferThird, the emission of info must be triggered with the `XOF_INFO` flag 1117983afe33SPhil Shaferusing either the `xo_set_flags` function or the "`--libxo=info`" 1118983afe33SPhil Shafercommand line argument. 1119983afe33SPhil Shafer 1120983afe33SPhil ShaferThe type and help values, if present, are emitted as the "data-type" 1121983afe33SPhil Shaferand "data-help" attributes:: 1122983afe33SPhil Shafer 1123983afe33SPhil Shafer <div class="data" data-tag="sku" data-type="string" 1124983afe33SPhil Shafer data-help="Stock Keeping Unit">GRO-000-533</div> 1125983afe33SPhil Shafer 1126983afe33SPhil Shafer.. c:function:: void xo_set_info (xo_handle_t *xop, xo_info_t *infop, int count) 1127983afe33SPhil Shafer 1128983afe33SPhil Shafer :param xop: Handle to use (or NULL for default handle) 1129983afe33SPhil Shafer :type xop: xo_handle_t * 1130983afe33SPhil Shafer :param infop: Array of information structures 1131983afe33SPhil Shafer :type infop: xo_info_t * 1132983afe33SPhil Shafer :returns: void 1133983afe33SPhil Shafer 1134983afe33SPhil Shafer.. index:: xo_set_allocator 1135983afe33SPhil Shafer.. index:: xo_realloc_func_t 1136983afe33SPhil Shafer.. index:: xo_free_func_t 1137983afe33SPhil Shafer 1138983afe33SPhil ShaferMemory Allocation 1139983afe33SPhil Shafer~~~~~~~~~~~~~~~~~ 1140983afe33SPhil Shafer 1141983afe33SPhil ShaferThe `xo_set_allocator` function allows libxo to be used in 1142983afe33SPhil Shaferenvironments where the standard :manpage:`realloc(3)` and 1143983afe33SPhil Shafer:manpage:`free(3)` functions are not appropriate. 1144983afe33SPhil Shafer 1145983afe33SPhil Shafer.. c:function:: void xo_set_allocator (xo_realloc_func_t realloc_func, xo_free_func_t free_func) 1146983afe33SPhil Shafer 1147983afe33SPhil Shafer :param xo_realloc_func_t realloc_func: Allocation function 1148983afe33SPhil Shafer :param xo_free_func_t free_func: Free function 1149983afe33SPhil Shafer 1150983afe33SPhil Shafer *realloc_func* should expect the same arguments as 1151983afe33SPhil Shafer :manpage:`realloc(3)` and return a pointer to memory following the 1152983afe33SPhil Shafer same convention. *free_func* will receive the same argument as 1153983afe33SPhil Shafer :manpage:`free(3)` and should release it, as appropriate for the 1154983afe33SPhil Shafer environment. 1155983afe33SPhil Shafer 1156983afe33SPhil ShaferBy default, the standard :manpage:`realloc(3)` and :manpage:`free(3)` 1157983afe33SPhil Shaferfunctions are used. 1158983afe33SPhil Shafer 1159983afe33SPhil Shafer.. index:: --libxo 1160983afe33SPhil Shafer 1161983afe33SPhil Shafer.. _libxo-options: 1162983afe33SPhil Shafer 1163983afe33SPhil ShaferLIBXO_OPTIONS 1164983afe33SPhil Shafer~~~~~~~~~~~~~ 1165983afe33SPhil Shafer 1166983afe33SPhil ShaferThe environment variable "LIBXO_OPTIONS" can be set to a subset of 1167983afe33SPhil Shaferlibxo options, including: 1168983afe33SPhil Shafer 1169983afe33SPhil Shafer- color 1170983afe33SPhil Shafer- flush 1171983afe33SPhil Shafer- flush-line 1172983afe33SPhil Shafer- no-color 1173983afe33SPhil Shafer- no-humanize 1174983afe33SPhil Shafer- no-locale 1175983afe33SPhil Shafer- no-retain 1176983afe33SPhil Shafer- pretty 1177983afe33SPhil Shafer- retain 1178983afe33SPhil Shafer- underscores 1179983afe33SPhil Shafer- warn 1180983afe33SPhil Shafer 1181983afe33SPhil ShaferFor example, warnings can be enabled by:: 1182983afe33SPhil Shafer 1183983afe33SPhil Shafer % env LIBXO_OPTIONS=warn my-app 1184983afe33SPhil Shafer 1185983afe33SPhil ShaferSince environment variables are inherited, child processes will have 1186983afe33SPhil Shaferthe same options, which may be undesirable, making the use of the 1187983afe33SPhil Shafer"`--libxo`" command-line option preferable in most situations. 1188983afe33SPhil Shafer 1189983afe33SPhil Shafer.. index:: xo_warn 1190983afe33SPhil Shafer.. index:: xo_err 1191983afe33SPhil Shafer.. index:: xo_errx 1192983afe33SPhil Shafer.. index:: xo_message 1193983afe33SPhil Shafer 1194983afe33SPhil ShaferErrors, Warnings, and Messages 1195983afe33SPhil Shafer~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1196983afe33SPhil Shafer 1197983afe33SPhil ShaferMany programs make use of the standard library functions 1198983afe33SPhil Shafer:manpage:`err(3)` and :manpage:`warn(3)` to generate errors and 1199983afe33SPhil Shaferwarnings for the user. libxo wants to pass that information via the 1200983afe33SPhil Shafercurrent output style, and provides compatible functions to allow 1201983afe33SPhil Shaferthis:: 1202983afe33SPhil Shafer 1203983afe33SPhil Shafer void xo_warn (const char *fmt, ...); 1204983afe33SPhil Shafer void xo_warnx (const char *fmt, ...); 1205983afe33SPhil Shafer void xo_warn_c (int code, const char *fmt, ...); 1206983afe33SPhil Shafer void xo_warn_hc (xo_handle_t *xop, int code, 1207983afe33SPhil Shafer const char *fmt, ...); 1208983afe33SPhil Shafer void xo_err (int eval, const char *fmt, ...); 1209983afe33SPhil Shafer void xo_errc (int eval, int code, const char *fmt, ...); 1210983afe33SPhil Shafer void xo_errx (int eval, const char *fmt, ...); 1211983afe33SPhil Shafer 1212983afe33SPhil Shafer:: 1213983afe33SPhil Shafer 1214983afe33SPhil Shafer void xo_message (const char *fmt, ...); 1215983afe33SPhil Shafer void xo_message_c (int code, const char *fmt, ...); 1216983afe33SPhil Shafer void xo_message_hc (xo_handle_t *xop, int code, 1217983afe33SPhil Shafer const char *fmt, ...); 1218983afe33SPhil Shafer void xo_message_hcv (xo_handle_t *xop, int code, 1219983afe33SPhil Shafer const char *fmt, va_list vap); 1220983afe33SPhil Shafer 1221983afe33SPhil ShaferThese functions display the program name, a colon, a formatted message 1222983afe33SPhil Shaferbased on the arguments, and then optionally a colon and an error 1223983afe33SPhil Shafermessage associated with either *errno* or the *code* parameter:: 1224983afe33SPhil Shafer 1225983afe33SPhil Shafer EXAMPLE: 1226983afe33SPhil Shafer if (open(filename, O_RDONLY) < 0) 1227983afe33SPhil Shafer xo_err(1, "cannot open file '%s'", filename); 1228983afe33SPhil Shafer 1229983afe33SPhil Shafer.. index:: xo_error 12305c5819b2SPhil Shafer.. index:: xo_error_h 12315c5819b2SPhil Shafer.. index:: xo_error_hv 12325c5819b2SPhil Shafer.. index:: xo_errorn 12335c5819b2SPhil Shafer.. index:: xo_errorn_h 12345c5819b2SPhil Shafer.. index:: xo_errorn_hv 1235983afe33SPhil Shafer 1236983afe33SPhil Shaferxo_error 1237983afe33SPhil Shafer~~~~~~~~ 1238983afe33SPhil Shafer 1239983afe33SPhil Shafer.. c:function:: void xo_error (const char *fmt, ...) 1240983afe33SPhil Shafer 1241983afe33SPhil Shafer :param fmt: Format string 1242983afe33SPhil Shafer :type fmt: const char * 1243983afe33SPhil Shafer :returns: void 1244983afe33SPhil Shafer 12455c5819b2SPhil Shafer.. c:function:: void xo_error_h (xo_handle_t *xop, const char *fmt, ...) 12465c5819b2SPhil Shafer 12475c5819b2SPhil Shafer :param xop: libxo handle pointer 12485c5819b2SPhil Shafer :type xop: xo_handle_t * 12495c5819b2SPhil Shafer :param fmt: Format string 12505c5819b2SPhil Shafer :type fmt: const char * 12515c5819b2SPhil Shafer :returns: void 12525c5819b2SPhil Shafer 12535c5819b2SPhil Shafer.. c:function:: void xo_error_hv (xo_handle_t *xop, const char *fmt, va_list vap) 12545c5819b2SPhil Shafer 12555c5819b2SPhil Shafer :param xop: libxo handle pointer 12565c5819b2SPhil Shafer :type xop: xo_handle_t * 12575c5819b2SPhil Shafer :param fmt: Format string 12585c5819b2SPhil Shafer :type fmt: const char * 12595c5819b2SPhil Shafer :param vap: variadic arguments 12605c5819b2SPhil Shafer :type xop: va_list 12615c5819b2SPhil Shafer :returns: void 12625c5819b2SPhil Shafer 12635c5819b2SPhil Shafer.. c:function:: void xo_errorn (const char *fmt, ...) 12645c5819b2SPhil Shafer 12655c5819b2SPhil Shafer :param fmt: Format string 12665c5819b2SPhil Shafer :type fmt: const char * 12675c5819b2SPhil Shafer :returns: void 12685c5819b2SPhil Shafer 12695c5819b2SPhil Shafer.. c:function:: void xo_errorn_h (xo_handle_t *xop, const char *fmt, ...) 12705c5819b2SPhil Shafer 12715c5819b2SPhil Shafer :param xop: libxo handle pointer 12725c5819b2SPhil Shafer :type xop: xo_handle_t * 12735c5819b2SPhil Shafer :param fmt: Format string 12745c5819b2SPhil Shafer :type fmt: const char * 12755c5819b2SPhil Shafer :returns: void 12765c5819b2SPhil Shafer 12775c5819b2SPhil Shafer.. c:function:: void xo_errorn_hv (xo_handle_t *xop, int need_newline, const char *fmt, va_list vap) 12785c5819b2SPhil Shafer 12795c5819b2SPhil Shafer :param xop: libxo handle pointer 12805c5819b2SPhil Shafer :type xop: xo_handle_t * 12815c5819b2SPhil Shafer :param need_newline: boolean indicating need for trailing newline 12825c5819b2SPhil Shafer :type need_newline: int 12835c5819b2SPhil Shafer :param fmt: Format string 12845c5819b2SPhil Shafer :type fmt: const char * 12855c5819b2SPhil Shafer :param vap: variadic arguments 12865c5819b2SPhil Shafer :type xop: va_list 12875c5819b2SPhil Shafer :returns: void 12885c5819b2SPhil Shafer 1289983afe33SPhil Shafer The `xo_error` function can be used for generic errors that should 1290983afe33SPhil Shafer be reported over the handle, rather than to stderr. The `xo_error` 1291983afe33SPhil Shafer function behaves like `xo_err` for TEXT and HTML output styles, but 1292983afe33SPhil Shafer puts the error into XML or JSON elements:: 1293983afe33SPhil Shafer 1294983afe33SPhil Shafer EXAMPLE:: 1295983afe33SPhil Shafer xo_error("Does not %s", "compute"); 1296983afe33SPhil Shafer XML:: 1297983afe33SPhil Shafer <error><message>Does not compute</message></error> 1298983afe33SPhil Shafer JSON:: 1299983afe33SPhil Shafer "error": { "message": "Does not compute" } 1300983afe33SPhil Shafer 13015c5819b2SPhil Shafer The `xo_error_h` and `xo_error_hv` add a handle object and a 13025c5819b2SPhil Shafer variadic-ized parameter to the signature, respectively. 13035c5819b2SPhil Shafer 13045c5819b2SPhil Shafer The `xo_errorn` function supplies a newline at the end the error 13055c5819b2SPhil Shafer message if the format string does not include one. The 13065c5819b2SPhil Shafer `xo_errorn_h` and `xo_errorn_hv` functions add a handle object and 13075c5819b2SPhil Shafer a variadic-ized parameter to the signature, respectively. The 13085c5819b2SPhil Shafer `xo_errorn_hv` function also adds a boolean to indicate the need for 13095c5819b2SPhil Shafer a trailing newline. 13105c5819b2SPhil Shafer 1311983afe33SPhil Shafer.. index:: xo_no_setlocale 1312983afe33SPhil Shafer.. index:: Locale 1313983afe33SPhil Shafer 1314983afe33SPhil Shaferxo_no_setlocale 1315983afe33SPhil Shafer~~~~~~~~~~~~~~~ 1316983afe33SPhil Shafer 1317983afe33SPhil Shafer.. c:function:: void xo_no_setlocale (void) 1318983afe33SPhil Shafer 1319983afe33SPhil Shafer libxo automatically initializes the locale based on setting of the 1320983afe33SPhil Shafer environment variables LC_CTYPE, LANG, and LC_ALL. The first of this 1321983afe33SPhil Shafer list of variables is used and if none of the variables, the locale 1322983afe33SPhil Shafer defaults to "UTF-8". The caller may wish to avoid this behavior, 1323983afe33SPhil Shafer and can do so by calling the `xo_no_setlocale` function. 1324983afe33SPhil Shafer 1325983afe33SPhil ShaferEmitting syslog Messages 1326983afe33SPhil Shafer------------------------ 1327983afe33SPhil Shafer 1328983afe33SPhil Shafersyslog is the system logging facility used throughout the unix world. 1329983afe33SPhil ShaferMessages are sent from commands, applications, and daemons to a 1330983afe33SPhil Shaferhierarchy of servers, where they are filtered, saved, and forwarded 1331983afe33SPhil Shaferbased on configuration behaviors. 1332983afe33SPhil Shafer 1333983afe33SPhil Shafersyslog is an older protocol, originally documented only in source 1334983afe33SPhil Shafercode. By the time :RFC:`3164` published, variation and mutation left the 1335983afe33SPhil Shaferleading "<pri>" string as only common content. :RFC:`5424` defines a new 1336983afe33SPhil Shaferversion (version 1) of syslog and introduces structured data into the 1337983afe33SPhil Shafermessages. Structured data is a set of name/value pairs transmitted 1338983afe33SPhil Shaferdistinctly alongside the traditional text message, allowing filtering 1339983afe33SPhil Shaferon precise values instead of regular expressions. 1340983afe33SPhil Shafer 1341983afe33SPhil ShaferThese name/value pairs are scoped by a two-part identifier; an 1342983afe33SPhil Shaferenterprise identifier names the party responsible for the message 1343983afe33SPhil Shafercatalog and a name identifying that message. `Enterprise IDs`_ are 1344983afe33SPhil Shaferdefined by IANA, the Internet Assigned Numbers Authority. 1345983afe33SPhil Shafer 1346983afe33SPhil Shafer.. _Enterprise IDs: 1347983afe33SPhil Shafer https://www.iana.org/assignments/enterprise-numbers/enterprise-numbers 1348983afe33SPhil Shafer 1349983afe33SPhil ShaferUse the `xo_set_syslog_enterprise_id` function to set the Enterprise 1350983afe33SPhil ShaferID, as needed. 1351983afe33SPhil Shafer 1352983afe33SPhil ShaferThe message name should follow the conventions in 1353983afe33SPhil Shafer:ref:`good-field-names`\ , as should the fields within the message:: 1354983afe33SPhil Shafer 1355983afe33SPhil Shafer /* Both of these calls are optional */ 1356983afe33SPhil Shafer xo_set_syslog_enterprise_id(32473); 1357983afe33SPhil Shafer xo_open_log("my-program", 0, LOG_DAEMON); 1358983afe33SPhil Shafer 1359983afe33SPhil Shafer /* Generate a syslog message */ 1360983afe33SPhil Shafer xo_syslog(LOG_ERR, "upload-failed", 1361983afe33SPhil Shafer "error <%d> uploading file '{:filename}' " 1362983afe33SPhil Shafer "as '{:target/%s:%s}'", 1363983afe33SPhil Shafer code, filename, protocol, remote); 1364983afe33SPhil Shafer 1365983afe33SPhil Shafer xo_syslog(LOG_INFO, "poofd-invalid-state", 1366983afe33SPhil Shafer "state {:current/%u} is invalid {:connection/%u}", 1367983afe33SPhil Shafer state, conn); 1368983afe33SPhil Shafer 1369983afe33SPhil ShaferThe developer should be aware that the message name may be used in the 1370983afe33SPhil Shaferfuture to allow access to further information, including 1371983afe33SPhil Shaferdocumentation. Care should be taken to choose quality, descriptive 1372983afe33SPhil Shafernames. 1373983afe33SPhil Shafer 1374983afe33SPhil Shafer.. _syslog-details: 1375983afe33SPhil Shafer 1376983afe33SPhil ShaferPriority, Facility, and Flags 1377983afe33SPhil Shafer~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1378983afe33SPhil Shafer 1379983afe33SPhil ShaferThe `xo_syslog`, `xo_vsyslog`, and `xo_open_log` functions 1380983afe33SPhil Shaferaccept a set of flags which provide the priority of the message, the 1381983afe33SPhil Shafersource facility, and some additional features. These values are OR'd 1382983afe33SPhil Shafertogether to create a single integer argument:: 1383983afe33SPhil Shafer 1384983afe33SPhil Shafer xo_syslog(LOG_ERR | LOG_AUTH, "login-failed", 1385983afe33SPhil Shafer "Login failed; user '{:user}' from host '{:address}'", 1386983afe33SPhil Shafer user, addr); 1387983afe33SPhil Shafer 1388983afe33SPhil ShaferThese values are defined in <syslog.h>. 1389983afe33SPhil Shafer 1390983afe33SPhil ShaferThe priority value indicates the importance and potential impact of 1391983afe33SPhil Shafereach message: 1392983afe33SPhil Shafer 1393983afe33SPhil Shafer ============= ======================================================= 1394983afe33SPhil Shafer Priority Description 1395983afe33SPhil Shafer ============= ======================================================= 1396983afe33SPhil Shafer LOG_EMERG A panic condition, normally broadcast to all users 1397983afe33SPhil Shafer LOG_ALERT A condition that should be corrected immediately 1398983afe33SPhil Shafer LOG_CRIT Critical conditions 1399983afe33SPhil Shafer LOG_ERR Generic errors 1400983afe33SPhil Shafer LOG_WARNING Warning messages 1401983afe33SPhil Shafer LOG_NOTICE Non-error conditions that might need special handling 1402983afe33SPhil Shafer LOG_INFO Informational messages 1403983afe33SPhil Shafer LOG_DEBUG Developer-oriented messages 1404983afe33SPhil Shafer ============= ======================================================= 1405983afe33SPhil Shafer 1406983afe33SPhil ShaferThe facility value indicates the source of message, in fairly generic 1407983afe33SPhil Shaferterms: 1408983afe33SPhil Shafer 1409983afe33SPhil Shafer =============== ======================================================= 1410983afe33SPhil Shafer Facility Description 1411983afe33SPhil Shafer =============== ======================================================= 1412983afe33SPhil Shafer LOG_AUTH The authorization system (e.g. :manpage:`login(1)`) 1413983afe33SPhil Shafer LOG_AUTHPRIV As LOG_AUTH, but logged to a privileged file 1414983afe33SPhil Shafer LOG_CRON The cron daemon: :manpage:`cron(8)` 1415983afe33SPhil Shafer LOG_DAEMON System daemons, not otherwise explicitly listed 1416983afe33SPhil Shafer LOG_FTP The file transfer protocol daemons 1417983afe33SPhil Shafer LOG_KERN Messages generated by the kernel 1418983afe33SPhil Shafer LOG_LPR The line printer spooling system 1419983afe33SPhil Shafer LOG_MAIL The mail system 1420983afe33SPhil Shafer LOG_NEWS The network news system 1421983afe33SPhil Shafer LOG_SECURITY Security subsystems, such as :manpage:`ipfw(4)` 1422983afe33SPhil Shafer LOG_SYSLOG Messages generated internally by :manpage:`syslogd(8)` 1423983afe33SPhil Shafer LOG_USER Messages generated by user processes (default) 1424983afe33SPhil Shafer LOG_UUCP The uucp system 1425983afe33SPhil Shafer LOG_LOCAL0..7 Reserved for local use 1426983afe33SPhil Shafer =============== ======================================================= 1427983afe33SPhil Shafer 1428983afe33SPhil ShaferIn addition to the values listed above, xo_open_log accepts a set of 1429983afe33SPhil Shaferaddition flags requesting specific logging behaviors: 1430983afe33SPhil Shafer 1431983afe33SPhil Shafer ============ ==================================================== 1432983afe33SPhil Shafer Flag Description 1433983afe33SPhil Shafer ============ ==================================================== 1434983afe33SPhil Shafer LOG_CONS If syslogd fails, attempt to write to /dev/console 1435983afe33SPhil Shafer LOG_NDELAY Open the connection to :manpage:`syslogd(8)` immediately 1436983afe33SPhil Shafer LOG_PERROR Write the message also to standard error output 1437983afe33SPhil Shafer LOG_PID Log the process id with each message 1438983afe33SPhil Shafer ============ ==================================================== 1439983afe33SPhil Shafer 1440983afe33SPhil Shafer.. index:: xo_syslog 1441983afe33SPhil Shafer 1442983afe33SPhil Shaferxo_syslog 1443983afe33SPhil Shafer~~~~~~~~~ 1444983afe33SPhil Shafer 1445983afe33SPhil Shafer.. c:function:: void xo_syslog (int pri, const char *name, const char *fmt, ...) 1446983afe33SPhil Shafer 1447983afe33SPhil Shafer :param int pri: syslog priority 1448983afe33SPhil Shafer :param name: Name of the syslog event 1449983afe33SPhil Shafer :type name: const char * 1450983afe33SPhil Shafer :param fmt: Format string, followed by arguments 1451983afe33SPhil Shafer :type fmt: const char * 1452983afe33SPhil Shafer :returns: void 1453983afe33SPhil Shafer 1454983afe33SPhil Shafer Use the `xo_syslog` function to generate syslog messages by calling 1455983afe33SPhil Shafer it with a log priority and facility, a message name, a format 1456983afe33SPhil Shafer string, and a set of arguments. The priority/facility argument are 1457983afe33SPhil Shafer discussed above, as is the message name. 1458983afe33SPhil Shafer 1459983afe33SPhil Shafer The format string follows the same conventions as `xo_emit`'s format 1460983afe33SPhil Shafer string, with each field being rendered as an SD-PARAM pair:: 1461983afe33SPhil Shafer 1462983afe33SPhil Shafer xo_syslog(LOG_ERR, "poofd-missing-file", 1463983afe33SPhil Shafer "'{:filename}' not found: {:error/%m}", filename); 1464983afe33SPhil Shafer 1465983afe33SPhil Shafer ... [poofd-missing-file@32473 filename="/etc/poofd.conf" 1466983afe33SPhil Shafer error="Permission denied"] '/etc/poofd.conf' not 1467983afe33SPhil Shafer found: Permission denied 1468983afe33SPhil Shafer 1469983afe33SPhil ShaferSupport functions 1470983afe33SPhil Shafer~~~~~~~~~~~~~~~~~ 1471983afe33SPhil Shafer 1472983afe33SPhil Shafer.. index:: xo_vsyslog 1473983afe33SPhil Shafer 1474983afe33SPhil Shaferxo_vsyslog 1475983afe33SPhil Shafer++++++++++ 1476983afe33SPhil Shafer 1477983afe33SPhil Shafer.. c:function:: void xo_vsyslog (int pri, const char *name, const char *fmt, va_list vap) 1478983afe33SPhil Shafer 1479983afe33SPhil Shafer :param int pri: syslog priority 1480983afe33SPhil Shafer :param name: Name of the syslog event 1481983afe33SPhil Shafer :type name: const char * 1482983afe33SPhil Shafer :param fmt: Format string 1483983afe33SPhil Shafer :type fmt: const char * 1484983afe33SPhil Shafer :param va_list vap: Variadic argument list 1485983afe33SPhil Shafer :returns: void 1486983afe33SPhil Shafer 1487983afe33SPhil Shafer xo_vsyslog is identical in function to xo_syslog, but takes the set of 1488983afe33SPhil Shafer arguments using a va_list:: 1489983afe33SPhil Shafer 1490983afe33SPhil Shafer EXAMPLE: 1491983afe33SPhil Shafer void 1492983afe33SPhil Shafer my_log (const char *name, const char *fmt, ...) 1493983afe33SPhil Shafer { 1494983afe33SPhil Shafer va_list vap; 1495983afe33SPhil Shafer va_start(vap, fmt); 1496983afe33SPhil Shafer xo_vsyslog(LOG_ERR, name, fmt, vap); 1497983afe33SPhil Shafer va_end(vap); 1498983afe33SPhil Shafer } 1499983afe33SPhil Shafer 1500983afe33SPhil Shafer.. index:: xo_open_log 1501983afe33SPhil Shafer 1502983afe33SPhil Shaferxo_open_log 1503983afe33SPhil Shafer+++++++++++ 1504983afe33SPhil Shafer 1505983afe33SPhil Shafer.. c:function:: void xo_open_log (const char *ident, int logopt, int facility) 1506983afe33SPhil Shafer 1507983afe33SPhil Shafer :param indent: 1508983afe33SPhil Shafer :type indent: const char * 1509983afe33SPhil Shafer :param int logopt: Bit field containing logging options 1510983afe33SPhil Shafer :param int facility: 1511983afe33SPhil Shafer :returns: void 1512983afe33SPhil Shafer 1513983afe33SPhil Shafer xo_open_log functions similar to :manpage:`openlog(3)`, allowing 1514983afe33SPhil Shafer customization of the program name, the log facility number, and the 1515983afe33SPhil Shafer additional option flags described in :ref:`syslog-details`. 1516983afe33SPhil Shafer 1517983afe33SPhil Shafer.. index:: xo_close_log 1518983afe33SPhil Shafer 1519983afe33SPhil Shaferxo_close_log 1520983afe33SPhil Shafer++++++++++++ 1521983afe33SPhil Shafer 1522983afe33SPhil Shafer.. c:function:: void xo_close_log (void) 1523983afe33SPhil Shafer 1524983afe33SPhil Shafer The `xo_close_log` function is similar to :manpage:`closelog(3)`, 1525983afe33SPhil Shafer closing the log file and releasing any associated resources. 1526983afe33SPhil Shafer 1527983afe33SPhil Shafer.. index:: xo_set_logmask 1528983afe33SPhil Shafer 1529983afe33SPhil Shaferxo_set_logmask 1530983afe33SPhil Shafer++++++++++++++ 1531983afe33SPhil Shafer 1532983afe33SPhil Shafer.. c:function:: int xo_set_logmask (int maskpri) 1533983afe33SPhil Shafer 1534983afe33SPhil Shafer :param int maskpri: the log priority mask 1535983afe33SPhil Shafer :returns: The previous log priority mask 1536983afe33SPhil Shafer 1537983afe33SPhil Shafer The `xo_set_logmask` function is similar to :manpage:`setlogmask(3)`, 1538983afe33SPhil Shafer restricting the set of generated log event to those whose associated 1539983afe33SPhil Shafer bit is set in maskpri. Use `LOG_MASK(pri)` to find the appropriate bit, 1540983afe33SPhil Shafer or `LOG_UPTO(toppri)` to create a mask for all priorities up to and 1541983afe33SPhil Shafer including toppri:: 1542983afe33SPhil Shafer 1543983afe33SPhil Shafer EXAMPLE: 1544983afe33SPhil Shafer setlogmask(LOG_UPTO(LOG_WARN)); 1545983afe33SPhil Shafer 1546983afe33SPhil Shafer.. index:: xo_set_syslog_enterprise_id 1547983afe33SPhil Shafer 1548983afe33SPhil Shaferxo_set_syslog_enterprise_id 1549983afe33SPhil Shafer+++++++++++++++++++++++++++ 1550983afe33SPhil Shafer 1551983afe33SPhil Shafer.. c:function:: void xo_set_syslog_enterprise_id (unsigned short eid) 1552983afe33SPhil Shafer 1553983afe33SPhil Shafer Use the `xo_set_syslog_enterprise_id` to supply a platform- or 1554983afe33SPhil Shafer application-specific enterprise id. This value is used in any future 1555983afe33SPhil Shafer syslog messages. 1556983afe33SPhil Shafer 1557983afe33SPhil Shafer Ideally, the operating system should supply a default value via the 1558983afe33SPhil Shafer "kern.syslog.enterprise_id" sysctl value. Lacking that, the 1559983afe33SPhil Shafer application should provide a suitable value. 1560983afe33SPhil Shafer 1561983afe33SPhil ShaferEnterprise IDs are administered by IANA, the Internet Assigned Number 1562983afe33SPhil ShaferAuthority. The complete list is EIDs on their web site:: 1563983afe33SPhil Shafer 1564983afe33SPhil Shafer https://www.iana.org/assignments/enterprise-numbers/enterprise-numbers 1565983afe33SPhil Shafer 1566983afe33SPhil ShaferNew EIDs can be requested from IANA using the following page:: 1567983afe33SPhil Shafer 1568983afe33SPhil Shafer http://pen.iana.org/pen/PenApplication.page 1569983afe33SPhil Shafer 1570983afe33SPhil ShaferEach software development organization that defines a set of syslog 1571983afe33SPhil Shafermessages should register their own EID and use that value in their 1572983afe33SPhil Shafersoftware to ensure that messages can be uniquely identified by the 1573983afe33SPhil Shafercombination of EID + message name. 1574983afe33SPhil Shafer 1575983afe33SPhil ShaferCreating Custom Encoders 1576983afe33SPhil Shafer------------------------ 1577983afe33SPhil Shafer 1578983afe33SPhil ShaferThe number of encoding schemes in current use is staggering, with new 1579983afe33SPhil Shaferand distinct schemes appearing daily. While libxo provide XML, JSON, 1580983afe33SPhil ShaferHMTL, and text natively, there are requirements for other encodings. 1581983afe33SPhil Shafer 1582983afe33SPhil ShaferRather than bake support for all possible encoders into libxo, the API 1583983afe33SPhil Shaferallows them to be defined externally. libxo can then interfaces with 1584983afe33SPhil Shaferthese encoding modules using a simplistic API. libxo processes all 1585983afe33SPhil Shaferfunctions calls, handles state transitions, performs all formatting, 1586983afe33SPhil Shaferand then passes the results as operations to a customized encoding 1587983afe33SPhil Shaferfunction, which implements specific encoding logic as required. This 1588983afe33SPhil Shafermeans your encoder doesn't need to detect errors with unbalanced 1589983afe33SPhil Shaferopen/close operations but can rely on libxo to pass correct data. 1590983afe33SPhil Shafer 1591983afe33SPhil ShaferBy making a simple API, libxo internals are not exposed, insulating the 1592983afe33SPhil Shaferencoder and the library from future or internal changes. 1593983afe33SPhil Shafer 1594983afe33SPhil ShaferThe three elements of the API are: 1595983afe33SPhil Shafer 1596983afe33SPhil Shafer- loading 1597983afe33SPhil Shafer- initialization 1598983afe33SPhil Shafer- operations 1599983afe33SPhil Shafer 1600983afe33SPhil ShaferThe following sections provide details about these topics. 1601983afe33SPhil Shafer 1602983afe33SPhil Shafer.. index:: CBOR 1603983afe33SPhil Shafer 1604983afe33SPhil Shaferlibxo source contains an encoder for Concise Binary Object 1605983afe33SPhil ShaferRepresentation, aka CBOR (:RFC:`7049`), which can be used as an 1606983afe33SPhil Shaferexample for the API for other encoders. 1607983afe33SPhil Shafer 1608983afe33SPhil ShaferLoading Encoders 1609983afe33SPhil Shafer~~~~~~~~~~~~~~~~ 1610983afe33SPhil Shafer 1611983afe33SPhil ShaferEncoders can be registered statically or discovered dynamically. 1612983afe33SPhil ShaferApplications can choose to call the `xo_encoder_register` function 1613983afe33SPhil Shaferto explicitly register encoders, but more typically they are built as 1614983afe33SPhil Shafershared libraries, placed in the libxo/extensions directory, and loaded 1615983afe33SPhil Shaferbased on name. libxo looks for a file with the name of the encoder 1616983afe33SPhil Shaferand an extension of ".enc". This can be a file or a symlink to the 1617983afe33SPhil Shafershared library file that supports the encoder:: 1618983afe33SPhil Shafer 1619983afe33SPhil Shafer % ls -1 lib/libxo/extensions/*.enc 1620983afe33SPhil Shafer lib/libxo/extensions/cbor.enc 1621983afe33SPhil Shafer lib/libxo/extensions/test.enc 1622983afe33SPhil Shafer 1623983afe33SPhil ShaferEncoder Initialization 1624983afe33SPhil Shafer~~~~~~~~~~~~~~~~~~~~~~ 1625983afe33SPhil Shafer 1626983afe33SPhil ShaferEach encoder must export a symbol used to access the library, which 1627983afe33SPhil Shafermust have the following signature:: 1628983afe33SPhil Shafer 1629983afe33SPhil Shafer int xo_encoder_library_init (XO_ENCODER_INIT_ARGS); 1630983afe33SPhil Shafer 1631983afe33SPhil Shafer`XO_ENCODER_INIT_ARGS` is a macro defined in "xo_encoder.h" that defines 1632983afe33SPhil Shaferan argument called "arg", a pointer of the type 1633983afe33SPhil Shafer`xo_encoder_init_args_t`. This structure contains two fields: 1634983afe33SPhil Shafer 1635983afe33SPhil Shafer- `xei_version` is the version number of the API as implemented 1636983afe33SPhil Shafer within libxo. This version is currently as 1 using 1637983afe33SPhil Shafer `XO_ENCODER_VERSION`. This number can be checked to ensure 1638983afe33SPhil Shafer compatibility. The working assumption is that all versions should 1639983afe33SPhil Shafer be backward compatible, but each side may need to accurately know 1640983afe33SPhil Shafer the version supported by the other side. `xo_encoder_library_init` 1641983afe33SPhil Shafer can optionally check this value, and must then set it to the version 1642983afe33SPhil Shafer number used by the encoder, allowing libxo to detect version 1643983afe33SPhil Shafer differences and react accordingly. For example, if version 2 adds 1644983afe33SPhil Shafer new operations, then libxo will know that an encoding library that 1645983afe33SPhil Shafer set `xei_version` to 1 cannot be expected to handle those new 1646983afe33SPhil Shafer operations. 1647983afe33SPhil Shafer 1648983afe33SPhil Shafer- xei_handler must be set to a pointer to a function of type 1649983afe33SPhil Shafer `xo_encoder_func_t`, as defined in "xo_encoder.h". This function 1650983afe33SPhil Shafer takes a set of parameters: 1651983afe33SPhil Shafer - xop is a pointer to the opaque `xo_handle_t` structure 1652983afe33SPhil Shafer - op is an integer representing the current operation 1653983afe33SPhil Shafer - name is a string whose meaning differs by operation 1654983afe33SPhil Shafer - value is a string whose meaning differs by operation 1655983afe33SPhil Shafer - private is an opaque structure provided by the encoder 1656983afe33SPhil Shafer 1657983afe33SPhil ShaferAdditional arguments may be added in the future, so handler functions 1658983afe33SPhil Shafershould use the `XO_ENCODER_HANDLER_ARGS` macro. An appropriate 1659983afe33SPhil Shafer"extern" declaration is provided to help catch errors. 1660983afe33SPhil Shafer 1661983afe33SPhil ShaferOnce the encoder initialization function has completed processing, it 1662983afe33SPhil Shafershould return zero to indicate that no error has occurred. A non-zero 1663983afe33SPhil Shaferreturn code will cause the handle initialization to fail. 1664983afe33SPhil Shafer 1665983afe33SPhil ShaferOperations 1666983afe33SPhil Shafer~~~~~~~~~~ 1667983afe33SPhil Shafer 1668983afe33SPhil ShaferThe encoder API defines a set of operations representing the 1669983afe33SPhil Shaferprocessing model of libxo. Content is formatted within libxo, and 1670983afe33SPhil Shafercallbacks are made to the encoder's handler function when data is 1671983afe33SPhil Shaferready to be processed: 1672983afe33SPhil Shafer 1673983afe33SPhil Shafer ======================= ======================================= 1674983afe33SPhil Shafer Operation Meaning (Base function) 1675983afe33SPhil Shafer ======================= ======================================= 1676983afe33SPhil Shafer XO_OP_CREATE Called when the handle is created 1677983afe33SPhil Shafer XO_OP_OPEN_CONTAINER Container opened (xo_open_container) 1678983afe33SPhil Shafer XO_OP_CLOSE_CONTAINER Container closed (xo_close_container) 1679983afe33SPhil Shafer XO_OP_OPEN_LIST List opened (xo_open_list) 1680983afe33SPhil Shafer XO_OP_CLOSE_LIST List closed (xo_close_list) 1681983afe33SPhil Shafer XO_OP_OPEN_LEAF_LIST Leaf list opened (xo_open_leaf_list) 1682983afe33SPhil Shafer XO_OP_CLOSE_LEAF_LIST Leaf list closed (xo_close_leaf_list) 1683983afe33SPhil Shafer XO_OP_OPEN_INSTANCE Instance opened (xo_open_instance) 1684983afe33SPhil Shafer XO_OP_CLOSE_INSTANCE Instance closed (xo_close_instance) 1685983afe33SPhil Shafer XO_OP_STRING Field with Quoted UTF-8 string 1686983afe33SPhil Shafer XO_OP_CONTENT Field with content 1687983afe33SPhil Shafer XO_OP_FINISH Finish any pending output 1688983afe33SPhil Shafer XO_OP_FLUSH Flush any buffered output 1689983afe33SPhil Shafer XO_OP_DESTROY Clean up resources 1690983afe33SPhil Shafer XO_OP_ATTRIBUTE An attribute name/value pair 1691983afe33SPhil Shafer XO_OP_VERSION A version string 1692983afe33SPhil Shafer ======================= ======================================= 1693983afe33SPhil Shafer 1694983afe33SPhil ShaferFor all the open and close operations, the name parameter holds the 1695983afe33SPhil Shafername of the construct. For string, content, and attribute operations, 1696983afe33SPhil Shaferthe name parameter is the name of the field and the value parameter is 1697983afe33SPhil Shaferthe value. "string" are differentiated from "content" to allow differing 1698983afe33SPhil Shafertreatment of true, false, null, and numbers from real strings, though 1699983afe33SPhil Shafercontent values are formatted as strings before the handler is called. 1700983afe33SPhil ShaferFor version operations, the value parameter contains the version. 1701983afe33SPhil Shafer 1702983afe33SPhil ShaferAll strings are encoded in UTF-8. 1703