8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * CDDL HEADER START
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * The contents of this file are subject to the terms of the
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Common Development and Distribution License (the "License").
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * You may not use this file except in compliance with the License.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * or http://www.opensolaris.org/os/licensing.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * See the License for the specific language governing permissions
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * and limitations under the License.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * When distributing Covered Code, include this CDDL HEADER in each
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * If applicable, add the following below this CDDL HEADER, with the
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * fields enclosed by brackets "[]" replaced with your own identifying
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * information: Portions Copyright [yyyy] [name of copyright owner]
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * CDDL HEADER END
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Use is subject to license terms.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * functions and structures to internally process a comma-separated string
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * of fields selected for output.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhantypedef struct {
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan const char **s_fields; /* array of pointers to the fields in s_buf */
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan uint_t s_nfields; /* the number of fields in s_buf */
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng uint_t s_currfield; /* the current field being processed */
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhanstatic split_t *split_str(const char *, uint_t);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhanstatic split_t *split_fields(const ofmt_field_t *, uint_t, uint_t);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * The state of the output is tracked in a ofmt_state_t structure.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Each os_fields[i] entry points at an ofmt_field_t array for
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * the sub-command whose contents are provided by the caller, with
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * os_nfields set to the number of requested fields.
dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9Sangeeta Misra int os_maxnamelen; /* longest name (f. multiline) */
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * A B_TRUE return value from the callback function will print out the contents
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * of the output buffer, except when the buffer is returned with the empty
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * string "", in which case the OFMT_VAL_UNDEF will be printed.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * If the callback function returns B_FALSE, the "?" string will be emitted.
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng * The maximum number of rows supported by the OFMT_WRAP option.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhanstatic void ofmt_print_header(ofmt_state_t *);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhanstatic void ofmt_print_field(ofmt_state_t *, ofmt_field_t *, const char *,
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Split `str' into at most `maxfields' fields, Return a pointer to a
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * split_t containing the split fields, or NULL on failure.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhansplit_str(const char *str, uint_t maxfields)
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan sp->s_fields = malloc(sizeof (char *) * maxfields);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan if (sp->s_buf == NULL || sp->s_fields == NULL)
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan while ((field = strtok_r(token, ",", &lasts)) != NULL) {
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Split `fields' into at most `maxfields' fields. Return a pointer to
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * a split_t containing the split fields, or NULL on failure. Invoked
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * when all fields are implicitly selected at handle creation by
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * passing in a NULL fields_str
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhansplit_fields(const ofmt_field_t *template, uint_t maxfields, uint_t maxcols)
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan sp->s_fields = malloc(sizeof (char *) * maxfields);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan for (i = 0; i < maxfields; i++) {
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * If all fields are implied without explicitly passing
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * in a fields_str, build a list of field names, stopping
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * when we run out of columns.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan sp->s_fields[sp->s_nfields++] = template[i].of_name;
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Free the split_t structure pointed to by `sp'.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Open a handle to be used for printing formatted output.
2b24ab6b3865caeede9eeb9db6b83e1d89dcd1eaSebastien Royofmt_open(const char *str, const ofmt_field_t *template, uint_t flags,
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer boolean_t parsable = (flags & OFMT_PARSABLE);
dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9Sangeeta Misra boolean_t multiline = (flags & OFMT_MULTILINE);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * For parsable output mode, the caller always needs
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * to specify precisely which fields are to be selected,
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * since the set of fields may change over time.
2b24ab6b3865caeede9eeb9db6b83e1d89dcd1eaSebastien Roy for (ofp = template; ofp->of_name != NULL; ofp++)
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * split str into the columns selected, or construct the
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * full set of columns (equivalent to -o all).
7bbc5723436be52a39f5c04d60cc236082c52d5aYuri Pankov if (str != NULL && strcasecmp(str, "all") != 0) {
7bbc5723436be52a39f5c04d60cc236082c52d5aYuri Pankov if (parsable || (str != NULL && strcasecmp(str, "all") == 0))
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan sp = split_fields(template, nfields, maxcols);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * sp->s_nfields is the number of fields requested in fields_str.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * nfields is the number of fields in template.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan for (j = 0; j < nfields; j++) {
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan sizeof (char *));
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan os->os_badfields[nbad] = strdup(sp->s_fields[i]);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan of[of_index].of_name = strdup(template[j].of_name);
dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9Sangeeta Misra os->os_maxnamelen = MAX(n, os->os_maxnamelen);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan of[of_index].of_width = template[j].of_width;
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan if (of_index == 0) /* all values in str are bogus */
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan os->os_nfields = of_index; /* actual number of fields printed */
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * free resources associated with the ofmt_handle_t
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Print the value for the selected field by calling the callback-function
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * registered for the field.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhanofmt_print_field(ofmt_state_t *os, ofmt_field_t *ofp, const char *value,
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer boolean_t parsable = (os->os_flags & OFMT_PARSABLE);
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer boolean_t multiline = (os->os_flags & OFMT_MULTILINE);
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer boolean_t rightjust = (os->os_flags & OFMT_RIGHTJUST);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Parsable fields are separated by ':'. If such a field contains
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * a ':' or '\', this character is prefixed by a '\'.
dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9Sangeeta Misra (void) printf("%*.*s: %s", os->os_maxnamelen,
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan compress = MIN(os->os_overflow, width - valwidth);
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng * Print enough to fit the field width.
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Chengofmt_fit_width(split_t **spp, uint_t width, char *value, uint_t bufsize)
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng for (i = sp->s_currfield; i < sp->s_nfields; i++) {
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng ptr += snprintf(ptr, lim - ptr, "%s,", sp->s_fields[i]);
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng if (strlen(value) + nextlen > width || ptr >= lim) {
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng * Print one or more rows of output values for the selected columns.
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer boolean_t parsable = (os->os_flags & OFMT_PARSABLE);
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer boolean_t multiline = (os->os_flags & OFMT_MULTILINE);
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer if ((os->os_nrow++ % os->os_winsize.ws_row) == 0 &&
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan os->os_lastfield = (i + 1 == os->os_nfields);
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng if ((*of[i].of_cb)(&ofarg, value, sizeof (value))) {
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng * 'value' will be split at comma boundaries
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng * and stored into sp[i].
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan ofmt_print_field(os, &of[i], OFMT_VAL_UNKNOWN, escsep);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Print the field headers
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan os->os_lastfield = (i + 1 == os->os_nfields);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan ofmt_print_field(os, &of[i], of[i].of_name, escsep);
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Update the current window size.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan winsize->ws_col == 0 || winsize->ws_row == 0) {
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Return error diagnostics using the information in the ofmt_handle_t
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhanofmt_strerror(ofmt_handle_t ofmt, ofmt_status_t err, char *buf, uint_t bufsize)
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan const char *s;
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * ebuf is intended for optional error-specific data to be appended
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * after the internationalized error string for an error code.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan s = "success";
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * Enumerate the singular/plural version of the warning
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan * and error to simplify and improve localization.
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan s = "ignoring unknown output fields:";
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan s = "ignoring unknown output field:";
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan s = "unknown output fields:";
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan s = "unknown output field:";
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan /* set up the bad fields in ebuf */
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan s = "no valid output fields";
dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9Sangeeta Misra s = "multiline mode incompatible with parsable mode";
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan s = "output field `all' invalid in parsable mode";
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan s = "output fields must be specified in parsable mode";
25ec3e3dd27cc1038c10efa18ed08f064eab5fbeEric Cheng s = "parsable mode is incompatible with wrap mode";
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan s = "no template provided for fields";
8002d4117c1ea26aff1f16f584ae97bdbd5b21d5Sowmini Varadhan dgettext(TEXT_DOMAIN, "unknown ofmt error (%d)"),