xref: /onnv-gate/usr/src/lib/libinetutil/common/ofmt.h (revision 11878:ac93462db6d7)
19071SSowmini.Varadhan@Sun.COM /*
29071SSowmini.Varadhan@Sun.COM  * CDDL HEADER START
39071SSowmini.Varadhan@Sun.COM  *
49071SSowmini.Varadhan@Sun.COM  * The contents of this file are subject to the terms of the
59071SSowmini.Varadhan@Sun.COM  * Common Development and Distribution License (the "License").
69071SSowmini.Varadhan@Sun.COM  * You may not use this file except in compliance with the License.
79071SSowmini.Varadhan@Sun.COM  *
89071SSowmini.Varadhan@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99071SSowmini.Varadhan@Sun.COM  * or http://www.opensolaris.org/os/licensing.
109071SSowmini.Varadhan@Sun.COM  * See the License for the specific language governing permissions
119071SSowmini.Varadhan@Sun.COM  * and limitations under the License.
129071SSowmini.Varadhan@Sun.COM  *
139071SSowmini.Varadhan@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
149071SSowmini.Varadhan@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159071SSowmini.Varadhan@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
169071SSowmini.Varadhan@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
179071SSowmini.Varadhan@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
189071SSowmini.Varadhan@Sun.COM  *
199071SSowmini.Varadhan@Sun.COM  * CDDL HEADER END
209071SSowmini.Varadhan@Sun.COM  */
219071SSowmini.Varadhan@Sun.COM 
229071SSowmini.Varadhan@Sun.COM /*
23*11878SVenu.Iyer@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
249071SSowmini.Varadhan@Sun.COM  * Use is subject to license terms.
259071SSowmini.Varadhan@Sun.COM  */
269071SSowmini.Varadhan@Sun.COM 
279071SSowmini.Varadhan@Sun.COM #ifndef _OFMT_H
289071SSowmini.Varadhan@Sun.COM #define	_OFMT_H
299071SSowmini.Varadhan@Sun.COM 
309071SSowmini.Varadhan@Sun.COM /*
319071SSowmini.Varadhan@Sun.COM  * Data structures and routines for printing output.
329071SSowmini.Varadhan@Sun.COM  *
339071SSowmini.Varadhan@Sun.COM  * All output is assumed to be in a columnar format, where each column
349071SSowmini.Varadhan@Sun.COM  * represents a field to be printed out. Multiple fields in parsable output
359071SSowmini.Varadhan@Sun.COM  * are separated by ':', with the ':' character itself escaped by a \
369071SSowmini.Varadhan@Sun.COM  * (e.g., IPv6 addresses  may be printed as "fe80\:\:1"); single field output
379071SSowmini.Varadhan@Sun.COM  * is printed as-is.
3810946SSangeeta.Misra@Sun.COM  * In multiline mode, every [field,value] pair is printed in a line of
3910946SSangeeta.Misra@Sun.COM  * its own, thus: "field: value".
409071SSowmini.Varadhan@Sun.COM  *
419071SSowmini.Varadhan@Sun.COM  * The caller must open a handle for each set of fields to be printed by
429071SSowmini.Varadhan@Sun.COM  * invoking ofmt_open(). The invocation to ofmt_open must provide the list of
439071SSowmini.Varadhan@Sun.COM  * supported fields, along with formatting information (e.g., field width), and
449071SSowmini.Varadhan@Sun.COM  * a pointer to a callback function that can provide a string representation of
459071SSowmini.Varadhan@Sun.COM  * the value to be printed out. The set of supported fields must be a NULL
469071SSowmini.Varadhan@Sun.COM  * terminated array of type ofmt_field_t *ofields[]. The contents of the
479071SSowmini.Varadhan@Sun.COM  * ofmt_field_t structure are used to construct the string that is emitted by
489071SSowmini.Varadhan@Sun.COM  * ofmt_print(), and the interpretation of these contents is described with the
499071SSowmini.Varadhan@Sun.COM  * semantics of ofmt_print() below.
509071SSowmini.Varadhan@Sun.COM  *
519071SSowmini.Varadhan@Sun.COM  * In addition, the call to ofmt_open() should provide a comma-separated
529071SSowmini.Varadhan@Sun.COM  * list of the fields, char *fields_str, that have been selected for output
539071SSowmini.Varadhan@Sun.COM  * (typically the string passed to -o in the command-line). The caller may
549071SSowmini.Varadhan@Sun.COM  * also specify machine-parsable mode by specifying OFMT_PARSABLE in the oflags
559071SSowmini.Varadhan@Sun.COM  * argument. Specifying a null or empty fields_str in the machine-parsable mode
569071SSowmini.Varadhan@Sun.COM  * will result in a returned error value of OFMT_EPARSENONE. An attempt to
579071SSowmini.Varadhan@Sun.COM  * create a handle in machine-parsable mode with the fields_str set to "all"
589071SSowmini.Varadhan@Sun.COM  * will result in a returned error value of OFMT_EPARSEALL. In human-friendly
599071SSowmini.Varadhan@Sun.COM  * (non machine-parsable) mode, a NULL fields_str, or a value of "all" for
609071SSowmini.Varadhan@Sun.COM  * fields_str, is treated as a request to print all allowable fields that fit
619071SSowmini.Varadhan@Sun.COM  * other applicable constraints.
6210946SSangeeta.Misra@Sun.COM  * To achieve multiline mode, OFMT_MULTILINE needs to be specified in oflags.
6310946SSangeeta.Misra@Sun.COM  * Specifying both OFMT_MULTILINE and OFMT_PARSABLE will result in
6410946SSangeeta.Misra@Sun.COM  * OFMT_EPARSEMULTI.
659071SSowmini.Varadhan@Sun.COM  *
669071SSowmini.Varadhan@Sun.COM  * Thus a typical invocation to open the ofmt_handle would be:
679071SSowmini.Varadhan@Sun.COM  *
689071SSowmini.Varadhan@Sun.COM  *	ofmt_handle_t ofmt;
699071SSowmini.Varadhan@Sun.COM  *	ofmt_status_t ofmt_err;
709071SSowmini.Varadhan@Sun.COM  *
719071SSowmini.Varadhan@Sun.COM  *	ofmt_err = ofmt_open(fields_str, ofields, oflags, maxcols, &ofmt);
729071SSowmini.Varadhan@Sun.COM  *
739071SSowmini.Varadhan@Sun.COM  * where ofields is an array of the form:
749071SSowmini.Varadhan@Sun.COM  *
759071SSowmini.Varadhan@Sun.COM  * static ofmt_field_t ofields[] = {
769071SSowmini.Varadhan@Sun.COM  *	{<name>, <field width>,  <id>, <callback> },
779071SSowmini.Varadhan@Sun.COM  *	:
789071SSowmini.Varadhan@Sun.COM  *	{<name>, <field width>,  <id>, <callback> },
799071SSowmini.Varadhan@Sun.COM  *	{NULL, 0, 0, NULL}}
809071SSowmini.Varadhan@Sun.COM  *
819071SSowmini.Varadhan@Sun.COM  * <callback> is the application-specified function that provides a string
829071SSowmini.Varadhan@Sun.COM  * representation of the value to be printed for the field. The calling
839071SSowmini.Varadhan@Sun.COM  * application may provide unique values of <id> that will be passed back to
849071SSowmini.Varadhan@Sun.COM  * <callback>, allowing a single <callback> to be shared between multiple
859071SSowmini.Varadhan@Sun.COM  * fields in ofields[] with the value of <id> identifying the field that
869071SSowmini.Varadhan@Sun.COM  * triggers the callback.
879071SSowmini.Varadhan@Sun.COM  *
889071SSowmini.Varadhan@Sun.COM  * If successful, ofmt_open() will return OFMT_SUCCESS, with a non-null
899071SSowmini.Varadhan@Sun.COM  * ofmt_handle. The function returns a failure code otherwise, and more
909071SSowmini.Varadhan@Sun.COM  * information about the type of failure can be obtained by calling
919071SSowmini.Varadhan@Sun.COM  * ofmt_strerror()
929071SSowmini.Varadhan@Sun.COM  *
939071SSowmini.Varadhan@Sun.COM  * In order to print a row of output, the calling application should invoke
949071SSowmini.Varadhan@Sun.COM  *
959071SSowmini.Varadhan@Sun.COM  *     ofmt_print(ofmt_handle, cbarg);
969071SSowmini.Varadhan@Sun.COM  *
979071SSowmini.Varadhan@Sun.COM  * where  'cbarg' points at the arguments to be  passed to the <callback>
989071SSowmini.Varadhan@Sun.COM  * function  for each column in the row. The call to ofmt_print() will then
999071SSowmini.Varadhan@Sun.COM  * result in the <callback> function of each selected field from ofields[]
1009071SSowmini.Varadhan@Sun.COM  * invoked with cbarg embedded in the ofmt_arg as
1019071SSowmini.Varadhan@Sun.COM  *
1029071SSowmini.Varadhan@Sun.COM  *     (*callback)(ofmt_arg_t *ofmt_arg, char *buf, uint_t bufsize)
1039071SSowmini.Varadhan@Sun.COM  *
1049071SSowmini.Varadhan@Sun.COM  * Columns selected for output are identified by a match between the of_name
1059071SSowmini.Varadhan@Sun.COM  * value in the ofmt_field_t and the fields_str requested. For each selected
1069071SSowmini.Varadhan@Sun.COM  * column, the callback function (*of_cb)() is invoked, and is passed the of_id
1079071SSowmini.Varadhan@Sun.COM  * value from the ofmt_field_t structure for the field.
1089071SSowmini.Varadhan@Sun.COM  *
1099071SSowmini.Varadhan@Sun.COM  * The interpretation of the of_id field is completely private to the caller,
1109071SSowmini.Varadhan@Sun.COM  * and can be optionally used by the callback function as a cookie
1119071SSowmini.Varadhan@Sun.COM  * to identify the field being printed when a single callback function is
1129071SSowmini.Varadhan@Sun.COM  * shared between multiple ofmt_field_t entries.
1139071SSowmini.Varadhan@Sun.COM  *
1149071SSowmini.Varadhan@Sun.COM  * The callback function should fill `buf' with the string to be printed for
1159071SSowmini.Varadhan@Sun.COM  * the field using the data in cbarg.
1169071SSowmini.Varadhan@Sun.COM  *
1179071SSowmini.Varadhan@Sun.COM  * The calling application should invoke ofmt_close(ofmt_handle) to free up any
1189071SSowmini.Varadhan@Sun.COM  * resources allocated for the handle after all printing is completed.
1199071SSowmini.Varadhan@Sun.COM  *
1209071SSowmini.Varadhan@Sun.COM  * The printing library computes the current size of the output window when the
1219071SSowmini.Varadhan@Sun.COM  * handle is first created. If the caller wishes to adjust the window size
1229071SSowmini.Varadhan@Sun.COM  * after the handle has been created (e.g., on the reception of SIGWINCH by the
1239071SSowmini.Varadhan@Sun.COM  * caller), the function ofmt_update_winsize(handle) may be called.
1249071SSowmini.Varadhan@Sun.COM  */
1259071SSowmini.Varadhan@Sun.COM 
1269071SSowmini.Varadhan@Sun.COM #ifdef __cplusplus
1279071SSowmini.Varadhan@Sun.COM extern "C" {
1289071SSowmini.Varadhan@Sun.COM #endif
1299071SSowmini.Varadhan@Sun.COM 
1309071SSowmini.Varadhan@Sun.COM /*
1319071SSowmini.Varadhan@Sun.COM  * Recommended buffer size for buffers passed, for example, to ofmt_strerror().
1329071SSowmini.Varadhan@Sun.COM  */
1339071SSowmini.Varadhan@Sun.COM #define	OFMT_BUFSIZE		256
1349071SSowmini.Varadhan@Sun.COM 
1359071SSowmini.Varadhan@Sun.COM typedef enum {
1369071SSowmini.Varadhan@Sun.COM 	OFMT_SUCCESS = 0,
1379071SSowmini.Varadhan@Sun.COM 	OFMT_ENOMEM,		/* out of memory */
1389071SSowmini.Varadhan@Sun.COM 	OFMT_EBADFIELDS,	/* one or more bad fields with good fields */
1399071SSowmini.Varadhan@Sun.COM 	OFMT_ENOFIELDS,		/* no valid output fields */
1409071SSowmini.Varadhan@Sun.COM 	OFMT_EPARSEALL,		/* 'all' invalid in parsable mode */
1419071SSowmini.Varadhan@Sun.COM 	OFMT_EPARSENONE,	/* output fields missing in parsable mode */
14210734SEric Cheng 	OFMT_EPARSEWRAP,	/* parsable mode incompatible with wrap mode */
14310946SSangeeta.Misra@Sun.COM 	OFMT_ENOTEMPLATE,	/* no template provided for fields */
14410946SSangeeta.Misra@Sun.COM 	OFMT_EPARSEMULTI	/* parsable and multiline don't mix */
1459071SSowmini.Varadhan@Sun.COM } ofmt_status_t;
1469071SSowmini.Varadhan@Sun.COM 
1479071SSowmini.Varadhan@Sun.COM /*
1489071SSowmini.Varadhan@Sun.COM  * The callback function for each field is invoked with a pointer to the
1499071SSowmini.Varadhan@Sun.COM  * ofmt_arg_t structure that contains the <id> registered by the application
1509071SSowmini.Varadhan@Sun.COM  * for that field, and the cbarg used by the application when invoking
1519071SSowmini.Varadhan@Sun.COM  * ofmt_output().
1529071SSowmini.Varadhan@Sun.COM  */
1539071SSowmini.Varadhan@Sun.COM typedef struct ofmt_arg_s {
1549071SSowmini.Varadhan@Sun.COM 	uint_t	ofmt_id;
155*11878SVenu.Iyer@Sun.COM 	uint_t	ofmt_width;
156*11878SVenu.Iyer@Sun.COM 	uint_t	ofmt_index;
1579071SSowmini.Varadhan@Sun.COM 	void	*ofmt_cbarg;
1589071SSowmini.Varadhan@Sun.COM } ofmt_arg_t;
1599071SSowmini.Varadhan@Sun.COM 
1609071SSowmini.Varadhan@Sun.COM /*
1619071SSowmini.Varadhan@Sun.COM  * ofmt callback function that provides a string representation of the value to
1629071SSowmini.Varadhan@Sun.COM  * be printed for the field.
1639071SSowmini.Varadhan@Sun.COM  */
1649071SSowmini.Varadhan@Sun.COM typedef boolean_t ofmt_cb_t(ofmt_arg_t *, char *, uint_t);
1659071SSowmini.Varadhan@Sun.COM typedef struct ofmt_field_s {
1669071SSowmini.Varadhan@Sun.COM 	char	*of_name;	/* column name */
1679071SSowmini.Varadhan@Sun.COM 	uint_t	of_width;	/* output column width */
1689071SSowmini.Varadhan@Sun.COM 	uint_t	of_id;		/* implementation specific cookie */
1699071SSowmini.Varadhan@Sun.COM 	ofmt_cb_t *of_cb;	/* callback function defined by caller */
1709071SSowmini.Varadhan@Sun.COM } ofmt_field_t;
1719071SSowmini.Varadhan@Sun.COM 
1729071SSowmini.Varadhan@Sun.COM /*
1739071SSowmini.Varadhan@Sun.COM  * ofmt_open() must be called to create the ofmt_handle_t; Resources allocated
1749071SSowmini.Varadhan@Sun.COM  * for the handle are freed by ofmt_close();
1759071SSowmini.Varadhan@Sun.COM  */
1769071SSowmini.Varadhan@Sun.COM typedef struct ofmt_state_s *ofmt_handle_t;
17710616SSebastien.Roy@Sun.COM extern ofmt_status_t ofmt_open(const char *, const ofmt_field_t *, uint_t,
1789071SSowmini.Varadhan@Sun.COM     uint_t, ofmt_handle_t *);
1799071SSowmini.Varadhan@Sun.COM 
1809071SSowmini.Varadhan@Sun.COM #define	OFMT_PARSABLE	0x00000001 /* machine parsable mode */
18110734SEric Cheng #define	OFMT_WRAP	0x00000002 /* wrap output if field width is exceeded */
18210946SSangeeta.Misra@Sun.COM #define	OFMT_MULTILINE	0x00000004 /* "long" output: "name: value" lines */
183*11878SVenu.Iyer@Sun.COM #define	OFMT_RIGHTJUST	0x00000008 /* right justified output */
1849071SSowmini.Varadhan@Sun.COM 
1859071SSowmini.Varadhan@Sun.COM /*
1869071SSowmini.Varadhan@Sun.COM  * ofmt_close() must be called to free resources associated
1879071SSowmini.Varadhan@Sun.COM  * with the ofmt_handle_t
1889071SSowmini.Varadhan@Sun.COM  */
1899071SSowmini.Varadhan@Sun.COM extern void ofmt_close(ofmt_handle_t);
1909071SSowmini.Varadhan@Sun.COM 
1919071SSowmini.Varadhan@Sun.COM /*
1929071SSowmini.Varadhan@Sun.COM  * ofmt_print() emits one row of output
1939071SSowmini.Varadhan@Sun.COM  */
1949071SSowmini.Varadhan@Sun.COM extern void ofmt_print(ofmt_handle_t, void *);
1959071SSowmini.Varadhan@Sun.COM 
1969071SSowmini.Varadhan@Sun.COM /*
1979071SSowmini.Varadhan@Sun.COM  * ofmt_update_winsize() updates the window size information for ofmt_handle_t
1989071SSowmini.Varadhan@Sun.COM  */
1999071SSowmini.Varadhan@Sun.COM extern void ofmt_update_winsize(ofmt_handle_t);
2009071SSowmini.Varadhan@Sun.COM 
2019071SSowmini.Varadhan@Sun.COM /*
2029071SSowmini.Varadhan@Sun.COM  * ofmt_strerror() provides error diagnostics in the buffer that it is passed.
2039071SSowmini.Varadhan@Sun.COM  */
2049071SSowmini.Varadhan@Sun.COM extern char *ofmt_strerror(ofmt_handle_t, ofmt_status_t, char *, uint_t);
2059071SSowmini.Varadhan@Sun.COM 
2069071SSowmini.Varadhan@Sun.COM #ifdef __cplusplus
2079071SSowmini.Varadhan@Sun.COM }
2089071SSowmini.Varadhan@Sun.COM #endif
2099071SSowmini.Varadhan@Sun.COM 
2109071SSowmini.Varadhan@Sun.COM #endif /* _OFMT_H */
211