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 /* 23*0dc2366fSVenugopal Iyer * Copyright 2010 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. 38dbed73cbSSangeeta Misra * In multiline mode, every [field,value] pair is printed in a line of 39dbed73cbSSangeeta Misra * its own, thus: "field: value". 408002d411SSowmini Varadhan * 418002d411SSowmini Varadhan * The caller must open a handle for each set of fields to be printed by 428002d411SSowmini Varadhan * invoking ofmt_open(). The invocation to ofmt_open must provide the list of 438002d411SSowmini Varadhan * supported fields, along with formatting information (e.g., field width), and 448002d411SSowmini Varadhan * a pointer to a callback function that can provide a string representation of 458002d411SSowmini Varadhan * the value to be printed out. The set of supported fields must be a NULL 468002d411SSowmini Varadhan * terminated array of type ofmt_field_t *ofields[]. The contents of the 478002d411SSowmini Varadhan * ofmt_field_t structure are used to construct the string that is emitted by 488002d411SSowmini Varadhan * ofmt_print(), and the interpretation of these contents is described with the 498002d411SSowmini Varadhan * semantics of ofmt_print() below. 508002d411SSowmini Varadhan * 518002d411SSowmini Varadhan * In addition, the call to ofmt_open() should provide a comma-separated 528002d411SSowmini Varadhan * list of the fields, char *fields_str, that have been selected for output 538002d411SSowmini Varadhan * (typically the string passed to -o in the command-line). The caller may 548002d411SSowmini Varadhan * also specify machine-parsable mode by specifying OFMT_PARSABLE in the oflags 558002d411SSowmini Varadhan * argument. Specifying a null or empty fields_str in the machine-parsable mode 568002d411SSowmini Varadhan * will result in a returned error value of OFMT_EPARSENONE. An attempt to 578002d411SSowmini Varadhan * create a handle in machine-parsable mode with the fields_str set to "all" 588002d411SSowmini Varadhan * will result in a returned error value of OFMT_EPARSEALL. In human-friendly 598002d411SSowmini Varadhan * (non machine-parsable) mode, a NULL fields_str, or a value of "all" for 608002d411SSowmini Varadhan * fields_str, is treated as a request to print all allowable fields that fit 618002d411SSowmini Varadhan * other applicable constraints. 62dbed73cbSSangeeta Misra * To achieve multiline mode, OFMT_MULTILINE needs to be specified in oflags. 63dbed73cbSSangeeta Misra * Specifying both OFMT_MULTILINE and OFMT_PARSABLE will result in 64dbed73cbSSangeeta Misra * OFMT_EPARSEMULTI. 658002d411SSowmini Varadhan * 668002d411SSowmini Varadhan * Thus a typical invocation to open the ofmt_handle would be: 678002d411SSowmini Varadhan * 688002d411SSowmini Varadhan * ofmt_handle_t ofmt; 698002d411SSowmini Varadhan * ofmt_status_t ofmt_err; 708002d411SSowmini Varadhan * 718002d411SSowmini Varadhan * ofmt_err = ofmt_open(fields_str, ofields, oflags, maxcols, &ofmt); 728002d411SSowmini Varadhan * 738002d411SSowmini Varadhan * where ofields is an array of the form: 748002d411SSowmini Varadhan * 758002d411SSowmini Varadhan * static ofmt_field_t ofields[] = { 768002d411SSowmini Varadhan * {<name>, <field width>, <id>, <callback> }, 778002d411SSowmini Varadhan * : 788002d411SSowmini Varadhan * {<name>, <field width>, <id>, <callback> }, 798002d411SSowmini Varadhan * {NULL, 0, 0, NULL}} 808002d411SSowmini Varadhan * 818002d411SSowmini Varadhan * <callback> is the application-specified function that provides a string 828002d411SSowmini Varadhan * representation of the value to be printed for the field. The calling 838002d411SSowmini Varadhan * application may provide unique values of <id> that will be passed back to 848002d411SSowmini Varadhan * <callback>, allowing a single <callback> to be shared between multiple 858002d411SSowmini Varadhan * fields in ofields[] with the value of <id> identifying the field that 868002d411SSowmini Varadhan * triggers the callback. 878002d411SSowmini Varadhan * 888002d411SSowmini Varadhan * If successful, ofmt_open() will return OFMT_SUCCESS, with a non-null 898002d411SSowmini Varadhan * ofmt_handle. The function returns a failure code otherwise, and more 908002d411SSowmini Varadhan * information about the type of failure can be obtained by calling 918002d411SSowmini Varadhan * ofmt_strerror() 928002d411SSowmini Varadhan * 938002d411SSowmini Varadhan * In order to print a row of output, the calling application should invoke 948002d411SSowmini Varadhan * 958002d411SSowmini Varadhan * ofmt_print(ofmt_handle, cbarg); 968002d411SSowmini Varadhan * 978002d411SSowmini Varadhan * where 'cbarg' points at the arguments to be passed to the <callback> 988002d411SSowmini Varadhan * function for each column in the row. The call to ofmt_print() will then 998002d411SSowmini Varadhan * result in the <callback> function of each selected field from ofields[] 1008002d411SSowmini Varadhan * invoked with cbarg embedded in the ofmt_arg as 1018002d411SSowmini Varadhan * 1028002d411SSowmini Varadhan * (*callback)(ofmt_arg_t *ofmt_arg, char *buf, uint_t bufsize) 1038002d411SSowmini Varadhan * 1048002d411SSowmini Varadhan * Columns selected for output are identified by a match between the of_name 1058002d411SSowmini Varadhan * value in the ofmt_field_t and the fields_str requested. For each selected 1068002d411SSowmini Varadhan * column, the callback function (*of_cb)() is invoked, and is passed the of_id 1078002d411SSowmini Varadhan * value from the ofmt_field_t structure for the field. 1088002d411SSowmini Varadhan * 1098002d411SSowmini Varadhan * The interpretation of the of_id field is completely private to the caller, 1108002d411SSowmini Varadhan * and can be optionally used by the callback function as a cookie 1118002d411SSowmini Varadhan * to identify the field being printed when a single callback function is 1128002d411SSowmini Varadhan * shared between multiple ofmt_field_t entries. 1138002d411SSowmini Varadhan * 1148002d411SSowmini Varadhan * The callback function should fill `buf' with the string to be printed for 1158002d411SSowmini Varadhan * the field using the data in cbarg. 1168002d411SSowmini Varadhan * 1178002d411SSowmini Varadhan * The calling application should invoke ofmt_close(ofmt_handle) to free up any 1188002d411SSowmini Varadhan * resources allocated for the handle after all printing is completed. 1198002d411SSowmini Varadhan * 1208002d411SSowmini Varadhan * The printing library computes the current size of the output window when the 1218002d411SSowmini Varadhan * handle is first created. If the caller wishes to adjust the window size 1228002d411SSowmini Varadhan * after the handle has been created (e.g., on the reception of SIGWINCH by the 1238002d411SSowmini Varadhan * caller), the function ofmt_update_winsize(handle) may be called. 1248002d411SSowmini Varadhan */ 1258002d411SSowmini Varadhan 1268002d411SSowmini Varadhan #ifdef __cplusplus 1278002d411SSowmini Varadhan extern "C" { 1288002d411SSowmini Varadhan #endif 1298002d411SSowmini Varadhan 1308002d411SSowmini Varadhan /* 1318002d411SSowmini Varadhan * Recommended buffer size for buffers passed, for example, to ofmt_strerror(). 1328002d411SSowmini Varadhan */ 1338002d411SSowmini Varadhan #define OFMT_BUFSIZE 256 1348002d411SSowmini Varadhan 1358002d411SSowmini Varadhan typedef enum { 1368002d411SSowmini Varadhan OFMT_SUCCESS = 0, 1378002d411SSowmini Varadhan OFMT_ENOMEM, /* out of memory */ 1388002d411SSowmini Varadhan OFMT_EBADFIELDS, /* one or more bad fields with good fields */ 1398002d411SSowmini Varadhan OFMT_ENOFIELDS, /* no valid output fields */ 1408002d411SSowmini Varadhan OFMT_EPARSEALL, /* 'all' invalid in parsable mode */ 1418002d411SSowmini Varadhan OFMT_EPARSENONE, /* output fields missing in parsable mode */ 14225ec3e3dSEric Cheng OFMT_EPARSEWRAP, /* parsable mode incompatible with wrap mode */ 143dbed73cbSSangeeta Misra OFMT_ENOTEMPLATE, /* no template provided for fields */ 144dbed73cbSSangeeta Misra OFMT_EPARSEMULTI /* parsable and multiline don't mix */ 1458002d411SSowmini Varadhan } ofmt_status_t; 1468002d411SSowmini Varadhan 1478002d411SSowmini Varadhan /* 1488002d411SSowmini Varadhan * The callback function for each field is invoked with a pointer to the 1498002d411SSowmini Varadhan * ofmt_arg_t structure that contains the <id> registered by the application 1508002d411SSowmini Varadhan * for that field, and the cbarg used by the application when invoking 1518002d411SSowmini Varadhan * ofmt_output(). 1528002d411SSowmini Varadhan */ 1538002d411SSowmini Varadhan typedef struct ofmt_arg_s { 1548002d411SSowmini Varadhan uint_t ofmt_id; 155*0dc2366fSVenugopal Iyer uint_t ofmt_width; 156*0dc2366fSVenugopal Iyer uint_t ofmt_index; 1578002d411SSowmini Varadhan void *ofmt_cbarg; 1588002d411SSowmini Varadhan } ofmt_arg_t; 1598002d411SSowmini Varadhan 1608002d411SSowmini Varadhan /* 1618002d411SSowmini Varadhan * ofmt callback function that provides a string representation of the value to 1628002d411SSowmini Varadhan * be printed for the field. 1638002d411SSowmini Varadhan */ 1648002d411SSowmini Varadhan typedef boolean_t ofmt_cb_t(ofmt_arg_t *, char *, uint_t); 1658002d411SSowmini Varadhan typedef struct ofmt_field_s { 1668002d411SSowmini Varadhan char *of_name; /* column name */ 1678002d411SSowmini Varadhan uint_t of_width; /* output column width */ 1688002d411SSowmini Varadhan uint_t of_id; /* implementation specific cookie */ 1698002d411SSowmini Varadhan ofmt_cb_t *of_cb; /* callback function defined by caller */ 1708002d411SSowmini Varadhan } ofmt_field_t; 1718002d411SSowmini Varadhan 1728002d411SSowmini Varadhan /* 1738002d411SSowmini Varadhan * ofmt_open() must be called to create the ofmt_handle_t; Resources allocated 1748002d411SSowmini Varadhan * for the handle are freed by ofmt_close(); 1758002d411SSowmini Varadhan */ 1768002d411SSowmini Varadhan typedef struct ofmt_state_s *ofmt_handle_t; 1772b24ab6bSSebastien Roy extern ofmt_status_t ofmt_open(const char *, const ofmt_field_t *, uint_t, 1788002d411SSowmini Varadhan uint_t, ofmt_handle_t *); 1798002d411SSowmini Varadhan 1808002d411SSowmini Varadhan #define OFMT_PARSABLE 0x00000001 /* machine parsable mode */ 18125ec3e3dSEric Cheng #define OFMT_WRAP 0x00000002 /* wrap output if field width is exceeded */ 182dbed73cbSSangeeta Misra #define OFMT_MULTILINE 0x00000004 /* "long" output: "name: value" lines */ 183*0dc2366fSVenugopal Iyer #define OFMT_RIGHTJUST 0x00000008 /* right justified output */ 1848002d411SSowmini Varadhan 1858002d411SSowmini Varadhan /* 1868002d411SSowmini Varadhan * ofmt_close() must be called to free resources associated 1878002d411SSowmini Varadhan * with the ofmt_handle_t 1888002d411SSowmini Varadhan */ 1898002d411SSowmini Varadhan extern void ofmt_close(ofmt_handle_t); 1908002d411SSowmini Varadhan 1918002d411SSowmini Varadhan /* 1928002d411SSowmini Varadhan * ofmt_print() emits one row of output 1938002d411SSowmini Varadhan */ 1948002d411SSowmini Varadhan extern void ofmt_print(ofmt_handle_t, void *); 1958002d411SSowmini Varadhan 1968002d411SSowmini Varadhan /* 1978002d411SSowmini Varadhan * ofmt_update_winsize() updates the window size information for ofmt_handle_t 1988002d411SSowmini Varadhan */ 1998002d411SSowmini Varadhan extern void ofmt_update_winsize(ofmt_handle_t); 2008002d411SSowmini Varadhan 2018002d411SSowmini Varadhan /* 2028002d411SSowmini Varadhan * ofmt_strerror() provides error diagnostics in the buffer that it is passed. 2038002d411SSowmini Varadhan */ 2048002d411SSowmini Varadhan extern char *ofmt_strerror(ofmt_handle_t, ofmt_status_t, char *, uint_t); 2058002d411SSowmini Varadhan 2068002d411SSowmini Varadhan #ifdef __cplusplus 2078002d411SSowmini Varadhan } 2088002d411SSowmini Varadhan #endif 2098002d411SSowmini Varadhan 2108002d411SSowmini Varadhan #endif /* _OFMT_H */ 211