xref: /titanic_53/usr/src/man/man3ofmt/ofmt.3ofmt (revision 071ca66ea5e45078699d3a2ade8c6ca5c00bba2c)
1*071ca66eSYuri Pankov.\"
2*071ca66eSYuri Pankov.\" This file and its contents are supplied under the terms of the
3*071ca66eSYuri Pankov.\" Common Development and Distribution License ("CDDL"), version 1.0.
4*071ca66eSYuri Pankov.\" You may only use this file in accordance with the terms of version
5*071ca66eSYuri Pankov.\" 1.0 of the CDDL.
6*071ca66eSYuri Pankov.\"
7*071ca66eSYuri Pankov.\" A full copy of the text of the CDDL should have accompanied this
8*071ca66eSYuri Pankov.\" source.  A copy of the CDDL is also available via the Internet at
9*071ca66eSYuri Pankov.\" http://www.illumos.org/license/CDDL.
10*071ca66eSYuri Pankov.\"
11*071ca66eSYuri Pankov.\"
12*071ca66eSYuri Pankov.\" Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
13*071ca66eSYuri Pankov.\" Copyright 2017 Nexenta Systems, Inc.
14*071ca66eSYuri Pankov.\"
15*071ca66eSYuri Pankov.Dd June 1, 2017
16*071ca66eSYuri Pankov.Dt OFMT 3OFMT
17*071ca66eSYuri Pankov.Os
18*071ca66eSYuri Pankov.Sh NAME
19*071ca66eSYuri Pankov.Nm ofmt_open ,
20*071ca66eSYuri Pankov.Nm ofmt_print ,
21*071ca66eSYuri Pankov.Nm ofmt_update_winsize ,
22*071ca66eSYuri Pankov.Nm ofmt_strerror ,
23*071ca66eSYuri Pankov.Nm ofmt_close
24*071ca66eSYuri Pankov.Nd data structures and routines for printing output
25*071ca66eSYuri Pankov.Sh LIBRARY
26*071ca66eSYuri Pankov.Lb libofmt
27*071ca66eSYuri Pankov.Sh SYNOPSIS
28*071ca66eSYuri Pankov.In ofmt.h
29*071ca66eSYuri Pankov.Ft ofmt_status_t
30*071ca66eSYuri Pankov.Fo ofmt_open
31*071ca66eSYuri Pankov.Fa "const char *fields"
32*071ca66eSYuri Pankov.Fa "const ofmt_field_t *template"
33*071ca66eSYuri Pankov.Fa "uint_t flags"
34*071ca66eSYuri Pankov.Fa "uint_t maxcols"
35*071ca66eSYuri Pankov.Fa "ofmt_handle_t *ofmt"
36*071ca66eSYuri Pankov.Fc
37*071ca66eSYuri Pankov.Ft void
38*071ca66eSYuri Pankov.Fo ofmt_print
39*071ca66eSYuri Pankov.Fa "ofmt_handle_t ofmt"
40*071ca66eSYuri Pankov.Fa "void *cbarg"
41*071ca66eSYuri Pankov.Fc
42*071ca66eSYuri Pankov.Ft void
43*071ca66eSYuri Pankov.Fo ofmt_update_winsize
44*071ca66eSYuri Pankov.Fa "ofmt_handle_t ofmt"
45*071ca66eSYuri Pankov.Fc
46*071ca66eSYuri Pankov.Ft "char *"
47*071ca66eSYuri Pankov.Fo ofmt_strerror
48*071ca66eSYuri Pankov.Fa "ofmt_handle_t ofmt"
49*071ca66eSYuri Pankov.Fa "ofmt_status_t error"
50*071ca66eSYuri Pankov.Fa "char *buf"
51*071ca66eSYuri Pankov.Fa "uint_t bufsize"
52*071ca66eSYuri Pankov.Fc
53*071ca66eSYuri Pankov.Ft void
54*071ca66eSYuri Pankov.Fo ofmt_close
55*071ca66eSYuri Pankov.Fa "ofmt_handle_t ofmt"
56*071ca66eSYuri Pankov.Fc
57*071ca66eSYuri Pankov.Sh DESCRIPTION
58*071ca66eSYuri PankovThe
59*071ca66eSYuri Pankov.Nm libofmt
60*071ca66eSYuri Pankovlibrary provides data structures and routines for printing output.
61*071ca66eSYuri Pankov.Pp
62*071ca66eSYuri PankovCurrently this is an internal interface.
63*071ca66eSYuri PankovThe interface can and will change without notice as the project needs, at any
64*071ca66eSYuri Pankovtime.
65*071ca66eSYuri Pankov.Pp
66*071ca66eSYuri PankovAll output is assumed to be in a columnar format, where each column represents
67*071ca66eSYuri Pankova field to be printed out.
68*071ca66eSYuri PankovMultiple fields in parsable output are separated by
69*071ca66eSYuri Pankov.Sq \&: ,
70*071ca66eSYuri Pankovwith the
71*071ca66eSYuri Pankov.Sq \&:
72*071ca66eSYuri Pankovcharacter itself escaped by a
73*071ca66eSYuri Pankov.Sq \e
74*071ca66eSYuri Pankov.Po e.g., IPv6 addresses  may be printed as
75*071ca66eSYuri Pankov.Qq fe80\e:\e:1
76*071ca66eSYuri Pankov.Pc ;
77*071ca66eSYuri Pankovsingle field output is printed as-is.
78*071ca66eSYuri PankovIn multiline mode, every
79*071ca66eSYuri Pankov.Bq field,value
80*071ca66eSYuri Pankovpair is printed in a line of its own, thus:
81*071ca66eSYuri Pankov.Qq field: value .
82*071ca66eSYuri Pankov.Ss Data Structures
83*071ca66eSYuri PankovThe
84*071ca66eSYuri Pankov.Vt ofmt_field_t
85*071ca66eSYuri Pankovdata structure used in
86*071ca66eSYuri Pankov.Sx ofmt_open
87*071ca66eSYuri Pankovis defined as follows:
88*071ca66eSYuri Pankov.Bd -literal
89*071ca66eSYuri Pankovtypedef struct ofmt_field_s {
90*071ca66eSYuri Pankov	char	*of_name;	/* column name */
91*071ca66eSYuri Pankov	uint_t	of_width;	/* output column width */
92*071ca66eSYuri Pankov	uint_t	of_id;		/* implementation specific cookie */
93*071ca66eSYuri Pankov	ofmt_cb_t *of_cb;	/* callback function defined by caller */
94*071ca66eSYuri Pankov} ofmt_field_t;
95*071ca66eSYuri Pankov.Ed
96*071ca66eSYuri Pankov.Pp
97*071ca66eSYuri PankovThe
98*071ca66eSYuri Pankov.Vt ofmt_arg_t
99*071ca66eSYuri Pankovdata structure which is passed to callback function is defined as follows:
100*071ca66eSYuri Pankov.Bd -literal
101*071ca66eSYuri Pankovtypedef struct ofmt_arg_s {
102*071ca66eSYuri Pankov	uint_t	ofmt_id;	/* implementation specific cookie */
103*071ca66eSYuri Pankov	uint_t	ofmt_width;	/* output column width */
104*071ca66eSYuri Pankov	uint_t	ofmt_index;	/* unused */
105*071ca66eSYuri Pankov	void	*ofmt_cbarg;	/* argument passed to ofmt_print() */
106*071ca66eSYuri Pankov} ofmt_arg_t;
107*071ca66eSYuri Pankov.Ed
108*071ca66eSYuri Pankov.Ss Fn ofmt_open
109*071ca66eSYuri PankovThe
110*071ca66eSYuri Pankov.Fn ofmt_open
111*071ca66eSYuri Pankovfunction opens a handle returned in
112*071ca66eSYuri Pankov.Fa ofmt
113*071ca66eSYuri Pankovfor each set of fields to be printed.
114*071ca66eSYuri Pankov.Pp
115*071ca66eSYuri Pankov.Fa fields
116*071ca66eSYuri Pankovis a comma-separated list of the fields that have been selected for output
117*071ca66eSYuri Pankov.Po typically the string passed to
118*071ca66eSYuri Pankov.Fl o
119*071ca66eSYuri Pankovin the command-line
120*071ca66eSYuri Pankov.Pc .
121*071ca66eSYuri PankovColumns selected for output are identified by a match between the
122*071ca66eSYuri Pankov.Va of_name
123*071ca66eSYuri Pankovvalue in the
124*071ca66eSYuri Pankov.Fa template
125*071ca66eSYuri Pankovand the
126*071ca66eSYuri Pankov.Fa fields
127*071ca66eSYuri Pankovrequested.
128*071ca66eSYuri PankovIn human-friendly
129*071ca66eSYuri Pankov.Pq non machine-parsable
130*071ca66eSYuri Pankovmode,
131*071ca66eSYuri Pankov.Dv NULL
132*071ca66eSYuri Pankov.Fa fields ,
133*071ca66eSYuri Pankovor a value of
134*071ca66eSYuri Pankov.Qq all
135*071ca66eSYuri Pankovis treated as a request to print all allowable fields that fit other applicable
136*071ca66eSYuri Pankovconstraints.
137*071ca66eSYuri Pankov.Pp
138*071ca66eSYuri Pankov.Fa template
139*071ca66eSYuri Pankovspecifies the list of supported fields, along with formatting information
140*071ca66eSYuri Pankov.Pq e.g., field width ,
141*071ca66eSYuri Pankovand a pointer to a callback function that can provide a string representation of
142*071ca66eSYuri Pankovthe value to be printed out.
143*071ca66eSYuri PankovThe set of supported fields must be a
144*071ca66eSYuri Pankov.Dv NULL
145*071ca66eSYuri Pankovterminated array of type
146*071ca66eSYuri Pankov.Vt ofmt_field_t ,
147*071ca66eSYuri Pankovdescribed in
148*071ca66eSYuri Pankov.Sx Data Structures ,
149*071ca66eSYuri Pankovas follows:
150*071ca66eSYuri Pankov.Bd -literal -offset indent
151*071ca66eSYuri Pankov{<of_name>, <of_width>, <of_id>, <of_cb> },
152*071ca66eSYuri Pankov\&.\&.\&.
153*071ca66eSYuri Pankov{<of_name>, <of_width>, <of_id>, <of_cb> },
154*071ca66eSYuri Pankov{NULL, 0, 0, NULL}
155*071ca66eSYuri Pankov.Ed
156*071ca66eSYuri Pankov.Pp
157*071ca66eSYuri Pankov.Va of_cb
158*071ca66eSYuri Pankovis the application-specified callback function with the following prototype that
159*071ca66eSYuri Pankovprovides a string representation of the value to be printed for the field:
160*071ca66eSYuri Pankov.Bd -literal -offset indent
161*071ca66eSYuri Pankov(*of_cb)(ofmt_arg_t *ofmt_arg, char *buf, uint_t bufsize)
162*071ca66eSYuri Pankov.Ed
163*071ca66eSYuri Pankov.Pp
164*071ca66eSYuri PankovThe interpretation of the
165*071ca66eSYuri Pankov.Va of_id
166*071ca66eSYuri Pankovfield is completely private to the caller, and can be optionally used by the
167*071ca66eSYuri Pankovcallback function as a cookie to identify the field being printed when a single
168*071ca66eSYuri Pankovcallback function is shared between multiple
169*071ca66eSYuri Pankov.Fa template
170*071ca66eSYuri Pankoventries.
171*071ca66eSYuri Pankov.Pp
172*071ca66eSYuri PankovThe
173*071ca66eSYuri Pankov.Fa flags
174*071ca66eSYuri Pankovcan be any valid combination of the following:
175*071ca66eSYuri Pankov.Pp
176*071ca66eSYuri Pankov.Bl -tag -width "OFMT_MULTILINE" -compact
177*071ca66eSYuri Pankov.It Dv OFMT_PARSABLE
178*071ca66eSYuri PankovMachine-parsable mode.
179*071ca66eSYuri PankovSpecifying a null or empty
180*071ca66eSYuri Pankov.Va fields
181*071ca66eSYuri Pankovin the machine-parsable mode will result in a returned error value of
182*071ca66eSYuri Pankov.Dv OFMT_EPARSENONE .
183*071ca66eSYuri PankovAn attempt to create a handle in machine-parsable mode with the
184*071ca66eSYuri Pankov.Fa fields
185*071ca66eSYuri Pankovset to
186*071ca66eSYuri Pankov.Qq all
187*071ca66eSYuri Pankovwill result in a returned error value of
188*071ca66eSYuri Pankov.Dv OFMT_EPARSEALL .
189*071ca66eSYuri Pankov.It Dv OFMT_WRAP
190*071ca66eSYuri PankovWrap output if field width is exceeded.
191*071ca66eSYuri PankovCurrently output is wrapped at whitespace or comma characters.
192*071ca66eSYuri Pankov.It Dv OFMT_MULTILINE
193*071ca66eSYuri PankovMultiline mode.
194*071ca66eSYuri PankovSpecifying both
195*071ca66eSYuri Pankov.Dv OFMT_MULTILINE
196*071ca66eSYuri Pankovand
197*071ca66eSYuri Pankov.Dv OFMT_PARSABLE
198*071ca66eSYuri Pankovwill result in
199*071ca66eSYuri Pankov.Dv OFMT_EPARSEMULTI .
200*071ca66eSYuri Pankov.It Dv OFMT_RIGHTJUST
201*071ca66eSYuri PankovRight justified output.
202*071ca66eSYuri Pankov.El
203*071ca66eSYuri Pankov.Pp
204*071ca66eSYuri PankovThe non-zero
205*071ca66eSYuri Pankov.Fa maxcols
206*071ca66eSYuri Pankovlimits the number of output columns.
207*071ca66eSYuri Pankov.Ss Fn ofmt_print
208*071ca66eSYuri PankovThe
209*071ca66eSYuri Pankov.Fn ofmt_print
210*071ca66eSYuri Pankovfunction prints a row of output.
211*071ca66eSYuri Pankov.Pp
212*071ca66eSYuri Pankov.Fa cbarg
213*071ca66eSYuri Pankovpoints at the arguments to be passed to the callback function for each column in
214*071ca66eSYuri Pankovthe row.
215*071ca66eSYuri PankovThe call to
216*071ca66eSYuri Pankov.Fn ofmt_print
217*071ca66eSYuri Pankovwill result in the callback function of each selected field invoked with
218*071ca66eSYuri Pankov.Va of_id ,
219*071ca66eSYuri Pankov.Va of_width
220*071ca66eSYuri Pankovand
221*071ca66eSYuri Pankov.Fa cbarg
222*071ca66eSYuri Pankovembedded in
223*071ca66eSYuri Pankov.Fa ofmt_arg ,
224*071ca66eSYuri Pankovdescribed in
225*071ca66eSYuri Pankov.Sx Data Structures .
226*071ca66eSYuri Pankov.Pp
227*071ca66eSYuri PankovThe callback function should fill
228*071ca66eSYuri Pankov.Fa buf
229*071ca66eSYuri Pankovwith the string to be printed for the field using the data in
230*071ca66eSYuri Pankov.Fa cbarg .
231*071ca66eSYuri Pankov.Ss Fn ofmt_update_winsize
232*071ca66eSYuri PankovThe
233*071ca66eSYuri Pankov.Fn ofmt_update_winsize
234*071ca66eSYuri Pankovfunction updates the window size information
235*071ca66eSYuri Pankov.Pq which is initially computed when the handle is created
236*071ca66eSYuri Pankovin the
237*071ca66eSYuri Pankov.Fa ofmt .
238*071ca66eSYuri PankovIf the
239*071ca66eSYuri Pankov.Dv TIOCGWINSZ
240*071ca66eSYuri Pankovioctl fails, the window size is set to 80x24.
241*071ca66eSYuri Pankov.Ss Fn ofmt_strerror
242*071ca66eSYuri PankovThe
243*071ca66eSYuri Pankov.Fn ofmt_strerror
244*071ca66eSYuri Pankovfunction returns error diagnostics in
245*071ca66eSYuri Pankov.Fa buf
246*071ca66eSYuri Pankovusing the information in the
247*071ca66eSYuri Pankov.Fa ofmt
248*071ca66eSYuri Pankovand
249*071ca66eSYuri Pankov.Fa error .
250*071ca66eSYuri Pankov.Pp
251*071ca66eSYuri PankovUsing a
252*071ca66eSYuri Pankov.Fa buf
253*071ca66eSYuri Pankovsize of
254*071ca66eSYuri Pankov.Dv OFMT_BUFSIZE
255*071ca66eSYuri Pankovis recommended.
256*071ca66eSYuri Pankov.Ss Fn ofmt_close
257*071ca66eSYuri PankovThe
258*071ca66eSYuri Pankov.Fn ofmt_close
259*071ca66eSYuri Pankovfunction frees any resources allocated for the handle after printing is
260*071ca66eSYuri Pankovcompleted.
261*071ca66eSYuri Pankov.Sh RETURN VALUES
262*071ca66eSYuri PankovIf successful, the
263*071ca66eSYuri Pankov.Fn ofmt_open
264*071ca66eSYuri Pankovfunction will return
265*071ca66eSYuri Pankov.Dv OFMT_SUCCESS ,
266*071ca66eSYuri Pankovwith a non-null
267*071ca66eSYuri Pankov.Fa ofmt_handle .
268*071ca66eSYuri PankovThe function returns one of the failure codes
269*071ca66eSYuri Pankov.Po enumerated in
270*071ca66eSYuri Pankov.Vt ofmt_status_t
271*071ca66eSYuri Pankov.Pc
272*071ca66eSYuri Pankovlisted below otherwise:
273*071ca66eSYuri Pankov.Pp
274*071ca66eSYuri Pankov.Bl -tag -width "OFMT_ENOTEMPLATE" -compact
275*071ca66eSYuri Pankov.It Dv OFMT_ENOMEM
276*071ca66eSYuri Pankovout of memory
277*071ca66eSYuri Pankov.It Dv OFMT_EBADFIELDS
278*071ca66eSYuri Pankovone or more bad fields with good fields
279*071ca66eSYuri Pankov.It Dv OFMT_ENOFIELDS
280*071ca66eSYuri Pankovno valid output fields
281*071ca66eSYuri Pankov.It Dv OFMT_EPARSEALL
282*071ca66eSYuri Pankov"all" is invalid in parsable mode
283*071ca66eSYuri Pankov.It Dv OFMT_EPARSENONE
284*071ca66eSYuri Pankovoutput fields missing in parsable mode
285*071ca66eSYuri Pankov.It Dv OFMT_EPARSEWRAP
286*071ca66eSYuri Pankovparsable mode incompatible with wrap mode
287*071ca66eSYuri Pankov.It Dv OFMT_ENOTEMPLATE
288*071ca66eSYuri Pankovno template provided for fields
289*071ca66eSYuri Pankov.It Dv OFMT_EPARSEMULTI
290*071ca66eSYuri Pankovparsable and multiline don't mix
291*071ca66eSYuri Pankov.El
292*071ca66eSYuri Pankov.Pp
293*071ca66eSYuri PankovMore information about the type of failure can be obtained by calling
294*071ca66eSYuri Pankov.Fn ofmt_strerror .
295*071ca66eSYuri Pankov.Pp
296*071ca66eSYuri PankovThe
297*071ca66eSYuri Pankov.Fn ofmt_strerror
298*071ca66eSYuri Pankovfunction returns the
299*071ca66eSYuri Pankov.Fa buf .
300*071ca66eSYuri Pankov.Sh INTERFACE STABILITY
301*071ca66eSYuri Pankov.Sy Private .
302*071ca66eSYuri Pankov.Sh SEE ALSO
303*071ca66eSYuri Pankov.Xr ioctl 2 ,
304*071ca66eSYuri Pankov.Xr strerror 3C ,
305*071ca66eSYuri Pankov.Xr attributes 5
306