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