17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5a727d47jwadams * Common Development and Distribution License (the "License").
6a727d47jwadams * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22a727d47jwadams * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
26cab8de1Bryan Cantrill/*
27f11c6b6John Levon * Copyright 2020 Joyent, Inc.
28218912fSteve Gonczi * Copyright (c) 2016 by Delphix. All rights reserved.
29cab8de1Bryan Cantrill */
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate/*
327c478bdstevel@tonic-gate * MDB uses its own enhanced standard i/o mechanism for all input and output.
337c478bdstevel@tonic-gate * This file provides the underpinnings of this mechanism, including the
347c478bdstevel@tonic-gate * printf-style formatting code, the output pager, and APIs for raw input
357c478bdstevel@tonic-gate * and output.  This mechanism is used throughout the debugger for everything
367c478bdstevel@tonic-gate * from simple sprintf and printf-style formatting, to input to the lexer
377c478bdstevel@tonic-gate * and parser, to raw file i/o for reading ELF files.  In general, we divide
387c478bdstevel@tonic-gate * our i/o implementation into two parts:
397c478bdstevel@tonic-gate *
407c478bdstevel@tonic-gate * (1) An i/o buffer (mdb_iob_t) provides buffered read or write capabilities,
417c478bdstevel@tonic-gate * as well as access to formatting and the ability to invoke a pager.  The
427c478bdstevel@tonic-gate * buffer is constructed explicitly for use in either reading or writing; it
437c478bdstevel@tonic-gate * may not be used for both simultaneously.
447c478bdstevel@tonic-gate *
457c478bdstevel@tonic-gate * (2) Each i/o buffer is associated with an underlying i/o backend (mdb_io_t).
467c478bdstevel@tonic-gate * The backend provides, through an ops-vector, equivalents for the standard
477c478bdstevel@tonic-gate * read, write, lseek, ioctl, and close operations.  In addition, the backend
487c478bdstevel@tonic-gate * can provide an IOP_NAME entry point for returning a name for the backend,
497c478bdstevel@tonic-gate * IOP_LINK and IOP_UNLINK entry points that are called when the backend is
507c478bdstevel@tonic-gate * connected or disconnected from an mdb_iob_t, and an IOP_SETATTR entry point
517c478bdstevel@tonic-gate * for manipulating terminal attributes.
527c478bdstevel@tonic-gate *
537c478bdstevel@tonic-gate * The i/o objects themselves are reference counted so that more than one i/o
547c478bdstevel@tonic-gate * buffer may make use of the same i/o backend.  In addition, each buffer
557c478bdstevel@tonic-gate * provides the ability to push or pop backends to interpose on input or output
567c478bdstevel@tonic-gate * behavior.  We make use of this, for example, to implement interactive
577c478bdstevel@tonic-gate * session logging.  Normally, the stdout iob has a backend that is either
587c478bdstevel@tonic-gate * file descriptor 1, or a terminal i/o backend associated with the tty.
597c478bdstevel@tonic-gate * However, we can push a log i/o backend on top that multiplexes stdout to
607c478bdstevel@tonic-gate * the original back-end and another backend that writes to a log file.  The
617c478bdstevel@tonic-gate * use of i/o backends is also used for simplifying tasks such as making
627c478bdstevel@tonic-gate * lex and yacc read from strings for mdb_eval(), and making our ELF file
637c478bdstevel@tonic-gate * processing code read executable "files" from a crash dump via kvm_uread.
647c478bdstevel@tonic-gate *
657c478bdstevel@tonic-gate * Additionally, the formatting code provides auto-wrap and indent facilities
667c478bdstevel@tonic-gate * that are necessary for compatibility with adb macro formatting.  In auto-
677c478bdstevel@tonic-gate * wrap mode, the formatting code examines each new chunk of output to determine
687c478bdstevel@tonic-gate * if it will fit on the current line.  If not, instead of having the chunk
697c478bdstevel@tonic-gate * divided between the current line of output and the next, the auto-wrap
707c478bdstevel@tonic-gate * code will automatically output a newline, auto-indent the next line,
717c478bdstevel@tonic-gate * and then continue.  Auto-indent is implemented by simply prepending a number
727c478bdstevel@tonic-gate * of blanks equal to iob_margin to the start of each line.  The margin is
737c478bdstevel@tonic-gate * inserted when the iob is created, and following each flush of the buffer.
747c478bdstevel@tonic-gate */
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate#include <sys/types.h>
777c478bdstevel@tonic-gate#include <sys/termios.h>
787c478bdstevel@tonic-gate#include <stdarg.h>
797c478bdstevel@tonic-gate#include <arpa/inet.h>
807c478bdstevel@tonic-gate#include <sys/socket.h>
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gate#include <mdb/mdb_types.h>
837c478bdstevel@tonic-gate#include <mdb/mdb_argvec.h>
847c478bdstevel@tonic-gate#include <mdb/mdb_stdlib.h>
857c478bdstevel@tonic-gate#include <mdb/mdb_string.h>
867c478bdstevel@tonic-gate#include <mdb/mdb_target.h>
877c478bdstevel@tonic-gate#include <mdb/mdb_signal.h>
887c478bdstevel@tonic-gate#include <mdb/mdb_debug.h>
897c478bdstevel@tonic-gate#include <mdb/mdb_io_impl.h>
907c478bdstevel@tonic-gate#include <mdb/mdb_modapi.h>
917c478bdstevel@tonic-gate#include <mdb/mdb_demangle.h>
927c478bdstevel@tonic-gate#include <mdb/mdb_err.h>
937c478bdstevel@tonic-gate#include <mdb/mdb_nv.h>
947c478bdstevel@tonic-gate#include <mdb/mdb_frame.h>
957c478bdstevel@tonic-gate#include <mdb/mdb_lex.h>
967c478bdstevel@tonic-gate#include <mdb/mdb.h>
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate/*
997c478bdstevel@tonic-gate * Define list of possible integer sizes for conversion routines:
1007c478bdstevel@tonic-gate */
1017c478bdstevel@tonic-gatetypedef enum {
1027c478bdstevel@tonic-gate	SZ_SHORT,		/* format %h? */
1037c478bdstevel@tonic-gate	SZ_INT,			/* format %? */
1047c478bdstevel@tonic-gate	SZ_LONG,		/* format %l? */
1057c478bdstevel@tonic-gate	SZ_LONGLONG		/* format %ll? */
1067c478bdstevel@tonic-gate} intsize_t;
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate/*
1097c478bdstevel@tonic-gate * The iob snprintf family of functions makes use of a special "sprintf
1107c478bdstevel@tonic-gate * buffer" i/o backend in order to provide the appropriate snprintf semantics.
1117c478bdstevel@tonic-gate * This structure is maintained as the backend-specific private storage,
1127c478bdstevel@tonic-gate * and its use is described in more detail below (see spbuf_write()).
1137c478bdstevel@tonic-gate */
1147c478bdstevel@tonic-gatetypedef struct {
1157c478bdstevel@tonic-gate	char *spb_buf;		/* pointer to underlying buffer */
1167c478bdstevel@tonic-gate	size_t spb_bufsiz;	/* length of underlying buffer */
1177c478bdstevel@tonic-gate	size_t spb_total;	/* total of all bytes passed via IOP_WRITE */
1187c478bdstevel@tonic-gate} spbuf_t;
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gate/*
1217c478bdstevel@tonic-gate * Define VA_ARG macro for grabbing the next datum to format for the printf
1227c478bdstevel@tonic-gate * family of functions.  We use VA_ARG so that we can support two kinds of
1237c478bdstevel@tonic-gate * argument lists: the va_list type supplied by <stdarg.h> used for printf and
1247c478bdstevel@tonic-gate * vprintf, and an array of mdb_arg_t structures, which we expect will be
1257c478bdstevel@tonic-gate * either type STRING or IMMEDIATE.  The vec_arg function takes care of
1267c478bdstevel@tonic-gate * handling the mdb_arg_t case.
1277c478bdstevel@tonic-gate */
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gatetypedef enum {
1307c478bdstevel@tonic-gate	VAT_VARARGS,		/* va_list is a va_list */
1317c478bdstevel@tonic-gate	VAT_ARGVEC		/* va_list is a const mdb_arg_t[] in disguise */
1327c478bdstevel@tonic-gate} vatype_t;
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gatetypedef struct {
1357c478bdstevel@tonic-gate	vatype_t val_type;
1367c478bdstevel@tonic-gate	union {
1377c478bdstevel@tonic-gate		va_list	_val_valist;
1387c478bdstevel@tonic-gate		const mdb_arg_t *_val_argv;
1397c478bdstevel@tonic-gate	} _val_u;
1407c478bdstevel@tonic-gate} varglist_t;
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate#define	val_valist	_val_u._val_valist
1437c478bdstevel@tonic-gate#define	val_argv	_val_u._val_argv
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate#define	VA_ARG(ap, type) ((ap->val_type == VAT_VARARGS) ? \
1467c478bdstevel@tonic-gate	va_arg(ap->val_valist, type) : (type)vec_arg(&ap->val_argv))
1477c478bdstevel@tonic-gate#define	VA_PTRARG(ap) ((ap->val_type == VAT_VARARGS) ? \
1487c478bdstevel@tonic-gate	(void *)va_arg(ap->val_valist, uintptr_t) : \
1497c478bdstevel@tonic-gate	(void *)(uintptr_t)vec_arg(&ap->val_argv))
1507c478bdstevel@tonic-gate
1517c478bdstevel@tonic-gate/*
1527c478bdstevel@tonic-gate * Define macro for converting char constant to Ctrl-char equivalent:
1537c478bdstevel@tonic-gate */
1547c478bdstevel@tonic-gate#ifndef CTRL
1557c478bdstevel@tonic-gate#define	CTRL(c)	((c) & 0x01f)
1567c478bdstevel@tonic-gate#endif
1577c478bdstevel@tonic-gate
158f76ff24Bryan Cantrill#define	IOB_AUTOWRAP(iob)	\
159f76ff24Bryan Cantrill	((mdb.m_flags & MDB_FL_AUTOWRAP) && \
160f76ff24Bryan Cantrill	((iob)->iob_flags & MDB_IOB_AUTOWRAP))
161f76ff24Bryan Cantrill
1627c478bdstevel@tonic-gate/*
1637c478bdstevel@tonic-gate * Define macro for determining if we should automatically wrap to the next
1647c478bdstevel@tonic-gate * line of output, based on the amount of consumed buffer space and the
165c8a3ee0Bryan Cantrill * specified size of the next thing to be inserted (n) -- being careful to
166c8a3ee0Bryan Cantrill * not force a spurious wrap if we're autoindented and already at the margin.
1677c478bdstevel@tonic-gate */
1687c478bdstevel@tonic-gate#define	IOB_WRAPNOW(iob, n)	\
169f76ff24Bryan Cantrill	(IOB_AUTOWRAP(iob) && (iob)->iob_nbytes != 0 && \
170c8a3ee0Bryan Cantrill	((n) + (iob)->iob_nbytes > (iob)->iob_cols) &&  \
171c8a3ee0Bryan Cantrill	!(((iob)->iob_flags & MDB_IOB_INDENT) && \
172c8a3ee0Bryan Cantrill	(iob)->iob_nbytes == (iob)->iob_margin))
1737c478bdstevel@tonic-gate
1747c478bdstevel@tonic-gate/*
1757c478bdstevel@tonic-gate * Define prompt string and string to erase prompt string for iob_pager
1767c478bdstevel@tonic-gate * function, which is invoked if the pager is enabled on an i/o buffer
1777c478bdstevel@tonic-gate * and we're about to print a line which would be the last on the screen.
1787c478bdstevel@tonic-gate */
1797c478bdstevel@tonic-gate
1807c478bdstevel@tonic-gatestatic const char io_prompt[] = ">> More [<space>, <cr>, q, n, c, a] ? ";
1817c478bdstevel@tonic-gatestatic const char io_perase[] = "                                      ";
1827c478bdstevel@tonic-gate
1837c478bdstevel@tonic-gatestatic const char io_pbcksp[] =
1847c478bdstevel@tonic-gate/*CSTYLED*/
1857c478bdstevel@tonic-gate"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
1867c478bdstevel@tonic-gate
1877c478bdstevel@tonic-gatestatic const size_t io_promptlen = sizeof (io_prompt) - 1;
1887c478bdstevel@tonic-gatestatic const size_t io_peraselen = sizeof (io_perase) - 1;
1897c478bdstevel@tonic-gatestatic const size_t io_pbcksplen = sizeof (io_pbcksp) - 1;
1907c478bdstevel@tonic-gate
1917c478bdstevel@tonic-gatestatic ssize_t
1927c478bdstevel@tonic-gateiob_write(mdb_iob_t *iob, mdb_io_t *io, const void *buf, size_t n)
1937c478bdstevel@tonic-gate{
1947c478bdstevel@tonic-gate	ssize_t resid = n;
1957c478bdstevel@tonic-gate	ssize_t len;
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate	while (resid != 0) {
1987c478bdstevel@tonic-gate		if ((len = IOP_WRITE(io, buf, resid)) <= 0)
1997c478bdstevel@tonic-gate			break;
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate		buf = (char *)buf + len;
2027c478bdstevel@tonic-gate		resid -= len;
2037c478bdstevel@tonic-gate	}
2047c478bdstevel@tonic-gate
2057c478bdstevel@tonic-gate	/*
2067c478bdstevel@tonic-gate	 * Note that if we had a partial write before an error, we still want
2077c478bdstevel@tonic-gate	 * to return the fact something was written.  The caller will get an
2087c478bdstevel@tonic-gate	 * error next time it tries to write anything.
2097c478bdstevel@tonic-gate	 */
2107c478bdstevel@tonic-gate	if (resid == n && n != 0) {
2117c478bdstevel@tonic-gate		iob->iob_flags |= MDB_IOB_ERR;
2127c478bdstevel@tonic-gate		return (-1);
2137c478bdstevel@tonic-gate	}
2147c478bdstevel@tonic-gate
2157c478bdstevel@tonic-gate	return (n - resid);
2167c478bdstevel@tonic-gate}
2177c478bdstevel@tonic-gate
2187c478bdstevel@tonic-gatestatic ssize_t
2197c478bdstevel@tonic-gateiob_read(mdb_iob_t *iob, mdb_io_t *io)
2207c478bdstevel@tonic-gate{
2217c478bdstevel@tonic-gate	ssize_t len;
2227c478bdstevel@tonic-gate
2237c478bdstevel@tonic-gate	ASSERT(iob->iob_nbytes == 0);
2247c478bdstevel@tonic-gate	len = IOP_READ(io, iob->iob_buf, iob->iob_bufsiz);
2257c478bdstevel@tonic-gate	iob->iob_bufp = &iob->iob_buf[0];
2267c478bdstevel@tonic-gate
2277c478bdstevel@tonic-gate	switch (len) {
2287c478bdstevel@tonic-gate	case -1:
2297c478bdstevel@tonic-gate		iob->iob_flags |= MDB_IOB_ERR;
2307c478bdstevel@tonic-gate		break;
2317c478bdstevel@tonic-gate	case 0:
2327c478bdstevel@tonic-gate		iob->iob_flags |= MDB_IOB_EOF;
2337c478bdstevel@tonic-gate		break;
2347c478bdstevel@tonic-gate	default:
2357c478bdstevel@tonic-gate		iob->iob_nbytes = len;
2367c478bdstevel@tonic-gate	}
2377c478bdstevel@tonic-gate
2387c478bdstevel@tonic-gate	return (len);
2397c478bdstevel@tonic-gate}
2407c478bdstevel@tonic-gate
2417c478bdstevel@tonic-gate/*ARGSUSED*/
2427c478bdstevel@tonic-gatestatic void
2437c478bdstevel@tonic-gateiob_winch(int sig, siginfo_t *sip, ucontext_t *ucp, void *data)
2447c478bdstevel@tonic-gate{
2457c478bdstevel@tonic-gate	siglongjmp(*((sigjmp_buf *)data), sig);
2467c478bdstevel@tonic-gate}
2477c478bdstevel@tonic-gate
2487c478bdstevel@tonic-gatestatic int
2497c478bdstevel@tonic-gateiob_pager(mdb_iob_t *iob)
2507c478bdstevel@tonic-gate{
2517c478bdstevel@tonic-gate	int status = 0;
2527c478bdstevel@tonic-gate	sigjmp_buf env;
2537c478bdstevel@tonic-gate	uchar_t c;
2547c478bdstevel@tonic-gate
2557c478bdstevel@tonic-gate	mdb_signal_f *termio_winch;
2567c478bdstevel@tonic-gate	void *termio_data;
2577c478bdstevel@tonic-gate	size_t old_rows;
2587c478bdstevel@tonic-gate
2597c478bdstevel@tonic-gate	if (iob->iob_pgp == NULL || (iob->iob_flags & MDB_IOB_PGCONT))
2607c478bdstevel@tonic-gate		return (0);
2617c478bdstevel@tonic-gate
2627c478bdstevel@tonic-gate	termio_winch = mdb_signal_gethandler(SIGWINCH, &termio_data);
2637c478bdstevel@tonic-gate	(void) mdb_signal_sethandler(SIGWINCH, iob_winch, &env);
2647c478bdstevel@tonic-gate
2657c478bdstevel@tonic-gate	if (sigsetjmp(env, 1) != 0) {
2667c478bdstevel@tonic-gate		/*
2677c478bdstevel@tonic-gate		 * Reset the cursor back to column zero before printing a new
2687c478bdstevel@tonic-gate		 * prompt, since its position is unreliable after a SIGWINCH.
2697c478bdstevel@tonic-gate		 */
2707c478bdstevel@tonic-gate		(void) iob_write(iob, iob->iob_pgp, "\r", sizeof (char));
2717c478bdstevel@tonic-gate		old_rows = iob->iob_rows;
2727c478bdstevel@tonic-gate
2737c478bdstevel@tonic-gate		/*
2747c478bdstevel@tonic-gate		 * If an existing SIGWINCH handler was present, call it.  We
2757c478bdstevel@tonic-gate		 * expect that this will be termio: the handler will read the
2767c478bdstevel@tonic-gate		 * new window size, and then resize this iob appropriately.
2777c478bdstevel@tonic-gate		 */
2787c478bdstevel@tonic-gate		if (termio_winch != (mdb_signal_f *)NULL)
2797c478bdstevel@tonic-gate			termio_winch(SIGWINCH, NULL, NULL, termio_data);
2807c478bdstevel@tonic-gate
2817c478bdstevel@tonic-gate		/*
2827c478bdstevel@tonic-gate		 * If the window has increased in size, we treat this like a
2837c478bdstevel@tonic-gate		 * request to fill out the new remainder of the page.
2847c478bdstevel@tonic-gate		 */
2857c478bdstevel@tonic-gate		if (iob->iob_rows > old_rows) {
2867c478bdstevel@tonic-gate			iob->iob_flags &= ~MDB_IOB_PGSINGLE;
2877c478bdstevel@tonic-gate			iob->iob_nlines = old_rows;
2887c478bdstevel@tonic-gate			status = 0;
2897c478bdstevel@tonic-gate			goto winch;
2907c478bdstevel@tonic-gate		}
2917c478bdstevel@tonic-gate	}
2927c478bdstevel@tonic-gate
2937c478bdstevel@tonic-gate	(void) iob_write(iob, iob->iob_pgp, io_prompt, io_promptlen);
2947c478bdstevel@tonic-gate
2957c478bdstevel@tonic-gate	for (;;) {
2967c478bdstevel@tonic-gate		if (IOP_READ(iob->iob_pgp, &c, sizeof (c)) != sizeof (c)) {
2977c478bdstevel@tonic-gate			status = MDB_ERR_PAGER;
2987c478bdstevel@tonic-gate			break;
2997c478bdstevel@tonic-gate		}
3007c478bdstevel@tonic-gate
3017c478bdstevel@tonic-gate		switch (c) {
3027c478bdstevel@tonic-gate		case 'N':
3037c478bdstevel@tonic-gate		case 'n':
3047c478bdstevel@tonic-gate		case '\n':
3057c478bdstevel@tonic-gate		case '\r':
3067c478bdstevel@tonic-gate			iob->iob_flags |= MDB_IOB_PGSINGLE;
3077c478bdstevel@tonic-gate			goto done;
3087c478bdstevel@tonic-gate
3097c478bdstevel@tonic-gate		case CTRL('c'):
3107c478bdstevel@tonic-gate		case CTRL('\\'):
3117c478bdstevel@tonic-gate		case 'Q':
3127c478bdstevel@tonic-gate		case 'q':
3137c478bdstevel@tonic-gate			mdb_iob_discard(iob);
3147c478bdstevel@tonic-gate			status = MDB_ERR_PAGER;
3157c478bdstevel@tonic-gate			goto done;
3167c478bdstevel@tonic-gate
3177c478bdstevel@tonic-gate		case 'A':
3187c478bdstevel@tonic-gate		case 'a':
3197c478bdstevel@tonic-gate			mdb_iob_discard(iob);
3207c478bdstevel@tonic-gate			status = MDB_ERR_ABORT;
3217c478bdstevel@tonic-gate			goto done;
3227c478bdstevel@tonic-gate
3237c478bdstevel@tonic-gate		case 'C':
3247c478bdstevel@tonic-gate		case 'c':
3257c478bdstevel@tonic-gate			iob->iob_flags |= MDB_IOB_PGCONT;
3267c478bdstevel@tonic-gate			/*FALLTHRU*/
3277c478bdstevel@tonic-gate
3287c478bdstevel@tonic-gate		case ' ':
3297c478bdstevel@tonic-gate			iob->iob_flags &= ~MDB_IOB_PGSINGLE;
3307c478bdstevel@tonic-gate			goto done;
3317c478bdstevel@tonic-gate		}
3327c478bdstevel@tonic-gate	}
3337c478bdstevel@tonic-gate
3347c478bdstevel@tonic-gatedone:
3357c478bdstevel@tonic-gate	(void) iob_write(iob, iob->iob_pgp, io_pbcksp, io_pbcksplen);
3367c478bdstevel@tonic-gatewinch:
3377c478bdstevel@tonic-gate	(void) iob_write(iob, iob->iob_pgp, io_perase, io_peraselen);
3387c478bdstevel@tonic-gate	(void) iob_write(iob, iob->iob_pgp, io_pbcksp, io_pbcksplen);
3397c478bdstevel@tonic-gate	(void) mdb_signal_sethandler(SIGWINCH, termio_winch, termio_data);
3407c478bdstevel@tonic-gate
3417c478bdstevel@tonic-gate	if ((iob->iob_flags & MDB_IOB_ERR) && status == 0)
3427c478bdstevel@tonic-gate		status = MDB_ERR_OUTPUT;
3437c478bdstevel@tonic-gate
3447c478bdstevel@tonic-gate	return (status);
3457c478bdstevel@tonic-gate}
3467c478bdstevel@tonic-gate
3477c478bdstevel@tonic-gatestatic void
3487c478bdstevel@tonic-gateiob_indent(mdb_iob_t *iob)
3497c478bdstevel@tonic-gate{
3507c478bdstevel@tonic-gate	if (iob->iob_nbytes == 0 && iob->iob_margin != 0 &&
3517c478bdstevel@tonic-gate	    (iob->iob_flags & MDB_IOB_INDENT)) {
3527c478bdstevel@tonic-gate		size_t i;
3537c478bdstevel@tonic-gate
3547c478bdstevel@tonic-gate		ASSERT(iob->iob_margin < iob->iob_cols);
3557c478bdstevel@tonic-gate		ASSERT(iob->iob_bufp == iob->iob_buf);
3567c478bdstevel@tonic-gate
3577c478bdstevel@tonic-gate		for (i = 0; i < iob->iob_margin; i++)
3587c478bdstevel@tonic-gate			*iob->iob_bufp++ = ' ';
3597c478bdstevel@tonic-gate
3607c478bdstevel@tonic-gate		iob->iob_nbytes = iob->iob_margin;
3617c478bdstevel@tonic-gate	}
3627c478bdstevel@tonic-gate}
3637c478bdstevel@tonic-gate
3647c478bdstevel@tonic-gatestatic void
3657c478bdstevel@tonic-gateiob_unindent(mdb_iob_t *iob)
3667c478bdstevel@tonic-gate{
3677c478bdstevel@tonic-gate	if (iob->iob_nbytes != 0 && iob->iob_nbytes == iob->iob_margin) {
3687c478bdstevel@tonic-gate		const char *p = iob->iob_buf;
3697c478bdstevel@tonic-gate
3707c478bdstevel@tonic-gate		while (p < &iob->iob_buf[iob->iob_margin]) {
3717c478bdstevel@tonic-gate			if (*p++ != ' ')
3727c478bdstevel@tonic-gate				return;
3737c478bdstevel@tonic-gate		}
3747c478bdstevel@tonic-gate
3757c478bdstevel@tonic-gate		iob->iob_bufp = &iob->iob_buf[0];
3767c478bdstevel@tonic-gate		iob->iob_nbytes = 0;
3777c478bdstevel@tonic-gate	}
3787c478bdstevel@tonic-gate}
3797c478bdstevel@tonic-gate
3807c478bdstevel@tonic-gatemdb_iob_t *
3817c478bdstevel@tonic-gatemdb_iob_create(mdb_io_t *io, uint_t flags)
3827c478bdstevel@tonic-gate{
3837c478bdstevel@tonic-gate	mdb_iob_t *iob = mdb_alloc(sizeof (mdb_iob_t), UM_SLEEP);
3847c478bdstevel@tonic-gate
3857c478bdstevel@tonic-gate	iob->iob_buf = mdb_alloc(BUFSIZ, UM_SLEEP);
3867c478bdstevel@tonic-gate	iob->iob_bufsiz = BUFSIZ;
3877c478bdstevel@tonic-gate	iob->iob_bufp = &iob->iob_buf[0];
3887c478bdstevel@tonic-gate	iob->iob_nbytes = 0;
3897c478bdstevel@tonic-gate	iob->iob_nlines = 0;
3907c478bdstevel@tonic-gate	iob->iob_lineno = 1;
3917c478bdstevel@tonic-gate	iob->iob_rows = MDB_IOB_DEFROWS;
3927c478bdstevel@tonic-gate	iob->iob_cols = MDB_IOB_DEFCOLS;
3937c478bdstevel@tonic-gate	iob->iob_tabstop = MDB_IOB_DEFTAB;
3947c478bdstevel@tonic-gate	iob->iob_margin = MDB_IOB_DEFMARGIN;
3957c478bdstevel@tonic-gate	iob->iob_flags = flags & ~(MDB_IOB_EOF|MDB_IOB_ERR) | MDB_IOB_AUTOWRAP;
3967c478bdstevel@tonic-gate	iob->iob_iop = mdb_io_hold(io);
3977c478bdstevel@tonic-gate	iob->iob_pgp = NULL;
3987c478bdstevel@tonic-gate	iob->iob_next = NULL;
3997c478bdstevel@tonic-gate
4007c478bdstevel@tonic-gate	IOP_LINK(io, iob);
4017c478bdstevel@tonic-gate	iob_indent(iob);
4027c478bdstevel@tonic-gate	return (iob);
4037c478bdstevel@tonic-gate}
4047c478bdstevel@tonic-gate
4057c478bdstevel@tonic-gatevoid
4067c478bdstevel@tonic-gatemdb_iob_pipe(mdb_iob_t **iobs, mdb_iobsvc_f *rdsvc, mdb_iobsvc_f *wrsvc)
4077c478bdstevel@tonic-gate{
4087c478bdstevel@tonic-gate	mdb_io_t *pio = mdb_pipeio_create(rdsvc, wrsvc);
4097c478bdstevel@tonic-gate	int i;
4107c478bdstevel@tonic-gate
4117c478bdstevel@tonic-gate	iobs[0] = mdb_iob_create(pio, MDB_IOB_RDONLY);
4127c478bdstevel@tonic-gate	iobs[1] = mdb_iob_create(pio, MDB_IOB_WRONLY);
4137c478bdstevel@tonic-gate
4147c478bdstevel@tonic-gate	for (i = 0; i < 2; i++) {
4157c478bdstevel@tonic-gate		iobs[i]->iob_flags &= ~MDB_IOB_AUTOWRAP;
4167c478bdstevel@tonic-gate		iobs[i]->iob_cols = iobs[i]->iob_bufsiz;
4177c478bdstevel@tonic-gate	}
4187c478bdstevel@tonic-gate}
4197c478bdstevel@tonic-gate
4207c478bdstevel@tonic-gatevoid
4217c478bdstevel@tonic-gatemdb_iob_destroy(mdb_iob_t *iob)
4227c478bdstevel@tonic-gate{
4237c478bdstevel@tonic-gate	/*
424f76ff24Bryan Cantrill	 * Don't flush a pipe, since it may cause a context switch when the
4257c478bdstevel@tonic-gate	 * other side has already been destroyed.
4267c478bdstevel@tonic-gate	 */
4277c478bdstevel@tonic-gate	if (!mdb_iob_isapipe(iob))
4287c478bdstevel@tonic-gate		mdb_iob_flush(iob);
4297c478bdstevel@tonic-gate
4307c478bdstevel@tonic-gate	if (iob->iob_pgp != NULL)
4317c478bdstevel@tonic-gate		mdb_io_rele(iob->iob_pgp);
4327c478bdstevel@tonic-gate
4337c478bdstevel@tonic-gate	while (iob->iob_iop != NULL) {
4347c478bdstevel@tonic-gate		IOP_UNLINK(iob->iob_iop, iob);
4357c478bdstevel@tonic-gate		(void) mdb_iob_pop_io(iob);
4367c478bdstevel@tonic-gate	}
4377c478bdstevel@tonic-gate
4387c478bdstevel@tonic-gate	mdb_free(iob->iob_buf, iob->iob_bufsiz);
4397c478bdstevel@tonic-gate	mdb_free(iob, sizeof (mdb_iob_t));
4407c478bdstevel@tonic-gate}
4417c478bdstevel@tonic-gate
4427c478bdstevel@tonic-gatevoid
4437c478bdstevel@tonic-gatemdb_iob_discard(mdb_iob_t *iob)
4447c478bdstevel@tonic-gate{
4457c478bdstevel@tonic-gate	iob->iob_bufp = &iob->iob_buf[0];
4467c478bdstevel@tonic-gate	iob->iob_nbytes = 0;
4477c478bdstevel@tonic-gate}
4487c478bdstevel@tonic-gate
4497c478bdstevel@tonic-gatevoid
4507c478bdstevel@tonic-gatemdb_iob_flush(mdb_iob_t *iob)
4517c478bdstevel@tonic-gate{
4527c478bdstevel@tonic-gate	int pgerr = 0;
4537c478bdstevel@tonic-gate
4547c478bdstevel@tonic-gate	if (iob->iob_nbytes == 0)
4557c478bdstevel@tonic-gate		return; /* Nothing to do if buffer is empty */
4567c478bdstevel@tonic-gate
4577c478bdstevel@tonic-gate	if (iob->iob_flags & MDB_IOB_WRONLY) {
4587c478bdstevel@tonic-gate		if (iob->iob_flags & MDB_IOB_PGSINGLE) {
4597c478bdstevel@tonic-gate			iob->iob_flags &= ~MDB_IOB_PGSINGLE;
4607c478bdstevel@tonic-gate			iob->iob_nlines = 0;
4617c478bdstevel@tonic-gate			pgerr = iob_pager(iob);
4627c478bdstevel@tonic-gate
4637c478bdstevel@tonic-gate		} else if (iob->iob_nlines >= iob->iob_rows - 1) {
4647c478bdstevel@tonic-gate			iob->iob_nlines = 0;
4657c478bdstevel@tonic-gate			if (iob->iob_flags & MDB_IOB_PGENABLE)
4667c478bdstevel@tonic-gate				pgerr = iob_pager(iob);
4677c478bdstevel@tonic-gate		}
4687c478bdstevel@tonic-gate
4697c478bdstevel@tonic-gate		if (pgerr == 0) {
4707c478bdstevel@tonic-gate			/*
4717c478bdstevel@tonic-gate			 * We only jump out of the dcmd on error if the iob is
4727c478bdstevel@tonic-gate			 * m_out. Presumably, if a dcmd has opened a special
4737c478bdstevel@tonic-gate			 * file and is writing to it, it will handle errors
4747c478bdstevel@tonic-gate			 * properly.
4757c478bdstevel@tonic-gate			 */
4767c478bdstevel@tonic-gate			if (iob_write(iob, iob->iob_iop, iob->iob_buf,
4777c478bdstevel@tonic-gate			    iob->iob_nbytes) < 0 && iob == mdb.m_out)
4787c478bdstevel@tonic-gate				pgerr = MDB_ERR_OUTPUT;
4797c478bdstevel@tonic-gate			iob->iob_nlines++;
4807c478bdstevel@tonic-gate		}
4817c478bdstevel@tonic-gate	}
4827c478bdstevel@tonic-gate
4837c478bdstevel@tonic-gate	iob->iob_bufp = &iob->iob_buf[0];
4847c478bdstevel@tonic-gate	iob->iob_nbytes = 0;
4857c478bdstevel@tonic-gate	iob_indent(iob);
4867c478bdstevel@tonic-gate
4877c478bdstevel@tonic-gate	if (pgerr)
4887c478bdstevel@tonic-gate		longjmp(mdb.m_frame->f_pcb, pgerr);
4897c478bdstevel@tonic-gate}
4907c478bdstevel@tonic-gate
4917c478bdstevel@tonic-gatevoid
4927c478bdstevel@tonic-gatemdb_iob_nlflush(mdb_iob_t *iob)
4937c478bdstevel@tonic-gate{
4947c478bdstevel@tonic-gate	iob_unindent(iob);
4957c478bdstevel@tonic-gate
4967c478bdstevel@tonic-gate	if (iob->iob_nbytes != 0)
4977c478bdstevel@tonic-gate		mdb_iob_nl(iob);
4987c478bdstevel@tonic-gate	else
4997c478bdstevel@tonic-gate		iob_indent(iob);
5007c478bdstevel@tonic-gate}
5017c478bdstevel@tonic-gate
5027c478bdstevel@tonic-gatevoid
5037c478bdstevel@tonic-gatemdb_iob_push_io(mdb_iob_t *iob, mdb_io_t *io)
5047c478bdstevel@tonic-gate{
5057c478bdstevel@tonic-gate	ASSERT(io->io_next == NULL);
5067c478bdstevel@tonic-gate
5077c478bdstevel@tonic-gate	io->io_next = iob->iob_iop;
5087c478bdstevel@tonic-gate	iob->iob_iop = mdb_io_hold(io);
5097c478bdstevel@tonic-gate}
5107c478bdstevel@tonic-gate
5117c478bdstevel@tonic-gatemdb_io_t *
5127c478bdstevel@tonic-gatemdb_iob_pop_io(mdb_iob_t *iob)
5137c478bdstevel@tonic-gate{
5147c478bdstevel@tonic-gate	mdb_io_t *io = iob->iob_iop;
5157c478bdstevel@tonic-gate
5167c478bdstevel@tonic-gate	if (io != NULL) {
5177c478bdstevel@tonic-gate		iob->iob_iop = io->io_next;
5187c478bdstevel@tonic-gate		io->io_next = NULL;
5197c478bdstevel@tonic-gate		mdb_io_rele(io);
5207c478bdstevel@tonic-gate	}
5217c478bdstevel@tonic-gate
5227c478bdstevel@tonic-gate	return (io);
5237c478bdstevel@tonic-gate}
5247c478bdstevel@tonic-gate
5257c478bdstevel@tonic-gatevoid
5267c478bdstevel@tonic-gatemdb_iob_resize(mdb_iob_t *iob, size_t rows, size_t cols)
5277c478bdstevel@tonic-gate{
5287c478bdstevel@tonic-gate	if (cols > iob->iob_bufsiz)
5297c478bdstevel@tonic-gate		iob->iob_cols = iob->iob_bufsiz;
5307c478bdstevel@tonic-gate	else
5317c478bdstevel@tonic-gate		iob->iob_cols = cols != 0 ? cols : MDB_IOB_DEFCOLS;
5327c478bdstevel@tonic-gate
5337c478bdstevel@tonic-gate	iob->iob_rows = rows != 0 ? rows : MDB_IOB_DEFROWS;
5347c478bdstevel@tonic-gate}
5357c478bdstevel@tonic-gate
5367c478bdstevel@tonic-gatevoid
5377c478bdstevel@tonic-gatemdb_iob_setpager(mdb_iob_t *iob, mdb_io_t *pgio)
5387c478bdstevel@tonic-gate{
5397c478bdstevel@tonic-gate	struct winsize winsz;
5407c478bdstevel@tonic-gate
5417c478bdstevel@tonic-gate	if (iob->iob_pgp != NULL) {
5427c478bdstevel@tonic-gate		IOP_UNLINK(iob->iob_pgp, iob);
5437c478bdstevel@tonic-gate		mdb_io_rele(iob->iob_pgp);
5447c478bdstevel@tonic-gate	}
5457c478bdstevel@tonic-gate
5467c478bdstevel@tonic-gate	iob->iob_flags |= MDB_IOB_PGENABLE;
5477c478bdstevel@tonic-gate	iob->iob_flags &= ~(MDB_IOB_PGSINGLE | MDB_IOB_PGCONT);
5487c478bdstevel@tonic-gate	iob->iob_pgp = mdb_io_hold(pgio);
5497c478bdstevel@tonic-gate
5507c478bdstevel@tonic-gate	IOP_LINK(iob->iob_pgp, iob);
5517c478bdstevel@tonic-gate
5527c478bdstevel@tonic-gate	if (IOP_CTL(pgio, TIOCGWINSZ, &winsz) == 0)
5537c478bdstevel@tonic-gate		mdb_iob_resize(iob, (size_t)winsz.ws_row, (size_t)winsz.ws_col);
5547c478bdstevel@tonic-gate}
5557c478bdstevel@tonic-gate
5567c478bdstevel@tonic-gatevoid
5577c478bdstevel@tonic-gatemdb_iob_tabstop(mdb_iob_t *iob, size_t tabstop)
5587c478bdstevel@tonic-gate{
5597c478bdstevel@tonic-gate	iob->iob_tabstop = MIN(tabstop, iob->iob_cols - 1);
5607c478bdstevel@tonic-gate}
5617c478bdstevel@tonic-gate
5627c478bdstevel@tonic-gatevoid
5637c478bdstevel@tonic-gatemdb_iob_margin(mdb_iob_t *iob, size_t margin)
5647c478bdstevel@tonic-gate{
5657c478bdstevel@tonic-gate	iob_unindent(iob);
5667c478bdstevel@tonic-gate	iob->iob_margin = MIN(margin, iob->iob_cols - 1);
5677c478bdstevel@tonic-gate	iob_indent(iob);
5687c478bdstevel@tonic-gate}
5697c478bdstevel@tonic-gate
5707c478bdstevel@tonic-gatevoid
5717c478bdstevel@tonic-gatemdb_iob_setbuf(mdb_iob_t *iob, void *buf, size_t bufsiz)
5727c478bdstevel@tonic-gate{
5737c478bdstevel@tonic-gate	ASSERT(buf != NULL && bufsiz != 0);
5747c478bdstevel@tonic-gate
5757c478bdstevel@tonic-gate	mdb_free(iob->iob_buf, iob->iob_bufsiz);
5767c478bdstevel@tonic-gate	iob->iob_buf = buf;
5777c478bdstevel@tonic-gate	iob->iob_bufsiz = bufsiz;
5787c478bdstevel@tonic-gate
5797c478bdstevel@tonic-gate	if (iob->iob_flags & MDB_IOB_WRONLY)
5807c478bdstevel@tonic-gate		iob->iob_cols = MIN(iob->iob_cols, iob->iob_bufsiz);
5817c478bdstevel@tonic-gate}
5827c478bdstevel@tonic-gate
5837c478bdstevel@tonic-gatevoid
5847c478bdstevel@tonic-gatemdb_iob_clearlines(mdb_iob_t *iob)
5857c478bdstevel@tonic-gate{
5867c478bdstevel@tonic-gate	iob->iob_flags &= ~(MDB_IOB_PGSINGLE | MDB_IOB_PGCONT);
5877c478bdstevel@tonic-gate	iob->iob_nlines = 0;
5887c478bdstevel@tonic-gate}
5897c478bdstevel@tonic-gate
5907c478bdstevel@tonic-gatevoid
5917c478bdstevel@tonic-gatemdb_iob_setflags(mdb_iob_t *iob, uint_t flags)
5927c478bdstevel@tonic-gate{
5937c478bdstevel@tonic-gate	iob->iob_flags |= flags;
5947c478bdstevel@tonic-gate	if (flags & MDB_IOB_INDENT)
5957c478bdstevel@tonic-gate		iob_indent(iob);
5967c478bdstevel@tonic-gate}
5977c478bdstevel@tonic-gate
5987c478bdstevel@tonic-gatevoid
5997c478bdstevel@tonic-gatemdb_iob_clrflags(mdb_iob_t *iob, uint_t flags)
6007c478bdstevel@tonic-gate{
6017c478bdstevel@tonic-gate	iob->iob_flags &= ~flags;
6027c478bdstevel@tonic-gate	if (flags & MDB_IOB_INDENT)
6037c478bdstevel@tonic-gate		iob_unindent(iob);
6047c478bdstevel@tonic-gate}
6057c478bdstevel@tonic-gate
6067c478bdstevel@tonic-gateuint_t
6077c478bdstevel@tonic-gatemdb_iob_getflags(mdb_iob_t *iob)
6087c478bdstevel@tonic-gate{
6097c478bdstevel@tonic-gate	return (iob->iob_flags);
6107c478bdstevel@tonic-gate}
6117c478bdstevel@tonic-gate
6127c478bdstevel@tonic-gatestatic uintmax_t
6137c478bdstevel@tonic-gatevec_arg(const mdb_arg_t **app)
6147c478bdstevel@tonic-gate{
6157c478bdstevel@tonic-gate	uintmax_t value;
6167c478bdstevel@tonic-gate
6177c478bdstevel@tonic-gate	if ((*app)->a_type == MDB_TYPE_STRING)
6187c478bdstevel@tonic-gate		value = (uintmax_t)(uintptr_t)(*app)->a_un.a_str;
6197c478bdstevel@tonic-gate	else
6207c478bdstevel@tonic-gate		value = (*app)->a_un.a_val;
6217c478bdstevel@tonic-gate
6227c478bdstevel@tonic-gate	(*app)++;
6237c478bdstevel@tonic-gate	return (value);
6247c478bdstevel@tonic-gate}
6257c478bdstevel@tonic-gate
6267c478bdstevel@tonic-gatestatic const char *
6277c478bdstevel@tonic-gateiob_size2str(intsize_t size)
6287c478bdstevel@tonic-gate{
6297c478bdstevel@tonic-gate	switch (size) {
6307c478bdstevel@tonic-gate	case SZ_SHORT:
6317c478bdstevel@tonic-gate		return ("short");
6327c478bdstevel@tonic-gate	case SZ_INT:
6337c478bdstevel@tonic-gate		return ("int");
6347c478bdstevel@tonic-gate	case SZ_LONG:
6357c478bdstevel@tonic-gate		return ("long");
6367c478bdstevel@tonic-gate	case SZ_LONGLONG:
6377c478bdstevel@tonic-gate		return ("long long");
6387c478bdstevel@tonic-gate	}
6397c478bdstevel@tonic-gate	return ("");
6407c478bdstevel@tonic-gate}
6417c478bdstevel@tonic-gate
6427c478bdstevel@tonic-gate/*
6437c478bdstevel@tonic-gate * In order to simplify maintenance of the ::formats display, we provide an
6447c478bdstevel@tonic-gate * unparser for mdb_printf format strings that converts a simple format
6457c478bdstevel@tonic-gate * string with one specifier into a descriptive representation, e.g.
6467c478bdstevel@tonic-gate * mdb_iob_format2str("%llx") returns "hexadecimal long long".
6477c478bdstevel@tonic-gate */
6487c478bdstevel@tonic-gateconst char *
6497c478bdstevel@tonic-gatemdb_iob_format2str(const char *format)
6507c478bdstevel@tonic-gate{
6517c478bdstevel@tonic-gate	intsize_t size = SZ_INT;
6527c478bdstevel@tonic-gate	const char *p;
6537c478bdstevel@tonic-gate
6547c478bdstevel@tonic-gate	static char buf[64];
6557c478bdstevel@tonic-gate
6567c478bdstevel@tonic-gate	buf[0] = '\0';
6577c478bdstevel@tonic-gate
6587c478bdstevel@tonic-gate	if ((p = strchr(format, '%')) == NULL)
6597c478bdstevel@tonic-gate		goto done;
6607c478bdstevel@tonic-gate
6617c478bdstevel@tonic-gatefmt_switch:
6627c478bdstevel@tonic-gate	switch (*++p) {
6637c478bdstevel@tonic-gate	case '0': case '1': case '2': case '3': case '4':
6647c478bdstevel@tonic-gate	case '5': case '6': case '7': case '8': case '9':
6657c478bdstevel@tonic-gate		while (*p >= '0' && *p <= '9')
6667c478bdstevel@tonic-gate			p++;
6677c478bdstevel@tonic-gate		p--;
6687c478bdstevel@tonic-gate		goto fmt_switch;
6697c478bdstevel@tonic-gate
6707c478bdstevel@tonic-gate	case 'a':
6717c478bdstevel@tonic-gate	case 'A':
6727c478bdstevel@tonic-gate		return ("symbol");
6737c478bdstevel@tonic-gate
6747c478bdstevel@tonic-gate	case 'b':
6757c478bdstevel@tonic-gate		(void) strcpy(buf, "unsigned ");
6767c478bdstevel@tonic-gate		(void) strcat(buf, iob_size2str(size));
6777c478bdstevel@tonic-gate		(void) strcat(buf, " bitfield");
6787c478bdstevel@tonic-gate		break;
6797c478bdstevel@tonic-gate
6807c478bdstevel@tonic-gate	case 'c':
6817c478bdstevel@tonic-gate		return ("character");
6827c478bdstevel@tonic-gate
6837c478bdstevel@tonic-gate	case 'd':
6847c478bdstevel@tonic-gate	case 'i':
6857c478bdstevel@tonic-gate		(void) strcpy(buf, "decimal signed ");
6867c478bdstevel@tonic-gate		(void) strcat(buf, iob_size2str(size));
6877c478bdstevel@tonic-gate		break;
6887c478bdstevel@tonic-gate
6897c478bdstevel@tonic-gate	case 'e':
6907c478bdstevel@tonic-gate	case 'E':
6917c478bdstevel@tonic-gate	case 'g':
6927c478bdstevel@tonic-gate	case 'G':
6937c478bdstevel@tonic-gate		return ("double");
6947c478bdstevel@tonic-gate
6957c478bdstevel@tonic-gate	case 'h':
6967c478bdstevel@tonic-gate		size = SZ_SHORT;
6977c478bdstevel@tonic-gate		goto fmt_switch;
6987c478bdstevel@tonic-gate
699cab8de1Bryan Cantrill	case 'H':
700cab8de1Bryan Cantrill		return ("human-readable size");
701cab8de1Bryan Cantrill
7027c478bdstevel@tonic-gate	case 'I':
7037c478bdstevel@tonic-gate		return ("IPv4 address");
7047c478bdstevel@tonic-gate
7057c478bdstevel@tonic-gate	case 'l':
7067c478bdstevel@tonic-gate		if (size >= SZ_LONG)
7077c478bdstevel@tonic-gate			size = SZ_LONGLONG;
7087c478bdstevel@tonic-gate		else
7097c478bdstevel@tonic-gate			size = SZ_LONG;
7107c478bdstevel@tonic-gate		goto fmt_switch;
7117c478bdstevel@tonic-gate
7127c478bdstevel@tonic-gate	case 'm':
7137c478bdstevel@tonic-gate		return ("margin");
7147c478bdstevel@tonic-gate
7157c478bdstevel@tonic-gate	case 'N':
7167c478bdstevel@tonic-gate		return ("IPv6 address");
7177c478bdstevel@tonic-gate
7187c478bdstevel@tonic-gate	case 'o':
7197c478bdstevel@tonic-gate		(void) strcpy(buf, "octal unsigned ");
7207c478bdstevel@tonic-gate		(void) strcat(buf, iob_size2str(size));
7217c478bdstevel@tonic-gate		break;
7227c478bdstevel@tonic-gate
7237c478bdstevel@tonic-gate	case 'p':
7247c478bdstevel@tonic-gate		return ("pointer");
7257c478bdstevel@tonic-gate
7267c478bdstevel@tonic-gate	case 'q':
7277c478bdstevel@tonic-gate		(void) strcpy(buf, "octal signed ");
7287c478bdstevel@tonic-gate		(void) strcat(buf, iob_size2str(size));
7297c478bdstevel@tonic-gate		break;
7307c478bdstevel@tonic-gate
7317c478bdstevel@tonic-gate	case 'r':
7327c478bdstevel@tonic-gate		(void) strcpy(buf, "default radix unsigned ");
7337c478bdstevel@tonic-gate		(void) strcat(buf, iob_size2str(size));
7347c478bdstevel@tonic-gate		break;
7357c478bdstevel@tonic-gate
7367c478bdstevel@tonic-gate	case 'R':
7377c478bdstevel@tonic-gate		(void) strcpy(buf, "default radix signed ");
7387c478bdstevel@tonic-gate		(void) strcat(buf, iob_size2str(size));
7397c478bdstevel@tonic-gate		break;
7407c478bdstevel@tonic-gate
7417c478bdstevel@tonic-gate	case 's':
7427c478bdstevel@tonic-gate		return ("string");
7437c478bdstevel@tonic-gate
7447c478bdstevel@tonic-gate	case 't':
7457c478bdstevel@tonic-gate	case 'T':
7467c478bdstevel@tonic-gate		return ("tab");
7477c478bdstevel@tonic-gate
7487c478bdstevel@tonic-gate	case 'u':
7497c478bdstevel@tonic-gate		(void) strcpy(buf, "decimal unsigned ");
7507c478bdstevel@tonic-gate		(void) strcat(buf, iob_size2str(size));
7517c478bdstevel@tonic-gate		break;
7527c478bdstevel@tonic-gate
7537c478bdstevel@tonic-gate	case 'x':
7547c478bdstevel@tonic-gate	case 'X':
7557c478bdstevel@tonic-gate		(void) strcat(buf, "hexadecimal ");
7567c478bdstevel@tonic-gate		(void) strcat(buf, iob_size2str(size));
7577c478bdstevel@tonic-gate		break;
7587c478bdstevel@tonic-gate
7597c478bdstevel@tonic-gate	case 'Y':
7607c478bdstevel@tonic-gate		return ("time_t");
7617c478bdstevel@tonic-gate
7627c478bdstevel@tonic-gate	case '<':
7637c478bdstevel@tonic-gate		return ("terminal attribute");
7647c478bdstevel@tonic-gate
7657c478bdstevel@tonic-gate	case '?':
7667c478bdstevel@tonic-gate	case '#':
7677c478bdstevel@tonic-gate	case '+':
7687c478bdstevel@tonic-gate	case '-':
7697c478bdstevel@tonic-gate		goto fmt_switch;
7707c478bdstevel@tonic-gate	}
7717c478bdstevel@tonic-gate
7727c478bdstevel@tonic-gatedone:
7737c478bdstevel@tonic-gate	if (buf[0] == '\0')
7747c478bdstevel@tonic-gate		(void) strcpy(buf, "text");
7757c478bdstevel@tonic-gate
7767c478bdstevel@tonic-gate	return ((const char *)buf);
7777c478bdstevel@tonic-gate}
7787c478bdstevel@tonic-gate
7797c478bdstevel@tonic-gatestatic const char *
7807c478bdstevel@tonic-gateiob_int2str(varglist_t *ap, intsize_t size, int base, uint_t flags, int *zero,
7817c478bdstevel@tonic-gate    u_longlong_t *value)
7827c478bdstevel@tonic-gate{
7837c478bdstevel@tonic-gate	uintmax_t i;
7847c478bdstevel@tonic-gate
7857c478bdstevel@tonic-gate	switch (size) {
7867c478bdstevel@tonic-gate	case SZ_LONGLONG:
7877c478bdstevel@tonic-gate		if (flags & NTOS_UNSIGNED)
7887c478bdstevel@tonic-gate			i = (u_longlong_t)VA_ARG(ap, u_longlong_t);
7897c478bdstevel@tonic-gate		else
7907c478bdstevel@tonic-gate			i = (longlong_t)VA_ARG(ap, longlong_t);
7917c478bdstevel@tonic-gate		break;
7927c478bdstevel@tonic-gate
7937c478bdstevel@tonic-gate	case SZ_LONG:
7947c478bdstevel@tonic-gate		if (flags & NTOS_UNSIGNED)
7957c478bdstevel@tonic-gate			i = (ulong_t)VA_ARG(ap, ulong_t);
7967c478bdstevel@tonic-gate		else
7977c478bdstevel@tonic-gate			i = (long)VA_ARG(ap, long);
7987c478bdstevel@tonic-gate		break;
7997c478bdstevel@tonic-gate
8007c478bdstevel@tonic-gate	case SZ_SHORT:
8017c478bdstevel@tonic-gate		if (flags & NTOS_UNSIGNED)
8027c478bdstevel@tonic-gate			i = (ushort_t)VA_ARG(ap, uint_t);
8037c478bdstevel@tonic-gate		else
8047c478bdstevel@tonic-gate			i = (short)VA_ARG(ap, int);
8057c478bdstevel@tonic-gate		break;
8067c478bdstevel@tonic-gate
8077c478bdstevel@tonic-gate	default:
8087c478bdstevel@tonic-gate		if (flags & NTOS_UNSIGNED)
8097c478bdstevel@tonic-gate			i = (uint_t)VA_ARG(ap, uint_t);
8107c478bdstevel@tonic-gate		else
8117c478bdstevel@tonic-gate			i = (int)VA_ARG(ap, int);
8127c478bdstevel@tonic-gate	}
8137c478bdstevel@tonic-gate
8147c478bdstevel@tonic-gate	*zero = i == 0;	/* Return flag indicating if result was zero */
8157c478bdstevel@tonic-gate	*value = i;	/* Return value retrieved from va_list */
8167c478bdstevel@tonic-gate
8177c478bdstevel@tonic-gate	return (numtostr(i, base, flags));
8187c478bdstevel@tonic-gate}
8197c478bdstevel@tonic-gate
8207c478bdstevel@tonic-gatestatic const char *
8217c478bdstevel@tonic-gateiob_time2str(time_t *tmp)
8227c478bdstevel@tonic-gate{
8237c478bdstevel@tonic-gate	/*
8247c478bdstevel@tonic-gate	 * ctime(3c) returns a string of the form
8257c478bdstevel@tonic-gate	 * "Fri Sep 13 00:00:00 1986\n\0".  We turn this into the canonical
8267c478bdstevel@tonic-gate	 * adb /y format "1986 Sep 13 00:00:00" below.
8277c478bdstevel@tonic-gate	 */
8287c478bdstevel@tonic-gate	const char *src = ctime(tmp);
8297c478bdstevel@tonic-gate	static char buf[32];
8307c478bdstevel@tonic-gate	char *dst = buf;
8317c478bdstevel@tonic-gate	int i;
8327c478bdstevel@tonic-gate
8337c478bdstevel@tonic-gate	if (src == NULL)
834a727d47jwadams		return (numtostr((uintmax_t)*tmp, mdb.m_radix, 0));
8357c478bdstevel@tonic-gate
8367c478bdstevel@tonic-gate	for (i = 20; i < 24; i++)
8377c478bdstevel@tonic-gate		*dst++ = src[i]; /* Copy the 4-digit year */
8387c478bdstevel@tonic-gate
8397c478bdstevel@tonic-gate	for (i = 3; i < 19; i++)
8407c478bdstevel@tonic-gate		*dst++ = src[i]; /* Copy month, day, and h:m:s */
8417c478bdstevel@tonic-gate
8427c478bdstevel@tonic-gate	*dst = '\0';
8437c478bdstevel@tonic-gate	return (buf);
8447c478bdstevel@tonic-gate}
8457c478bdstevel@tonic-gate
8467c478bdstevel@tonic-gatestatic const char *
8477c478bdstevel@tonic-gateiob_addr2str(uintptr_t addr)
8487c478bdstevel@tonic-gate{
8497c478bdstevel@tonic-gate	static char buf[MDB_TGT_SYM_NAMLEN];
8507c478bdstevel@tonic-gate	char *name = buf;
8517c478bdstevel@tonic-gate	longlong_t offset;
8527c478bdstevel@tonic-gate	GElf_Sym sym;
8537c478bdstevel@tonic-gate
8547c478bdstevel@tonic-gate	if (mdb_tgt_lookup_by_addr(mdb.m_target, addr,
8557c478bdstevel@tonic-gate	    MDB_TGT_SYM_FUZZY, buf, sizeof (buf), &sym, NULL) == -1)
8567c478bdstevel@tonic-gate		return (NULL);
8577c478bdstevel@tonic-gate
8587c478bdstevel@tonic-gate	if (mdb.m_demangler != NULL && (mdb.m_flags & MDB_FL_DEMANGLE))
8597c478bdstevel@tonic-gate		name = (char *)mdb_dem_convert(mdb.m_demangler, buf);
8607c478bdstevel@tonic-gate
8617c478bdstevel@tonic-gate	/*
8627c478bdstevel@tonic-gate	 * Here we provide a little cooperation between the %a formatting code
8637c478bdstevel@tonic-gate	 * and the proc target: if the initial address passed to %a is in fact
8647c478bdstevel@tonic-gate	 * a PLT address, the proc target's lookup_by_addr code will convert
8657c478bdstevel@tonic-gate	 * this to the PLT destination (a different address).  We do not want
8667c478bdstevel@tonic-gate	 * to append a "+/-offset" suffix based on comparison with the query
8677c478bdstevel@tonic-gate	 * symbol in this case because the proc target has really done a hidden
8687c478bdstevel@tonic-gate	 * query for us with a different address.  We detect this case by
8697c478bdstevel@tonic-gate	 * comparing the initial characters of buf to the special PLT= string.
8707c478bdstevel@tonic-gate	 */
8717c478bdstevel@tonic-gate	if (sym.st_value != addr && strncmp(name, "PLT=", 4) != 0) {
8727c478bdstevel@tonic-gate		if (sym.st_value > addr)
8737c478bdstevel@tonic-gate			offset = -(longlong_t)(sym.st_value - addr);
8747c478bdstevel@tonic-gate		else
8757c478bdstevel@tonic-gate			offset = (longlong_t)(addr - sym.st_value);
8767c478bdstevel@tonic-gate
8777c478bdstevel@tonic-gate		(void) strcat(name, numtostr(offset, mdb.m_radix,
8787c478bdstevel@tonic-gate		    NTOS_SIGNPOS | NTOS_SHOWBASE));
8797c478bdstevel@tonic-gate	}
8807c478bdstevel@tonic-gate
8817c478bdstevel@tonic-gate	return (name);
8827c478bdstevel@tonic-gate}
8837c478bdstevel@tonic-gate
884cab8de1Bryan Cantrill/*
885cab8de1Bryan Cantrill * Produce human-readable size, similar in spirit (and identical in output)
886cab8de1Bryan Cantrill * to libzfs's zfs_nicenum() -- but made significantly more complicated by
887cab8de1Bryan Cantrill * the constraint that we cannot use snprintf() as an implementation detail.
88813e9820Bryan Cantrill * Recall, floating point is verboten in kmdb.
889cab8de1Bryan Cantrill */
890cab8de1Bryan Cantrillstatic const char *
891cab8de1Bryan Cantrilliob_bytes2str(varglist_t *ap, intsize_t size)
892cab8de1Bryan Cantrill{
89313e9820Bryan Cantrill#ifndef _KMDB
894cab8de1Bryan Cantrill	const int sigfig = 3;
89513e9820Bryan Cantrill	uint64_t orig;
89613e9820Bryan Cantrill#endif
89713e9820Bryan Cantrill	uint64_t n;
89813e9820Bryan Cantrill
899cab8de1Bryan Cantrill	static char buf[68], *c;
900cab8de1Bryan Cantrill	int index = 0;
901cab8de1Bryan Cantrill	char u;
902cab8de1Bryan Cantrill
903cab8de1Bryan Cantrill	switch (size) {
904cab8de1Bryan Cantrill	case SZ_LONGLONG:
905cab8de1Bryan Cantrill		n = (u_longlong_t)VA_ARG(ap, u_longlong_t);
906cab8de1Bryan Cantrill		break;
907cab8de1Bryan Cantrill
908cab8de1Bryan Cantrill	case SZ_LONG:
909cab8de1Bryan Cantrill		n = (ulong_t)VA_ARG(ap, ulong_t);
910cab8de1Bryan Cantrill		break;
911cab8de1Bryan Cantrill
912cab8de1Bryan Cantrill	case SZ_SHORT:
913cab8de1Bryan Cantrill		n = (ushort_t)VA_ARG(ap, uint_t);
914b1da084Toomas Soome		break;
915cab8de1Bryan Cantrill
916cab8de1Bryan Cantrill	default:
917cab8de1Bryan Cantrill		n = (uint_t)VA_ARG(ap, uint_t);
918cab8de1Bryan Cantrill	}
919cab8de1Bryan Cantrill
92013e9820Bryan Cantrill#ifndef _KMDB
921cab8de1Bryan Cantrill	orig = n;
92213e9820Bryan Cantrill#endif
923cab8de1Bryan Cantrill
924cab8de1Bryan Cantrill	while (n >= 1024) {
925cab8de1Bryan Cantrill		n /= 1024;
926cab8de1Bryan Cantrill		index++;
927cab8de1Bryan Cantrill	}
928cab8de1Bryan Cantrill
929cab8de1Bryan Cantrill	u = " KMGTPE"[index];
930cab8de1Bryan Cantrill	buf[0] = '\0';
931cab8de1Bryan Cantrill
932cab8de1Bryan Cantrill	if (index == 0) {
933cab8de1Bryan Cantrill		return (numtostr(n, 10, 0));
93413e9820Bryan Cantrill#ifndef _KMDB
935cab8de1Bryan Cantrill	} else if ((orig & ((1ULL << 10 * index) - 1)) == 0) {
93613e9820Bryan Cantrill#else
93713e9820Bryan Cantrill	} else {
93813e9820Bryan Cantrill#endif
939cab8de1Bryan Cantrill		/*
94013e9820Bryan Cantrill		 * If this is an even multiple of the base or we are in an
94113e9820Bryan Cantrill		 * environment where floating point is verboten (i.e., kmdb),
94213e9820Bryan Cantrill		 * always display without any decimal precision.
943cab8de1Bryan Cantrill		 */
944cab8de1Bryan Cantrill		(void) strcat(buf, numtostr(n, 10, 0));
94513e9820Bryan Cantrill#ifndef _KMDB
946cab8de1Bryan Cantrill	} else {
947cab8de1Bryan Cantrill		/*
948cab8de1Bryan Cantrill		 * We want to choose a precision that results in the specified
949cab8de1Bryan Cantrill		 * number of significant figures (by default, 3).  This is
950cab8de1Bryan Cantrill		 * similar to the output that one would get specifying the %.*g
951cab8de1Bryan Cantrill		 * format specifier (where the asterisk denotes the number of
952cab8de1Bryan Cantrill		 * significant digits), but (1) we include trailing zeros if
953cab8de1Bryan Cantrill		 * the there are non-zero digits beyond the number of
954cab8de1Bryan Cantrill		 * significant digits (that is, 10241 is '10.0K', not the
955cab8de1Bryan Cantrill		 * '10K' that it would be with %.3g) and (2) we never resort
956cab8de1Bryan Cantrill		 * to %e notation when the number of digits exceeds the
957cab8de1Bryan Cantrill		 * number of significant figures (that is, 1043968 is '1020K',
958cab8de1Bryan Cantrill		 * not '1.02e+03K').  This is also made somewhat complicated
959cab8de1Bryan Cantrill		 * by the fact that we need to deal with rounding (10239 is
960cab8de1Bryan Cantrill		 * '10.0K', not '9.99K'), for which we perform nearest-even
961cab8de1Bryan Cantrill		 * rounding.
962cab8de1Bryan Cantrill		 */
963cab8de1Bryan Cantrill		double val = (double)orig / (1ULL << 10 * index);
964cab8de1Bryan Cantrill		int i, mag = 1, thresh;
965cab8de1Bryan Cantrill
966cab8de1Bryan Cantrill		for (i = 0; i < sigfig - 1; i++)
967cab8de1Bryan Cantrill			mag *= 10;
968cab8de1Bryan Cantrill
969cab8de1Bryan Cantrill		for (thresh = mag * 10; mag >= 1; mag /= 10, i--) {
970cab8de1Bryan Cantrill			double mult = val * (double)mag;
971cab8de1Bryan Cantrill			uint32_t v;
972cab8de1Bryan Cantrill
973cab8de1Bryan Cantrill			/*
974cab8de1Bryan Cantrill			 * Note that we cast mult to a 32-bit value.  We know
975cab8de1Bryan Cantrill			 * that val is less than 1024 due to the logic above,
976cab8de1Bryan Cantrill			 * and that mag is at most 10^(sigfig - 1).  This means
977cab8de1Bryan Cantrill			 * that as long as sigfig is 9 or lower, this will not
978cab8de1Bryan Cantrill			 * overflow.  (We perform this cast because it assures
979cab8de1Bryan Cantrill			 * that we are never converting a double to a uint64_t,
980cab8de1Bryan Cantrill			 * which for some compilers requires a call to a
981cab8de1Bryan Cantrill			 * function not guaranteed to be in libstand.)
982cab8de1Bryan Cantrill			 */
983cab8de1Bryan Cantrill			if (mult - (double)(uint32_t)mult != 0.5) {
984cab8de1Bryan Cantrill				v = (uint32_t)(mult + 0.5);
985cab8de1Bryan Cantrill			} else {
986cab8de1Bryan Cantrill				/*
987cab8de1Bryan Cantrill				 * We are exactly between integer multiples
988cab8de1Bryan Cantrill				 * of units; perform nearest-even rounding
989cab8de1Bryan Cantrill				 * to be consistent with the behavior of
990cab8de1Bryan Cantrill				 * printf().
991cab8de1Bryan Cantrill				 */
992cab8de1Bryan Cantrill				if ((v = (uint32_t)mult) & 1)
993cab8de1Bryan Cantrill					v++;
994cab8de1Bryan Cantrill			}
995cab8de1Bryan Cantrill
996cab8de1Bryan Cantrill			if (mag == 1) {
997cab8de1Bryan Cantrill				(void) strcat(buf, numtostr(v, 10, 0));
998cab8de1Bryan Cantrill				break;
999cab8de1Bryan Cantrill			}
1000cab8de1Bryan Cantrill
1001cab8de1Bryan Cantrill			if (v < thresh) {
1002cab8de1Bryan Cantrill				(void) strcat(buf, numtostr(v / mag, 10, 0));
1003cab8de1Bryan Cantrill				(void) strcat(buf, ".");
1004cab8de1Bryan Cantrill
1005cab8de1Bryan Cantrill				c = (char *)numtostr(v % mag, 10, 0);
1006cab8de1Bryan Cantrill				i -= strlen(c);
1007cab8de1Bryan Cantrill
1008cab8de1Bryan Cantrill				/*
1009cab8de1Bryan Cantrill				 * We need to zero-fill from the right of the
1010cab8de1Bryan Cantrill				 * decimal point to the first significant digit
1011cab8de1Bryan Cantrill				 * of the fractional component.
1012cab8de1Bryan Cantrill				 */
1013cab8de1Bryan Cantrill				while (i--)
1014cab8de1Bryan Cantrill					(void) strcat(buf, "0");
1015cab8de1Bryan Cantrill
1016cab8de1Bryan Cantrill				(void) strcat(buf, c);
1017cab8de1Bryan Cantrill				break;
1018cab8de1Bryan Cantrill			}
1019cab8de1Bryan Cantrill		}
102013e9820Bryan Cantrill#endif
1021cab8de1Bryan Cantrill	}
1022cab8de1Bryan Cantrill
1023cab8de1Bryan Cantrill	c = &buf[strlen(buf)];
1024cab8de1Bryan Cantrill	*c++ = u;
1025cab8de1Bryan Cantrill	*c++ = '\0';
1026cab8de1Bryan Cantrill
1027cab8de1Bryan Cantrill	return (buf);
1028cab8de1Bryan Cantrill}
1029cab8de1Bryan Cantrill
10307c478bdstevel@tonic-gatestatic int
10317c478bdstevel@tonic-gateiob_setattr(mdb_iob_t *iob, const char *s, size_t nbytes)
10327c478bdstevel@tonic-gate{
10337c478bdstevel@tonic-gate	uint_t attr;
10347c478bdstevel@tonic-gate	int req;
10357c478bdstevel@tonic-gate
10367c478bdstevel@tonic-gate	if (iob->iob_pgp == NULL)
10377c478bdstevel@tonic-gate		return (set_errno(ENOTTY));
10387c478bdstevel@tonic-gate
10397c478bdstevel@tonic-gate	if (nbytes != 0 && *s == '/') {
10407c478bdstevel@tonic-gate		req = ATT_OFF;
10417c478bdstevel@tonic-gate		nbytes--;
10427c478bdstevel@tonic-gate		s++;
10437c478bdstevel@tonic-gate	} else
10447c478bdstevel@tonic-gate		req = ATT_ON;
10457c478bdstevel@tonic-gate
10467c478bdstevel@tonic-gate	if (nbytes != 1)
10477c478bdstevel@tonic-gate		return (set_errno(EINVAL));
10487c478bdstevel@tonic-gate
10497c478bdstevel@tonic-gate	switch (*s) {
10507c478bdstevel@tonic-gate	case 's':
10517c478bdstevel@tonic-gate		attr = ATT_STANDOUT;
10527c478bdstevel@tonic-gate		break;
10537c478bdstevel@tonic-gate	case 'u':
10547c478bdstevel@tonic-gate		attr = ATT_UNDERLINE;
10557c478bdstevel@tonic-gate		break;
10567c478bdstevel@tonic-gate	case 'r':
10577c478bdstevel@tonic-gate		attr = ATT_REVERSE;
10587c478bdstevel@tonic-gate		break;
10597c478bdstevel@tonic-gate	case 'b':
10607c478bdstevel@tonic-gate		attr = ATT_BOLD;
10617c478bdstevel@tonic-gate		break;
10627c478bdstevel@tonic-gate	case 'd':
10637c478bdstevel@tonic-gate		attr = ATT_DIM;
10647c478bdstevel@tonic-gate		break;
10657c478bdstevel@tonic-gate	case 'a':
10667c478bdstevel@tonic-gate		attr = ATT_ALTCHARSET;
10677c478bdstevel@tonic-gate		break;
10687c478bdstevel@tonic-gate	default:
10697c478bdstevel@tonic-gate		return (set_errno(EINVAL));
10707c478bdstevel@tonic-gate	}
10717c478bdstevel@tonic-gate
10727c478bdstevel@tonic-gate	/*
10737c478bdstevel@tonic-gate	 * We need to flush the current buffer contents before calling
10747c478bdstevel@tonic-gate	 * IOP_SETATTR because IOP_SETATTR may need to synchronously output
10757c478bdstevel@tonic-gate	 * terminal escape sequences directly to the underlying device.
10767c478bdstevel@tonic-gate	 */
10777c478bdstevel@tonic-gate	(void) iob_write(iob, iob->iob_iop, iob->iob_buf, iob->iob_nbytes);
10787c478bdstevel@tonic-gate	iob->iob_bufp = &iob->iob_buf[0];
10797c478bdstevel@tonic-gate	iob->iob_nbytes = 0;
10807c478bdstevel@tonic-gate
10817c478bdstevel@tonic-gate	return (IOP_SETATTR(iob->iob_pgp, req, attr));
10827c478bdstevel@tonic-gate}
10837c478bdstevel@tonic-gate
10847c478bdstevel@tonic-gatestatic void
10857c478bdstevel@tonic-gateiob_bits2str(mdb_iob_t *iob, u_longlong_t value, const mdb_bitmask_t *bmp,
10867c478bdstevel@tonic-gate    mdb_bool_t altflag)
10877c478bdstevel@tonic-gate{
10887c478bdstevel@tonic-gate	mdb_bool_t delim = FALSE;
10897c478bdstevel@tonic-gate	const char *str;
10907c478bdstevel@tonic-gate	size_t width;
10917c478bdstevel@tonic-gate
10927c478bdstevel@tonic-gate	if (bmp == NULL)
10937c478bdstevel@tonic-gate		goto out;
10947c478bdstevel@tonic-gate
10957c478bdstevel@tonic-gate	for (; bmp->bm_name != NULL; bmp++) {
10967c478bdstevel@tonic-gate		if ((value & bmp->bm_mask) == bmp->bm_bits) {
10977c478bdstevel@tonic-gate			width = strlen(bmp->bm_name) + delim;
10987c478bdstevel@tonic-gate
10997c478bdstevel@tonic-gate			if (IOB_WRAPNOW(iob, width))
11007c478bdstevel@tonic-gate				mdb_iob_nl(iob);
11017c478bdstevel@tonic-gate
11027c478bdstevel@tonic-gate			if (delim)
11037c478bdstevel@tonic-gate				mdb_iob_putc(iob, ',');
11047c478bdstevel@tonic-gate			else
11057c478bdstevel@tonic-gate				delim = TRUE;
11067c478bdstevel@tonic-gate
11077c478bdstevel@tonic-gate			mdb_iob_puts(iob, bmp->bm_name);
11087c478bdstevel@tonic-gate			value &= ~bmp->bm_bits;
11097c478bdstevel@tonic-gate		}
11107c478bdstevel@tonic-gate	}
11117c478bdstevel@tonic-gate
11127c478bdstevel@tonic-gateout:
11137c478bdstevel@tonic-gate	if (altflag == TRUE && (delim == FALSE || value != 0)) {
11147c478bdstevel@tonic-gate		str = numtostr(value, 16, NTOS_UNSIGNED | NTOS_SHOWBASE);
11157c478bdstevel@tonic-gate		width = strlen(str) + delim;
11167c478bdstevel@tonic-gate
11177c478bdstevel@tonic-gate		if (IOB_WRAPNOW(iob, width))
11187c478bdstevel@tonic-gate			mdb_iob_nl(iob);
11197c478bdstevel@tonic-gate		if (delim)
11207c478bdstevel@tonic-gate			mdb_iob_putc(iob, ',');
11217c478bdstevel@tonic-gate		mdb_iob_puts(iob, str);
11227c478bdstevel@tonic-gate	}
11237c478bdstevel@tonic-gate}
11247c478bdstevel@tonic-gate
11257c478bdstevel@tonic-gatestatic const char *
11267c478bdstevel@tonic-gateiob_inaddr2str(uint32_t addr)
11277c478bdstevel@tonic-gate{
11287c478bdstevel@tonic-gate	static char buf[INET_ADDRSTRLEN];
11297c478bdstevel@tonic-gate
11307c478bdstevel@tonic-gate	(void) mdb_inet_ntop(AF_INET, &addr, buf, sizeof (buf));
11317c478bdstevel@tonic-gate
11327c478bdstevel@tonic-gate	return (buf);
11337c478bdstevel@tonic-gate}
11347c478bdstevel@tonic-gate
11357c478bdstevel@tonic-gatestatic const char *
11367c478bdstevel@tonic-gateiob_ipv6addr2str(void *addr)
11377c478bdstevel@tonic-gate{
11387c478bdstevel@tonic-gate	static char buf[INET6_ADDRSTRLEN];
11397c478bdstevel@tonic-gate
11407c478bdstevel@tonic-gate	(void) mdb_inet_ntop(AF_INET6, addr, buf, sizeof (buf));
11417c478bdstevel@tonic-gate
11427c478bdstevel@tonic-gate	return (buf);
11437c478bdstevel@tonic-gate}
11447c478bdstevel@tonic-gate
11457c478bdstevel@tonic-gatestatic const char *
11467c478bdstevel@tonic-gateiob_getvar(const char *s, size_t len)
11477c478bdstevel@tonic-gate{
11487c478bdstevel@tonic-gate	mdb_var_t *val;
11497c478bdstevel@tonic-gate	char *var;
11507c478bdstevel@tonic-gate
11517c478bdstevel@tonic-gate	if (len == 0) {
11527c478bdstevel@tonic-gate		(void) set_errno(EINVAL);
11537c478bdstevel@tonic-gate		return (NULL);
11547c478bdstevel@tonic-gate	}
11557c478bdstevel@tonic-gate
11567c478bdstevel@tonic-gate	var = strndup(s, len);
11577c478bdstevel@tonic-gate	val = mdb_nv_lookup(&mdb.m_nv, var);
11587c478bdstevel@tonic-gate	strfree(var);
11597c478bdstevel@tonic-gate
11607c478bdstevel@tonic-gate	if (val == NULL) {
11617c478bdstevel@tonic-gate		(void) set_errno(EINVAL);
11627c478bdstevel@tonic-gate		return (NULL);
11637c478bdstevel@tonic-gate	}
11647c478bdstevel@tonic-gate
11657c478bdstevel@tonic-gate	return (numtostr(mdb_nv_get_value(val), 10, 0));
11667c478bdstevel@tonic-gate}
11677c478bdstevel@tonic-gate
11687c478bdstevel@tonic-gate/*
11697c478bdstevel@tonic-gate * The iob_doprnt function forms the main engine of the debugger's output
11707c478bdstevel@tonic-gate * formatting capabilities.  Note that this is NOT exactly compatible with
11717c478bdstevel@tonic-gate * the printf(3S) family, nor is it intended to be so.  We support some
11727c478bdstevel@tonic-gate * extensions and format characters not supported by printf(3S), and we
11737c478bdstevel@tonic-gate * explicitly do NOT provide support for %C, %S, %ws (wide-character strings),
11747c478bdstevel@tonic-gate * do NOT provide for the complete functionality of %f, %e, %E, %g, %G
11757c478bdstevel@tonic-gate * (alternate double formats), and do NOT support %.x (precision specification).
11767c478bdstevel@tonic-gate * Note that iob_doprnt consumes varargs off the original va_list.
11777c478bdstevel@tonic-gate */
11787c478bdstevel@tonic-gatestatic void
11797c478bdstevel@tonic-gateiob_doprnt(mdb_iob_t *iob, const char *format, varglist_t *ap)
11807c478bdstevel@tonic-gate{
11817c478bdstevel@tonic-gate	char c[2] = { 0, 0 };	/* Buffer for single character output */
11827c478bdstevel@tonic-gate	const char *p;		/* Current position in format string */
11837c478bdstevel@tonic-gate	size_t len;		/* Length of format string to copy verbatim */
11847c478bdstevel@tonic-gate	size_t altlen;		/* Length of alternate print format prefix */
11857c478bdstevel@tonic-gate	const char *altstr;	/* Alternate print format prefix */
11867c478bdstevel@tonic-gate	const char *symstr;	/* Symbol + offset string */
11877c478bdstevel@tonic-gate
11887c478bdstevel@tonic-gate	u_longlong_t val;	/* Current integer value */
11897c478bdstevel@tonic-gate	intsize_t size;		/* Current integer value size */
11907c478bdstevel@tonic-gate	uint_t flags;		/* Current flags to pass to iob_int2str */
11917c478bdstevel@tonic-gate	size_t width;		/* Current field width */
11927c478bdstevel@tonic-gate	int zero;		/* If != 0, then integer value == 0 */
11937c478bdstevel@tonic-gate
11947c478bdstevel@tonic-gate	mdb_bool_t f_alt;	/* Use alternate print format (%#) */
11957c478bdstevel@tonic-gate	mdb_bool_t f_altsuff;	/* Alternate print format is a suffix */
11967c478bdstevel@tonic-gate	mdb_bool_t f_zfill;	/* Zero-fill field (%0) */
11977c478bdstevel@tonic-gate	mdb_bool_t f_left;	/* Left-adjust field (%-) */
11987c478bdstevel@tonic-gate	mdb_bool_t f_digits;	/* Explicit digits used to set field width */
11997c478bdstevel@tonic-gate
12007c478bdstevel@tonic-gate	union {
12017c478bdstevel@tonic-gate		const char *str;
12027c478bdstevel@tonic-gate		uint32_t ui32;
12037c478bdstevel@tonic-gate		void *ptr;
12047c478bdstevel@tonic-gate		time_t tm;
12057c478bdstevel@tonic-gate		char c;
12067c478bdstevel@tonic-gate		double d;
12077c478bdstevel@tonic-gate		long double ld;
12087c478bdstevel@tonic-gate	} u;
12097c478bdstevel@tonic-gate
12107c478bdstevel@tonic-gate	ASSERT(iob->iob_flags & MDB_IOB_WRONLY);
12117c478bdstevel@tonic-gate
12127c478bdstevel@tonic-gate	while ((p = strchr(format, '%')) != NULL) {
12137c478bdstevel@tonic-gate		/*
12147c478bdstevel@tonic-gate		 * Output the format string verbatim up to the next '%' char
12157c478bdstevel@tonic-gate		 */
12167c478bdstevel@tonic-gate		if (p != format) {
12177c478bdstevel@tonic-gate			len = p - format;
12187c478bdstevel@tonic-gate			if (IOB_WRAPNOW(iob, len) && *format != '\n')
12197c478bdstevel@tonic-gate				mdb_iob_nl(iob);
12207c478bdstevel@tonic-gate			mdb_iob_nputs(iob, format, len);
12217c478bdstevel@tonic-gate		}
12227c478bdstevel@tonic-gate
12237c478bdstevel@tonic-gate		/*
12247c478bdstevel@tonic-gate		 * Now we need to parse the sequence of format characters
12257c478bdstevel@tonic-gate		 * following the % marker and do the appropriate thing.
12267c478bdstevel@tonic-gate		 */
12277c478bdstevel@tonic-gate		size = SZ_INT;		/* Use normal-sized int by default */
12287c478bdstevel@tonic-gate		flags = 0;		/* Clear numtostr() format flags */
12297c478bdstevel@tonic-gate		width = 0;		/* No field width limit by default */
12307c478bdstevel@tonic-gate		altlen = 0;		/* No alternate format string yet */
12317c478bdstevel@tonic-gate		altstr = NULL;		/* No alternate format string yet */
12327c478bdstevel@tonic-gate
12337c478bdstevel@tonic-gate		f_alt = FALSE;		/* Alternate format off by default */
12347c478bdstevel@tonic-gate		f_altsuff = FALSE;	/* Alternate format is a prefix */
12357c478bdstevel@tonic-gate		f_zfill = FALSE;	/* Zero-fill off by default */
12367c478bdstevel@tonic-gate		f_left = FALSE;		/* Left-adjust off by default */
12377c478bdstevel@tonic-gate		f_digits = FALSE;	/* No digits for width specified yet */
12387c478bdstevel@tonic-gate
12397c478bdstevel@tonic-gate		fmt_switch:
12407c478bdstevel@tonic-gate		switch (*++p) {
12417c478bdstevel@tonic-gate		case '0': case '1': case '2': case '3': case '4':
12427c478bdstevel@tonic-gate		case '5': case '6': case '7': case '8': case '9':
12437c478bdstevel@tonic-gate			if (f_digits == FALSE && *p == '0') {
12447c478bdstevel@tonic-gate				f_zfill = TRUE;
12457c478bdstevel@tonic-gate				goto fmt_switch;
12467c478bdstevel@tonic-gate			}
12477c478bdstevel@tonic-gate
12487c478bdstevel@tonic-gate			if (f_digits == FALSE)
12497c478bdstevel@tonic-gate				width = 0; /* clear any other width specifier */
12507c478bdstevel@tonic-gate
12517c478bdstevel@tonic-gate			for (u.c = *p; u.c >= '0' && u.c <= '9'; u.c = *++p)
12527c478bdstevel@tonic-gate				width = width * 10 + u.c - '0';
12537c478bdstevel@tonic-gate
12547c478bdstevel@tonic-gate			p--;
12557c478bdstevel@tonic-gate			f_digits = TRUE;
12567c478bdstevel@tonic-gate			goto fmt_switch;
12577c478bdstevel@tonic-gate
12587c478bdstevel@tonic-gate		case 'a':
12597c478bdstevel@tonic-gate			if (size < SZ_LONG)
12607c478bdstevel@tonic-gate				size = SZ_LONG;	/* Bump to size of uintptr_t */
12617c478bdstevel@tonic-gate
12627c478bdstevel@tonic-gate			u.str = iob_int2str(ap, size, 16,
12637c478bdstevel@tonic-gate			    NTOS_UNSIGNED | NTOS_SHOWBASE, &zero, &val);
12647c478bdstevel@tonic-gate
12657c478bdstevel@tonic-gate			if ((symstr = iob_addr2str(val)) != NULL)
12667c478bdstevel@tonic-gate				u.str = symstr;
12677c478bdstevel@tonic-gate
12687c478bdstevel@tonic-gate			if (f_alt == TRUE) {
12697c478bdstevel@tonic-gate				f_altsuff = TRUE;
12707c478bdstevel@tonic-gate				altstr = ":";
12717c478bdstevel@tonic-gate				altlen = 1;
12727c478bdstevel@tonic-gate			}
12737c478bdstevel@tonic-gate			break;
12747c478bdstevel@tonic-gate
12757c478bdstevel@tonic-gate		case 'A':
12767c478bdstevel@tonic-gate			if (size < SZ_LONG)
12777c478bdstevel@tonic-gate				size = SZ_LONG;	/* Bump to size of uintptr_t */
12787c478bdstevel@tonic-gate
12797c478bdstevel@tonic-gate			(void) iob_int2str(ap, size, 16,
12807c478bdstevel@tonic-gate			    NTOS_UNSIGNED, &zero, &val);
12817c478bdstevel@tonic-gate
12827c478bdstevel@tonic-gate			u.str = iob_addr2str(val);
12837c478bdstevel@tonic-gate
12847c478bdstevel@tonic-gate			if (f_alt == TRUE && u.str == NULL)
12857c478bdstevel@tonic-gate				u.str = "?";
12867c478bdstevel@tonic-gate			break;
12877c478bdstevel@tonic-gate
12887c478bdstevel@tonic-gate		case 'b':
12897c478bdstevel@tonic-gate			u.str = iob_int2str(ap, size, 16,
12907c478bdstevel@tonic-gate			    NTOS_UNSIGNED | NTOS_SHOWBASE, &zero, &val);
12917c478bdstevel@tonic-gate
12927c478bdstevel@tonic-gate			iob_bits2str(iob, val, VA_PTRARG(ap), f_alt);
12937c478bdstevel@tonic-gate
12947c478bdstevel@tonic-gate			format = ++p;
12957c478bdstevel@tonic-gate			continue;
12967c478bdstevel@tonic-gate
12977c478bdstevel@tonic-gate		case 'c':
12987c478bdstevel@tonic-gate			c[0] = (char)VA_ARG(ap, int);
12997c478bdstevel@tonic-gate			u.str = c;
13007c478bdstevel@tonic-gate			break;
13017c478bdstevel@tonic-gate
13027c478bdstevel@tonic-gate		case 'd':
13037c478bdstevel@tonic-gate		case 'i':
13047c478bdstevel@tonic-gate			if (f_alt)
13057c478bdstevel@tonic-gate				flags |= NTOS_SHOWBASE;
13067c478bdstevel@tonic-gate			u.str = iob_int2str(ap, size, 10, flags, &zero, &val);
13077c478bdstevel@tonic-gate			break;
13087c478bdstevel@tonic-gate
13097c478bdstevel@tonic-gate		/* No floating point in kmdb */
13107c478bdstevel@tonic-gate#ifndef _KMDB
13117c478bdstevel@tonic-gate		case 'e':
13127c478bdstevel@tonic-gate		case 'E':
13137c478bdstevel@tonic-gate			u.d = VA_ARG(ap, double);
13147c478bdstevel@tonic-gate			u.str = doubletos(u.d, 7, *p);
13157c478bdstevel@tonic-gate			break;
13167c478bdstevel@tonic-gate
13177c478bdstevel@tonic-gate		case 'g':
13187c478bdstevel@tonic-gate		case 'G':
13197c478bdstevel@tonic-gate			if (size >= SZ_LONG) {
13207c478bdstevel@tonic-gate				u.ld = VA_ARG(ap, long double);
13217c478bdstevel@tonic-gate				u.str = longdoubletos(&u.ld, 16,
13227c478bdstevel@tonic-gate				    (*p == 'g') ? 'e' : 'E');
13237c478bdstevel@tonic-gate			} else {
13247c478bdstevel@tonic-gate				u.d = VA_ARG(ap, double);
13257c478bdstevel@tonic-gate				u.str = doubletos(u.d, 16,
13267c478bdstevel@tonic-gate				    (*p == 'g') ? 'e' : 'E');
13277c478bdstevel@tonic-gate			}
13287c478bdstevel@tonic-gate			break;
13297c478bdstevel@tonic-gate#endif
13307c478bdstevel@tonic-gate
13317c478bdstevel@tonic-gate		case 'h':
13327c478bdstevel@tonic-gate			size = SZ_SHORT;
13337c478bdstevel@tonic-gate			goto fmt_switch;
13347c478bdstevel@tonic-gate
1335cab8de1Bryan Cantrill		case 'H':
1336cab8de1Bryan Cantrill			u.str = iob_bytes2str(ap, size);
1337cab8de1Bryan Cantrill			break;
1338cab8de1Bryan Cantrill
13397c478bdstevel@tonic-gate		case 'I':
13407c478bdstevel@tonic-gate			u.ui32 = VA_ARG(ap, uint32_t);
13417c478bdstevel@tonic-gate			u.str = iob_inaddr2str(u.ui32);
13427c478bdstevel@tonic-gate			break;
13437c478bdstevel@tonic-gate
13447c478bdstevel@tonic-gate		case 'l':
13457c478bdstevel@tonic-gate			if (size >= SZ_LONG)
13467c478bdstevel@tonic-gate				size = SZ_LONGLONG;
13477c478bdstevel@tonic-gate			else
13487c478bdstevel@tonic-gate				size = SZ_LONG;
13497c478bdstevel@tonic-gate			goto fmt_switch;
13507c478bdstevel@tonic-gate
13517c478bdstevel@tonic-gate		case 'm':
13527c478bdstevel@tonic-gate			if (iob->iob_nbytes == 0) {
13537c478bdstevel@tonic-gate				mdb_iob_ws(iob, (width != 0) ? width :
13547c478bdstevel@tonic-gate				    iob->iob_margin);
13557c478bdstevel@tonic-gate			}
13567c478bdstevel@tonic-gate			format = ++p;
13577c478bdstevel@tonic-gate			continue;
13587c478bdstevel@tonic-gate
13597c478bdstevel@tonic-gate		case 'N':
13607c478bdstevel@tonic-gate			u.ptr = VA_PTRARG(ap);
13617c478bdstevel@tonic-gate			u.str = iob_ipv6addr2str(u.ptr);
13627c478bdstevel@tonic-gate			break;
13637c478bdstevel@tonic-gate
13647c478bdstevel@tonic-gate		case 'o':
13657c478bdstevel@tonic-gate			u.str = iob_int2str(ap, size, 8, NTOS_UNSIGNED,
13667c478bdstevel@tonic-gate			    &zero, &val);
13677c478bdstevel@tonic-gate
13687c478bdstevel@tonic-gate			if (f_alt && !zero) {
13697c478bdstevel@tonic-gate				altstr = "0";
13707c478bdstevel@tonic-gate				altlen = 1;
13717c478bdstevel@tonic-gate			}
13727c478bdstevel@tonic-gate			break;
13737c478bdstevel@tonic-gate
13747c478bdstevel@tonic-gate		case 'p':
13757c478bdstevel@tonic-gate			u.ptr = VA_PTRARG(ap);
13767c478bdstevel@tonic-gate			u.str = numtostr((uintptr_t)u.ptr, 16, NTOS_UNSIGNED);
13777c478bdstevel@tonic-gate			break;
13787c478bdstevel@tonic-gate
13797c478bdstevel@tonic-gate		case 'q':
13807c478bdstevel@tonic-gate			u.str = iob_int2str(ap, size, 8, flags, &zero, &val);
13817c478bdstevel@tonic-gate
13827c478bdstevel@tonic-gate			if (f_alt && !zero) {
13837c478bdstevel@tonic-gate				altstr = "0";
13847c478bdstevel@tonic-gate				altlen = 1;
13857c478bdstevel@tonic-gate			}
13867c478bdstevel@tonic-gate			break;
13877c478bdstevel@tonic-gate
13887c478bdstevel@tonic-gate		case 'r':
13897c478bdstevel@tonic-gate			if (f_alt)
13907c478bdstevel@tonic-gate				flags |= NTOS_SHOWBASE;
13917c478bdstevel@tonic-gate			u.str = iob_int2str(ap, size, mdb.m_radix,
13927c478bdstevel@tonic-gate			    NTOS_UNSIGNED | flags, &zero, &val);
13937c478bdstevel@tonic-gate			break;
13947c478bdstevel@tonic-gate
13957c478bdstevel@tonic-gate		case 'R':
13967c478bdstevel@tonic-gate			if (f_alt)
13977c478bdstevel@tonic-gate				flags |= NTOS_SHOWBASE;
13987c478bdstevel@tonic-gate			u.str = iob_int2str(ap, size, mdb.m_radix, flags,
13997c478bdstevel@tonic-gate			    &zero, &val);
14007c478bdstevel@tonic-gate			break;
14017c478bdstevel@tonic-gate
14027c478bdstevel@tonic-gate		case 's':
14037c478bdstevel@tonic-gate			u.str = VA_PTRARG(ap);
14047c478bdstevel@tonic-gate			if (u.str == NULL)
14057c478bdstevel@tonic-gate				u.str = "<NULL>"; /* Be forgiving of NULL */
14067c478bdstevel@tonic-gate			break;
14077c478bdstevel@tonic-gate
14087c478bdstevel@tonic-gate		case 't':
14097c478bdstevel@tonic-gate			if (width != 0) {
14107c478bdstevel@tonic-gate				while (width-- > 0)
14117c478bdstevel@tonic-gate					mdb_iob_tab(iob);
14127c478bdstevel@tonic-gate			} else
14137c478bdstevel@tonic-gate				mdb_iob_tab(iob);
14147c478bdstevel@tonic-gate
14157c478bdstevel@tonic-gate			format = ++p;
14167c478bdstevel@tonic-gate			continue;
14177c478bdstevel@tonic-gate
14187c478bdstevel@tonic-gate		case 'T':
14197c478bdstevel@tonic-gate			if (width != 0 && (iob->iob_nbytes % width) != 0) {
14207c478bdstevel@tonic-gate				size_t ots = iob->iob_tabstop;
14217c478bdstevel@tonic-gate				iob->iob_tabstop = width;
14227c478bdstevel@tonic-gate				mdb_iob_tab(iob);
14237c478bdstevel@tonic-gate				iob->iob_tabstop = ots;
14247c478bdstevel@tonic-gate			}
14257c478bdstevel@tonic-gate			format = ++p;
14267c478bdstevel@tonic-gate			continue;
14277c478bdstevel@tonic-gate
14287c478bdstevel@tonic-gate		case 'u':
14297c478bdstevel@tonic-gate			if (f_alt)
14307c478bdstevel@tonic-gate				flags |= NTOS_SHOWBASE;
14317c478bdstevel@tonic-gate			u.str = iob_int2str(ap, size, 10,
14327c478bdstevel@tonic-gate			    flags | NTOS_UNSIGNED, &zero, &val);
14337c478bdstevel@tonic-gate			break;
14347c478bdstevel@tonic-gate
14357c478bdstevel@tonic-gate		case 'x':
14367c478bdstevel@tonic-gate			u.str = iob_int2str(ap, size, 16, NTOS_UNSIGNED,
14377c478bdstevel@tonic-gate			    &zero, &val);
14387c478bdstevel@tonic-gate
14397c478bdstevel@tonic-gate			if (f_alt && !zero) {
14407c478bdstevel@tonic-gate				altstr = "0x";
14417c478bdstevel@tonic-gate				altlen = 2;
14427c478bdstevel@tonic-gate			}
14437c478bdstevel@tonic-gate			break;
14447c478bdstevel@tonic-gate
14457c478bdstevel@tonic-gate		case 'X':
14467c478bdstevel@tonic-gate			u.str = iob_int2str(ap, size, 16,
14477c478bdstevel@tonic-gate			    NTOS_UNSIGNED | NTOS_UPCASE, &zero, &val);
14487c478bdstevel@tonic-gate
14497c478bdstevel@tonic-gate			if (f_alt && !zero) {
14507c478bdstevel@tonic-gate				altstr = "0X";
14517c478bdstevel@tonic-gate				altlen = 2;
14527c478bdstevel@tonic-gate			}
14537c478bdstevel@tonic-gate			break;
14547c478bdstevel@tonic-gate
14557c478bdstevel@tonic-gate		case 'Y':
14567c478bdstevel@tonic-gate			u.tm = VA_ARG(ap, time_t);
14577c478bdstevel@tonic-gate			u.str = iob_time2str(&u.tm);
14587c478bdstevel@tonic-gate			break;
14597c478bdstevel@tonic-gate
14607c478bdstevel@tonic-gate		case '<':
14617c478bdstevel@tonic-gate			/*
14627c478bdstevel@tonic-gate			 * Used to turn attributes on (<b>), to turn them
14637c478bdstevel@tonic-gate			 * off (</b>), or to print variables (<_var>).
14647c478bdstevel@tonic-gate			 */
14657c478bdstevel@tonic-gate			for (u.str = ++p; *p != '\0' && *p != '>'; p++)
14667c478bdstevel@tonic-gate				continue;
14677c478bdstevel@tonic-gate
14687c478bdstevel@tonic-gate			if (*p == '>') {
14697c478bdstevel@tonic-gate				size_t paramlen = p - u.str;
14707c478bdstevel@tonic-gate
14717c478bdstevel@tonic-gate				if (paramlen > 0) {
14727c478bdstevel@tonic-gate					if (*u.str == '_') {
14737c478bdstevel@tonic-gate						u.str = iob_getvar(u.str + 1,
14747c478bdstevel@tonic-gate						    paramlen - 1);
14757c478bdstevel@tonic-gate						break;
14767c478bdstevel@tonic-gate					} else {
14777c478bdstevel@tonic-gate						(void) iob_setattr(iob, u.str,
14787c478bdstevel@tonic-gate						    paramlen);
14797c478bdstevel@tonic-gate					}
14807c478bdstevel@tonic-gate				}
14817c478bdstevel@tonic-gate
14827c478bdstevel@tonic-gate				p++;
14837c478bdstevel@tonic-gate			}
14847c478bdstevel@tonic-gate
14857c478bdstevel@tonic-gate			format = p;
14867c478bdstevel@tonic-gate			continue;
14877c478bdstevel@tonic-gate
14887c478bdstevel@tonic-gate		case '*':
14897c478bdstevel@tonic-gate			width = (size_t)(uint_t)VA_ARG(ap, int);
14907c478bdstevel@tonic-gate			goto fmt_switch;
14917c478bdstevel@tonic-gate
14927c478bdstevel@tonic-gate		case '%':
14937c478bdstevel@tonic-gate			u.str = "%";
14947c478bdstevel@tonic-gate			break;
14957c478bdstevel@tonic-gate
14967c478bdstevel@tonic-gate		case '?':
14977c478bdstevel@tonic-gate			width = sizeof (uintptr_t) * 2;
14987c478bdstevel@tonic-gate			goto fmt_switch;
14997c478bdstevel@tonic-gate
15007c478bdstevel@tonic-gate		case '#':
15017c478bdstevel@tonic-gate			f_alt = TRUE;
15027c478bdstevel@tonic-gate			goto fmt_switch;
15037c478bdstevel@tonic-gate
15047c478bdstevel@tonic-gate		case '+':
15057c478bdstevel@tonic-gate			flags |= NTOS_SIGNPOS;
15067c478bdstevel@tonic-gate			goto fmt_switch;
15077c478bdstevel@tonic-gate
15087c478bdstevel@tonic-gate		case '-':
15097c478bdstevel@tonic-gate			f_left = TRUE;
15107c478bdstevel@tonic-gate			goto fmt_switch;
15117c478bdstevel@tonic-gate
15127c478bdstevel@tonic-gate		default:
15137c478bdstevel@tonic-gate			c[0] = p[0];
15147c478bdstevel@tonic-gate			u.str = c;
15157c478bdstevel@tonic-gate		}
15167c478bdstevel@tonic-gate
15177c478bdstevel@tonic-gate		len = u.str != NULL ? strlen(u.str) : 0;
15187c478bdstevel@tonic-gate
15197c478bdstevel@tonic-gate		if (len + altlen > width)
15207c478bdstevel@tonic-gate			width = len + altlen;
15217c478bdstevel@tonic-gate
15227c478bdstevel@tonic-gate		/*
15237c478bdstevel@tonic-gate		 * If the string and the option altstr won't fit on this line
15247c478bdstevel@tonic-gate		 * and auto-wrap is set (default), skip to the next line.
1525218912fSteve Gonczi		 * If the string contains \n, and the \n terminated substring
1526218912fSteve Gonczi		 * + altstr is shorter than the above, use the shorter lf_len.
15277c478bdstevel@tonic-gate		 */
1528218912fSteve Gonczi		if (u.str != NULL) {
1529218912fSteve Gonczi			char *np = strchr(u.str, '\n');
1530218912fSteve Gonczi			if (np != NULL) {
1531218912fSteve Gonczi				int lf_len = (np - u.str) + altlen;
1532218912fSteve Gonczi				if (lf_len < width)
1533218912fSteve Gonczi					width = lf_len;
1534218912fSteve Gonczi			}
1535218912fSteve Gonczi		}
15367c478bdstevel@tonic-gate		if (IOB_WRAPNOW(iob, width))
15377c478bdstevel@tonic-gate			mdb_iob_nl(iob);
15387c478bdstevel@tonic-gate
15397c478bdstevel@tonic-gate		/*
15407c478bdstevel@tonic-gate		 * Optionally add whitespace or zeroes prefixing the value if
15417c478bdstevel@tonic-gate		 * we haven't filled the minimum width and we're right-aligned.
15427c478bdstevel@tonic-gate		 */
15437c478bdstevel@tonic-gate		if (len < (width - altlen) && f_left == FALSE) {
15447c478bdstevel@tonic-gate			mdb_iob_fill(iob, f_zfill ? '0' : ' ',
15457c478bdstevel@tonic-gate			    width - altlen - len);
15467c478bdstevel@tonic-gate		}
15477c478bdstevel@tonic-gate
15487c478bdstevel@tonic-gate		/*
15497c478bdstevel@tonic-gate		 * Print the alternate string if it's a prefix, and then
15507c478bdstevel@tonic-gate		 * print the value string itself.
15517c478bdstevel@tonic-gate		 */
15527c478bdstevel@tonic-gate		if (altstr != NULL && f_altsuff == FALSE)
15537c478bdstevel@tonic-gate			mdb_iob_nputs(iob, altstr, altlen);
15547c478bdstevel@tonic-gate		if (len != 0)
15557c478bdstevel@tonic-gate			mdb_iob_nputs(iob, u.str, len);
15567c478bdstevel@tonic-gate
15577c478bdstevel@tonic-gate		/*
15587c478bdstevel@tonic-gate		 * If we have an alternate string and it's a suffix, print it.
15597c478bdstevel@tonic-gate		 */
15607c478bdstevel@tonic-gate		if (altstr != NULL && f_altsuff == TRUE)
15617c478bdstevel@tonic-gate			mdb_iob_nputs(iob, altstr, altlen);
15627c478bdstevel@tonic-gate
15637c478bdstevel@tonic-gate		/*
15647c478bdstevel@tonic-gate		 * Finally, if we haven't filled the field width and we're
15657c478bdstevel@tonic-gate		 * left-aligned, pad out the rest with whitespace.
15667c478bdstevel@tonic-gate		 */
15677c478bdstevel@tonic-gate		if ((len + altlen) < width && f_left == TRUE)
15687c478bdstevel@tonic-gate			mdb_iob_ws(iob, width - altlen - len);
15697c478bdstevel@tonic-gate
15707c478bdstevel@tonic-gate		format = (*p != '\0') ? ++p : p;
15717c478bdstevel@tonic-gate	}
15727c478bdstevel@tonic-gate
15737c478bdstevel@tonic-gate	/*
15747c478bdstevel@tonic-gate	 * If there's anything left in the format string, output it now
15757c478bdstevel@tonic-gate	 */
15767c478bdstevel@tonic-gate	if (*format != '\0') {
15777c478bdstevel@tonic-gate		len = strlen(format);
15787c478bdstevel@tonic-gate		if (IOB_WRAPNOW(iob, len) && *format != '\n')
15797c478bdstevel@tonic-gate			mdb_iob_nl(iob);
15807c478bdstevel@tonic-gate		mdb_iob_nputs(iob, format, len);
15817c478bdstevel@tonic-gate	}
15827c478bdstevel@tonic-gate}
15837c478bdstevel@tonic-gate
15847c478bdstevel@tonic-gatevoid
15857c478bdstevel@tonic-gatemdb_iob_vprintf(mdb_iob_t *iob, const char *format, va_list alist)
15867c478bdstevel@tonic-gate{
15877c478bdstevel@tonic-gate	varglist_t ap = { VAT_VARARGS };
15887c478bdstevel@tonic-gate	va_copy(ap.val_valist, alist);
15897c478bdstevel@tonic-gate	iob_doprnt(iob, format, &ap);
15907c478bdstevel@tonic-gate}
15917c478bdstevel@tonic-gate
15927c478bdstevel@tonic-gatevoid
15937c478bdstevel@tonic-gatemdb_iob_aprintf(mdb_iob_t *iob, const char *format, const mdb_arg_t *argv)
15947c478bdstevel@tonic-gate{
15957c478bdstevel@tonic-gate	varglist_t ap = { VAT_ARGVEC };
15967c478bdstevel@tonic-gate	ap.val_argv = argv;
15977c478bdstevel@tonic-gate	iob_doprnt(iob, format, &ap);
15987c478bdstevel@tonic-gate}
15997c478bdstevel@tonic-gate
16007c478bdstevel@tonic-gatevoid
16017c478bdstevel@tonic-gatemdb_iob_printf(mdb_iob_t *iob, const char *format, ...)
16027c478bdstevel@tonic-gate{
16037c478bdstevel@tonic-gate	va_list alist;
16047c478bdstevel@tonic-gate
16057c478bdstevel@tonic-gate	va_start(alist, format);
16067c478bdstevel@tonic-gate	mdb_iob_vprintf(iob, format, alist);
16077c478bdstevel@tonic-gate	va_end(alist);
16087c478bdstevel@tonic-gate}
16097c478bdstevel@tonic-gate
16107c478bdstevel@tonic-gate/*
16117c478bdstevel@tonic-gate * In order to handle the sprintf family of functions, we define a special
16127c478bdstevel@tonic-gate * i/o backend known as a "sprintf buf" (or spbuf for short).  This back end
16137c478bdstevel@tonic-gate * provides an IOP_WRITE entry point that concatenates each buffer sent from
16147c478bdstevel@tonic-gate * mdb_iob_flush() onto the caller's buffer until the caller's buffer is
16157c478bdstevel@tonic-gate * exhausted.  We also keep an absolute count of how many bytes were sent to
16167c478bdstevel@tonic-gate * this function during the lifetime of the snprintf call.  This allows us
16177c478bdstevel@tonic-gate * to provide the ability to (1) return the total size required for the given
16187c478bdstevel@tonic-gate * format string and argument list, and (2) support a call to snprintf with a
16197c478bdstevel@tonic-gate * NULL buffer argument with no special case code elsewhere.
16207c478bdstevel@tonic-gate */
16217c478bdstevel@tonic-gatestatic ssize_t
16227c478bdstevel@tonic-gatespbuf_write(mdb_io_t *io, const void *buf, size_t buflen)
16237c478bdstevel@tonic-gate{
16247c478bdstevel@tonic-gate	spbuf_t *spb = io->io_data;
16257c478bdstevel@tonic-gate
16267c478bdstevel@tonic-gate	if (spb->spb_bufsiz != 0) {
16277c478bdstevel@tonic-gate		size_t n = MIN(spb->spb_bufsiz, buflen);
16287c478bdstevel@tonic-gate		bcopy(buf, spb->spb_buf, n);
16297c478bdstevel@tonic-gate		spb->spb_buf += n;
16307c478bdstevel@tonic-gate		spb->spb_bufsiz -= n;
16317c478bdstevel@tonic-gate	}
16327c478bdstevel@tonic-gate
16337c478bdstevel@tonic-gate	spb->spb_total += buflen;
16347c478bdstevel@tonic-gate	return (buflen);
16357c478bdstevel@tonic-gate}
16367c478bdstevel@tonic-gate
16377c478bdstevel@tonic-gatestatic const mdb_io_ops_t spbuf_ops = {
16387c478bdstevel@tonic-gate	no_io_read,
16397c478bdstevel@tonic-gate	spbuf_write,
16407c478bdstevel@tonic-gate	no_io_seek,
16417c478bdstevel@tonic-gate	no_io_ctl,
16427c478bdstevel@tonic-gate	no_io_close,
16437c478bdstevel@tonic-gate	no_io_name,
16447c478bdstevel@tonic-gate	no_io_link,
16457c478bdstevel@tonic-gate	no_io_unlink,
16467c478bdstevel@tonic-gate	no_io_setattr,
16477c478bdstevel@tonic-gate	no_io_suspend,
16487c478bdstevel@tonic-gate	no_io_resume
16497c478bdstevel@tonic-gate};
16507c478bdstevel@tonic-gate
16517c478bdstevel@tonic-gate/*
16527c478bdstevel@tonic-gate * The iob_spb_create function initializes an iob suitable for snprintf calls,
16537c478bdstevel@tonic-gate * a spbuf i/o backend, and the spbuf private data, and then glues these
16547c478bdstevel@tonic-gate * objects together.  The caller (either vsnprintf or asnprintf below) is
16557c478bdstevel@tonic-gate * expected to have allocated the various structures on their stack.
16567c478bdstevel@tonic-gate */
16577c478bdstevel@tonic-gatestatic void
16587c478bdstevel@tonic-gateiob_spb_create(mdb_iob_t *iob, char *iob_buf, size_t iob_len,
16597c478bdstevel@tonic-gate    mdb_io_t *io, spbuf_t *spb, char *spb_buf, size_t spb_len)
16607c478bdstevel@tonic-gate{
16617c478bdstevel@tonic-gate	spb->spb_buf = spb_buf;
16627c478bdstevel@tonic-gate	spb->spb_bufsiz = spb_len;
16637c478bdstevel@tonic-gate	spb->spb_total = 0;
16647c478bdstevel@tonic-gate
16657c478bdstevel@tonic-gate	io->io_ops = &spbuf_ops;
16667c478bdstevel@tonic-gate	io->io_data = spb;
16677c478bdstevel@tonic-gate	io->io_next = NULL;
16687c478bdstevel@tonic-gate	io->io_refcnt = 1;
16697c478bdstevel@tonic-gate
16707c478bdstevel@tonic-gate	iob->iob_buf = iob_buf;
16717c478bdstevel@tonic-gate	iob->iob_bufsiz = iob_len;
16727c478bdstevel@tonic-gate	iob->iob_bufp = iob_buf;
16737c478bdstevel@tonic-gate	iob->iob_nbytes = 0;
16747c478bdstevel@tonic-gate	iob->iob_nlines = 0;
16757c478bdstevel@tonic-gate	iob->iob_lineno = 1;
16767c478bdstevel@tonic-gate	iob->iob_rows = MDB_IOB_DEFROWS;
16777c478bdstevel@tonic-gate	iob->iob_cols = iob_len;
16787c478bdstevel@tonic-gate	iob->iob_tabstop = MDB_IOB_DEFTAB;
16797c478bdstevel@tonic-gate	iob->iob_margin = MDB_IOB_DEFMARGIN;
16807c478bdstevel@tonic-gate	iob->iob_flags = MDB_IOB_WRONLY;
16817c478bdstevel@tonic-gate	iob->iob_iop = io;
16827c478bdstevel@tonic-gate	iob->iob_pgp = NULL;
16837c478bdstevel@tonic-gate	iob->iob_next = NULL;
16847c478bdstevel@tonic-gate}
16857c478bdstevel@tonic-gate
16867c478bdstevel@tonic-gate/*ARGSUSED*/
16877c478bdstevel@tonic-gatessize_t
16887c478bdstevel@tonic-gatenull_io_write(mdb_io_t *io, const void *buf, size_t nbytes)
16897c478bdstevel@tonic-gate{
16907c478bdstevel@tonic-gate	return (nbytes);
16917c478bdstevel@tonic-gate}
16927c478bdstevel@tonic-gate
16937c478bdstevel@tonic-gatestatic const mdb_io_ops_t null_ops = {
16947c478bdstevel@tonic-gate	no_io_read,
16957c478bdstevel@tonic-gate	null_io_write,
16967c478bdstevel@tonic-gate	no_io_seek,
16977c478bdstevel@tonic-gate	no_io_ctl,
16987c478bdstevel@tonic-gate	no_io_close,
16997c478bdstevel@tonic-gate	no_io_name,
17007c478bdstevel@tonic-gate	no_io_link,
17017c478bdstevel@tonic-gate	no_io_unlink,
17027c478bdstevel@tonic-gate	no_io_setattr,
17037c478bdstevel@tonic-gate	no_io_suspend,
17047c478bdstevel@tonic-gate	no_io_resume
17057c478bdstevel@tonic-gate};
17067c478bdstevel@tonic-gate
17077c478bdstevel@tonic-gatemdb_io_t *
17087c478bdstevel@tonic-gatemdb_nullio_create(void)
17097c478bdstevel@tonic-gate{
17107c478bdstevel@tonic-gate	static mdb_io_t null_io = {
17117c478bdstevel@tonic-gate		&null_ops,
17127c478bdstevel@tonic-gate		NULL,
17137c478bdstevel@tonic-gate		NULL,
17147c478bdstevel@tonic-gate		1
17157c478bdstevel@tonic-gate	};
17167c478bdstevel@tonic-gate
17177c478bdstevel@tonic-gate	return (&null_io);
17187c478bdstevel@tonic-gate}
17197c478bdstevel@tonic-gate
17207c478bdstevel@tonic-gatesize_t
17217c478bdstevel@tonic-gatemdb_iob_vsnprintf(char *buf, size_t nbytes, const char *format, va_list alist)
17227c478bdstevel@tonic-gate{
17237c478bdstevel@tonic-gate	varglist_t ap = { VAT_VARARGS };
17247c478bdstevel@tonic-gate	char iob_buf[64];
17257c478bdstevel@tonic-gate	mdb_iob_t iob;
17267c478bdstevel@tonic-gate	mdb_io_t io;
17277c478bdstevel@tonic-gate	spbuf_t spb;
17287c478bdstevel@tonic-gate
17297c478bdstevel@tonic-gate	ASSERT(buf != NULL || nbytes == 0);
17307c478bdstevel@tonic-gate	iob_spb_create(&iob, iob_buf, sizeof (iob_buf), &io, &spb, buf, nbytes);
17317c478bdstevel@tonic-gate	va_copy(ap.val_valist, alist);
17327c478bdstevel@tonic-gate	iob_doprnt(&iob, format, &ap);
17337c478bdstevel@tonic-gate	mdb_iob_flush(&iob);
17347c478bdstevel@tonic-gate
17357c478bdstevel@tonic-gate	if (spb.spb_bufsiz != 0)
17367c478bdstevel@tonic-gate		*spb.spb_buf = '\0';
17377c478bdstevel@tonic-gate	else if (buf != NULL && nbytes > 0)
17387c478bdstevel@tonic-gate		*--spb.spb_buf = '\0';
17397c478bdstevel@tonic-gate
17407c478bdstevel@tonic-gate	return (spb.spb_total);
17417c478bdstevel@tonic-gate}
17427c478bdstevel@tonic-gate
17437c478bdstevel@tonic-gatesize_t
17447c478bdstevel@tonic-gatemdb_iob_asnprintf(char *buf, size_t nbytes, const char *format,
17457c478bdstevel@tonic-gate    const mdb_arg_t *argv)
17467c478bdstevel@tonic-gate{
17477c478bdstevel@tonic-gate	varglist_t ap = { VAT_ARGVEC };
17487c478bdstevel@tonic-gate	char iob_buf[64];
17497c478bdstevel@tonic-gate	mdb_iob_t iob;
17507c478bdstevel@tonic-gate	mdb_io_t io;
17517c478bdstevel@tonic-gate	spbuf_t spb;
17527c478bdstevel@tonic-gate
17537c478bdstevel@tonic-gate	ASSERT(buf != NULL || nbytes == 0);
17547c478bdstevel@tonic-gate	iob_spb_create(&iob, iob_buf, sizeof (iob_buf), &io, &spb, buf, nbytes);
17557c478bdstevel@tonic-gate	ap.val_argv = argv;
17567c478bdstevel@tonic-gate	iob_doprnt(&iob, format, &ap);
17577c478bdstevel@tonic-gate	mdb_iob_flush(&iob);
17587c478bdstevel@tonic-gate
17597c478bdstevel@tonic-gate	if (spb.spb_bufsiz != 0)
17607c478bdstevel@tonic-gate		*spb.spb_buf = '\0';
17617c478bdstevel@tonic-gate	else if (buf != NULL && nbytes > 0)
17627c478bdstevel@tonic-gate		*--spb.spb_buf = '\0';
17637c478bdstevel@tonic-gate
17647c478bdstevel@tonic-gate	return (spb.spb_total);
17657c478bdstevel@tonic-gate}
17667c478bdstevel@tonic-gate
17677c478bdstevel@tonic-gate/*PRINTFLIKE3*/
17687c478bdstevel@tonic-gatesize_t
17697c478bdstevel@tonic-gatemdb_iob_snprintf(char *buf, size_t nbytes, const char *format, ...)
17707c478bdstevel@tonic-gate{
17717c478bdstevel@tonic-gate	va_list alist;
17727c478bdstevel@tonic-gate
17737c478bdstevel@tonic-gate	va_start(alist, format);
17747c478bdstevel@tonic-gate	nbytes = mdb_iob_vsnprintf(buf, nbytes, format, alist);
17757c478bdstevel@tonic-gate	va_end(alist);
17767c478bdstevel@tonic-gate
17777c478bdstevel@tonic-gate	return (nbytes);
17787c478bdstevel@tonic-gate}
17797c478bdstevel@tonic-gate
1780f11c6b6John Levon/*
1781f11c6b6John Levon * Return how many bytes we can copy into our buffer, limited by either cols or
1782f11c6b6John Levon * bufsiz depending on whether AUTOWRAP is on.  Note that typically,
1783f11c6b6John Levon * mdb_iob_set_autowrap() will have already checked for an existing
1784f11c6b6John Levon * "->iob_nbytes > ->iob_cols" situation, but we double check here anyway.
1785f11c6b6John Levon */
1786f11c6b6John Levonstatic size_t
1787f11c6b6John Levoniob_bufleft(mdb_iob_t *iob)
1788f11c6b6John Levon{
1789f11c6b6John Levon	if (IOB_AUTOWRAP(iob)) {
1790f11c6b6John Levon		if (iob->iob_cols < iob->iob_nbytes) {
1791f11c6b6John Levon			mdb_iob_nl(iob);
1792f11c6b6John Levon			ASSERT(iob->iob_cols >= iob->iob_nbytes);
1793f11c6b6John Levon		}
1794f11c6b6John Levon		return (iob->iob_cols - iob->iob_nbytes);
1795f11c6b6John Levon	}
1796f11c6b6John Levon
1797f11c6b6John Levon	ASSERT(iob->iob_bufsiz >= iob->iob_nbytes);
1798f11c6b6John Levon	return (iob->iob_bufsiz - iob->iob_nbytes);
1799f11c6b6John Levon}
1800f11c6b6John Levon
18017c478bdstevel@tonic-gatevoid
18027c478bdstevel@tonic-gatemdb_iob_nputs(mdb_iob_t *iob, const char *s, size_t nbytes)
18037c478bdstevel@tonic-gate{
18047c478bdstevel@tonic-gate	size_t m, n, nleft = nbytes;
18057c478bdstevel@tonic-gate	const char *p, *q = s;
18067c478bdstevel@tonic-gate
18077c478bdstevel@tonic-gate	ASSERT(iob->iob_flags & MDB_IOB_WRONLY);
18087c478bdstevel@tonic-gate
18097c478bdstevel@tonic-gate	if (nbytes == 0)
18107c478bdstevel@tonic-gate		return; /* Return immediately if there is no work to do */
18117c478bdstevel@tonic-gate
18127c478bdstevel@tonic-gate	/*
18137c478bdstevel@tonic-gate	 * If the string contains embedded newlines or tabs, invoke ourself
18147c478bdstevel@tonic-gate	 * recursively for each string component, followed by a call to the
18157c478bdstevel@tonic-gate	 * newline or tab routine.  This insures that strings with these
18167c478bdstevel@tonic-gate	 * characters obey our wrapping and indenting rules, and that strings
18177c478bdstevel@tonic-gate	 * with embedded newlines are flushed after each newline, allowing
18187c478bdstevel@tonic-gate	 * the output pager to take over if it is enabled.
18197c478bdstevel@tonic-gate	 */
18207c478bdstevel@tonic-gate	while ((p = strnpbrk(q, "\t\n", nleft)) != NULL) {
18217c478bdstevel@tonic-gate		if (p > q)
18227c478bdstevel@tonic-gate			mdb_iob_nputs(iob, q, (size_t)(p - q));
18237c478bdstevel@tonic-gate
18247c478bdstevel@tonic-gate		if (*p == '\t')
18257c478bdstevel@tonic-gate			mdb_iob_tab(iob);
18267c478bdstevel@tonic-gate		else
18277c478bdstevel@tonic-gate			mdb_iob_nl(iob);
18287c478bdstevel@tonic-gate
18297c478bdstevel@tonic-gate		nleft -= (size_t)(p - q) + 1;	/* Update byte count */
18307c478bdstevel@tonic-gate		q = p + 1;			/* Advance past delimiter */
18317c478bdstevel@tonic-gate	}
18327c478bdstevel@tonic-gate
18337c478bdstevel@tonic-gate	/*
1834f11c6b6John Levon	 * For a given string component, we copy a chunk into the buffer, and
18357c478bdstevel@tonic-gate	 * flush the buffer if we reach the end of a line.
18367c478bdstevel@tonic-gate	 */
18377c478bdstevel@tonic-gate	while (nleft != 0) {
1838f11c6b6John Levon		n = iob_bufleft(iob);
18397c478bdstevel@tonic-gate		m = MIN(nleft, n); /* copy at most n bytes in this pass */
18407c478bdstevel@tonic-gate
18417c478bdstevel@tonic-gate		bcopy(q, iob->iob_bufp, m);
18427c478bdstevel@tonic-gate		nleft -= m;
18437c478bdstevel@tonic-gate		q += m;
18447c478bdstevel@tonic-gate
18457c478bdstevel@tonic-gate		iob->iob_bufp += m;
18467c478bdstevel@tonic-gate		iob->iob_nbytes += m;
18477c478bdstevel@tonic-gate
18487c478bdstevel@tonic-gate		if (m == n && nleft != 0) {
1849f76ff24Bryan Cantrill			if (IOB_AUTOWRAP(iob)) {
18507c478bdstevel@tonic-gate				mdb_iob_nl(iob);
1851f76ff24Bryan Cantrill			} else {
18527c478bdstevel@tonic-gate				mdb_iob_flush(iob);
1853f76ff24Bryan Cantrill			}
18547c478bdstevel@tonic-gate		}
18557c478bdstevel@tonic-gate	}
18567c478bdstevel@tonic-gate}
18577c478bdstevel@tonic-gate
18587c478bdstevel@tonic-gatevoid
18597c478bdstevel@tonic-gatemdb_iob_puts(mdb_iob_t *iob, const char *s)
18607c478bdstevel@tonic-gate{
18617c478bdstevel@tonic-gate	mdb_iob_nputs(iob, s, strlen(s));
18627c478bdstevel@tonic-gate}
18637c478bdstevel@tonic-gate
18647c478bdstevel@tonic-gatevoid
18657c478bdstevel@tonic-gatemdb_iob_putc(mdb_iob_t *iob, int c)
18667c478bdstevel@tonic-gate{
18677c478bdstevel@tonic-gate	mdb_iob_fill(iob, c, 1);
18687c478bdstevel@tonic-gate}
18697c478bdstevel@tonic-gate
18707c478bdstevel@tonic-gatevoid
18717c478bdstevel@tonic-gatemdb_iob_tab(mdb_iob_t *iob)
18727c478bdstevel@tonic-gate{
18737c478bdstevel@tonic-gate	ASSERT(iob->iob_flags & MDB_IOB_WRONLY);
18747c478bdstevel@tonic-gate
18757c478bdstevel@tonic-gate	if (iob->iob_tabstop != 0) {
18767c478bdstevel@tonic-gate		/*
18777c478bdstevel@tonic-gate		 * Round up to the next multiple of the tabstop.  If this puts
18787c478bdstevel@tonic-gate		 * us off the end of the line, just insert a newline; otherwise
18797c478bdstevel@tonic-gate		 * insert sufficient whitespace to reach position n.
18807c478bdstevel@tonic-gate		 */
18817c478bdstevel@tonic-gate		size_t n = (iob->iob_nbytes + iob->iob_tabstop) /
18827c478bdstevel@tonic-gate		    iob->iob_tabstop * iob->iob_tabstop;
18837c478bdstevel@tonic-gate
18847c478bdstevel@tonic-gate		if (n < iob->iob_cols)
18857c478bdstevel@tonic-gate			mdb_iob_fill(iob, ' ', n - iob->iob_nbytes);
18867c478bdstevel@tonic-gate		else
18877c478bdstevel@tonic-gate			mdb_iob_nl(iob);
18887c478bdstevel@tonic-gate	}
18897c478bdstevel@tonic-gate}
18907c478bdstevel@tonic-gate
18917c478bdstevel@tonic-gatevoid
18927c478bdstevel@tonic-gatemdb_iob_fill(mdb_iob_t *iob, int c, size_t nfill)
18937c478bdstevel@tonic-gate{
18947c478bdstevel@tonic-gate	size_t i, m, n;
18957c478bdstevel@tonic-gate
18967c478bdstevel@tonic-gate	ASSERT(iob->iob_flags & MDB_IOB_WRONLY);
18977c478bdstevel@tonic-gate
18987c478bdstevel@tonic-gate	while (nfill != 0) {
1899f11c6b6John Levon		n = iob_bufleft(iob);
19007c478bdstevel@tonic-gate		m = MIN(nfill, n); /* fill at most n bytes in this pass */
19017c478bdstevel@tonic-gate
19027c478bdstevel@tonic-gate		for (i = 0; i < m; i++)
19037c478bdstevel@tonic-gate			*iob->iob_bufp++ = (char)c;
19047c478bdstevel@tonic-gate
19057c478bdstevel@tonic-gate		iob->iob_nbytes += m;
19067c478bdstevel@tonic-gate		nfill -= m;
19077c478bdstevel@tonic-gate
19087c478bdstevel@tonic-gate		if (m == n && nfill != 0) {
1909f76ff24Bryan Cantrill			if (IOB_AUTOWRAP(iob)) {
19107c478bdstevel@tonic-gate				mdb_iob_nl(iob);
1911f76ff24Bryan Cantrill			} else {
19127c478bdstevel@tonic-gate				mdb_iob_flush(iob);
1913f76ff24Bryan Cantrill			}
19147c478bdstevel@tonic-gate		}
19157c478bdstevel@tonic-gate	}
19167c478bdstevel@tonic-gate}
19177c478bdstevel@tonic-gate
19187c478bdstevel@tonic-gatevoid
19197c478bdstevel@tonic-gatemdb_iob_ws(mdb_iob_t *iob, size_t n)
19207c478bdstevel@tonic-gate{
1921c8a3ee0Bryan Cantrill	if (!IOB_AUTOWRAP(iob) || iob->iob_nbytes + n < iob->iob_cols)
19227c478bdstevel@tonic-gate		mdb_iob_fill(iob, ' ', n);
19237c478bdstevel@tonic-gate	else
19247c478bdstevel@tonic-gate		mdb_iob_nl(iob);
19257c478bdstevel@tonic-gate}
19267c478bdstevel@tonic-gate
19277c478bdstevel@tonic-gatevoid
19287c478bdstevel@tonic-gatemdb_iob_nl(mdb_iob_t *iob)
19297c478bdstevel@tonic-gate{
19307c478bdstevel@tonic-gate	ASSERT(iob->iob_flags & MDB_IOB_WRONLY);
19317c478bdstevel@tonic-gate
19327c478bdstevel@tonic-gate	if (iob->iob_nbytes == iob->iob_bufsiz)
19337c478bdstevel@tonic-gate		mdb_iob_flush(iob);
19347c478bdstevel@tonic-gate
19357c478bdstevel@tonic-gate	*iob->iob_bufp++ = '\n';
19367c478bdstevel@tonic-gate	iob->iob_nbytes++;
19377c478bdstevel@tonic-gate
19387c478bdstevel@tonic-gate	mdb_iob_flush(iob);
19397c478bdstevel@tonic-gate}
19407c478bdstevel@tonic-gate
19417c478bdstevel@tonic-gatessize_t
19427c478bdstevel@tonic-gatemdb_iob_ngets(mdb_iob_t *iob, char *buf, size_t n)
19437c478bdstevel@tonic-gate{
19447c478bdstevel@tonic-gate	ssize_t resid = n - 1;
19457c478bdstevel@tonic-gate	ssize_t len;
19467c478bdstevel@tonic-gate	int c;
19477c478bdstevel@tonic-gate
19487c478bdstevel@tonic-gate	if (iob->iob_flags & (MDB_IOB_WRONLY | MDB_IOB_EOF))
19497c478bdstevel@tonic-gate		return (EOF); /* can't gets a write buf or a read buf at EOF */
19507c478bdstevel@tonic-gate
19517c478bdstevel@tonic-gate	if (n == 0)
19527c478bdstevel@tonic-gate		return (0);   /* we need room for a terminating \0 */
19537c478bdstevel@tonic-gate
19547c478bdstevel@tonic-gate	while (resid != 0) {
19557c478bdstevel@tonic-gate		if (iob->iob_nbytes == 0 && iob_read(iob, iob->iob_iop) <= 0)
19567c478bdstevel@tonic-gate			goto done; /* failed to refill buffer */
19577c478bdstevel@tonic-gate
19587c478bdstevel@tonic-gate		for (len = MIN(iob->iob_nbytes, resid); len != 0; len--) {
19597c478bdstevel@tonic-gate			c = *iob->iob_bufp++;
19607c478bdstevel@tonic-gate			iob->iob_nbytes--;
19617c478bdstevel@tonic-gate
19627c478bdstevel@tonic-gate			if (c == EOF || c == '\n')
19637c478bdstevel@tonic-gate				goto done;
19647c478bdstevel@tonic-gate
19657c478bdstevel@tonic-gate			*buf++ = (char)c;
19667c478bdstevel@tonic-gate			resid--;
19677c478bdstevel@tonic-gate		}
19687c478bdstevel@tonic-gate	}
19697c478bdstevel@tonic-gatedone:
19707c478bdstevel@tonic-gate	*buf = '\0';
19717c478bdstevel@tonic-gate	return (n - resid - 1);
19727c478bdstevel@tonic-gate}
19737c478bdstevel@tonic-gate
19747c478bdstevel@tonic-gateint
19757c478bdstevel@tonic-gatemdb_iob_getc(mdb_iob_t *iob)
19767c478bdstevel@tonic-gate{
19777c478bdstevel@tonic-gate	int c;
19787c478bdstevel@tonic-gate
19797c478bdstevel@tonic-gate	if (iob->iob_flags & (MDB_IOB_WRONLY | MDB_IOB_EOF | MDB_IOB_ERR))
19807c478bdstevel@tonic-gate		return (EOF); /* can't getc if write-only, EOF, or error bit */
19817c478bdstevel@tonic-gate
19827c478bdstevel@tonic-gate	if (iob->iob_nbytes == 0 && iob_read(iob, iob->iob_iop) <= 0)
19837c478bdstevel@tonic-gate		return (EOF); /* failed to refill buffer */
19847c478bdstevel@tonic-gate
19857c478bdstevel@tonic-gate	c = (uchar_t)*iob->iob_bufp++;
19867c478bdstevel@tonic-gate	iob->iob_nbytes--;
19877c478bdstevel@tonic-gate
19887c478bdstevel@tonic-gate	return (c);
19897c478bdstevel@tonic-gate}
19907c478bdstevel@tonic-gate
19917c478bdstevel@tonic-gateint
19927c478bdstevel@tonic-gatemdb_iob_ungetc(mdb_iob_t *iob, int c)
19937c478bdstevel@tonic-gate{
19947c478bdstevel@tonic-gate	if (iob->iob_flags & (MDB_IOB_WRONLY | MDB_IOB_ERR))
19957c478bdstevel@tonic-gate		return (EOF); /* can't ungetc if write-only or error bit set */
19967c478bdstevel@tonic-gate
19977c478bdstevel@tonic-gate	if (c == EOF || iob->iob_nbytes == iob->iob_bufsiz)
19987c478bdstevel@tonic-gate		return (EOF); /* can't ungetc EOF, or ungetc if buffer full */
19997c478bdstevel@tonic-gate
20007c478bdstevel@tonic-gate	*--iob->iob_bufp = (char)c;
20017c478bdstevel@tonic-gate	iob->iob_nbytes++;
20027c478bdstevel@tonic-gate	iob->iob_flags &= ~MDB_IOB_EOF;
20037c478bdstevel@tonic-gate
20047c478bdstevel@tonic-gate	return (c);
20057c478bdstevel@tonic-gate}
20067c478bdstevel@tonic-gate
20077c478bdstevel@tonic-gateint
20087c478bdstevel@tonic-gatemdb_iob_eof(mdb_iob_t *iob)
20097c478bdstevel@tonic-gate{
20107c478bdstevel@tonic-gate	return ((iob->iob_flags & (MDB_IOB_RDONLY | MDB_IOB_EOF)) ==
20117c478bdstevel@tonic-gate	    (MDB_IOB_RDONLY | MDB_IOB_EOF));
20127c478bdstevel@tonic-gate}
20137c478bdstevel@tonic-gate
20147c478bdstevel@tonic-gateint
20157c478bdstevel@tonic-gatemdb_iob_err(mdb_iob_t *iob)
20167c478bdstevel@tonic-gate{
20177c478bdstevel@tonic-gate	return ((iob->iob_flags & MDB_IOB_ERR) == MDB_IOB_ERR);
20187c478bdstevel@tonic-gate}
20197c478bdstevel@tonic-gate
20207c478bdstevel@tonic-gatessize_t
20217c478bdstevel@tonic-gatemdb_iob_read(mdb_iob_t *iob, void *buf, size_t n)
20227c478bdstevel@tonic-gate{
20237c478bdstevel@tonic-gate	ssize_t resid = n;
20247c478bdstevel@tonic-gate	ssize_t len;
20257c478bdstevel@tonic-gate
20267c478bdstevel@tonic-gate	if (iob->iob_flags & (MDB_IOB_WRONLY | MDB_IOB_EOF | MDB_IOB_ERR))
20277c478bdstevel@tonic-gate		return (0); /* can't read if write-only, eof, or error */
20287c478bdstevel@tonic-gate
20297c478bdstevel@tonic-gate	while (resid != 0) {
20307c478bdstevel@tonic-gate		if (iob->iob_nbytes == 0 && iob_read(iob, iob->iob_iop) <= 0)
20317c478bdstevel@tonic-gate			break; /* failed to refill buffer */
20327c478bdstevel@tonic-gate
20337c478bdstevel@tonic-gate		len = MIN(resid, iob->iob_nbytes);
20347c478bdstevel@tonic-gate		bcopy(iob->iob_bufp, buf, len);
20357c478bdstevel@tonic-gate
20367c478bdstevel@tonic-gate		iob->iob_bufp += len;
20377c478bdstevel@tonic-gate		iob->iob_nbytes -= len;
20387c478bdstevel@tonic-gate
20397c478bdstevel@tonic-gate		buf = (char *)buf + len;
20407c478bdstevel@tonic-gate		resid -= len;
20417c478bdstevel@tonic-gate	}
20427c478bdstevel@tonic-gate
20437c478bdstevel@tonic-gate	return (n - resid);
20447c478bdstevel@tonic-gate}
20457c478bdstevel@tonic-gate
20467c478bdstevel@tonic-gate/*
20477c478bdstevel@tonic-gate * For now, all binary writes are performed unbuffered.  This has the
20487c478bdstevel@tonic-gate * side effect that the pager will not be triggered by mdb_iob_write.
20497c478bdstevel@tonic-gate */
20507c478bdstevel@tonic-gatessize_t
20517c478bdstevel@tonic-gatemdb_iob_write(mdb_iob_t *iob, const void *buf, size_t n)
20527c478bdstevel@tonic-gate{
20537c478bdstevel@tonic-gate	ssize_t ret;
20547c478bdstevel@tonic-gate
20557c478bdstevel@tonic-gate	if (iob->iob_flags & MDB_IOB_ERR)
20567c478bdstevel@tonic-gate		return (set_errno(EIO));
20577c478bdstevel@tonic-gate	if (iob->iob_flags & MDB_IOB_RDONLY)
20587c478bdstevel@tonic-gate		return (set_errno(EMDB_IORO));
20597c478bdstevel@tonic-gate
20607c478bdstevel@tonic-gate	mdb_iob_flush(iob);
20617c478bdstevel@tonic-gate	ret = iob_write(iob, iob->iob_iop, buf, n);
20627c478bdstevel@tonic-gate
20637c478bdstevel@tonic-gate	if (ret < 0 && iob == mdb.m_out)
20647c478bdstevel@tonic-gate		longjmp(mdb.m_frame->f_pcb, MDB_ERR_OUTPUT);
20657c478bdstevel@tonic-gate
20667c478bdstevel@tonic-gate	return (ret);
20677c478bdstevel@tonic-gate}
20687c478bdstevel@tonic-gate
20697c478bdstevel@tonic-gateint
20707c478bdstevel@tonic-gatemdb_iob_ctl(mdb_iob_t *iob, int req, void *arg)
20717c478bdstevel@tonic-gate{
20727c478bdstevel@tonic-gate	return (IOP_CTL(iob->iob_iop, req, arg));
20737c478bdstevel@tonic-gate}
20747c478bdstevel@tonic-gate
20757c478bdstevel@tonic-gateconst char *
20767c478bdstevel@tonic-gatemdb_iob_name(mdb_iob_t *iob)
20777c478bdstevel@tonic-gate{
20787c478bdstevel@tonic-gate	if (iob == NULL)
20797c478bdstevel@tonic-gate		return ("<NULL>");
20807c478bdstevel@tonic-gate
20817c478bdstevel@tonic-gate	return (IOP_NAME(iob->iob_iop));
20827c478bdstevel@tonic-gate}
20837c478bdstevel@tonic-gate
20847c478bdstevel@tonic-gatesize_t
20857c478bdstevel@tonic-gatemdb_iob_lineno(mdb_iob_t *iob)
20867c478bdstevel@tonic-gate{
20877c478bdstevel@tonic-gate	return (iob->iob_lineno);
20887c478bdstevel@tonic-gate}
20897c478bdstevel@tonic-gate
20907c478bdstevel@tonic-gatesize_t
20917c478bdstevel@tonic-gatemdb_iob_gettabstop(mdb_iob_t *iob)
20927c478bdstevel@tonic-gate{
20937c478bdstevel@tonic-gate	return (iob->iob_tabstop);
20947c478bdstevel@tonic-gate}
20957c478bdstevel@tonic-gate
20967c478bdstevel@tonic-gatesize_t
20977c478bdstevel@tonic-gatemdb_iob_getmargin(mdb_iob_t *iob)
20987c478bdstevel@tonic-gate{
20997c478bdstevel@tonic-gate	return (iob->iob_margin);
21007c478bdstevel@tonic-gate}
21017c478bdstevel@tonic-gate
21027c478bdstevel@tonic-gatemdb_io_t *
21037c478bdstevel@tonic-gatemdb_io_hold(mdb_io_t *io)
21047c478bdstevel@tonic-gate{
21057c478bdstevel@tonic-gate	io->io_refcnt++;
21067c478bdstevel@tonic-gate	return (io);
21077c478bdstevel@tonic-gate}
21087c478bdstevel@tonic-gate
21097c478bdstevel@tonic-gatevoid
21107c478bdstevel@tonic-gatemdb_io_rele(mdb_io_t *io)
21117c478bdstevel@tonic-gate{
21127c478bdstevel@tonic-gate	ASSERT(io->io_refcnt != 0);
21137c478bdstevel@tonic-gate
21147c478bdstevel@tonic-gate	if (--io->io_refcnt == 0) {
21157c478bdstevel@tonic-gate		IOP_CLOSE(io);
21167c478bdstevel@tonic-gate		mdb_free(io, sizeof (mdb_io_t));
21177c478bdstevel@tonic-gate	}
21187c478bdstevel@tonic-gate}
21197c478bdstevel@tonic-gate
21207c478bdstevel@tonic-gatevoid
21217c478bdstevel@tonic-gatemdb_io_destroy(mdb_io_t *io)
21227c478bdstevel@tonic-gate{
21237c478bdstevel@tonic-gate	ASSERT(io->io_refcnt == 0);
21247c478bdstevel@tonic-gate	IOP_CLOSE(io);
21257c478bdstevel@tonic-gate	mdb_free(io, sizeof (mdb_io_t));
21267c478bdstevel@tonic-gate}
21277c478bdstevel@tonic-gate
21287c478bdstevel@tonic-gatevoid
21297c478bdstevel@tonic-gatemdb_iob_stack_create(mdb_iob_stack_t *stk)
21307c478bdstevel@tonic-gate{
21317c478bdstevel@tonic-gate	stk->stk_top = NULL;
21327c478bdstevel@tonic-gate	stk->stk_size = 0;
21337c478bdstevel@tonic-gate}
21347c478bdstevel@tonic-gate
21357c478bdstevel@tonic-gatevoid
21367c478bdstevel@tonic-gatemdb_iob_stack_destroy(mdb_iob_stack_t *stk)
21377c478bdstevel@tonic-gate{
21387c478bdstevel@tonic-gate	mdb_iob_t *top, *ntop;
21397c478bdstevel@tonic-gate
21407c478bdstevel@tonic-gate	for (top = stk->stk_top; top != NULL; top = ntop) {
21417c478bdstevel@tonic-gate		ntop = top->iob_next;
21427c478bdstevel@tonic-gate		mdb_iob_destroy(top);
21437c478bdstevel@tonic-gate	}
21447c478bdstevel@tonic-gate}
21457c478bdstevel@tonic-gate
21467c478bdstevel@tonic-gatevoid
21477c478bdstevel@tonic-gatemdb_iob_stack_push(mdb_iob_stack_t *stk, mdb_iob_t *iob, size_t lineno)
21487c478bdstevel@tonic-gate{
21497c478bdstevel@tonic-gate	iob->iob_lineno = lineno;
21507c478bdstevel@tonic-gate	iob->iob_next = stk->stk_top;
21517c478bdstevel@tonic-gate	stk->stk_top = iob;
21527c478bdstevel@tonic-gate	stk->stk_size++;
21537c478bdstevel@tonic-gate	yylineno = 1;
21547c478bdstevel@tonic-gate}
21557c478bdstevel@tonic-gate
21567c478bdstevel@tonic-gatemdb_iob_t *
21577c478bdstevel@tonic-gatemdb_iob_stack_pop(mdb_iob_stack_t *stk)
21587c478bdstevel@tonic-gate{
21597c478bdstevel@tonic-gate	mdb_iob_t *top = stk->stk_top;
21607c478bdstevel@tonic-gate
21617c478bdstevel@tonic-gate	ASSERT(top != NULL);
21627c478bdstevel@tonic-gate
21637c478bdstevel@tonic-gate	stk->stk_top = top->iob_next;
21647c478bdstevel@tonic-gate	top->iob_next = NULL;
21657c478bdstevel@tonic-gate	stk->stk_size--;
21667c478bdstevel@tonic-gate
21677c478bdstevel@tonic-gate	return (top);
21687c478bdstevel@tonic-gate}
21697c478bdstevel@tonic-gate
21707c478bdstevel@tonic-gatesize_t
21717c478bdstevel@tonic-gatemdb_iob_stack_size(mdb_iob_stack_t *stk)
21727c478bdstevel@tonic-gate{
21737c478bdstevel@tonic-gate	return (stk->stk_size);
21747c478bdstevel@tonic-gate}
21757c478bdstevel@tonic-gate
21767c478bdstevel@tonic-gate/*
2177f11c6b6John Levon * This only enables autowrap for iobs that are already autowrap themselves such
2178f11c6b6John Levon * as mdb.m_out typically.
2179f11c6b6John Levon *
2180f11c6b6John Levon * Note that we might be the middle of the iob buffer at this point, and
2181f11c6b6John Levon * specifically, iob->iob_nbytes could be more than iob->iob_cols.  As that's
2182f11c6b6John Levon * not a valid situation, we may need to do an autowrap *now*.
2183f11c6b6John Levon *
2184f11c6b6John Levon * In theory, we would need to do this across all MDB_IOB_AUTOWRAP iob's;
2185f11c6b6John Levon * instead, we have a failsafe in iob_bufleft().
2186f11c6b6John Levon */
2187f11c6b6John Levonvoid
2188f11c6b6John Levonmdb_iob_set_autowrap(mdb_iob_t *iob)
2189f11c6b6John Levon{
2190f11c6b6John Levon	mdb.m_flags |= MDB_FL_AUTOWRAP;
2191f11c6b6John Levon	if (IOB_WRAPNOW(iob, 0))
2192f11c6b6John Levon		mdb_iob_nl(iob);
2193f11c6b6John Levon	ASSERT(iob->iob_cols >= iob->iob_nbytes);
2194f11c6b6John Levon}
2195f11c6b6John Levon
2196f11c6b6John Levon/*
21977c478bdstevel@tonic-gate * Stub functions for i/o backend implementors: these stubs either act as
21987c478bdstevel@tonic-gate * pass-through no-ops or return ENOTSUP as appropriate.
21997c478bdstevel@tonic-gate */
22007c478bdstevel@tonic-gatessize_t
22017c478bdstevel@tonic-gateno_io_read(mdb_io_t *io, void *buf, size_t nbytes)
22027c478bdstevel@tonic-gate{
22037c478bdstevel@tonic-gate	if (io->io_next != NULL)
22047c478bdstevel@tonic-gate		return (IOP_READ(io->io_next, buf, nbytes));
22057c478bdstevel@tonic-gate
22067c478bdstevel@tonic-gate	return (set_errno(EMDB_IOWO));
22077c478bdstevel@tonic-gate}
22087c478bdstevel@tonic-gate
22097c478bdstevel@tonic-gatessize_t
22107c478bdstevel@tonic-gateno_io_write(mdb_io_t *io, const void *buf, size_t nbytes)
22117c478bdstevel@tonic-gate{
22127c478bdstevel@tonic-gate	if (io->io_next != NULL)
22137c478bdstevel@tonic-gate		return (IOP_WRITE(io->io_next, buf, nbytes));
22147c478bdstevel@tonic-gate
22157c478bdstevel@tonic-gate	return (set_errno(EMDB_IORO));
22167c478bdstevel@tonic-gate}
22177c478bdstevel@tonic-gate
22187c478bdstevel@tonic-gateoff64_t
22197c478bdstevel@tonic-gateno_io_seek(mdb_io_t *io, off64_t offset, int whence)
22207c478bdstevel@tonic-gate{
22217c478bdstevel@tonic-gate	if (io->io_next != NULL)
22227c478bdstevel@tonic-gate		return (IOP_SEEK(io->io_next, offset, whence));
22237c478bdstevel@tonic-gate
22247c478bdstevel@tonic-gate	return (set_errno(ENOTSUP));
22257c478bdstevel@tonic-gate}
22267c478bdstevel@tonic-gate
22277c478bdstevel@tonic-gateint
22287c478bdstevel@tonic-gateno_io_ctl(mdb_io_t *io, int req, void *arg)
22297c478bdstevel@tonic-gate{
22307c478bdstevel@tonic-gate	if (io->io_next != NULL)
22317c478bdstevel@tonic-gate		return (IOP_CTL(io->io_next, req, arg));
22327c478bdstevel@tonic-gate
22337c478bdstevel@tonic-gate	return (set_errno(ENOTSUP));
22347c478bdstevel@tonic-gate}
22357c478bdstevel@tonic-gate
22367c478bdstevel@tonic-gate/*ARGSUSED*/
22377c478bdstevel@tonic-gatevoid
22387c478bdstevel@tonic-gateno_io_close(mdb_io_t *io)
22397c478bdstevel@tonic-gate{
22407c478bdstevel@tonic-gate/*
22417c478bdstevel@tonic-gate * Note that we do not propagate IOP_CLOSE down the io stack.  IOP_CLOSE should
22427c478bdstevel@tonic-gate * only be called by mdb_io_rele when an io's reference count has gone to zero.
22437c478bdstevel@tonic-gate */
22447c478bdstevel@tonic-gate}
22457c478bdstevel@tonic-gate
22467c478bdstevel@tonic-gateconst char *
22477c478bdstevel@tonic-gateno_io_name(mdb_io_t *io)
22487c478bdstevel@tonic-gate{
22497c478bdstevel@tonic-gate	if (io->io_next != NULL)
22507c478bdstevel@tonic-gate		return (IOP_NAME(io->io_next));
22517c478bdstevel@tonic-gate
22527c478bdstevel@tonic-gate	return ("(anonymous)");
22537c478bdstevel@tonic-gate}
22547c478bdstevel@tonic-gate
22557c478bdstevel@tonic-gatevoid
22567c478bdstevel@tonic-gateno_io_link(mdb_io_t *io, mdb_iob_t *iob)
22577c478bdstevel@tonic-gate{
22587c478bdstevel@tonic-gate	if (io->io_next != NULL)
22597c478bdstevel@tonic-gate		IOP_LINK(io->io_next, iob);
22607c478bdstevel@tonic-gate}
22617c478bdstevel@tonic-gate
22627c478bdstevel@tonic-gatevoid
22637c478bdstevel@tonic-gateno_io_unlink(mdb_io_t *io, mdb_iob_t *iob)
22647c478bdstevel@tonic-gate{
22657c478bdstevel@tonic-gate	if (io->io_next != NULL)
22667c478bdstevel@tonic-gate		IOP_UNLINK(io->io_next, iob);
22677c478bdstevel@tonic-gate}
22687c478bdstevel@tonic-gate
22697c478bdstevel@tonic-gateint
22707c478bdstevel@tonic-gateno_io_setattr(mdb_io_t *io, int req, uint_t attrs)
22717c478bdstevel@tonic-gate{
22727c478bdstevel@tonic-gate	if (io->io_next != NULL)
22737c478bdstevel@tonic-gate		return (IOP_SETATTR(io->io_next, req, attrs));
22747c478bdstevel@tonic-gate
22757c478bdstevel@tonic-gate	return (set_errno(ENOTSUP));
22767c478bdstevel@tonic-gate}
22777c478bdstevel@tonic-gate
22787c478bdstevel@tonic-gatevoid
22797c478bdstevel@tonic-gateno_io_suspend(mdb_io_t *io)
22807c478bdstevel@tonic-gate{
22817c478bdstevel@tonic-gate	if (io->io_next != NULL)
22827c478bdstevel@tonic-gate		IOP_SUSPEND(io->io_next);
22837c478bdstevel@tonic-gate}
22847c478bdstevel@tonic-gate
22857c478bdstevel@tonic-gatevoid
22867c478bdstevel@tonic-gateno_io_resume(mdb_io_t *io)
22877c478bdstevel@tonic-gate{
22887c478bdstevel@tonic-gate	if (io->io_next != NULL)
22897c478bdstevel@tonic-gate		IOP_RESUME(io->io_next);
22907c478bdstevel@tonic-gate}
22917c478bdstevel@tonic-gate
22927c478bdstevel@tonic-gate/*
22937c478bdstevel@tonic-gate * Iterate over the varargs. The first item indicates the mode:
22947c478bdstevel@tonic-gate * MDB_TBL_PRNT
2295f11c6b6John Levon *	pull out the next vararg as a const char * and pass it and the
2296f11c6b6John Levon *	remaining varargs to iob_doprnt; if we want to print the column,
2297f11c6b6John Levon *	direct the output to mdb.m_out otherwise direct it to mdb.m_null
22987c478bdstevel@tonic-gate *
22997c478bdstevel@tonic-gate * MDB_TBL_FUNC
2300f11c6b6John Levon *	pull out the next vararg as type mdb_table_print_f and the
2301f11c6b6John Levon *	following one as a void * argument to the function; call the
2302f11c6b6John Levon *	function with the given argument if we want to print the column
23037c478bdstevel@tonic-gate *
23047c478bdstevel@tonic-gate * The second item indicates the flag; if the flag is set in the flags
23057c478bdstevel@tonic-gate * argument, then the column is printed. A flag value of 0 indicates
23067c478bdstevel@tonic-gate * that the column should always be printed.
23077c478bdstevel@tonic-gate */
23087c478bdstevel@tonic-gatevoid
23097c478bdstevel@tonic-gatemdb_table_print(uint_t flags, const char *delimeter, ...)
23107c478bdstevel@tonic-gate{
23117c478bdstevel@tonic-gate	va_list alist;
23127c478bdstevel@tonic-gate	uint_t flg;
23137c478bdstevel@tonic-gate	uint_t