18002d411SSowmini Varadhan /* 28002d411SSowmini Varadhan * CDDL HEADER START 38002d411SSowmini Varadhan * 48002d411SSowmini Varadhan * The contents of this file are subject to the terms of the 58002d411SSowmini Varadhan * Common Development and Distribution License (the "License"). 68002d411SSowmini Varadhan * You may not use this file except in compliance with the License. 78002d411SSowmini Varadhan * 88002d411SSowmini Varadhan * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 98002d411SSowmini Varadhan * or http://www.opensolaris.org/os/licensing. 108002d411SSowmini Varadhan * See the License for the specific language governing permissions 118002d411SSowmini Varadhan * and limitations under the License. 128002d411SSowmini Varadhan * 138002d411SSowmini Varadhan * When distributing Covered Code, include this CDDL HEADER in each 148002d411SSowmini Varadhan * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 158002d411SSowmini Varadhan * If applicable, add the following below this CDDL HEADER, with the 168002d411SSowmini Varadhan * fields enclosed by brackets "[]" replaced with your own identifying 178002d411SSowmini Varadhan * information: Portions Copyright [yyyy] [name of copyright owner] 188002d411SSowmini Varadhan * 198002d411SSowmini Varadhan * CDDL HEADER END 208002d411SSowmini Varadhan */ 218002d411SSowmini Varadhan 228002d411SSowmini Varadhan /* 238002d411SSowmini Varadhan * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 248002d411SSowmini Varadhan * Use is subject to license terms. 258002d411SSowmini Varadhan */ 268002d411SSowmini Varadhan 278002d411SSowmini Varadhan #ifndef _OFMT_H 288002d411SSowmini Varadhan #define _OFMT_H 298002d411SSowmini Varadhan 308002d411SSowmini Varadhan /* 318002d411SSowmini Varadhan * Data structures and routines for printing output. 328002d411SSowmini Varadhan * 338002d411SSowmini Varadhan * All output is assumed to be in a columnar format, where each column 348002d411SSowmini Varadhan * represents a field to be printed out. Multiple fields in parsable output 358002d411SSowmini Varadhan * are separated by ':', with the ':' character itself escaped by a \ 368002d411SSowmini Varadhan * (e.g., IPv6 addresses may be printed as "fe80\:\:1"); single field output 378002d411SSowmini Varadhan * is printed as-is. 388002d411SSowmini Varadhan * 398002d411SSowmini Varadhan * The caller must open a handle for each set of fields to be printed by 408002d411SSowmini Varadhan * invoking ofmt_open(). The invocation to ofmt_open must provide the list of 418002d411SSowmini Varadhan * supported fields, along with formatting information (e.g., field width), and 428002d411SSowmini Varadhan * a pointer to a callback function that can provide a string representation of 438002d411SSowmini Varadhan * the value to be printed out. The set of supported fields must be a NULL 448002d411SSowmini Varadhan * terminated array of type ofmt_field_t *ofields[]. The contents of the 458002d411SSowmini Varadhan * ofmt_field_t structure are used to construct the string that is emitted by 468002d411SSowmini Varadhan * ofmt_print(), and the interpretation of these contents is described with the 478002d411SSowmini Varadhan * semantics of ofmt_print() below. 488002d411SSowmini Varadhan * 498002d411SSowmini Varadhan * In addition, the call to ofmt_open() should provide a comma-separated 508002d411SSowmini Varadhan * list of the fields, char *fields_str, that have been selected for output 518002d411SSowmini Varadhan * (typically the string passed to -o in the command-line). The caller may 528002d411SSowmini Varadhan * also specify machine-parsable mode by specifying OFMT_PARSABLE in the oflags 538002d411SSowmini Varadhan * argument. Specifying a null or empty fields_str in the machine-parsable mode 548002d411SSowmini Varadhan * will result in a returned error value of OFMT_EPARSENONE. An attempt to 558002d411SSowmini Varadhan * create a handle in machine-parsable mode with the fields_str set to "all" 568002d411SSowmini Varadhan * will result in a returned error value of OFMT_EPARSEALL. In human-friendly 578002d411SSowmini Varadhan * (non machine-parsable) mode, a NULL fields_str, or a value of "all" for 588002d411SSowmini Varadhan * fields_str, is treated as a request to print all allowable fields that fit 598002d411SSowmini Varadhan * other applicable constraints. 608002d411SSowmini Varadhan * 618002d411SSowmini Varadhan * Thus a typical invocation to open the ofmt_handle would be: 628002d411SSowmini Varadhan * 638002d411SSowmini Varadhan * ofmt_handle_t ofmt; 648002d411SSowmini Varadhan * ofmt_status_t ofmt_err; 658002d411SSowmini Varadhan * 668002d411SSowmini Varadhan * ofmt_err = ofmt_open(fields_str, ofields, oflags, maxcols, &ofmt); 678002d411SSowmini Varadhan * 688002d411SSowmini Varadhan * where ofields is an array of the form: 698002d411SSowmini Varadhan * 708002d411SSowmini Varadhan * static ofmt_field_t ofields[] = { 718002d411SSowmini Varadhan * {<name>, <field width>, <id>, <callback> }, 728002d411SSowmini Varadhan * : 738002d411SSowmini Varadhan * {<name>, <field width>, <id>, <callback> }, 748002d411SSowmini Varadhan * {NULL, 0, 0, NULL}} 758002d411SSowmini Varadhan * 768002d411SSowmini Varadhan * <callback> is the application-specified function that provides a string 778002d411SSowmini Varadhan * representation of the value to be printed for the field. The calling 788002d411SSowmini Varadhan * application may provide unique values of <id> that will be passed back to 798002d411SSowmini Varadhan * <callback>, allowing a single <callback> to be shared between multiple 808002d411SSowmini Varadhan * fields in ofields[] with the value of <id> identifying the field that 818002d411SSowmini Varadhan * triggers the callback. 828002d411SSowmini Varadhan * 838002d411SSowmini Varadhan * If successful, ofmt_open() will return OFMT_SUCCESS, with a non-null 848002d411SSowmini Varadhan * ofmt_handle. The function returns a failure code otherwise, and more 858002d411SSowmini Varadhan * information about the type of failure can be obtained by calling 868002d411SSowmini Varadhan * ofmt_strerror() 878002d411SSowmini Varadhan * 888002d411SSowmini Varadhan * In order to print a row of output, the calling application should invoke 898002d411SSowmini Varadhan * 908002d411SSowmini Varadhan * ofmt_print(ofmt_handle, cbarg); 918002d411SSowmini Varadhan * 928002d411SSowmini Varadhan * where 'cbarg' points at the arguments to be passed to the <callback> 938002d411SSowmini Varadhan * function for each column in the row. The call to ofmt_print() will then 948002d411SSowmini Varadhan * result in the <callback> function of each selected field from ofields[] 958002d411SSowmini Varadhan * invoked with cbarg embedded in the ofmt_arg as 968002d411SSowmini Varadhan * 978002d411SSowmini Varadhan * (*callback)(ofmt_arg_t *ofmt_arg, char *buf, uint_t bufsize) 988002d411SSowmini Varadhan * 998002d411SSowmini Varadhan * Columns selected for output are identified by a match between the of_name 1008002d411SSowmini Varadhan * value in the ofmt_field_t and the fields_str requested. For each selected 1018002d411SSowmini Varadhan * column, the callback function (*of_cb)() is invoked, and is passed the of_id 1028002d411SSowmini Varadhan * value from the ofmt_field_t structure for the field. 1038002d411SSowmini Varadhan * 1048002d411SSowmini Varadhan * The interpretation of the of_id field is completely private to the caller, 1058002d411SSowmini Varadhan * and can be optionally used by the callback function as a cookie 1068002d411SSowmini Varadhan * to identify the field being printed when a single callback function is 1078002d411SSowmini Varadhan * shared between multiple ofmt_field_t entries. 1088002d411SSowmini Varadhan * 1098002d411SSowmini Varadhan * The callback function should fill `buf' with the string to be printed for 1108002d411SSowmini Varadhan * the field using the data in cbarg. 1118002d411SSowmini Varadhan * 1128002d411SSowmini Varadhan * The calling application should invoke ofmt_close(ofmt_handle) to free up any 1138002d411SSowmini Varadhan * resources allocated for the handle after all printing is completed. 1148002d411SSowmini Varadhan * 1158002d411SSowmini Varadhan * The printing library computes the current size of the output window when the 1168002d411SSowmini Varadhan * handle is first created. If the caller wishes to adjust the window size 1178002d411SSowmini Varadhan * after the handle has been created (e.g., on the reception of SIGWINCH by the 1188002d411SSowmini Varadhan * caller), the function ofmt_update_winsize(handle) may be called. 1198002d411SSowmini Varadhan */ 1208002d411SSowmini Varadhan 1218002d411SSowmini Varadhan #ifdef __cplusplus 1228002d411SSowmini Varadhan extern "C" { 1238002d411SSowmini Varadhan #endif 1248002d411SSowmini Varadhan 1258002d411SSowmini Varadhan /* 1268002d411SSowmini Varadhan * Recommended buffer size for buffers passed, for example, to ofmt_strerror(). 1278002d411SSowmini Varadhan */ 1288002d411SSowmini Varadhan #define OFMT_BUFSIZE 256 1298002d411SSowmini Varadhan 1308002d411SSowmini Varadhan typedef enum { 1318002d411SSowmini Varadhan OFMT_SUCCESS = 0, 1328002d411SSowmini Varadhan OFMT_ENOMEM, /* out of memory */ 1338002d411SSowmini Varadhan OFMT_EBADFIELDS, /* one or more bad fields with good fields */ 1348002d411SSowmini Varadhan OFMT_ENOFIELDS, /* no valid output fields */ 1358002d411SSowmini Varadhan OFMT_EPARSEALL, /* 'all' invalid in parsable mode */ 1368002d411SSowmini Varadhan OFMT_EPARSENONE, /* output fields missing in parsable mode */ 1378002d411SSowmini Varadhan OFMT_ENOTEMPLATE /* no template provided for fields */ 1388002d411SSowmini Varadhan } ofmt_status_t; 1398002d411SSowmini Varadhan 1408002d411SSowmini Varadhan /* 1418002d411SSowmini Varadhan * The callback function for each field is invoked with a pointer to the 1428002d411SSowmini Varadhan * ofmt_arg_t structure that contains the <id> registered by the application 1438002d411SSowmini Varadhan * for that field, and the cbarg used by the application when invoking 1448002d411SSowmini Varadhan * ofmt_output(). 1458002d411SSowmini Varadhan */ 1468002d411SSowmini Varadhan typedef struct ofmt_arg_s { 1478002d411SSowmini Varadhan uint_t ofmt_id; 1488002d411SSowmini Varadhan void *ofmt_cbarg; 1498002d411SSowmini Varadhan } ofmt_arg_t; 1508002d411SSowmini Varadhan 1518002d411SSowmini Varadhan /* 1528002d411SSowmini Varadhan * ofmt callback function that provides a string representation of the value to 1538002d411SSowmini Varadhan * be printed for the field. 1548002d411SSowmini Varadhan */ 1558002d411SSowmini Varadhan typedef boolean_t ofmt_cb_t(ofmt_arg_t *, char *, uint_t); 1568002d411SSowmini Varadhan typedef struct ofmt_field_s { 1578002d411SSowmini Varadhan char *of_name; /* column name */ 1588002d411SSowmini Varadhan uint_t of_width; /* output column width */ 1598002d411SSowmini Varadhan uint_t of_id; /* implementation specific cookie */ 1608002d411SSowmini Varadhan ofmt_cb_t *of_cb; /* callback function defined by caller */ 1618002d411SSowmini Varadhan } ofmt_field_t; 1628002d411SSowmini Varadhan 1638002d411SSowmini Varadhan /* 1648002d411SSowmini Varadhan * ofmt_open() must be called to create the ofmt_handle_t; Resources allocated 1658002d411SSowmini Varadhan * for the handle are freed by ofmt_close(); 1668002d411SSowmini Varadhan */ 1678002d411SSowmini Varadhan typedef struct ofmt_state_s *ofmt_handle_t; 168*2b24ab6bSSebastien Roy extern ofmt_status_t ofmt_open(const char *, const ofmt_field_t *, uint_t, 1698002d411SSowmini Varadhan uint_t, ofmt_handle_t *); 1708002d411SSowmini Varadhan 1718002d411SSowmini Varadhan #define OFMT_PARSABLE 0x00000001 /* machine parsable mode */ 1728002d411SSowmini Varadhan 1738002d411SSowmini Varadhan /* 1748002d411SSowmini Varadhan * ofmt_close() must be called to free resources associated 1758002d411SSowmini Varadhan * with the ofmt_handle_t 1768002d411SSowmini Varadhan */ 1778002d411SSowmini Varadhan extern void ofmt_close(ofmt_handle_t); 1788002d411SSowmini Varadhan 1798002d411SSowmini Varadhan /* 1808002d411SSowmini Varadhan * ofmt_print() emits one row of output 1818002d411SSowmini Varadhan */ 1828002d411SSowmini Varadhan extern void ofmt_print(ofmt_handle_t, void *); 1838002d411SSowmini Varadhan 1848002d411SSowmini Varadhan /* 1858002d411SSowmini Varadhan * ofmt_update_winsize() updates the window size information for ofmt_handle_t 1868002d411SSowmini Varadhan */ 1878002d411SSowmini Varadhan extern void ofmt_update_winsize(ofmt_handle_t); 1888002d411SSowmini Varadhan 1898002d411SSowmini Varadhan /* 1908002d411SSowmini Varadhan * ofmt_strerror() provides error diagnostics in the buffer that it is passed. 1918002d411SSowmini Varadhan */ 1928002d411SSowmini Varadhan extern char *ofmt_strerror(ofmt_handle_t, ofmt_status_t, char *, uint_t); 1938002d411SSowmini Varadhan 1948002d411SSowmini Varadhan #ifdef __cplusplus 1958002d411SSowmini Varadhan } 1968002d411SSowmini Varadhan #endif 1978002d411SSowmini Varadhan 1988002d411SSowmini Varadhan #endif /* _OFMT_H */ 199