1 /* Jitter: custom contextual printing.
2 
3    Copyright (C) 2020 Luca Saiu
4    Written by Luca Saiu
5 
6    This file is part of Jitter.
7 
8    Jitter is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation, either version 3 of the License, or
11    (at your option) any later version.
12 
13    Jitter is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with Jitter.  If not, see <http://www.gnu.org/licenses/>. */
20 
21 
22 #ifndef JITTER_PRINT_H_
23 #define JITTER_PRINT_H_
24 
25 #include <stdio.h>  /* For the definition of FILE . */
26 
27 #include <jitter/jitter.h>
28 #include <jitter/jitter-dynamic-buffer.h>
29 
30 #if defined (JITTER_HAVE_LIBTEXTSTYLE)
31 #include <textstyle.h>  /* For the definition of styled_ostream_t . */
32 #endif // #if defined (JITTER_HAVE_LIBTEXTSTYLE)
33 
34 
35 
36 
37 /* Introduction.
38  * ************************************************************************** */
39 
40 /* This header defines an extensible output abstraction called "print context",
41    allowing the user to print data along with user-specified "decorations" to an
42    underlying channel such as a file descriptor, libc stream or socket, or even
43    a memory buffer.  A "print context kind" describes the behaviour of
44    operations, including the way decorations are handled; every print context
45    has a print context kind, plus an underlying channel on which to write.  A
46    few print context kinds are provided here; other use cases are possible for
47    the user to define using this documented API (pretty-printing comes to mind),
48    and might be directly provided here in the future.
49 
50    "Decorations" may express semantic attributes such as the nesting level of a
51    program form or encode cosmetic features such as fonts, colours or
52    hyperlinks, for example as implemented by GNU libtextstyle .  Some (or all)
53    decorations will have no effect in some context kinds; a user is still free
54    to write any decoration to any print context, and to reuse the same code for
55    printing to different contexts -- for example on a terminal supporting
56    colours and fonts, to a file, into a buffer in memory.
57 
58    The API is meant to be expressive enough for wrapping the functionality of
59    GNU libtextstyle for printed colour or font attributes and hyperlinks where
60    the library is available, while ignoring such functionality otherwise.
61    If the underlying channel abstraction does not support such facilities then
62    the additional styling and hyperlinking operations will have no effect, but
63    the data output operations will still behave as intended.
64    A libtextstyle print context kind is provided in a separate library, as
65    explained below, in order not to have libtextstyle as a dependency for
66    libjitter.
67 
68    It is possible in general to interleave other input/output operations on the
69    underlying channel (for example file seeking, or reading) with print context
70    operations, as long as the print context is explicitly flushed before each
71    explicit access to the underlying channel.
72 
73    This API is not by itself thread-safe: in a concurrent environment it is the
74    user's responsibility to ensure that critical sections are executed
75    atomically.
76 
77    The Jitter runtime library prints VM routines and native code disassemblies
78    through this facility, generating output to a print context provided by the
79    user. */
80 
81 /* Every function with return type int in this API returns 0 to signify success,
82    while any other result signifies failure.
83    In case of error these functions may print an unspecified number of
84    characters before failing; there is currently no support for error recovery.
85    Depending on the definition of the context kind, some of the functions
86    defined below may have no effect on the output, and simply return 0.  When
87    the context kind in question defines the operation as having any effect, any
88    operation is subject to failure.
89    Notice that beginning or ending a decoration may well be output operations,
90    subject to failure. */
91 
92 
93 /* A print context is a pointer to an opaque structure. */
94 typedef struct jitter_print_context_private *
95 jitter_print_context;
96 
97 
98 
99 
100 /* Context initialisation.
101  * ************************************************************************** */
102 
103 /* Functions making new contexts are specific to each context kind, and a few
104    can be found below.  Any context returned by a context-making function will
105    have initially no active decorations. */
106 
107 
108 
109 
110 /* Printing with contexts.
111  * ************************************************************************** */
112 
113 /* A formatted output function in the style of printf , while easy to define on
114    GNU and a few other platforms thanks to aprintf , is unfortunately
115    impractical to provide using standard C only and a generic libc, and is
116    therefore omitted in the interest of portability.  Of course the user is free
117    to employ aprintf , sprintf or any other facility in order to compose a
118    string in memory to be then printed by calling jitter_print_char_star . */
119 
120 /* Print a char object in the given context. */
121 int
122 jitter_print_char (jitter_print_context ct, char x)
123   __attribute__ ((nonnull (1)));
124 
125 /* Print char_no char objects read from memory starting at the address p,
126    including any '\0' chars.  This function is intended for binary output. */
127 int
128 jitter_print_chars (jitter_print_context ct, const char *p, size_t char_no)
129   __attribute__ ((nonnull (1, 2)));
130 
131 /* Print the pointed string in the given context, up to and not including its
132    first '\0' character.  Undefined behaviour is the string is NULL or not
133    '\0'-terminated. */
134 int
135 jitter_print_char_star (jitter_print_context ct, const char *x)
136   __attribute__ ((nonnull (1, 2)));
137 
138 /* Print a pointer in the given context, using the same syntax as printf's
139    "%p" output format. */
140 int
141 jitter_print_pointer (jitter_print_context ct, void *x)
142   __attribute__ ((nonnull (1)));
143 
144 /* Print an integer in the given context, using the given radix.  The minimum
145    supported radix is 2, and the maximum is 36.  Undefined behaviour for
146    unsupported radixes. */
147 int
148 jitter_print_short (jitter_print_context ct, int radix, short x)
149   __attribute__ ((nonnull (1)));
150 int
151 jitter_print_int (jitter_print_context ct, int radix, int x)
152   __attribute__ ((nonnull (1)));
153 int
154 jitter_print_long (jitter_print_context ct, int radix, long x)
155   __attribute__ ((nonnull (1)));
156 int
157 jitter_print_long_long (jitter_print_context ct, int radix, jitter_long_long x)
158   __attribute__ ((nonnull (1)));
159 int
160 jitter_print_ushort (jitter_print_context ct, int radix, unsigned short x)
161   __attribute__ ((nonnull (1)));
162 int
163 jitter_print_uint (jitter_print_context ct, int radix, unsigned int x)
164   __attribute__ ((nonnull (1)));
165 int
166 jitter_print_ulong (jitter_print_context ct, int radix, unsigned long x)
167   __attribute__ ((nonnull (1)));
168 int
169 jitter_print_ulong_long (jitter_print_context ct, int radix,
170                          jitter_ulong_long x)
171   __attribute__ ((nonnull (1)));
172 
173 /* Print an floating-point number in the given context.  Right now only the
174    equivalent of printf's "%f" output format is currently supported; for
175    printing in other formats the user will have to resort to sprintf or
176    custom alternatives. */
177 int
178 jitter_print_float (jitter_print_context ct, float x)
179   __attribute__ ((nonnull (1)));
180 int
181 jitter_print_double (jitter_print_context ct, double x)
182   __attribute__ ((nonnull (1)));
183 #if defined (JITTER_HAVE_LONG_DOUBLE)
184 int
185 jitter_print_long_double (jitter_print_context ct, long double x)
186   __attribute__ ((nonnull (1)));
187 #endif // #if defined (JITTER_HAVE_LONG_DOUBLE)
188 
189 /* Convenience aliases for printing integer or floating-point data having Jitter
190    types.  Each of these definition is just a macro resolving to one of the
191    previous integer-printing function names, according to the configuration. */
192 #if   JITTER_SIZEOF_VOID_P == JITTER_SIZEOF_SHORT
193   /* This will not happen on GNU, but one check costs almost nothing. */
194 # define  jitter_print_jitter_int jitter_print_short
195 # define  jitter_print_jitter_uint jitter_print_ushort
196 #elif JITTER_SIZEOF_VOID_P == JITTER_SIZEOF_INT
197 # define  jitter_print_jitter_int jitter_print_int
198 # define  jitter_print_jitter_uint jitter_print_uint
199 #elif JITTER_SIZEOF_VOID_P == JITTER_SIZEOF_LONG
200 # define  jitter_print_jitter_int jitter_print_long
201 # define  jitter_print_jitter_uint jitter_print_ulong
202 #elif defined (JITTER_HAVE_LONG_LONG_INT) \
203       && (JITTER_SIZEOF_VOID_P == JITTER_SIZEOF_LONG_LONG)
204 # define  jitter_print_jitter_int jitter_print_long_long
205 # define  jitter_print_jitter_uint jitter_print_ulong_long
206 #else
207 # error "this should never happen: sizeof (void *) has an unexpected value"
208 #endif
209 #if    JITTER_SIZEOF_VOID_P == JITTER_SIZEOF_FLOAT
210 # define  jitter_print_jitter_float jitter_print_float
211 #elif  JITTER_SIZEOF_VOID_P == JITTER_SIZEOF_DOUBLE
212 # define  jitter_print_jitter_float jitter_print_double
213 #elif  defined (JITTER_HAVE_LONG_DOUBLE) \
214        && (JITTER_SIZEOF_VOID_P == JITTER_SIZEOF_LONG_DOUBLE)
215 # define  jitter_print_jitter_float jitter_print_long_double
216 #else
217 # error "this should never happen: no floating-point type is defined with the"
218 # error "same size as void *."
219 #endif
220 
221 
222 
223 
224 /* Decorations.
225  * ************************************************************************** */
226 
227 /* Each decoration has a name, a type and a value.  Decoration may be nested
228    inside each other, and have to be "begun" or "ended" in a strictly LIFO
229    order.  The topmost decoration with a given name, if one exists, is the
230    "active" one.
231    Decoration values are dynamically typed. */
232 
233 /* The name of a decoration is a string.  User functions may pass temporary
234    strings to this API, for example always reusing the same memory; decoration
235    names (and string values: see below) are copied into internally managed
236    memory as needed. */
237 typedef char *
238 jitter_print_decoration_name;
239 
240 /* An indicator of which supported type a decoration currently has. */
241 enum jitter_print_decoration_type
242   {
243     /* The decoration value is integer. */
244     jitter_print_decoration_type_integer,
245 
246     /* The decoration value is floating-point. */
247     jitter_print_decoration_type_floating_point,
248 
249     /* The decoration value is a text string (copied into local memory) by
250        the print context subsystem. */
251     jitter_print_decoration_type_string,
252 
253     /* The decoration value is a pointer to arbitrary user memory. */
254     jitter_print_decoration_type_pointer
255   };
256 
257 /* The value of a particular decoration.  Each field is associated to one
258    value of enum jitter_print_decoration_type , defined above. */
259 union jitter_print_decoration_value
260 {
261   /* A signed integer value. */
262   jitter_int integer;
263 
264   /* A floating-point value. */
265   double floating_point;
266 
267   /* A '\0'-terminated string.  Any value given by the user is copied into
268      locally managed memory, and the user should not attempt to free the copy
269      pointed by this field. */
270   char *string;
271 
272   /* A pointer to arbitrary data supplied by the user.  Differently from the
273      string field this pointer refers user data, which is the user's
274      responsibility to keep alive until the decoration remains in use. */
275   void *pointer;
276 };
277 
278 
279 
280 
281 /* Decoration beginning.
282  * ************************************************************************** */
283 
284 /* "Beginning" a decoration in a print context makes the decoration "active" and
285    (according to the print context kind) may affect how text printed after the
286    "begin" operation looks like, until the decoration is "ended".  Decorations
287    can be nested, but must be begun and ended in a strictly LIFO order. */
288 
289 /* Begin an integer-type decoration in the pointed context, having the given
290    name and the given value. */
291 int
292 jitter_print_begin_decoration_integer (jitter_print_context ct,
293                                        jitter_print_decoration_name name,
294                                        jitter_int value)
295   __attribute__ ((nonnull (1, 2)));
296 
297 /* Begin a floating-point-type decoration in the pointed context, having the
298    given name and the given value. */
299 int
300 jitter_print_begin_decoration_floating_point (jitter_print_context ct,
301                                               jitter_print_decoration_name name,
302                                               double value)
303   __attribute__ ((nonnull (1, 2)));
304 
305 /* Begin a string-type decoration in the pointed context, having the given name
306    and the given value.  The value is copied into internally managed memory, and
307    it is the user's responsibility to dispose of her original copy. */
308 int
309 jitter_print_begin_decoration_string (jitter_print_context ct,
310                                       jitter_print_decoration_name name,
311                                       char *value)
312   __attribute__ ((nonnull (1, 2, 3)));
313 
314 /* Begin a pointer-type decoration in the pointed context, having the given name
315    and the given value.  This function, differently from
316    jitter_print_begin_decoration_string , never derefenreces the value pointer
317    but simply copies it; it is the user's responsibility to keep the pointed
318    data alive until the decoration is ended by a matching
319    jitter_print_end_decoration call. */
320 int
321 jitter_print_begin_decoration_pointer (jitter_print_context ct,
322                                        jitter_print_decoration_name name,
323                                        void *value)
324   __attribute__ ((nonnull (1, 2)));
325 
326 
327 
328 
329 /* Decoration ending.
330  * ************************************************************************** */
331 
332 /* End the last started decoration, which must have the given name.  Undefined
333    behaviour if no matching decoration was begun, or if beginning and end
334    of multiple decorations do not follow a LIFO order. */
335 int
336 jitter_print_end_decoration (jitter_print_context ct,
337                              jitter_print_decoration_name name)
338   __attribute__ ((nonnull (1, 2)));
339 
340 
341 
342 
343 /* Decoration introspection.
344  * ************************************************************************** */
345 
346 /* Check the currently active decoration in the given context.  If any exists
347    then set the memory pointed from name, type and value to pointers to relevant
348    decoration attributes in internal memory, which will remain valid as long as
349    no further decoration is begun or ended.  Set the memory pointed by the three
350    pointers to NULL otherwise. */
351 void
352 jitter_print_get_decoration (jitter_print_context ct,
353                              jitter_print_decoration_name *name_p,
354                              enum jitter_print_decoration_type **type_pp,
355                              union jitter_print_decoration_value **value_pp)
356   __attribute__ ((nonnull (1, 2, 3, 4)));
357 
358 /* Like jitter_print_get_decoration , but only consider decorations with the
359    given name.  There is no user pointer name to set in this case. */
360 void
361 jitter_print_get_decoration_named (jitter_print_context ct,
362                                    jitter_print_decoration_name name,
363                                    enum jitter_print_decoration_type **type_pp,
364                                    union jitter_print_decoration_value
365                                    **value_pp)
366   __attribute__ ((nonnull (1, 2, 3, 4)));
367 
368 
369 
370 
371 /* Convenience functions for known decorations: classes.
372  * ************************************************************************** */
373 
374 /* "Classes" describe the cosmetic look of text output.  Libtextstyle uses
375    string values for classes. */
376 
377 /* The name of class decorations. */
378 #define JITTER_PRINT_DECORATION_NAME_CLASS "class"
379 
380 /* The name of a class, as used by libtextstyle.  It is a simple
381    '\0'-terminated text string, which is copied into internally managed
382    memory as needed. */
383 typedef char *
384 jitter_print_class;
385 
386 /* Switch to using the given class for the forthcoming output, until the
387    next call to jitter_print_begin_class or to jitter_print_end_class.  Class
388    uses nest in a LIFO style: after ending a class use the previously "begun"
389    class, if any, comes back into effect. */
390 int
391 jitter_print_begin_class (jitter_print_context ct, jitter_print_class c)
392   __attribute__ ((nonnull (1, 2)));
393 
394 /* Stop using the last class given in jitter_print_begin_class, and switch back
395    to the class defined by the previoys jitter_print_begin_class call (or no
396    class, if the outermost call was matched) for the following.  Undefined
397    behaviour if there is no previous jitter_print_begin_class call to match. */
398 int
399 jitter_print_end_class (jitter_print_context ct)
400   __attribute__ ((nonnull (1)));
401 
402 /* If any class is currently active in the pointed context return its value,
403    using internal memory managed internally by the print context system; return
404    NULL otherwise.  Undefined behaviour if the current class is set to a
405    non-string value. */
406 char *
407 jitter_print_get_class (jitter_print_context ct)
408   __attribute__ ((returns_nonnull,
409                   nonnull (1)));
410 
411 
412 
413 
414 /* Convenience functions for known decorations: hyperlinks.
415  * ************************************************************************** */
416 
417 /* In Libtextstyle a "hyperlink" is a text string formatted like a URL
418    associated to output text; some recent terminal emulator actually render URLs
419    as interactive clickable links.
420    Libtextstyle hyperlinks are not nestable and always have a string type.
421    Jitter print contexts only enforce this non-nestability and the decoration
422    type when using the convenience functions in this section; otherwise generic
423    decorations are dynamically typed, and can nest. */
424 #define JITTER_PRINT_DECORATION_NAME_HYPERLINK "url"
425 
426 /* A hyperlink destination, as used by libtextstyle.  It is a simple
427    '\0'-terminated string, which gets copied into internally handled memory as
428    needed. */
429 typedef char *
430 jitter_print_url;
431 
432 /* Mark the current output as the beginning of a hyperlink to the given URL,
433    which must be a valid '\0'-terminated string.
434    Fail fatally if a hyperlink is already active at the time of the call. */
435 int
436 jitter_print_begin_hyperlink (jitter_print_context ct, jitter_print_url url)
437   __attribute__ ((nonnull (1, 2)));
438 
439 /* Mark the current output as the end of the hyperlink which was started before
440    with jitter_print_begin_hyperlink.  Undefined behaviour if there is no
441    previous jitter_print_begin_hyperlink call to match.  At the end of the
442    call the context has no longer any current hyperlink. */
443 int
444 jitter_print_end_hyperlink (jitter_print_context ct)
445   __attribute__ ((nonnull (1)));
446 
447 /* If any hyperlink is currently active in the pointed context return its value,
448    using internal memory managed internally by the print context system; return
449    NULL otherwise.  Undefined behaviour if the current hyperlink is set to a
450    non-string value. */
451 char *
452 jitter_print_get_hyperlink (jitter_print_context ct)
453   __attribute__ ((returns_nonnull,
454                   nonnull (1)));
455 
456 
457 
458 
459 /* Other functionality available in every context.
460  * ************************************************************************** */
461 
462 /* Functions making new contexts are specific to each context kind, and
463    are not defined here.  Any context returned by a context-making function
464    will have initially no class and no hyperlink destination. */
465 
466 /* Flush the output.  Return 0 on success, a non-zero value on error. */
467 int
468 jitter_print_flush (jitter_print_context ct)
469   __attribute__ ((nonnull (1)));
470 
471 /* End any still open decoration, flush the output then destroy the given
472    context.  Undefined behaviour if the context is not valid or has already
473    been destroyed.  Return 0 if all of the close and flush operations succeed,
474    a non-zero result otherwise; destroy the data structure in either case.
475    Notice that, for every reasonable print context type, destroying the
476    context should *not* close the underlying channel: it may be useful
477    for the user to perform additional input/output even after the print
478    context has been finalised. */
479 int
480 jitter_print_context_destroy (jitter_print_context ct)
481   __attribute__ ((nonnull (1)));
482 
483 
484 
485 
486 /* Predefined print context kinds: FILE * and file descriptor.
487  * ************************************************************************** */
488 
489 /* Return a fresh print context printing to the pointed libc buffered stream,
490    ignoring decorations. */
491 jitter_print_context
492 jitter_print_context_make_file_star (FILE *f)
493   __attribute__ ((warn_unused_result, returns_nonnull,
494                   nonnull (1)));
495 
496 /* Return a fresh print context printing to the given libc unbuffered file
497    descriptor, ignoring flushing and decorations. */
498 jitter_print_context
499 jitter_print_context_make_fd (int fd)
500   __attribute__ ((warn_unused_result, returns_nonnull));
501 
502 
503 
504 
505 /* Predefined print context kind: string.
506  * ************************************************************************** */
507 
508 /* Return a fresh print context "printing" into a memory buffer which
509    automatically grows to fit the output.  Such a context ignores flushing and
510    decorations. */
511 jitter_print_context
512 jitter_print_context_make_memory (void)
513   __attribute__ ((warn_unused_result, returns_nonnull));
514 
515 /* Given a print context made by jitter_print_context_make_memory, return
516    a malloc-allocated copy of its current buffer, also including any '\0'
517    chars previously printed by the user, terminated by a further '\0' char.
518    It is the user's responsibility to eventually dispose of the copied buffer
519    by calling free .
520    If the supplied length pointer is not NULL write the length of the printed
521    data (not counting the one added trailing '\0') into its pointed memory.
522 
523    Notice that the returned buffer will contain '\0' chars *before* the trailing
524    one which is appended by this function, if any were printed as single chars
525    (printing using print_char_star functions does not copy any '\0' char to the
526    output) or binary data (using print_chars functions).  This feature is
527    intended for binary formats, in which case the user should pass a non-NULL
528    value for length_p . */
529 char *
530 jitter_print_context_get_memory (jitter_print_context c, size_t *length_p)
531   __attribute__ ((warn_unused_result, returns_nonnull,
532                   nonnull (1)));
533 
534 
535 
536 
537 /* Predefined print context kind: GNU libtextstyle.
538  * ************************************************************************** */
539 
540 /* The functionality in this section is only available if Jitter was configured
541    with GNU libtextstyle support.
542    User code is easy to conditionalise by using either a Libtextstyle context,
543    or a different FILE * or file descriptor context. */
544 #if defined (JITTER_HAVE_LIBTEXTSTYLE)
545 
546 /* GNU Libtextstyle is a large library which should not be unconditionally
547    linked in Jittery VMs.  For this reason the Libtextstyle print context kind
548    is distributed in a separate optional library, libjitter-libtextstyle .  Like
549    the rest of the Jitter runtime it is built as a Libtool library, and also
550    available as a static library when Jitter is configured in sub-package mode.
551 
552    libjitter-libtextstyle requires its own initialisation and finalisation.  The
553    sources are in jitter/jitter-print-libtextstyle.c .  There is no separate
554    header file. */
555 
556 /* Initialise the Jitter print context wrapper for GNU libtextstyle.  This
557    needs to be called once before using the facility. */
558 void
559 jitter_print_libtextstyle_initialize (void);
560 
561 /* Finalise the Jitter print context wrapper for GNU libtextstyle.  This may
562    be called after the facility is no longer needed, to free up resources --
563    and, maybe mainly, in order to avoid spurious valgrind warnings.
564    It is possible to initialise the subsystem again after calling this. */
565 void
566 jitter_print_libtextstyle_finalize (void);
567 
568 /* Return a fresh print context using the given Libtextstyle ostream.  The
569    returned print context supports classes and hyperlinks.
570    This function can only called after the Jitter print context wrapper for GNU
571    libtextstyle has been initialised by jitter_print_libtextstyle_initialize . */
572 jitter_print_context
573 jitter_print_context_make_libtextstyle (styled_ostream_t ostream)
574   __attribute__ ((warn_unused_result, returns_nonnull,
575                   nonnull (1)));
576 
577 #endif /* #if defined (JITTER_HAVE_LIBTEXTSTYLE) */
578 
579 
580 
581 
582 /* Defining new print context kinds.
583  * ************************************************************************** */
584 
585 /* The user may define a custom context kind by providing functions associated
586    to fundamental operations and storing pointers to them in memory as fields
587    of a structure of type struct jitter_print_context_kind_struct .
588    Each print context contains a pointer to user-provided data, which the
589    functions within the struct jitter_print_context_kind_struct object may
590    access.
591    Since more fields within struct jitter_print_context_kind_struct may be
592    added in the future the user is strongly advised to dynamically initialise
593    such structures using jitter_print_context_kind_make_trivial , and then only
594    set the fields associated to implemented operations. */
595 struct jitter_print_context_kind_struct;
596 typedef struct jitter_print_context_kind_struct *
597 jitter_print_context_kind;
598 
599 /* A pointer to any data needed for the context.  The user is free to cast this
600    pointer to any other kind of applicable pointer; an example is the file_star
601    context defined in jitter-print.c , whose functions cast
602    jitter_print_context_data to FILE * in order to keep track of the underlying
603    libc stream. */
604 typedef void *
605 jitter_print_context_data;
606 
607 /* Return a new context kind, the function pointers fields all set to NULL.  The
608    caller is supposed to call this function and then set the fields she is
609    interested in within the struct pointed by the result, leaving the rest as
610    NULL so that the undefined operations do nothing. */
611 jitter_print_context_kind
612 jitter_print_context_kind_make_trivial (void)
613   __attribute__ ((warn_unused_result, returns_nonnull));
614 
615 /* Destroy the pointed context kind.  This is mostly provided for symmetry's
616    sake, and to silence valgrind: in practice context kinds will be allocated
617    globally and there is no real need to destroy them at finalisation.  After
618    calling this it becomes invalid to use any context of the destroyed kind. */
619 void
620 jitter_print_context_kind_destroy (jitter_print_context_kind k)
621   __attribute__ ((nonnull (1)));
622 
623 /* It is the user's responsibility to provide a convenient function for making a
624    context of her new custom kind.  The context kinds defined in the section
625    above may serve as examples: see the definition of jitter_print_initialize,
626    with the caveat that the user should use
627    jitter_print_context_kind_make_trivial rather than
628    jitter_print_context_kind_initialize_trivial .
629    See jitter/jitter-print-libtextstyle.c for an example of a context kind
630    definition which does not ignore decorations.
631 
632    Every context-making function should use this function and return its result;
633    the final user working on context, however, does not need to ever see this,
634    and should instead allocate each context with a function appropriate for its
635    kind such as jitter_print_context_make_file_star for file_star contexts. */
636 jitter_print_context
637 jitter_print_context_make (jitter_print_context_kind k,
638                            jitter_print_context_data d)
639   __attribute__ ((warn_unused_result, returns_nonnull,
640                   nonnull (1)));
641 
642 /* The fields within the struct below, along with its total size, may change in
643    future versions and the user should treat the structure as containing unknown
644    fields, by first allocating an instance through
645    jitter_print_context_kind_make_trivial and then setting its known non-NULL
646    fields. */
647 struct jitter_print_context_kind_struct
648 {
649   /* All the functions here return 0 on success and a non-zero result on
650      failure.
651 
652      If a field is NULL within a specific context kind then the corresponding
653      operation does nothing and always returns 0, with one exception: either one
654      of print_char and print_chars, or both, may be non-NULL.  If only one is
655      defined then the missing operation is emulated via the other as described
656      below. */
657 
658   /* Print the given character to the pointed underlying channel.  If the
659      underlying primitive is interrupted by a recoverable error (for example
660      of the kind where errno is set to EAGAIN) retry until success.
661      If this field is NULL but print_chars is not then this functionality is
662      emulated via the function pointed by the print_chars field, reading one
663      char's worth of memory from a temporary copy. */
664   int (* print_char) (jitter_print_context_data d, char c);
665 
666   /* Print char_no char objects read from memory starting at the address p and
667      including any '\0' characters found in the interval to the underlying
668      channel.  If the underlying primitive is interrupted by a recoverable error
669      (for example of the kind where errno is set to EAGAIN) retry until success.
670      If this field is NULL but print_char is not then this functionality is
671      emulated by a loop performing multiple calls to the function pointed by
672      the print_char field. */
673   int (* print_chars) (jitter_print_context_data d, const char *p,
674                        size_t char_no);
675 
676   /* Begin a decoration with the name, value type and value, in the pointed
677      underlying channel.  This function is called when the new decoration is
678      not active yet.
679      The pointers received here point to internally managed memory, that the
680      user should not alter. */
681   int (* begin_decoration) (jitter_print_context_data d,
682                             const jitter_print_decoration_name name,
683                             enum jitter_print_decoration_type type,
684                             const union jitter_print_decoration_value *value);
685 
686   /* End a decoration with the given name, value type and value, in the pointed
687      underlying channel.  This is always called on the last begun decoration
688      in LIFO order, with the decoration no longer active.
689      The pointers received here point to internally managed memory, that the
690      user should not alter. */
691   int (* end_decoration) (jitter_print_context_data d,
692                           const jitter_print_decoration_name name,
693                           enum jitter_print_decoration_type type,
694                           const union jitter_print_decoration_value *value);
695 
696   /* Flush the pointed underlying channel. */
697   int (* flush) (jitter_print_context_data d);
698 
699   /* Destroy the pointed print context data, without closing the underlying
700      channel.  No flushing is necessary here; the function pointed by the
701      flush field, if any, will be called at destruction time when appropriate. */
702   int (* destroy_without_flushing) (jitter_print_context_data d);
703 };
704 
705 
706 
707 
708 /* Global initialisation.
709  * ************************************************************************** */
710 
711 /* This function, which makes the predefined context kinds available, is
712    automatically called the first time any VM is initialised.  There is no
713    matching finalisation.  Not for the user. */
714 void
715 jitter_print_initialize (void);
716 
717 #endif // #ifndef JITTER_PRINT_H_
718