3 fmtinstall, dofmt, dorfmt, fmtprint, fmtvprint, fmtrune, fmtstrcpy, fmtrunestrcpy, fmtfdinit, fmtfdflush, fmtstrinit, fmtstrflush, runefmtstrinit, runefmtstrflush, errfmt \- support for user-defined print formats and output routines
11 .ta \w' 'u +\w' 'u +\w' 'u +\w' 'u +\w' 'u
12 typedef struct Fmt Fmt;
14 uchar runes; /* output buffer is runes or chars? */
15 void *start; /* of buffer */
16 void *to; /* current place in the buffer */
17 void *stop; /* end of the buffer; overwritten if flush fails */
18 int (*flush)(Fmt*); /* called when to == stop */
19 void *farg; /* to make flush a closure */
20 int nfmt; /* num chars formatted so far */
21 va_list args; /* args passed to dofmt */
22 int r; /* % format Rune */
30 FmtLeft = FmtWidth << 1,
31 FmtPrec = FmtLeft << 1,
32 FmtSharp = FmtPrec << 1,
33 FmtSpace = FmtSharp << 1,
34 FmtSign = FmtSpace << 1,
35 FmtZero = FmtSign << 1,
36 FmtUnsigned = FmtZero << 1,
37 FmtShort = FmtUnsigned << 1,
38 FmtLong = FmtShort << 1,
39 FmtVLong = FmtLong << 1,
40 FmtComma = FmtVLong << 1,
42 FmtFlag = FmtComma << 1
51 int fmtfdinit(Fmt *f, int fd, char *buf, int nbuf);
54 int fmtfdflush(Fmt *f);
57 int fmtstrinit(Fmt *f);
60 char* fmtstrflush(Fmt *f);
63 int runefmtstrinit(Fmt *f);
66 Rune* runefmtstrflush(Fmt *f);
70 int fmtinstall(int c, int (*fn)(Fmt*));
73 int dofmt(Fmt *f, char *fmt);
76 int dorfmt(Fmt*, Rune *fmt);
79 int fmtprint(Fmt *f, char *fmt, ...);
82 int fmtvprint(Fmt *f, char *fmt, va_list v);
85 int fmtrune(Fmt *f, int r);
88 int fmtstrcpy(Fmt *f, char *s);
91 int fmtrunestrcpy(Fmt *f, Rune *s);
96 The interface described here allows the construction of custom
98 verbs and output routines.
99 In essence, they provide access to the workings of the formatted print code.
103 suite maintains its state with a data structure called
107 or its relatives initializes a
109 structure, passes it to subsidiary routines to process the output,
110 and finishes by emitting any saved state recorded in the
114 are unimportant to outside users, except insofar as the general
115 design influences the interface.
118 records whether the output is in runes or bytes,
119 the verb being processed, its precision and width,
120 and buffering parameters.
121 Most important, it also records a
123 routine that the library will call if a buffer overflows.
124 When printing to a file descriptor, the flush routine will
125 emit saved characters and reset the buffer; when printing
126 to an allocated string, it will resize the string to receive more output.
127 The flush routine is nil when printing to fixed-size buffers.
128 User code need never provide a flush routine; this is done internally
130 .SS Custom output routines
131 To write a custom output routine, such as an error handler that
132 formats and prints custom error messages, the output sequence can be run
133 from outside the library using the routines described here.
134 There are two main cases: output to an open file descriptor
135 and output to a string.
137 To write to a file descriptor, call
139 to initialize the local
143 giving the file descriptor
153 to generate the output.
160 except that the characters are buffered until
162 is called and the return value is either 0 or \-1.
163 A typical example of this sequence appears in the Examples section.
165 The same basic sequence applies when outputting to an allocated string:
174 to generate the output.
177 will return the allocated string, which should be freed after use.
178 To output to a rune string, use
181 .IR runefmtstrflush .
182 Regardless of the output style or type,
186 generates the characters.
187 .SS Custom format verbs
189 is used to install custom verbs and flags labeled by character
191 which may be any non-zero Unicode character.
193 should be declared as
200 is the flag or verb character to cause
207 are the width and precision, and
209 the decoded flags for the verb (see
211 for a description of these items).
212 The standard flag values are:
236 identify whether a width and precision were specified.
239 is passed a pointer to the
241 structure recording the state of the output.
244 is a verb (rather than a flag),
248 to fetch its argument from the list,
249 then format it, and return zero.
255 All interpretation of
260 is left up to the conversion routine.
262 returns 0 if the installation succeeds, \-1 if it fails.
268 help prepare output in custom conversion routines.
269 However, these functions clear the width, precision, and flags.
270 Both functions return 0 for success and \-1 for failure.
276 are the underlying formatters; they
277 use the existing contents of
279 and should be called only by sophisticated conversion routines.
280 These routines return the number of characters (bytes of UTF or runes)
283 Some internal functions may be useful to format primitive types.
284 They honor the width, precision and flags as described in
287 formats a single character
293 formats a rune string
296 formats the system error string.
297 All these routines return zero for successful execution.
298 Conversion routines that call these functions will work properly
299 regardless of whether the output is bytes or runes.
302 .\" describes the C directive
305 .\" that can be used to provide type-checking for custom print verbs and output routines.
307 This function prints an error message with a variable
308 number of arguments and then quits.
309 Compared to the corresponding example in
311 this version uses a smaller buffer, will never truncate
312 the output message, but might generate multiple
314 system calls to produce its output.
317 .ta 6n +6n +6n +6n +6n +6n +6n +6n +6n
318 #pragma varargck argpos error 1
320 void fatal(char *fmt, ...)
326 fmtfdinit(&f, 1, buf, sizeof buf);
327 fmtprint(&f, "fatal: ");
329 fmtvprint(&f, fmt, arg);
333 exits("fatal error");
337 This example adds a verb to print complex numbers.
345 #pragma varargck type "X" Complex
352 c = va_arg(f->args, Complex);
353 return fmtprint(f, "(%g,%g)", c.r, c.i);
358 Complex x = (Complex){ 1.5, -2.3 };
360 fmtinstall('X', Xfmt);
361 print("x = %X\en", x);
371 These routines return negative numbers or nil for errors and set