2 * Copyright (c) 2014-2015, 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
10 * This is the implementation of libxo, the formatting library that
11 * generates multiple styles of output from a single code path.
12 * Command line utilities can have their normal text output while
13 * automation tools can see XML or JSON output, and web tools can use
14 * HTML output that encodes the text output annotated with additional
15 * information. Specialized encoders can be built that allow custom
16 * encoding including binary ones like CBOR, thrift, protobufs, etc.
18 * Full documentation is available in ./doc/libxo.txt or online at:
19 * http://juniper.github.io/libxo/libxo-manual.html
21 * For first time readers, the core bits of code to start looking at are:
22 * - xo_do_emit() -- parse and emit a set of fields
23 * - xo_do_emit_fields -- the central function of the library
24 * - xo_do_format_field() -- handles formatting a single field
25 * - xo_transiton() -- the state machine that keeps things sane
26 * and of course the "xo_handle_t" data structure, which carries all
27 * configuration and state.
37 #include <sys/types.h>
46 #include "xo_config.h"
48 #include "xo_encoder.h"
52 * We ask wcwidth() to do an impossible job, really. It's supposed to
53 * need to tell us the number of columns consumed to display a unicode
54 * character. It returns that number without any sort of context, but
55 * we know they are characters whose glyph differs based on placement
56 * (end of word, middle of word, etc) and many that affect characters
57 * previously emitted. Without content, it can't hope to tell us.
58 * But it's the only standard tool we've got, so we use it. We would
59 * use wcswidth() but it typically just loops thru adding the results
60 * of wcwidth() calls in an entirely unhelpful way.
62 * Even then, there are many poor implementations (macosx), so we have
63 * to carry our own. We could have configure.ac test this (with
64 * something like 'assert(wcwidth(0x200d) == 0)'), but it would have
65 * to run a binary, which breaks cross-compilation. Hmm... I could
66 * run this test at init time and make a warning for our dear user.
68 * Anyhow, it remains a best-effort sort of thing. And it's all made
69 * more hopeless because we assume the display code doing the rendering is
70 * playing by the same rules we are. If it display 0x200d as a square
71 * box or a funky question mark, the output will be hosed.
74 #include "xo_wcwidth.h"
75 #else /* LIBXO_WCWIDTH */
76 #define xo_wcwidth(_x) wcwidth(_x)
77 #endif /* LIBXO_WCWIDTH */
79 #ifdef HAVE_STDIO_EXT_H
80 #include <stdio_ext.h>
81 #endif /* HAVE_STDIO_EXT_H */
84 * humanize_number is a great function, unless you don't have it. So
85 * we carry one in our pocket.
87 #ifdef HAVE_HUMANIZE_NUMBER
89 #define xo_humanize_number humanize_number
90 #else /* HAVE_HUMANIZE_NUMBER */
91 #include "xo_humanize.h"
92 #endif /* HAVE_HUMANIZE_NUMBER */
96 #endif /* HAVE_GETTEXT */
99 * Three styles of specifying thread-local variables are supported.
100 * configure.ac has the brains to run each possibility thru the
101 * compiler and see what works; we are left to define the THREAD_LOCAL
102 * macro to the right value. Most toolchains (clang, gcc) use
103 * "before", but some (borland) use "after" and I've heard of some
104 * (ms) that use __declspec. Any others out there?
106 #define THREAD_LOCAL_before 1
107 #define THREAD_LOCAL_after 2
108 #define THREAD_LOCAL_declspec 3
110 #ifndef HAVE_THREAD_LOCAL
111 #define THREAD_LOCAL(_x) _x
112 #elif HAVE_THREAD_LOCAL == THREAD_LOCAL_before
113 #define THREAD_LOCAL(_x) __thread _x
114 #elif HAVE_THREAD_LOCAL == THREAD_LOCAL_after
115 #define THREAD_LOCAL(_x) _x __thread
116 #elif HAVE_THREAD_LOCAL == THREAD_LOCAL_declspec
117 #define THREAD_LOCAL(_x) __declspec(_x)
119 #error unknown thread-local setting
120 #endif /* HAVE_THREADS_H */
122 const char xo_version[] = LIBXO_VERSION;
123 const char xo_version_extra[] = LIBXO_VERSION_EXTRA;
124 static const char xo_default_format[] = "%s";
127 #define UNUSED __attribute__ ((__unused__))
130 #define XO_INDENT_BY 2 /* Amount to indent when pretty printing */
131 #define XO_DEPTH 128 /* Default stack depth */
132 #define XO_MAX_ANCHOR_WIDTH (8*1024) /* Anything wider is just sillyb */
134 #define XO_FAILURE_NAME "failure"
136 /* Flags for the stack frame */
137 typedef unsigned xo_xsf_flags_t; /* XSF_* flags */
138 #define XSF_NOT_FIRST (1<<0) /* Not the first element */
139 #define XSF_LIST (1<<1) /* Frame is a list */
140 #define XSF_INSTANCE (1<<2) /* Frame is an instance */
141 #define XSF_DTRT (1<<3) /* Save the name for DTRT mode */
143 #define XSF_CONTENT (1<<4) /* Some content has been emitted */
144 #define XSF_EMIT (1<<5) /* Some field has been emitted */
145 #define XSF_EMIT_KEY (1<<6) /* A key has been emitted */
146 #define XSF_EMIT_LEAF_LIST (1<<7) /* A leaf-list field has been emitted */
148 /* These are the flags we propagate between markers and their parents */
149 #define XSF_MARKER_FLAGS \
150 (XSF_NOT_FIRST | XSF_CONTENT | XSF_EMIT | XSF_EMIT_KEY | XSF_EMIT_LEAF_LIST )
153 * A word about states: We use a finite state machine (FMS) approach
154 * to help remove fragility from the caller's code. Instead of
155 * requiring a specific order of calls, we'll allow the caller more
156 * flexibility and make the library responsible for recovering from
157 * missed steps. The goal is that the library should not be capable
158 * of emitting invalid xml or json, but the developer shouldn't need
159 * to know or understand all the details about these encodings.
161 * You can think of states as either states or events, since they
162 * function rather like both. None of the XO_CLOSE_* events will
163 * persist as states, since the matching stack frame will be popped.
164 * Same is true of XSS_EMIT, which is an event that asks us to
165 * prep for emitting output fields.
168 /* Stack frame states */
169 typedef unsigned xo_state_t;
170 #define XSS_INIT 0 /* Initial stack state */
171 #define XSS_OPEN_CONTAINER 1
172 #define XSS_CLOSE_CONTAINER 2
173 #define XSS_OPEN_LIST 3
174 #define XSS_CLOSE_LIST 4
175 #define XSS_OPEN_INSTANCE 5
176 #define XSS_CLOSE_INSTANCE 6
177 #define XSS_OPEN_LEAF_LIST 7
178 #define XSS_CLOSE_LEAF_LIST 8
179 #define XSS_DISCARDING 9 /* Discarding data until recovered */
180 #define XSS_MARKER 10 /* xo_open_marker's marker */
181 #define XSS_EMIT 11 /* xo_emit has a leaf field */
182 #define XSS_EMIT_LEAF_LIST 12 /* xo_emit has a leaf-list ({l:}) */
183 #define XSS_FINISH 13 /* xo_finish was called */
187 #define XSS_TRANSITION(_old, _new) ((_old) << 8 | (_new))
190 * xo_stack_t: As we open and close containers and levels, we
191 * create a stack of frames to track them. This is needed for
192 * XOF_WARN and XOF_XPATH.
194 typedef struct xo_stack_s {
195 xo_xsf_flags_t xs_flags; /* Flags for this frame */
196 xo_state_t xs_state; /* State for this stack frame */
197 char *xs_name; /* Name (for XPath value) */
198 char *xs_keys; /* XPath predicate for any key fields */
202 * libxo supports colors and effects, for those who like them.
203 * XO_COL_* ("colors") refers to fancy ansi codes, while X__EFF_*
204 * ("effects") are bits since we need to maintain state.
206 #define XO_COL_DEFAULT 0
207 #define XO_COL_BLACK 1
209 #define XO_COL_GREEN 3
210 #define XO_COL_YELLOW 4
211 #define XO_COL_BLUE 5
212 #define XO_COL_MAGENTA 6
213 #define XO_COL_CYAN 7
214 #define XO_COL_WHITE 8
216 #define XO_NUM_COLORS 9
219 * Yes, there's no blink. We're civilized. We like users. Blink
220 * isn't something one does to someone you like. Friends don't let
221 * friends use blink. On friends. You know what I mean. Blink is
222 * like, well, it's like bursting into show tunes at a funeral. It's
223 * just not done. Not something anyone wants. And on those rare
224 * instances where it might actually be appropriate, it's still wrong,
225 * since it's likely done by the wrong person for the wrong reason.
226 * Just like blink. And if I implemented blink, I'd be like a funeral
227 * director who adds "Would you like us to burst into show tunes?" on
228 * the list of questions asked while making funeral arrangements.
229 * It's formalizing wrongness in the wrong way. And we're just too
230 * civilized to do that. Hhhmph!
232 #define XO_EFF_RESET (1<<0)
233 #define XO_EFF_NORMAL (1<<1)
234 #define XO_EFF_BOLD (1<<2)
235 #define XO_EFF_UNDERLINE (1<<3)
236 #define XO_EFF_INVERSE (1<<4)
238 #define XO_EFF_CLEAR_BITS XO_EFF_RESET /* Reset gets reset, surprisingly */
240 typedef uint8_t xo_effect_t;
241 typedef uint8_t xo_color_t;
242 typedef struct xo_colors_s {
243 xo_effect_t xoc_effects; /* Current effect set */
244 xo_color_t xoc_col_fg; /* Foreground color */
245 xo_color_t xoc_col_bg; /* Background color */
249 * xo_handle_t: this is the principle data structure for libxo.
250 * It's used as a store for state, options, content, and all manor
251 * of other information.
254 xo_xof_flags_t xo_flags; /* Flags (XOF_*) from the user*/
255 xo_xof_flags_t xo_iflags; /* Internal flags (XOIF_*) */
256 xo_style_t xo_style; /* XO_STYLE_* value */
257 unsigned short xo_indent; /* Indent level (if pretty) */
258 unsigned short xo_indent_by; /* Indent amount (tab stop) */
259 xo_write_func_t xo_write; /* Write callback */
260 xo_close_func_t xo_close; /* Close callback */
261 xo_flush_func_t xo_flush; /* Flush callback */
262 xo_formatter_t xo_formatter; /* Custom formating function */
263 xo_checkpointer_t xo_checkpointer; /* Custom formating support function */
264 void *xo_opaque; /* Opaque data for write function */
265 xo_buffer_t xo_data; /* Output data */
266 xo_buffer_t xo_fmt; /* Work area for building format strings */
267 xo_buffer_t xo_attrs; /* Work area for building XML attributes */
268 xo_buffer_t xo_predicate; /* Work area for building XPath predicates */
269 xo_stack_t *xo_stack; /* Stack pointer */
270 int xo_depth; /* Depth of stack */
271 int xo_stack_size; /* Size of the stack */
272 xo_info_t *xo_info; /* Info fields for all elements */
273 int xo_info_count; /* Number of info entries */
274 va_list xo_vap; /* Variable arguments (stdargs) */
275 char *xo_leading_xpath; /* A leading XPath expression */
276 mbstate_t xo_mbstate; /* Multi-byte character conversion state */
277 unsigned xo_anchor_offset; /* Start of anchored text */
278 unsigned xo_anchor_columns; /* Number of columns since the start anchor */
279 int xo_anchor_min_width; /* Desired width of anchored text */
280 unsigned xo_units_offset; /* Start of units insertion point */
281 unsigned xo_columns; /* Columns emitted during this xo_emit call */
282 uint8_t xo_color_map_fg[XO_NUM_COLORS]; /* Foreground color mappings */
283 uint8_t xo_color_map_bg[XO_NUM_COLORS]; /* Background color mappings */
284 xo_colors_t xo_colors; /* Current color and effect values */
285 xo_buffer_t xo_color_buf; /* HTML: buffer of colors and effects */
286 char *xo_version; /* Version string */
287 int xo_errno; /* Saved errno for "%m" */
288 char *xo_gt_domain; /* Gettext domain, suitable for dgettext(3) */
289 xo_encoder_func_t xo_encoder; /* Encoding function */
290 void *xo_private; /* Private data for external encoders */
293 /* Flag operations */
294 #define XOF_BIT_ISSET(_flag, _bit) (((_flag) & (_bit)) ? 1 : 0)
295 #define XOF_BIT_SET(_flag, _bit) do { (_flag) |= (_bit); } while (0)
296 #define XOF_BIT_CLEAR(_flag, _bit) do { (_flag) &= ~(_bit); } while (0)
298 #define XOF_ISSET(_xop, _bit) XOF_BIT_ISSET(_xop->xo_flags, _bit)
299 #define XOF_SET(_xop, _bit) XOF_BIT_SET(_xop->xo_flags, _bit)
300 #define XOF_CLEAR(_xop, _bit) XOF_BIT_CLEAR(_xop->xo_flags, _bit)
302 #define XOIF_ISSET(_xop, _bit) XOF_BIT_ISSET(_xop->xo_iflags, _bit)
303 #define XOIF_SET(_xop, _bit) XOF_BIT_SET(_xop->xo_iflags, _bit)
304 #define XOIF_CLEAR(_xop, _bit) XOF_BIT_CLEAR(_xop->xo_iflags, _bit)
307 #define XOIF_REORDER XOF_BIT(0) /* Reordering fields; record field info */
308 #define XOIF_DIV_OPEN XOF_BIT(1) /* A <div> is open */
309 #define XOIF_TOP_EMITTED XOF_BIT(2) /* The top JSON braces have been emitted */
310 #define XOIF_ANCHOR XOF_BIT(3) /* An anchor is in place */
312 #define XOIF_UNITS_PENDING XOF_BIT(4) /* We have a units-insertion pending */
313 #define XOIF_INIT_IN_PROGRESS XOF_BIT(5) /* Init of handle is in progress */
315 /* Flags for formatting functions */
316 typedef unsigned long xo_xff_flags_t;
317 #define XFF_COLON (1<<0) /* Append a ":" */
318 #define XFF_COMMA (1<<1) /* Append a "," iff there's more output */
319 #define XFF_WS (1<<2) /* Append a blank */
320 #define XFF_ENCODE_ONLY (1<<3) /* Only emit for encoding styles (XML, JSON) */
322 #define XFF_QUOTE (1<<4) /* Force quotes */
323 #define XFF_NOQUOTE (1<<5) /* Force no quotes */
324 #define XFF_DISPLAY_ONLY (1<<6) /* Only emit for display styles (text, html) */
325 #define XFF_KEY (1<<7) /* Field is a key (for XPath) */
327 #define XFF_XML (1<<8) /* Force XML encoding style (for XPath) */
328 #define XFF_ATTR (1<<9) /* Escape value using attribute rules (XML) */
329 #define XFF_BLANK_LINE (1<<10) /* Emit a blank line */
330 #define XFF_NO_OUTPUT (1<<11) /* Do not make any output */
332 #define XFF_TRIM_WS (1<<12) /* Trim whitespace off encoded values */
333 #define XFF_LEAF_LIST (1<<13) /* A leaf-list (list of values) */
334 #define XFF_UNESCAPE (1<<14) /* Need to printf-style unescape the value */
335 #define XFF_HUMANIZE (1<<15) /* Humanize the value (for display styles) */
337 #define XFF_HN_SPACE (1<<16) /* Humanize: put space before suffix */
338 #define XFF_HN_DECIMAL (1<<17) /* Humanize: add one decimal place if <10 */
339 #define XFF_HN_1000 (1<<18) /* Humanize: use 1000, not 1024 */
340 #define XFF_GT_FIELD (1<<19) /* Call gettext() on a field */
342 #define XFF_GT_PLURAL (1<<20) /* Call dngettext to find plural form */
343 #define XFF_ARGUMENT (1<<21) /* Content provided via argument */
345 /* Flags to turn off when we don't want i18n processing */
346 #define XFF_GT_FLAGS (XFF_GT_FIELD | XFF_GT_PLURAL)
349 * Normal printf has width and precision, which for strings operate as
350 * min and max number of columns. But this depends on the idea that
351 * one byte means one column, which UTF-8 and multi-byte characters
352 * pitches on its ear. It may take 40 bytes of data to populate 14
353 * columns, but we can't go off looking at 40 bytes of data without the
354 * caller's permission for fear/knowledge that we'll generate core files.
356 * So we make three values, distinguishing between "max column" and
357 * "number of bytes that we will inspect inspect safely" We call the
358 * later "size", and make the format "%[[<min>].[[<size>].<max>]]s".
360 * Under the "first do no harm" theory, we default "max" to "size".
361 * This is a reasonable assumption for folks that don't grok the
362 * MBS/WCS/UTF-8 world, and while it will be annoying, it will never
365 * For example, xo_emit("{:tag/%-14.14s}", buf) will make 14
366 * columns of output, but will never look at more than 14 bytes of the
367 * input buffer. This is mostly compatible with printf and caller's
370 * In contrast xo_emit("{:tag/%-14..14s}", buf) will look at however
371 * many bytes (or until a NUL is seen) are needed to fill 14 columns
372 * of output. xo_emit("{:tag/%-14.*.14s}", xx, buf) will look at up
373 * to xx bytes (or until a NUL is seen) in order to fill 14 columns
376 * It's fairly amazing how a good idea (handle all languages of the
377 * world) blows such a big hole in the bottom of the fairly weak boat
378 * that is C string handling. The simplicity and completenesss are
379 * sunk in ways we haven't even begun to understand.
381 #define XF_WIDTH_MIN 0 /* Minimal width */
382 #define XF_WIDTH_SIZE 1 /* Maximum number of bytes to examine */
383 #define XF_WIDTH_MAX 2 /* Maximum width */
384 #define XF_WIDTH_NUM 3 /* Numeric fields in printf (min.size.max) */
386 /* Input and output string encodings */
387 #define XF_ENC_WIDE 1 /* Wide characters (wchar_t) */
388 #define XF_ENC_UTF8 2 /* UTF-8 */
389 #define XF_ENC_LOCALE 3 /* Current locale */
392 * A place to parse printf-style format flags for each field
394 typedef struct xo_format_s {
395 unsigned char xf_fc; /* Format character */
396 unsigned char xf_enc; /* Encoding of the string (XF_ENC_*) */
397 unsigned char xf_skip; /* Skip this field */
398 unsigned char xf_lflag; /* 'l' (long) */
399 unsigned char xf_hflag;; /* 'h' (half) */
400 unsigned char xf_jflag; /* 'j' (intmax_t) */
401 unsigned char xf_tflag; /* 't' (ptrdiff_t) */
402 unsigned char xf_zflag; /* 'z' (size_t) */
403 unsigned char xf_qflag; /* 'q' (quad_t) */
404 unsigned char xf_seen_minus; /* Seen a minus */
405 int xf_leading_zero; /* Seen a leading zero (zero fill) */
406 unsigned xf_dots; /* Seen one or more '.'s */
407 int xf_width[XF_WIDTH_NUM]; /* Width/precision/size numeric fields */
408 unsigned xf_stars; /* Seen one or more '*'s */
409 unsigned char xf_star[XF_WIDTH_NUM]; /* Seen one or more '*'s */
413 * This structure represents the parsed field information, suitable for
414 * processing by xo_do_emit and anything else that needs to parse fields.
415 * Note that all pointers point to the main format string.
417 * XXX This is a first step toward compilable or cachable format
418 * strings. We can also cache the results of dgettext when no format
419 * is used, assuming the 'p' modifier has _not_ been set.
421 typedef struct xo_field_info_s {
422 xo_xff_flags_t xfi_flags; /* Flags for this field */
423 unsigned xfi_ftype; /* Field type, as character (e.g. 'V') */
424 const char *xfi_start; /* Start of field in the format string */
425 const char *xfi_content; /* Field's content */
426 const char *xfi_format; /* Field's Format */
427 const char *xfi_encoding; /* Field's encoding format */
428 const char *xfi_next; /* Next character in format string */
429 unsigned xfi_len; /* Length of field */
430 unsigned xfi_clen; /* Content length */
431 unsigned xfi_flen; /* Format length */
432 unsigned xfi_elen; /* Encoding length */
433 unsigned xfi_fnum; /* Field number (if used; 0 otherwise) */
434 unsigned xfi_renum; /* Reordered number (0 == no renumbering) */
438 * We keep a 'default' handle to allow callers to avoid having to
439 * allocate one. Passing NULL to any of our functions will use
440 * this default handle. Most functions have a variant that doesn't
441 * require a handle at all, since most output is to stdout, which
442 * the default handle handles handily.
444 static THREAD_LOCAL(xo_handle_t) xo_default_handle;
445 static THREAD_LOCAL(int) xo_default_inited;
446 static int xo_locale_inited;
447 static const char *xo_program;
450 * To allow libxo to be used in diverse environment, we allow the
451 * caller to give callbacks for memory allocation.
453 xo_realloc_func_t xo_realloc = realloc;
454 xo_free_func_t xo_free = free;
456 /* Forward declarations */
458 xo_failure (xo_handle_t *xop, const char *fmt, ...);
461 xo_transition (xo_handle_t *xop, xo_xsf_flags_t flags, const char *name,
462 xo_state_t new_state);
465 xo_buf_append_div (xo_handle_t *xop, const char *class, xo_xff_flags_t flags,
466 const char *name, int nlen,
467 const char *value, int vlen,
468 const char *encoding, int elen);
471 xo_anchor_clear (xo_handle_t *xop);
474 * xo_style is used to retrieve the current style. When we're built
475 * for "text only" mode, we use this function to drive the removal
476 * of most of the code in libxo. We return a constant and the compiler
477 * happily removes the non-text code that is not longer executed. This
478 * trims our code nicely without needing to trampel perfectly readable
481 static inline xo_style_t
482 xo_style (xo_handle_t *xop UNUSED)
484 #ifdef LIBXO_TEXT_ONLY
485 return XO_STYLE_TEXT;
486 #else /* LIBXO_TEXT_ONLY */
487 return xop->xo_style;
488 #endif /* LIBXO_TEXT_ONLY */
492 * Callback to write data to a FILE pointer
495 xo_write_to_file (void *opaque, const char *data)
497 FILE *fp = (FILE *) opaque;
499 return fprintf(fp, "%s", data);
503 * Callback to close a file
506 xo_close_file (void *opaque)
508 FILE *fp = (FILE *) opaque;
514 * Callback to flush a FILE pointer
517 xo_flush_file (void *opaque)
519 FILE *fp = (FILE *) opaque;
525 * Use a rotating stock of buffers to make a printable string
528 #define XO_SMBUFSZ 128
531 xo_printable (const char *str)
533 static THREAD_LOCAL(char) bufset[XO_NUMBUFS][XO_SMBUFSZ];
534 static THREAD_LOCAL(int) bufnum = 0;
539 if (++bufnum == XO_NUMBUFS)
542 char *res = bufset[bufnum], *cp, *ep;
544 for (cp = res, ep = res + XO_SMBUFSZ - 1; *str && cp < ep; cp++, str++) {
548 } else if (*str == '\r') {
551 } else if (*str == '\"') {
563 xo_depth_check (xo_handle_t *xop, int depth)
567 if (depth >= xop->xo_stack_size) {
568 depth += XO_DEPTH; /* Extra room */
570 xsp = xo_realloc(xop->xo_stack, sizeof(xop->xo_stack[0]) * depth);
572 xo_failure(xop, "xo_depth_check: out of memory (%d)", depth);
576 int count = depth - xop->xo_stack_size;
578 bzero(xsp + xop->xo_stack_size, count * sizeof(*xsp));
579 xop->xo_stack_size = depth;
587 xo_no_setlocale (void)
589 xo_locale_inited = 1; /* Skip initialization */
593 * We need to decide if stdout is line buffered (_IOLBF). Lacking a
594 * standard way to decide this (e.g. getlinebuf()), we have configure
595 * look to find __flbf, which glibc supported. If not, we'll rely on
596 * isatty, with the assumption that terminals are the only thing
597 * that's line buffered. We _could_ test for "steam._flags & _IOLBF",
598 * which is all __flbf does, but that's even tackier. Like a
599 * bedazzled Elvis outfit on an ugly lap dog sort of tacky. Not
600 * something we're willing to do.
603 xo_is_line_buffered (FILE *stream)
608 #else /* HAVE___FLBF */
609 if (isatty(fileno(stream)))
611 #endif /* HAVE___FLBF */
616 * Initialize an xo_handle_t, using both static defaults and
617 * the global settings from the LIBXO_OPTIONS environment
621 xo_init_handle (xo_handle_t *xop)
623 xop->xo_opaque = stdout;
624 xop->xo_write = xo_write_to_file;
625 xop->xo_flush = xo_flush_file;
627 if (xo_is_line_buffered(stdout))
628 XOF_SET(xop, XOF_FLUSH_LINE);
631 * We only want to do color output on terminals, but we only want
632 * to do this if the user has asked for color.
634 if (XOF_ISSET(xop, XOF_COLOR_ALLOWED) && isatty(1))
635 XOF_SET(xop, XOF_COLOR);
638 * We need to initialize the locale, which isn't really pretty.
639 * Libraries should depend on their caller to set up the
640 * environment. But we really can't count on the caller to do
641 * this, because well, they won't. Trust me.
643 if (!xo_locale_inited) {
644 xo_locale_inited = 1; /* Only do this once */
646 const char *cp = getenv("LC_CTYPE");
650 cp = getenv("LC_ALL");
652 cp = "C"; /* Default for C programs */
653 (void) setlocale(LC_CTYPE, cp);
657 * Initialize only the xo_buffers we know we'll need; the others
658 * can be allocated as needed.
660 xo_buf_init(&xop->xo_data);
661 xo_buf_init(&xop->xo_fmt);
663 if (XOIF_ISSET(xop, XOIF_INIT_IN_PROGRESS))
665 XOIF_SET(xop, XOIF_INIT_IN_PROGRESS);
667 xop->xo_indent_by = XO_INDENT_BY;
668 xo_depth_check(xop, XO_DEPTH);
670 #if !defined(NO_LIBXO_OPTIONS)
671 if (!XOF_ISSET(xop, XOF_NO_ENV)) {
672 char *env = getenv("LIBXO_OPTIONS");
674 xo_set_options(xop, env);
677 #endif /* NO_GETENV */
679 XOIF_CLEAR(xop, XOIF_INIT_IN_PROGRESS);
683 * Initialize the default handle.
686 xo_default_init (void)
688 xo_handle_t *xop = &xo_default_handle;
692 xo_default_inited = 1;
696 * Cheap convenience function to return either the argument, or
697 * the internal handle, after it has been initialized. The usage
699 * xop = xo_default(xop);
702 xo_default (xo_handle_t *xop)
705 if (xo_default_inited == 0)
707 xop = &xo_default_handle;
714 * Return the number of spaces we should be indenting. If
715 * we are pretty-printing, this is indent * indent_by.
718 xo_indent (xo_handle_t *xop)
722 xop = xo_default(xop);
724 if (XOF_ISSET(xop, XOF_PRETTY)) {
725 rc = xop->xo_indent * xop->xo_indent_by;
726 if (XOIF_ISSET(xop, XOIF_TOP_EMITTED))
727 rc += xop->xo_indent_by;
730 return (rc > 0) ? rc : 0;
734 xo_buf_indent (xo_handle_t *xop, int indent)
736 xo_buffer_t *xbp = &xop->xo_data;
739 indent = xo_indent(xop);
741 if (!xo_buf_has_room(xbp, indent))
744 memset(xbp->xb_curp, ' ', indent);
745 xbp->xb_curp += indent;
748 static char xo_xml_amp[] = "&";
749 static char xo_xml_lt[] = "<";
750 static char xo_xml_gt[] = ">";
751 static char xo_xml_quot[] = """;
754 xo_escape_xml (xo_buffer_t *xbp, int len, xo_xff_flags_t flags)
760 int attr = (flags & XFF_ATTR);
762 for (cp = xbp->xb_curp, ep = cp + len; cp < ep; cp++) {
763 /* We're subtracting 2: 1 for the NUL, 1 for the char we replace */
765 delta += sizeof(xo_xml_lt) - 2;
767 delta += sizeof(xo_xml_gt) - 2;
769 delta += sizeof(xo_xml_amp) - 2;
770 else if (attr && *cp == '"')
771 delta += sizeof(xo_xml_quot) - 2;
774 if (delta == 0) /* Nothing to escape; bail */
777 if (!xo_buf_has_room(xbp, delta)) /* No room; bail, but don't append */
793 else if (attr && *cp == '"')
802 memcpy(ip, sp, slen);
804 } while (cp > ep && cp != ip);
810 xo_escape_json (xo_buffer_t *xbp, int len, xo_xff_flags_t flags UNUSED)
815 for (cp = xbp->xb_curp, ep = cp + len; cp < ep; cp++) {
816 if (*cp == '\\' || *cp == '"')
818 else if (*cp == '\n' || *cp == '\r')
822 if (delta == 0) /* Nothing to escape; bail */
825 if (!xo_buf_has_room(xbp, delta)) /* No room; bail, but don't append */
835 if (*cp == '\\' || *cp == '"') {
838 } else if (*cp == '\n') {
841 } else if (*cp == '\r') {
848 } while (cp > ep && cp != ip);
854 * PARAM-VALUE = UTF-8-STRING ; characters '"', '\' and
855 * ; ']' MUST be escaped.
858 xo_escape_sdparams (xo_buffer_t *xbp, int len, xo_xff_flags_t flags UNUSED)
863 for (cp = xbp->xb_curp, ep = cp + len; cp < ep; cp++) {
864 if (*cp == '\\' || *cp == '"' || *cp == ']')
868 if (delta == 0) /* Nothing to escape; bail */
871 if (!xo_buf_has_room(xbp, delta)) /* No room; bail, but don't append */
881 if (*cp == '\\' || *cp == '"' || *cp == ']') {
888 } while (cp > ep && cp != ip);
894 xo_buf_escape (xo_handle_t *xop, xo_buffer_t *xbp,
895 const char *str, int len, xo_xff_flags_t flags)
897 if (!xo_buf_has_room(xbp, len))
900 memcpy(xbp->xb_curp, str, len);
902 switch (xo_style(xop)) {
905 len = xo_escape_xml(xbp, len, flags);
909 len = xo_escape_json(xbp, len, flags);
912 case XO_STYLE_SDPARAMS:
913 len = xo_escape_sdparams(xbp, len, flags);
921 * Write the current contents of the data buffer using the handle's
925 xo_write (xo_handle_t *xop)
928 xo_buffer_t *xbp = &xop->xo_data;
930 if (xbp->xb_curp != xbp->xb_bufp) {
931 xo_buf_append(xbp, "", 1); /* Append ending NUL */
932 xo_anchor_clear(xop);
934 rc = xop->xo_write(xop->xo_opaque, xbp->xb_bufp);
935 xbp->xb_curp = xbp->xb_bufp;
938 /* Turn off the flags that don't survive across writes */
939 XOIF_CLEAR(xop, XOIF_UNITS_PENDING);
945 * Format arguments into our buffer. If a custom formatter has been set,
946 * we use that to do the work; otherwise we vsnprintf().
949 xo_vsnprintf (xo_handle_t *xop, xo_buffer_t *xbp, const char *fmt, va_list vap)
953 int left = xbp->xb_size - (xbp->xb_curp - xbp->xb_bufp);
955 va_copy(va_local, vap);
957 if (xop->xo_formatter)
958 rc = xop->xo_formatter(xop, xbp->xb_curp, left, fmt, va_local);
960 rc = vsnprintf(xbp->xb_curp, left, fmt, va_local);
963 if (!xo_buf_has_room(xbp, rc)) {
969 * After we call vsnprintf(), the stage of vap is not defined.
970 * We need to copy it before we pass. Then we have to do our
971 * own logic below to move it along. This is because the
972 * implementation can have va_list be a pointer (bsd) or a
973 * structure (macosx) or anything in between.
976 va_end(va_local); /* Reset vap to the start */
977 va_copy(va_local, vap);
979 left = xbp->xb_size - (xbp->xb_curp - xbp->xb_bufp);
980 if (xop->xo_formatter)
981 rc = xop->xo_formatter(xop, xbp->xb_curp, left, fmt, va_local);
983 rc = vsnprintf(xbp->xb_curp, left, fmt, va_local);
991 * Print some data thru the handle.
994 xo_printf_v (xo_handle_t *xop, const char *fmt, va_list vap)
996 xo_buffer_t *xbp = &xop->xo_data;
997 int left = xbp->xb_size - (xbp->xb_curp - xbp->xb_bufp);
1001 va_copy(va_local, vap);
1003 rc = vsnprintf(xbp->xb_curp, left, fmt, va_local);
1006 if (!xo_buf_has_room(xbp, rc)) {
1011 va_end(va_local); /* Reset vap to the start */
1012 va_copy(va_local, vap);
1014 left = xbp->xb_size - (xbp->xb_curp - xbp->xb_bufp);
1015 rc = vsnprintf(xbp->xb_curp, left, fmt, va_local);
1027 xo_printf (xo_handle_t *xop, const char *fmt, ...)
1034 rc = xo_printf_v(xop, fmt, vap);
1041 * These next few function are make The Essential UTF-8 Ginsu Knife.
1042 * Identify an input and output character, and convert it.
1044 static int xo_utf8_bits[7] = { 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
1047 xo_is_utf8 (char ch)
1053 xo_utf8_to_wc_len (const char *buf)
1055 unsigned b = (unsigned char) *buf;
1058 if ((b & 0x80) == 0x0)
1060 else if ((b & 0xe0) == 0xc0)
1062 else if ((b & 0xf0) == 0xe0)
1064 else if ((b & 0xf8) == 0xf0)
1066 else if ((b & 0xfc) == 0xf8)
1068 else if ((b & 0xfe) == 0xfc)
1077 xo_buf_utf8_len (xo_handle_t *xop, const char *buf, int bufsiz)
1080 unsigned b = (unsigned char) *buf;
1083 len = xo_utf8_to_wc_len(buf);
1085 xo_failure(xop, "invalid UTF-8 data: %02hhx", b);
1090 xo_failure(xop, "invalid UTF-8 data (short): %02hhx (%d/%d)",
1095 for (i = 2; i < len; i++) {
1096 b = (unsigned char ) buf[i];
1097 if ((b & 0xc0) != 0x80) {
1098 xo_failure(xop, "invalid UTF-8 data (byte %d): %x", i, b);
1107 * Build a wide character from the input buffer; the number of
1108 * bits we pull off the first character is dependent on the length,
1109 * but we put 6 bits off all other bytes.
1111 static inline wchar_t
1112 xo_utf8_char (const char *buf, int len)
1114 /* Most common case: singleton byte */
1116 return (unsigned char) buf[0];
1120 const unsigned char *cp = (const unsigned char *) buf;
1122 wc = *cp & xo_utf8_bits[len];
1123 for (i = 1; i < len; i++) {
1126 if ((cp[i] & 0xc0) != 0x80)
1127 return (wchar_t) -1;
1134 * Determine the number of bytes needed to encode a wide character.
1137 xo_utf8_emit_len (wchar_t wc)
1141 if ((wc & ((1<<7) - 1)) == wc) /* Simple case */
1143 else if ((wc & ((1<<11) - 1)) == wc)
1145 else if ((wc & ((1<<16) - 1)) == wc)
1147 else if ((wc & ((1<<21) - 1)) == wc)
1149 else if ((wc & ((1<<26) - 1)) == wc)
1158 xo_utf8_emit_char (char *buf, int len, wchar_t wc)
1162 if (len == 1) { /* Simple case */
1167 for (i = len - 1; i >= 0; i--) {
1168 buf[i] = 0x80 | (wc & 0x3f);
1172 buf[0] &= xo_utf8_bits[len];
1173 buf[0] |= ~xo_utf8_bits[len] << 1;
1177 xo_buf_append_locale_from_utf8 (xo_handle_t *xop, xo_buffer_t *xbp,
1178 const char *ibuf, int ilen)
1184 * Build our wide character from the input buffer; the number of
1185 * bits we pull off the first character is dependent on the length,
1186 * but we put 6 bits off all other bytes.
1188 wc = xo_utf8_char(ibuf, ilen);
1189 if (wc == (wchar_t) -1) {
1190 xo_failure(xop, "invalid utf-8 byte sequence");
1194 if (XOF_ISSET(xop, XOF_NO_LOCALE)) {
1195 if (!xo_buf_has_room(xbp, ilen))
1198 memcpy(xbp->xb_curp, ibuf, ilen);
1199 xbp->xb_curp += ilen;
1202 if (!xo_buf_has_room(xbp, MB_LEN_MAX + 1))
1205 bzero(&xop->xo_mbstate, sizeof(xop->xo_mbstate));
1206 len = wcrtomb(xbp->xb_curp, wc, &xop->xo_mbstate);
1209 xo_failure(xop, "could not convert wide char: %lx",
1210 (unsigned long) wc);
1213 xbp->xb_curp += len;
1216 return xo_wcwidth(wc);
1220 xo_buf_append_locale (xo_handle_t *xop, xo_buffer_t *xbp,
1221 const char *cp, int len)
1223 const char *sp = cp, *ep = cp + len;
1224 unsigned save_off = xbp->xb_bufp - xbp->xb_curp;
1228 for ( ; cp < ep; cp++) {
1229 if (!xo_is_utf8(*cp)) {
1235 * We're looking at a non-ascii UTF-8 character.
1236 * First we copy the previous data.
1237 * Then we need find the length and validate it.
1238 * Then we turn it into a wide string.
1239 * Then we turn it into a localized string.
1240 * Then we repeat. Isn't i18n fun?
1243 xo_buf_append(xbp, sp, cp - sp); /* Append previous data */
1245 slen = xo_buf_utf8_len(xop, cp, ep - cp);
1247 /* Bad data; back it all out */
1248 xbp->xb_curp = xbp->xb_bufp + save_off;
1252 cols += xo_buf_append_locale_from_utf8(xop, xbp, cp, slen);
1254 /* Next time thru, we'll start at the next character */
1259 /* Update column values */
1260 if (XOF_ISSET(xop, XOF_COLUMNS))
1261 xop->xo_columns += cols;
1262 if (XOIF_ISSET(xop, XOIF_ANCHOR))
1263 xop->xo_anchor_columns += cols;
1265 /* Before we fall into the basic logic below, we need reset len */
1267 if (len != 0) /* Append trailing data */
1268 xo_buf_append(xbp, sp, len);
1272 * Append the given string to the given buffer, without escaping or
1273 * character set conversion. This is the straight copy to the data
1274 * buffer with no fanciness.
1277 xo_data_append (xo_handle_t *xop, const char *str, int len)
1279 xo_buf_append(&xop->xo_data, str, len);
1283 * Append the given string to the given buffer
1286 xo_data_escape (xo_handle_t *xop, const char *str, int len)
1288 xo_buf_escape(xop, &xop->xo_data, str, len, 0);
1291 #ifdef LIBXO_NO_RETAIN
1293 * Empty implementations of the retain logic
1297 xo_retain_clear_all (void)
1303 xo_retain_clear (const char *fmt UNUSED)
1308 xo_retain_add (const char *fmt UNUSED, xo_field_info_t *fields UNUSED,
1309 unsigned num_fields UNUSED)
1315 xo_retain_find (const char *fmt UNUSED, xo_field_info_t **valp UNUSED,
1316 unsigned *nump UNUSED)
1321 #else /* !LIBXO_NO_RETAIN */
1323 * Retain: We retain parsed field definitions to enhance performance,
1324 * especially inside loops. We depend on the caller treating the format
1325 * strings as immutable, so that we can retain pointers into them. We
1326 * hold the pointers in a hash table, so allow quick access. Retained
1327 * information is retained until xo_retain_clear is called.
1331 * xo_retain_entry_t holds information about one retained set of
1334 typedef struct xo_retain_entry_s {
1335 struct xo_retain_entry_s *xre_next; /* Pointer to next (older) entry */
1336 unsigned long xre_hits; /* Number of times we've hit */
1337 const char *xre_format; /* Pointer to format string */
1338 unsigned xre_num_fields; /* Number of fields saved */
1339 xo_field_info_t *xre_fields; /* Pointer to fields */
1340 } xo_retain_entry_t;
1343 * xo_retain_t holds a complete set of parsed fields as a hash table.
1345 #ifndef XO_RETAIN_SIZE
1346 #define XO_RETAIN_SIZE 6
1347 #endif /* XO_RETAIN_SIZE */
1348 #define RETAIN_HASH_SIZE (1<<XO_RETAIN_SIZE)
1350 typedef struct xo_retain_s {
1351 xo_retain_entry_t *xr_bucket[RETAIN_HASH_SIZE];
1354 static THREAD_LOCAL(xo_retain_t) xo_retain;
1355 static THREAD_LOCAL(unsigned) xo_retain_count;
1358 * Simple hash function based on Thomas Wang's paper. The original is
1359 * gone, but an archive is available on the Way Back Machine:
1361 * http://web.archive.org/web/20071223173210/\
1362 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1364 * For our purposes, we can assume the low four bits are uninteresting
1365 * since any string less that 16 bytes wouldn't be worthy of
1366 * retaining. We toss the high bits also, since these bits are likely
1367 * to be common among constant format strings. We then run Wang's
1368 * algorithm, and cap the result at RETAIN_HASH_SIZE.
1371 xo_retain_hash (const char *fmt)
1373 volatile uintptr_t iptr = (uintptr_t) (const void *) fmt;
1375 /* Discard low four bits and high bits; they aren't interesting */
1376 uint32_t val = (uint32_t) ((iptr >> 4) & (((1 << 24) - 1)));
1378 val = (val ^ 61) ^ (val >> 16);
1379 val = val + (val << 3);
1380 val = val ^ (val >> 4);
1381 val = val * 0x3a8f05c5; /* My large prime number */
1382 val = val ^ (val >> 15);
1383 val &= RETAIN_HASH_SIZE - 1;
1389 * Walk all buckets, clearing all retained entries
1392 xo_retain_clear_all (void)
1395 xo_retain_entry_t *xrep, *next;
1397 for (i = 0; i < RETAIN_HASH_SIZE; i++) {
1398 for (xrep = xo_retain.xr_bucket[i]; xrep; xrep = next) {
1399 next = xrep->xre_next;
1402 xo_retain.xr_bucket[i] = NULL;
1404 xo_retain_count = 0;
1408 * Walk all buckets, clearing all retained entries
1411 xo_retain_clear (const char *fmt)
1413 xo_retain_entry_t **xrepp;
1414 unsigned hash = xo_retain_hash(fmt);
1416 for (xrepp = &xo_retain.xr_bucket[hash]; *xrepp;
1417 xrepp = &(*xrepp)->xre_next) {
1418 if ((*xrepp)->xre_format == fmt) {
1419 *xrepp = (*xrepp)->xre_next;
1420 xo_retain_count -= 1;
1427 * Search the hash for an entry matching 'fmt'; return it's fields.
1430 xo_retain_find (const char *fmt, xo_field_info_t **valp, unsigned *nump)
1432 if (xo_retain_count == 0)
1435 unsigned hash = xo_retain_hash(fmt);
1436 xo_retain_entry_t *xrep;
1438 for (xrep = xo_retain.xr_bucket[hash]; xrep != NULL;
1439 xrep = xrep->xre_next) {
1440 if (xrep->xre_format == fmt) {
1441 *valp = xrep->xre_fields;
1442 *nump = xrep->xre_num_fields;
1443 xrep->xre_hits += 1;
1452 xo_retain_add (const char *fmt, xo_field_info_t *fields, unsigned num_fields)
1454 unsigned hash = xo_retain_hash(fmt);
1455 xo_retain_entry_t *xrep;
1456 unsigned sz = sizeof(*xrep) + (num_fields + 1) * sizeof(*fields);
1457 xo_field_info_t *xfip;
1459 xrep = xo_realloc(NULL, sz);
1463 xfip = (xo_field_info_t *) &xrep[1];
1464 memcpy(xfip, fields, num_fields * sizeof(*fields));
1466 bzero(xrep, sizeof(*xrep));
1468 xrep->xre_format = fmt;
1469 xrep->xre_fields = xfip;
1470 xrep->xre_num_fields = num_fields;
1472 /* Record the field info in the retain bucket */
1473 xrep->xre_next = xo_retain.xr_bucket[hash];
1474 xo_retain.xr_bucket[hash] = xrep;
1475 xo_retain_count += 1;
1478 #endif /* !LIBXO_NO_RETAIN */
1481 * Generate a warning. Normally, this is a text message written to
1482 * standard error. If the XOF_WARN_XML flag is set, then we generate
1483 * XMLified content on standard output.
1486 xo_warn_hcv (xo_handle_t *xop, int code, int check_warn,
1487 const char *fmt, va_list vap)
1489 xop = xo_default(xop);
1490 if (check_warn && !XOF_ISSET(xop, XOF_WARN))
1496 int len = strlen(fmt);
1497 int plen = xo_program ? strlen(xo_program) : 0;
1498 char *newfmt = alloca(len + 1 + plen + 2); /* NUL, and ": " */
1501 memcpy(newfmt, xo_program, plen);
1502 newfmt[plen++] = ':';
1503 newfmt[plen++] = ' ';
1505 memcpy(newfmt + plen, fmt, len);
1506 newfmt[len + plen] = '\0';
1508 if (XOF_ISSET(xop, XOF_WARN_XML)) {
1509 static char err_open[] = "<error>";
1510 static char err_close[] = "</error>";
1511 static char msg_open[] = "<message>";
1512 static char msg_close[] = "</message>";
1514 xo_buffer_t *xbp = &xop->xo_data;
1516 xo_buf_append(xbp, err_open, sizeof(err_open) - 1);
1517 xo_buf_append(xbp, msg_open, sizeof(msg_open) - 1);
1520 va_copy(va_local, vap);
1522 int left = xbp->xb_size - (xbp->xb_curp - xbp->xb_bufp);
1523 int rc = vsnprintf(xbp->xb_curp, left, newfmt, vap);
1525 if (!xo_buf_has_room(xbp, rc)) {
1530 va_end(vap); /* Reset vap to the start */
1531 va_copy(vap, va_local);
1533 left = xbp->xb_size - (xbp->xb_curp - xbp->xb_bufp);
1534 rc = vsnprintf(xbp->xb_curp, left, fmt, vap);
1538 rc = xo_escape_xml(xbp, rc, 1);
1541 xo_buf_append(xbp, msg_close, sizeof(msg_close) - 1);
1542 xo_buf_append(xbp, err_close, sizeof(err_close) - 1);
1545 const char *msg = strerror(code);
1547 xo_buf_append(xbp, ": ", 2);
1548 xo_buf_append(xbp, msg, strlen(msg));
1552 xo_buf_append(xbp, "\n", 1); /* Append newline and NUL to string */
1553 (void) xo_write(xop);
1556 vfprintf(stderr, newfmt, vap);
1558 const char *msg = strerror(code);
1560 fprintf(stderr, ": %s", msg);
1562 fprintf(stderr, "\n");
1567 xo_warn_hc (xo_handle_t *xop, int code, const char *fmt, ...)
1572 xo_warn_hcv(xop, code, 0, fmt, vap);
1577 xo_warn_c (int code, const char *fmt, ...)
1582 xo_warn_hcv(NULL, code, 0, fmt, vap);
1587 xo_warn (const char *fmt, ...)
1593 xo_warn_hcv(NULL, code, 0, fmt, vap);
1598 xo_warnx (const char *fmt, ...)
1603 xo_warn_hcv(NULL, -1, 0, fmt, vap);
1608 xo_err (int eval, const char *fmt, ...)
1614 xo_warn_hcv(NULL, code, 0, fmt, vap);
1621 xo_errx (int eval, const char *fmt, ...)
1626 xo_warn_hcv(NULL, -1, 0, fmt, vap);
1633 xo_errc (int eval, int code, const char *fmt, ...)
1638 xo_warn_hcv(NULL, code, 0, fmt, vap);
1645 * Generate a warning. Normally, this is a text message written to
1646 * standard error. If the XOF_WARN_XML flag is set, then we generate
1647 * XMLified content on standard output.
1650 xo_message_hcv (xo_handle_t *xop, int code, const char *fmt, va_list vap)
1652 static char msg_open[] = "<message>";
1653 static char msg_close[] = "</message>";
1658 xop = xo_default(xop);
1660 if (fmt == NULL || *fmt == '\0')
1663 int need_nl = (fmt[strlen(fmt) - 1] != '\n');
1665 switch (xo_style(xop)) {
1667 xbp = &xop->xo_data;
1668 if (XOF_ISSET(xop, XOF_PRETTY))
1669 xo_buf_indent(xop, xop->xo_indent_by);
1670 xo_buf_append(xbp, msg_open, sizeof(msg_open) - 1);
1672 va_copy(va_local, vap);
1674 int left = xbp->xb_size - (xbp->xb_curp - xbp->xb_bufp);
1675 rc = vsnprintf(xbp->xb_curp, left, fmt, vap);
1677 if (!xo_buf_has_room(xbp, rc)) {
1682 va_end(vap); /* Reset vap to the start */
1683 va_copy(vap, va_local);
1685 left = xbp->xb_size - (xbp->xb_curp - xbp->xb_bufp);
1686 rc = vsnprintf(xbp->xb_curp, left, fmt, vap);
1690 rc = xo_escape_xml(xbp, rc, 0);
1693 if (need_nl && code > 0) {
1694 const char *msg = strerror(code);
1696 xo_buf_append(xbp, ": ", 2);
1697 xo_buf_append(xbp, msg, strlen(msg));
1702 xo_buf_append(xbp, "\n", 1); /* Append newline and NUL to string */
1704 xo_buf_append(xbp, msg_close, sizeof(msg_close) - 1);
1706 if (XOF_ISSET(xop, XOF_PRETTY))
1707 xo_buf_append(xbp, "\n", 1); /* Append newline and NUL to string */
1709 (void) xo_write(xop);
1714 char buf[BUFSIZ], *bp = buf, *cp;
1715 int bufsiz = sizeof(buf);
1718 va_copy(va_local, vap);
1720 rc = vsnprintf(bp, bufsiz, fmt, va_local);
1722 bufsiz = rc + BUFSIZ;
1723 bp = alloca(bufsiz);
1725 va_copy(va_local, vap);
1726 rc = vsnprintf(bp, bufsiz, fmt, va_local);
1732 rc2 = snprintf(cp, bufsiz - rc, "%s%s\n",
1733 (code > 0) ? ": " : "",
1734 (code > 0) ? strerror(code) : "");
1739 xo_buf_append_div(xop, "message", 0, NULL, 0, bp, rc, NULL, 0);
1744 case XO_STYLE_SDPARAMS:
1745 case XO_STYLE_ENCODER:
1746 /* No means of representing messages */
1750 rc = xo_printf_v(xop, fmt, vap);
1752 * XXX need to handle UTF-8 widths
1755 if (XOF_ISSET(xop, XOF_COLUMNS))
1756 xop->xo_columns += rc;
1757 if (XOIF_ISSET(xop, XOIF_ANCHOR))
1758 xop->xo_anchor_columns += rc;
1761 if (need_nl && code > 0) {
1762 const char *msg = strerror(code);
1764 xo_printf(xop, ": %s", msg);
1768 xo_printf(xop, "\n");
1773 switch (xo_style(xop)) {
1775 if (XOIF_ISSET(xop, XOIF_DIV_OPEN)) {
1776 static char div_close[] = "</div>";
1777 XOIF_CLEAR(xop, XOIF_DIV_OPEN);
1778 xo_data_append(xop, div_close, sizeof(div_close) - 1);
1780 if (XOF_ISSET(xop, XOF_PRETTY))
1781 xo_data_append(xop, "\n", 1);
1786 (void) xo_flush_h(xop);
1790 xo_message_hc (xo_handle_t *xop, int code, const char *fmt, ...)
1795 xo_message_hcv(xop, code, fmt, vap);
1800 xo_message_c (int code, const char *fmt, ...)
1805 xo_message_hcv(NULL, code, fmt, vap);
1810 xo_message_e (const char *fmt, ...)
1816 xo_message_hcv(NULL, code, fmt, vap);
1821 xo_message (const char *fmt, ...)
1826 xo_message_hcv(NULL, 0, fmt, vap);
1831 xo_failure (xo_handle_t *xop, const char *fmt, ...)
1833 if (!XOF_ISSET(xop, XOF_WARN))
1839 xo_warn_hcv(xop, -1, 1, fmt, vap);
1844 * Create a handle for use by later libxo functions.
1846 * Note: normal use of libxo does not require a distinct handle, since
1847 * the default handle (used when NULL is passed) generates text on stdout.
1849 * @style Style of output desired (XO_STYLE_* value)
1850 * @flags Set of XOF_* flags in use with this handle
1853 xo_create (xo_style_t style, xo_xof_flags_t flags)
1855 xo_handle_t *xop = xo_realloc(NULL, sizeof(*xop));
1858 bzero(xop, sizeof(*xop));
1860 xop->xo_style = style;
1861 XOF_SET(xop, flags);
1862 xo_init_handle(xop);
1863 xop->xo_style = style; /* Reset style (see LIBXO_OPTIONS) */
1870 * Create a handle that will write to the given file. Use
1871 * the XOF_CLOSE_FP flag to have the file closed on xo_destroy().
1872 * @fp FILE pointer to use
1873 * @style Style of output desired (XO_STYLE_* value)
1874 * @flags Set of XOF_* flags to use with this handle
1877 xo_create_to_file (FILE *fp, xo_style_t style, xo_xof_flags_t flags)
1879 xo_handle_t *xop = xo_create(style, flags);
1882 xop->xo_opaque = fp;
1883 xop->xo_write = xo_write_to_file;
1884 xop->xo_close = xo_close_file;
1885 xop->xo_flush = xo_flush_file;
1892 * Set the default handler to output to a file.
1894 * @fp FILE pointer to use
1897 xo_set_file_h (xo_handle_t *xop, FILE *fp)
1899 xop = xo_default(xop);
1902 xo_failure(xop, "xo_set_file: NULL fp");
1906 xop->xo_opaque = fp;
1907 xop->xo_write = xo_write_to_file;
1908 xop->xo_close = xo_close_file;
1909 xop->xo_flush = xo_flush_file;
1915 * Set the default handler to output to a file.
1916 * @fp FILE pointer to use
1919 xo_set_file (FILE *fp)
1921 return xo_set_file_h(NULL, fp);
1925 * Release any resources held by the handle.
1926 * @xop XO handle to alter (or NULL for default handle)
1929 xo_destroy (xo_handle_t *xop_arg)
1931 xo_handle_t *xop = xo_default(xop_arg);
1935 if (xop->xo_close && XOF_ISSET(xop, XOF_CLOSE_FP))
1936 xop->xo_close(xop->xo_opaque);
1938 xo_free(xop->xo_stack);
1939 xo_buf_cleanup(&xop->xo_data);
1940 xo_buf_cleanup(&xop->xo_fmt);
1941 xo_buf_cleanup(&xop->xo_predicate);
1942 xo_buf_cleanup(&xop->xo_attrs);
1943 xo_buf_cleanup(&xop->xo_color_buf);
1945 if (xop->xo_version)
1946 xo_free(xop->xo_version);
1948 if (xop_arg == NULL) {
1949 bzero(&xo_default_handle, sizeof(xo_default_handle));
1950 xo_default_inited = 0;
1956 * Record a new output style to use for the given handle (or default if
1957 * handle is NULL). This output style will be used for any future output.
1959 * @xop XO handle to alter (or NULL for default handle)
1960 * @style new output style (XO_STYLE_*)
1963 xo_set_style (xo_handle_t *xop, xo_style_t style)
1965 xop = xo_default(xop);
1966 xop->xo_style = style;
1970 xo_get_style (xo_handle_t *xop)
1972 xop = xo_default(xop);
1973 return xo_style(xop);
1977 xo_name_to_style (const char *name)
1979 if (strcmp(name, "xml") == 0)
1980 return XO_STYLE_XML;
1981 else if (strcmp(name, "json") == 0)
1982 return XO_STYLE_JSON;
1983 else if (strcmp(name, "encoder") == 0)
1984 return XO_STYLE_ENCODER;
1985 else if (strcmp(name, "text") == 0)
1986 return XO_STYLE_TEXT;
1987 else if (strcmp(name, "html") == 0)
1988 return XO_STYLE_HTML;
1989 else if (strcmp(name, "sdparams") == 0)
1990 return XO_STYLE_SDPARAMS;
1996 * Indicate if the style is an "encoding" one as opposed to a "display" one.
1999 xo_style_is_encoding (xo_handle_t *xop)
2001 if (xo_style(xop) == XO_STYLE_JSON
2002 || xo_style(xop) == XO_STYLE_XML
2003 || xo_style(xop) == XO_STYLE_SDPARAMS
2004 || xo_style(xop) == XO_STYLE_ENCODER)
2009 /* Simple name-value mapping */
2010 typedef struct xo_mapping_s {
2011 xo_xff_flags_t xm_value;
2012 const char *xm_name;
2015 static xo_xff_flags_t
2016 xo_name_lookup (xo_mapping_t *map, const char *value, int len)
2022 len = strlen(value);
2024 while (isspace((int) *value)) {
2029 while (isspace((int) value[len]))
2035 for ( ; map->xm_name; map++)
2036 if (strncmp(map->xm_name, value, len) == 0)
2037 return map->xm_value;
2042 #ifdef NOT_NEEDED_YET
2044 xo_value_lookup (xo_mapping_t *map, xo_xff_flags_t value)
2049 for ( ; map->xm_name; map++)
2050 if (map->xm_value == value)
2051 return map->xm_name;
2055 #endif /* NOT_NEEDED_YET */
2057 static xo_mapping_t xo_xof_names[] = {
2058 { XOF_COLOR_ALLOWED, "color" },
2059 { XOF_COLUMNS, "columns" },
2060 { XOF_DTRT, "dtrt" },
2061 { XOF_FLUSH, "flush" },
2062 { XOF_IGNORE_CLOSE, "ignore-close" },
2063 { XOF_INFO, "info" },
2064 { XOF_KEYS, "keys" },
2065 { XOF_LOG_GETTEXT, "log-gettext" },
2066 { XOF_LOG_SYSLOG, "log-syslog" },
2067 { XOF_NO_HUMANIZE, "no-humanize" },
2068 { XOF_NO_LOCALE, "no-locale" },
2069 { XOF_RETAIN_NONE, "no-retain" },
2070 { XOF_NO_TOP, "no-top" },
2071 { XOF_NOT_FIRST, "not-first" },
2072 { XOF_PRETTY, "pretty" },
2073 { XOF_RETAIN_ALL, "retain" },
2074 { XOF_UNDERSCORES, "underscores" },
2075 { XOF_UNITS, "units" },
2076 { XOF_WARN, "warn" },
2077 { XOF_WARN_XML, "warn-xml" },
2078 { XOF_XPATH, "xpath" },
2083 * Convert string name to XOF_* flag value.
2084 * Not all are useful. Or safe. Or sane.
2087 xo_name_to_flag (const char *name)
2089 return (unsigned) xo_name_lookup(xo_xof_names, name, -1);
2093 xo_set_style_name (xo_handle_t *xop, const char *name)
2098 int style = xo_name_to_style(name);
2102 xo_set_style(xop, style);
2107 * Set the options for a handle using a string of options
2108 * passed in. The input is a comma-separated set of names
2109 * and optional values: "xml,pretty,indent=4"
2112 xo_set_options (xo_handle_t *xop, const char *input)
2114 char *cp, *ep, *vp, *np, *bp;
2115 int style = -1, new_style, len, rc = 0;
2116 xo_xof_flags_t new_flag;
2121 xop = xo_default(xop);
2123 #ifdef LIBXO_COLOR_ON_BY_DEFAULT
2124 /* If the installer used --enable-color-on-by-default, then we allow it */
2125 XOF_SET(xop, XOF_COLOR_ALLOWED);
2126 #endif /* LIBXO_COLOR_ON_BY_DEFAULT */
2129 * We support a simpler, old-school style of giving option
2130 * also, using a single character for each option. It's
2131 * ideal for lazy people, such as myself.
2133 if (*input == ':') {
2136 for (input++ ; *input; input++) {
2139 XOF_SET(xop, XOF_COLOR_ALLOWED);
2143 XOF_SET(xop, XOF_FLUSH);
2147 XOF_SET(xop, XOF_FLUSH_LINE);
2151 XOF_SET(xop, XOF_LOG_GETTEXT);
2155 xop->xo_style = XO_STYLE_HTML;
2159 XOF_SET(xop, XOF_INFO);
2163 sz = strspn(input + 1, "0123456789");
2165 xop->xo_indent_by = atoi(input + 1);
2166 input += sz - 1; /* Skip value */
2171 xop->xo_style = XO_STYLE_JSON;
2175 XOF_SET(xop, XOF_KEYS);
2179 XOF_SET(xop, XOF_NO_HUMANIZE);
2183 XOF_SET(xop, XOF_PRETTY);
2187 xop->xo_style = XO_STYLE_TEXT;
2191 XOF_SET(xop, XOF_UNITS);
2195 XOF_SET(xop, XOF_UNDERSCORES);
2199 XOF_SET(xop, XOF_WARN);
2203 xop->xo_style = XO_STYLE_XML;
2207 XOF_SET(xop, XOF_XPATH);
2214 len = strlen(input) + 1;
2216 memcpy(bp, input, len);
2218 for (cp = bp, ep = cp + len - 1; cp && cp < ep; cp = np) {
2219 np = strchr(cp, ',');
2223 vp = strchr(cp, '=');
2227 if (strcmp("colors", cp) == 0) {
2228 /* XXX Look for colors=red-blue+green-yellow */
2233 * For options, we don't allow "encoder" since we want to
2234 * handle it explicitly below as "encoder=xxx".
2236 new_style = xo_name_to_style(cp);
2237 if (new_style >= 0 && new_style != XO_STYLE_ENCODER) {
2239 xo_warnx("ignoring multiple styles: '%s'", cp);
2243 new_flag = xo_name_to_flag(cp);
2245 XOF_SET(xop, new_flag);
2247 if (strcmp(cp, "no-color") == 0) {
2248 XOF_CLEAR(xop, XOF_COLOR_ALLOWED);
2249 } else if (strcmp(cp, "indent") == 0) {
2251 xop->xo_indent_by = atoi(vp);
2253 xo_failure(xop, "missing value for indent option");
2254 } else if (strcmp(cp, "encoder") == 0) {
2256 xo_failure(xop, "missing value for encoder option");
2258 if (xo_encoder_init(xop, vp)) {
2259 xo_failure(xop, "encoder not found: %s", vp);
2265 xo_warnx("unknown libxo option value: '%s'", cp);
2273 xop->xo_style= style;
2279 * Set one or more flags for a given handle (or default if handle is NULL).
2280 * These flags will affect future output.
2282 * @xop XO handle to alter (or NULL for default handle)
2283 * @flags Flags to be set (XOF_*)
2286 xo_set_flags (xo_handle_t *xop, xo_xof_flags_t flags)
2288 xop = xo_default(xop);
2290 XOF_SET(xop, flags);
2294 xo_get_flags (xo_handle_t *xop)
2296 xop = xo_default(xop);
2298 return xop->xo_flags;
2302 * strndup with a twist: len < 0 means strlen
2305 xo_strndup (const char *str, int len)
2310 char *cp = xo_realloc(NULL, len + 1);
2312 memcpy(cp, str, len);
2320 * Record a leading prefix for the XPath we generate. This allows the
2321 * generated data to be placed within an XML hierarchy but still have
2322 * accurate XPath expressions.
2324 * @xop XO handle to alter (or NULL for default handle)
2325 * @path The XPath expression
2328 xo_set_leading_xpath (xo_handle_t *xop, const char *path)
2330 xop = xo_default(xop);
2332 if (xop->xo_leading_xpath) {
2333 xo_free(xop->xo_leading_xpath);
2334 xop->xo_leading_xpath = NULL;
2340 xop->xo_leading_xpath = xo_strndup(path, -1);
2344 * Record the info data for a set of tags
2346 * @xop XO handle to alter (or NULL for default handle)
2347 * @info Info data (xo_info_t) to be recorded (or NULL) (MUST BE SORTED)
2348 * @count Number of entries in info (or -1 to count them ourselves)
2351 xo_set_info (xo_handle_t *xop, xo_info_t *infop, int count)
2353 xop = xo_default(xop);
2355 if (count < 0 && infop) {
2358 for (xip = infop, count = 0; xip->xi_name; xip++, count++)
2362 xop->xo_info = infop;
2363 xop->xo_info_count = count;
2367 * Set the formatter callback for a handle. The callback should
2368 * return a newly formatting contents of a formatting instruction,
2369 * meaning the bits inside the braces.
2372 xo_set_formatter (xo_handle_t *xop, xo_formatter_t func,
2373 xo_checkpointer_t cfunc)
2375 xop = xo_default(xop);
2377 xop->xo_formatter = func;
2378 xop->xo_checkpointer = cfunc;
2382 * Clear one or more flags for a given handle (or default if handle is NULL).
2383 * These flags will affect future output.
2385 * @xop XO handle to alter (or NULL for default handle)
2386 * @flags Flags to be cleared (XOF_*)
2389 xo_clear_flags (xo_handle_t *xop, xo_xof_flags_t flags)
2391 xop = xo_default(xop);
2393 XOF_CLEAR(xop, flags);
2397 xo_state_name (xo_state_t state)
2399 static const char *names[] = {
2417 if (state < (sizeof(names) / sizeof(names[0])))
2418 return names[state];
2424 xo_line_ensure_open (xo_handle_t *xop, xo_xff_flags_t flags UNUSED)
2426 static char div_open[] = "<div class=\"line\">";
2427 static char div_open_blank[] = "<div class=\"blank-line\">";
2429 if (XOIF_ISSET(xop, XOIF_DIV_OPEN))
2432 if (xo_style(xop) != XO_STYLE_HTML)
2435 XOIF_SET(xop, XOIF_DIV_OPEN);
2436 if (flags & XFF_BLANK_LINE)
2437 xo_data_append(xop, div_open_blank, sizeof(div_open_blank) - 1);
2439 xo_data_append(xop, div_open, sizeof(div_open) - 1);
2441 if (XOF_ISSET(xop, XOF_PRETTY))
2442 xo_data_append(xop, "\n", 1);
2446 xo_line_close (xo_handle_t *xop)
2448 static char div_close[] = "</div>";
2450 switch (xo_style(xop)) {
2452 if (!XOIF_ISSET(xop, XOIF_DIV_OPEN))
2453 xo_line_ensure_open(xop, 0);
2455 XOIF_CLEAR(xop, XOIF_DIV_OPEN);
2456 xo_data_append(xop, div_close, sizeof(div_close) - 1);
2458 if (XOF_ISSET(xop, XOF_PRETTY))
2459 xo_data_append(xop, "\n", 1);
2463 xo_data_append(xop, "\n", 1);
2469 xo_info_compare (const void *key, const void *data)
2471 const char *name = key;
2472 const xo_info_t *xip = data;
2474 return strcmp(name, xip->xi_name);
2479 xo_info_find (xo_handle_t *xop, const char *name, int nlen)
2482 char *cp = alloca(nlen + 1); /* Need local copy for NUL termination */
2484 memcpy(cp, name, nlen);
2487 xip = bsearch(cp, xop->xo_info, xop->xo_info_count,
2488 sizeof(xop->xo_info[0]), xo_info_compare);
2492 #define CONVERT(_have, _need) (((_have) << 8) | (_need))
2495 * Check to see that the conversion is safe and sane.
2498 xo_check_conversion (xo_handle_t *xop, int have_enc, int need_enc)
2500 switch (CONVERT(have_enc, need_enc)) {
2501 case CONVERT(XF_ENC_UTF8, XF_ENC_UTF8):
2502 case CONVERT(XF_ENC_UTF8, XF_ENC_LOCALE):
2503 case CONVERT(XF_ENC_WIDE, XF_ENC_UTF8):
2504 case CONVERT(XF_ENC_WIDE, XF_ENC_LOCALE):
2505 case CONVERT(XF_ENC_LOCALE, XF_ENC_LOCALE):
2506 case CONVERT(XF_ENC_LOCALE, XF_ENC_UTF8):
2510 xo_failure(xop, "invalid conversion (%c:%c)", have_enc, need_enc);
2516 xo_format_string_direct (xo_handle_t *xop, xo_buffer_t *xbp,
2517 xo_xff_flags_t flags,
2518 const wchar_t *wcp, const char *cp, int len, int max,
2519 int need_enc, int have_enc)
2523 int ilen, olen, width;
2524 int attr = (flags & XFF_ATTR);
2527 if (len > 0 && !xo_buf_has_room(xbp, len))
2537 if ((flags & XFF_UNESCAPE) && (*cp == '\\' || *cp == '%')) {
2543 if (wcp && *wcp == L'\0')
2549 case XF_ENC_WIDE: /* Wide character */
2554 case XF_ENC_UTF8: /* UTF-8 */
2555 ilen = xo_utf8_to_wc_len(cp);
2557 xo_failure(xop, "invalid UTF-8 character: %02hhx", *cp);
2558 return -1; /* Can't continue; we can't find the end */
2561 if (len > 0 && len < ilen) {
2562 len = 0; /* Break out of the loop */
2566 wc = xo_utf8_char(cp, ilen);
2567 if (wc == (wchar_t) -1) {
2568 xo_failure(xop, "invalid UTF-8 character: %02hhx/%d",
2570 return -1; /* Can't continue; we can't find the end */
2575 case XF_ENC_LOCALE: /* Native locale */
2576 ilen = (len > 0) ? len : MB_LEN_MAX;
2577 ilen = mbrtowc(&wc, cp, ilen, &xop->xo_mbstate);
2578 if (ilen < 0) { /* Invalid data; skip */
2579 xo_failure(xop, "invalid mbs char: %02hhx", *cp);
2584 if (ilen == 0) { /* Hit a wide NUL character */
2593 /* Reduce len, but not below zero */
2601 * Find the width-in-columns of this character, which must be done
2602 * in wide characters, since we lack a mbswidth() function. If
2605 width = xo_wcwidth(wc);
2607 width = iswcntrl(wc) ? 0 : 1;
2609 if (xo_style(xop) == XO_STYLE_TEXT || xo_style(xop) == XO_STYLE_HTML) {
2610 if (max > 0 && cols + width > max)
2617 /* Output in UTF-8 needs to be escaped, based on the style */
2618 switch (xo_style(xop)) {
2627 else if (attr && wc == '"')
2632 int slen = strlen(sp);
2633 if (!xo_buf_has_room(xbp, slen - 1))
2636 memcpy(xbp->xb_curp, sp, slen);
2637 xbp->xb_curp += slen;
2638 goto done_with_encoding; /* Need multi-level 'break' */
2641 if (wc != '\\' && wc != '"' && wc != '\n' && wc != '\r')
2644 if (!xo_buf_has_room(xbp, 2))
2647 *xbp->xb_curp++ = '\\';
2650 else if (wc == '\r')
2652 else wc = wc & 0x7f;
2654 *xbp->xb_curp++ = wc;
2655 goto done_with_encoding;
2657 case XO_STYLE_SDPARAMS:
2658 if (wc != '\\' && wc != '"' && wc != ']')
2661 if (!xo_buf_has_room(xbp, 2))
2664 *xbp->xb_curp++ = '\\';
2666 *xbp->xb_curp++ = wc;
2667 goto done_with_encoding;
2670 olen = xo_utf8_emit_len(wc);
2672 xo_failure(xop, "ignoring bad length");
2676 if (!xo_buf_has_room(xbp, olen))
2679 xo_utf8_emit_char(xbp->xb_curp, olen, wc);
2680 xbp->xb_curp += olen;
2684 if (!xo_buf_has_room(xbp, MB_LEN_MAX + 1))
2687 olen = wcrtomb(xbp->xb_curp, wc, &xop->xo_mbstate);
2689 xo_failure(xop, "could not convert wide char: %lx",
2690 (unsigned long) wc);
2692 *xbp->xb_curp++ = '?';
2694 xbp->xb_curp += olen;
2706 xo_needed_encoding (xo_handle_t *xop)
2708 if (XOF_ISSET(xop, XOF_UTF8)) /* Check the override flag */
2711 if (xo_style(xop) == XO_STYLE_TEXT) /* Text means locale */
2712 return XF_ENC_LOCALE;
2714 return XF_ENC_UTF8; /* Otherwise, we love UTF-8 */
2718 xo_format_string (xo_handle_t *xop, xo_buffer_t *xbp, xo_xff_flags_t flags,
2721 static char null[] = "(null)";
2722 static char null_no_quotes[] = "null";
2725 wchar_t *wcp = NULL;
2726 int len, cols = 0, rc = 0;
2727 int off = xbp->xb_curp - xbp->xb_bufp, off2;
2728 int need_enc = xo_needed_encoding(xop);
2730 if (xo_check_conversion(xop, xfp->xf_enc, need_enc))
2733 len = xfp->xf_width[XF_WIDTH_SIZE];
2735 if (xfp->xf_fc == 'm') {
2736 cp = strerror(xop->xo_errno);
2738 len = cp ? strlen(cp) : 0;
2741 } else if (xfp->xf_enc == XF_ENC_WIDE) {
2742 wcp = va_arg(xop->xo_vap, wchar_t *);
2747 * Dont' deref NULL; use the traditional "(null)" instead
2748 * of the more accurate "who's been a naughty boy, then?".
2752 len = sizeof(null) - 1;
2756 cp = va_arg(xop->xo_vap, char *); /* UTF-8 or native */
2762 /* Echo "Dont' deref NULL" logic */
2764 if ((flags & XFF_NOQUOTE) && xo_style_is_encoding(xop)) {
2765 cp = null_no_quotes;
2766 len = sizeof(null_no_quotes) - 1;
2769 len = sizeof(null) - 1;
2774 * Optimize the most common case, which is "%s". We just
2775 * need to copy the complete string to the output buffer.
2777 if (xfp->xf_enc == need_enc
2778 && xfp->xf_width[XF_WIDTH_MIN] < 0
2779 && xfp->xf_width[XF_WIDTH_SIZE] < 0
2780 && xfp->xf_width[XF_WIDTH_MAX] < 0
2781 && !(XOIF_ISSET(xop, XOIF_ANCHOR)
2782 || XOF_ISSET(xop, XOF_COLUMNS))) {
2784 xo_buf_escape(xop, xbp, cp, len, flags);
2787 * Our caller expects xb_curp left untouched, so we have
2788 * to reset it and return the number of bytes written to
2791 off2 = xbp->xb_curp - xbp->xb_bufp;
2793 xbp->xb_curp = xbp->xb_bufp + off;
2799 cols = xo_format_string_direct(xop, xbp, flags, wcp, cp, len,
2800 xfp->xf_width[XF_WIDTH_MAX],
2801 need_enc, xfp->xf_enc);
2806 * xo_buf_append* will move xb_curp, so we save/restore it.
2808 off2 = xbp->xb_curp - xbp->xb_bufp;
2810 xbp->xb_curp = xbp->xb_bufp + off;
2812 if (cols < xfp->xf_width[XF_WIDTH_MIN]) {
2814 * Find the number of columns needed to display the string.
2815 * If we have the original wide string, we just call wcswidth,
2816 * but if we did the work ourselves, then we need to do it.
2818 int delta = xfp->xf_width[XF_WIDTH_MIN] - cols;
2819 if (!xo_buf_has_room(xbp, delta))
2823 * If seen_minus, then pad on the right; otherwise move it so
2824 * we can pad on the left.
2826 if (xfp->xf_seen_minus) {
2827 cp = xbp->xb_curp + rc;
2830 memmove(xbp->xb_curp + delta, xbp->xb_curp, rc);
2833 /* Set the padding */
2834 memset(cp, (xfp->xf_leading_zero > 0) ? '0' : ' ', delta);
2839 if (XOF_ISSET(xop, XOF_COLUMNS))
2840 xop->xo_columns += cols;
2841 if (XOIF_ISSET(xop, XOIF_ANCHOR))
2842 xop->xo_anchor_columns += cols;
2847 xbp->xb_curp = xbp->xb_bufp + off;
2852 * Look backwards in a buffer to find a numeric value
2855 xo_buf_find_last_number (xo_buffer_t *xbp, int start_offset)
2857 int rc = 0; /* Fail with zero */
2859 char *sp = xbp->xb_bufp;
2860 char *cp = sp + start_offset;
2863 if (isdigit((int) *cp))
2866 for ( ; cp >= sp; cp--) {
2867 if (!isdigit((int) *cp))
2869 rc += (*cp - '0') * digit;
2877 xo_count_utf8_cols (const char *str, int len)
2882 const char *ep = str + len;
2885 tlen = xo_utf8_to_wc_len(str);
2886 if (tlen < 0) /* Broken input is very bad */
2889 wc = xo_utf8_char(str, tlen);
2890 if (wc == (wchar_t) -1)
2893 /* We only print printable characters */
2894 if (iswprint((wint_t) wc)) {
2896 * Find the width-in-columns of this character, which must be done
2897 * in wide characters, since we lack a mbswidth() function.
2899 int width = xo_wcwidth(wc);
2901 width = iswcntrl(wc) ? 0 : 1;
2913 static inline const char *
2914 xo_dgettext (xo_handle_t *xop, const char *str)
2916 const char *domainname = xop->xo_gt_domain;
2919 res = dgettext(domainname, str);
2921 if (XOF_ISSET(xop, XOF_LOG_GETTEXT))
2922 fprintf(stderr, "xo: gettext: %s%s%smsgid \"%s\" returns \"%s\"\n",
2923 domainname ? "domain \"" : "", xo_printable(domainname),
2924 domainname ? "\", " : "", xo_printable(str), xo_printable(res));
2929 static inline const char *
2930 xo_dngettext (xo_handle_t *xop, const char *sing, const char *plural,
2931 unsigned long int n)
2933 const char *domainname = xop->xo_gt_domain;
2936 res = dngettext(domainname, sing, plural, n);
2937 if (XOF_ISSET(xop, XOF_LOG_GETTEXT))
2938 fprintf(stderr, "xo: gettext: %s%s%s"
2939 "msgid \"%s\", msgid_plural \"%s\" (%lu) returns \"%s\"\n",
2940 domainname ? "domain \"" : "",
2941 xo_printable(domainname), domainname ? "\", " : "",
2943 xo_printable(plural), n, xo_printable(res));
2947 #else /* HAVE_GETTEXT */
2948 static inline const char *
2949 xo_dgettext (xo_handle_t *xop UNUSED, const char *str)
2954 static inline const char *
2955 xo_dngettext (xo_handle_t *xop UNUSED, const char *singular,
2956 const char *plural, unsigned long int n)
2958 return (n == 1) ? singular : plural;
2960 #endif /* HAVE_GETTEXT */
2963 * This is really _re_formatting, since the normal format code has
2964 * generated a beautiful string into xo_data, starting at
2965 * start_offset. We need to see if it's plural, which means
2966 * comma-separated options, or singular. Then we make the appropriate
2967 * call to d[n]gettext() to get the locale-based version. Note that
2968 * both input and output of gettext() this should be UTF-8.
2971 xo_format_gettext (xo_handle_t *xop, xo_xff_flags_t flags,
2972 int start_offset, int cols, int need_enc)
2974 xo_buffer_t *xbp = &xop->xo_data;
2976 if (!xo_buf_has_room(xbp, 1))
2979 xbp->xb_curp[0] = '\0'; /* NUL-terminate the input string */
2981 char *cp = xbp->xb_bufp + start_offset;
2982 int len = xbp->xb_curp - cp;
2983 const char *newstr = NULL;
2986 * The plural flag asks us to look backwards at the last numeric
2987 * value rendered and disect the string into two pieces.
2989 if (flags & XFF_GT_PLURAL) {
2990 int n = xo_buf_find_last_number(xbp, start_offset);
2991 char *two = memchr(cp, (int) ',', len);
2993 xo_failure(xop, "no comma in plural gettext field: '%s'", cp);
2998 xo_failure(xop, "nothing before comma in plural gettext "
3003 if (two == xbp->xb_curp) {
3004 xo_failure(xop, "nothing after comma in plural gettext "
3010 if (flags & XFF_GT_FIELD) {
3011 newstr = xo_dngettext(xop, cp, two, n);
3013 /* Don't do a gettext() look up, just get the plural form */
3014 newstr = (n == 1) ? cp : two;
3018 * If we returned the first string, optimize a bit by
3019 * backing up over comma
3022 xbp->xb_curp = two - 1; /* One for comma */
3024 * If the caller wanted UTF8, we're done; nothing changed,
3025 * but we need to count the columns used.
3027 if (need_enc == XF_ENC_UTF8)
3028 return xo_count_utf8_cols(cp, xbp->xb_curp - cp);
3032 /* The simple case (singular) */
3033 newstr = xo_dgettext(xop, cp);
3036 /* If the caller wanted UTF8, we're done; nothing changed */
3037 if (need_enc == XF_ENC_UTF8)
3043 * Since the new string string might be in gettext's buffer or
3044 * in the buffer (as the plural form), we make a copy.
3046 int nlen = strlen(newstr);
3047 char *newcopy = alloca(nlen + 1);
3048 memcpy(newcopy, newstr, nlen + 1);
3050 xbp->xb_curp = xbp->xb_bufp + start_offset; /* Reset the buffer */
3051 return xo_format_string_direct(xop, xbp, flags, NULL, newcopy, nlen, 0,
3052 need_enc, XF_ENC_UTF8);
3056 xo_data_append_content (xo_handle_t *xop, const char *str, int len,
3057 xo_xff_flags_t flags)
3060 int need_enc = xo_needed_encoding(xop);
3061 int start_offset = xo_buf_offset(&xop->xo_data);
3063 cols = xo_format_string_direct(xop, &xop->xo_data, XFF_UNESCAPE | flags,
3065 need_enc, XF_ENC_UTF8);
3066 if (flags & XFF_GT_FLAGS)
3067 cols = xo_format_gettext(xop, flags, start_offset, cols, need_enc);
3069 if (XOF_ISSET(xop, XOF_COLUMNS))
3070 xop->xo_columns += cols;
3071 if (XOIF_ISSET(xop, XOIF_ANCHOR))
3072 xop->xo_anchor_columns += cols;
3076 xo_bump_width (xo_format_t *xfp, int digit)
3078 int *ip = &xfp->xf_width[xfp->xf_dots];
3080 *ip = ((*ip > 0) ? *ip : 0) * 10 + digit;
3084 xo_trim_ws (xo_buffer_t *xbp, int len)
3089 /* First trim leading space */
3090 for (cp = sp = xbp->xb_curp, ep = cp + len; cp < ep; cp++) {
3098 memmove(sp, cp, len);
3101 /* Then trim off the end */
3102 for (cp = xbp->xb_curp, sp = ep = cp + len; cp < ep; ep--) {
3117 * Interface to format a single field. The arguments are in xo_vap,
3118 * and the format is in 'fmt'. If 'xbp' is null, we use xop->xo_data;
3119 * this is the most common case.
3122 xo_do_format_field (xo_handle_t *xop, xo_buffer_t *xbp,
3123 const char *fmt, int flen, xo_xff_flags_t flags)
3126 const char *cp, *ep, *sp, *xp = NULL;
3128 int style = (flags & XFF_XML) ? XO_STYLE_XML : xo_style(xop);
3129 unsigned make_output = !(flags & XFF_NO_OUTPUT);
3130 int need_enc = xo_needed_encoding(xop);
3131 int real_need_enc = need_enc;
3132 int old_cols = xop->xo_columns;
3134 /* The gettext interface is UTF-8, so we'll need that for now */
3135 if (flags & XFF_GT_FIELD)
3136 need_enc = XF_ENC_UTF8;
3139 xbp = &xop->xo_data;
3141 unsigned start_offset = xo_buf_offset(xbp);
3143 for (cp = fmt, ep = fmt + flen; cp < ep; cp++) {
3145 * Since we're starting a new field, save the starting offset.
3146 * We'll need this later for field-related operations.
3154 if (*cp == '\\' && cp[1] != '\0')
3158 } if (cp + 1 < ep && cp[1] == '%') {
3165 cols = xo_format_string_direct(xop, xbp, flags | XFF_UNESCAPE,
3166 NULL, xp, cp - xp, -1,
3167 need_enc, XF_ENC_UTF8);
3168 if (XOF_ISSET(xop, XOF_COLUMNS))
3169 xop->xo_columns += cols;
3170 if (XOIF_ISSET(xop, XOIF_ANCHOR))
3171 xop->xo_anchor_columns += cols;
3177 bzero(&xf, sizeof(xf));
3178 xf.xf_leading_zero = -1;
3179 xf.xf_width[0] = xf.xf_width[1] = xf.xf_width[2] = -1;
3182 * "%@" starts an XO-specific set of flags:
3183 * @X@ - XML-only field; ignored if style isn't XML
3186 for (cp += 2; cp < ep; cp++) {
3192 * '*' means there's a "%*.*s" value in vap that
3195 if (!XOF_ISSET(xop, XOF_NO_VA_ARG))
3196 va_arg(xop->xo_vap, int);
3201 /* Hidden fields are only visible to JSON and XML */
3202 if (XOF_ISSET(xop, XFF_ENCODE_ONLY)) {
3203 if (style != XO_STYLE_XML
3204 && !xo_style_is_encoding(xop))
3206 } else if (XOF_ISSET(xop, XFF_DISPLAY_ONLY)) {
3207 if (style != XO_STYLE_TEXT
3208 && xo_style(xop) != XO_STYLE_HTML)
3216 * Looking at one piece of a format; find the end and
3217 * call snprintf. Then advance xo_vap on our own.
3219 * Note that 'n', 'v', and '$' are not supported.
3221 sp = cp; /* Save start pointer */
3222 for (cp += 1; cp < ep; cp++) {
3225 else if (*cp == 'h')
3227 else if (*cp == 'j')
3229 else if (*cp == 't')
3231 else if (*cp == 'z')
3233 else if (*cp == 'q')
3235 else if (*cp == '.') {
3236 if (++xf.xf_dots >= XF_WIDTH_NUM) {
3237 xo_failure(xop, "Too many dots in format: '%s'", fmt);
3240 } else if (*cp == '-')
3241 xf.xf_seen_minus = 1;
3242 else if (isdigit((int) *cp)) {
3243 if (xf.xf_leading_zero < 0)
3244 xf.xf_leading_zero = (*cp == '0');
3245 xo_bump_width(&xf, *cp - '0');
3246 } else if (*cp == '*') {
3248 xf.xf_star[xf.xf_dots] = 1;
3249 } else if (strchr("diouxXDOUeEfFgGaAcCsSpm", *cp) != NULL)
3251 else if (*cp == 'n' || *cp == 'v') {
3252 xo_failure(xop, "unsupported format: '%s'", fmt);
3258 xo_failure(xop, "field format missing format character: %s",
3263 if (!XOF_ISSET(xop, XOF_NO_VA_ARG)) {
3264 if (*cp == 's' || *cp == 'S') {
3265 /* Handle "%*.*.*s" */
3267 for (s = 0; s < XF_WIDTH_NUM; s++) {
3268 if (xf.xf_star[s]) {
3269 xf.xf_width[s] = va_arg(xop->xo_vap, int);
3271 /* Normalize a negative width value */
3272 if (xf.xf_width[s] < 0) {
3274 xf.xf_width[0] = -xf.xf_width[0];
3275 xf.xf_seen_minus = 1;
3277 xf.xf_width[s] = -1; /* Ignore negative values */
3284 /* If no max is given, it defaults to size */
3285 if (xf.xf_width[XF_WIDTH_MAX] < 0 && xf.xf_width[XF_WIDTH_SIZE] >= 0)
3286 xf.xf_width[XF_WIDTH_MAX] = xf.xf_width[XF_WIDTH_SIZE];
3288 if (xf.xf_fc == 'D' || xf.xf_fc == 'O' || xf.xf_fc == 'U')
3292 xo_buffer_t *fbp = &xop->xo_fmt;
3293 int len = cp - sp + 1;
3294 if (!xo_buf_has_room(fbp, len + 1))
3297 char *newfmt = fbp->xb_curp;
3298 memcpy(newfmt, sp, len);
3299 newfmt[0] = '%'; /* If we skipped over a "%@...@s" format */
3303 * Bad news: our strings are UTF-8, but the stock printf
3304 * functions won't handle field widths for wide characters
3305 * correctly. So we have to handle this ourselves.
3307 if (xop->xo_formatter == NULL
3308 && (xf.xf_fc == 's' || xf.xf_fc == 'S'
3309 || xf.xf_fc == 'm')) {
3311 xf.xf_enc = (xf.xf_fc == 'm') ? XF_ENC_UTF8
3312 : (xf.xf_lflag || (xf.xf_fc == 'S')) ? XF_ENC_WIDE
3313 : xf.xf_hflag ? XF_ENC_LOCALE : XF_ENC_UTF8;
3315 rc = xo_format_string(xop, xbp, flags, &xf);
3317 if ((flags & XFF_TRIM_WS) && xo_style_is_encoding(xop))
3318 rc = xo_trim_ws(xbp, rc);
3321 int columns = rc = xo_vsnprintf(xop, xbp, newfmt, xop->xo_vap);
3324 * For XML and HTML, we need "&<>" processing; for JSON,
3325 * it's quotes. Text gets nothing.
3329 if (flags & XFF_TRIM_WS)
3330 columns = rc = xo_trim_ws(xbp, rc);
3333 rc = xo_escape_xml(xbp, rc, (flags & XFF_ATTR));
3337 if (flags & XFF_TRIM_WS)
3338 columns = rc = xo_trim_ws(xbp, rc);
3339 rc = xo_escape_json(xbp, rc, 0);
3342 case XO_STYLE_SDPARAMS:
3343 if (flags & XFF_TRIM_WS)
3344 columns = rc = xo_trim_ws(xbp, rc);
3345 rc = xo_escape_sdparams(xbp, rc, 0);
3348 case XO_STYLE_ENCODER:
3349 if (flags & XFF_TRIM_WS)
3350 columns = rc = xo_trim_ws(xbp, rc);
3355 * We can assume all the non-%s data we've
3356 * added is ASCII, so the columns and bytes are the
3357 * same. xo_format_string handles all the fancy
3358 * string conversions and updates xo_anchor_columns
3361 if (XOF_ISSET(xop, XOF_COLUMNS))
3362 xop->xo_columns += columns;
3363 if (XOIF_ISSET(xop, XOIF_ANCHOR))
3364 xop->xo_anchor_columns += columns;
3371 * Now for the tricky part: we need to move the argument pointer
3372 * along by the amount needed.
3374 if (!XOF_ISSET(xop, XOF_NO_VA_ARG)) {
3376 if (xf.xf_fc == 's' ||xf.xf_fc == 'S') {
3378 * The 'S' and 's' formats are normally handled in
3379 * xo_format_string, but if we skipped it, then we
3383 va_arg(xop->xo_vap, char *);
3385 } else if (xf.xf_fc == 'm') {
3386 /* Nothing on the stack for "%m" */
3390 for (s = 0; s < XF_WIDTH_NUM; s++) {
3392 va_arg(xop->xo_vap, int);
3395 if (strchr("diouxXDOU", xf.xf_fc) != NULL) {
3396 if (xf.xf_hflag > 1) {
3397 va_arg(xop->xo_vap, int);
3399 } else if (xf.xf_hflag > 0) {
3400 va_arg(xop->xo_vap, int);
3402 } else if (xf.xf_lflag > 1) {
3403 va_arg(xop->xo_vap, unsigned long long);
3405 } else if (xf.xf_lflag > 0) {
3406 va_arg(xop->xo_vap, unsigned long);
3408 } else if (xf.xf_jflag > 0) {
3409 va_arg(xop->xo_vap, intmax_t);
3411 } else if (xf.xf_tflag > 0) {
3412 va_arg(xop->xo_vap, ptrdiff_t);
3414 } else if (xf.xf_zflag > 0) {
3415 va_arg(xop->xo_vap, size_t);
3417 } else if (xf.xf_qflag > 0) {
3418 va_arg(xop->xo_vap, quad_t);
3421 va_arg(xop->xo_vap, int);
3423 } else if (strchr("eEfFgGaA", xf.xf_fc) != NULL)
3425 va_arg(xop->xo_vap, long double);
3427 va_arg(xop->xo_vap, double);
3429 else if (xf.xf_fc == 'C' || (xf.xf_fc == 'c' && xf.xf_lflag))
3430 va_arg(xop->xo_vap, wint_t);
3432 else if (xf.xf_fc == 'c')
3433 va_arg(xop->xo_vap, int);
3435 else if (xf.xf_fc == 'p')
3436 va_arg(xop->xo_vap, void *);
3443 cols = xo_format_string_direct(xop, xbp, flags | XFF_UNESCAPE,
3444 NULL, xp, cp - xp, -1,
3445 need_enc, XF_ENC_UTF8);
3447 if (XOF_ISSET(xop, XOF_COLUMNS))
3448 xop->xo_columns += cols;
3449 if (XOIF_ISSET(xop, XOIF_ANCHOR))
3450 xop->xo_anchor_columns += cols;
3456 if (flags & XFF_GT_FLAGS) {
3458 * Handle gettext()ing the field by looking up the value
3459 * and then copying it in, while converting to locale, if
3462 int new_cols = xo_format_gettext(xop, flags, start_offset,
3463 old_cols, real_need_enc);
3465 if (XOF_ISSET(xop, XOF_COLUMNS))
3466 xop->xo_columns += new_cols - old_cols;
3467 if (XOIF_ISSET(xop, XOIF_ANCHOR))
3468 xop->xo_anchor_columns += new_cols - old_cols;
3475 xo_fix_encoding (xo_handle_t *xop UNUSED, char *encoding)
3477 char *cp = encoding;
3479 if (cp[0] != '%' || !isdigit((int) cp[1]))
3482 for (cp += 2; *cp; cp++) {
3483 if (!isdigit((int) *cp))
3494 xo_color_append_html (xo_handle_t *xop)
3497 * If the color buffer has content, we add it now. It's already
3498 * prebuilt and ready, since we want to add it to every <div>.
3500 if (!xo_buf_is_empty(&xop->xo_color_buf)) {
3501 xo_buffer_t *xbp = &xop->xo_color_buf;
3503 xo_data_append(xop, xbp->xb_bufp, xbp->xb_curp - xbp->xb_bufp);
3508 * A wrapper for humanize_number that autoscales, since the
3509 * HN_AUTOSCALE flag scales as needed based on the size of
3510 * the output buffer, not the size of the value. I also
3511 * wish HN_DECIMAL was more imperative, without the <10
3512 * test. But the boat only goes where we want when we hold
3513 * the rudder, so xo_humanize fixes part of the problem.
3516 xo_humanize (char *buf, int len, uint64_t value, int flags)
3521 uint64_t left = value;
3523 if (flags & HN_DIVISOR_1000) {
3524 for ( ; left; scale++)
3527 for ( ; left; scale++)
3533 return xo_humanize_number(buf, len, value, "", scale, flags);
3537 * This is an area where we can save information from the handle for
3538 * later restoration. We need to know what data was rendered to know
3539 * what needs cleaned up.
3541 typedef struct xo_humanize_save_s {
3542 unsigned xhs_offset; /* Saved xo_offset */
3543 unsigned xhs_columns; /* Saved xo_columns */
3544 unsigned xhs_anchor_columns; /* Saved xo_anchor_columns */
3545 } xo_humanize_save_t;
3548 * Format a "humanized" value for a numeric, meaning something nice
3549 * like "44M" instead of "44470272". We autoscale, choosing the
3550 * most appropriate value for K/M/G/T/P/E based on the value given.
3553 xo_format_humanize (xo_handle_t *xop, xo_buffer_t *xbp,
3554 xo_humanize_save_t *savep, xo_xff_flags_t flags)
3556 if (XOF_ISSET(xop, XOF_NO_HUMANIZE))
3559 unsigned end_offset = xbp->xb_curp - xbp->xb_bufp;
3560 if (end_offset == savep->xhs_offset) /* Huh? Nothing to render */
3564 * We have a string that's allegedly a number. We want to
3565 * humanize it, which means turning it back into a number
3566 * and calling xo_humanize_number on it.
3571 xo_buf_append(xbp, "", 1); /* NUL-terminate it */
3573 value = strtoull(xbp->xb_bufp + savep->xhs_offset, &ep, 0);
3574 if (!(value == ULLONG_MAX && errno == ERANGE)
3575 && (ep != xbp->xb_bufp + savep->xhs_offset)) {
3577 * There are few values where humanize_number needs
3578 * more bytes than the original value. I've used
3579 * 10 as a rectal number to cover those scenarios.
3581 if (xo_buf_has_room(xbp, 10)) {
3582 xbp->xb_curp = xbp->xb_bufp + savep->xhs_offset;
3585 int left = (xbp->xb_bufp + xbp->xb_size) - xbp->xb_curp;
3586 int hn_flags = HN_NOSPACE; /* On by default */
3588 if (flags & XFF_HN_SPACE)
3589 hn_flags &= ~HN_NOSPACE;
3591 if (flags & XFF_HN_DECIMAL)
3592 hn_flags |= HN_DECIMAL;
3594 if (flags & XFF_HN_1000)
3595 hn_flags |= HN_DIVISOR_1000;
3597 rc = xo_humanize(xbp->xb_curp,
3598 left, value, hn_flags);
3601 xop->xo_columns = savep->xhs_columns + rc;
3602 xop->xo_anchor_columns = savep->xhs_anchor_columns + rc;
3609 xo_buf_append_div (xo_handle_t *xop, const char *class, xo_xff_flags_t flags,
3610 const char *name, int nlen,
3611 const char *value, int vlen,
3612 const char *encoding, int elen)
3614 static char div_start[] = "<div class=\"";
3615 static char div_tag[] = "\" data-tag=\"";
3616 static char div_xpath[] = "\" data-xpath=\"";
3617 static char div_key[] = "\" data-key=\"key";
3618 static char div_end[] = "\">";
3619 static char div_close[] = "</div>";
3621 /* The encoding format defaults to the normal format */
3622 if (encoding == NULL) {
3623 char *enc = alloca(vlen + 1);
3624 memcpy(enc, value, vlen);
3626 encoding = xo_fix_encoding(xop, enc);
3627 elen = strlen(encoding);
3631 * To build our XPath predicate, we need to save the va_list before
3632 * we format our data, and then restore it before we format the
3634 * Display-only keys implies that we've got an encode-only key
3635 * elsewhere, so we don't use them from making predicates.
3637 int need_predidate =
3638 (name && (flags & XFF_KEY) && !(flags & XFF_DISPLAY_ONLY)
3639 && XOF_ISSET(xop, XOF_XPATH));
3641 if (need_predidate) {
3644 va_copy(va_local, xop->xo_vap);
3645 if (xop->xo_checkpointer)
3646 xop->xo_checkpointer(xop, xop->xo_vap, 0);
3649 * Build an XPath predicate expression to match this key.
3650 * We use the format buffer.
3652 xo_buffer_t *pbp = &xop->xo_predicate;
3653 pbp->xb_curp = pbp->xb_bufp; /* Restart buffer */
3655 xo_buf_append(pbp, "[", 1);
3656 xo_buf_escape(xop, pbp, name, nlen, 0);
3657 if (XOF_ISSET(xop, XOF_PRETTY))
3658 xo_buf_append(pbp, " = '", 4);
3660 xo_buf_append(pbp, "='", 2);
3662 xo_xff_flags_t pflags = flags | XFF_XML | XFF_ATTR;
3663 pflags &= ~(XFF_NO_OUTPUT | XFF_ENCODE_ONLY);
3664 xo_do_format_field(xop, pbp, encoding, elen, pflags);
3666 xo_buf_append(pbp, "']", 2);
3668 /* Now we record this predicate expression in the stack */
3669 xo_stack_t *xsp = &xop->xo_stack[xop->xo_depth];
3670 int olen = xsp->xs_keys ? strlen(xsp->xs_keys) : 0;
3671 int dlen = pbp->xb_curp - pbp->xb_bufp;
3673 char *cp = xo_realloc(xsp->xs_keys, olen + dlen + 1);
3675 memcpy(cp + olen, pbp->xb_bufp, dlen);
3676 cp[olen + dlen] = '\0';
3680 /* Now we reset the xo_vap as if we were never here */
3681 va_end(xop->xo_vap);
3682 va_copy(xop->xo_vap, va_local);
3684 if (xop->xo_checkpointer)
3685 xop->xo_checkpointer(xop, xop->xo_vap, 1);
3688 if (flags & XFF_ENCODE_ONLY) {
3690 * Even if this is encode-only, we need to go thru the
3691 * work of formatting it to make sure the args are cleared
3694 xo_do_format_field(xop, NULL, encoding, elen,
3695 flags | XFF_NO_OUTPUT);
3699 xo_line_ensure_open(xop, 0);
3701 if (XOF_ISSET(xop, XOF_PRETTY))
3702 xo_buf_indent(xop, xop->xo_indent_by);
3704 xo_data_append(xop, div_start, sizeof(div_start) - 1);
3705 xo_data_append(xop, class, strlen(class));
3708 * If the color buffer has content, we add it now. It's already
3709 * prebuilt and ready, since we want to add it to every <div>.
3711 if (!xo_buf_is_empty(&xop->xo_color_buf)) {
3712 xo_buffer_t *xbp = &xop->xo_color_buf;
3714 xo_data_append(xop, xbp->xb_bufp, xbp->xb_curp - xbp->xb_bufp);
3718 xo_data_append(xop, div_tag, sizeof(div_tag) - 1);
3719 xo_data_escape(xop, name, nlen);
3722 * Save the offset at which we'd place units. See xo_format_units.
3724 if (XOF_ISSET(xop, XOF_UNITS)) {
3725 XOIF_SET(xop, XOIF_UNITS_PENDING);
3727 * Note: We need the '+1' here because we know we've not
3728 * added the closing quote. We add one, knowing the quote
3729 * will be added shortly.
3731 xop->xo_units_offset =
3732 xop->xo_data.xb_curp -xop->xo_data.xb_bufp + 1;
3735 if (XOF_ISSET(xop, XOF_XPATH)) {
3739 xo_data_append(xop, div_xpath, sizeof(div_xpath) - 1);
3740 if (xop->xo_leading_xpath)
3741 xo_data_append(xop, xop->xo_leading_xpath,
3742 strlen(xop->xo_leading_xpath));
3744 for (i = 0; i <= xop->xo_depth; i++) {
3745 xsp = &xop->xo_stack[i];
3746 if (xsp->xs_name == NULL)
3750 * XSS_OPEN_LIST and XSS_OPEN_LEAF_LIST stack frames
3751 * are directly under XSS_OPEN_INSTANCE frames so we
3752 * don't need to put these in our XPath expressions.
3754 if (xsp->xs_state == XSS_OPEN_LIST
3755 || xsp->xs_state == XSS_OPEN_LEAF_LIST)
3758 xo_data_append(xop, "/", 1);
3759 xo_data_escape(xop, xsp->xs_name, strlen(xsp->xs_name));
3761 /* Don't show keys for the key field */
3762 if (i != xop->xo_depth || !(flags & XFF_KEY))
3763 xo_data_append(xop, xsp->xs_keys, strlen(xsp->xs_keys));
3767 xo_data_append(xop, "/", 1);
3768 xo_data_escape(xop, name, nlen);
3771 if (XOF_ISSET(xop, XOF_INFO) && xop->xo_info) {
3772 static char in_type[] = "\" data-type=\"";
3773 static char in_help[] = "\" data-help=\"";
3775 xo_info_t *xip = xo_info_find(xop, name, nlen);
3778 xo_data_append(xop, in_type, sizeof(in_type) - 1);
3779 xo_data_escape(xop, xip->xi_type, strlen(xip->xi_type));
3782 xo_data_append(xop, in_help, sizeof(in_help) - 1);
3783 xo_data_escape(xop, xip->xi_help, strlen(xip->xi_help));
3788 if ((flags & XFF_KEY) && XOF_ISSET(xop, XOF_KEYS))
3789 xo_data_append(xop, div_key, sizeof(div_key) - 1);
3792 xo_buffer_t *xbp = &xop->xo_data;
3793 unsigned base_offset = xbp->xb_curp - xbp->xb_bufp;
3795 xo_data_append(xop, div_end, sizeof(div_end) - 1);
3797 xo_humanize_save_t save; /* Save values for humanizing logic */
3799 save.xhs_offset = xbp->xb_curp - xbp->xb_bufp;
3800 save.xhs_columns = xop->xo_columns;
3801 save.xhs_anchor_columns = xop->xo_anchor_columns;
3803 xo_do_format_field(xop, NULL, value, vlen, flags);
3805 if (flags & XFF_HUMANIZE) {
3807 * Unlike text style, we want to retain the original value and
3808 * stuff it into the "data-number" attribute.
3810 static const char div_number[] = "\" data-number=\"";
3811 int div_len = sizeof(div_number) - 1;
3813 unsigned end_offset = xbp->xb_curp - xbp->xb_bufp;
3814 int olen = end_offset - save.xhs_offset;
3816 char *cp = alloca(olen + 1);
3817 memcpy(cp, xbp->xb_bufp + save.xhs_offset, olen);
3820 xo_format_humanize(xop, xbp, &save, flags);
3822 if (xo_buf_has_room(xbp, div_len + olen)) {
3823 unsigned new_offset = xbp->xb_curp - xbp->xb_bufp;
3826 /* Move the humanized string off to the left */
3827 memmove(xbp->xb_bufp + base_offset + div_len + olen,
3828 xbp->xb_bufp + base_offset, new_offset - base_offset);
3830 /* Copy the data_number attribute name */
3831 memcpy(xbp->xb_bufp + base_offset, div_number, div_len);
3833 /* Copy the original long value */
3834 memcpy(xbp->xb_bufp + base_offset + div_len, cp, olen);
3835 xbp->xb_curp += div_len + olen;
3839 xo_data_append(xop, div_close, sizeof(div_close) - 1);
3841 if (XOF_ISSET(xop, XOF_PRETTY))
3842 xo_data_append(xop, "\n", 1);
3846 xo_format_text (xo_handle_t *xop, const char *str, int len)
3848 switch (xo_style(xop)) {
3850 xo_buf_append_locale(xop, &xop->xo_data, str, len);
3854 xo_buf_append_div(xop, "text", 0, NULL, 0, str, len, NULL, 0);
3860 xo_format_title (xo_handle_t *xop, xo_field_info_t *xfip,
3861 const char *str, unsigned len)
3863 const char *fmt = xfip->xfi_format;
3864 unsigned flen = xfip->xfi_flen;
3865 xo_xff_flags_t flags = xfip->xfi_flags;
3867 static char div_open[] = "<div class=\"title";
3868 static char div_middle[] = "\">";
3869 static char div_close[] = "</div>";
3876 switch (xo_style(xop)) {
3879 case XO_STYLE_SDPARAMS:
3880 case XO_STYLE_ENCODER:
3882 * Even though we don't care about text, we need to do
3883 * enough parsing work to skip over the right bits of xo_vap.
3886 xo_do_format_field(xop, NULL, fmt, flen, flags | XFF_NO_OUTPUT);
3890 xo_buffer_t *xbp = &xop->xo_data;
3891 int start = xbp->xb_curp - xbp->xb_bufp;
3892 int left = xbp->xb_size - start;
3895 if (xo_style(xop) == XO_STYLE_HTML) {
3896 xo_line_ensure_open(xop, 0);
3897 if (XOF_ISSET(xop, XOF_PRETTY))
3898 xo_buf_indent(xop, xop->xo_indent_by);
3899 xo_buf_append(&xop->xo_data, div_open, sizeof(div_open) - 1);
3900 xo_color_append_html(xop);
3901 xo_buf_append(&xop->xo_data, div_middle, sizeof(div_middle) - 1);
3904 start = xbp->xb_curp - xbp->xb_bufp; /* Reset start */
3906 char *newfmt = alloca(flen + 1);
3907 memcpy(newfmt, fmt, flen);
3908 newfmt[flen] = '\0';
3910 /* If len is non-zero, the format string apply to the name */
3911 char *newstr = alloca(len + 1);
3912 memcpy(newstr, str, len);
3915 if (newstr[len - 1] == 's') {
3918 rc = snprintf(NULL, 0, newfmt, newstr);
3921 * We have to do this the hard way, since we might need
3924 bp = alloca(rc + 1);
3925 rc = snprintf(bp, rc + 1, newfmt, newstr);
3927 xo_data_append_content(xop, bp, rc, flags);
3932 rc = snprintf(xbp->xb_curp, left, newfmt, newstr);
3934 if (!xo_buf_has_room(xbp, rc))
3936 left = xbp->xb_size - (xbp->xb_curp - xbp->xb_bufp);
3937 rc = snprintf(xbp->xb_curp, left, newfmt, newstr);
3941 if (XOF_ISSET(xop, XOF_COLUMNS))
3942 xop->xo_columns += rc;
3943 if (XOIF_ISSET(xop, XOIF_ANCHOR))
3944 xop->xo_anchor_columns += rc;
3949 xo_do_format_field(xop, NULL, fmt, flen, flags);
3951 /* xo_do_format_field moved curp, so we need to reset it */
3952 rc = xbp->xb_curp - (xbp->xb_bufp + start);
3953 xbp->xb_curp = xbp->xb_bufp + start;
3956 /* If we're styling HTML, then we need to escape it */
3957 if (xo_style(xop) == XO_STYLE_HTML) {
3958 rc = xo_escape_xml(xbp, rc, 0);
3965 if (xo_style(xop) == XO_STYLE_HTML) {
3966 xo_data_append(xop, div_close, sizeof(div_close) - 1);
3967 if (XOF_ISSET(xop, XOF_PRETTY))
3968 xo_data_append(xop, "\n", 1);
3973 xo_format_prep (xo_handle_t *xop, xo_xff_flags_t flags)
3975 if (xop->xo_stack[xop->xo_depth].xs_flags & XSF_NOT_FIRST) {
3976 xo_data_append(xop, ",", 1);
3977 if (!(flags & XFF_LEAF_LIST) && XOF_ISSET(xop, XOF_PRETTY))
3978 xo_data_append(xop, "\n", 1);
3980 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
3984 /* Useful debugging function */
3986 xo_arg (xo_handle_t *xop);
3988 xo_arg (xo_handle_t *xop)
3990 xop = xo_default(xop);
3991 fprintf(stderr, "0x%x", va_arg(xop->xo_vap, unsigned));
3996 xo_format_value (xo_handle_t *xop, const char *name, int nlen,
3997 const char *format, int flen,
3998 const char *encoding, int elen, xo_xff_flags_t flags)
4000 int pretty = XOF_ISSET(xop, XOF_PRETTY);
4004 * Before we emit a value, we need to know that the frame is ready.
4006 xo_stack_t *xsp = &xop->xo_stack[xop->xo_depth];
4008 if (flags & XFF_LEAF_LIST) {
4010 * Check if we've already started to emit normal leafs
4011 * or if we're not in a leaf list.
4013 if ((xsp->xs_flags & (XSF_EMIT | XSF_EMIT_KEY))
4014 || !(xsp->xs_flags & XSF_EMIT_LEAF_LIST)) {
4015 char nbuf[nlen + 1];
4016 memcpy(nbuf, name, nlen);
4019 int rc = xo_transition(xop, 0, nbuf, XSS_EMIT_LEAF_LIST);
4021 flags |= XFF_DISPLAY_ONLY | XFF_ENCODE_ONLY;
4023 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_EMIT_LEAF_LIST;
4026 xsp = &xop->xo_stack[xop->xo_depth];
4028 name = xsp->xs_name;
4029 nlen = strlen(name);
4032 } else if (flags & XFF_KEY) {
4033 /* Emitting a 'k' (key) field */
4034 if ((xsp->xs_flags & XSF_EMIT) && !(flags & XFF_DISPLAY_ONLY)) {
4035 xo_failure(xop, "key field emitted after normal value field: '%.*s'",
4038 } else if (!(xsp->xs_flags & XSF_EMIT_KEY)) {
4039 char nbuf[nlen + 1];
4040 memcpy(nbuf, name, nlen);
4043 int rc = xo_transition(xop, 0, nbuf, XSS_EMIT);
4045 flags |= XFF_DISPLAY_ONLY | XFF_ENCODE_ONLY;
4047 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_EMIT_KEY;
4049 xsp = &xop->xo_stack[xop->xo_depth];
4050 xsp->xs_flags |= XSF_EMIT_KEY;
4054 /* Emitting a normal value field */
4055 if ((xsp->xs_flags & XSF_EMIT_LEAF_LIST)
4056 || !(xsp->xs_flags & XSF_EMIT)) {
4057 char nbuf[nlen + 1];
4058 memcpy(nbuf, name, nlen);
4061 int rc = xo_transition(xop, 0, nbuf, XSS_EMIT);
4063 flags |= XFF_DISPLAY_ONLY | XFF_ENCODE_ONLY;
4065 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_EMIT;
4067 xsp = &xop->xo_stack[xop->xo_depth];
4068 xsp->xs_flags |= XSF_EMIT;
4072 xo_buffer_t *xbp = &xop->xo_data;
4073 xo_humanize_save_t save; /* Save values for humanizing logic */
4075 switch (xo_style(xop)) {
4077 if (flags & XFF_ENCODE_ONLY)
4078 flags |= XFF_NO_OUTPUT;
4080 save.xhs_offset = xbp->xb_curp - xbp->xb_bufp;
4081 save.xhs_columns = xop->xo_columns;
4082 save.xhs_anchor_columns = xop->xo_anchor_columns;
4084 xo_do_format_field(xop, NULL, format, flen, flags);
4086 if (flags & XFF_HUMANIZE)
4087 xo_format_humanize(xop, xbp, &save, flags);
4091 if (flags & XFF_ENCODE_ONLY)
4092 flags |= XFF_NO_OUTPUT;
4094 xo_buf_append_div(xop, "data", flags, name, nlen,
4095 format, flen, encoding, elen);
4100 * Even though we're not making output, we still need to
4101 * let the formatting code handle the va_arg popping.
4103 if (flags & XFF_DISPLAY_ONLY) {
4104 flags |= XFF_NO_OUTPUT;
4105 xo_do_format_field(xop, NULL, format, flen, flags);
4113 char *enc = alloca(flen + 1);
4114 memcpy(enc, format, flen);
4116 format = xo_fix_encoding(xop, enc);
4117 flen = strlen(format);
4121 static char missing[] = "missing-field-name";
4122 xo_failure(xop, "missing field name: %s", format);
4124 nlen = sizeof(missing) - 1;
4128 xo_buf_indent(xop, -1);
4129 xo_data_append(xop, "<", 1);
4130 xo_data_escape(xop, name, nlen);
4132 if (xop->xo_attrs.xb_curp != xop->xo_attrs.xb_bufp) {
4133 xo_data_append(xop, xop->xo_attrs.xb_bufp,
4134 xop->xo_attrs.xb_curp - xop->xo_attrs.xb_bufp);
4135 xop->xo_attrs.xb_curp = xop->xo_attrs.xb_bufp;
4139 * We indicate 'key' fields using the 'key' attribute. While
4140 * this is really committing the crime of mixing meta-data with
4141 * data, it's often useful. Especially when format meta-data is
4142 * difficult to come by.
4144 if ((flags & XFF_KEY) && XOF_ISSET(xop, XOF_KEYS)) {
4145 static char attr[] = " key=\"key\"";
4146 xo_data_append(xop, attr, sizeof(attr) - 1);
4150 * Save the offset at which we'd place units. See xo_format_units.
4152 if (XOF_ISSET(xop, XOF_UNITS)) {
4153 XOIF_SET(xop, XOIF_UNITS_PENDING);
4154 xop->xo_units_offset = xop->xo_data.xb_curp -xop->xo_data.xb_bufp;
4157 xo_data_append(xop, ">", 1);
4158 xo_do_format_field(xop, NULL, format, flen, flags);
4159 xo_data_append(xop, "</", 2);
4160 xo_data_escape(xop, name, nlen);
4161 xo_data_append(xop, ">", 1);
4163 xo_data_append(xop, "\n", 1);
4167 if (flags & XFF_DISPLAY_ONLY) {
4168 flags |= XFF_NO_OUTPUT;
4169 xo_do_format_field(xop, NULL, format, flen, flags);
4177 char *enc = alloca(flen + 1);
4178 memcpy(enc, format, flen);
4180 format = xo_fix_encoding(xop, enc);
4181 flen = strlen(format);
4184 int first = !(xop->xo_stack[xop->xo_depth].xs_flags & XSF_NOT_FIRST);
4186 xo_format_prep(xop, flags);
4188 if (flags & XFF_QUOTE)
4190 else if (flags & XFF_NOQUOTE)
4192 else if (flen == 0) {
4194 format = "true"; /* JSON encodes empty tags as a boolean true */
4196 } else if (strchr("diouxXDOUeEfFgGaAcCp", format[flen - 1]) == NULL)
4202 static char missing[] = "missing-field-name";
4203 xo_failure(xop, "missing field name: %s", format);
4205 nlen = sizeof(missing) - 1;
4208 if (flags & XFF_LEAF_LIST) {
4209 if (!first && pretty)
4210 xo_data_append(xop, "\n", 1);
4212 xo_buf_indent(xop, -1);
4215 xo_buf_indent(xop, -1);
4216 xo_data_append(xop, "\"", 1);
4218 xbp = &xop->xo_data;
4219 int off = xbp->xb_curp - xbp->xb_bufp;
4221 xo_data_escape(xop, name, nlen);
4223 if (XOF_ISSET(xop, XOF_UNDERSCORES)) {
4224 int now = xbp->xb_curp - xbp->xb_bufp;
4225 for ( ; off < now; off++)
4226 if (xbp->xb_bufp[off] == '-')
4227 xbp->xb_bufp[off] = '_';
4229 xo_data_append(xop, "\":", 2);
4231 xo_data_append(xop, " ", 1);
4235 xo_data_append(xop, "\"", 1);
4237 xo_do_format_field(xop, NULL, format, flen, flags);
4240 xo_data_append(xop, "\"", 1);
4243 case XO_STYLE_SDPARAMS:
4244 if (flags & XFF_DISPLAY_ONLY) {
4245 flags |= XFF_NO_OUTPUT;
4246 xo_do_format_field(xop, NULL, format, flen, flags);
4254 char *enc = alloca(flen + 1);
4255 memcpy(enc, format, flen);
4257 format = xo_fix_encoding(xop, enc);
4258 flen = strlen(format);
4262 static char missing[] = "missing-field-name";
4263 xo_failure(xop, "missing field name: %s", format);
4265 nlen = sizeof(missing) - 1;
4268 xo_data_escape(xop, name, nlen);
4269 xo_data_append(xop, "=\"", 2);
4270 xo_do_format_field(xop, NULL, format, flen, flags);
4271 xo_data_append(xop, "\" ", 2);
4274 case XO_STYLE_ENCODER:
4275 if (flags & XFF_DISPLAY_ONLY) {
4276 flags |= XFF_NO_OUTPUT;
4277 xo_do_format_field(xop, NULL, format, flen, flags);
4281 if (flags & XFF_QUOTE)
4283 else if (flags & XFF_NOQUOTE)
4285 else if (flen == 0) {
4287 format = "true"; /* JSON encodes empty tags as a boolean true */
4289 } else if (strchr("diouxXDOUeEfFgGaAcCp", format[flen - 1]) == NULL)
4298 char *enc = alloca(flen + 1);
4299 memcpy(enc, format, flen);
4301 format = xo_fix_encoding(xop, enc);
4302 flen = strlen(format);
4306 static char missing[] = "missing-field-name";
4307 xo_failure(xop, "missing field name: %s", format);
4309 nlen = sizeof(missing) - 1;
4312 unsigned name_offset = xo_buf_offset(&xop->xo_data);
4313 xo_data_append(xop, name, nlen);
4314 xo_data_append(xop, "", 1);
4316 unsigned value_offset = xo_buf_offset(&xop->xo_data);
4317 xo_do_format_field(xop, NULL, format, flen, flags);
4318 xo_data_append(xop, "", 1);
4320 xo_encoder_handle(xop, quote ? XO_OP_STRING : XO_OP_CONTENT,
4321 xo_buf_data(&xop->xo_data, name_offset),
4322 xo_buf_data(&xop->xo_data, value_offset));
4323 xo_buf_reset(&xop->xo_data);
4329 xo_set_gettext_domain (xo_handle_t *xop, xo_field_info_t *xfip,
4330 const char *str, unsigned len)
4332 const char *fmt = xfip->xfi_format;
4333 unsigned flen = xfip->xfi_flen;
4335 /* Start by discarding previous domain */
4336 if (xop->xo_gt_domain) {
4337 xo_free(xop->xo_gt_domain);
4338 xop->xo_gt_domain = NULL;
4341 /* An empty {G:} means no domainname */
4342 if (len == 0 && flen == 0)
4345 int start_offset = -1;
4346 if (len == 0 && flen != 0) {
4347 /* Need to do format the data to get the domainname from args */
4348 start_offset = xop->xo_data.xb_curp - xop->xo_data.xb_bufp;
4349 xo_do_format_field(xop, NULL, fmt, flen, 0);
4351 int end_offset = xop->xo_data.xb_curp - xop->xo_data.xb_bufp;
4352 len = end_offset - start_offset;
4353 str = xop->xo_data.xb_bufp + start_offset;
4356 xop->xo_gt_domain = xo_strndup(str, len);
4358 /* Reset the current buffer point to avoid emitting the name as output */
4359 if (start_offset >= 0)
4360 xop->xo_data.xb_curp = xop->xo_data.xb_bufp + start_offset;
4364 xo_format_content (xo_handle_t *xop, const char *class_name,
4365 const char *tag_name,
4366 const char *str, int len, const char *fmt, int flen,
4367 xo_xff_flags_t flags)
4369 switch (xo_style(xop)) {
4372 xo_data_append_content(xop, str, len, flags);
4374 xo_do_format_field(xop, NULL, fmt, flen, flags);
4383 xo_buf_append_div(xop, class_name, flags, NULL, 0, str, len, NULL, 0);
4388 case XO_STYLE_SDPARAMS:
4395 xo_open_container_h(xop, tag_name);
4396 xo_format_value(xop, "message", 7, str, len, NULL, 0, flags);
4397 xo_close_container_h(xop, tag_name);
4401 * Even though we don't care about labels, we need to do
4402 * enough parsing work to skip over the right bits of xo_vap.
4405 xo_do_format_field(xop, NULL, fmt, flen,
4406 flags | XFF_NO_OUTPUT);
4410 case XO_STYLE_ENCODER:
4412 xo_do_format_field(xop, NULL, fmt, flen,
4413 flags | XFF_NO_OUTPUT);
4418 static const char *xo_color_names[] = {
4419 "default", /* XO_COL_DEFAULT */
4420 "black", /* XO_COL_BLACK */
4421 "red", /* XO_CLOR_RED */
4422 "green", /* XO_COL_GREEN */
4423 "yellow", /* XO_COL_YELLOW */
4424 "blue", /* XO_COL_BLUE */
4425 "magenta", /* XO_COL_MAGENTA */
4426 "cyan", /* XO_COL_CYAN */
4427 "white", /* XO_COL_WHITE */
4432 xo_color_find (const char *str)
4436 for (i = 0; xo_color_names[i]; i++) {
4437 if (strcmp(xo_color_names[i], str) == 0)
4444 static const char *xo_effect_names[] = {
4445 "reset", /* XO_EFF_RESET */
4446 "normal", /* XO_EFF_NORMAL */
4447 "bold", /* XO_EFF_BOLD */
4448 "underline", /* XO_EFF_UNDERLINE */
4449 "inverse", /* XO_EFF_INVERSE */
4453 static const char *xo_effect_on_codes[] = {
4454 "0", /* XO_EFF_RESET */
4455 "0", /* XO_EFF_NORMAL */
4456 "1", /* XO_EFF_BOLD */
4457 "4", /* XO_EFF_UNDERLINE */
4458 "7", /* XO_EFF_INVERSE */
4464 * See comment below re: joy of terminal standards. These can
4465 * be use by just adding:
4466 * + if (newp->xoc_effects & bit)
4467 * code = xo_effect_on_codes[i];
4469 * + code = xo_effect_off_codes[i];
4470 * in xo_color_handle_text.
4472 static const char *xo_effect_off_codes[] = {
4473 "0", /* XO_EFF_RESET */
4474 "0", /* XO_EFF_NORMAL */
4475 "21", /* XO_EFF_BOLD */
4476 "24", /* XO_EFF_UNDERLINE */
4477 "27", /* XO_EFF_INVERSE */
4483 xo_effect_find (const char *str)
4487 for (i = 0; xo_effect_names[i]; i++) {
4488 if (strcmp(xo_effect_names[i], str) == 0)
4496 xo_colors_parse (xo_handle_t *xop, xo_colors_t *xocp, char *str)
4498 #ifdef LIBXO_TEXT_ONLY
4500 #endif /* LIBXO_TEXT_ONLY */
4502 char *cp, *ep, *np, *xp;
4503 int len = strlen(str);
4507 * Possible tokens: colors, bg-colors, effects, no-effects, "reset".
4509 for (cp = str, ep = cp + len - 1; cp && cp < ep; cp = np) {
4510 /* Trim leading whitespace */
4511 while (isspace((int) *cp))
4514 np = strchr(cp, ',');
4518 /* Trim trailing whitespace */
4519 xp = cp + strlen(cp) - 1;
4520 while (isspace(*xp) && xp > cp)
4523 if (cp[0] == 'f' && cp[1] == 'g' && cp[2] == '-') {
4524 rc = xo_color_find(cp + 3);
4528 xocp->xoc_col_fg = rc;
4530 } else if (cp[0] == 'b' && cp[1] == 'g' && cp[2] == '-') {
4531 rc = xo_color_find(cp + 3);
4534 xocp->xoc_col_bg = rc;
4536 } else if (cp[0] == 'n' && cp[1] == 'o' && cp[2] == '-') {
4537 rc = xo_effect_find(cp + 3);
4540 xocp->xoc_effects &= ~(1 << rc);
4543 rc = xo_effect_find(cp);
4546 xocp->xoc_effects |= 1 << rc;
4550 xocp->xoc_col_fg = xocp->xoc_col_bg = 0;
4551 /* Note: not "|=" since we want to wipe out the old value */
4552 xocp->xoc_effects = XO_EFF_RESET;
4556 xocp->xoc_effects &= ~(XO_EFF_BOLD | XO_EFF_UNDERLINE
4557 | XO_EFF_INVERSE | XO_EFF_NORMAL);
4564 if (XOF_ISSET(xop, XOF_WARN))
4565 xo_failure(xop, "unknown color/effect string detected: '%s'", cp);
4570 xo_colors_enabled (xo_handle_t *xop UNUSED)
4572 #ifdef LIBXO_TEXT_ONLY
4574 #else /* LIBXO_TEXT_ONLY */
4575 return XOF_ISSET(xop, XOF_COLOR);
4576 #endif /* LIBXO_TEXT_ONLY */
4580 xo_colors_handle_text (xo_handle_t *xop, xo_colors_t *newp)
4583 char *cp = buf, *ep = buf + sizeof(buf);
4585 xo_colors_t *oldp = &xop->xo_colors;
4586 const char *code = NULL;
4589 * Start the buffer with an escape. We don't want to add the '['
4590 * now, since we let xo_effect_text_add unconditionally add the ';'.
4591 * We'll replace the first ';' with a '[' when we're done.
4593 *cp++ = 0x1b; /* Escape */
4596 * Terminals were designed back in the age before "certainty" was
4597 * invented, when standards were more what you'd call "guidelines"
4598 * than actual rules. Anyway we can't depend on them to operate
4599 * correctly. So when display attributes are changed, we punt,
4600 * reseting them all and turning back on the ones we want to keep.
4601 * Longer, but should be completely reliable. Savvy?
4603 if (oldp->xoc_effects != (newp->xoc_effects & oldp->xoc_effects)) {
4604 newp->xoc_effects |= XO_EFF_RESET;
4605 oldp->xoc_effects = 0;
4608 for (i = 0, bit = 1; xo_effect_names[i]; i++, bit <<= 1) {
4609 if ((newp->xoc_effects & bit) == (oldp->xoc_effects & bit))
4612 code = xo_effect_on_codes[i];
4614 cp += snprintf(cp, ep - cp, ";%s", code);
4616 return; /* Should not occur */
4618 if (bit == XO_EFF_RESET) {
4619 /* Mark up the old value so we can detect current values as new */
4620 oldp->xoc_effects = 0;
4621 oldp->xoc_col_fg = oldp->xoc_col_bg = XO_COL_DEFAULT;
4625 if (newp->xoc_col_fg != oldp->xoc_col_fg) {
4626 cp += snprintf(cp, ep - cp, ";3%u",
4627 (newp->xoc_col_fg != XO_COL_DEFAULT)
4628 ? newp->xoc_col_fg - 1 : 9);
4631 if (newp->xoc_col_bg != oldp->xoc_col_bg) {
4632 cp += snprintf(cp, ep - cp, ";4%u",
4633 (newp->xoc_col_bg != XO_COL_DEFAULT)
4634 ? newp->xoc_col_bg - 1 : 9);
4637 if (cp - buf != 1 && cp < ep - 3) {
4638 buf[1] = '['; /* Overwrite leading ';' */
4641 xo_buf_append(&xop->xo_data, buf, cp - buf);
4646 xo_colors_handle_html (xo_handle_t *xop, xo_colors_t *newp)
4648 xo_colors_t *oldp = &xop->xo_colors;
4651 * HTML colors are mostly trivial: fill in xo_color_buf with
4652 * a set of class tags representing the colors and effects.
4655 /* If nothing changed, then do nothing */
4656 if (oldp->xoc_effects == newp->xoc_effects
4657 && oldp->xoc_col_fg == newp->xoc_col_fg
4658 && oldp->xoc_col_bg == newp->xoc_col_bg)
4662 xo_buffer_t *xbp = &xop->xo_color_buf;
4664 xo_buf_reset(xbp); /* We rebuild content after each change */
4666 for (i = 0, bit = 1; xo_effect_names[i]; i++, bit <<= 1) {
4667 if (!(newp->xoc_effects & bit))
4670 xo_buf_append_str(xbp, " effect-");
4671 xo_buf_append_str(xbp, xo_effect_names[i]);
4674 const char *fg = NULL;
4675 const char *bg = NULL;
4677 if (newp->xoc_col_fg != XO_COL_DEFAULT)
4678 fg = xo_color_names[newp->xoc_col_fg];
4679 if (newp->xoc_col_bg != XO_COL_DEFAULT)
4680 bg = xo_color_names[newp->xoc_col_bg];
4682 if (newp->xoc_effects & XO_EFF_INVERSE) {
4683 const char *tmp = fg;
4694 xo_buf_append_str(xbp, " color-fg-");
4695 xo_buf_append_str(xbp, fg);
4699 xo_buf_append_str(xbp, " color-bg-");
4700 xo_buf_append_str(xbp, bg);
4705 xo_format_colors (xo_handle_t *xop, xo_field_info_t *xfip,
4706 const char *str, unsigned len)
4708 const char *fmt = xfip->xfi_format;
4709 unsigned flen = xfip->xfi_flen;
4713 /* If the string is static and we've in an encoding style, bail */
4714 if (len != 0 && xo_style_is_encoding(xop))
4720 xo_buf_append(&xb, str, len);
4722 xo_do_format_field(xop, &xb, fmt, flen, 0);
4724 xo_buf_append(&xb, "reset", 6); /* Default if empty */
4726 if (xo_colors_enabled(xop)) {
4727 switch (xo_style(xop)) {
4730 xo_buf_append(&xb, "", 1);
4732 xo_colors_t xoc = xop->xo_colors;
4733 xo_colors_parse(xop, &xoc, xb.xb_bufp);
4735 if (xo_style(xop) == XO_STYLE_TEXT) {
4737 * Text mode means emitting the colors as ANSI character
4738 * codes. This will allow people who like colors to have
4739 * colors. The issue is, of course conflicting with the
4740 * user's perfectly reasonable color scheme. Which leads
4741 * to the hell of LSCOLORS, where even app need to have
4742 * customization hooks for adjusting colors. Instead we
4743 * provide a simpler-but-still-annoying answer where one
4744 * can map colors to other colors.
4746 xo_colors_handle_text(xop, &xoc);
4747 xoc.xoc_effects &= ~XO_EFF_RESET; /* After handling it */
4751 * HTML output is wrapped in divs, so the color information
4752 * must appear in every div until cleared. Most pathetic.
4755 xoc.xoc_effects &= ~XO_EFF_RESET; /* Before handling effects */
4756 xo_colors_handle_html(xop, &xoc);
4759 xop->xo_colors = xoc;
4764 case XO_STYLE_SDPARAMS:
4765 case XO_STYLE_ENCODER:
4767 * Nothing to do; we did all that work just to clear the stack of
4768 * formatting arguments.
4774 xo_buf_cleanup(&xb);
4778 xo_format_units (xo_handle_t *xop, xo_field_info_t *xfip,
4779 const char *str, unsigned len)
4781 const char *fmt = xfip->xfi_format;
4782 unsigned flen = xfip->xfi_flen;
4783 xo_xff_flags_t flags = xfip->xfi_flags;
4785 static char units_start_xml[] = " units=\"";
4786 static char units_start_html[] = " data-units=\"";
4788 if (!XOIF_ISSET(xop, XOIF_UNITS_PENDING)) {
4789 xo_format_content(xop, "units", NULL, str, len, fmt, flen, flags);
4793 xo_buffer_t *xbp = &xop->xo_data;
4794 int start = xop->xo_units_offset;
4795 int stop = xbp->xb_curp - xbp->xb_bufp;
4797 if (xo_style(xop) == XO_STYLE_XML)
4798 xo_buf_append(xbp, units_start_xml, sizeof(units_start_xml) - 1);
4799 else if (xo_style(xop) == XO_STYLE_HTML)
4800 xo_buf_append(xbp, units_start_html, sizeof(units_start_html) - 1);
4805 xo_data_escape(xop, str, len);
4807 xo_do_format_field(xop, NULL, fmt, flen, flags);
4809 xo_buf_append(xbp, "\"", 1);
4811 int now = xbp->xb_curp - xbp->xb_bufp;
4812 int delta = now - stop;
4813 if (delta <= 0) { /* Strange; no output to move */
4814 xbp->xb_curp = xbp->xb_bufp + stop; /* Reset buffer to prior state */
4819 * Now we're in it alright. We've need to insert the unit value
4820 * we just created into the right spot. We make a local copy,
4821 * move it and then insert our copy. We know there's room in the
4822 * buffer, since we're just moving this around.
4824 char *buf = alloca(delta);
4826 memcpy(buf, xbp->xb_bufp + stop, delta);
4827 memmove(xbp->xb_bufp + start + delta, xbp->xb_bufp + start, stop - start);
4828 memmove(xbp->xb_bufp + start, buf, delta);
4832 xo_find_width (xo_handle_t *xop, xo_field_info_t *xfip,
4833 const char *str, unsigned len)
4835 const char *fmt = xfip->xfi_format;
4836 unsigned flen = xfip->xfi_flen;
4843 bp = alloca(len + 1); /* Make local NUL-terminated copy of str */
4844 memcpy(bp, str, len);
4847 width = strtol(bp, &cp, 0);
4848 if (width == LONG_MIN || width == LONG_MAX
4849 || bp == cp || *cp != '\0' ) {
4851 xo_failure(xop, "invalid width for anchor: '%s'", bp);
4854 if (flen != 2 || strncmp("%d", fmt, flen) != 0)
4855 xo_failure(xop, "invalid width format: '%*.*s'", flen, flen, fmt);
4856 if (!XOF_ISSET(xop, XOF_NO_VA_ARG))
4857 width = va_arg(xop->xo_vap, int);
4864 xo_anchor_clear (xo_handle_t *xop)
4866 XOIF_CLEAR(xop, XOIF_ANCHOR);
4867 xop->xo_anchor_offset = 0;
4868 xop->xo_anchor_columns = 0;
4869 xop->xo_anchor_min_width = 0;
4873 * An anchor is a marker used to delay field width implications.
4874 * Imagine the format string "{[:10}{min:%d}/{cur:%d}/{max:%d}{:]}".
4875 * We are looking for output like " 1/4/5"
4877 * To make this work, we record the anchor and then return to
4878 * format it when the end anchor tag is seen.
4881 xo_anchor_start (xo_handle_t *xop, xo_field_info_t *xfip,
4882 const char *str, unsigned len)
4884 if (xo_style(xop) != XO_STYLE_TEXT && xo_style(xop) != XO_STYLE_HTML)
4887 if (XOIF_ISSET(xop, XOIF_ANCHOR))
4888 xo_failure(xop, "the anchor already recording is discarded");
4890 XOIF_SET(xop, XOIF_ANCHOR);
4891 xo_buffer_t *xbp = &xop->xo_data;
4892 xop->xo_anchor_offset = xbp->xb_curp - xbp->xb_bufp;
4893 xop->xo_anchor_columns = 0;
4896 * Now we find the width, if possible. If it's not there,
4897 * we'll get it on the end anchor.
4899 xop->xo_anchor_min_width = xo_find_width(xop, xfip, str, len);
4903 xo_anchor_stop (xo_handle_t *xop, xo_field_info_t *xfip,
4904 const char *str, unsigned len)
4906 if (xo_style(xop) != XO_STYLE_TEXT && xo_style(xop) != XO_STYLE_HTML)
4909 if (!XOIF_ISSET(xop, XOIF_ANCHOR)) {
4910 xo_failure(xop, "no start anchor");
4914 XOIF_CLEAR(xop, XOIF_UNITS_PENDING);
4916 int width = xo_find_width(xop, xfip, str, len);
4918 width = xop->xo_anchor_min_width;
4920 if (width == 0) /* No width given; nothing to do */
4923 xo_buffer_t *xbp = &xop->xo_data;
4924 int start = xop->xo_anchor_offset;
4925 int stop = xbp->xb_curp - xbp->xb_bufp;
4926 int abswidth = (width > 0) ? width : -width;
4927 int blen = abswidth - xop->xo_anchor_columns;
4929 if (blen <= 0) /* Already over width */
4932 if (abswidth > XO_MAX_ANCHOR_WIDTH) {
4933 xo_failure(xop, "width over %u are not supported",
4934 XO_MAX_ANCHOR_WIDTH);
4938 /* Make a suitable padding field and emit it */
4939 char *buf = alloca(blen);
4940 memset(buf, ' ', blen);
4941 xo_format_content(xop, "padding", NULL, buf, blen, NULL, 0, 0);
4943 if (width < 0) /* Already left justified */
4946 int now = xbp->xb_curp - xbp->xb_bufp;
4947 int delta = now - stop;
4948 if (delta <= 0) /* Strange; no output to move */
4952 * Now we're in it alright. We've need to insert the padding data
4953 * we just created (which might be an HTML <div> or text) before
4954 * the formatted data. We make a local copy, move it and then
4955 * insert our copy. We know there's room in the buffer, since
4956 * we're just moving this around.
4959 buf = alloca(delta); /* Expand buffer if needed */
4961 memcpy(buf, xbp->xb_bufp + stop, delta);
4962 memmove(xbp->xb_bufp + start + delta, xbp->xb_bufp + start, stop - start);
4963 memmove(xbp->xb_bufp + start, buf, delta);
4966 xo_anchor_clear(xop);
4970 xo_class_name (int ftype)
4973 case 'D': return "decoration";
4974 case 'E': return "error";
4975 case 'L': return "label";
4976 case 'N': return "note";
4977 case 'P': return "padding";
4978 case 'W': return "warning";
4985 xo_tag_name (int ftype)
4988 case 'E': return "__error";
4989 case 'W': return "__warning";
4996 xo_role_wants_default_format (int ftype)
4999 /* These roles can be completely empty and/or without formatting */
5010 static xo_mapping_t xo_role_names[] = {
5012 { 'D', "decoration" },
5021 { '[', "start-anchor" },
5022 { ']', "stop-anchor" },
5026 #define XO_ROLE_EBRACE '{' /* Escaped braces */
5027 #define XO_ROLE_TEXT '+'
5028 #define XO_ROLE_NEWLINE '\n'
5030 static xo_mapping_t xo_modifier_names[] = {
5031 { XFF_ARGUMENT, "argument" },
5032 { XFF_COLON, "colon" },
5033 { XFF_COMMA, "comma" },
5034 { XFF_DISPLAY_ONLY, "display" },
5035 { XFF_ENCODE_ONLY, "encoding" },
5036 { XFF_GT_FIELD, "gettext" },
5037 { XFF_HUMANIZE, "humanize" },
5038 { XFF_HUMANIZE, "hn" },
5039 { XFF_HN_SPACE, "hn-space" },
5040 { XFF_HN_DECIMAL, "hn-decimal" },
5041 { XFF_HN_1000, "hn-1000" },
5043 { XFF_LEAF_LIST, "leaf-list" },
5044 { XFF_LEAF_LIST, "list" },
5045 { XFF_NOQUOTE, "no-quotes" },
5046 { XFF_NOQUOTE, "no-quote" },
5047 { XFF_GT_PLURAL, "plural" },
5048 { XFF_QUOTE, "quotes" },
5049 { XFF_QUOTE, "quote" },
5050 { XFF_TRIM_WS, "trim" },
5051 { XFF_WS, "white" },
5055 #ifdef NOT_NEEDED_YET
5056 static xo_mapping_t xo_modifier_short_names[] = {
5058 { XFF_DISPLAY_ONLY, "d" },
5059 { XFF_ENCODE_ONLY, "e" },
5060 { XFF_GT_FIELD, "g" },
5061 { XFF_HUMANIZE, "h" },
5063 { XFF_LEAF_LIST, "l" },
5064 { XFF_NOQUOTE, "n" },
5065 { XFF_GT_PLURAL, "p" },
5067 { XFF_TRIM_WS, "t" },
5071 #endif /* NOT_NEEDED_YET */
5074 xo_count_fields (xo_handle_t *xop UNUSED, const char *fmt)
5079 for (cp = fmt; *cp; cp++)
5080 if (*cp == '{' || *cp == '\n')
5087 * The field format is:
5088 * '{' modifiers ':' content [ '/' print-fmt [ '/' encode-fmt ]] '}'
5089 * Roles are optional and include the following field types:
5090 * 'D': decoration; something non-text and non-data (colons, commmas)
5091 * 'E': error message
5092 * 'G': gettext() the entire string; optional domainname as content
5093 * 'L': label; text preceding data
5094 * 'N': note; text following data
5095 * 'P': padding; whitespace
5096 * 'T': Title, where 'content' is a column title
5097 * 'U': Units, where 'content' is the unit label
5098 * 'V': value, where 'content' is the name of the field (the default)
5099 * 'W': warning message
5100 * '[': start a section of anchored text
5101 * ']': end a section of anchored text
5102 * The following modifiers are also supported:
5103 * 'a': content is provided via argument (const char *), not descriptor
5104 * 'c': flag: emit a colon after the label
5105 * 'd': field is only emitted for display styles (text and html)
5106 * 'e': field is only emitted for encoding styles (xml and json)
5107 * 'g': gettext() the field
5108 * 'h': humanize a numeric value (only for display styles)
5109 * 'k': this field is a key, suitable for XPath predicates
5110 * 'l': a leaf-list, a simple list of values
5111 * 'n': no quotes around this field
5112 * 'p': the field has plural gettext semantics (ngettext)
5113 * 'q': add quotes around this field
5114 * 't': trim whitespace around the value
5115 * 'w': emit a blank after the label
5116 * The print-fmt and encode-fmt strings is the printf-style formating
5117 * for this data. JSON and XML will use the encoding-fmt, if present.
5118 * If the encode-fmt is not provided, it defaults to the print-fmt.
5119 * If the print-fmt is not provided, it defaults to 's'.
5122 xo_parse_roles (xo_handle_t *xop, const char *fmt,
5123 const char *basep, xo_field_info_t *xfip)
5127 xo_xff_flags_t flags = 0;
5130 for (sp = basep; sp && *sp; sp++) {
5131 if (*sp == ':' || *sp == '/' || *sp == '}')
5135 if (sp[1] == '\0') {
5136 xo_failure(xop, "backslash at the end of string");
5140 /* Anything backslashed is ignored */
5147 for (np = ++sp; *np; np++)
5148 if (*np == ':' || *np == '/' || *np == '}' || *np == ',')
5153 xo_xff_flags_t value;
5155 value = xo_name_lookup(xo_role_names, sp, slen);
5159 value = xo_name_lookup(xo_modifier_names, sp, slen);
5163 xo_failure(xop, "unknown keyword ignored: '%.*s'",
5187 xo_failure(xop, "field descriptor uses multiple types: '%s'",
5204 fnum = (fnum * 10) + (*sp - '0');
5208 flags |= XFF_ARGUMENT;
5216 flags |= XFF_DISPLAY_ONLY;
5220 flags |= XFF_ENCODE_ONLY;
5224 flags |= XFF_GT_FIELD;
5228 flags |= XFF_HUMANIZE;
5236 flags |= XFF_LEAF_LIST;
5240 flags |= XFF_NOQUOTE;
5244 flags |= XFF_GT_PLURAL;
5252 flags |= XFF_TRIM_WS;
5260 xo_failure(xop, "field descriptor uses unknown modifier: '%s'",
5263 * No good answer here; a bad format will likely
5264 * mean a core file. We just return and hope
5265 * the caller notices there's no output, and while
5266 * that seems, well, bad, there's nothing better.
5271 if (ftype == 'N' || ftype == 'U') {
5272 if (flags & XFF_COLON) {
5273 xo_failure(xop, "colon modifier on 'N' or 'U' field ignored: "
5274 "'%s'", xo_printable(fmt));
5275 flags &= ~XFF_COLON;
5280 xfip->xfi_flags = flags;
5281 xfip->xfi_ftype = ftype ?: 'V';
5282 xfip->xfi_fnum = fnum;
5288 * Number any remaining fields that need numbers. Note that some
5289 * field types (text, newline, escaped braces) never get numbers.
5292 xo_gettext_finish_numbering_fields (xo_handle_t *xop UNUSED,
5293 const char *fmt UNUSED,
5294 xo_field_info_t *fields)
5296 xo_field_info_t *xfip;
5297 unsigned fnum, max_fields;
5300 /* First make a list of add the explicitly used bits */
5301 for (xfip = fields, fnum = 0; xfip->xfi_ftype; xfip++) {
5302 switch (xfip->xfi_ftype) {
5303 case XO_ROLE_NEWLINE: /* Don't get numbered */
5305 case XO_ROLE_EBRACE:
5315 bits |= 1 << xfip->xfi_fnum;
5320 for (xfip = fields, fnum = 0; xfip->xfi_ftype; xfip++) {
5321 switch (xfip->xfi_ftype) {
5322 case XO_ROLE_NEWLINE: /* Don't get numbered */
5324 case XO_ROLE_EBRACE:
5329 if (xfip->xfi_fnum != 0)
5332 /* Find the next unassigned field */
5333 for (fnum++; bits & (1 << fnum); fnum++)
5336 if (fnum > max_fields)
5339 xfip->xfi_fnum = fnum; /* Mark the field number */
5340 bits |= 1 << fnum; /* Mark it used */
5345 * The format string uses field numbers, so we need to whiffle thru it
5346 * and make sure everything's sane and lovely.
5349 xo_parse_field_numbers (xo_handle_t *xop, const char *fmt,
5350 xo_field_info_t *fields, unsigned num_fields)
5352 xo_field_info_t *xfip;
5353 unsigned field, fnum;
5356 for (xfip = fields, field = 0; field < num_fields; xfip++, field++) {
5357 /* Fields default to 1:1 with natural position */
5358 if (xfip->xfi_fnum == 0)
5359 xfip->xfi_fnum = field + 1;
5360 else if (xfip->xfi_fnum > num_fields) {
5361 xo_failure(xop, "field number exceeds number of fields: '%s'", fmt);
5365 fnum = xfip->xfi_fnum - 1; /* Move to zero origin */
5366 if (fnum < 64) { /* Only test what fits */
5367 if (bits & (1 << fnum)) {
5368 xo_failure(xop, "field number %u reused: '%s'",
5369 xfip->xfi_fnum, fmt);
5380 xo_parse_fields (xo_handle_t *xop, xo_field_info_t *fields,
5381 unsigned num_fields, const char *fmt)
5383 const char *cp, *sp, *ep, *basep;
5385 xo_field_info_t *xfip = fields;
5386 unsigned seen_fnum = 0;
5388 for (cp = fmt; *cp && field < num_fields; field++, xfip++) {
5389 xfip->xfi_start = cp;
5392 xfip->xfi_ftype = XO_ROLE_NEWLINE;
5400 for (sp = cp; *sp; sp++) {
5401 if (*sp == '{' || *sp == '\n')
5405 xfip->xfi_ftype = XO_ROLE_TEXT;
5406 xfip->xfi_content = cp;
5407 xfip->xfi_clen = sp - cp;
5408 xfip->xfi_next = sp;
5414 if (cp[1] == '{') { /* Start of {{escaped braces}} */
5415 xfip->xfi_start = cp + 1; /* Start at second brace */
5416 xfip->xfi_ftype = XO_ROLE_EBRACE;
5418 cp += 2; /* Skip over _both_ characters */
5419 for (sp = cp; *sp; sp++) {
5420 if (*sp == '}' && sp[1] == '}')
5424 xo_failure(xop, "missing closing '}}': '%s'",
5429 xfip->xfi_len = sp - xfip->xfi_start + 1;
5431 /* Move along the string, but don't run off the end */
5432 if (*sp == '}' && sp[1] == '}')
5435 xfip->xfi_next = cp;
5439 /* We are looking at the start of a field definition */
5440 xfip->xfi_start = basep = cp + 1;
5442 const char *format = NULL;
5445 /* Looking at roles and modifiers */
5446 sp = xo_parse_roles(xop, fmt, basep, xfip);
5448 /* xo_failure has already been called */
5455 /* Looking at content */
5457 for (ep = ++sp; *sp; sp++) {
5458 if (*sp == '}' || *sp == '/')
5461 if (sp[1] == '\0') {
5462 xo_failure(xop, "backslash at the end of string");
5470 xfip->xfi_clen = sp - ep;
5471 xfip->xfi_content = ep;
5474 xo_failure(xop, "missing content (':'): '%s'", xo_printable(fmt));
5478 /* Looking at main (display) format */
5480 for (ep = ++sp; *sp; sp++) {
5481 if (*sp == '}' || *sp == '/')
5484 if (sp[1] == '\0') {
5485 xo_failure(xop, "backslash at the end of string");
5496 /* Looking at encoding format */
5498 for (ep = ++sp; *sp; sp++) {
5503 xfip->xfi_encoding = ep;
5504 xfip->xfi_elen = sp - ep;
5508 xo_failure(xop, "missing closing '}': %s", xo_printable(fmt));
5512 xfip->xfi_len = sp - xfip->xfi_start;
5513 xfip->xfi_next = ++sp;
5515 /* If we have content, then we have a default format */
5516 if (xfip->xfi_clen || format || (xfip->xfi_flags & XFF_ARGUMENT)) {
5518 xfip->xfi_format = format;
5519 xfip->xfi_flen = flen;
5520 } else if (xo_role_wants_default_format(xfip->xfi_ftype)) {
5521 xfip->xfi_format = xo_default_format;
5532 * If we saw a field number on at least one field, then we need
5533 * to enforce some rules and/or guidelines.
5536 rc = xo_parse_field_numbers(xop, fmt, fields, field);
5542 * We are passed a pointer to a format string just past the "{G:}"
5543 * field. We build a simplified version of the format string.
5546 xo_gettext_simplify_format (xo_handle_t *xop UNUSED,
5548 xo_field_info_t *fields,
5550 const char *fmt UNUSED,
5551 xo_simplify_field_func_t field_cb)
5554 xo_xff_flags_t flags;
5555 int field = this_field + 1;
5556 xo_field_info_t *xfip;
5559 for (xfip = &fields[field]; xfip->xfi_ftype; xfip++, field++) {
5560 ftype = xfip->xfi_ftype;
5561 flags = xfip->xfi_flags;
5563 if ((flags & XFF_GT_FIELD) && xfip->xfi_content && ftype != 'V') {
5565 field_cb(xfip->xfi_content, xfip->xfi_clen,
5566 (flags & XFF_GT_PLURAL) ? 1 : 0);
5571 /* Ignore gettext roles */
5574 case XO_ROLE_NEWLINE:
5575 xo_buf_append(xbp, "\n", 1);
5578 case XO_ROLE_EBRACE:
5579 xo_buf_append(xbp, "{", 1);
5580 xo_buf_append(xbp, xfip->xfi_content, xfip->xfi_clen);
5581 xo_buf_append(xbp, "}", 1);
5585 xo_buf_append(xbp, xfip->xfi_content, xfip->xfi_clen);
5589 xo_buf_append(xbp, "{", 1);
5592 xo_buf_append(xbp, &ch, 1);
5595 unsigned fnum = xfip->xfi_fnum ?: 0;
5598 /* Field numbers are origin 1, not 0, following printf(3) */
5599 snprintf(num, sizeof(num), "%u", fnum);
5600 xo_buf_append(xbp, num, strlen(num));
5603 xo_buf_append(xbp, ":", 1);
5604 xo_buf_append(xbp, xfip->xfi_content, xfip->xfi_clen);
5605 xo_buf_append(xbp, "}", 1);
5609 xo_buf_append(xbp, "", 1);
5614 xo_dump_fields (xo_field_info_t *); /* Fake prototype for debug function */
5616 xo_dump_fields (xo_field_info_t *fields)
5618 xo_field_info_t *xfip;
5620 for (xfip = fields; xfip->xfi_ftype; xfip++) {
5621 printf("%lu(%u): %lx [%c/%u] [%.*s] [%.*s] [%.*s]\n",
5622 (unsigned long) (xfip - fields), xfip->xfi_fnum,
5623 (unsigned long) xfip->xfi_flags,
5624 isprint((int) xfip->xfi_ftype) ? xfip->xfi_ftype : ' ',
5626 xfip->xfi_clen, xfip->xfi_content ?: "",
5627 xfip->xfi_flen, xfip->xfi_format ?: "",
5628 xfip->xfi_elen, xfip->xfi_encoding ?: "");
5634 * Find the field that matches the given field number
5636 static xo_field_info_t *
5637 xo_gettext_find_field (xo_field_info_t *fields, unsigned fnum)
5639 xo_field_info_t *xfip;
5641 for (xfip = fields; xfip->xfi_ftype; xfip++)
5642 if (xfip->xfi_fnum == fnum)
5649 * At this point, we need to consider if the fields have been reordered,
5650 * such as "The {:adjective} {:noun}" to "La {:noun} {:adjective}".
5652 * We need to rewrite the new_fields using the old fields order,
5653 * so that we can render the message using the arguments as they
5654 * appear on the stack. It's a lot of work, but we don't really
5655 * want to (eventually) fall into the standard printf code which
5656 * means using the arguments straight (and in order) from the
5657 * varargs we were originally passed.
5660 xo_gettext_rewrite_fields (xo_handle_t *xop UNUSED,
5661 xo_field_info_t *fields, unsigned max_fields)
5663 xo_field_info_t tmp[max_fields];
5664 bzero(tmp, max_fields * sizeof(tmp[0]));
5667 xo_field_info_t *newp, *outp, *zp;
5668 for (newp = fields, outp = tmp; newp->xfi_ftype; newp++, outp++) {
5669 switch (newp->xfi_ftype) {
5670 case XO_ROLE_NEWLINE: /* Don't get numbered */
5672 case XO_ROLE_EBRACE:
5675 outp->xfi_renum = 0;
5679 zp = xo_gettext_find_field(fields, ++fnum);
5680 if (zp == NULL) { /* Should not occur */
5682 outp->xfi_renum = 0;
5687 outp->xfi_renum = newp->xfi_fnum;
5690 memcpy(fields, tmp, max_fields * sizeof(tmp[0]));
5694 * We've got two lists of fields, the old list from the original
5695 * format string and the new one from the parsed gettext reply. The
5696 * new list has the localized words, where the old list has the
5697 * formatting information. We need to combine them into a single list
5700 * If the list needs to be reordered, then we've got more serious work
5704 xo_gettext_combine_formats (xo_handle_t *xop, const char *fmt UNUSED,
5705 const char *gtfmt, xo_field_info_t *old_fields,
5706 xo_field_info_t *new_fields, unsigned new_max_fields,
5710 xo_field_info_t *newp, *oldp, *startp = old_fields;
5712 xo_gettext_finish_numbering_fields(xop, fmt, old_fields);
5714 for (newp = new_fields; newp->xfi_ftype; newp++) {
5715 switch (newp->xfi_ftype) {
5716 case XO_ROLE_NEWLINE:
5718 case XO_ROLE_EBRACE:
5722 for (oldp = startp; oldp->xfi_ftype; oldp++) {
5723 if (oldp->xfi_ftype != 'V')
5725 if (newp->xfi_clen != oldp->xfi_clen
5726 || strncmp(newp->xfi_content, oldp->xfi_content,
5727 oldp->xfi_clen) != 0) {
5735 /* Didn't find it on the first pass (starting from start) */
5736 if (oldp->xfi_ftype == 0) {
5737 for (oldp = old_fields; oldp < startp; oldp++) {
5738 if (oldp->xfi_ftype != 'V')
5740 if (newp->xfi_clen != oldp->xfi_clen)
5742 if (strncmp(newp->xfi_content, oldp->xfi_content,
5743 oldp->xfi_clen) != 0)
5748 if (oldp == startp) {
5749 /* Field not found */
5750 xo_failure(xop, "post-gettext format can't find field "
5751 "'%.*s' in format '%s'",
5752 newp->xfi_clen, newp->xfi_content,
5753 xo_printable(gtfmt));
5761 * Other fields don't have names for us to use, so if
5762 * the types aren't the same, then we'll have to assume
5763 * the original field is a match.
5765 for (oldp = startp; oldp->xfi_ftype; oldp++) {
5766 if (oldp->xfi_ftype == 'V') /* Can't go past these */
5768 if (oldp->xfi_ftype == newp->xfi_ftype)
5769 goto copy_it; /* Assumably we have a match */
5775 * Found a match; copy over appropriate fields
5778 newp->xfi_flags = oldp->xfi_flags;
5779 newp->xfi_fnum = oldp->xfi_fnum;
5780 newp->xfi_format = oldp->xfi_format;
5781 newp->xfi_flen = oldp->xfi_flen;
5782 newp->xfi_encoding = oldp->xfi_encoding;
5783 newp->xfi_elen = oldp->xfi_elen;
5786 *reorderedp = reordered;
5788 xo_gettext_finish_numbering_fields(xop, fmt, new_fields);
5789 xo_gettext_rewrite_fields(xop, new_fields, new_max_fields);
5796 * We don't want to make gettext() calls here with a complete format
5797 * string, since that means changing a flag would mean a
5798 * labor-intensive re-translation expense. Instead we build a
5799 * simplified form with a reduced level of detail, perform a lookup on
5800 * that string and then re-insert the formating info.
5802 * So something like:
5803 * xo_emit("{G:}close {:fd/%ld} returned {g:error/%m} {:test/%6.6s}\n", ...)
5804 * would have a lookup string of:
5805 * "close {:fd} returned {:error} {:test}\n"
5807 * We also need to handling reordering of fields, where the gettext()
5808 * reply string uses fields in a different order than the original
5810 * "cluse-a {:fd} retoorned {:test}. Bork {:error} Bork. Bork.\n"
5811 * If we have to reorder fields within the message, then things get
5812 * complicated. See xo_gettext_rewrite_fields.
5814 * Summary: i18n aighn't cheap.
5817 xo_gettext_build_format (xo_handle_t *xop,
5818 xo_field_info_t *fields, int this_field,
5819 const char *fmt, char **new_fmtp)
5821 if (xo_style_is_encoding(xop))
5827 if (xo_gettext_simplify_format(xop, &xb, fields,
5828 this_field, fmt, NULL))
5831 const char *gtfmt = xo_dgettext(xop, xb.xb_bufp);
5832 if (gtfmt == NULL || gtfmt == fmt || strcmp(gtfmt, fmt) == 0)
5835 xo_buf_cleanup(&xb);
5837 char *new_fmt = xo_strndup(gtfmt, -1);
5838 if (new_fmt == NULL)
5841 *new_fmtp = new_fmt;
5845 xo_buf_cleanup(&xb);
5852 xo_gettext_rebuild_content (xo_handle_t *xop, xo_field_info_t *fields,
5853 unsigned *fstart, unsigned min_fstart,
5854 unsigned *fend, unsigned max_fend)
5856 xo_field_info_t *xfip;
5858 unsigned base = fstart[min_fstart];
5859 unsigned blen = fend[max_fend] - base;
5860 xo_buffer_t *xbp = &xop->xo_data;
5865 buf = xo_realloc(NULL, blen);
5869 memcpy(buf, xbp->xb_bufp + fstart[min_fstart], blen); /* Copy our data */
5871 unsigned field = min_fstart, soff, doff = base, len, fnum;
5872 xo_field_info_t *zp;
5875 * Be aware there are two competing views of "field number": we
5876 * want the user to thing in terms of "The {1:size}" where {G:},
5877 * newlines, escaped braces, and text don't have numbers. But is
5878 * also the internal view, where we have an array of
5879 * xo_field_info_t and every field have an index. fnum, fstart[]
5880 * and fend[] are the latter, but xfi_renum is the former.
5882 for (xfip = fields + field; xfip->xfi_ftype; xfip++, field++) {
5884 if (xfip->xfi_renum) {
5885 zp = xo_gettext_find_field(fields, xfip->xfi_renum);
5886 fnum = zp ? zp - fields : field;
5889 soff = fstart[fnum];
5890 len = fend[fnum] - soff;
5894 memcpy(xbp->xb_bufp + doff, buf + soff, len);
5901 #else /* HAVE_GETTEXT */
5903 xo_gettext_build_format (xo_handle_t *xop UNUSED,
5904 xo_field_info_t *fields UNUSED,
5905 int this_field UNUSED,
5906 const char *fmt UNUSED, char **new_fmtp)
5913 xo_gettext_combine_formats (xo_handle_t *xop UNUSED, const char *fmt UNUSED,
5914 const char *gtfmt UNUSED,
5915 xo_field_info_t *old_fields UNUSED,
5916 xo_field_info_t *new_fields UNUSED,
5917 unsigned new_max_fields UNUSED,
5918 int *reorderedp UNUSED)
5924 xo_gettext_rebuild_content (xo_handle_t *xop UNUSED,
5925 xo_field_info_t *fields UNUSED,
5926 unsigned *fstart UNUSED, unsigned min_fstart UNUSED,
5927 unsigned *fend UNUSED, unsigned max_fend UNUSED)
5931 #endif /* HAVE_GETTEXT */
5934 * Emit a set of fields. This is really the core of libxo.
5937 xo_do_emit_fields (xo_handle_t *xop, xo_field_info_t *fields,
5938 unsigned max_fields, const char *fmt)
5940 int gettext_inuse = 0;
5941 int gettext_changed = 0;
5942 int gettext_reordered = 0;
5944 xo_xff_flags_t flags;
5945 xo_field_info_t *new_fields = NULL;
5946 xo_field_info_t *xfip;
5950 int flush = XOF_ISSET(xop, XOF_FLUSH);
5951 int flush_line = XOF_ISSET(xop, XOF_FLUSH_LINE);
5952 char *new_fmt = NULL;
5954 if (XOIF_ISSET(xop, XOIF_REORDER) || xo_style(xop) == XO_STYLE_ENCODER)
5958 * Some overhead for gettext; if the fields in the msgstr returned
5959 * by gettext are reordered, then we need to record start and end
5960 * for each field. We'll go ahead and render the fields in the
5961 * normal order, but later we can then reconstruct the reordered
5962 * fields using these fstart/fend values.
5964 unsigned flimit = max_fields * 2; /* Pessimistic limit */
5965 unsigned min_fstart = flimit - 1;
5966 unsigned max_fend = 0; /* Highest recorded fend[] entry */
5967 unsigned fstart[flimit];
5968 bzero(fstart, flimit * sizeof(fstart[0]));
5969 unsigned fend[flimit];
5970 bzero(fend, flimit * sizeof(fend[0]));
5972 for (xfip = fields, field = 0; xfip->xfi_ftype && field < max_fields;
5974 ftype = xfip->xfi_ftype;
5975 flags = xfip->xfi_flags;
5977 /* Record field start offset */
5978 if (gettext_reordered) {
5979 fstart[field] = xo_buf_offset(&xop->xo_data);
5980 if (min_fstart > field)
5984 const char *content = xfip->xfi_content;
5985 int clen = xfip->xfi_clen;
5987 if (flags & XFF_ARGUMENT) {
5989 * Argument flag means the content isn't given in the descriptor,
5990 * but as a UTF-8 string ('const char *') argument in xo_vap.
5992 content = va_arg(xop->xo_vap, char *);
5993 clen = content ? strlen(content) : 0;
5996 if (ftype == XO_ROLE_NEWLINE) {
5998 if (flush_line && xo_flush_h(xop) < 0)
6002 } else if (ftype == XO_ROLE_EBRACE) {
6003 xo_format_text(xop, xfip->xfi_start, xfip->xfi_len);
6006 } else if (ftype == XO_ROLE_TEXT) {
6008 xo_format_text(xop, xfip->xfi_content, xfip->xfi_clen);
6013 * Notes and units need the 'w' flag handled before the content.
6015 if (ftype == 'N' || ftype == 'U') {
6016 if (flags & XFF_WS) {
6017 xo_format_content(xop, "padding", NULL, " ", 1,
6019 flags &= ~XFF_WS; /* Block later handling of this */
6024 xo_format_value(xop, content, clen,
6025 xfip->xfi_format, xfip->xfi_flen,
6026 xfip->xfi_encoding, xfip->xfi_elen, flags);
6027 else if (ftype == '[')
6028 xo_anchor_start(xop, xfip, content, clen);
6029 else if (ftype == ']')
6030 xo_anchor_stop(xop, xfip, content, clen);
6031 else if (ftype == 'C')
6032 xo_format_colors(xop, xfip, content, clen);
6034 else if (ftype == 'G') {
6036 * A {G:domain} field; disect the domain name and translate
6037 * the remaining portion of the input string. If the user
6038 * didn't put the {G:} at the start of the format string, then
6039 * assumably they just want us to translate the rest of it.
6040 * Since gettext returns strings in a static buffer, we make
6041 * a copy in new_fmt.
6043 xo_set_gettext_domain(xop, xfip, content, clen);
6045 if (!gettext_inuse) { /* Only translate once */
6052 xo_gettext_build_format(xop, fields, field,
6053 xfip->xfi_next, &new_fmt);
6055 gettext_changed = 1;
6057 unsigned new_max_fields = xo_count_fields(xop, new_fmt);
6059 if (++new_max_fields < max_fields)
6060 new_max_fields = max_fields;
6062 /* Leave a blank slot at the beginning */
6063 int sz = (new_max_fields + 1) * sizeof(xo_field_info_t);
6064 new_fields = alloca(sz);
6065 bzero(new_fields, sz);
6067 if (!xo_parse_fields(xop, new_fields + 1,
6068 new_max_fields, new_fmt)) {
6069 gettext_reordered = 0;
6071 if (!xo_gettext_combine_formats(xop, fmt, new_fmt,
6072 fields, new_fields + 1,
6073 new_max_fields, &gettext_reordered)) {
6075 if (gettext_reordered) {
6076 if (XOF_ISSET(xop, XOF_LOG_GETTEXT))
6077 xo_failure(xop, "gettext finds reordered "
6078 "fields in '%s' and '%s'",
6080 xo_printable(new_fmt));
6081 flush_line = 0; /* Must keep at content */
6082 XOIF_SET(xop, XOIF_REORDER);
6085 field = -1; /* Will be incremented at top of loop */
6087 max_fields = new_max_fields;
6094 } else if (clen || xfip->xfi_format) {
6096 const char *class_name = xo_class_name(ftype);
6098 xo_format_content(xop, class_name, xo_tag_name(ftype),
6100 xfip->xfi_format, xfip->xfi_flen, flags);
6101 else if (ftype == 'T')
6102 xo_format_title(xop, xfip, content, clen);
6103 else if (ftype == 'U')
6104 xo_format_units(xop, xfip, content, clen);
6106 xo_failure(xop, "unknown field type: '%c'", ftype);
6109 if (flags & XFF_COLON)
6110 xo_format_content(xop, "decoration", NULL, ":", 1, NULL, 0, 0);
6113 xo_format_content(xop, "padding", NULL, " ", 1, NULL, 0, 0);
6116 /* Record the end-of-field offset */
6117 if (gettext_reordered) {
6118 fend[field] = xo_buf_offset(&xop->xo_data);
6123 if (gettext_changed && gettext_reordered) {
6124 /* Final step: rebuild the content using the rendered fields */
6125 xo_gettext_rebuild_content(xop, new_fields + 1, fstart, min_fstart,
6129 XOIF_CLEAR(xop, XOIF_REORDER);
6131 /* If we don't have an anchor, write the text out */
6132 if (flush && !XOIF_ISSET(xop, XOIF_ANCHOR)) {
6133 if (xo_write(xop) < 0)
6134 rc = -1; /* Report failure */
6135 else if (xo_flush_h(xop) < 0)
6143 * We've carried the gettext domainname inside our handle just for
6144 * convenience, but we need to ensure it doesn't survive across
6147 if (xop->xo_gt_domain) {
6148 xo_free(xop->xo_gt_domain);
6149 xop->xo_gt_domain = NULL;
6152 return (rc < 0) ? rc : (int) xop->xo_columns;
6156 * Parse and emit a set of fields
6159 xo_do_emit (xo_handle_t *xop, xo_emit_flags_t flags, const char *fmt)
6161 xop->xo_columns = 0; /* Always reset it */
6162 xop->xo_errno = errno; /* Save for "%m" */
6167 unsigned max_fields;
6168 xo_field_info_t *fields = NULL;
6170 /* Adjust XOEF_RETAIN based on global flags */
6171 if (XOF_ISSET(xop, XOF_RETAIN_ALL))
6172 flags |= XOEF_RETAIN;
6173 if (XOF_ISSET(xop, XOF_RETAIN_NONE))
6174 flags &= ~XOEF_RETAIN;
6177 * Check for 'retain' flag, telling us to retain the field
6178 * information. If we've already saved it, then we can avoid
6179 * re-parsing the format string.
6181 if (!(flags & XOEF_RETAIN)
6182 || xo_retain_find(fmt, &fields, &max_fields) != 0
6183 || fields == NULL) {
6185 /* Nothing retained; parse the format string */
6186 max_fields = xo_count_fields(xop, fmt);
6187 fields = alloca(max_fields * sizeof(fields[0]));
6188 bzero(fields, max_fields * sizeof(fields[0]));
6190 if (xo_parse_fields(xop, fields, max_fields, fmt))
6191 return -1; /* Warning already displayed */
6193 if (flags & XOEF_RETAIN) {
6194 /* Retain the info */
6195 xo_retain_add(fmt, fields, max_fields);
6199 return xo_do_emit_fields(xop, fields, max_fields, fmt);
6203 * Rebuild a format string in a gettext-friendly format. This function
6204 * is exposed to tools can perform this function. See xo(1).
6207 xo_simplify_format (xo_handle_t *xop, const char *fmt, int with_numbers,
6208 xo_simplify_field_func_t field_cb)
6210 xop = xo_default(xop);
6212 xop->xo_columns = 0; /* Always reset it */
6213 xop->xo_errno = errno; /* Save for "%m" */
6215 unsigned max_fields = xo_count_fields(xop, fmt);
6216 xo_field_info_t fields[max_fields];
6218 bzero(fields, max_fields * sizeof(fields[0]));
6220 if (xo_parse_fields(xop, fields, max_fields, fmt))
6221 return NULL; /* Warning already displayed */
6227 xo_gettext_finish_numbering_fields(xop, fmt, fields);
6229 if (xo_gettext_simplify_format(xop, &xb, fields, -1, fmt, field_cb))
6236 xo_emit_hv (xo_handle_t *xop, const char *fmt, va_list vap)
6240 xop = xo_default(xop);
6241 va_copy(xop->xo_vap, vap);
6242 rc = xo_do_emit(xop, 0, fmt);
6243 va_end(xop->xo_vap);
6244 bzero(&xop->xo_vap, sizeof(xop->xo_vap));
6250 xo_emit_h (xo_handle_t *xop, const char *fmt, ...)
6254 xop = xo_default(xop);
6255 va_start(xop->xo_vap, fmt);
6256 rc = xo_do_emit(xop, 0, fmt);
6257 va_end(xop->xo_vap);
6258 bzero(&xop->xo_vap, sizeof(xop->xo_vap));
6264 xo_emit (const char *fmt, ...)
6266 xo_handle_t *xop = xo_default(NULL);
6269 va_start(xop->xo_vap, fmt);
6270 rc = xo_do_emit(xop, 0, fmt);
6271 va_end(xop->xo_vap);
6272 bzero(&xop->xo_vap, sizeof(xop->xo_vap));
6278 xo_emit_hvf (xo_handle_t *xop, xo_emit_flags_t flags,
6279 const char *fmt, va_list vap)
6283 xop = xo_default(xop);
6284 va_copy(xop->xo_vap, vap);
6285 rc = xo_do_emit(xop, flags, fmt);
6286 va_end(xop->xo_vap);
6287 bzero(&xop->xo_vap, sizeof(xop->xo_vap));
6293 xo_emit_hf (xo_handle_t *xop, xo_emit_flags_t flags, const char *fmt, ...)
6297 xop = xo_default(xop);
6298 va_start(xop->xo_vap, fmt);
6299 rc = xo_do_emit(xop, flags, fmt);
6300 va_end(xop->xo_vap);
6301 bzero(&xop->xo_vap, sizeof(xop->xo_vap));
6307 xo_emit_f (xo_emit_flags_t flags, const char *fmt, ...)
6309 xo_handle_t *xop = xo_default(NULL);
6312 va_start(xop->xo_vap, fmt);
6313 rc = xo_do_emit(xop, flags, fmt);
6314 va_end(xop->xo_vap);
6315 bzero(&xop->xo_vap, sizeof(xop->xo_vap));
6321 * Emit a single field by providing the info information typically provided
6322 * inside the field description (role, modifiers, and formats). This is
6323 * a convenience function to avoid callers using snprintf to build field
6327 xo_emit_field_hv (xo_handle_t *xop, const char *rolmod, const char *contents,
6328 const char *fmt, const char *efmt,
6333 xop = xo_default(xop);
6338 xo_field_info_t xfi;
6340 bzero(&xfi, sizeof(xfi));
6343 cp = xo_parse_roles(xop, rolmod, rolmod, &xfi);
6347 xfi.xfi_start = fmt;
6348 xfi.xfi_content = contents;
6349 xfi.xfi_format = fmt;
6350 xfi.xfi_encoding = efmt;
6351 xfi.xfi_clen = contents ? strlen(contents) : 0;
6352 xfi.xfi_flen = fmt ? strlen(fmt) : 0;
6353 xfi.xfi_elen = efmt ? strlen(efmt) : 0;
6355 /* If we have content, then we have a default format */
6356 if (contents && fmt == NULL
6357 && xo_role_wants_default_format(xfi.xfi_ftype)) {
6358 xfi.xfi_format = xo_default_format;
6364 va_copy(xop->xo_vap, vap);
6366 rc = xo_do_emit_fields(xop, &xfi, 1, fmt ?: contents ?: "field");
6368 va_end(xop->xo_vap);
6374 xo_emit_field_h (xo_handle_t *xop, const char *rolmod, const char *contents,
6375 const char *fmt, const char *efmt, ...)
6380 va_start(vap, efmt);
6381 rc = xo_emit_field_hv(xop, rolmod, contents, fmt, efmt, vap);
6388 xo_emit_field (const char *rolmod, const char *contents,
6389 const char *fmt, const char *efmt, ...)
6394 va_start(vap, efmt);
6395 rc = xo_emit_field_hv(NULL, rolmod, contents, fmt, efmt, vap);
6402 xo_attr_hv (xo_handle_t *xop, const char *name, const char *fmt, va_list vap)
6404 const int extra = 5; /* space, equals, quote, quote, and nul */
6405 xop = xo_default(xop);
6408 int nlen = strlen(name);
6409 xo_buffer_t *xbp = &xop->xo_attrs;
6410 unsigned name_offset, value_offset;
6412 switch (xo_style(xop)) {
6414 if (!xo_buf_has_room(xbp, nlen + extra))
6417 *xbp->xb_curp++ = ' ';
6418 memcpy(xbp->xb_curp, name, nlen);
6419 xbp->xb_curp += nlen;
6420 *xbp->xb_curp++ = '=';
6421 *xbp->xb_curp++ = '"';
6423 rc = xo_vsnprintf(xop, xbp, fmt, vap);
6426 rc = xo_escape_xml(xbp, rc, 1);
6430 if (!xo_buf_has_room(xbp, 2))
6433 *xbp->xb_curp++ = '"';
6434 *xbp->xb_curp = '\0';
6439 case XO_STYLE_ENCODER:
6440 name_offset = xo_buf_offset(xbp);
6441 xo_buf_append(xbp, name, nlen);
6442 xo_buf_append(xbp, "", 1);
6444 value_offset = xo_buf_offset(xbp);
6445 rc = xo_vsnprintf(xop, xbp, fmt, vap);
6448 *xbp->xb_curp = '\0';
6449 rc = xo_encoder_handle(xop, XO_OP_ATTRIBUTE,
6450 xo_buf_data(xbp, name_offset),
6451 xo_buf_data(xbp, value_offset));
6459 xo_attr_h (xo_handle_t *xop, const char *name, const char *fmt, ...)
6465 rc = xo_attr_hv(xop, name, fmt, vap);
6472 xo_attr (const char *name, const char *fmt, ...)
6478 rc = xo_attr_hv(NULL, name, fmt, vap);
6485 xo_stack_set_flags (xo_handle_t *xop)
6487 if (XOF_ISSET(xop, XOF_NOT_FIRST)) {
6488 xo_stack_t *xsp = &xop->xo_stack[xop->xo_depth];
6490 xsp->xs_flags |= XSF_NOT_FIRST;
6491 XOF_CLEAR(xop, XOF_NOT_FIRST);
6496 xo_depth_change (xo_handle_t *xop, const char *name,
6497 int delta, int indent, xo_state_t state, xo_xsf_flags_t flags)
6499 if (xo_style(xop) == XO_STYLE_HTML || xo_style(xop) == XO_STYLE_TEXT)
6502 if (XOF_ISSET(xop, XOF_DTRT))
6505 if (delta >= 0) { /* Push operation */
6506 if (xo_depth_check(xop, xop->xo_depth + delta))
6509 xo_stack_t *xsp = &xop->xo_stack[xop->xo_depth + delta];
6510 xsp->xs_flags = flags;
6511 xsp->xs_state = state;
6512 xo_stack_set_flags(xop);
6515 name = XO_FAILURE_NAME;
6517 xsp->xs_name = xo_strndup(name, -1);
6519 } else { /* Pop operation */
6520 if (xop->xo_depth == 0) {
6521 if (!XOF_ISSET(xop, XOF_IGNORE_CLOSE))
6522 xo_failure(xop, "close with empty stack: '%s'", name);
6526 xo_stack_t *xsp = &xop->xo_stack[xop->xo_depth];
6527 if (XOF_ISSET(xop, XOF_WARN)) {
6528 const char *top = xsp->xs_name;
6529 if (top && strcmp(name, top) != 0) {
6530 xo_failure(xop, "incorrect close: '%s' .vs. '%s'",
6534 if ((xsp->xs_flags & XSF_LIST) != (flags & XSF_LIST)) {
6535 xo_failure(xop, "list close on list confict: '%s'",
6539 if ((xsp->xs_flags & XSF_INSTANCE) != (flags & XSF_INSTANCE)) {
6540 xo_failure(xop, "list close on instance confict: '%s'",
6547 xo_free(xsp->xs_name);
6548 xsp->xs_name = NULL;
6551 xo_free(xsp->xs_keys);
6552 xsp->xs_keys = NULL;
6556 xop->xo_depth += delta; /* Record new depth */
6557 xop->xo_indent += indent;
6561 xo_set_depth (xo_handle_t *xop, int depth)
6563 xop = xo_default(xop);
6565 if (xo_depth_check(xop, depth))
6568 xop->xo_depth += depth;
6569 xop->xo_indent += depth;
6572 static xo_xsf_flags_t
6573 xo_stack_flags (unsigned xflags)
6575 if (xflags & XOF_DTRT)
6581 xo_emit_top (xo_handle_t *xop, const char *ppn)
6583 xo_printf(xop, "%*s{%s", xo_indent(xop), "", ppn);
6584 XOIF_SET(xop, XOIF_TOP_EMITTED);
6586 if (xop->xo_version) {
6587 xo_printf(xop, "%*s\"__version\": \"%s\", %s",
6588 xo_indent(xop), "", xop->xo_version, ppn);
6589 xo_free(xop->xo_version);
6590 xop->xo_version = NULL;
6595 xo_do_open_container (xo_handle_t *xop, xo_xof_flags_t flags, const char *name)
6598 const char *ppn = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : "";
6599 const char *pre_nl = "";
6602 xo_failure(xop, "NULL passed for container name");
6603 name = XO_FAILURE_NAME;
6606 flags |= xop->xo_flags; /* Pick up handle flags */
6608 switch (xo_style(xop)) {
6610 rc = xo_printf(xop, "%*s<%s", xo_indent(xop), "", name);
6612 if (xop->xo_attrs.xb_curp != xop->xo_attrs.xb_bufp) {
6613 rc += xop->xo_attrs.xb_curp - xop->xo_attrs.xb_bufp;
6614 xo_data_append(xop, xop->xo_attrs.xb_bufp,
6615 xop->xo_attrs.xb_curp - xop->xo_attrs.xb_bufp);
6616 xop->xo_attrs.xb_curp = xop->xo_attrs.xb_bufp;
6619 rc += xo_printf(xop, ">%s", ppn);
6623 xo_stack_set_flags(xop);
6625 if (!XOF_ISSET(xop, XOF_NO_TOP)
6626 && !XOIF_ISSET(xop, XOIF_TOP_EMITTED))
6627 xo_emit_top(xop, ppn);
6629 if (xop->xo_stack[xop->xo_depth].xs_flags & XSF_NOT_FIRST)
6630 pre_nl = XOF_ISSET(xop, XOF_PRETTY) ? ",\n" : ", ";
6631 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
6633 rc = xo_printf(xop, "%s%*s\"%s\": {%s",
6634 pre_nl, xo_indent(xop), "", name, ppn);
6637 case XO_STYLE_SDPARAMS:
6640 case XO_STYLE_ENCODER:
6641 rc = xo_encoder_handle(xop, XO_OP_OPEN_CONTAINER, name, NULL);
6645 xo_depth_change(xop, name, 1, 1, XSS_OPEN_CONTAINER,
6646 xo_stack_flags(flags));
6652 xo_open_container_hf (xo_handle_t *xop, xo_xof_flags_t flags, const char *name)
6654 return xo_transition(xop, flags, name, XSS_OPEN_CONTAINER);
6658 xo_open_container_h (xo_handle_t *xop, const char *name)
6660 return xo_open_container_hf(xop, 0, name);
6664 xo_open_container (const char *name)
6666 return xo_open_container_hf(NULL, 0, name);
6670 xo_open_container_hd (xo_handle_t *xop, const char *name)
6672 return xo_open_container_hf(xop, XOF_DTRT, name);
6676 xo_open_container_d (const char *name)
6678 return xo_open_container_hf(NULL, XOF_DTRT, name);
6682 xo_do_close_container (xo_handle_t *xop, const char *name)
6684 xop = xo_default(xop);
6687 const char *ppn = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : "";
6688 const char *pre_nl = "";
6691 xo_stack_t *xsp = &xop->xo_stack[xop->xo_depth];
6693 name = xsp->xs_name;
6695 int len = strlen(name) + 1;
6696 /* We need to make a local copy; xo_depth_change will free it */
6697 char *cp = alloca(len);
6698 memcpy(cp, name, len);
6700 } else if (!(xsp->xs_flags & XSF_DTRT)) {
6701 xo_failure(xop, "missing name without 'dtrt' mode");
6702 name = XO_FAILURE_NAME;
6706 switch (xo_style(xop)) {
6708 xo_depth_change(xop, name, -1, -1, XSS_CLOSE_CONTAINER, 0);
6709 rc = xo_printf(xop, "%*s</%s>%s", xo_indent(xop), "", name, ppn);
6713 pre_nl = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : "";
6714 ppn = (xop->xo_depth <= 1) ? "\n" : "";
6716 xo_depth_change(xop, name, -1, -1, XSS_CLOSE_CONTAINER, 0);
6717 rc = xo_printf(xop, "%s%*s}%s", pre_nl, xo_indent(xop), "", ppn);
6718 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
6723 xo_depth_change(xop, name, -1, 0, XSS_CLOSE_CONTAINER, 0);
6726 case XO_STYLE_SDPARAMS:
6729 case XO_STYLE_ENCODER:
6730 xo_depth_change(xop, name, -1, 0, XSS_CLOSE_CONTAINER, 0);
6731 rc = xo_encoder_handle(xop, XO_OP_CLOSE_CONTAINER, name, NULL);
6739 xo_close_container_h (xo_handle_t *xop, const char *name)
6741 return xo_transition(xop, 0, name, XSS_CLOSE_CONTAINER);
6745 xo_close_container (const char *name)
6747 return xo_close_container_h(NULL, name);
6751 xo_close_container_hd (xo_handle_t *xop)
6753 return xo_close_container_h(xop, NULL);
6757 xo_close_container_d (void)
6759 return xo_close_container_h(NULL, NULL);
6763 xo_do_open_list (xo_handle_t *xop, xo_xsf_flags_t flags, const char *name)
6768 xop = xo_default(xop);
6770 const char *ppn = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : "";
6771 const char *pre_nl = "";
6773 switch (xo_style(xop)) {
6777 if (!XOF_ISSET(xop, XOF_NO_TOP)
6778 && !XOIF_ISSET(xop, XOIF_TOP_EMITTED))
6779 xo_emit_top(xop, ppn);
6782 xo_failure(xop, "NULL passed for list name");
6783 name = XO_FAILURE_NAME;
6786 xo_stack_set_flags(xop);
6788 if (xop->xo_stack[xop->xo_depth].xs_flags & XSF_NOT_FIRST)
6789 pre_nl = XOF_ISSET(xop, XOF_PRETTY) ? ",\n" : ", ";
6790 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
6792 rc = xo_printf(xop, "%s%*s\"%s\": [%s",
6793 pre_nl, xo_indent(xop), "", name, ppn);
6796 case XO_STYLE_ENCODER:
6797 rc = xo_encoder_handle(xop, XO_OP_OPEN_LIST, name, NULL);
6801 xo_depth_change(xop, name, 1, indent, XSS_OPEN_LIST,
6802 XSF_LIST | xo_stack_flags(flags));
6808 xo_open_list_hf (xo_handle_t *xop, xo_xsf_flags_t flags, const char *name)
6810 return xo_transition(xop, flags, name, XSS_OPEN_LIST);
6814 xo_open_list_h (xo_handle_t *xop, const char *name)
6816 return xo_open_list_hf(xop, 0, name);
6820 xo_open_list (const char *name)
6822 return xo_open_list_hf(NULL, 0, name);
6826 xo_open_list_hd (xo_handle_t *xop, const char *name)
6828 return xo_open_list_hf(xop, XOF_DTRT, name);
6832 xo_open_list_d (const char *name)
6834 return xo_open_list_hf(NULL, XOF_DTRT, name);
6838 xo_do_close_list (xo_handle_t *xop, const char *name)
6841 const char *pre_nl = "";
6844 xo_stack_t *xsp = &xop->xo_stack[xop->xo_depth];
6846 name = xsp->xs_name;
6848 int len = strlen(name) + 1;
6849 /* We need to make a local copy; xo_depth_change will free it */
6850 char *cp = alloca(len);
6851 memcpy(cp, name, len);
6853 } else if (!(xsp->xs_flags & XSF_DTRT)) {
6854 xo_failure(xop, "missing name without 'dtrt' mode");
6855 name = XO_FAILURE_NAME;
6859 switch (xo_style(xop)) {
6861 if (xop->xo_stack[xop->xo_depth].xs_flags & XSF_NOT_FIRST)
6862 pre_nl = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : "";
6863 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
6865 xo_depth_change(xop, name, -1, -1, XSS_CLOSE_LIST, XSF_LIST);
6866 rc = xo_printf(xop, "%s%*s]", pre_nl, xo_indent(xop), "");
6867 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
6870 case XO_STYLE_ENCODER:
6871 xo_depth_change(xop, name, -1, 0, XSS_CLOSE_LIST, XSF_LIST);
6872 rc = xo_encoder_handle(xop, XO_OP_CLOSE_LIST, name, NULL);
6876 xo_depth_change(xop, name, -1, 0, XSS_CLOSE_LIST, XSF_LIST);
6877 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
6885 xo_close_list_h (xo_handle_t *xop, const char *name)
6887 return xo_transition(xop, 0, name, XSS_CLOSE_LIST);
6891 xo_close_list (const char *name)
6893 return xo_close_list_h(NULL, name);
6897 xo_close_list_hd (xo_handle_t *xop)
6899 return xo_close_list_h(xop, NULL);
6903 xo_close_list_d (void)
6905 return xo_close_list_h(NULL, NULL);
6909 xo_do_open_leaf_list (xo_handle_t *xop, xo_xsf_flags_t flags, const char *name)
6914 xop = xo_default(xop);
6916 const char *ppn = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : "";
6917 const char *pre_nl = "";
6919 switch (xo_style(xop)) {
6923 if (!XOF_ISSET(xop, XOF_NO_TOP)) {
6924 if (!XOIF_ISSET(xop, XOIF_TOP_EMITTED)) {
6925 xo_printf(xop, "%*s{%s", xo_indent(xop), "", ppn);
6926 XOIF_SET(xop, XOIF_TOP_EMITTED);
6931 xo_failure(xop, "NULL passed for list name");
6932 name = XO_FAILURE_NAME;
6935 xo_stack_set_flags(xop);
6937 if (xop->xo_stack[xop->xo_depth].xs_flags & XSF_NOT_FIRST)
6938 pre_nl = XOF_ISSET(xop, XOF_PRETTY) ? ",\n" : ", ";
6939 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
6941 rc = xo_printf(xop, "%s%*s\"%s\": [%s",
6942 pre_nl, xo_indent(xop), "", name, ppn);
6945 case XO_STYLE_ENCODER:
6946 rc = xo_encoder_handle(xop, XO_OP_OPEN_LEAF_LIST, name, NULL);
6950 xo_depth_change(xop, name, 1, indent, XSS_OPEN_LEAF_LIST,
6951 XSF_LIST | xo_stack_flags(flags));
6957 xo_do_close_leaf_list (xo_handle_t *xop, const char *name)
6960 const char *pre_nl = "";
6963 xo_stack_t *xsp = &xop->xo_stack[xop->xo_depth];
6965 name = xsp->xs_name;
6967 int len = strlen(name) + 1;
6968 /* We need to make a local copy; xo_depth_change will free it */
6969 char *cp = alloca(len);
6970 memcpy(cp, name, len);
6972 } else if (!(xsp->xs_flags & XSF_DTRT)) {
6973 xo_failure(xop, "missing name without 'dtrt' mode");
6974 name = XO_FAILURE_NAME;
6978 switch (xo_style(xop)) {
6980 if (xop->xo_stack[xop->xo_depth].xs_flags & XSF_NOT_FIRST)
6981 pre_nl = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : "";
6982 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
6984 xo_depth_change(xop, name, -1, -1, XSS_CLOSE_LEAF_LIST, XSF_LIST);
6985 rc = xo_printf(xop, "%s%*s]", pre_nl, xo_indent(xop), "");
6986 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
6989 case XO_STYLE_ENCODER:
6990 rc = xo_encoder_handle(xop, XO_OP_CLOSE_LEAF_LIST, name, NULL);
6994 xo_depth_change(xop, name, -1, 0, XSS_CLOSE_LEAF_LIST, XSF_LIST);
6995 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
7003 xo_do_open_instance (xo_handle_t *xop, xo_xsf_flags_t flags, const char *name)
7005 xop = xo_default(xop);
7008 const char *ppn = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : "";
7009 const char *pre_nl = "";
7011 flags |= xop->xo_flags;
7014 xo_failure(xop, "NULL passed for instance name");
7015 name = XO_FAILURE_NAME;
7018 switch (xo_style(xop)) {
7020 rc = xo_printf(xop, "%*s<%s", xo_indent(xop), "", name);
7022 if (xop->xo_attrs.xb_curp != xop->xo_attrs.xb_bufp) {
7023 rc += xop->xo_attrs.xb_curp - xop->xo_attrs.xb_bufp;
7024 xo_data_append(xop, xop->xo_attrs.xb_bufp,
7025 xop->xo_attrs.xb_curp - xop->xo_attrs.xb_bufp);
7026 xop->xo_attrs.xb_curp = xop->xo_attrs.xb_bufp;
7029 rc += xo_printf(xop, ">%s", ppn);
7033 xo_stack_set_flags(xop);
7035 if (xop->xo_stack[xop->xo_depth].xs_flags & XSF_NOT_FIRST)
7036 pre_nl = XOF_ISSET(xop, XOF_PRETTY) ? ",\n" : ", ";
7037 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
7039 rc = xo_printf(xop, "%s%*s{%s",
7040 pre_nl, xo_indent(xop), "", ppn);
7043 case XO_STYLE_SDPARAMS:
7046 case XO_STYLE_ENCODER:
7047 rc = xo_encoder_handle(xop, XO_OP_OPEN_INSTANCE, name, NULL);
7051 xo_depth_change(xop, name, 1, 1, XSS_OPEN_INSTANCE, xo_stack_flags(flags));
7057 xo_open_instance_hf (xo_handle_t *xop, xo_xsf_flags_t flags, const char *name)
7059 return xo_transition(xop, flags, name, XSS_OPEN_INSTANCE);
7063 xo_open_instance_h (xo_handle_t *xop, const char *name)
7065 return xo_open_instance_hf(xop, 0, name);
7069 xo_open_instance (const char *name)
7071 return xo_open_instance_hf(NULL, 0, name);
7075 xo_open_instance_hd (xo_handle_t *xop, const char *name)
7077 return xo_open_instance_hf(xop, XOF_DTRT, name);
7081 xo_open_instance_d (const char *name)
7083 return xo_open_instance_hf(NULL, XOF_DTRT, name);
7087 xo_do_close_instance (xo_handle_t *xop, const char *name)
7089 xop = xo_default(xop);
7092 const char *ppn = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : "";
7093 const char *pre_nl = "";
7096 xo_stack_t *xsp = &xop->xo_stack[xop->xo_depth];
7098 name = xsp->xs_name;
7100 int len = strlen(name) + 1;
7101 /* We need to make a local copy; xo_depth_change will free it */
7102 char *cp = alloca(len);
7103 memcpy(cp, name, len);
7105 } else if (!(xsp->xs_flags & XSF_DTRT)) {
7106 xo_failure(xop, "missing name without 'dtrt' mode");
7107 name = XO_FAILURE_NAME;
7111 switch (xo_style(xop)) {
7113 xo_depth_change(xop, name, -1, -1, XSS_CLOSE_INSTANCE, 0);
7114 rc = xo_printf(xop, "%*s</%s>%s", xo_indent(xop), "", name, ppn);
7118 pre_nl = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : "";
7120 xo_depth_change(xop, name, -1, -1, XSS_CLOSE_INSTANCE, 0);
7121 rc = xo_printf(xop, "%s%*s}", pre_nl, xo_indent(xop), "");
7122 xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST;
7127 xo_depth_change(xop, name, -1, 0, XSS_CLOSE_INSTANCE, 0);
7130 case XO_STYLE_SDPARAMS:
7133 case XO_STYLE_ENCODER:
7134 xo_depth_change(xop, name, -1, 0, XSS_CLOSE_INSTANCE, 0);
7135 rc = xo_encoder_handle(xop, XO_OP_CLOSE_INSTANCE, name, NULL);
7143 xo_close_instance_h (xo_handle_t *xop, const char *name)
7145 return xo_transition(xop, 0, name, XSS_CLOSE_INSTANCE);
7149 xo_close_instance (const char *name)
7151 return xo_close_instance_h(NULL, name);
7155 xo_close_instance_hd (xo_handle_t *xop)
7157 return xo_close_instance_h(xop, NULL);
7161 xo_close_instance_d (void)
7163 return xo_close_instance_h(NULL, NULL);
7167 xo_do_close_all (xo_handle_t *xop, xo_stack_t *limit)
7171 xo_xsf_flags_t flags;
7173 for (xsp = &xop->xo_stack[xop->xo_depth]; xsp >= limit; xsp--) {
7174 switch (xsp->xs_state) {
7180 case XSS_OPEN_CONTAINER:
7181 rc = xo_do_close_container(xop, NULL);
7185 rc = xo_do_close_list(xop, NULL);
7188 case XSS_OPEN_INSTANCE:
7189 rc = xo_do_close_instance(xop, NULL);
7192 case XSS_OPEN_LEAF_LIST:
7193 rc = xo_do_close_leaf_list(xop, NULL);
7197 flags = xsp->xs_flags & XSF_MARKER_FLAGS;
7198 xo_depth_change(xop, xsp->xs_name, -1, 0, XSS_MARKER, 0);
7199 xop->xo_stack[xop->xo_depth].xs_flags |= flags;
7205 xo_failure(xop, "close %d failed: %d", xsp->xs_state, rc);
7212 * This function is responsible for clearing out whatever is needed
7213 * to get to the desired state, if possible.
7216 xo_do_close (xo_handle_t *xop, const char *name, xo_state_t new_state)
7218 xo_stack_t *xsp, *limit = NULL;
7220 xo_state_t need_state = new_state;
7222 if (new_state == XSS_CLOSE_CONTAINER)
7223 need_state = XSS_OPEN_CONTAINER;
7224 else if (new_state == XSS_CLOSE_LIST)
7225 need_state = XSS_OPEN_LIST;
7226 else if (new_state == XSS_CLOSE_INSTANCE)
7227 need_state = XSS_OPEN_INSTANCE;
7228 else if (new_state == XSS_CLOSE_LEAF_LIST)
7229 need_state = XSS_OPEN_LEAF_LIST;
7230 else if (new_state == XSS_MARKER)
7231 need_state = XSS_MARKER;
7233 return 0; /* Unknown or useless new states are ignored */
7235 for (xsp = &xop->xo_stack[xop->xo_depth]; xsp > xop->xo_stack; xsp--) {
7237 * Marker's normally stop us from going any further, unless
7238 * we are popping a marker (new_state == XSS_MARKER).
7240 if (xsp->xs_state == XSS_MARKER && need_state != XSS_MARKER) {
7242 xo_failure(xop, "close (xo_%s) fails at marker '%s'; "
7244 xo_state_name(new_state),
7245 xsp->xs_name, name);
7250 xo_failure(xop, "close stops at marker '%s'", xsp->xs_name);
7255 if (xsp->xs_state != need_state)
7258 if (name && xsp->xs_name && strcmp(name, xsp->xs_name) != 0)
7265 if (limit == NULL) {
7266 xo_failure(xop, "xo_%s can't find match for '%s'",
7267 xo_state_name(new_state), name);
7271 rc = xo_do_close_all(xop, limit);
7277 * We are in a given state and need to transition to the new state.
7280 xo_transition (xo_handle_t *xop, xo_xsf_flags_t flags, const char *name,
7281 xo_state_t new_state)
7285 int old_state, on_marker;
7287 xop = xo_default(xop);
7290 xsp = &xop->xo_stack[xop->xo_depth];
7291 old_state = xsp->xs_state;
7292 on_marker = (old_state == XSS_MARKER);
7294 /* If there's a marker on top of the stack, we need to find a real state */
7295 while (old_state == XSS_MARKER) {
7296 if (xsp == xop->xo_stack)
7299 old_state = xsp->xs_state;
7303 * At this point, the list of possible states are:
7304 * XSS_INIT, XSS_OPEN_CONTAINER, XSS_OPEN_LIST,
7305 * XSS_OPEN_INSTANCE, XSS_OPEN_LEAF_LIST, XSS_DISCARDING
7307 switch (XSS_TRANSITION(old_state, new_state)) {
7310 case XSS_TRANSITION(XSS_INIT, XSS_OPEN_CONTAINER):
7311 case XSS_TRANSITION(XSS_OPEN_INSTANCE, XSS_OPEN_CONTAINER):
7312 case XSS_TRANSITION(XSS_OPEN_CONTAINER, XSS_OPEN_CONTAINER):
7313 rc = xo_do_open_container(xop, flags, name);
7316 case XSS_TRANSITION(XSS_OPEN_LIST, XSS_OPEN_CONTAINER):
7318 goto marker_prevents_close;
7319 rc = xo_do_close_list(xop, NULL);
7321 goto open_container;
7324 case XSS_TRANSITION(XSS_OPEN_LEAF_LIST, XSS_OPEN_CONTAINER):
7326 goto marker_prevents_close;
7327 rc = xo_do_close_leaf_list(xop, NULL);
7329 goto open_container;
7332 /*close_container:*/
7333 case XSS_TRANSITION(XSS_OPEN_CONTAINER, XSS_CLOSE_CONTAINER):
7335 goto marker_prevents_close;
7336 rc = xo_do_close(xop, name, new_state);
7339 case XSS_TRANSITION(XSS_INIT, XSS_CLOSE_CONTAINER):
7340 /* This is an exception for "xo --close" */
7341 rc = xo_do_close_container(xop, name);
7344 case XSS_TRANSITION(XSS_OPEN_LIST, XSS_CLOSE_CONTAINER):
7345 case XSS_TRANSITION(XSS_OPEN_INSTANCE, XSS_CLOSE_CONTAINER):
7347 goto marker_prevents_close;
7348 rc = xo_do_close(xop, name, new_state);
7351 case XSS_TRANSITION(XSS_OPEN_LEAF_LIST, XSS_CLOSE_CONTAINER):
7353 goto marker_prevents_close;
7354 rc = xo_do_close_leaf_list(xop, NULL);
7356 rc = xo_do_close(xop, name, new_state);
7360 case XSS_TRANSITION(XSS_INIT, XSS_OPEN_LIST):
7361 case XSS_TRANSITION(XSS_OPEN_CONTAINER, XSS_OPEN_LIST):
7362 case XSS_TRANSITION(XSS_OPEN_INSTANCE, XSS_OPEN_LIST):
7363 rc = xo_do_open_list(xop, flags, name);
7366 case XSS_TRANSITION(XSS_OPEN_LIST, XSS_OPEN_LIST):
7368 goto marker_prevents_close;
7369 rc = xo_do_close_list(xop, NULL);
7374 case XSS_TRANSITION(XSS_OPEN_LEAF_LIST, XSS_OPEN_LIST):
7376 goto marker_prevents_close;
7377 rc = xo_do_close_leaf_list(xop, NULL);
7383 case XSS_TRANSITION(XSS_OPEN_LIST, XSS_CLOSE_LIST):
7385 goto marker_prevents_close;
7386 rc = xo_do_close(xop, name, new_state);
7389 case XSS_TRANSITION(XSS_INIT, XSS_CLOSE_LIST):
7390 case XSS_TRANSITION(XSS_OPEN_CONTAINER, XSS_CLOSE_LIST):
7391 case XSS_TRANSITION(XSS_OPEN_INSTANCE, XSS_CLOSE_LIST):
7392 case XSS_TRANSITION(XSS_OPEN_LEAF_LIST, XSS_CLOSE_LIST):
7393 rc = xo_do_close(xop, name, new_state);
7397 case XSS_TRANSITION(XSS_OPEN_LIST, XSS_OPEN_INSTANCE):
7398 rc = xo_do_open_instance(xop, flags, name);
7401 case XSS_TRANSITION(XSS_INIT, XSS_OPEN_INSTANCE):
7402 case XSS_TRANSITION(XSS_OPEN_CONTAINER, XSS_OPEN_INSTANCE):
7403 rc = xo_do_open_list(xop, flags, name);
7408 case XSS_TRANSITION(XSS_OPEN_INSTANCE, XSS_OPEN_INSTANCE):
7410 rc = xo_do_open_list(xop, flags, name);
7412 rc = xo_do_close_instance(xop, NULL);
7418 case XSS_TRANSITION(XSS_OPEN_LEAF_LIST, XSS_OPEN_INSTANCE):
7420 goto marker_prevents_close;
7421 rc = xo_do_close_leaf_list(xop, NULL);
7427 case XSS_TRANSITION(XSS_OPEN_INSTANCE, XSS_CLOSE_INSTANCE):
7429 goto marker_prevents_close;
7430 rc = xo_do_close_instance(xop, name);
7433 case XSS_TRANSITION(XSS_INIT, XSS_CLOSE_INSTANCE):
7434 /* This one makes no sense; ignore it */
7435 xo_failure(xop, "xo_close_instance ignored when called from "
7436 "initial state ('%s')", name ?: "(unknown)");
7439 case XSS_TRANSITION(XSS_OPEN_CONTAINER, XSS_CLOSE_INSTANCE):
7440 case XSS_TRANSITION(XSS_OPEN_LIST, XSS_CLOSE_INSTANCE):
7442 goto marker_prevents_close;
7443 rc = xo_do_close(xop, name, new_state);
7446 case XSS_TRANSITION(XSS_OPEN_LEAF_LIST, XSS_CLOSE_INSTANCE):
7448 goto marker_prevents_close;
7449 rc = xo_do_close_leaf_list(xop, NULL);
7451 rc = xo_do_close(xop, name, new_state);
7455 case XSS_TRANSITION(XSS_OPEN_CONTAINER, XSS_OPEN_LEAF_LIST):
7456 case XSS_TRANSITION(XSS_OPEN_INSTANCE, XSS_OPEN_LEAF_LIST):
7457 case XSS_TRANSITION(XSS_INIT, XSS_OPEN_LEAF_LIST):
7458 rc = xo_do_open_leaf_list(xop, flags, name);
7461 case XSS_TRANSITION(XSS_OPEN_LIST, XSS_OPEN_LEAF_LIST):
7462 case XSS_TRANSITION(XSS_OPEN_LEAF_LIST, XSS_OPEN_LEAF_LIST):
7464 goto marker_prevents_close;
7465 rc = xo_do_close_list(xop, NULL);
7467 goto open_leaf_list;
7470 /*close_leaf_list:*/
7471 case XSS_TRANSITION(XSS_OPEN_LEAF_LIST, XSS_CLOSE_LEAF_LIST):
7473 goto marker_prevents_close;
7474 rc = xo_do_close_leaf_list(xop, name);
7477 case XSS_TRANSITION(XSS_INIT, XSS_CLOSE_LEAF_LIST):
7478 /* Makes no sense; ignore */
7479 xo_failure(xop, "xo_close_leaf_list ignored when called from "
7480 "initial state ('%s')", name ?: "(unknown)");
7483 case XSS_TRANSITION(XSS_OPEN_CONTAINER, XSS_CLOSE_LEAF_LIST):
7484 case XSS_TRANSITION(XSS_OPEN_LIST, XSS_CLOSE_LEAF_LIST):
7485 case XSS_TRANSITION(XSS_OPEN_INSTANCE, XSS_CLOSE_LEAF_LIST):
7487 goto marker_prevents_close;
7488 rc = xo_do_close(xop, name, new_state);
7492 case XSS_TRANSITION(XSS_OPEN_CONTAINER, XSS_EMIT):
7493 case XSS_TRANSITION(XSS_OPEN_INSTANCE, XSS_EMIT):
7496 case XSS_TRANSITION(XSS_OPEN_LIST, XSS_EMIT):
7498 goto marker_prevents_close;
7499 rc = xo_do_close(xop, NULL, XSS_CLOSE_LIST);
7502 case XSS_TRANSITION(XSS_INIT, XSS_EMIT):
7505 case XSS_TRANSITION(XSS_OPEN_LEAF_LIST, XSS_EMIT):
7507 goto marker_prevents_close;
7508 rc = xo_do_close_leaf_list(xop, NULL);
7512 case XSS_TRANSITION(XSS_INIT, XSS_EMIT_LEAF_LIST):
7513 case XSS_TRANSITION(XSS_OPEN_CONTAINER, XSS_EMIT_LEAF_LIST):
7514 case XSS_TRANSITION(XSS_OPEN_INSTANCE, XSS_EMIT_LEAF_LIST):
7515 rc = xo_do_open_leaf_list(xop, flags, name);
7518 case XSS_TRANSITION(XSS_OPEN_LEAF_LIST, XSS_EMIT_LEAF_LIST):
7521 case XSS_TRANSITION(XSS_OPEN_LIST, XSS_EMIT_LEAF_LIST):
7523 * We need to be backward compatible with the pre-xo_open_leaf_list
7524 * API, where both lists and leaf-lists were opened as lists. So
7525 * if we find an open list that hasn't had anything written to it,
7531 xo_failure(xop, "unknown transition: (%u -> %u)",
7532 xsp->xs_state, new_state);
7535 /* Handle the flush flag */
7536 if (rc >= 0 && XOF_ISSET(xop, XOF_FLUSH))
7537 if (xo_flush_h(xop))
7542 marker_prevents_close:
7543 xo_failure(xop, "marker '%s' prevents transition from %s to %s",
7544 xop->xo_stack[xop->xo_depth].xs_name,
7545 xo_state_name(old_state), xo_state_name(new_state));
7550 xo_open_marker_h (xo_handle_t *xop, const char *name)
7552 xop = xo_default(xop);
7554 xo_depth_change(xop, name, 1, 0, XSS_MARKER,
7555 xop->xo_stack[xop->xo_depth].xs_flags & XSF_MARKER_FLAGS);
7561 xo_open_marker (const char *name)
7563 return xo_open_marker_h(NULL, name);
7567 xo_close_marker_h (xo_handle_t *xop, const char *name)
7569 xop = xo_default(xop);
7571 return xo_do_close(xop, name, XSS_MARKER);
7575 xo_close_marker (const char *name)
7577 return xo_close_marker_h(NULL, name);
7581 * Record custom output functions into the xo handle, allowing
7582 * integration with a variety of output frameworks.
7585 xo_set_writer (xo_handle_t *xop, void *opaque, xo_write_func_t write_func,
7586 xo_close_func_t close_func, xo_flush_func_t flush_func)
7588 xop = xo_default(xop);
7590 xop->xo_opaque = opaque;
7591 xop->xo_write = write_func;
7592 xop->xo_close = close_func;
7593 xop->xo_flush = flush_func;
7597 xo_set_allocator (xo_realloc_func_t realloc_func, xo_free_func_t free_func)
7599 xo_realloc = realloc_func;
7600 xo_free = free_func;
7604 xo_flush_h (xo_handle_t *xop)
7608 xop = xo_default(xop);
7610 switch (xo_style(xop)) {
7611 case XO_STYLE_ENCODER:
7612 xo_encoder_handle(xop, XO_OP_FLUSH, NULL, NULL);
7616 if (rc >= 0 && xop->xo_flush)
7617 if (xop->xo_flush(xop->xo_opaque) < 0)
7626 return xo_flush_h(NULL);
7630 xo_finish_h (xo_handle_t *xop)
7632 const char *cp = "";
7633 xop = xo_default(xop);
7635 if (!XOF_ISSET(xop, XOF_NO_CLOSE))
7636 xo_do_close_all(xop, xop->xo_stack);
7638 switch (xo_style(xop)) {
7640 if (!XOF_ISSET(xop, XOF_NO_TOP)) {
7641 if (XOIF_ISSET(xop, XOIF_TOP_EMITTED))
7642 XOIF_CLEAR(xop, XOIF_TOP_EMITTED); /* Turn off before output */
7645 xo_printf(xop, "%*s%s}\n",xo_indent(xop), "", cp);
7649 case XO_STYLE_ENCODER:
7650 xo_encoder_handle(xop, XO_OP_FINISH, NULL, NULL);
7654 return xo_flush_h(xop);
7660 return xo_finish_h(NULL);
7664 * xo_finish_atexit is suitable for atexit() calls, to force clear up
7665 * and finalizing output.
7668 xo_finish_atexit (void)
7670 (void) xo_finish_h(NULL);
7674 * Generate an error message, such as would be displayed on stderr
7677 xo_error_hv (xo_handle_t *xop, const char *fmt, va_list vap)
7679 xop = xo_default(xop);
7682 * If the format string doesn't end with a newline, we pop
7685 int len = strlen(fmt);
7686 if (len > 0 && fmt[len - 1] != '\n') {
7687 char *newfmt = alloca(len + 2);
7688 memcpy(newfmt, fmt, len);
7694 switch (xo_style(xop)) {
7696 vfprintf(stderr, fmt, vap);
7700 va_copy(xop->xo_vap, vap);
7702 xo_buf_append_div(xop, "error", 0, NULL, 0, fmt, strlen(fmt), NULL, 0);
7704 if (XOIF_ISSET(xop, XOIF_DIV_OPEN))
7709 va_end(xop->xo_vap);
7710 bzero(&xop->xo_vap, sizeof(xop->xo_vap));
7715 va_copy(xop->xo_vap, vap);
7717 xo_open_container_h(xop, "error");
7718 xo_format_value(xop, "message", 7, fmt, strlen(fmt), NULL, 0, 0);
7719 xo_close_container_h(xop, "error");
7721 va_end(xop->xo_vap);
7722 bzero(&xop->xo_vap, sizeof(xop->xo_vap));
7725 case XO_STYLE_SDPARAMS:
7726 case XO_STYLE_ENCODER:
7732 xo_error_h (xo_handle_t *xop, const char *fmt, ...)
7737 xo_error_hv(xop, fmt, vap);
7742 * Generate an error message, such as would be displayed on stderr
7745 xo_error (const char *fmt, ...)
7750 xo_error_hv(NULL, fmt, vap);
7755 * Parse any libxo-specific options from the command line, removing them
7756 * so the main() argument parsing won't see them. We return the new value
7757 * for argc or -1 for error. If an error occurred, the program should
7758 * exit. A suitable error message has already been displayed.
7761 xo_parse_args (int argc, char **argv)
7763 static char libxo_opt[] = "--libxo";
7767 /* Save our program name for xo_err and friends */
7768 xo_program = argv[0];
7769 cp = strrchr(xo_program, '/');
7771 xo_program = cp + 1;
7773 for (save = i = 1; i < argc; i++) {
7775 || strncmp(argv[i], libxo_opt, sizeof(libxo_opt) - 1) != 0) {
7777 argv[save] = argv[i];
7782 cp = argv[i] + sizeof(libxo_opt) - 1;
7786 xo_warnx("missing libxo option");
7790 if (xo_set_options(NULL, cp) < 0)
7792 } else if (*cp == ':') {
7793 if (xo_set_options(NULL, cp) < 0)
7796 } else if (*cp == '=') {
7797 if (xo_set_options(NULL, ++cp) < 0)
7800 } else if (*cp == '-') {
7802 if (strcmp(cp, "check") == 0) {
7806 xo_warnx("unknown libxo option: '%s'", argv[i]);
7810 xo_warnx("unknown libxo option: '%s'", argv[i]);
7820 * Debugging function that dumps the current stack of open libxo constructs,
7821 * suitable for calling from the debugger.
7824 xo_dump_stack (xo_handle_t *xop)
7829 xop = xo_default(xop);
7831 fprintf(stderr, "Stack dump:\n");
7833 xsp = xop->xo_stack;
7834 for (i = 1, xsp++; i <= xop->xo_depth; i++, xsp++) {
7835 fprintf(stderr, " [%d] %s '%s' [%x]\n",
7836 i, xo_state_name(xsp->xs_state),
7837 xsp->xs_name ?: "--", xsp->xs_flags);
7842 * Record the program name used for error messages
7845 xo_set_program (const char *name)
7851 xo_set_version_h (xo_handle_t *xop, const char *version)
7853 xop = xo_default(xop);
7855 if (version == NULL || strchr(version, '"') != NULL)
7858 if (!xo_style_is_encoding(xop))
7861 switch (xo_style(xop)) {
7863 /* For XML, we record this as an attribute for the first tag */
7864 xo_attr_h(xop, "__version", "%s", version);
7869 * For JSON, we record the version string in our handle, and emit
7870 * it in xo_emit_top.
7872 xop->xo_version = xo_strndup(version, -1);
7875 case XO_STYLE_ENCODER:
7876 xo_encoder_handle(xop, XO_OP_VERSION, NULL, version);
7882 * Set the version number for the API content being carried thru
7886 xo_set_version (const char *version)
7888 xo_set_version_h(NULL, version);
7892 * Generate a warning. Normally, this is a text message written to
7893 * standard error. If the XOF_WARN_XML flag is set, then we generate
7894 * XMLified content on standard output.
7897 xo_emit_warn_hcv (xo_handle_t *xop, int as_warning, int code,
7898 const char *fmt, va_list vap)
7900 xop = xo_default(xop);
7905 xo_open_marker_h(xop, "xo_emit_warn_hcv");
7906 xo_open_container_h(xop, as_warning ? "__warning" : "__error");
7909 xo_emit("{wc:program}", xo_program);
7911 if (xo_style(xop) == XO_STYLE_XML || xo_style(xop) == XO_STYLE_JSON) {
7915 bzero(&temp, sizeof(temp));
7916 temp.xo_style = XO_STYLE_TEXT;
7917 xo_buf_init(&temp.xo_data);
7918 xo_depth_check(&temp, XO_DEPTH);
7921 (void) xo_emit_hv(&temp, fmt, ap);
7924 xo_buffer_t *src = &temp.xo_data;
7925 xo_format_value(xop, "message", 7, src->xb_bufp,
7926 src->xb_curp - src->xb_bufp, NULL, 0, 0);
7928 xo_free(temp.xo_stack);
7929 xo_buf_cleanup(src);
7932 (void) xo_emit_hv(xop, fmt, vap);
7934 int len = strlen(fmt);
7935 if (len > 0 && fmt[len - 1] != '\n') {
7937 const char *msg = strerror(code);
7939 xo_emit_h(xop, ": {G:strerror}{g:error/%s}", msg);
7944 xo_close_marker_h(xop, "xo_emit_warn_hcv");
7949 xo_emit_warn_hc (xo_handle_t *xop, int code, const char *fmt, ...)
7954 xo_emit_warn_hcv(xop, 1, code, fmt, vap);
7959 xo_emit_warn_c (int code, const char *fmt, ...)
7964 xo_emit_warn_hcv(NULL, 1, code, fmt, vap);
7969 xo_emit_warn (const char *fmt, ...)
7975 xo_emit_warn_hcv(NULL, 1, code, fmt, vap);
7980 xo_emit_warnx (const char *fmt, ...)
7985 xo_emit_warn_hcv(NULL, 1, -1, fmt, vap);
7990 xo_emit_err_v (int eval, int code, const char *fmt, va_list vap)
7992 xo_emit_warn_hcv(NULL, 0, code, fmt, vap);
7998 xo_emit_err (int eval, const char *fmt, ...)
8003 xo_emit_err_v(0, code, fmt, vap);
8009 xo_emit_errx (int eval, const char *fmt, ...)
8014 xo_emit_err_v(0, -1, fmt, vap);
8021 xo_emit_errc (int eval, int code, const char *fmt, ...)
8026 xo_emit_warn_hcv(NULL, 0, code, fmt, vap);
8033 * Get the opaque private pointer for an xo handle
8036 xo_get_private (xo_handle_t *xop)
8038 xop = xo_default(xop);
8039 return xop->xo_private;
8043 * Set the opaque private pointer for an xo handle.
8046 xo_set_private (xo_handle_t *xop, void *opaque)
8048 xop = xo_default(xop);
8049 xop->xo_private = opaque;
8053 * Get the encoder function
8056 xo_get_encoder (xo_handle_t *xop)
8058 xop = xo_default(xop);
8059 return xop->xo_encoder;
8063 * Record an encoder callback function in an xo handle.
8066 xo_set_encoder (xo_handle_t *xop, xo_encoder_func_t encoder)
8068 xop = xo_default(xop);
8070 xop->xo_style = XO_STYLE_ENCODER;
8071 xop->xo_encoder = encoder;