/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <ofmt.h>
#include <unistd.h>
#include <sys/sysmacros.h>
#include <libintl.h>
/*
* functions and structures to internally process a comma-separated string
* of fields selected for output.
*/
typedef struct {
char *s_buf;
} split_t;
/*
* The state of the output is tracked in a ofmt_state_t structure.
* Each os_fields[i] entry points at an ofmt_field_t array for
* the sub-command whose contents are provided by the caller, with
* os_nfields set to the number of requested fields.
*/
typedef struct ofmt_state_s {
int os_nrow;
int os_nbad;
char **os_badfields;
} ofmt_state_t;
/*
* A B_TRUE return value from the callback function will print out the contents
* of the output buffer, except when the buffer is returned with the empty
* string "", in which case the OFMT_VAL_UNDEF will be printed.
*
* If the callback function returns B_FALSE, the "?" string will be emitted.
*/
/*
* The maximum number of rows supported by the OFMT_WRAP option.
*/
static void ofmt_print_header(ofmt_state_t *);
/*
* Split `str' into at most `maxfields' fields, Return a pointer to a
* split_t containing the split fields, or NULL on failure.
*/
static split_t *
{
return (NULL);
return (NULL);
goto fail;
goto fail;
}
return (sp);
fail:
return (NULL);
}
/*
* Split `fields' into at most `maxfields' fields. Return a pointer to
* a split_t containing the split fields, or NULL on failure. Invoked
* when all fields are implicitly selected at handle creation by
* passing in a NULL fields_str
*/
static split_t *
{
int i, cols;
return (NULL);
goto fail;
cols = 0;
for (i = 0; i < maxfields; i++) {
/*
* If all fields are implied without explicitly passing
* in a fields_str, build a list of field names, stopping
* when we run out of columns.
*/
break;
}
return (sp);
fail:
return (NULL);
}
/*
* Free the split_t structure pointed to by `sp'.
*/
static void
{
return;
}
/*
* Open a handle to be used for printing formatted output.
*/
{
int nfields = 0;
if (parsable) {
if (multiline)
return (OFMT_EPARSEMULTI);
/*
* For parsable output mode, the caller always needs
* to specify precisely which fields are to be selected,
* since the set of fields may change over time.
*/
return (OFMT_EPARSENONE);
return (OFMT_EPARSEALL);
if (wrap)
return (OFMT_EPARSEWRAP);
}
return (OFMT_ENOTEMPLATE);
nfields++;
/*
* split str into the columns selected, or construct the
* full set of columns (equivalent to -o all).
*/
} else {
maxcols = 0;
}
goto nomem;
goto nomem;
of_index = 0;
/*
* sp->s_nfields is the number of fields requested in fields_str.
* nfields is the number of fields in template.
*/
for (j = 0; j < nfields; j++) {
break;
}
}
if (j == nfields) {
sizeof (char *));
goto nomem;
}
goto nomem;
continue;
}
goto nomem;
if (multiline) {
}
of_index++;
}
if (of_index == 0) /* all values in str are bogus */
return (OFMT_ENOFIELDS);
return (err);
err = OFMT_ENOMEM;
ofmt_close(os);
return (err);
}
/*
* free resources associated with the ofmt_handle_t
*/
void
{
int i;
return;
for (i = 0; i < os->os_nfields; i++)
}
/*
* Print the value for the selected field by calling the callback-function
* registered for the field.
*/
static void
{
char c;
/*
* Parsable fields are separated by ':'. If such a field contains
* a ':' or '\', this character is prefixed by a '\'.
*/
if (parsable) {
return;
}
while ((c = *value++) != '\0') {
(void) putchar('\\');
(void) putchar(c);
}
if (!os->os_lastfield)
(void) putchar(':');
} else if (multiline) {
if (value[0] == '\0')
if (!os->os_lastfield)
(void) putchar('\n');
} else {
if (os->os_lastfield) {
if (rightjust)
else
os->os_overflow = 0;
return;
}
if (rightjust)
else
return;
}
if (os->os_overflow > 0) {
}
if (rightjust)
else
}
}
/*
* Print enough to fit the field width.
*/
static void
{
int i, nextlen;
return;
}
nextlen = 0;
} else {
}
i++;
break;
}
}
sp->s_currfield = i;
}
/*
* Print one or more rows of output values for the selected columns.
*/
void
{
int i;
if (wrap) {
return;
}
}
(void) putchar('\n');
for (i = 0; i < os->os_nfields; i++) {
value[0] = '\0';
if (wrap) {
/*
* 'value' will be split at comma boundaries
* and stored into sp[i].
*/
sizeof (value));
}
} else {
}
}
(void) putchar('\n');
while (more_rows) {
for (i = 0; i < os->os_nfields; i++) {
value[0] = '\0';
}
(void) putchar('\n');
}
for (i = 0; i < os->os_nfields; i++)
}
}
/*
* Print the field headers
*/
static void
{
int i;
for (i = 0; i < os->os_nfields; i++) {
}
(void) putchar('\n');
}
/*
* Update the current window size.
*/
void
{
}
}
/*
* Return error diagnostics using the information in the ofmt_handle_t
*/
char *
{
int i;
const char *s;
/*
* ebuf is intended for optional error-specific data to be appended
* after the internationalized error string for an error code.
*/
ebuf[0] = '\0';
switch (err) {
case OFMT_SUCCESS:
s = "success";
break;
case OFMT_EBADFIELDS:
/*
* and error to simplify and improve localization.
*/
if (!parsable) {
s = "ignoring unknown output fields:";
else
s = "ignoring unknown output field:";
} else {
s = "unknown output fields:";
else
s = "unknown output field:";
}
/* set up the bad fields in ebuf */
sizeof (ebuf));
}
break;
case OFMT_ENOFIELDS:
s = "no valid output fields";
break;
case OFMT_EPARSEMULTI:
s = "multiline mode incompatible with parsable mode";
break;
case OFMT_EPARSEALL:
s = "output field `all' invalid in parsable mode";
break;
case OFMT_EPARSENONE:
s = "output fields must be specified in parsable mode";
break;
case OFMT_EPARSEWRAP:
s = "parsable mode is incompatible with wrap mode";
break;
case OFMT_ENOTEMPLATE:
s = "no template provided for fields";
break;
case OFMT_ENOMEM:
break;
default:
err);
return (buf);
}
return (buf);
}