aboutsummaryrefslogtreecommitdiff
path: root/man/man3/fmtinstall.html
blob: 848c1333f56fab8033c5d7375d02497e72f78e55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<head>
<title>fmtinstall(3) - Plan 9 from User Space</title>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
</head>
<body bgcolor=#ffffff>
<table border=0 cellpadding=0 cellspacing=0 width=100%>
<tr height=10><td>
<tr><td width=20><td>
<tr><td width=20><td><b>FMTINSTALL(3)</b><td align=right><b>FMTINSTALL(3)</b>
<tr><td width=20><td colspan=2>
    <br>
<p><font size=+1><b>NAME     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    fmtinstall, dofmt, dorfmt, fmtprint, fmtvprint, fmtrune, fmtstrcpy,
    fmtrunestrcpy, fmtfdinit, fmtfdflush, fmtstrinit, fmtstrflush,
    runefmtstrinit, runefmtstrflush, errfmt &ndash; support for user-defined
    print formats and output routines<br>
    
</table>
<p><font size=+1><b>SYNOPSIS     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    <tt><font size=+1>#include &lt;u.h&gt;<br>
    #include &lt;libc.h&gt; 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>typedef struct Fmt &nbsp;&nbsp;&nbsp;Fmt;<br>
    struct Fmt{<br>
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

        uchar &nbsp;&nbsp;&nbsp;&nbsp;runes; &nbsp;&nbsp;&nbsp;/* output buffer is runes or chars? */<br>
        void &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*start; /* of buffer */<br>
        void &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*to; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* current place in the buffer */<br>
        void &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*stop; &nbsp;&nbsp;&nbsp;/* end of the buffer; overwritten if flush fails */<br>
        int  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(*flush)(Fmt*);/* called when to == stop */<br>
        void &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*farg; &nbsp;&nbsp;&nbsp;/* to make flush a closure */<br>
        int  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nfmt; &nbsp;&nbsp;&nbsp;&nbsp;/* num chars formatted so far */<br>
        va_list args; &nbsp;&nbsp;&nbsp;&nbsp;/* args passed to dofmt */<br>
        int  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* % format Rune */<br>
        int  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width;<br>
        int  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prec;<br>
        ulong &nbsp;&nbsp;&nbsp;&nbsp;flags;<br>
        
    </table>
    };<br>
    enum{<br>
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

        FmtWidth &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1,<br>
        FmtLeft  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtWidth &lt;&lt; 1,<br>
        FmtPrec  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtLeft &lt;&lt; 1,<br>
        FmtSharp &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtPrec &lt;&lt; 1,<br>
        FmtSpace &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtSharp &lt;&lt; 1,<br>
        FmtSign  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtSpace &lt;&lt; 1,<br>
        FmtZero  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtSign &lt;&lt; 1,<br>
        FmtUnsigned = FmtZero &lt;&lt; 1,<br>
        FmtShort &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtUnsigned &lt;&lt; 1,<br>
        FmtLong  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtShort &lt;&lt; 1,<br>
        FmtVLong &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtLong &lt;&lt; 1,<br>
        FmtComma &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtVLong &lt;&lt; 1,<br>
        FmtFlag  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= FmtComma &lt;&lt; 1<br>
        
    </table>
    };<br>
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    int &nbsp;&nbsp;&nbsp;&nbsp;fmtfdinit(Fmt *f, int fd, char *buf, int nbuf); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;fmtfdflush(Fmt *f); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;fmtstrinit(Fmt *f); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>char* fmtstrflush(Fmt *f); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;runefmtstrinit(Fmt *f); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>Rune* runefmtstrflush(Fmt *f);<br>
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;fmtinstall(int c, int (*fn)(Fmt*)); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;dofmt(Fmt *f, char *fmt); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;dorfmt(Fmt*, Rune *fmt); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;fmtprint(Fmt *f, char *fmt, ...); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;fmtvprint(Fmt *f, char *fmt, va_list v); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;fmtrune(Fmt *f, int r); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;fmtstrcpy(Fmt *f, char *s); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;fmtrunestrcpy(Fmt *f, Rune *s); 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;errfmt(Fmt *f);<br>
    </font></tt>
</table>
<p><font size=+1><b>DESCRIPTION     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    The interface described here allows the construction of custom
    <a href="../man3/print.html"><i>print</i>(3)</a> verbs and output routines. In essence, they provide access
    to the workings of the formatted print code. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    The <a href="../man3/print.html"><i>print</i>(3)</a> suite maintains its state with a data structure called
    <tt><font size=+1>Fmt</font></tt>. A typical call to <a href="../man3/print.html"><i>print</i>(3)</a> or its relatives initializes a
    <tt><font size=+1>Fmt</font></tt> structure, passes it to subsidiary routines to process the
    output, and finishes by emitting any saved state recorded in the
    <tt><font size=+1>Fmt</font></tt>. The details of the <tt><font size=+1>Fmt</font></tt> are unimportant to outside users,
    except
    insofar as the general design influences the interface. The <tt><font size=+1>Fmt</font></tt>
    records whether the output is in runes or bytes, the verb being
    processed, its precision and width, and buffering parameters.
    Most important, it also records a <i>flush</i> routine that the library
    will call if a buffer overflows. When printing to a file descriptor,
    the
    flush routine will emit saved characters and reset the buffer;
    when printing to an allocated string, it will resize the string
    to receive more output. The flush routine is nil when printing
    to fixed-size buffers. User code need never provide a flush routine;
    this is done internally by the library.<br>
    <p><font size=+1><b>Custom output routines   </b></font><br>
    To write a custom output routine, such as an error handler that
    formats and prints custom error messages, the output sequence
    can be run from outside the library using the routines described
    here. There are two main cases: output to an open file descriptor
    and output to a string. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    To write to a file descriptor, call <i>fmtfdinit</i> to initialize the
    local <tt><font size=+1>Fmt</font></tt> structure <i>f</i>, giving the file descriptor <i>fd</i>, the buffer
    <i>buf</i>, and its size <i>nbuf</i>. Then call <i>fmtprint</i> or <i>fmtvprint</i> to generate
    the output. These behave like <tt><font size=+1>fprint</font></tt> (see <a href="../man3/print.html"><i>print</i>(3)</a>) or <tt><font size=+1>vfprint</font></tt>
    except that the characters are buffered until <i>fmtfdflush</i> is called
    and the return value is either 0 or &ndash;1. A typical example of this
    sequence appears in the Examples section. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    The same basic sequence applies when outputting to an allocated
    string: call <i>fmtstrinit</i> to initialize the <tt><font size=+1>Fmt</font></tt>, then call <i>fmtprint</i>
    and <i>fmtvprint</i> to generate the output. Finally, <i>fmtstrflush</i> will
    return the allocated string, which should be freed after use.
    To output to a rune string, use <i>runefmtstrinit</i> and <i>runefmtstrflush</i>.
    Regardless of the output style or type, <i>fmtprint</i> or <i>fmtvprint</i>
    generates the characters.<br>
    <p><font size=+1><b>Custom format verbs   </b></font><br>
    <i>Fmtinstall</i> is used to install custom verbs and flags labeled by
    character <i>c</i>, which may be any non-zero Unicode character. <i>Fn</i> should
    be declared as<br>
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

        <tt><font size=+1>int &nbsp;&nbsp;&nbsp;&nbsp;fn(Fmt*)<br>
        
        <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
        </font></tt>
        
    </table>
    <i>Fp</i><tt><font size=+1>&#8722;&gt;r</font></tt> is the flag or verb character to cause <i>fn</i> to be called. In
    <i>fn</i>, <i>fp</i><tt><font size=+1>&#8722;&gt;width</font></tt><i>, fp</i><tt><font size=+1>&#8722;&gt;prec</font></tt> are the width and precision, and <i>fp</i><tt><font size=+1>&#8722;&gt;flags</font></tt>
    the decoded flags for the verb (see <a href="../man3/print.html"><i>print</i>(3)</a> for a description
    of these items). The standard flag values are: <tt><font size=+1>FmtSign</font></tt> (<tt><font size=+1>+</font></tt>), <tt><font size=+1>FmtLeft</font></tt>
    (<tt><font size=+1>&#8722;</font></tt>), <tt><font size=+1>FmtSpace</font></tt> (<tt><font size=+1>' '</font></tt>), <tt><font size=+1>FmtSharp</font></tt> (<tt><font size=+1>#</font></tt>),
    <tt><font size=+1>FmtComma</font></tt> (<tt><font size=+1>,</font></tt>), <tt><font size=+1>FmtLong</font></tt> (<tt><font size=+1>l</font></tt>), <tt><font size=+1>FmtShort</font></tt> (<tt><font size=+1>h</font></tt>), <tt><font size=+1>FmtUnsigned</font></tt> (<tt><font size=+1>u</font></tt>), and
    <tt><font size=+1>FmtVLong</font></tt> (<tt><font size=+1>ll</font></tt>). The flag bits <tt><font size=+1>FmtWidth</font></tt> and <tt><font size=+1>FmtPrec</font></tt> identify whether
    a width and precision were specified. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    <i>Fn</i> is passed a pointer to the <tt><font size=+1>Fmt</font></tt> structure recording the state
    of the output. If <i>fp</i><tt><font size=+1>&#8722;&gt;r</font></tt> is a verb (rather than a flag), <i>fn</i> should
    use <tt><font size=+1>Fmt&#8722;&gt;args</font></tt> to fetch its argument from the list, then format
    it, and return zero. If <i>fp</i><tt><font size=+1>&#8722;&gt;r</font></tt> is a flag, <i>fn</i> should return one.
    All interpretation of <i>fp</i><tt><font size=+1>&#8722;&gt;width</font></tt>, <i>fp</i><tt><font size=+1>&#8722;&gt;prec</font></tt>, and <i>fp-&gt;</i><tt><font size=+1>flags</font></tt> is
    left up to the conversion routine. <i>Fmtinstall</i> returns 0 if the
    installation succeeds, &ndash;1 if it fails. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    <i>Fmtprint</i> and <i>fmtvprint</i> may be called to help prepare output in
    custom conversion routines. However, these functions clear the
    width, precision, and flags. Both functions return 0 for success
    and &ndash;1 for failure. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    The functions <i>dofmt</i> and <i>dorfmt</i> are the underlying formatters;
    they use the existing contents of <tt><font size=+1>Fmt</font></tt> and should be called only
    by sophisticated conversion routines. These routines return the
    number of characters (bytes of UTF or runes) produced. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    Some internal functions may be useful to format primitive types.
    They honor the width, precision and flags as described in <a href="../man3/print.html"><i>print</i>(3)</a>.
    <i>Fmtrune</i> formats a single character <tt><font size=+1>r</font></tt>. <i>Fmtstrcpy</i> formats a string
    <tt><font size=+1>s</font></tt>; <i>fmtrunestrcpy</i> formats a rune string <tt><font size=+1>s</font></tt>. <i>Errfmt</i> formats the system
    error string. All these routines return zero for
    successful execution. Conversion routines that call these functions
    will work properly regardless of whether the output is bytes or
    runes.<br>
    
</table>
<p><font size=+1><b>EXAMPLES     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    This function prints an error message with a variable number of
    arguments and then quits. Compared to the corresponding example
    in <a href="../man3/print.html"><i>print</i>(3)</a>, this version uses a smaller buffer, will never truncate
    the output message, but might generate multiple <tt><font size=+1>write</font></tt> system calls
    to produce its output.
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

        <tt><font size=+1>#pragma &nbsp;&nbsp;&nbsp;varargck argpos &nbsp;&nbsp;&nbsp;error &nbsp;&nbsp;&nbsp;&nbsp;1<br>
        void fatal(char *fmt, ...)<br>
        {<br>
        
        <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

            Fmt f;<br>
            char buf[64];<br>
            va_list arg;<br>
            fmtfdinit(&amp;f, 1, buf, sizeof buf);<br>
            fmtprint(&amp;f, &quot;fatal: &quot;);<br>
            va_start(arg, fmt);<br>
            fmtvprint(&amp;f, fmt, arg);<br>
            va_end(arg);<br>
            fmtprint(&amp;f, &quot;\n&quot;);<br>
            fmtfdflush(&amp;f);<br>
            exits(&quot;fatal error&quot;);<br>
            
        </table>
        }<br>
        
        <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
        </font></tt>
        
    </table>
    This example adds a verb to print complex numbers.<br>
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

        <tt><font size=+1>typedef<br>
        struct {<br>
        
        <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

            double &nbsp;&nbsp;&nbsp;r, i;<br>
            
        </table>
        } Complex;<br>
        #pragma &nbsp;&nbsp;&nbsp;varargck type &quot;X&quot; Complex<br>
        int<br>
        Xfmt(Fmt *f)<br>
        {<br>
        
        <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

            Complex c;<br>
            c = va_arg(f&#8722;&gt;args, Complex);<br>
            return fmtprint(f, &quot;(%g,%g)&quot;, c.r, c.i);<br>
            
        </table>
        }<br>
        main(...)<br>
        {<br>
        
        <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

            Complex x = (Complex){ 1.5, &#8722;2.3 };<br>
            fmtinstall('X', Xfmt);<br>
            print(&quot;x = %X\n&quot;, x);<br>
            
        </table>
        }<br>
        </font></tt>
    </table>
    
</table>
<p><font size=+1><b>SOURCE     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    <tt><font size=+1>/usr/local/plan9/src/lib9/fmt<br>
    </font></tt>
</table>
<p><font size=+1><b>SEE ALSO    </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    <a href="../man3/print.html"><i>print</i>(3)</a>, <a href="../man7/utf.html"><i>utf</i>(7)</a>, <a href="../man3/errstr.html"><i>errstr</i>(3)</a><br>
    
</table>
<p><font size=+1><b>DIAGNOSTICS     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    These routines return negative numbers or nil for errors and set
    <i>errstr</i>.<br>
    
</table>

<td width=20>
<tr height=20><td>
</table>
<!-- TRAILER -->
<table border=0 cellpadding=0 cellspacing=0 width=100%>
<tr height=15><td width=10><td><td width=10>
<tr><td><td>
<center>
<a href="../../"><img src="../../dist/spaceglenda100.png" alt="Space Glenda" border=1></a>
</center>
</table>
<!-- TRAILER -->
</body></html>