xref: /titanic_44/usr/src/lib/libinetutil/common/ofmt.c (revision 8f18d1fadf6a0c20fac9ff7259a5368faa3c3bfb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <stdio.h>
32 #include <ofmt.h>
33 #include <sys/termios.h>
34 #include <unistd.h>
35 #include <sys/sysmacros.h>
36 #include <libintl.h>
37 
38 /*
39  * functions and structures to internally process a comma-separated string
40  * of fields selected for output.
41  */
42 typedef struct {
43 	char	*s_buf;
44 	const char **s_fields;	/* array of pointers to the fields in s_buf */
45 	uint_t	s_nfields;	/* the number of fields in s_buf */
46 } split_t;
47 static void splitfree(split_t *);
48 static split_t *split_str(const char *, uint_t);
49 static split_t *split_fields(const ofmt_field_t *, uint_t, uint_t);
50 
51 /*
52  * The state of the output is tracked in a ofmt_state_t structure.
53  * Each os_fields[i] entry points at an ofmt_field_t array for
54  * the sub-command whose contents are provided by the caller, with
55  * os_nfields set to the number of requested fields.
56  */
57 typedef struct ofmt_state_s {
58 	ofmt_field_t  	*os_fields;
59 	uint_t		os_nfields;
60 	boolean_t	os_lastfield;
61 	uint_t		os_overflow;
62 	struct winsize	os_winsize;
63 	int		os_nrow;
64 	boolean_t	os_parsable;
65 	int		os_nbad;
66 	char		**os_badfields;
67 } ofmt_state_t;
68 /*
69  * A B_TRUE return value from the callback function will print out the contents
70  * of the output buffer, except when the buffer is returned with the empty
71  * string "", in which case the  OFMT_VAL_UNDEF will be printed.
72  *
73  * If the callback function returns B_FALSE, the "?" string will be emitted.
74  */
75 #define	OFMT_VAL_UNDEF		"--"
76 #define	OFMT_VAL_UNKNOWN	"?"
77 static void ofmt_print_header(ofmt_state_t *);
78 static void ofmt_print_field(ofmt_state_t *, ofmt_field_t *, const char *,
79     boolean_t);
80 
81 /*
82  * Split `str' into at most `maxfields' fields, Return a pointer to a
83  * split_t containing the split fields, or NULL on failure.
84  */
85 static split_t *
86 split_str(const char *str, uint_t maxfields)
87 {
88 	char	*field, *token, *lasts = NULL;
89 	split_t	*sp;
90 
91 	if (*str == '\0' || maxfields == 0)
92 		return (NULL);
93 
94 	sp = calloc(sizeof (split_t), 1);
95 	if (sp == NULL)
96 		return (NULL);
97 
98 	sp->s_buf = strdup(str);
99 	sp->s_fields = malloc(sizeof (char *) * maxfields);
100 	if (sp->s_buf == NULL || sp->s_fields == NULL)
101 		goto fail;
102 
103 	token = sp->s_buf;
104 	while ((field = strtok_r(token, ",", &lasts)) != NULL) {
105 		if (sp->s_nfields == maxfields)
106 			goto fail;
107 		token = NULL;
108 		sp->s_fields[sp->s_nfields++] = field;
109 	}
110 	return (sp);
111 fail:
112 	splitfree(sp);
113 	return (NULL);
114 }
115 
116 /*
117  * Split `fields' into at most `maxfields' fields. Return a pointer to
118  * a split_t containing the split fields, or NULL on failure. Invoked
119  * when all fields are implicitly selected at handle creation by
120  * passing in a NULL fields_str
121  */
122 static split_t *
123 split_fields(const ofmt_field_t *template, uint_t maxfields, uint_t maxcols)
124 {
125 	split_t	*sp;
126 	int i, cols;
127 
128 	sp = calloc(sizeof (split_t), 1);
129 	if (sp == NULL)
130 		return (NULL);
131 
132 	sp->s_fields = malloc(sizeof (char *) * maxfields);
133 	if (sp->s_fields == NULL)
134 		goto fail;
135 	cols = 0;
136 	for (i = 0; i < maxfields; i++) {
137 		cols += template[i].of_width;
138 		/*
139 		 * If all fields are implied without explicitly passing
140 		 * in a fields_str, build a list of field names, stopping
141 		 * when we run out of columns.
142 		 */
143 		if (maxcols > 0 && cols > maxcols)
144 			break;
145 		sp->s_fields[sp->s_nfields++] = template[i].of_name;
146 	}
147 	return (sp);
148 fail:
149 	splitfree(sp);
150 	return (NULL);
151 }
152 
153 /*
154  * Free the split_t structure pointed to by `sp'.
155  */
156 static void
157 splitfree(split_t *sp)
158 {
159 	if (sp == NULL)
160 		return;
161 	free(sp->s_buf);
162 	free(sp->s_fields);
163 	free(sp);
164 }
165 
166 /*
167  * Open a handle to be used for printing formatted output.
168  */
169 ofmt_status_t
170 ofmt_open(const char *str, const ofmt_field_t *template, uint_t flags,
171     uint_t maxcols, ofmt_handle_t *ofmt)
172 {
173 	split_t		*sp;
174 	uint_t		i, j, of_index;
175 	const ofmt_field_t *ofp;
176 	ofmt_field_t	*of;
177 	ofmt_state_t	*os;
178 	int		nfields = 0;
179 	ofmt_status_t	err = OFMT_SUCCESS;
180 	boolean_t	parsable = (flags & OFMT_PARSABLE);
181 
182 	*ofmt = NULL;
183 	if (parsable) {
184 		/*
185 		 * For parsable output mode, the caller always needs
186 		 * to specify precisely which fields are to be selected,
187 		 * since the set of fields may change over time.
188 		 */
189 		if (str == NULL || str[0] == '\0')
190 			return (OFMT_EPARSENONE);
191 		if (strcmp(str, "all") == 0)
192 			return (OFMT_EPARSEALL);
193 	}
194 	if (template == NULL)
195 		return (OFMT_ENOTEMPLATE);
196 	for (ofp = template; ofp->of_name != NULL; ofp++)
197 		nfields++;
198 	/*
199 	 * split str into the columns selected, or construct the
200 	 * full set of columns (equivalent to -o all).
201 	 */
202 	if (str != NULL && strcmp(str, "all") != 0) {
203 		sp = split_str(str, nfields);
204 	} else {
205 		if (parsable || (str != NULL && strcmp(str, "all") == 0))
206 			maxcols = 0;
207 		sp = split_fields(template, nfields, maxcols);
208 	}
209 	if (sp == NULL)
210 		goto nomem;
211 
212 	os = calloc(sizeof (ofmt_state_t) +
213 	    sp->s_nfields * sizeof (ofmt_field_t), 1);
214 	if (os == NULL)
215 		goto nomem;
216 	*ofmt = os;
217 	os->os_fields = (ofmt_field_t *)&os[1];
218 	os->os_parsable = parsable;
219 	of = os->os_fields;
220 	of_index = 0;
221 	/*
222 	 * sp->s_nfields is the number of fields requested in fields_str.
223 	 * nfields is the number of fields in template.
224 	 */
225 	for (i = 0; i < sp->s_nfields; i++) {
226 		for (j = 0; j < nfields; j++) {
227 			if (strcasecmp(sp->s_fields[i],
228 			    template[j].of_name) == 0) {
229 				break;
230 			}
231 		}
232 		if (j == nfields) {
233 			int nbad = os->os_nbad++;
234 
235 			err = OFMT_EBADFIELDS;
236 			if (os->os_badfields == NULL) {
237 				os->os_badfields = malloc(sp->s_nfields *
238 				    sizeof (char *));
239 				if (os->os_badfields == NULL)
240 					goto nomem;
241 			}
242 			os->os_badfields[nbad] = strdup(sp->s_fields[i]);
243 			if (os->os_badfields[nbad] == NULL)
244 				goto nomem;
245 			continue;
246 		}
247 		of[of_index].of_name = strdup(template[j].of_name);
248 		if (of[of_index].of_name == NULL)
249 			goto nomem;
250 		of[of_index].of_width = template[j].of_width;
251 		of[of_index].of_id = template[j].of_id;
252 		of[of_index].of_cb = template[j].of_cb;
253 		of_index++;
254 	}
255 	splitfree(sp);
256 	if (of_index == 0) /* all values in str are bogus */
257 		return (OFMT_ENOFIELDS);
258 	os->os_nfields = of_index; /* actual number of fields printed */
259 	ofmt_update_winsize(*ofmt);
260 	return (err);
261 nomem:
262 	err = OFMT_ENOMEM;
263 	if (os != NULL)
264 		ofmt_close(os);
265 	*ofmt = NULL;
266 	splitfree(sp);
267 	return (err);
268 }
269 
270 /*
271  * free resources associated with the ofmt_handle_t
272  */
273 void
274 ofmt_close(ofmt_handle_t ofmt)
275 {
276 	ofmt_state_t *os = ofmt;
277 	int i;
278 
279 	if (os == NULL)
280 		return;
281 	for (i = 0; i < os->os_nfields; i++)
282 		free(os->os_fields[i].of_name);
283 	for (i = 0; i < os->os_nbad; i++)
284 		free(os->os_badfields[i]);
285 	free(os->os_badfields);
286 	free(os);
287 }
288 
289 /*
290  * Print the value for the selected field by calling the callback-function
291  * registered for the field.
292  */
293 static void
294 ofmt_print_field(ofmt_state_t *os, ofmt_field_t *ofp, const char *value,
295     boolean_t escsep)
296 {
297 	uint_t	width = ofp->of_width;
298 	uint_t	valwidth;
299 	uint_t	compress;
300 	boolean_t parsable = os->os_parsable;
301 	char	c;
302 
303 	/*
304 	 * Parsable fields are separated by ':'. If such a field contains
305 	 * a ':' or '\', this character is prefixed by a '\'.
306 	 */
307 	if (parsable) {
308 		if (os->os_nfields == 1) {
309 			(void) printf("%s", value);
310 			return;
311 		}
312 		while ((c = *value++) != '\0') {
313 			if (escsep && ((c == ':' || c == '\\')))
314 				(void) putchar('\\');
315 			(void) putchar(c);
316 		}
317 		if (!os->os_lastfield)
318 			(void) putchar(':');
319 		return;
320 	} else {
321 		if (value[0] == '\0')
322 			value = OFMT_VAL_UNDEF;
323 		if (os->os_lastfield) {
324 			(void) printf("%s", value);
325 			os->os_overflow = 0;
326 			return;
327 		}
328 
329 		valwidth = strlen(value);
330 		if (valwidth + os->os_overflow >= width) {
331 			os->os_overflow += valwidth - width + 1;
332 			(void) printf("%s ", value);
333 			return;
334 		}
335 
336 		if (os->os_overflow > 0) {
337 			compress = MIN(os->os_overflow, width - valwidth);
338 			os->os_overflow -= compress;
339 			width -= compress;
340 		}
341 		(void) printf("%-*s", width, value);
342 	}
343 }
344 
345 /*
346  * print one row of output values for the selected columns.
347  */
348 void
349 ofmt_print(ofmt_handle_t ofmt, void *arg)
350 {
351 	ofmt_state_t *os = ofmt;
352 	int i;
353 	char value[1024];
354 	ofmt_field_t *of;
355 	boolean_t escsep;
356 	ofmt_arg_t ofarg;
357 
358 	if ((os->os_nrow++ % os->os_winsize.ws_row) == 0 && !os->os_parsable) {
359 		ofmt_print_header(os);
360 		os->os_nrow++;
361 	}
362 
363 	of = os->os_fields;
364 	escsep = (os->os_nfields > 1);
365 	for (i = 0; i < os->os_nfields; i++) {
366 		os->os_lastfield = (i + 1 == os->os_nfields);
367 		value[0] = '\0';
368 		ofarg.ofmt_id = of[i].of_id;
369 		ofarg.ofmt_cbarg = arg;
370 		if ((*of[i].of_cb)(&ofarg, value, sizeof (value)))
371 			ofmt_print_field(os, &of[i], value, escsep);
372 		else
373 			ofmt_print_field(os, &of[i], OFMT_VAL_UNKNOWN, escsep);
374 	}
375 	(void) putchar('\n');
376 	(void) fflush(stdout);
377 }
378 
379 /*
380  * Print the field headers
381  */
382 static void
383 ofmt_print_header(ofmt_state_t *os)
384 {
385 	int i;
386 	ofmt_field_t *of = os->os_fields;
387 	boolean_t escsep = (os->os_nfields > 1);
388 
389 	for (i = 0; i < os->os_nfields; i++) {
390 		os->os_lastfield = (i + 1 == os->os_nfields);
391 		ofmt_print_field(os, &of[i], of[i].of_name, escsep);
392 	}
393 	(void) putchar('\n');
394 }
395 
396 /*
397  * Update the current window size.
398  */
399 void
400 ofmt_update_winsize(ofmt_handle_t ofmt)
401 {
402 	ofmt_state_t *os = ofmt;
403 	struct winsize *winsize = &os->os_winsize;
404 
405 	if (ioctl(1, TIOCGWINSZ, winsize) == -1 ||
406 	    winsize->ws_col == 0 || winsize->ws_row == 0) {
407 		winsize->ws_col = 80;
408 		winsize->ws_row = 24;
409 	}
410 }
411 
412 /*
413  * Return error diagnostics using the information in the ofmt_handle_t
414  */
415 char *
416 ofmt_strerror(ofmt_handle_t ofmt, ofmt_status_t err, char *buf, uint_t bufsize)
417 {
418 	ofmt_state_t *os = ofmt;
419 	int i;
420 	const char *s;
421 	char ebuf[OFMT_BUFSIZE];
422 
423 	/*
424 	 * ebuf is intended for optional error-specific data to be appended
425 	 * after the internationalized error string for an error code.
426 	 */
427 	ebuf[0] = '\0';
428 
429 	switch (err) {
430 	case OFMT_SUCCESS:
431 		s = "success";
432 		break;
433 	case OFMT_EBADFIELDS:
434 		/*
435 		 * Enumerate the singular/plural version of the warning
436 		 * and error to simplify and improve localization.
437 		 */
438 		if (!os->os_parsable) {
439 			if (os->os_nbad > 1)
440 				s = "ignoring unknown output fields:";
441 			else
442 				s = "ignoring unknown output field:";
443 		} else {
444 			if (os->os_nbad > 1)
445 				s = "unknown output fields:";
446 			else
447 				s = "unknown output field:";
448 		}
449 		/* set up the bad fields in ebuf */
450 		for (i = 0; i < os->os_nbad; i++) {
451 			(void) strlcat(ebuf, " `", sizeof (ebuf));
452 			(void) strlcat(ebuf, os->os_badfields[i],
453 			    sizeof (ebuf));
454 			(void) strlcat(ebuf, "'", sizeof (ebuf));
455 		}
456 		break;
457 	case OFMT_ENOFIELDS:
458 		s = "no valid output fields";
459 		break;
460 	case OFMT_EPARSEALL:
461 		s = "output field `all' invalid in parsable mode";
462 		break;
463 	case OFMT_EPARSENONE:
464 		s = "output fields must be specified in parsable mode";
465 		break;
466 	case OFMT_ENOTEMPLATE:
467 		s = "no template provided for fields";
468 		break;
469 	case OFMT_ENOMEM:
470 		s = strerror(ENOMEM);
471 		break;
472 	default:
473 		(void) snprintf(buf, bufsize,
474 		    dgettext(TEXT_DOMAIN, "unknown ofmt error (%d)"),
475 		    err);
476 		return (buf);
477 	}
478 	(void) snprintf(buf, bufsize, dgettext(TEXT_DOMAIN, s));
479 	(void) strlcat(buf, ebuf, bufsize);
480 	return (buf);
481 }
482