xref: /titanic_50/usr/src/lib/libinetutil/common/ofmt.h (revision 8002d4117c1ea26aff1f16f584ae97bdbd5b21d5)
1*8002d411SSowmini Varadhan /*
2*8002d411SSowmini Varadhan  * CDDL HEADER START
3*8002d411SSowmini Varadhan  *
4*8002d411SSowmini Varadhan  * The contents of this file are subject to the terms of the
5*8002d411SSowmini Varadhan  * Common Development and Distribution License (the "License").
6*8002d411SSowmini Varadhan  * You may not use this file except in compliance with the License.
7*8002d411SSowmini Varadhan  *
8*8002d411SSowmini Varadhan  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*8002d411SSowmini Varadhan  * or http://www.opensolaris.org/os/licensing.
10*8002d411SSowmini Varadhan  * See the License for the specific language governing permissions
11*8002d411SSowmini Varadhan  * and limitations under the License.
12*8002d411SSowmini Varadhan  *
13*8002d411SSowmini Varadhan  * When distributing Covered Code, include this CDDL HEADER in each
14*8002d411SSowmini Varadhan  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*8002d411SSowmini Varadhan  * If applicable, add the following below this CDDL HEADER, with the
16*8002d411SSowmini Varadhan  * fields enclosed by brackets "[]" replaced with your own identifying
17*8002d411SSowmini Varadhan  * information: Portions Copyright [yyyy] [name of copyright owner]
18*8002d411SSowmini Varadhan  *
19*8002d411SSowmini Varadhan  * CDDL HEADER END
20*8002d411SSowmini Varadhan  */
21*8002d411SSowmini Varadhan 
22*8002d411SSowmini Varadhan /*
23*8002d411SSowmini Varadhan  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*8002d411SSowmini Varadhan  * Use is subject to license terms.
25*8002d411SSowmini Varadhan  */
26*8002d411SSowmini Varadhan 
27*8002d411SSowmini Varadhan #ifndef _OFMT_H
28*8002d411SSowmini Varadhan #define	_OFMT_H
29*8002d411SSowmini Varadhan 
30*8002d411SSowmini Varadhan /*
31*8002d411SSowmini Varadhan  * Data structures and routines for printing output.
32*8002d411SSowmini Varadhan  *
33*8002d411SSowmini Varadhan  * All output is assumed to be in a columnar format, where each column
34*8002d411SSowmini Varadhan  * represents a field to be printed out. Multiple fields in parsable output
35*8002d411SSowmini Varadhan  * are separated by ':', with the ':' character itself escaped by a \
36*8002d411SSowmini Varadhan  * (e.g., IPv6 addresses  may be printed as "fe80\:\:1"); single field output
37*8002d411SSowmini Varadhan  * is printed as-is.
38*8002d411SSowmini Varadhan  *
39*8002d411SSowmini Varadhan  * The caller must open a handle for each set of fields to be printed by
40*8002d411SSowmini Varadhan  * invoking ofmt_open(). The invocation to ofmt_open must provide the list of
41*8002d411SSowmini Varadhan  * supported fields, along with formatting information (e.g., field width), and
42*8002d411SSowmini Varadhan  * a pointer to a callback function that can provide a string representation of
43*8002d411SSowmini Varadhan  * the value to be printed out. The set of supported fields must be a NULL
44*8002d411SSowmini Varadhan  * terminated array of type ofmt_field_t *ofields[]. The contents of the
45*8002d411SSowmini Varadhan  * ofmt_field_t structure are used to construct the string that is emitted by
46*8002d411SSowmini Varadhan  * ofmt_print(), and the interpretation of these contents is described with the
47*8002d411SSowmini Varadhan  * semantics of ofmt_print() below.
48*8002d411SSowmini Varadhan  *
49*8002d411SSowmini Varadhan  * In addition, the call to ofmt_open() should provide a comma-separated
50*8002d411SSowmini Varadhan  * list of the fields, char *fields_str, that have been selected for output
51*8002d411SSowmini Varadhan  * (typically the string passed to -o in the command-line). The caller may
52*8002d411SSowmini Varadhan  * also specify machine-parsable mode by specifying OFMT_PARSABLE in the oflags
53*8002d411SSowmini Varadhan  * argument. Specifying a null or empty fields_str in the machine-parsable mode
54*8002d411SSowmini Varadhan  * will result in a returned error value of OFMT_EPARSENONE. An attempt to
55*8002d411SSowmini Varadhan  * create a handle in machine-parsable mode with the fields_str set to "all"
56*8002d411SSowmini Varadhan  * will result in a returned error value of OFMT_EPARSEALL. In human-friendly
57*8002d411SSowmini Varadhan  * (non machine-parsable) mode, a NULL fields_str, or a value of "all" for
58*8002d411SSowmini Varadhan  * fields_str, is treated as a request to print all allowable fields that fit
59*8002d411SSowmini Varadhan  * other applicable constraints.
60*8002d411SSowmini Varadhan  *
61*8002d411SSowmini Varadhan  * Thus a typical invocation to open the ofmt_handle would be:
62*8002d411SSowmini Varadhan  *
63*8002d411SSowmini Varadhan  *	ofmt_handle_t ofmt;
64*8002d411SSowmini Varadhan  *	ofmt_status_t ofmt_err;
65*8002d411SSowmini Varadhan  *
66*8002d411SSowmini Varadhan  *	ofmt_err = ofmt_open(fields_str, ofields, oflags, maxcols, &ofmt);
67*8002d411SSowmini Varadhan  *
68*8002d411SSowmini Varadhan  * where ofields is an array of the form:
69*8002d411SSowmini Varadhan  *
70*8002d411SSowmini Varadhan  * static ofmt_field_t ofields[] = {
71*8002d411SSowmini Varadhan  *	{<name>, <field width>,  <id>, <callback> },
72*8002d411SSowmini Varadhan  *	:
73*8002d411SSowmini Varadhan  *	{<name>, <field width>,  <id>, <callback> },
74*8002d411SSowmini Varadhan  *	{NULL, 0, 0, NULL}}
75*8002d411SSowmini Varadhan  *
76*8002d411SSowmini Varadhan  * <callback> is the application-specified function that provides a string
77*8002d411SSowmini Varadhan  * representation of the value to be printed for the field. The calling
78*8002d411SSowmini Varadhan  * application may provide unique values of <id> that will be passed back to
79*8002d411SSowmini Varadhan  * <callback>, allowing a single <callback> to be shared between multiple
80*8002d411SSowmini Varadhan  * fields in ofields[] with the value of <id> identifying the field that
81*8002d411SSowmini Varadhan  * triggers the callback.
82*8002d411SSowmini Varadhan  *
83*8002d411SSowmini Varadhan  * If successful, ofmt_open() will return OFMT_SUCCESS, with a non-null
84*8002d411SSowmini Varadhan  * ofmt_handle. The function returns a failure code otherwise, and more
85*8002d411SSowmini Varadhan  * information about the type of failure can be obtained by calling
86*8002d411SSowmini Varadhan  * ofmt_strerror()
87*8002d411SSowmini Varadhan  *
88*8002d411SSowmini Varadhan  * In order to print a row of output, the calling application should invoke
89*8002d411SSowmini Varadhan  *
90*8002d411SSowmini Varadhan  *     ofmt_print(ofmt_handle, cbarg);
91*8002d411SSowmini Varadhan  *
92*8002d411SSowmini Varadhan  * where  'cbarg' points at the arguments to be  passed to the <callback>
93*8002d411SSowmini Varadhan  * function  for each column in the row. The call to ofmt_print() will then
94*8002d411SSowmini Varadhan  * result in the <callback> function of each selected field from ofields[]
95*8002d411SSowmini Varadhan  * invoked with cbarg embedded in the ofmt_arg as
96*8002d411SSowmini Varadhan  *
97*8002d411SSowmini Varadhan  *     (*callback)(ofmt_arg_t *ofmt_arg, char *buf, uint_t bufsize)
98*8002d411SSowmini Varadhan  *
99*8002d411SSowmini Varadhan  * Columns selected for output are identified by a match between the of_name
100*8002d411SSowmini Varadhan  * value in the ofmt_field_t and the fields_str requested. For each selected
101*8002d411SSowmini Varadhan  * column, the callback function (*of_cb)() is invoked, and is passed the of_id
102*8002d411SSowmini Varadhan  * value from the ofmt_field_t structure for the field.
103*8002d411SSowmini Varadhan  *
104*8002d411SSowmini Varadhan  * The interpretation of the of_id field is completely private to the caller,
105*8002d411SSowmini Varadhan  * and can be optionally used by the callback function as a cookie
106*8002d411SSowmini Varadhan  * to identify the field being printed when a single callback function is
107*8002d411SSowmini Varadhan  * shared between multiple ofmt_field_t entries.
108*8002d411SSowmini Varadhan  *
109*8002d411SSowmini Varadhan  * The callback function should fill `buf' with the string to be printed for
110*8002d411SSowmini Varadhan  * the field using the data in cbarg.
111*8002d411SSowmini Varadhan  *
112*8002d411SSowmini Varadhan  * The calling application should invoke ofmt_close(ofmt_handle) to free up any
113*8002d411SSowmini Varadhan  * resources allocated for the handle after all printing is completed.
114*8002d411SSowmini Varadhan  *
115*8002d411SSowmini Varadhan  * The printing library computes the current size of the output window when the
116*8002d411SSowmini Varadhan  * handle is first created. If the caller wishes to adjust the window size
117*8002d411SSowmini Varadhan  * after the handle has been created (e.g., on the reception of SIGWINCH by the
118*8002d411SSowmini Varadhan  * caller), the function ofmt_update_winsize(handle) may be called.
119*8002d411SSowmini Varadhan  */
120*8002d411SSowmini Varadhan 
121*8002d411SSowmini Varadhan #ifdef __cplusplus
122*8002d411SSowmini Varadhan extern "C" {
123*8002d411SSowmini Varadhan #endif
124*8002d411SSowmini Varadhan 
125*8002d411SSowmini Varadhan /*
126*8002d411SSowmini Varadhan  * Recommended buffer size for buffers passed, for example, to ofmt_strerror().
127*8002d411SSowmini Varadhan  */
128*8002d411SSowmini Varadhan #define	OFMT_BUFSIZE		256
129*8002d411SSowmini Varadhan 
130*8002d411SSowmini Varadhan typedef enum {
131*8002d411SSowmini Varadhan 	OFMT_SUCCESS = 0,
132*8002d411SSowmini Varadhan 	OFMT_ENOMEM,		/* out of memory */
133*8002d411SSowmini Varadhan 	OFMT_EBADFIELDS,	/* one or more bad fields with good fields */
134*8002d411SSowmini Varadhan 	OFMT_ENOFIELDS,		/* no valid output fields */
135*8002d411SSowmini Varadhan 	OFMT_EPARSEALL,		/* 'all' invalid in parsable mode */
136*8002d411SSowmini Varadhan 	OFMT_EPARSENONE,	/* output fields missing in parsable mode */
137*8002d411SSowmini Varadhan 	OFMT_ENOTEMPLATE	/* no template provided for fields */
138*8002d411SSowmini Varadhan } ofmt_status_t;
139*8002d411SSowmini Varadhan 
140*8002d411SSowmini Varadhan /*
141*8002d411SSowmini Varadhan  * The callback function for each field is invoked with a pointer to the
142*8002d411SSowmini Varadhan  * ofmt_arg_t structure that contains the <id> registered by the application
143*8002d411SSowmini Varadhan  * for that field, and the cbarg used by the application when invoking
144*8002d411SSowmini Varadhan  * ofmt_output().
145*8002d411SSowmini Varadhan  */
146*8002d411SSowmini Varadhan typedef struct ofmt_arg_s {
147*8002d411SSowmini Varadhan 	uint_t	ofmt_id;
148*8002d411SSowmini Varadhan 	void	*ofmt_cbarg;
149*8002d411SSowmini Varadhan } ofmt_arg_t;
150*8002d411SSowmini Varadhan 
151*8002d411SSowmini Varadhan /*
152*8002d411SSowmini Varadhan  * ofmt callback function that provides a string representation of the value to
153*8002d411SSowmini Varadhan  * be printed for the field.
154*8002d411SSowmini Varadhan  */
155*8002d411SSowmini Varadhan typedef boolean_t ofmt_cb_t(ofmt_arg_t *, char *, uint_t);
156*8002d411SSowmini Varadhan typedef struct ofmt_field_s {
157*8002d411SSowmini Varadhan 	char	*of_name;	/* column name */
158*8002d411SSowmini Varadhan 	uint_t	of_width;	/* output column width */
159*8002d411SSowmini Varadhan 	uint_t	of_id;		/* implementation specific cookie */
160*8002d411SSowmini Varadhan 	ofmt_cb_t *of_cb;	/* callback function defined by caller */
161*8002d411SSowmini Varadhan } ofmt_field_t;
162*8002d411SSowmini Varadhan 
163*8002d411SSowmini Varadhan /*
164*8002d411SSowmini Varadhan  * ofmt_open() must be called to create the ofmt_handle_t; Resources allocated
165*8002d411SSowmini Varadhan  * for the handle are freed by ofmt_close();
166*8002d411SSowmini Varadhan  */
167*8002d411SSowmini Varadhan typedef struct ofmt_state_s *ofmt_handle_t;
168*8002d411SSowmini Varadhan extern ofmt_status_t ofmt_open(const char *, ofmt_field_t *, uint_t,
169*8002d411SSowmini Varadhan     uint_t, ofmt_handle_t *);
170*8002d411SSowmini Varadhan 
171*8002d411SSowmini Varadhan #define	OFMT_PARSABLE	0x00000001 /* machine parsable mode */
172*8002d411SSowmini Varadhan 
173*8002d411SSowmini Varadhan /*
174*8002d411SSowmini Varadhan  * ofmt_close() must be called to free resources associated
175*8002d411SSowmini Varadhan  * with the ofmt_handle_t
176*8002d411SSowmini Varadhan  */
177*8002d411SSowmini Varadhan extern void ofmt_close(ofmt_handle_t);
178*8002d411SSowmini Varadhan 
179*8002d411SSowmini Varadhan /*
180*8002d411SSowmini Varadhan  * ofmt_print() emits one row of output
181*8002d411SSowmini Varadhan  */
182*8002d411SSowmini Varadhan extern void ofmt_print(ofmt_handle_t, void *);
183*8002d411SSowmini Varadhan 
184*8002d411SSowmini Varadhan /*
185*8002d411SSowmini Varadhan  * ofmt_update_winsize() updates the window size information for ofmt_handle_t
186*8002d411SSowmini Varadhan  */
187*8002d411SSowmini Varadhan extern void ofmt_update_winsize(ofmt_handle_t);
188*8002d411SSowmini Varadhan 
189*8002d411SSowmini Varadhan /*
190*8002d411SSowmini Varadhan  * ofmt_strerror() provides error diagnostics in the buffer that it is passed.
191*8002d411SSowmini Varadhan  */
192*8002d411SSowmini Varadhan extern char *ofmt_strerror(ofmt_handle_t, ofmt_status_t, char *, uint_t);
193*8002d411SSowmini Varadhan 
194*8002d411SSowmini Varadhan #ifdef __cplusplus
195*8002d411SSowmini Varadhan }
196*8002d411SSowmini Varadhan #endif
197*8002d411SSowmini Varadhan 
198*8002d411SSowmini Varadhan #endif /* _OFMT_H */
199