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 */ 468002d411SSowmini Varadhan } split_t; 478002d411SSowmini Varadhan static void splitfree(split_t *); 488002d411SSowmini Varadhan static split_t *split_str(const char *, uint_t); 498002d411SSowmini Varadhan static split_t *split_fields(const ofmt_field_t *, uint_t, uint_t); 508002d411SSowmini Varadhan 518002d411SSowmini Varadhan /* 528002d411SSowmini Varadhan * The state of the output is tracked in a ofmt_state_t structure. 538002d411SSowmini Varadhan * Each os_fields[i] entry points at an ofmt_field_t array for 548002d411SSowmini Varadhan * the sub-command whose contents are provided by the caller, with 558002d411SSowmini Varadhan * os_nfields set to the number of requested fields. 568002d411SSowmini Varadhan */ 578002d411SSowmini Varadhan typedef struct ofmt_state_s { 588002d411SSowmini Varadhan ofmt_field_t *os_fields; 598002d411SSowmini Varadhan uint_t os_nfields; 608002d411SSowmini Varadhan boolean_t os_lastfield; 618002d411SSowmini Varadhan uint_t os_overflow; 628002d411SSowmini Varadhan struct winsize os_winsize; 638002d411SSowmini Varadhan int os_nrow; 648002d411SSowmini Varadhan boolean_t os_parsable; 658002d411SSowmini Varadhan int os_nbad; 668002d411SSowmini Varadhan char **os_badfields; 678002d411SSowmini Varadhan } ofmt_state_t; 688002d411SSowmini Varadhan /* 698002d411SSowmini Varadhan * A B_TRUE return value from the callback function will print out the contents 708002d411SSowmini Varadhan * of the output buffer, except when the buffer is returned with the empty 718002d411SSowmini Varadhan * string "", in which case the OFMT_VAL_UNDEF will be printed. 728002d411SSowmini Varadhan * 738002d411SSowmini Varadhan * If the callback function returns B_FALSE, the "?" string will be emitted. 748002d411SSowmini Varadhan */ 758002d411SSowmini Varadhan #define OFMT_VAL_UNDEF "--" 768002d411SSowmini Varadhan #define OFMT_VAL_UNKNOWN "?" 778002d411SSowmini Varadhan static void ofmt_print_header(ofmt_state_t *); 788002d411SSowmini Varadhan static void ofmt_print_field(ofmt_state_t *, ofmt_field_t *, const char *, 798002d411SSowmini Varadhan boolean_t); 808002d411SSowmini Varadhan 818002d411SSowmini Varadhan /* 828002d411SSowmini Varadhan * Split `str' into at most `maxfields' fields, Return a pointer to a 838002d411SSowmini Varadhan * split_t containing the split fields, or NULL on failure. 848002d411SSowmini Varadhan */ 858002d411SSowmini Varadhan static split_t * 868002d411SSowmini Varadhan split_str(const char *str, uint_t maxfields) 878002d411SSowmini Varadhan { 888002d411SSowmini Varadhan char *field, *token, *lasts = NULL; 898002d411SSowmini Varadhan split_t *sp; 908002d411SSowmini Varadhan 918002d411SSowmini Varadhan if (*str == '\0' || maxfields == 0) 928002d411SSowmini Varadhan return (NULL); 938002d411SSowmini Varadhan 948002d411SSowmini Varadhan sp = calloc(sizeof (split_t), 1); 958002d411SSowmini Varadhan if (sp == NULL) 968002d411SSowmini Varadhan return (NULL); 978002d411SSowmini Varadhan 988002d411SSowmini Varadhan sp->s_buf = strdup(str); 998002d411SSowmini Varadhan sp->s_fields = malloc(sizeof (char *) * maxfields); 1008002d411SSowmini Varadhan if (sp->s_buf == NULL || sp->s_fields == NULL) 1018002d411SSowmini Varadhan goto fail; 1028002d411SSowmini Varadhan 1038002d411SSowmini Varadhan token = sp->s_buf; 1048002d411SSowmini Varadhan while ((field = strtok_r(token, ",", &lasts)) != NULL) { 1058002d411SSowmini Varadhan if (sp->s_nfields == maxfields) 1068002d411SSowmini Varadhan goto fail; 1078002d411SSowmini Varadhan token = NULL; 1088002d411SSowmini Varadhan sp->s_fields[sp->s_nfields++] = field; 1098002d411SSowmini Varadhan } 1108002d411SSowmini Varadhan return (sp); 1118002d411SSowmini Varadhan fail: 1128002d411SSowmini Varadhan splitfree(sp); 1138002d411SSowmini Varadhan return (NULL); 1148002d411SSowmini Varadhan } 1158002d411SSowmini Varadhan 1168002d411SSowmini Varadhan /* 1178002d411SSowmini Varadhan * Split `fields' into at most `maxfields' fields. Return a pointer to 1188002d411SSowmini Varadhan * a split_t containing the split fields, or NULL on failure. Invoked 1198002d411SSowmini Varadhan * when all fields are implicitly selected at handle creation by 1208002d411SSowmini Varadhan * passing in a NULL fields_str 1218002d411SSowmini Varadhan */ 1228002d411SSowmini Varadhan static split_t * 1238002d411SSowmini Varadhan split_fields(const ofmt_field_t *template, uint_t maxfields, uint_t maxcols) 1248002d411SSowmini Varadhan { 1258002d411SSowmini Varadhan split_t *sp; 1268002d411SSowmini Varadhan int i, cols; 1278002d411SSowmini Varadhan 1288002d411SSowmini Varadhan sp = calloc(sizeof (split_t), 1); 1298002d411SSowmini Varadhan if (sp == NULL) 1308002d411SSowmini Varadhan return (NULL); 1318002d411SSowmini Varadhan 1328002d411SSowmini Varadhan sp->s_fields = malloc(sizeof (char *) * maxfields); 1338002d411SSowmini Varadhan if (sp->s_fields == NULL) 1348002d411SSowmini Varadhan goto fail; 1358002d411SSowmini Varadhan cols = 0; 1368002d411SSowmini Varadhan for (i = 0; i < maxfields; i++) { 1378002d411SSowmini Varadhan cols += template[i].of_width; 1388002d411SSowmini Varadhan /* 1398002d411SSowmini Varadhan * If all fields are implied without explicitly passing 1408002d411SSowmini Varadhan * in a fields_str, build a list of field names, stopping 1418002d411SSowmini Varadhan * when we run out of columns. 1428002d411SSowmini Varadhan */ 1438002d411SSowmini Varadhan if (maxcols > 0 && cols > maxcols) 1448002d411SSowmini Varadhan break; 1458002d411SSowmini Varadhan sp->s_fields[sp->s_nfields++] = template[i].of_name; 1468002d411SSowmini Varadhan } 1478002d411SSowmini Varadhan return (sp); 1488002d411SSowmini Varadhan fail: 1498002d411SSowmini Varadhan splitfree(sp); 1508002d411SSowmini Varadhan return (NULL); 1518002d411SSowmini Varadhan } 1528002d411SSowmini Varadhan 1538002d411SSowmini Varadhan /* 1548002d411SSowmini Varadhan * Free the split_t structure pointed to by `sp'. 1558002d411SSowmini Varadhan */ 1568002d411SSowmini Varadhan static void 1578002d411SSowmini Varadhan splitfree(split_t *sp) 1588002d411SSowmini Varadhan { 1598002d411SSowmini Varadhan if (sp == NULL) 1608002d411SSowmini Varadhan return; 1618002d411SSowmini Varadhan free(sp->s_buf); 1628002d411SSowmini Varadhan free(sp->s_fields); 1638002d411SSowmini Varadhan free(sp); 1648002d411SSowmini Varadhan } 1658002d411SSowmini Varadhan 1668002d411SSowmini Varadhan /* 1678002d411SSowmini Varadhan * Open a handle to be used for printing formatted output. 1688002d411SSowmini Varadhan */ 1698002d411SSowmini Varadhan ofmt_status_t 170*2b24ab6bSSebastien Roy ofmt_open(const char *str, const ofmt_field_t *template, uint_t flags, 1718002d411SSowmini Varadhan uint_t maxcols, ofmt_handle_t *ofmt) 1728002d411SSowmini Varadhan { 1738002d411SSowmini Varadhan split_t *sp; 1748002d411SSowmini Varadhan uint_t i, j, of_index; 175*2b24ab6bSSebastien Roy const ofmt_field_t *ofp; 1768002d411SSowmini Varadhan ofmt_field_t *of; 1778002d411SSowmini Varadhan ofmt_state_t *os; 1788002d411SSowmini Varadhan int nfields = 0; 1798002d411SSowmini Varadhan ofmt_status_t err = OFMT_SUCCESS; 1808002d411SSowmini Varadhan boolean_t parsable = (flags & OFMT_PARSABLE); 1818002d411SSowmini Varadhan 1828002d411SSowmini Varadhan *ofmt = NULL; 1838002d411SSowmini Varadhan if (parsable) { 1848002d411SSowmini Varadhan /* 1858002d411SSowmini Varadhan * For parsable output mode, the caller always needs 1868002d411SSowmini Varadhan * to specify precisely which fields are to be selected, 1878002d411SSowmini Varadhan * since the set of fields may change over time. 1888002d411SSowmini Varadhan */ 1898002d411SSowmini Varadhan if (str == NULL || str[0] == '\0') 1908002d411SSowmini Varadhan return (OFMT_EPARSENONE); 1918002d411SSowmini Varadhan if (strcmp(str, "all") == 0) 1928002d411SSowmini Varadhan return (OFMT_EPARSEALL); 1938002d411SSowmini Varadhan } 1948002d411SSowmini Varadhan if (template == NULL) 1958002d411SSowmini Varadhan return (OFMT_ENOTEMPLATE); 196*2b24ab6bSSebastien Roy for (ofp = template; ofp->of_name != NULL; ofp++) 1978002d411SSowmini Varadhan nfields++; 1988002d411SSowmini Varadhan /* 1998002d411SSowmini Varadhan * split str into the columns selected, or construct the 2008002d411SSowmini Varadhan * full set of columns (equivalent to -o all). 2018002d411SSowmini Varadhan */ 2028002d411SSowmini Varadhan if (str != NULL && strcmp(str, "all") != 0) { 2038002d411SSowmini Varadhan sp = split_str(str, nfields); 2048002d411SSowmini Varadhan } else { 2058002d411SSowmini Varadhan if (parsable || (str != NULL && strcmp(str, "all") == 0)) 2068002d411SSowmini Varadhan maxcols = 0; 2078002d411SSowmini Varadhan sp = split_fields(template, nfields, maxcols); 2088002d411SSowmini Varadhan } 2098002d411SSowmini Varadhan if (sp == NULL) 2108002d411SSowmini Varadhan goto nomem; 2118002d411SSowmini Varadhan 2128002d411SSowmini Varadhan os = calloc(sizeof (ofmt_state_t) + 2138002d411SSowmini Varadhan sp->s_nfields * sizeof (ofmt_field_t), 1); 2148002d411SSowmini Varadhan if (os == NULL) 2158002d411SSowmini Varadhan goto nomem; 2168002d411SSowmini Varadhan *ofmt = os; 2178002d411SSowmini Varadhan os->os_fields = (ofmt_field_t *)&os[1]; 2188002d411SSowmini Varadhan os->os_parsable = parsable; 2198002d411SSowmini Varadhan of = os->os_fields; 2208002d411SSowmini Varadhan of_index = 0; 2218002d411SSowmini Varadhan /* 2228002d411SSowmini Varadhan * sp->s_nfields is the number of fields requested in fields_str. 2238002d411SSowmini Varadhan * nfields is the number of fields in template. 2248002d411SSowmini Varadhan */ 2258002d411SSowmini Varadhan for (i = 0; i < sp->s_nfields; i++) { 2268002d411SSowmini Varadhan for (j = 0; j < nfields; j++) { 2278002d411SSowmini Varadhan if (strcasecmp(sp->s_fields[i], 2288002d411SSowmini Varadhan template[j].of_name) == 0) { 2298002d411SSowmini Varadhan break; 2308002d411SSowmini Varadhan } 2318002d411SSowmini Varadhan } 2328002d411SSowmini Varadhan if (j == nfields) { 2338002d411SSowmini Varadhan int nbad = os->os_nbad++; 2348002d411SSowmini Varadhan 2358002d411SSowmini Varadhan err = OFMT_EBADFIELDS; 2368002d411SSowmini Varadhan if (os->os_badfields == NULL) { 2378002d411SSowmini Varadhan os->os_badfields = malloc(sp->s_nfields * 2388002d411SSowmini Varadhan sizeof (char *)); 2398002d411SSowmini Varadhan if (os->os_badfields == NULL) 2408002d411SSowmini Varadhan goto nomem; 2418002d411SSowmini Varadhan } 2428002d411SSowmini Varadhan os->os_badfields[nbad] = strdup(sp->s_fields[i]); 2438002d411SSowmini Varadhan if (os->os_badfields[nbad] == NULL) 2448002d411SSowmini Varadhan goto nomem; 2458002d411SSowmini Varadhan continue; 2468002d411SSowmini Varadhan } 2478002d411SSowmini Varadhan of[of_index].of_name = strdup(template[j].of_name); 2488002d411SSowmini Varadhan if (of[of_index].of_name == NULL) 2498002d411SSowmini Varadhan goto nomem; 2508002d411SSowmini Varadhan of[of_index].of_width = template[j].of_width; 2518002d411SSowmini Varadhan of[of_index].of_id = template[j].of_id; 2528002d411SSowmini Varadhan of[of_index].of_cb = template[j].of_cb; 2538002d411SSowmini Varadhan of_index++; 2548002d411SSowmini Varadhan } 2558002d411SSowmini Varadhan splitfree(sp); 2568002d411SSowmini Varadhan if (of_index == 0) /* all values in str are bogus */ 2578002d411SSowmini Varadhan return (OFMT_ENOFIELDS); 2588002d411SSowmini Varadhan os->os_nfields = of_index; /* actual number of fields printed */ 2598002d411SSowmini Varadhan ofmt_update_winsize(*ofmt); 2608002d411SSowmini Varadhan return (err); 2618002d411SSowmini Varadhan nomem: 2628002d411SSowmini Varadhan err = OFMT_ENOMEM; 2638002d411SSowmini Varadhan if (os != NULL) 2648002d411SSowmini Varadhan ofmt_close(os); 2658002d411SSowmini Varadhan *ofmt = NULL; 2668002d411SSowmini Varadhan splitfree(sp); 2678002d411SSowmini Varadhan return (err); 2688002d411SSowmini Varadhan } 2698002d411SSowmini Varadhan 2708002d411SSowmini Varadhan /* 2718002d411SSowmini Varadhan * free resources associated with the ofmt_handle_t 2728002d411SSowmini Varadhan */ 2738002d411SSowmini Varadhan void 2748002d411SSowmini Varadhan ofmt_close(ofmt_handle_t ofmt) 2758002d411SSowmini Varadhan { 2768002d411SSowmini Varadhan ofmt_state_t *os = ofmt; 2778002d411SSowmini Varadhan int i; 2788002d411SSowmini Varadhan 2798002d411SSowmini Varadhan if (os == NULL) 2808002d411SSowmini Varadhan return; 2818002d411SSowmini Varadhan for (i = 0; i < os->os_nfields; i++) 2828002d411SSowmini Varadhan free(os->os_fields[i].of_name); 2838002d411SSowmini Varadhan for (i = 0; i < os->os_nbad; i++) 2848002d411SSowmini Varadhan free(os->os_badfields[i]); 2858002d411SSowmini Varadhan free(os->os_badfields); 2868002d411SSowmini Varadhan free(os); 2878002d411SSowmini Varadhan } 2888002d411SSowmini Varadhan 2898002d411SSowmini Varadhan /* 2908002d411SSowmini Varadhan * Print the value for the selected field by calling the callback-function 2918002d411SSowmini Varadhan * registered for the field. 2928002d411SSowmini Varadhan */ 2938002d411SSowmini Varadhan static void 2948002d411SSowmini Varadhan ofmt_print_field(ofmt_state_t *os, ofmt_field_t *ofp, const char *value, 2958002d411SSowmini Varadhan boolean_t escsep) 2968002d411SSowmini Varadhan { 2978002d411SSowmini Varadhan uint_t width = ofp->of_width; 2988002d411SSowmini Varadhan uint_t valwidth; 2998002d411SSowmini Varadhan uint_t compress; 3008002d411SSowmini Varadhan boolean_t parsable = os->os_parsable; 3018002d411SSowmini Varadhan char c; 3028002d411SSowmini Varadhan 3038002d411SSowmini Varadhan /* 3048002d411SSowmini Varadhan * Parsable fields are separated by ':'. If such a field contains 3058002d411SSowmini Varadhan * a ':' or '\', this character is prefixed by a '\'. 3068002d411SSowmini Varadhan */ 3078002d411SSowmini Varadhan if (parsable) { 3088002d411SSowmini Varadhan if (os->os_nfields == 1) { 3098002d411SSowmini Varadhan (void) printf("%s", value); 3108002d411SSowmini Varadhan return; 3118002d411SSowmini Varadhan } 3128002d411SSowmini Varadhan while ((c = *value++) != '\0') { 3138002d411SSowmini Varadhan if (escsep && ((c == ':' || c == '\\'))) 3148002d411SSowmini Varadhan (void) putchar('\\'); 3158002d411SSowmini Varadhan (void) putchar(c); 3168002d411SSowmini Varadhan } 3178002d411SSowmini Varadhan if (!os->os_lastfield) 3188002d411SSowmini Varadhan (void) putchar(':'); 3198002d411SSowmini Varadhan return; 3208002d411SSowmini Varadhan } else { 3218002d411SSowmini Varadhan if (value[0] == '\0') 3228002d411SSowmini Varadhan value = OFMT_VAL_UNDEF; 3238002d411SSowmini Varadhan if (os->os_lastfield) { 3248002d411SSowmini Varadhan (void) printf("%s", value); 3258002d411SSowmini Varadhan os->os_overflow = 0; 3268002d411SSowmini Varadhan return; 3278002d411SSowmini Varadhan } 3288002d411SSowmini Varadhan 3298002d411SSowmini Varadhan valwidth = strlen(value); 3308002d411SSowmini Varadhan if (valwidth + os->os_overflow >= width) { 3318002d411SSowmini Varadhan os->os_overflow += valwidth - width + 1; 3328002d411SSowmini Varadhan (void) printf("%s ", value); 3338002d411SSowmini Varadhan return; 3348002d411SSowmini Varadhan } 3358002d411SSowmini Varadhan 3368002d411SSowmini Varadhan if (os->os_overflow > 0) { 3378002d411SSowmini Varadhan compress = MIN(os->os_overflow, width - valwidth); 3388002d411SSowmini Varadhan os->os_overflow -= compress; 3398002d411SSowmini Varadhan width -= compress; 3408002d411SSowmini Varadhan } 3418002d411SSowmini Varadhan (void) printf("%-*s", width, value); 3428002d411SSowmini Varadhan } 3438002d411SSowmini Varadhan } 3448002d411SSowmini Varadhan 3458002d411SSowmini Varadhan /* 3468002d411SSowmini Varadhan * print one row of output values for the selected columns. 3478002d411SSowmini Varadhan */ 3488002d411SSowmini Varadhan void 3498002d411SSowmini Varadhan ofmt_print(ofmt_handle_t ofmt, void *arg) 3508002d411SSowmini Varadhan { 3518002d411SSowmini Varadhan ofmt_state_t *os = ofmt; 3528002d411SSowmini Varadhan int i; 3538002d411SSowmini Varadhan char value[1024]; 3548002d411SSowmini Varadhan ofmt_field_t *of; 3558002d411SSowmini Varadhan boolean_t escsep; 3568002d411SSowmini Varadhan ofmt_arg_t ofarg; 3578002d411SSowmini Varadhan 3588002d411SSowmini Varadhan if ((os->os_nrow++ % os->os_winsize.ws_row) == 0 && !os->os_parsable) { 3598002d411SSowmini Varadhan ofmt_print_header(os); 3608002d411SSowmini Varadhan os->os_nrow++; 3618002d411SSowmini Varadhan } 3628002d411SSowmini Varadhan 3638002d411SSowmini Varadhan of = os->os_fields; 3648002d411SSowmini Varadhan escsep = (os->os_nfields > 1); 3658002d411SSowmini Varadhan for (i = 0; i < os->os_nfields; i++) { 3668002d411SSowmini Varadhan os->os_lastfield = (i + 1 == os->os_nfields); 3678002d411SSowmini Varadhan value[0] = '\0'; 3688002d411SSowmini Varadhan ofarg.ofmt_id = of[i].of_id; 3698002d411SSowmini Varadhan ofarg.ofmt_cbarg = arg; 3708002d411SSowmini Varadhan if ((*of[i].of_cb)(&ofarg, value, sizeof (value))) 3718002d411SSowmini Varadhan ofmt_print_field(os, &of[i], value, escsep); 3728002d411SSowmini Varadhan else 3738002d411SSowmini Varadhan ofmt_print_field(os, &of[i], OFMT_VAL_UNKNOWN, escsep); 3748002d411SSowmini Varadhan } 3758002d411SSowmini Varadhan (void) putchar('\n'); 3768002d411SSowmini Varadhan (void) fflush(stdout); 3778002d411SSowmini Varadhan } 3788002d411SSowmini Varadhan 3798002d411SSowmini Varadhan /* 3808002d411SSowmini Varadhan * Print the field headers 3818002d411SSowmini Varadhan */ 3828002d411SSowmini Varadhan static void 3838002d411SSowmini Varadhan ofmt_print_header(ofmt_state_t *os) 3848002d411SSowmini Varadhan { 3858002d411SSowmini Varadhan int i; 3868002d411SSowmini Varadhan ofmt_field_t *of = os->os_fields; 3878002d411SSowmini Varadhan boolean_t escsep = (os->os_nfields > 1); 3888002d411SSowmini Varadhan 3898002d411SSowmini Varadhan for (i = 0; i < os->os_nfields; i++) { 3908002d411SSowmini Varadhan os->os_lastfield = (i + 1 == os->os_nfields); 3918002d411SSowmini Varadhan ofmt_print_field(os, &of[i], of[i].of_name, escsep); 3928002d411SSowmini Varadhan } 3938002d411SSowmini Varadhan (void) putchar('\n'); 3948002d411SSowmini Varadhan } 3958002d411SSowmini Varadhan 3968002d411SSowmini Varadhan /* 3978002d411SSowmini Varadhan * Update the current window size. 3988002d411SSowmini Varadhan */ 3998002d411SSowmini Varadhan void 4008002d411SSowmini Varadhan ofmt_update_winsize(ofmt_handle_t ofmt) 4018002d411SSowmini Varadhan { 4028002d411SSowmini Varadhan ofmt_state_t *os = ofmt; 4038002d411SSowmini Varadhan struct winsize *winsize = &os->os_winsize; 4048002d411SSowmini Varadhan 4058002d411SSowmini Varadhan if (ioctl(1, TIOCGWINSZ, winsize) == -1 || 4068002d411SSowmini Varadhan winsize->ws_col == 0 || winsize->ws_row == 0) { 4078002d411SSowmini Varadhan winsize->ws_col = 80; 4088002d411SSowmini Varadhan winsize->ws_row = 24; 4098002d411SSowmini Varadhan } 4108002d411SSowmini Varadhan } 4118002d411SSowmini Varadhan 4128002d411SSowmini Varadhan /* 4138002d411SSowmini Varadhan * Return error diagnostics using the information in the ofmt_handle_t 4148002d411SSowmini Varadhan */ 4158002d411SSowmini Varadhan char * 4168002d411SSowmini Varadhan ofmt_strerror(ofmt_handle_t ofmt, ofmt_status_t err, char *buf, uint_t bufsize) 4178002d411SSowmini Varadhan { 4188002d411SSowmini Varadhan ofmt_state_t *os = ofmt; 4198002d411SSowmini Varadhan int i; 4208002d411SSowmini Varadhan const char *s; 4218002d411SSowmini Varadhan char ebuf[OFMT_BUFSIZE]; 4228002d411SSowmini Varadhan 4238002d411SSowmini Varadhan /* 4248002d411SSowmini Varadhan * ebuf is intended for optional error-specific data to be appended 4258002d411SSowmini Varadhan * after the internationalized error string for an error code. 4268002d411SSowmini Varadhan */ 4278002d411SSowmini Varadhan ebuf[0] = '\0'; 4288002d411SSowmini Varadhan 4298002d411SSowmini Varadhan switch (err) { 4308002d411SSowmini Varadhan case OFMT_SUCCESS: 4318002d411SSowmini Varadhan s = "success"; 4328002d411SSowmini Varadhan break; 4338002d411SSowmini Varadhan case OFMT_EBADFIELDS: 4348002d411SSowmini Varadhan /* 4358002d411SSowmini Varadhan * Enumerate the singular/plural version of the warning 4368002d411SSowmini Varadhan * and error to simplify and improve localization. 4378002d411SSowmini Varadhan */ 4388002d411SSowmini Varadhan if (!os->os_parsable) { 4398002d411SSowmini Varadhan if (os->os_nbad > 1) 4408002d411SSowmini Varadhan s = "ignoring unknown output fields:"; 4418002d411SSowmini Varadhan else 4428002d411SSowmini Varadhan s = "ignoring unknown output field:"; 4438002d411SSowmini Varadhan } else { 4448002d411SSowmini Varadhan if (os->os_nbad > 1) 4458002d411SSowmini Varadhan s = "unknown output fields:"; 4468002d411SSowmini Varadhan else 4478002d411SSowmini Varadhan s = "unknown output field:"; 4488002d411SSowmini Varadhan } 4498002d411SSowmini Varadhan /* set up the bad fields in ebuf */ 4508002d411SSowmini Varadhan for (i = 0; i < os->os_nbad; i++) { 4518002d411SSowmini Varadhan (void) strlcat(ebuf, " `", sizeof (ebuf)); 4528002d411SSowmini Varadhan (void) strlcat(ebuf, os->os_badfields[i], 4538002d411SSowmini Varadhan sizeof (ebuf)); 4548002d411SSowmini Varadhan (void) strlcat(ebuf, "'", sizeof (ebuf)); 4558002d411SSowmini Varadhan } 4568002d411SSowmini Varadhan break; 4578002d411SSowmini Varadhan case OFMT_ENOFIELDS: 4588002d411SSowmini Varadhan s = "no valid output fields"; 4598002d411SSowmini Varadhan break; 4608002d411SSowmini Varadhan case OFMT_EPARSEALL: 4618002d411SSowmini Varadhan s = "output field `all' invalid in parsable mode"; 4628002d411SSowmini Varadhan break; 4638002d411SSowmini Varadhan case OFMT_EPARSENONE: 4648002d411SSowmini Varadhan s = "output fields must be specified in parsable mode"; 4658002d411SSowmini Varadhan break; 4668002d411SSowmini Varadhan case OFMT_ENOTEMPLATE: 4678002d411SSowmini Varadhan s = "no template provided for fields"; 4688002d411SSowmini Varadhan break; 4698002d411SSowmini Varadhan case OFMT_ENOMEM: 4708002d411SSowmini Varadhan s = strerror(ENOMEM); 4718002d411SSowmini Varadhan break; 4728002d411SSowmini Varadhan default: 4738002d411SSowmini Varadhan (void) snprintf(buf, bufsize, 4748002d411SSowmini Varadhan dgettext(TEXT_DOMAIN, "unknown ofmt error (%d)"), 4758002d411SSowmini Varadhan err); 4768002d411SSowmini Varadhan return (buf); 4778002d411SSowmini Varadhan } 4788002d411SSowmini Varadhan (void) snprintf(buf, bufsize, dgettext(TEXT_DOMAIN, s)); 4798002d411SSowmini Varadhan (void) strlcat(buf, ebuf, bufsize); 4808002d411SSowmini Varadhan return (buf); 4818002d411SSowmini Varadhan } 482