xref: /titanic_41/usr/src/lib/libinetutil/common/ofmt.c (revision e9af4bc0b1cc30cea75d6ad4aa2fde97d985e9be)
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 	uint_t	s_currfield;	/* the current field being processed */
47 } split_t;
48 static void splitfree(split_t *);
49 static split_t *split_str(const char *, uint_t);
50 static split_t *split_fields(const ofmt_field_t *, uint_t, uint_t);
51 
52 /*
53  * The state of the output is tracked in a ofmt_state_t structure.
54  * Each os_fields[i] entry points at an ofmt_field_t array for
55  * the sub-command whose contents are provided by the caller, with
56  * os_nfields set to the number of requested fields.
57  */
58 typedef struct ofmt_state_s {
59 	ofmt_field_t  	*os_fields;
60 	uint_t		os_nfields;
61 	boolean_t	os_lastfield;
62 	uint_t		os_overflow;
63 	struct winsize	os_winsize;
64 	int		os_nrow;
65 	boolean_t	os_parsable;
66 	boolean_t	os_wrap;
67 	int		os_nbad;
68 	char		**os_badfields;
69 	boolean_t	os_multiline;
70 	int		os_maxnamelen;	/* longest name (f. multiline) */
71 } ofmt_state_t;
72 /*
73  * A B_TRUE return value from the callback function will print out the contents
74  * of the output buffer, except when the buffer is returned with the empty
75  * string "", in which case the  OFMT_VAL_UNDEF will be printed.
76  *
77  * If the callback function returns B_FALSE, the "?" string will be emitted.
78  */
79 #define	OFMT_VAL_UNDEF		"--"
80 #define	OFMT_VAL_UNKNOWN	"?"
81 
82 /*
83  * The maximum number of rows supported by the OFMT_WRAP option.
84  */
85 #define	OFMT_MAX_ROWS		128
86 
87 static void ofmt_print_header(ofmt_state_t *);
88 static void ofmt_print_field(ofmt_state_t *, ofmt_field_t *, const char *,
89     boolean_t);
90 
91 /*
92  * Split `str' into at most `maxfields' fields, Return a pointer to a
93  * split_t containing the split fields, or NULL on failure.
94  */
95 static split_t *
96 split_str(const char *str, uint_t maxfields)
97 {
98 	char	*field, *token, *lasts = NULL;
99 	split_t	*sp;
100 
101 	if (*str == '\0' || maxfields == 0)
102 		return (NULL);
103 
104 	sp = calloc(sizeof (split_t), 1);
105 	if (sp == NULL)
106 		return (NULL);
107 
108 	sp->s_buf = strdup(str);
109 	sp->s_fields = malloc(sizeof (char *) * maxfields);
110 	if (sp->s_buf == NULL || sp->s_fields == NULL)
111 		goto fail;
112 
113 	token = sp->s_buf;
114 	while ((field = strtok_r(token, ",", &lasts)) != NULL) {
115 		if (sp->s_nfields == maxfields)
116 			goto fail;
117 		token = NULL;
118 		sp->s_fields[sp->s_nfields++] = field;
119 	}
120 	return (sp);
121 fail:
122 	splitfree(sp);
123 	return (NULL);
124 }
125 
126 /*
127  * Split `fields' into at most `maxfields' fields. Return a pointer to
128  * a split_t containing the split fields, or NULL on failure. Invoked
129  * when all fields are implicitly selected at handle creation by
130  * passing in a NULL fields_str
131  */
132 static split_t *
133 split_fields(const ofmt_field_t *template, uint_t maxfields, uint_t maxcols)
134 {
135 	split_t	*sp;
136 	int i, cols;
137 
138 	sp = calloc(sizeof (split_t), 1);
139 	if (sp == NULL)
140 		return (NULL);
141 
142 	sp->s_fields = malloc(sizeof (char *) * maxfields);
143 	if (sp->s_fields == NULL)
144 		goto fail;
145 	cols = 0;
146 	for (i = 0; i < maxfields; i++) {
147 		cols += template[i].of_width;
148 		/*
149 		 * If all fields are implied without explicitly passing
150 		 * in a fields_str, build a list of field names, stopping
151 		 * when we run out of columns.
152 		 */
153 		if (maxcols > 0 && cols > maxcols)
154 			break;
155 		sp->s_fields[sp->s_nfields++] = template[i].of_name;
156 	}
157 	return (sp);
158 fail:
159 	splitfree(sp);
160 	return (NULL);
161 }
162 
163 /*
164  * Free the split_t structure pointed to by `sp'.
165  */
166 static void
167 splitfree(split_t *sp)
168 {
169 	if (sp == NULL)
170 		return;
171 	free(sp->s_buf);
172 	free(sp->s_fields);
173 	free(sp);
174 }
175 
176 /*
177  * Open a handle to be used for printing formatted output.
178  */
179 ofmt_status_t
180 ofmt_open(const char *str, const ofmt_field_t *template, uint_t flags,
181     uint_t maxcols, ofmt_handle_t *ofmt)
182 {
183 	split_t		*sp;
184 	uint_t		i, j, of_index;
185 	const ofmt_field_t *ofp;
186 	ofmt_field_t	*of;
187 	ofmt_state_t	*os;
188 	int		nfields = 0;
189 	ofmt_status_t	err = OFMT_SUCCESS;
190 	boolean_t	parsable = ((flags & OFMT_PARSABLE) != 0);
191 	boolean_t	wrap = ((flags & OFMT_WRAP) != 0);
192 	boolean_t	multiline = (flags & OFMT_MULTILINE);
193 
194 	*ofmt = NULL;
195 	if (parsable) {
196 		if (multiline)
197 			return (OFMT_EPARSEMULTI);
198 		/*
199 		 * For parsable output mode, the caller always needs
200 		 * to specify precisely which fields are to be selected,
201 		 * since the set of fields may change over time.
202 		 */
203 		if (str == NULL || str[0] == '\0')
204 			return (OFMT_EPARSENONE);
205 		if (strcmp(str, "all") == 0)
206 			return (OFMT_EPARSEALL);
207 		if (wrap)
208 			return (OFMT_EPARSEWRAP);
209 	}
210 	if (template == NULL)
211 		return (OFMT_ENOTEMPLATE);
212 	for (ofp = template; ofp->of_name != NULL; ofp++)
213 		nfields++;
214 	/*
215 	 * split str into the columns selected, or construct the
216 	 * full set of columns (equivalent to -o all).
217 	 */
218 	if (str != NULL && strcmp(str, "all") != 0) {
219 		sp = split_str(str, nfields);
220 	} else {
221 		if (parsable || (str != NULL && strcmp(str, "all") == 0))
222 			maxcols = 0;
223 		sp = split_fields(template, nfields, maxcols);
224 	}
225 	if (sp == NULL)
226 		goto nomem;
227 
228 	os = calloc(sizeof (ofmt_state_t) +
229 	    sp->s_nfields * sizeof (ofmt_field_t), 1);
230 	if (os == NULL)
231 		goto nomem;
232 	*ofmt = os;
233 	os->os_fields = (ofmt_field_t *)&os[1];
234 	os->os_parsable = parsable;
235 	os->os_wrap = wrap;
236 
237 	os->os_multiline = multiline;
238 	of = os->os_fields;
239 	of_index = 0;
240 	/*
241 	 * sp->s_nfields is the number of fields requested in fields_str.
242 	 * nfields is the number of fields in template.
243 	 */
244 	for (i = 0; i < sp->s_nfields; i++) {
245 		for (j = 0; j < nfields; j++) {
246 			if (strcasecmp(sp->s_fields[i],
247 			    template[j].of_name) == 0) {
248 				break;
249 			}
250 		}
251 		if (j == nfields) {
252 			int nbad = os->os_nbad++;
253 
254 			err = OFMT_EBADFIELDS;
255 			if (os->os_badfields == NULL) {
256 				os->os_badfields = malloc(sp->s_nfields *
257 				    sizeof (char *));
258 				if (os->os_badfields == NULL)
259 					goto nomem;
260 			}
261 			os->os_badfields[nbad] = strdup(sp->s_fields[i]);
262 			if (os->os_badfields[nbad] == NULL)
263 				goto nomem;
264 			continue;
265 		}
266 		of[of_index].of_name = strdup(template[j].of_name);
267 		if (of[of_index].of_name == NULL)
268 			goto nomem;
269 		if (multiline) {
270 			int n = strlen(of[of_index].of_name);
271 
272 			os->os_maxnamelen = MAX(n, os->os_maxnamelen);
273 		}
274 		of[of_index].of_width = template[j].of_width;
275 		of[of_index].of_id = template[j].of_id;
276 		of[of_index].of_cb = template[j].of_cb;
277 		of_index++;
278 	}
279 	splitfree(sp);
280 	if (of_index == 0) /* all values in str are bogus */
281 		return (OFMT_ENOFIELDS);
282 	os->os_nfields = of_index; /* actual number of fields printed */
283 	ofmt_update_winsize(*ofmt);
284 	return (err);
285 nomem:
286 	err = OFMT_ENOMEM;
287 	if (os != NULL)
288 		ofmt_close(os);
289 	*ofmt = NULL;
290 	splitfree(sp);
291 	return (err);
292 }
293 
294 /*
295  * free resources associated with the ofmt_handle_t
296  */
297 void
298 ofmt_close(ofmt_handle_t ofmt)
299 {
300 	ofmt_state_t *os = ofmt;
301 	int i;
302 
303 	if (os == NULL)
304 		return;
305 	for (i = 0; i < os->os_nfields; i++)
306 		free(os->os_fields[i].of_name);
307 	for (i = 0; i < os->os_nbad; i++)
308 		free(os->os_badfields[i]);
309 	free(os->os_badfields);
310 	free(os);
311 }
312 
313 /*
314  * Print the value for the selected field by calling the callback-function
315  * registered for the field.
316  */
317 static void
318 ofmt_print_field(ofmt_state_t *os, ofmt_field_t *ofp, const char *value,
319     boolean_t escsep)
320 {
321 	uint_t	width = ofp->of_width;
322 	uint_t	valwidth;
323 	uint_t	compress;
324 	char	c;
325 
326 	/*
327 	 * Parsable fields are separated by ':'. If such a field contains
328 	 * a ':' or '\', this character is prefixed by a '\'.
329 	 */
330 	if (os->os_parsable) {
331 		if (os->os_nfields == 1) {
332 			(void) printf("%s", value);
333 			return;
334 		}
335 		while ((c = *value++) != '\0') {
336 			if (escsep && ((c == ':' || c == '\\')))
337 				(void) putchar('\\');
338 			(void) putchar(c);
339 		}
340 		if (!os->os_lastfield)
341 			(void) putchar(':');
342 	} else if (os->os_multiline) {
343 		if (value[0] == '\0')
344 			value = OFMT_VAL_UNDEF;
345 		(void) printf("%*.*s: %s", os->os_maxnamelen,
346 		    os->os_maxnamelen, ofp->of_name, value);
347 		if (!os->os_lastfield)
348 			(void) putchar('\n');
349 	} else {
350 		if (os->os_lastfield) {
351 			(void) printf("%s", value);
352 			os->os_overflow = 0;
353 			return;
354 		}
355 
356 		valwidth = strlen(value);
357 		if (valwidth + os->os_overflow >= width) {
358 			os->os_overflow += valwidth - width + 1;
359 			(void) printf("%s ", value);
360 			return;
361 		}
362 
363 		if (os->os_overflow > 0) {
364 			compress = MIN(os->os_overflow, width - valwidth);
365 			os->os_overflow -= compress;
366 			width -= compress;
367 		}
368 		(void) printf("%-*s", width, value);
369 	}
370 }
371 
372 /*
373  * Print enough to fit the field width.
374  */
375 static void
376 ofmt_fit_width(split_t **spp, uint_t width, char *value, uint_t bufsize)
377 {
378 	split_t		*sp = *spp;
379 	char		*ptr = value, *lim = ptr + bufsize;
380 	int		i, nextlen;
381 
382 	if (sp == NULL) {
383 		sp = split_str(value, OFMT_MAX_ROWS);
384 		if (sp == NULL)
385 			return;
386 
387 		*spp = sp;
388 	}
389 	for (i = sp->s_currfield; i < sp->s_nfields; i++) {
390 		ptr += snprintf(ptr, lim - ptr, "%s,", sp->s_fields[i]);
391 		if (i + 1 == sp->s_nfields) {
392 			nextlen = 0;
393 			if (ptr > value)
394 				ptr[-1] = '\0';
395 		} else {
396 			nextlen = strlen(sp->s_fields[i + 1]);
397 		}
398 
399 		if (strlen(value) + nextlen > width || ptr >= lim) {
400 			i++;
401 			break;
402 		}
403 	}
404 	sp->s_currfield = i;
405 }
406 
407 /*
408  * Print one or more rows of output values for the selected columns.
409  */
410 void
411 ofmt_print(ofmt_handle_t ofmt, void *arg)
412 {
413 	ofmt_state_t *os = ofmt;
414 	int i;
415 	char value[1024];
416 	ofmt_field_t *of;
417 	boolean_t escsep, more_rows;
418 	ofmt_arg_t ofarg;
419 	split_t **sp = NULL;
420 
421 	if (os->os_wrap) {
422 		sp = calloc(sizeof (split_t *), os->os_nfields);
423 		if (sp == NULL)
424 			return;
425 	}
426 
427 	if ((os->os_nrow++ % os->os_winsize.ws_row) == 0 && !os->os_parsable &&
428 	    !os->os_multiline) {
429 		ofmt_print_header(os);
430 		os->os_nrow++;
431 	}
432 
433 	if (os->os_multiline && os->os_nrow > 1)
434 		(void) putchar('\n');
435 
436 	of = os->os_fields;
437 	escsep = (os->os_nfields > 1);
438 	more_rows = B_FALSE;
439 	for (i = 0; i < os->os_nfields; i++) {
440 		os->os_lastfield = (i + 1 == os->os_nfields);
441 		value[0] = '\0';
442 		ofarg.ofmt_id = of[i].of_id;
443 		ofarg.ofmt_cbarg = arg;
444 
445 		if ((*of[i].of_cb)(&ofarg, value, sizeof (value))) {
446 			if (os->os_wrap) {
447 				/*
448 				 * 'value' will be split at comma boundaries
449 				 * and stored into sp[i].
450 				 */
451 				ofmt_fit_width(&sp[i], of[i].of_width, value,
452 				    sizeof (value));
453 				if (sp[i] != NULL &&
454 				    sp[i]->s_currfield < sp[i]->s_nfields)
455 					more_rows = B_TRUE;
456 			}
457 			ofmt_print_field(os, &of[i],
458 			    (*value == '\0' && !os->os_parsable) ?
459 			    OFMT_VAL_UNDEF : value, escsep);
460 		} else {
461 			ofmt_print_field(os, &of[i], OFMT_VAL_UNKNOWN, escsep);
462 		}
463 	}
464 	(void) putchar('\n');
465 
466 	while (more_rows) {
467 		more_rows = B_FALSE;
468 		for (i = 0; i < os->os_nfields; i++) {
469 			os->os_lastfield = (i + 1 == os->os_nfields);
470 			value[0] = '\0';
471 
472 			ofmt_fit_width(&sp[i], of[i].of_width,
473 			    value, sizeof (value));
474 			if (sp[i] != NULL &&
475 			    sp[i]->s_currfield < sp[i]->s_nfields)
476 				more_rows = B_TRUE;
477 
478 			ofmt_print_field(os, &of[i], value, escsep);
479 		}
480 		(void) putchar('\n');
481 	}
482 	(void) fflush(stdout);
483 
484 	if (sp != NULL) {
485 		for (i = 0; i < os->os_nfields; i++)
486 			splitfree(sp[i]);
487 		free(sp);
488 	}
489 }
490 
491 /*
492  * Print the field headers
493  */
494 static void
495 ofmt_print_header(ofmt_state_t *os)
496 {
497 	int i;
498 	ofmt_field_t *of = os->os_fields;
499 	boolean_t escsep = (os->os_nfields > 1);
500 
501 	for (i = 0; i < os->os_nfields; i++) {
502 		os->os_lastfield = (i + 1 == os->os_nfields);
503 		ofmt_print_field(os, &of[i], of[i].of_name, escsep);
504 	}
505 	(void) putchar('\n');
506 }
507 
508 /*
509  * Update the current window size.
510  */
511 void
512 ofmt_update_winsize(ofmt_handle_t ofmt)
513 {
514 	ofmt_state_t *os = ofmt;
515 	struct winsize *winsize = &os->os_winsize;
516 
517 	if (ioctl(1, TIOCGWINSZ, winsize) == -1 ||
518 	    winsize->ws_col == 0 || winsize->ws_row == 0) {
519 		winsize->ws_col = 80;
520 		winsize->ws_row = 24;
521 	}
522 }
523 
524 /*
525  * Return error diagnostics using the information in the ofmt_handle_t
526  */
527 char *
528 ofmt_strerror(ofmt_handle_t ofmt, ofmt_status_t err, char *buf, uint_t bufsize)
529 {
530 	ofmt_state_t *os = ofmt;
531 	int i;
532 	const char *s;
533 	char ebuf[OFMT_BUFSIZE];
534 
535 	/*
536 	 * ebuf is intended for optional error-specific data to be appended
537 	 * after the internationalized error string for an error code.
538 	 */
539 	ebuf[0] = '\0';
540 
541 	switch (err) {
542 	case OFMT_SUCCESS:
543 		s = "success";
544 		break;
545 	case OFMT_EBADFIELDS:
546 		/*
547 		 * Enumerate the singular/plural version of the warning
548 		 * and error to simplify and improve localization.
549 		 */
550 		if (!os->os_parsable) {
551 			if (os->os_nbad > 1)
552 				s = "ignoring unknown output fields:";
553 			else
554 				s = "ignoring unknown output field:";
555 		} else {
556 			if (os->os_nbad > 1)
557 				s = "unknown output fields:";
558 			else
559 				s = "unknown output field:";
560 		}
561 		/* set up the bad fields in ebuf */
562 		for (i = 0; i < os->os_nbad; i++) {
563 			(void) strlcat(ebuf, " `", sizeof (ebuf));
564 			(void) strlcat(ebuf, os->os_badfields[i],
565 			    sizeof (ebuf));
566 			(void) strlcat(ebuf, "'", sizeof (ebuf));
567 		}
568 		break;
569 	case OFMT_ENOFIELDS:
570 		s = "no valid output fields";
571 		break;
572 	case OFMT_EPARSEMULTI:
573 		s = "multiline mode incompatible with parsable mode";
574 		break;
575 	case OFMT_EPARSEALL:
576 		s = "output field `all' invalid in parsable mode";
577 		break;
578 	case OFMT_EPARSENONE:
579 		s = "output fields must be specified in parsable mode";
580 		break;
581 	case OFMT_EPARSEWRAP:
582 		s = "parsable mode is incompatible with wrap mode";
583 		break;
584 	case OFMT_ENOTEMPLATE:
585 		s = "no template provided for fields";
586 		break;
587 	case OFMT_ENOMEM:
588 		s = strerror(ENOMEM);
589 		break;
590 	default:
591 		(void) snprintf(buf, bufsize,
592 		    dgettext(TEXT_DOMAIN, "unknown ofmt error (%d)"),
593 		    err);
594 		return (buf);
595 	}
596 	(void) snprintf(buf, bufsize, dgettext(TEXT_DOMAIN, s));
597 	(void) strlcat(buf, ebuf, bufsize);
598 	return (buf);
599 }
600