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