17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#include <netinet/in.h>
287c478bdstevel@tonic-gate#include <limits.h>
297c478bdstevel@tonic-gate#include <sys/types.h>
307c478bdstevel@tonic-gate#include <sys/socket.h>
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate#include <mdb/mdb_string.h>
337c478bdstevel@tonic-gate#include <mdb/mdb_modapi.h>
347c478bdstevel@tonic-gate#include <mdb/mdb_lex.h>
357c478bdstevel@tonic-gate#include <mdb/mdb_debug.h>
367c478bdstevel@tonic-gate#include <mdb/mdb.h>
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate/*
397c478bdstevel@tonic-gate * Convert the specified integer value to a string represented in the given
407c478bdstevel@tonic-gate * base.  The flags parameter is a bitfield of the formatting flags defined in
417c478bdstevel@tonic-gate * mdb_string.h.  A pointer to a static conversion buffer is returned.
427c478bdstevel@tonic-gate */
437c478bdstevel@tonic-gateconst char *
447c478bdstevel@tonic-gatenumtostr(uintmax_t uvalue, int base, uint_t flags)
457c478bdstevel@tonic-gate{
467c478bdstevel@tonic-gate	static const char ldigits[] = "0123456789abcdef";
477c478bdstevel@tonic-gate	static const char udigits[] = "0123456789ABCDEF";
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate	static char buf[68]; /* Enough for ULLONG_MAX in binary plus prefixes */
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gate	const char *digits = (flags & NTOS_UPCASE) ? udigits : ldigits;
527c478bdstevel@tonic-gate	int i = sizeof (buf);
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate	intmax_t value = (intmax_t)uvalue;
557c478bdstevel@tonic-gate	int neg = (flags & NTOS_UNSIGNED) == 0 && value < 0;
567c478bdstevel@tonic-gate	uintmax_t rem = neg ? -value : value;
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gate	buf[--i] = 0;
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate	do {
617c478bdstevel@tonic-gate		buf[--i] = digits[rem % base];
627c478bdstevel@tonic-gate		rem /= base;
637c478bdstevel@tonic-gate	} while (rem != 0);
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate	if (flags & NTOS_SHOWBASE) {
667c478bdstevel@tonic-gate		uintmax_t lim;
677c478bdstevel@tonic-gate		char c = 0;
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate		switch (base) {
707c478bdstevel@tonic-gate		case 2:
717c478bdstevel@tonic-gate			lim = 1;
727c478bdstevel@tonic-gate			c = 'i';
737c478bdstevel@tonic-gate			break;
747c478bdstevel@tonic-gate		case 8:
757c478bdstevel@tonic-gate			lim = 7;
767c478bdstevel@tonic-gate			c = 'o';
777c478bdstevel@tonic-gate			break;
787c478bdstevel@tonic-gate		case 10:
797c478bdstevel@tonic-gate			lim = 9;
807c478bdstevel@tonic-gate			c = 't';
817c478bdstevel@tonic-gate			break;
827c478bdstevel@tonic-gate		case 16:
837c478bdstevel@tonic-gate			lim = 9;
847c478bdstevel@tonic-gate			c = 'x';
857c478bdstevel@tonic-gate			break;
867c478bdstevel@tonic-gate		}
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gate		if (c != 0 && uvalue > lim) {
897c478bdstevel@tonic-gate			buf[--i] = c;
907c478bdstevel@tonic-gate			buf[--i] = '0';
917c478bdstevel@tonic-gate		}
927c478bdstevel@tonic-gate	}
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate	if (neg)
957c478bdstevel@tonic-gate		buf[--i] = '-';
967c478bdstevel@tonic-gate	else if (flags & NTOS_SIGNPOS)
977c478bdstevel@tonic-gate		buf[--i] = '+';
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate	return ((const char *)(&buf[i]));
1007c478bdstevel@tonic-gate}
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate#define	CTOI(x)	(((x) >= '0' && (x) <= '9') ? (x) - '0' : \
1037c478bdstevel@tonic-gate	((x) >= 'a' && (x) <= 'z') ? (x) + 10 - 'a' : (x) + 10 - 'A')
1047c478bdstevel@tonic-gate
1057c478bdstevel@tonic-gate/*
1067c478bdstevel@tonic-gate * Convert a string to an unsigned integer value using the specified base.
1077c478bdstevel@tonic-gate * In the event of overflow or an invalid character, we generate an
1087c478bdstevel@tonic-gate * error message and longjmp back to the main loop using yyerror().
1097c478bdstevel@tonic-gate */
1107c478bdstevel@tonic-gateuintmax_t
1114585130Yuri Pankovmdb_strtonum(const char *s, int base)
1127c478bdstevel@tonic-gate{
1137c478bdstevel@tonic-gate	uintmax_t multmax = (uintmax_t)ULLONG_MAX / (uintmax_t)(uint_t)base;
1147c478bdstevel@tonic-gate	uintmax_t val = 0;
1157c478bdstevel@tonic-gate	int c, i, neg = 0;
1167c478bdstevel@tonic-gate
1177c478bdstevel@tonic-gate	switch (c = *s) {
1187c478bdstevel@tonic-gate	case '-':
1197c478bdstevel@tonic-gate		neg++;
1207c478bdstevel@tonic-gate		/*FALLTHRU*/
1217c478bdstevel@tonic-gate	case '+':
1227c478bdstevel@tonic-gate		c = *++s;
1237c478bdstevel@tonic-gate	}
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gate	if (c == '\0')
1267c478bdstevel@tonic-gate		goto done;
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gate	if ((val = CTOI(c)) >= base)
1297c478bdstevel@tonic-gate		yyerror("digit '%c' is invalid in current base\n", c);
1307c478bdstevel@tonic-gate
1317c478bdstevel@tonic-gate	for (c = *++s; c != '\0'; c = *++s) {
1327c478bdstevel@tonic-gate		if (val > multmax)
1337c478bdstevel@tonic-gate			goto oflow;
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gate		if ((i = CTOI(c)) >= base)
1367c478bdstevel@tonic-gate			yyerror("digit '%c' is invalid in current base\n", c);
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gate		val *= base;
1397c478bdstevel@tonic-gate
1407c478bdstevel@tonic-gate		if ((uintmax_t)ULLONG_MAX - val < (uintmax_t)i)
1417c478bdstevel@tonic-gate			goto oflow;
1427c478bdstevel@tonic-gate
1437c478bdstevel@tonic-gate		val += i;
1447c478bdstevel@tonic-gate	}
1457c478bdstevel@tonic-gatedone:
1467c478bdstevel@tonic-gate	return (neg ? -val : val);
1477c478bdstevel@tonic-gateoflow:
1487c478bdstevel@tonic-gate	yyerror("specified value exceeds maximum immediate value\n");
1497c478bdstevel@tonic-gate	return ((uintmax_t)ULLONG_MAX);
1507c478bdstevel@tonic-gate}
1517c478bdstevel@tonic-gate
1527c478bdstevel@tonic-gate/*
1537c478bdstevel@tonic-gate * Quick string to unsigned long conversion function.  This function performs
1547c478bdstevel@tonic-gate * no overflow checking and is only meant for internal mdb use.  It allows
1557c478bdstevel@tonic-gate * the caller to specify the length of the string in bytes and a base.
1567c478bdstevel@tonic-gate */
1577c478bdstevel@tonic-gateulong_t
1587c478bdstevel@tonic-gatestrntoul(const char *s, size_t nbytes, int base)
1597c478bdstevel@tonic-gate{
1607c478bdstevel@tonic-gate	ulong_t n;
1617c478bdstevel@tonic-gate	int c;
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gate	for (n = 0; nbytes != 0 && (c = *s) != '\0'; s++, nbytes--)
1647c478bdstevel@tonic-gate		n = n * base + CTOI(c);
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gate	return (n);
1677c478bdstevel@tonic-gate}
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate/*
1707c478bdstevel@tonic-gate * Return a boolean value indicating whether or not a string consists
1717c478bdstevel@tonic-gate * solely of characters which are digits 0..9.
1727c478bdstevel@tonic-gate */
1737c478bdstevel@tonic-gateint
1747c478bdstevel@tonic-gatestrisnum(const char *s)
1757c478bdstevel@tonic-gate{
1767c478bdstevel@tonic-gate	for (; *s != '\0'; s++) {
1777c478bdstevel@tonic-gate		if (*s < '0' || *s > '9')
1787c478bdstevel@tonic-gate			return (0);
1797c478bdstevel@tonic-gate	}
1807c478bdstevel@tonic-gate
1817c478bdstevel@tonic-gate	return (1);
1827c478bdstevel@tonic-gate}
1837c478bdstevel@tonic-gate
1847c478bdstevel@tonic-gate/*
1857c478bdstevel@tonic-gate * Return a boolean value indicating whether or not a string contains a
1867c478bdstevel@tonic-gate * number.  The number may be in the current radix, or it may have an
1877c478bdstevel@tonic-gate * explicit radix qualifier.  The number will be validated against the
1887c478bdstevel@tonic-gate * legal characters for the given radix.
1897c478bdstevel@tonic-gate */
1907c478bdstevel@tonic-gateint
1917c478bdstevel@tonic-gatestrisbasenum(const char *s)
1927c478bdstevel@tonic-gate{
1937c478bdstevel@tonic-gate	char valid[] = "0123456789aAbBcCdDeEfF";
1947c478bdstevel@tonic-gate	int radix = mdb.m_radix;
1957c478bdstevel@tonic-gate
1967c478bdstevel@tonic-gate	if (s[0] == '0') {
1977c478bdstevel@tonic-gate		switch (s[1]) {
1987c478bdstevel@tonic-gate		case 'I':
1997c478bdstevel@tonic-gate		case 'i':
2007c478bdstevel@tonic-gate			radix = 2;
2017c478bdstevel@tonic-gate			s += 2;
2027c478bdstevel@tonic-gate			break;
2037c478bdstevel@tonic-gate		case 'O':
2047c478bdstevel@tonic-gate		case 'o':
2057c478bdstevel@tonic-gate			radix = 8;
2067c478bdstevel@tonic-gate			s += 2;
2077c478bdstevel@tonic-gate			break;
2087c478bdstevel@tonic-gate		case 'T':
2097c478bdstevel@tonic-gate		case 't':
2107c478bdstevel@tonic-gate			radix = 10;
2117c478bdstevel@tonic-gate			s += 2;
2127c478bdstevel@tonic-gate			break;
2137c478bdstevel@tonic-gate		case 'x':
2147c478bdstevel@tonic-gate		case 'X':
2157c478bdstevel@tonic-gate			radix = 16;
2167c478bdstevel@tonic-gate			s += 2;
2177c478bdstevel@tonic-gate			break;
2187c478bdstevel@tonic-gate		}
2197c478bdstevel@tonic-gate	}
2207c478bdstevel@tonic-gate
2217c478bdstevel@tonic-gate	/* limit `valid' to the digits valid for this base */
2227c478bdstevel@tonic-gate	valid[radix > 10 ? 10 + (radix - 10) * 2 : radix] = '\0';
2237c478bdstevel@tonic-gate
2247c478bdstevel@tonic-gate	do {
2257c478bdstevel@tonic-gate		if (!strchr(valid, *s))
2267c478bdstevel@tonic-gate			return (0);
2277c478bdstevel@tonic-gate	} while (*++s != '\0');
2287c478bdstevel@tonic-gate
2297c478bdstevel@tonic-gate	return (1);
2307c478bdstevel@tonic-gate}
2317c478bdstevel@tonic-gate
2327c478bdstevel@tonic-gate/*
2337c478bdstevel@tonic-gate * Quick string to integer (base 10) conversion function.  This performs
2347c478bdstevel@tonic-gate * no overflow checking and is only meant for internal mdb use.
2357c478bdstevel@tonic-gate */
2367c478bdstevel@tonic-gateint
2377c478bdstevel@tonic-gatestrtoi(const char *s)
2387c478bdstevel@tonic-gate{
2397c478bdstevel@tonic-gate	int c, n;
2407c478bdstevel@tonic-gate
2417c478bdstevel@tonic-gate	for (n = 0; (c = *s) >= '0' && c <= '9'; s++)
2427c478bdstevel@tonic-gate		n = n * 10 + c - '0';
2437c478bdstevel@tonic-gate
2447c478bdstevel@tonic-gate	return (n);
2457c478bdstevel@tonic-gate}
2467c478bdstevel@tonic-gate
2477c478bdstevel@tonic-gate/*
2487c478bdstevel@tonic-gate * Create a copy of string s using the mdb allocator interface.
2497c478bdstevel@tonic-gate */
2507c478bdstevel@tonic-gatechar *
2517c478bdstevel@tonic-gatestrdup(const char *s)
2527c478bdstevel@tonic-gate{
2537c478bdstevel@tonic-gate	char *s1 = mdb_alloc(strlen(s) + 1, UM_SLEEP);
2547c478bdstevel@tonic-gate
2557c478bdstevel@tonic-gate	(void) strcpy(s1, s);
2567c478bdstevel@tonic-gate	return (s1);
2577c478bdstevel@tonic-gate}
2587c478bdstevel@tonic-gate
2597c478bdstevel@tonic-gate/*
2607c478bdstevel@tonic-gate * Create a copy of string s, but only duplicate the first n bytes.
2617c478bdstevel@tonic-gate */
2627c478bdstevel@tonic-gatechar *
2637c478bdstevel@tonic-gatestrndup(const char *s, size_t n)
2647c478bdstevel@tonic-gate{
2657c478bdstevel@tonic-gate	char *s2 = mdb_alloc(n + 1, UM_SLEEP);
2667c478bdstevel@tonic-gate
2677c478bdstevel@tonic-gate	(void) strncpy(s2, s, n);
2687c478bdstevel@tonic-gate	s2[n] = '\0';
2697c478bdstevel@tonic-gate	return (s2);
2707c478bdstevel@tonic-gate}
2717c478bdstevel@tonic-gate
2727c478bdstevel@tonic-gate/*
2737c478bdstevel@tonic-gate * Convenience routine for freeing strings.
2747c478bdstevel@tonic-gate */
2757c478bdstevel@tonic-gatevoid
2767c478bdstevel@tonic-gatestrfree(char *s)
2777c478bdstevel@tonic-gate{
2787c478bdstevel@tonic-gate	mdb_free(s, strlen(s) + 1);
2797c478bdstevel@tonic-gate}
2807c478bdstevel@tonic-gate
2817c478bdstevel@tonic-gate/*
2827c478bdstevel@tonic-gate * Transform string s inline, converting each embedded C escape sequence string
2837c478bdstevel@tonic-gate * to the corresponding character.  For example, the substring "\n" is replaced
2847c478bdstevel@tonic-gate * by an inline '\n' character.  The length of the resulting string is returned.
2857c478bdstevel@tonic-gate */
2867c478bdstevel@tonic-gatesize_t
2877c478bdstevel@tonic-gatestresc2chr(char *s)
2887c478bdstevel@tonic-gate{
2897c478bdstevel@tonic-gate	char *p, *q, c;
2907c478bdstevel@tonic-gate	int esc = 0;
2917c478bdstevel@tonic-gate
2927c478bdstevel@tonic-gate	for (p = q = s; (c = *p) != '\0'; p++) {
2937c478bdstevel@tonic-gate		if (esc) {
2947c478bdstevel@tonic-gate			switch (c) {
2957c478bdstevel@tonic-gate				case '0':
2967c478bdstevel@tonic-gate				case '1':
2977c478bdstevel@tonic-gate				case '2':
2987c478bdstevel@tonic-gate				case '3':
2997c478bdstevel@tonic-gate				case '4':
3007c478bdstevel@tonic-gate				case '5':
3017c478bdstevel@tonic-gate				case '6':
3027c478bdstevel@tonic-gate				case '7':
3037c478bdstevel@tonic-gate					c -= '0';
3047c478bdstevel@tonic-gate					p++;
3057c478bdstevel@tonic-gate
3067c478bdstevel@tonic-gate					if (*p >= '0' && *p <= '7') {
3077c478bdstevel@tonic-gate						c = c * 8 + *p++ - '0';
3087c478bdstevel@tonic-gate
3097c478bdstevel@tonic-gate						if (*p >= '0' && *p <= '7')
3107c478bdstevel@tonic-gate							c = c * 8 + *p - '0';
3117c478bdstevel@tonic-gate						else
3127c478bdstevel@tonic-gate							p--;
3137c478bdstevel@tonic-gate					} else
3147c478bdstevel@tonic-gate						p--;
3157c478bdstevel@tonic-gate
3167c478bdstevel@tonic-gate					*q++ = c;
3177c478bdstevel@tonic-gate					break;
3187c478bdstevel@tonic-gate
3197c478bdstevel@tonic-gate				case 'a':
3207c478bdstevel@tonic-gate					*q++ = '\a';
3217c478bdstevel@tonic-gate					break;
3227c478bdstevel@tonic-gate				case 'b':
3237c478bdstevel@tonic-gate					*q++ = '\b';
3247c478bdstevel@tonic-gate					break;
3257c478bdstevel@tonic-gate				case 'f':
3267c478bdstevel@tonic-gate					*q++ = '\f';
3277c478bdstevel@tonic-gate					break;
3287c478bdstevel@tonic-gate				case 'n':
3297c478bdstevel@tonic-gate					*q++ = '\n';
3307c478bdstevel@tonic-gate					break;
3317c478bdstevel@tonic-gate				case 'r':
3327c478bdstevel@tonic-gate					*q++ = '\r';
3337c478bdstevel@tonic-gate					break;
3347c478bdstevel@tonic-gate				case 't':
3357c478bdstevel@tonic-gate					*q++ = '\t';
3367c478bdstevel@tonic-gate					break;
3377c478bdstevel@tonic-gate				case 'v':
3387c478bdstevel@tonic-gate					*q++ = '\v';
3397c478bdstevel@tonic-gate					break;
3407c478bdstevel@tonic-gate				case '"':
3417c478bdstevel@tonic-gate				case '\\':
3427c478bdstevel@tonic-gate					*q++ = c;
3437c478bdstevel@tonic-gate					break;
3447c478bdstevel@tonic-gate				default:
3457c478bdstevel@tonic-gate					*q++ = '\\';
3467c478bdstevel@tonic-gate					*q++ = c;
3477c478bdstevel@tonic-gate			}
3487c478bdstevel@tonic-gate
3497c478bdstevel@tonic-gate			esc = 0;
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate		} else {
3527c478bdstevel@tonic-gate			if ((esc = c == '\\') == 0)
3537c478bdstevel@tonic-gate				*q++ = c;
3547c478bdstevel@tonic-gate		}
3557c478bdstevel@tonic-gate	}
3567c478bdstevel@tonic-gate
3577c478bdstevel@tonic-gate	*q = '\0';
3587c478bdstevel@tonic-gate	return ((size_t)(q - s));
3597c478bdstevel@tonic-gate}
3607c478bdstevel@tonic-gate
3617c478bdstevel@tonic-gate/*
3627c478bdstevel@tonic-gate * Create a copy of string s in which certain unprintable or special characters
3637c478bdstevel@tonic-gate * have been converted to the string representation of their C escape sequence.
3647c478bdstevel@tonic-gate * For example, the newline character is expanded to the string "\n".
3657c478bdstevel@tonic-gate */
3667c478bdstevel@tonic-gatechar *
3677c478bdstevel@tonic-gatestrchr2esc(const char *s, size_t n)
3687c478bdstevel@tonic-gate{
3697c478bdstevel@tonic-gate	const char *p;
3707c478bdstevel@tonic-gate	char *q, *s2, c;
3717c478bdstevel@tonic-gate	size_t addl = 0;
3727c478bdstevel@tonic-gate
3737c478bdstevel@tonic-gate	for (p = s; p < s + n; p++) {
3747c478bdstevel@tonic-gate		switch (c = *p) {
3757c478bdstevel@tonic-gate		case '\0':
3767c478bdstevel@tonic-gate		case '\a':
3777c478bdstevel@tonic-gate		case '\b':
3787c478bdstevel@tonic-gate		case '\f':
3797c478bdstevel@tonic-gate		case '\n':
3807c478bdstevel@tonic-gate		case '\r':
3817c478bdstevel@tonic-gate		case '\t':
3827c478bdstevel@tonic-gate		case '\v':
3837c478bdstevel@tonic-gate		case '"':
3847c478bdstevel@tonic-gate		case '\\':
3857c478bdstevel@tonic-gate			addl++;		/* 1 add'l char needed to follow \ */
3867c478bdstevel@tonic-gate			break;
3877c478bdstevel@tonic-gate		case ' ':
3887c478bdstevel@tonic-gate			break;
3897c478bdstevel@tonic-gate		default:
3907c478bdstevel@tonic-gate			if (c < '!' || c > '~')
3917c478bdstevel@tonic-gate				addl += 3; /* 3 add'l chars following \ */
3927c478bdstevel@tonic-gate		}
3937c478bdstevel@tonic-gate	}
3947c478bdstevel@tonic-gate
3957c478bdstevel@tonic-gate	s2 = mdb_alloc(n + addl + 1, UM_SLEEP);
3967c478bdstevel@tonic-gate
3977c478bdstevel@tonic-gate	for (p = s, q = s2; p < s + n; p++) {
3987c478bdstevel@tonic-gate		switch (c = *p) {
3997c478bdstevel@tonic-gate		case '\0':
4007c478bdstevel@tonic-gate			*q++ = '\\';
4017c478bdstevel@tonic-gate			*q++ = '0';
4027c478bdstevel@tonic-gate			break;
4037c478bdstevel@tonic-gate		case '\a':
4047c478bdstevel@tonic-gate			*q++ = '\\';
4057c478bdstevel@tonic-gate			*q++ = 'a';
4067c478bdstevel@tonic-gate			break;
4077c478bdstevel@tonic-gate		case '\b':
4087c478bdstevel@tonic-gate			*q++ = '\\';
4097c478bdstevel@tonic-gate			*q++ = 'b';
4107c478bdstevel@tonic-gate			break;
4117c478bdstevel@tonic-gate		case '\f':
4127c478bdstevel@tonic-gate			*q++ = '\\';
4137c478bdstevel@tonic-gate			*q++ = 'f';
4147c478bdstevel@tonic-gate			break;
4157c478bdstevel@tonic-gate		case '\n':
4167c478bdstevel@tonic-gate			*q++ = '\\';
4177c478bdstevel@tonic-gate			*q++ = 'n';
4187c478bdstevel@tonic-gate			break;
4197c478bdstevel@tonic-gate		case '\r':
4207c478bdstevel@tonic-gate			*q++ = '\\';
4217c478bdstevel@tonic-gate			*q++ = 'r';
4227c478bdstevel@tonic-gate			break;
4237c478bdstevel@tonic-gate		case '\t':
4247c478bdstevel@tonic-gate			*q++ = '\\';
4257c478bdstevel@tonic-gate			*q++ = 't';
4267c478bdstevel@tonic-gate			break;
4277c478bdstevel@tonic-gate		case '\v':
4287c478bdstevel@tonic-gate			*q++ = '\\';
4297c478bdstevel@tonic-gate			*q++ = 'v';
4307c478bdstevel@tonic-gate			break;
4317c478bdstevel@tonic-gate		case '"':
4327c478bdstevel@tonic-gate			*q++ = '\\';
4337c478bdstevel@tonic-gate			*q++ = '"';
4347c478bdstevel@tonic-gate			break;
4357c478bdstevel@tonic-gate		case '\\':
4367c478bdstevel@tonic-gate			*q++ = '\\';
4377c478bdstevel@tonic-gate			*q++ = '\\';
4387c478bdstevel@tonic-gate			break;
4397c478bdstevel@tonic-gate		case ' ':
4407c478bdstevel@tonic-gate			*q++ = c;
4417c478bdstevel@tonic-gate			break;
4427c478bdstevel@tonic-gate		default:
4437c478bdstevel@tonic-gate			if (c < '!' || c > '~') {
4447c478bdstevel@tonic-gate				*q++ = '\\';
4457c478bdstevel@tonic-gate				*q++ = ((c >> 6) & 3) + '0';
4467c478bdstevel@tonic-gate				*q++ = ((c >> 3) & 7) + '0';
4477c478bdstevel@tonic-gate				*q++ = (c & 7) + '0';
4487c478bdstevel@tonic-gate			} else
4497c478bdstevel@tonic-gate				*q++ = c;
4507c478bdstevel@tonic-gate		}
4517c478bdstevel@tonic-gate	}
4527c478bdstevel@tonic-gate
4537c478bdstevel@tonic-gate	*q = '\0';
4547c478bdstevel@tonic-gate	return (s2);
4557c478bdstevel@tonic-gate}
4567c478bdstevel@tonic-gate
4577c478bdstevel@tonic-gate/*
4587c478bdstevel@tonic-gate * Create a copy of string s in which certain unprintable or special characters
4597c478bdstevel@tonic-gate * have been converted to an odd representation of their escape sequence.
4607c478bdstevel@tonic-gate * This algorithm is the old adb convention for representing such sequences.
4617c478bdstevel@tonic-gate */
4627c478bdstevel@tonic-gatechar *
4637c478bdstevel@tonic-gatestrchr2adb(const char *s, size_t n)
4647c478bdstevel@tonic-gate{
4657c478bdstevel@tonic-gate	size_t addl = 0;
4667c478bdstevel@tonic-gate	const char *p;
4677c478bdstevel@tonic-gate	char *q, *s2;
4687c478bdstevel@tonic-gate
4697c478bdstevel@tonic-gate	for (p = s; p < s + n; p++) {
4707c478bdstevel@tonic-gate		char c = *p & CHAR_MAX;
4717c478bdstevel@tonic-gate
4727c478bdstevel@tonic-gate		if (c < ' ' || c == CHAR_MAX)
4737c478bdstevel@tonic-gate			addl++; /* 1 add'l char needed for "^" */
4747c478bdstevel@tonic-gate	}
4757c478bdstevel@tonic-gate
4767c478bdstevel@tonic-gate	s2 = mdb_alloc(n + addl + 1, UM_SLEEP);
4777c478bdstevel@tonic-gate
4787c478bdstevel@tonic-gate	for (p = s, q = s2; p < s + n; p++) {
4797c478bdstevel@tonic-gate		char c = *p & CHAR_MAX;
4807c478bdstevel@tonic-gate
4817c478bdstevel@tonic-gate		if (c == CHAR_MAX) {
4827c478bdstevel@tonic-gate			*q++ = '^';
4837c478bdstevel@tonic-gate			*q++ = '?';
4847c478bdstevel@tonic-gate		} else if (c < ' ') {
4857c478bdstevel@tonic-gate			*q++ = '^';
4867c478bdstevel@tonic-gate			*q++ = c + '@';
4877c478bdstevel@tonic-gate		} else
4887c478bdstevel@tonic-gate			*q++ = c;
4897c478bdstevel@tonic-gate	}
4907c478bdstevel@tonic-gate
4917c478bdstevel@tonic-gate	*q = '\0';
4927c478bdstevel@tonic-gate	return (s2);
4937c478bdstevel@tonic-gate}
4947c478bdstevel@tonic-gate
4957c478bdstevel@tonic-gate/*
4967c478bdstevel@tonic-gate * Same as strchr, but we only search the first n characters
4977c478bdstevel@tonic-gate */
4987c478bdstevel@tonic-gatechar *
4997c478bdstevel@tonic-gatestrnchr(const char *s, int c, size_t n)
5007c478bdstevel@tonic-gate{
5017c478bdstevel@tonic-gate	int i = 0;
5027c478bdstevel@tonic-gate
5037c478bdstevel@tonic-gate	for (i = 0; i < n; i++) {
5047c478bdstevel@tonic-gate		if (*(s + i) == (char)c)
5057c478bdstevel@tonic-gate			return ((char *)(s + i));
5067c478bdstevel@tonic-gate	}
5077c478bdstevel@tonic-gate
5087c478bdstevel@tonic-gate	return (NULL);
5097c478bdstevel@tonic-gate}
5107c478bdstevel@tonic-gate
5117c478bdstevel@tonic-gate/*
5127c478bdstevel@tonic-gate * Split the string s at the first occurrence of character c.  This character
5137c478bdstevel@tonic-gate * is replaced by \0, and a pointer to the remainder of the string is returned.
5147c478bdstevel@tonic-gate */
5157c478bdstevel@tonic-gatechar *
5167c478bdstevel@tonic-gatestrsplit(char *s, char c)
5177c478bdstevel@tonic-gate{
5187c478bdstevel@tonic-gate	char *p;
5197c478bdstevel@tonic-gate
5207c478bdstevel@tonic-gate	if ((p = strchr(s, c)) == NULL)
5217c478bdstevel@tonic-gate		return (NULL);
5227c478bdstevel@tonic-gate
5237c478bdstevel@tonic-gate	*p++ = '\0';
5247c478bdstevel@tonic-gate	return (p);
5257c478bdstevel@tonic-gate}
5267c478bdstevel@tonic-gate
5277c478bdstevel@tonic-gate/*
5287c478bdstevel@tonic-gate * Same as strsplit, but split from the last occurrence of character c.
5297c478bdstevel@tonic-gate */
5307c478bdstevel@tonic-gatechar *
5317c478bdstevel@tonic-gatestrrsplit(char *s, char c)
5327c478bdstevel@tonic-gate{
5337c478bdstevel@tonic-gate	char *p;
5347c478bdstevel@tonic-gate
5357c478bdstevel@tonic-gate	if ((p = strrchr(s, c)) == NULL)
5367c478bdstevel@tonic-gate		return (NULL);
5377c478bdstevel@tonic-gate
5387c478bdstevel@tonic-gate	*p++ = '\0';
5397c478bdstevel@tonic-gate	return (p);
5407c478bdstevel@tonic-gate}
5417c478bdstevel@tonic-gate
5427c478bdstevel@tonic-gate/*
5437c478bdstevel@tonic-gate * Return the address of the first occurrence of any character from s2
5447c478bdstevel@tonic-gate * in the string s1, or NULL if none exists.  This is similar to libc's
5457c478bdstevel@tonic-gate * strpbrk, but we add a third parameter to limit the search to the
5467c478bdstevel@tonic-gate * specified number of bytes in s1, or a \0 character, whichever is
5477c478bdstevel@tonic-gate * encountered first.
5487c478bdstevel@tonic-gate */
5497c478bdstevel@tonic-gateconst char *
5507c478bdstevel@tonic-gatestrnpbrk(const char *s1, const char *s2, size_t nbytes)
5517c478bdstevel@tonic-gate{
5527c478bdstevel@tonic-gate	const char *p;
5537c478bdstevel@tonic-gate
5547c478bdstevel@tonic-gate	if (nbytes == 0)
5557c478bdstevel@tonic-gate		return (NULL);
5567c478bdstevel@tonic-gate
5577c478bdstevel@tonic-gate	do {
5587c478bdstevel@tonic-gate		for (p = s2; *p != '\0' && *p != *s1; p++)
5597c478bdstevel@tonic-gate			continue;
5607c478bdstevel@tonic-gate
5617c478bdstevel@tonic-gate		if (*p != '\0')
5627c478bdstevel@tonic-gate			return (s1);
5637c478bdstevel@tonic-gate
5647c478bdstevel@tonic-gate	} while (--nbytes != 0 && *s1++ != '\0');
5657c478bdstevel@tonic-gate
5667c478bdstevel@tonic-gate	return (NULL);
5677c478bdstevel@tonic-gate}
5687c478bdstevel@tonic-gate
5697c478bdstevel@tonic-gate/*
5707c478bdstevel@tonic-gate * Abbreviate a string if it meets or exceeds the specified length, including
5717c478bdstevel@tonic-gate * the terminating null character.  The string is abbreviated by replacing the
5727c478bdstevel@tonic-gate * last four characters with " ...".  strabbr is useful in constructs such as
5737c478bdstevel@tonic-gate * this one, where nbytes = sizeof (buf):
5747c478bdstevel@tonic-gate *
5757c478bdstevel@tonic-gate * if (mdb_snprintf(buf, nbytes, "%s %d %c", ...) >= nbytes)
5767c478bdstevel@tonic-gate *         (void) strabbr(buf, nbytes);
5777c478bdstevel@tonic-gate *
5787c478bdstevel@tonic-gate * No modifications are made if nbytes is too small to hold the suffix itself.
5797c478bdstevel@tonic-gate */
5807c478bdstevel@tonic-gatechar *
5817c478bdstevel@tonic-gatestrabbr(char *s, size_t nbytes)
5827c478bdstevel@tonic-gate{
5837c478bdstevel@tonic-gate	static const char suffix[] = " ...";
5847c478bdstevel@tonic-gate
5857c478bdstevel@tonic-gate	if (nbytes > sizeof (suffix) && strlen(s) >= nbytes - 1)
5867c478bdstevel@tonic-gate		(void) strcpy(&s[nbytes - sizeof (suffix)], suffix);
5877c478bdstevel@tonic-gate
5887c478bdstevel@tonic-gate	return (s);
5897c478bdstevel@tonic-gate}
5907c478bdstevel@tonic-gate
5917c478bdstevel@tonic-gate/*
5927c478bdstevel@tonic-gate * Return the basename (name after final /) of the given string.  We use
5937c478bdstevel@tonic-gate * strbasename rather than basename to avoid conflicting with libgen.h's
5947c478bdstevel@tonic-gate * non-const function prototype.
5957c478bdstevel@tonic-gate */
5967c478bdstevel@tonic-gateconst char *
5977c478bdstevel@tonic-gatestrbasename(const char *s)
5987c478bdstevel@tonic-gate{
5997c478bdstevel@tonic-gate	const char *p = strrchr(s, '/');
6007c478bdstevel@tonic-gate
6017c478bdstevel@tonic-gate	if (p == NULL)
6027c478bdstevel@tonic-gate		return (s);
6037c478bdstevel@tonic-gate
6047c478bdstevel@tonic-gate	return (++p);
6057c478bdstevel@tonic-gate}
6067c478bdstevel@tonic-gate
6077c478bdstevel@tonic-gate/*
6087c478bdstevel@tonic-gate * Return the directory name (name prior to the final /) of the given string.
6097c478bdstevel@tonic-gate * The string itself is modified.
6107c478bdstevel@tonic-gate */
6117c478bdstevel@tonic-gatechar *
6127c478bdstevel@tonic-gatestrdirname(char *s)
6137c478bdstevel@tonic-gate{
6147c478bdstevel@tonic-gate	static char slash[] = "/";
6157c478bdstevel@tonic-gate	static char dot[] = ".";
6167c478bdstevel@tonic-gate	char *p;
6177c478bdstevel@tonic-gate
6187c478bdstevel@tonic-gate	if (s == NULL || *s == '\0')
6197c478bdstevel@tonic-gate		return (dot);
6207c478bdstevel@tonic-gate
6217c478bdstevel@tonic-gate	for (p = s + strlen(s); p != s && *--p == '/'; )
6227c478bdstevel@tonic-gate		continue;
6237c478bdstevel@tonic-gate
6247c478bdstevel@tonic-gate	if (p == s && *p == '/')
6257c478bdstevel@tonic-gate		return (slash);
6267c478bdstevel@tonic-gate
6277c478bdstevel@tonic-gate	while (p != s) {
6287c478bdstevel@tonic-gate		if (*--p == '/') {
6297c478bdstevel@tonic-gate			while (*p == '/' && p != s)
6307c478bdstevel@tonic-gate				p--;
6317c478bdstevel@tonic-gate			*++p = '\0';
6327c478bdstevel@tonic-gate			return (s);
6337c478bdstevel@tonic-gate		}
6347c478bdstevel@tonic-gate	}
6357c478bdstevel@tonic-gate
6367c478bdstevel@tonic-gate	return (dot);
6377c478bdstevel@tonic-gate}
6387c478bdstevel@tonic-gate
6397c478bdstevel@tonic-gate/*
6407c478bdstevel@tonic-gate * Return a pointer to the first character in the string that makes it an
6417c478bdstevel@tonic-gate * invalid identifer (i.e. incompatible with the mdb syntax), or NULL if
6427c478bdstevel@tonic-gate * the string is a valid identifier.
6437c478bdstevel@tonic-gate */
6447c478bdstevel@tonic-gateconst char *
6457c478bdstevel@tonic-gatestrbadid(const char *s)
6467c478bdstevel@tonic-gate{
6477c478bdstevel@tonic-gate	return (strpbrk(s, "#%^&*-+=,:$/\\?<>;|!`'\"[]\n\t() {}"));
6487c478bdstevel@tonic-gate}
6497c478bdstevel@tonic-gate
6507c478bdstevel@tonic-gate/*
6517c478bdstevel@tonic-gate * Return a boolean value indicating if the given string consists solely
6527c478bdstevel@tonic-gate * of printable ASCII characters terminated by \0.
6537c478bdstevel@tonic-gate */
6547c478bdstevel@tonic-gateint
6557c478bdstevel@tonic-gatestrisprint(const char *s)
6567c478bdstevel@tonic-gate{
6577c478bdstevel@tonic-gate	for (; *s != '\0'; s++) {
6587c478bdstevel@tonic-gate		if (*s < ' ' || *s > '~')
6597c478bdstevel@tonic-gate			return (0);
6607c478bdstevel@tonic-gate	}
6617c478bdstevel@tonic-gate
6627c478bdstevel@tonic-gate	return (1);
6637c478bdstevel@tonic-gate}
6647c478bdstevel@tonic-gate
6657c478bdstevel@tonic-gate/*
6667c478bdstevel@tonic-gate * This is a near direct copy of the inet_ntop() code in
6677c478bdstevel@tonic-gate * uts/common/inet/ip/ipv6.c, duplicated here for kmdb's sake.
6687c478bdstevel@tonic-gate */
6697c478bdstevel@tonic-gatestatic void
6707c478bdstevel@tonic-gateconvert2ascii(char *buf, const in6_addr_t *addr)
6717c478bdstevel@tonic-gate{
6727c478bdstevel@tonic-gate	int		hexdigits;
6737c478bdstevel@tonic-gate	int		head_zero = 0;
6747c478bdstevel@tonic-gate	int		tail_zero = 0;
6757c478bdstevel@tonic-gate	/* tempbuf must be big enough to hold ffff:\0 */
6767c478bdstevel@tonic-gate	char		tempbuf[6];
6777c478bdstevel@tonic-gate	char		*ptr;
6787c478bdstevel@tonic-gate	uint16_t	*addr_component, host_component;
6797c478bdstevel@tonic-gate	size_t		len;
6807c478bdstevel@tonic-gate	int		first = FALSE;
6817c478bdstevel@tonic-gate	int		med_zero = FALSE;
6827c478bdstevel@tonic-gate	int		end_zero = FALSE;
6837c478bdstevel@tonic-gate
6847c478bdstevel@tonic-gate	addr_component = (uint16_t *)addr;
6857c478bdstevel@tonic-gate	ptr = buf;
6867c478bdstevel@tonic-gate
6877c478bdstevel@tonic-gate	/* First count if trailing zeroes higher in number */
6887c478bdstevel@tonic-gate	for (hexdigits = 0; hexdigits < 8; hexdigits++) {
6897c478bdstevel@tonic-gate		if (*addr_component == 0) {
6907c478bdstevel@tonic-gate			if (hexdigits < 4)
6917c478bdstevel@tonic-gate				head_zero++;
6927c478bdstevel@tonic-gate			else
6937c478bdstevel@tonic-gate				tail_zero++;
6947c478bdstevel@tonic-gate		}
6957c478bdstevel@tonic-gate		addr_component++;
6967c478bdstevel@tonic-gate	}
6977c478bdstevel@tonic-gate	addr_component = (uint16_t *)addr;
6987c478bdstevel@tonic-gate	if (tail_zero > head_zero && (head_zero + tail_zero) != 7)
6997c478bdstevel@tonic-gate		end_zero = TRUE;
7007c478bdstevel@tonic-gate
7017c478bdstevel@tonic-gate	for (hexdigits = 0; hexdigits < 8; hexdigits++) {
7027c478bdstevel@tonic-gate		/* if entry is a 0 */
7037c478bdstevel@tonic-gate		if (*addr_component == 0) {
7047c478bdstevel@tonic-gate			if (!first && *(addr_component + 1) == 0) {
7057c478bdstevel@tonic-gate				if (end_zero && (hexdigits < 4)) {
7067c478bdstevel@tonic-gate					*ptr++ = '0';
7077c478bdstevel@tonic-gate					*ptr++ = ':';
7087c478bdstevel@tonic-gate				} else {
7097c478bdstevel@tonic-gate					if (hexdigits == 0)
7107c478bdstevel@tonic-gate						*ptr++ = ':';
7117c478bdstevel@tonic-gate					/* add another */
7127c478bdstevel@tonic-gate					*ptr++ = ':';
7137c478bdstevel@tonic-gate					first = TRUE;
7147c478bdstevel@tonic-gate					med_zero = TRUE;
7157c478bdstevel@tonic-gate				}
7167c478bdstevel@tonic-gate			} else if (first && med_zero) {
7177c478bdstevel@tonic-gate				if (hexdigits == 7)
7187c478bdstevel@tonic-gate					*ptr++ = ':';
7197c478bdstevel@tonic-gate				addr_component++;
7207c478bdstevel@tonic-gate				continue;
7217c478bdstevel@tonic-gate			} else {
7227c478bdstevel@tonic-gate				*ptr++ = '0';
7237c478bdstevel@tonic-gate				*ptr++ = ':';
7247c478bdstevel@tonic-gate			}
7257c478bdstevel@tonic-gate			addr_component++;
7267c478bdstevel@tonic-gate			continue;
7277c478bdstevel@tonic-gate		}
7287c478bdstevel@tonic-gate		if (med_zero)
7297c478bdstevel@tonic-gate			med_zero = FALSE;
7307c478bdstevel@tonic-gate
7317c478bdstevel@tonic-gate		tempbuf[0] = '\0';
7327c478bdstevel@tonic-gate		mdb_nhconvert(&host_component, addr_component,
7337c478bdstevel@tonic-gate		    sizeof (uint16_t));
7347c478bdstevel@tonic-gate		(void) mdb_snprintf(tempbuf, sizeof (tempbuf), "%x:",
7357c478bdstevel@tonic-gate		    host_component & 0xffff);
7367c478bdstevel@tonic-gate		len = strlen(tempbuf);
7377c478bdstevel@tonic-gate		bcopy(tempbuf, ptr, len);
7387c478bdstevel@tonic-gate		ptr = ptr + len;
7397c478bdstevel@tonic-gate		addr_component++;
7407c478bdstevel@tonic-gate	}
7417c478bdstevel@tonic-gate	*--ptr = '\0';
7427c478bdstevel@tonic-gate}
7437c478bdstevel@tonic-gate
7447c478bdstevel@tonic-gatechar *
7457c478bdstevel@tonic-gatemdb_inet_ntop(int af, const void *addr, char *buf, size_t buflen)
7467c478bdstevel@tonic-gate{
7477c478bdstevel@tonic-gate	in6_addr_t	*v6addr;
7487c478bdstevel@tonic-gate	uchar_t		*v4addr;
7497c478bdstevel@tonic-gate	char		*caddr;
7507c478bdstevel@tonic-gate
7517c478bdstevel@tonic-gate#define	UC(b)	(((int)b) & 0xff)
7527c478bdstevel@tonic-gate	switch (af) {
7537c478bdstevel@tonic-gate	case AF_INET:
7547c478bdstevel@tonic-gate		ASSERT(buflen >= INET_ADDRSTRLEN);
7557c478bdstevel@tonic-gate		v4addr = (uchar_t *)addr;
7567c478bdstevel@tonic-gate		(void) mdb_snprintf(buf, buflen, "%d.%d.%d.%d",
7577c478bdstevel@tonic-gate		    UC(v4addr[0]), UC(v4addr[1]), UC(v4addr[2]), UC(v4addr[3]));
7587c478bdstevel@tonic-gate		return (buf);
7597c478bdstevel@tonic-gate
7607c478bdstevel@tonic-gate	case AF_INET6:
7617c478bdstevel@tonic-gate		ASSERT(buflen >= INET6_ADDRSTRLEN);
7627c478bdstevel@tonic-gate		v6addr = (in6_addr_t *)addr;
7637c478bdstevel@tonic-gate		if (IN6_IS_ADDR_V4MAPPED(v6addr)) {
7647c478bdstevel@tonic-gate			caddr = (char *)addr;
7657c478bdstevel@tonic-gate			(void) mdb_snprintf(buf, buflen, "::ffff:%d.%d.%d.%d",
7667c478bdstevel@tonic-gate			    UC(caddr[12]), UC(caddr[13]),
7677c478bdstevel@tonic-gate			    UC(caddr[14]), UC(caddr[15]));
7687c478bdstevel@tonic-gate		} else if (IN6_IS_ADDR_V4COMPAT(v6addr)) {
7697c478bdstevel@tonic-gate			caddr = (char *)addr;
7707c478bdstevel@tonic-gate			(void) mdb_snprintf(buf, buflen, "::%d.%d.%d.%d",
7717c478bdstevel@tonic-gate			    UC(caddr[12]), UC(caddr[13]), UC(caddr[14]),
7727c478bdstevel@tonic-gate			    UC(caddr[15]));
7737c478bdstevel@tonic-gate		} else if (IN6_IS_ADDR_UNSPECIFIED(v6addr)) {
7747c478bdstevel@tonic-gate			(void) mdb_snprintf(buf, buflen, "::");
7757c478bdstevel@tonic-gate		} else {
7767c478bdstevel@tonic-gate			convert2ascii(buf, v6addr);
7777c478bdstevel@tonic-gate		}
7787c478bdstevel@tonic-gate		return (buf);
7797c478bdstevel@tonic-gate	}
7807c478bdstevel@tonic-gate#undef UC
7817c478bdstevel@tonic-gate
7827c478bdstevel@tonic-gate	return (NULL);
7837c478bdstevel@tonic-gate}
784