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 #include <errno.h> 278002d411SSowmini Varadhan #include <sys/types.h> 288002d411SSowmini Varadhan #include <stdlib.h> 298002d411SSowmini Varadhan #include <string.h> 308002d411SSowmini Varadhan #include <strings.h> 318002d411SSowmini Varadhan #include <stdio.h> 328002d411SSowmini Varadhan #include <ofmt.h> 338002d411SSowmini Varadhan #include <sys/termios.h> 348002d411SSowmini Varadhan #include <unistd.h> 358002d411SSowmini Varadhan #include <sys/sysmacros.h> 368002d411SSowmini Varadhan #include <libintl.h> 378002d411SSowmini Varadhan 388002d411SSowmini Varadhan /* 398002d411SSowmini Varadhan * functions and structures to internally process a comma-separated string 408002d411SSowmini Varadhan * of fields selected for output. 418002d411SSowmini Varadhan */ 428002d411SSowmini Varadhan typedef struct { 438002d411SSowmini Varadhan char *s_buf; 448002d411SSowmini Varadhan const char **s_fields; /* array of pointers to the fields in s_buf */ 458002d411SSowmini Varadhan uint_t s_nfields; /* the number of fields in s_buf */ 46*25ec3e3dSEric Cheng uint_t s_currfield; /* the current field being processed */ 478002d411SSowmini Varadhan } split_t; 488002d411SSowmini Varadhan static void splitfree(split_t *); 498002d411SSowmini Varadhan static split_t *split_str(const char *, uint_t); 508002d411SSowmini Varadhan static split_t *split_fields(const ofmt_field_t *, uint_t, uint_t); 518002d411SSowmini Varadhan 528002d411SSowmini Varadhan /* 538002d411SSowmini Varadhan * The state of the output is tracked in a ofmt_state_t structure. 548002d411SSowmini Varadhan * Each os_fields[i] entry points at an ofmt_field_t array for 558002d411SSowmini Varadhan * the sub-command whose contents are provided by the caller, with 568002d411SSowmini Varadhan * os_nfields set to the number of requested fields. 578002d411SSowmini Varadhan */ 588002d411SSowmini Varadhan typedef struct ofmt_state_s { 598002d411SSowmini Varadhan ofmt_field_t *os_fields; 608002d411SSowmini Varadhan uint_t os_nfields; 618002d411SSowmini Varadhan boolean_t os_lastfield; 628002d411SSowmini Varadhan uint_t os_overflow; 638002d411SSowmini Varadhan struct winsize os_winsize; 648002d411SSowmini Varadhan int os_nrow; 658002d411SSowmini Varadhan boolean_t os_parsable; 66*25ec3e3dSEric Cheng boolean_t os_wrap; 678002d411SSowmini Varadhan int os_nbad; 688002d411SSowmini Varadhan char **os_badfields; 698002d411SSowmini Varadhan } ofmt_state_t; 708002d411SSowmini Varadhan /* 718002d411SSowmini Varadhan * A B_TRUE return value from the callback function will print out the contents 728002d411SSowmini Varadhan * of the output buffer, except when the buffer is returned with the empty 738002d411SSowmini Varadhan * string "", in which case the OFMT_VAL_UNDEF will be printed. 748002d411SSowmini Varadhan * 758002d411SSowmini Varadhan * If the callback function returns B_FALSE, the "?" string will be emitted. 768002d411SSowmini Varadhan */ 778002d411SSowmini Varadhan #define OFMT_VAL_UNDEF "--" 788002d411SSowmini Varadhan #define OFMT_VAL_UNKNOWN "?" 79*25ec3e3dSEric Cheng 80*25ec3e3dSEric Cheng /* 81*25ec3e3dSEric Cheng * The maximum number of rows supported by the OFMT_WRAP option. 82*25ec3e3dSEric Cheng */ 83*25ec3e3dSEric Cheng #define OFMT_MAX_ROWS 128 84*25ec3e3dSEric Cheng 858002d411SSowmini Varadhan static void ofmt_print_header(ofmt_state_t *); 868002d411SSowmini Varadhan static void ofmt_print_field(ofmt_state_t *, ofmt_field_t *, const char *, 878002d411SSowmini Varadhan boolean_t); 888002d411SSowmini Varadhan 898002d411SSowmini Varadhan /* 908002d411SSowmini Varadhan * Split `str' into at most `maxfields' fields, Return a pointer to a 918002d411SSowmini Varadhan * split_t containing the split fields, or NULL on failure. 928002d411SSowmini Varadhan */ 938002d411SSowmini Varadhan static split_t * 948002d411SSowmini Varadhan split_str(const char *str, uint_t maxfields) 958002d411SSowmini Varadhan { 968002d411SSowmini Varadhan char *field, *token, *lasts = NULL; 978002d411SSowmini Varadhan split_t *sp; 988002d411SSowmini Varadhan 998002d411SSowmini Varadhan if (*str == '\0' || maxfields == 0) 1008002d411SSowmini Varadhan return (NULL); 1018002d411SSowmini Varadhan 1028002d411SSowmini Varadhan sp = calloc(sizeof (split_t), 1); 1038002d411SSowmini Varadhan if (sp == NULL) 1048002d411SSowmini Varadhan return (NULL); 1058002d411SSowmini Varadhan 1068002d411SSowmini Varadhan sp->s_buf = strdup(str); 1078002d411SSowmini Varadhan sp->s_fields = malloc(sizeof (char *) * maxfields); 1088002d411SSowmini Varadhan if (sp->s_buf == NULL || sp->s_fields == NULL) 1098002d411SSowmini Varadhan goto fail; 1108002d411SSowmini Varadhan 1118002d411SSowmini Varadhan token = sp->s_buf; 1128002d411SSowmini Varadhan while ((field = strtok_r(token, ",", &lasts)) != NULL) { 1138002d411SSowmini Varadhan if (sp->s_nfields == maxfields) 1148002d411SSowmini Varadhan goto fail; 1158002d411SSowmini Varadhan token = NULL; 1168002d411SSowmini Varadhan sp->s_fields[sp->s_nfields++] = field; 1178002d411SSowmini Varadhan } 1188002d411SSowmini Varadhan return (sp); 1198002d411SSowmini Varadhan fail: 1208002d411SSowmini Varadhan splitfree(sp); 1218002d411SSowmini Varadhan return (NULL); 1228002d411SSowmini Varadhan } 1238002d411SSowmini Varadhan 1248002d411SSowmini Varadhan /* 1258002d411SSowmini Varadhan * Split `fields' into at most `maxfields' fields. Return a pointer to 1268002d411SSowmini Varadhan * a split_t containing the split fields, or NULL on failure. Invoked 1278002d411SSowmini Varadhan * when all fields are implicitly selected at handle creation by 1288002d411SSowmini Varadhan * passing in a NULL fields_str 1298002d411SSowmini Varadhan */ 1308002d411SSowmini Varadhan static split_t * 1318002d411SSowmini Varadhan split_fields(const ofmt_field_t *template, uint_t maxfields, uint_t maxcols) 1328002d411SSowmini Varadhan { 1338002d411SSowmini Varadhan split_t *sp; 1348002d411SSowmini Varadhan int i, cols; 1358002d411SSowmini Varadhan 1368002d411SSowmini Varadhan sp = calloc(sizeof (split_t), 1); 1378002d411SSowmini Varadhan if (sp == NULL) 1388002d411SSowmini Varadhan return (NULL); 1398002d411SSowmini Varadhan 1408002d411SSowmini Varadhan sp->s_fields = malloc(sizeof (char *) * maxfields); 1418002d411SSowmini Varadhan if (sp->s_fields == NULL) 1428002d411SSowmini Varadhan goto fail; 1438002d411SSowmini Varadhan cols = 0; 1448002d411SSowmini Varadhan for (i = 0; i < maxfields; i++) { 1458002d411SSowmini Varadhan cols += template[i].of_width; 1468002d411SSowmini Varadhan /* 1478002d411SSowmini Varadhan * If all fields are implied without explicitly passing 1488002d411SSowmini Varadhan * in a fields_str, build a list of field names, stopping 1498002d411SSowmini Varadhan * when we run out of columns. 1508002d411SSowmini Varadhan */ 1518002d411SSowmini Varadhan if (maxcols > 0 && cols > maxcols) 1528002d411SSowmini Varadhan break; 1538002d411SSowmini Varadhan sp->s_fields[sp->s_nfields++] = template[i].of_name; 1548002d411SSowmini Varadhan } 1558002d411SSowmini Varadhan return (sp); 1568002d411SSowmini Varadhan fail: 1578002d411SSowmini Varadhan splitfree(sp); 1588002d411SSowmini Varadhan return (NULL); 1598002d411SSowmini Varadhan } 1608002d411SSowmini Varadhan 1618002d411SSowmini Varadhan /* 1628002d411SSowmini Varadhan * Free the split_t structure pointed to by `sp'. 1638002d411SSowmini Varadhan */ 1648002d411SSowmini Varadhan static void 1658002d411SSowmini Varadhan splitfree(split_t *sp) 1668002d411SSowmini Varadhan { 1678002d411SSowmini Varadhan if (sp == NULL) 1688002d411SSowmini Varadhan return; 1698002d411SSowmini Varadhan free(sp->s_buf); 1708002d411SSowmini Varadhan free(sp->s_fields); 1718002d411SSowmini Varadhan free(sp); 1728002d411SSowmini Varadhan } 1738002d411SSowmini Varadhan 1748002d411SSowmini Varadhan /* 1758002d411SSowmini Varadhan * Open a handle to be used for printing formatted output. 1768002d411SSowmini Varadhan */ 1778002d411SSowmini Varadhan ofmt_status_t 1782b24ab6bSSebastien Roy ofmt_open(const char *str, const ofmt_field_t *template, uint_t flags, 1798002d411SSowmini Varadhan uint_t maxcols, ofmt_handle_t *ofmt) 1808002d411SSowmini Varadhan { 1818002d411SSowmini Varadhan split_t *sp; 1828002d411SSowmini Varadhan uint_t i, j, of_index; 1832b24ab6bSSebastien Roy const ofmt_field_t *ofp; 1848002d411SSowmini Varadhan ofmt_field_t *of; 1858002d411SSowmini Varadhan ofmt_state_t *os; 1868002d411SSowmini Varadhan int nfields = 0; 1878002d411SSowmini Varadhan ofmt_status_t err = OFMT_SUCCESS; 188*25ec3e3dSEric Cheng boolean_t parsable = ((flags & OFMT_PARSABLE) != 0); 189*25ec3e3dSEric Cheng boolean_t wrap = ((flags & OFMT_WRAP) != 0); 1908002d411SSowmini Varadhan 1918002d411SSowmini Varadhan *ofmt = NULL; 1928002d411SSowmini Varadhan if (parsable) { 1938002d411SSowmini Varadhan /* 1948002d411SSowmini Varadhan * For parsable output mode, the caller always needs 1958002d411SSowmini Varadhan * to specify precisely which fields are to be selected, 1968002d411SSowmini Varadhan * since the set of fields may change over time. 1978002d411SSowmini Varadhan */ 1988002d411SSowmini Varadhan if (str == NULL || str[0] == '\0') 1998002d411SSowmini Varadhan return (OFMT_EPARSENONE); 2008002d411SSowmini Varadhan if (strcmp(str, "all") == 0) 2018002d411SSowmini Varadhan return (OFMT_EPARSEALL); 202*25ec3e3dSEric Cheng if (wrap) 203*25ec3e3dSEric Cheng return (OFMT_EPARSEWRAP); 2048002d411SSowmini Varadhan } 2058002d411SSowmini Varadhan if (template == NULL) 2068002d411SSowmini Varadhan return (OFMT_ENOTEMPLATE); 2072b24ab6bSSebastien Roy for (ofp = template; ofp->of_name != NULL; ofp++) 2088002d411SSowmini Varadhan nfields++; 2098002d411SSowmini Varadhan /* 2108002d411SSowmini Varadhan * split str into the columns selected, or construct the 2118002d411SSowmini Varadhan * full set of columns (equivalent to -o all). 2128002d411SSowmini Varadhan */ 2138002d411SSowmini Varadhan if (str != NULL && strcmp(str, "all") != 0) { 2148002d411SSowmini Varadhan sp = split_str(str, nfields); 2158002d411SSowmini Varadhan } else { 2168002d411SSowmini Varadhan if (parsable || (str != NULL && strcmp(str, "all") == 0)) 2178002d411SSowmini Varadhan maxcols = 0; 2188002d411SSowmini Varadhan sp = split_fields(template, nfields, maxcols); 2198002d411SSowmini Varadhan } 2208002d411SSowmini Varadhan if (sp == NULL) 2218002d411SSowmini Varadhan goto nomem; 2228002d411SSowmini Varadhan 2238002d411SSowmini Varadhan os = calloc(sizeof (ofmt_state_t) + 2248002d411SSowmini Varadhan sp->s_nfields * sizeof (ofmt_field_t), 1); 2258002d411SSowmini Varadhan if (os == NULL) 2268002d411SSowmini Varadhan goto nomem; 2278002d411SSowmini Varadhan *ofmt = os; 2288002d411SSowmini Varadhan os->os_fields = (ofmt_field_t *)&os[1]; 2298002d411SSowmini Varadhan os->os_parsable = parsable; 230*25ec3e3dSEric Cheng os->os_wrap = wrap; 231*25ec3e3dSEric Cheng 2328002d411SSowmini Varadhan of = os->os_fields; 2338002d411SSowmini Varadhan of_index = 0; 2348002d411SSowmini Varadhan /* 2358002d411SSowmini Varadhan * sp->s_nfields is the number of fields requested in fields_str. 2368002d411SSowmini Varadhan * nfields is the number of fields in template. 2378002d411SSowmini Varadhan */ 2388002d411SSowmini Varadhan for (i = 0; i < sp->s_nfields; i++) { 2398002d411SSowmini Varadhan for (j = 0; j < nfields; j++) { 2408002d411SSowmini Varadhan if (strcasecmp(sp->s_fields[i], 2418002d411SSowmini Varadhan template[j].of_name) == 0) { 2428002d411SSowmini Varadhan break; 2438002d411SSowmini Varadhan } 2448002d411SSowmini Varadhan } 2458002d411SSowmini Varadhan if (j == nfields) { 2468002d411SSowmini Varadhan int nbad = os->os_nbad++; 2478002d411SSowmini Varadhan 2488002d411SSowmini Varadhan err = OFMT_EBADFIELDS; 2498002d411SSowmini Varadhan if (os->os_badfields == NULL) { 2508002d411SSowmini Varadhan os->os_badfields = malloc(sp->s_nfields * 2518002d411SSowmini Varadhan sizeof (char *)); 2528002d411SSowmini Varadhan if (os->os_badfields == NULL) 2538002d411SSowmini Varadhan goto nomem; 2548002d411SSowmini Varadhan } 2558002d411SSowmini Varadhan os->os_badfields[nbad] = strdup(sp->s_fields[i]); 2568002d411SSowmini Varadhan if (os->os_badfields[nbad] == NULL) 2578002d411SSowmini Varadhan goto nomem; 2588002d411SSowmini Varadhan continue; 2598002d411SSowmini Varadhan } 2608002d411SSowmini Varadhan of[of_index].of_name = strdup(template[j].of_name); 2618002d411SSowmini Varadhan if (of[of_index].of_name == NULL) 2628002d411SSowmini Varadhan goto nomem; 2638002d411SSowmini Varadhan of[of_index].of_width = template[j].of_width; 2648002d411SSowmini Varadhan of[of_index].of_id = template[j].of_id; 2658002d411SSowmini Varadhan of[of_index].of_cb = template[j].of_cb; 2668002d411SSowmini Varadhan of_index++; 2678002d411SSowmini Varadhan } 2688002d411SSowmini Varadhan splitfree(sp); 2698002d411SSowmini Varadhan if (of_index == 0) /* all values in str are bogus */ 2708002d411SSowmini Varadhan return (OFMT_ENOFIELDS); 2718002d411SSowmini Varadhan os->os_nfields = of_index; /* actual number of fields printed */ 2728002d411SSowmini Varadhan ofmt_update_winsize(*ofmt); 2738002d411SSowmini Varadhan return (err); 2748002d411SSowmini Varadhan nomem: 2758002d411SSowmini Varadhan err = OFMT_ENOMEM; 2768002d411SSowmini Varadhan if (os != NULL) 2778002d411SSowmini Varadhan ofmt_close(os); 2788002d411SSowmini Varadhan *ofmt = NULL; 2798002d411SSowmini Varadhan splitfree(sp); 2808002d411SSowmini Varadhan return (err); 2818002d411SSowmini Varadhan } 2828002d411SSowmini Varadhan 2838002d411SSowmini Varadhan /* 2848002d411SSowmini Varadhan * free resources associated with the ofmt_handle_t 2858002d411SSowmini Varadhan */ 2868002d411SSowmini Varadhan void 2878002d411SSowmini Varadhan ofmt_close(ofmt_handle_t ofmt) 2888002d411SSowmini Varadhan { 2898002d411SSowmini Varadhan ofmt_state_t *os = ofmt; 2908002d411SSowmini Varadhan int i; 2918002d411SSowmini Varadhan 2928002d411SSowmini Varadhan if (os == NULL) 2938002d411SSowmini Varadhan return; 2948002d411SSowmini Varadhan for (i = 0; i < os->os_nfields; i++) 2958002d411SSowmini Varadhan free(os->os_fields[i].of_name); 2968002d411SSowmini Varadhan for (i = 0; i < os->os_nbad; i++) 2978002d411SSowmini Varadhan free(os->os_badfields[i]); 2988002d411SSowmini Varadhan free(os->os_badfields); 2998002d411SSowmini Varadhan free(os); 3008002d411SSowmini Varadhan } 3018002d411SSowmini Varadhan 3028002d411SSowmini Varadhan /* 3038002d411SSowmini Varadhan * Print the value for the selected field by calling the callback-function 3048002d411SSowmini Varadhan * registered for the field. 3058002d411SSowmini Varadhan */ 3068002d411SSowmini Varadhan static void 3078002d411SSowmini Varadhan ofmt_print_field(ofmt_state_t *os, ofmt_field_t *ofp, const char *value, 3088002d411SSowmini Varadhan boolean_t escsep) 3098002d411SSowmini Varadhan { 3108002d411SSowmini Varadhan uint_t width = ofp->of_width; 3118002d411SSowmini Varadhan uint_t valwidth; 3128002d411SSowmini Varadhan uint_t compress; 3138002d411SSowmini Varadhan boolean_t parsable = os->os_parsable; 3148002d411SSowmini Varadhan char c; 3158002d411SSowmini Varadhan 3168002d411SSowmini Varadhan /* 3178002d411SSowmini Varadhan * Parsable fields are separated by ':'. If such a field contains 3188002d411SSowmini Varadhan * a ':' or '\', this character is prefixed by a '\'. 3198002d411SSowmini Varadhan */ 3208002d411SSowmini Varadhan if (parsable) { 3218002d411SSowmini Varadhan if (os->os_nfields == 1) { 3228002d411SSowmini Varadhan (void) printf("%s", value); 3238002d411SSowmini Varadhan return; 3248002d411SSowmini Varadhan } 3258002d411SSowmini Varadhan while ((c = *value++) != '\0') { 3268002d411SSowmini Varadhan if (escsep && ((c == ':' || c == '\\'))) 3278002d411SSowmini Varadhan (void) putchar('\\'); 3288002d411SSowmini Varadhan (void) putchar(c); 3298002d411SSowmini Varadhan } 3308002d411SSowmini Varadhan if (!os->os_lastfield) 3318002d411SSowmini Varadhan (void) putchar(':'); 3328002d411SSowmini Varadhan return; 3338002d411SSowmini Varadhan } else { 3348002d411SSowmini Varadhan if (os->os_lastfield) { 3358002d411SSowmini Varadhan (void) printf("%s", value); 3368002d411SSowmini Varadhan os->os_overflow = 0; 3378002d411SSowmini Varadhan return; 3388002d411SSowmini Varadhan } 3398002d411SSowmini Varadhan 3408002d411SSowmini Varadhan valwidth = strlen(value); 3418002d411SSowmini Varadhan if (valwidth + os->os_overflow >= width) { 3428002d411SSowmini Varadhan os->os_overflow += valwidth - width + 1; 3438002d411SSowmini Varadhan (void) printf("%s ", value); 3448002d411SSowmini Varadhan return; 3458002d411SSowmini Varadhan } 3468002d411SSowmini Varadhan 3478002d411SSowmini Varadhan if (os->os_overflow > 0) { 3488002d411SSowmini Varadhan compress = MIN(os->os_overflow, width - valwidth); 3498002d411SSowmini Varadhan os->os_overflow -= compress; 3508002d411SSowmini Varadhan width -= compress; 3518002d411SSowmini Varadhan } 3528002d411SSowmini Varadhan (void) printf("%-*s", width, value); 3538002d411SSowmini Varadhan } 3548002d411SSowmini Varadhan } 3558002d411SSowmini Varadhan 3568002d411SSowmini Varadhan /* 357*25ec3e3dSEric Cheng * Print enough to fit the field width. 358*25ec3e3dSEric Cheng */ 359*25ec3e3dSEric Cheng static void 360*25ec3e3dSEric Cheng ofmt_fit_width(split_t **spp, uint_t width, char *value, uint_t bufsize) 361*25ec3e3dSEric Cheng { 362*25ec3e3dSEric Cheng split_t *sp = *spp; 363*25ec3e3dSEric Cheng char *ptr = value, *lim = ptr + bufsize; 364*25ec3e3dSEric Cheng int i, nextlen; 365*25ec3e3dSEric Cheng 366*25ec3e3dSEric Cheng if (sp == NULL) { 367*25ec3e3dSEric Cheng sp = split_str(value, OFMT_MAX_ROWS); 368*25ec3e3dSEric Cheng if (sp == NULL) 369*25ec3e3dSEric Cheng return; 370*25ec3e3dSEric Cheng 371*25ec3e3dSEric Cheng *spp = sp; 372*25ec3e3dSEric Cheng } 373*25ec3e3dSEric Cheng for (i = sp->s_currfield; i < sp->s_nfields; i++) { 374*25ec3e3dSEric Cheng ptr += snprintf(ptr, lim - ptr, "%s,", sp->s_fields[i]); 375*25ec3e3dSEric Cheng if (i + 1 == sp->s_nfields) { 376*25ec3e3dSEric Cheng nextlen = 0; 377*25ec3e3dSEric Cheng if (ptr > value) 378*25ec3e3dSEric Cheng ptr[-1] = '\0'; 379*25ec3e3dSEric Cheng } else { 380*25ec3e3dSEric Cheng nextlen = strlen(sp->s_fields[i + 1]); 381*25ec3e3dSEric Cheng } 382*25ec3e3dSEric Cheng 383*25ec3e3dSEric Cheng if (strlen(value) + nextlen > width || ptr >= lim) { 384*25ec3e3dSEric Cheng i++; 385*25ec3e3dSEric Cheng break; 386*25ec3e3dSEric Cheng } 387*25ec3e3dSEric Cheng } 388*25ec3e3dSEric Cheng sp->s_currfield = i; 389*25ec3e3dSEric Cheng } 390*25ec3e3dSEric Cheng 391*25ec3e3dSEric Cheng /* 392*25ec3e3dSEric Cheng * Print one or more rows of output values for the selected columns. 3938002d411SSowmini Varadhan */ 3948002d411SSowmini Varadhan void 3958002d411SSowmini Varadhan ofmt_print(ofmt_handle_t ofmt, void *arg) 3968002d411SSowmini Varadhan { 3978002d411SSowmini Varadhan ofmt_state_t *os = ofmt; 3988002d411SSowmini Varadhan int i; 3998002d411SSowmini Varadhan char value[1024]; 4008002d411SSowmini Varadhan ofmt_field_t *of; 401*25ec3e3dSEric Cheng boolean_t escsep, more_rows; 4028002d411SSowmini Varadhan ofmt_arg_t ofarg; 403*25ec3e3dSEric Cheng split_t **sp = NULL; 404*25ec3e3dSEric Cheng 405*25ec3e3dSEric Cheng if (os->os_wrap) { 406*25ec3e3dSEric Cheng sp = calloc(sizeof (split_t *), os->os_nfields); 407*25ec3e3dSEric Cheng if (sp == NULL) 408*25ec3e3dSEric Cheng return; 409*25ec3e3dSEric Cheng } 4108002d411SSowmini Varadhan 4118002d411SSowmini Varadhan if ((os->os_nrow++ % os->os_winsize.ws_row) == 0 && !os->os_parsable) { 4128002d411SSowmini Varadhan ofmt_print_header(os); 4138002d411SSowmini Varadhan os->os_nrow++; 4148002d411SSowmini Varadhan } 4158002d411SSowmini Varadhan 4168002d411SSowmini Varadhan of = os->os_fields; 4178002d411SSowmini Varadhan escsep = (os->os_nfields > 1); 418*25ec3e3dSEric Cheng more_rows = B_FALSE; 4198002d411SSowmini Varadhan for (i = 0; i < os->os_nfields; i++) { 4208002d411SSowmini Varadhan os->os_lastfield = (i + 1 == os->os_nfields); 4218002d411SSowmini Varadhan value[0] = '\0'; 4228002d411SSowmini Varadhan ofarg.ofmt_id = of[i].of_id; 4238002d411SSowmini Varadhan ofarg.ofmt_cbarg = arg; 424*25ec3e3dSEric Cheng 425*25ec3e3dSEric Cheng if ((*of[i].of_cb)(&ofarg, value, sizeof (value))) { 426*25ec3e3dSEric Cheng if (os->os_wrap) { 427*25ec3e3dSEric Cheng /* 428*25ec3e3dSEric Cheng * 'value' will be split at comma boundaries 429*25ec3e3dSEric Cheng * and stored into sp[i]. 430*25ec3e3dSEric Cheng */ 431*25ec3e3dSEric Cheng ofmt_fit_width(&sp[i], of[i].of_width, value, 432*25ec3e3dSEric Cheng sizeof (value)); 433*25ec3e3dSEric Cheng if (sp[i] != NULL && 434*25ec3e3dSEric Cheng sp[i]->s_currfield < sp[i]->s_nfields) 435*25ec3e3dSEric Cheng more_rows = B_TRUE; 436*25ec3e3dSEric Cheng } 437*25ec3e3dSEric Cheng ofmt_print_field(os, &of[i], 438*25ec3e3dSEric Cheng (*value == '\0' && !os->os_parsable) ? 439*25ec3e3dSEric Cheng OFMT_VAL_UNDEF : value, escsep); 440*25ec3e3dSEric Cheng } else { 4418002d411SSowmini Varadhan ofmt_print_field(os, &of[i], OFMT_VAL_UNKNOWN, escsep); 4428002d411SSowmini Varadhan } 443*25ec3e3dSEric Cheng } 4448002d411SSowmini Varadhan (void) putchar('\n'); 445*25ec3e3dSEric Cheng 446*25ec3e3dSEric Cheng while (more_rows) { 447*25ec3e3dSEric Cheng more_rows = B_FALSE; 448*25ec3e3dSEric Cheng for (i = 0; i < os->os_nfields; i++) { 449*25ec3e3dSEric Cheng os->os_lastfield = (i + 1 == os->os_nfields); 450*25ec3e3dSEric Cheng value[0] = '\0'; 451*25ec3e3dSEric Cheng 452*25ec3e3dSEric Cheng ofmt_fit_width(&sp[i], of[i].of_width, 453*25ec3e3dSEric Cheng value, sizeof (value)); 454*25ec3e3dSEric Cheng if (sp[i] != NULL && 455*25ec3e3dSEric Cheng sp[i]->s_currfield < sp[i]->s_nfields) 456*25ec3e3dSEric Cheng more_rows = B_TRUE; 457*25ec3e3dSEric Cheng 458*25ec3e3dSEric Cheng ofmt_print_field(os, &of[i], value, escsep); 459*25ec3e3dSEric Cheng } 460*25ec3e3dSEric Cheng (void) putchar('\n'); 461*25ec3e3dSEric Cheng } 4628002d411SSowmini Varadhan (void) fflush(stdout); 463*25ec3e3dSEric Cheng 464*25ec3e3dSEric Cheng if (sp != NULL) { 465*25ec3e3dSEric Cheng for (i = 0; i < os->os_nfields; i++) 466*25ec3e3dSEric Cheng splitfree(sp[i]); 467*25ec3e3dSEric Cheng free(sp); 468*25ec3e3dSEric Cheng } 4698002d411SSowmini Varadhan } 4708002d411SSowmini Varadhan 4718002d411SSowmini Varadhan /* 4728002d411SSowmini Varadhan * Print the field headers 4738002d411SSowmini Varadhan */ 4748002d411SSowmini Varadhan static void 4758002d411SSowmini Varadhan ofmt_print_header(ofmt_state_t *os) 4768002d411SSowmini Varadhan { 4778002d411SSowmini Varadhan int i; 4788002d411SSowmini Varadhan ofmt_field_t *of = os->os_fields; 4798002d411SSowmini Varadhan boolean_t escsep = (os->os_nfields > 1); 4808002d411SSowmini Varadhan 4818002d411SSowmini Varadhan for (i = 0; i < os->os_nfields; i++) { 4828002d411SSowmini Varadhan os->os_lastfield = (i + 1 == os->os_nfields); 4838002d411SSowmini Varadhan ofmt_print_field(os, &of[i], of[i].of_name, escsep); 4848002d411SSowmini Varadhan } 4858002d411SSowmini Varadhan (void) putchar('\n'); 4868002d411SSowmini Varadhan } 4878002d411SSowmini Varadhan 4888002d411SSowmini Varadhan /* 4898002d411SSowmini Varadhan * Update the current window size. 4908002d411SSowmini Varadhan */ 4918002d411SSowmini Varadhan void 4928002d411SSowmini Varadhan ofmt_update_winsize(ofmt_handle_t ofmt) 4938002d411SSowmini Varadhan { 4948002d411SSowmini Varadhan ofmt_state_t *os = ofmt; 4958002d411SSowmini Varadhan struct winsize *winsize = &os->os_winsize; 4968002d411SSowmini Varadhan 4978002d411SSowmini Varadhan if (ioctl(1, TIOCGWINSZ, winsize) == -1 || 4988002d411SSowmini Varadhan winsize->ws_col == 0 || winsize->ws_row == 0) { 4998002d411SSowmini Varadhan winsize->ws_col = 80; 5008002d411SSowmini Varadhan winsize->ws_row = 24; 5018002d411SSowmini Varadhan } 5028002d411SSowmini Varadhan } 5038002d411SSowmini Varadhan 5048002d411SSowmini Varadhan /* 5058002d411SSowmini Varadhan * Return error diagnostics using the information in the ofmt_handle_t 5068002d411SSowmini Varadhan */ 5078002d411SSowmini Varadhan char * 5088002d411SSowmini Varadhan ofmt_strerror(ofmt_handle_t ofmt, ofmt_status_t err, char *buf, uint_t bufsize) 5098002d411SSowmini Varadhan { 5108002d411SSowmini Varadhan ofmt_state_t *os = ofmt; 5118002d411SSowmini Varadhan int i; 5128002d411SSowmini Varadhan const char *s; 5138002d411SSowmini Varadhan char ebuf[OFMT_BUFSIZE]; 5148002d411SSowmini Varadhan 5158002d411SSowmini Varadhan /* 5168002d411SSowmini Varadhan * ebuf is intended for optional error-specific data to be appended 5178002d411SSowmini Varadhan * after the internationalized error string for an error code. 5188002d411SSowmini Varadhan */ 5198002d411SSowmini Varadhan ebuf[0] = '\0'; 5208002d411SSowmini Varadhan 5218002d411SSowmini Varadhan switch (err) { 5228002d411SSowmini Varadhan case OFMT_SUCCESS: 5238002d411SSowmini Varadhan s = "success"; 5248002d411SSowmini Varadhan break; 5258002d411SSowmini Varadhan case OFMT_EBADFIELDS: 5268002d411SSowmini Varadhan /* 5278002d411SSowmini Varadhan * Enumerate the singular/plural version of the warning 5288002d411SSowmini Varadhan * and error to simplify and improve localization. 5298002d411SSowmini Varadhan */ 5308002d411SSowmini Varadhan if (!os->os_parsable) { 5318002d411SSowmini Varadhan if (os->os_nbad > 1) 5328002d411SSowmini Varadhan s = "ignoring unknown output fields:"; 5338002d411SSowmini Varadhan else 5348002d411SSowmini Varadhan s = "ignoring unknown output field:"; 5358002d411SSowmini Varadhan } else { 5368002d411SSowmini Varadhan if (os->os_nbad > 1) 5378002d411SSowmini Varadhan s = "unknown output fields:"; 5388002d411SSowmini Varadhan else 5398002d411SSowmini Varadhan s = "unknown output field:"; 5408002d411SSowmini Varadhan } 5418002d411SSowmini Varadhan /* set up the bad fields in ebuf */ 5428002d411SSowmini Varadhan for (i = 0; i < os->os_nbad; i++) { 5438002d411SSowmini Varadhan (void) strlcat(ebuf, " `", sizeof (ebuf)); 5448002d411SSowmini Varadhan (void) strlcat(ebuf, os->os_badfields[i], 5458002d411SSowmini Varadhan sizeof (ebuf)); 5468002d411SSowmini Varadhan (void) strlcat(ebuf, "'", sizeof (ebuf)); 5478002d411SSowmini Varadhan } 5488002d411SSowmini Varadhan break; 5498002d411SSowmini Varadhan case OFMT_ENOFIELDS: 5508002d411SSowmini Varadhan s = "no valid output fields"; 5518002d411SSowmini Varadhan break; 5528002d411SSowmini Varadhan case OFMT_EPARSEALL: 5538002d411SSowmini Varadhan s = "output field `all' invalid in parsable mode"; 5548002d411SSowmini Varadhan break; 5558002d411SSowmini Varadhan case OFMT_EPARSENONE: 5568002d411SSowmini Varadhan s = "output fields must be specified in parsable mode"; 5578002d411SSowmini Varadhan break; 558*25ec3e3dSEric Cheng case OFMT_EPARSEWRAP: 559*25ec3e3dSEric Cheng s = "parsable mode is incompatible with wrap mode"; 560*25ec3e3dSEric Cheng break; 5618002d411SSowmini Varadhan case OFMT_ENOTEMPLATE: 5628002d411SSowmini Varadhan s = "no template provided for fields"; 5638002d411SSowmini Varadhan break; 5648002d411SSowmini Varadhan case OFMT_ENOMEM: 5658002d411SSowmini Varadhan s = strerror(ENOMEM); 5668002d411SSowmini Varadhan break; 5678002d411SSowmini Varadhan default: 5688002d411SSowmini Varadhan (void) snprintf(buf, bufsize, 5698002d411SSowmini Varadhan dgettext(TEXT_DOMAIN, "unknown ofmt error (%d)"), 5708002d411SSowmini Varadhan err); 5718002d411SSowmini Varadhan return (buf); 5728002d411SSowmini Varadhan } 5738002d411SSowmini Varadhan (void) snprintf(buf, bufsize, dgettext(TEXT_DOMAIN, s)); 5748002d411SSowmini Varadhan (void) strlcat(buf, ebuf, bufsize); 5758002d411SSowmini Varadhan return (buf); 5768002d411SSowmini Varadhan } 577