2 * Copyright (c) 2014, Juniper Networks, Inc.
4 * This SOFTWARE is licensed under the LICENSE provided in the
5 * ../Copyright file. By downloading, installing, copying, or otherwise
6 * using the SOFTWARE, you agree to be bound by the terms of that
8 * Phil Shafer, July 2014
12 * libxo provides a means of generating text, XML, and JSON output
13 * using a single set of function calls, maximizing the value of output
14 * while minimizing the cost/impact on the code.
20 /** Formatting types */
21 typedef unsigned xo_style_t;
22 #define XO_STYLE_TEXT 0 /** Generate text output */
23 #define XO_STYLE_XML 1 /** Generate XML output */
24 #define XO_STYLE_JSON 2 /** Generate JSON output */
25 #define XO_STYLE_HTML 3 /** Generate HTML output */
27 /** Flags for libxo */
28 typedef unsigned long xo_xof_flags_t;
29 #define XOF_CLOSE_FP (1<<0) /** Close file pointer on xo_close() */
30 #define XOF_PRETTY (1<<1) /** Make 'pretty printed' output */
31 #define XOF_DIV_OPEN (1<<2) /** Internal use only: a <div> is open */
32 #define XOF_LINE_OPEN (1<<3) /** Internal use only: a <div class="line"> */
34 #define XOF_WARN (1<<4) /** Generate warnings for broken calls */
35 #define XOF_XPATH (1<<5) /** Emit XPath attributes in HTML */
36 #define XOF_INFO (1<<6) /** Emit additional info fields (HTML) */
37 #define XOF_WARN_XML (1<<7) /** Emit warnings in XML (on stdout) */
39 #define XOF_NO_ENV (1<<8) /** Don't look at the LIBXO_OPTIONS env var */
40 #define XOF_NO_VA_ARG (1<<9) /** Don't advance va_list w/ va_arg() */
41 #define XOF_DTRT (1<<10) /** Enable "do the right thing" mode */
42 #define XOF_KEYS (1<<11) /** Flag 'key' fields for xml and json */
44 #define XOF_IGNORE_CLOSE (1<<12) /** Ignore errors on close tags */
45 #define XOF_NOT_FIRST (1<<13) /* Not the first item (JSON) */
46 #define XOF_NO_LOCALE (1<<14) /** Don't bother with locale */
47 #define XOF_TOP_EMITTED (1<<15) /* The top JSON braces have been emitted */
49 #define XOF_NO_TOP (1<<16) /** Don't emit the top braces in JSON */
50 #define XOF_ANCHOR (1<<17) /** An anchor is in place */
51 #define XOF_UNITS (1<<18) /** Encode units in XML */
52 #define XOF_UNITS_PENDING (1<<19) /** We have a units-insertion pending */
54 #define XOF_UNDERSCORES (1<<20) /** Replace dashes with underscores (JSON) */
55 #define XOF_COLUMNS (1<<21) /** xo_emit should return a column count */
56 #define XOF_FLUSH (1<<22) /** Flush after each xo_emit call */
59 * The xo_info_t structure provides a mapping between names and
60 * additional data emitted via HTML.
62 typedef struct xo_info_s {
63 const char *xi_name; /* Name of the element */
64 const char *xi_type; /* Type of field */
65 const char *xi_help; /* Description of field */
68 struct xo_handle_s; /* Opaque structure forward */
69 typedef struct xo_handle_s xo_handle_t; /* Handle for XO output */
71 typedef int (*xo_write_func_t)(void *, const char *);
72 typedef void (*xo_close_func_t)(void *);
73 typedef void *(*xo_realloc_func_t)(void *, size_t);
74 typedef void (*xo_free_func_t)(void *);
77 * The formatter function mirrors "vsnprintf", with an additional argument
78 * of the xo handle. The caller should return the number of bytes _needed_
79 * to fit the data, even if this exceeds 'len'.
81 typedef int (*xo_formatter_t)(xo_handle_t *, char *, int,
82 const char *, va_list);
83 typedef void (*xo_checkpointer_t)(xo_handle_t *, va_list, int);
86 xo_create (xo_style_t style, xo_xof_flags_t flags);
89 xo_create_to_file (FILE *fp, xo_style_t style, xo_xof_flags_t flags);
92 xo_destroy (xo_handle_t *xop);
95 xo_set_writer (xo_handle_t *xop, void *opaque, xo_write_func_t write_func,
96 xo_close_func_t close_func);
99 xo_set_allocator (xo_realloc_func_t realloc_func, xo_free_func_t free_func);
102 xo_set_style (xo_handle_t *xop, xo_style_t style);
105 xo_get_style (xo_handle_t *xop);
108 xo_set_style_name (xo_handle_t *xop, const char *style);
111 xo_set_options (xo_handle_t *xop, const char *input);
114 xo_get_flags (xo_handle_t *xop);
117 xo_set_flags (xo_handle_t *xop, xo_xof_flags_t flags);
120 xo_clear_flags (xo_handle_t *xop, xo_xof_flags_t flags);
123 xo_set_info (xo_handle_t *xop, xo_info_t *infop, int count);
126 xo_set_formatter (xo_handle_t *xop, xo_formatter_t func, xo_checkpointer_t);
129 xo_set_depth (xo_handle_t *xop, int depth);
132 xo_emit_hv (xo_handle_t *xop, const char *fmt, va_list vap);
135 xo_emit_h (xo_handle_t *xop, const char *fmt, ...);
138 xo_emit (const char *fmt, ...);
141 xo_open_container_h (xo_handle_t *xop, const char *name);
144 xo_open_container (const char *name);
147 xo_open_container_hd (xo_handle_t *xop, const char *name);
150 xo_open_container_d (const char *name);
153 xo_close_container_h (xo_handle_t *xop, const char *name);
156 xo_close_container (const char *name);
159 xo_close_container_hd (xo_handle_t *xop);
162 xo_close_container_d (void);
165 xo_open_list_h (xo_handle_t *xop, const char *name);
168 xo_open_list (const char *name);
171 xo_open_list_hd (xo_handle_t *xop, const char *name);
174 xo_open_list_d (const char *name);
177 xo_close_list_h (xo_handle_t *xop, const char *name);
180 xo_close_list (const char *name);
183 xo_close_list_hd (xo_handle_t *xop);
186 xo_close_list_d (void);
189 xo_open_instance_h (xo_handle_t *xop, const char *name);
192 xo_open_instance (const char *name);
195 xo_open_instance_hd (xo_handle_t *xop, const char *name);
198 xo_open_instance_d (const char *name);
201 xo_close_instance_h (xo_handle_t *xop, const char *name);
204 xo_close_instance (const char *name);
207 xo_close_instance_hd (xo_handle_t *xop);
210 xo_close_instance_d (void);
213 xo_attr_h (xo_handle_t *xop, const char *name, const char *fmt, ...);
216 xo_attr_hv (xo_handle_t *xop, const char *name, const char *fmt, va_list vap);
219 xo_attr (const char *name, const char *fmt, ...);
222 xo_error_hv (xo_handle_t *xop, const char *fmt, va_list vap);
225 xo_error_h (xo_handle_t *xop, const char *fmt, ...);
228 xo_error (const char *fmt, ...);
231 xo_flush_h (xo_handle_t *xop);
237 xo_finish_h (xo_handle_t *xop);
243 xo_set_leading_xpath (xo_handle_t *xop, const char *path);
246 xo_warn_hc (xo_handle_t *xop, int code, const char *fmt, ...);
249 xo_warn_c (int code, const char *fmt, ...);
252 xo_warn (const char *fmt, ...);
255 xo_warnx (const char *fmt, ...);
258 xo_err (int eval, const char *fmt, ...);
261 xo_errx (int eval, const char *fmt, ...);
264 xo_errc (int eval, int code, const char *fmt, ...);
267 xo_message_hcv (xo_handle_t *xop, int code, const char *fmt, va_list vap);
270 xo_message_hc (xo_handle_t *xop, int code, const char *fmt, ...);
273 xo_message_c (int code, const char *fmt, ...);
276 xo_message (const char *fmt, ...);
279 xo_no_setlocale (void);
282 xo_parse_args (int argc, char **argv);
285 * This is the "magic" number returned by libxo-supporting commands
286 * when passed the equally magic "--libxo-check" option. If you
287 * return this, we can assume that since you know the magic handshake,
288 * you'll happily handle future --libxo options and not do something
289 * violent like reboot the box or create another hole in the ozone
292 #define XO_HAS_LIBXO 121
295 * externs for our version number strings
297 extern const char xo_version[];
298 extern const char xo_version_extra[];
300 #endif /* INCLUDE_XO_H */