xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_string.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*12902SBryan.Cantrill@Sun.COM  * Common Development and Distribution License (the "License").
6*12902SBryan.Cantrill@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*12902SBryan.Cantrill@Sun.COM 
220Sstevel@tonic-gate /*
23*12902SBryan.Cantrill@Sun.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <strings.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <errno.h>
290Sstevel@tonic-gate #include <ctype.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <dt_string.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Transform string s inline, converting each embedded C escape sequence string
350Sstevel@tonic-gate  * to the corresponding character.  For example, the substring "\n" is replaced
360Sstevel@tonic-gate  * by an inline '\n' character.  The length of the resulting string is returned.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate size_t
stresc2chr(char * s)390Sstevel@tonic-gate stresc2chr(char *s)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate 	char *p, *q, c;
420Sstevel@tonic-gate 	int esc = 0;
430Sstevel@tonic-gate 	int x;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 	for (p = q = s; (c = *p) != '\0'; p++) {
460Sstevel@tonic-gate 		if (esc) {
470Sstevel@tonic-gate 			switch (c) {
480Sstevel@tonic-gate 			case '0':
490Sstevel@tonic-gate 			case '1':
500Sstevel@tonic-gate 			case '2':
510Sstevel@tonic-gate 			case '3':
520Sstevel@tonic-gate 			case '4':
530Sstevel@tonic-gate 			case '5':
540Sstevel@tonic-gate 			case '6':
550Sstevel@tonic-gate 			case '7':
560Sstevel@tonic-gate 				c -= '0';
570Sstevel@tonic-gate 				p++;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 				if (*p >= '0' && *p <= '7') {
600Sstevel@tonic-gate 					c = c * 8 + *p++ - '0';
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 					if (*p >= '0' && *p <= '7')
630Sstevel@tonic-gate 						c = c * 8 + *p - '0';
640Sstevel@tonic-gate 					else
650Sstevel@tonic-gate 						p--;
660Sstevel@tonic-gate 				} else
670Sstevel@tonic-gate 					p--;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 				*q++ = c;
700Sstevel@tonic-gate 				break;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 			case 'a':
730Sstevel@tonic-gate 				*q++ = '\a';
740Sstevel@tonic-gate 				break;
750Sstevel@tonic-gate 			case 'b':
760Sstevel@tonic-gate 				*q++ = '\b';
770Sstevel@tonic-gate 				break;
780Sstevel@tonic-gate 			case 'f':
790Sstevel@tonic-gate 				*q++ = '\f';
800Sstevel@tonic-gate 				break;
810Sstevel@tonic-gate 			case 'n':
820Sstevel@tonic-gate 				*q++ = '\n';
830Sstevel@tonic-gate 				break;
840Sstevel@tonic-gate 			case 'r':
850Sstevel@tonic-gate 				*q++ = '\r';
860Sstevel@tonic-gate 				break;
870Sstevel@tonic-gate 			case 't':
880Sstevel@tonic-gate 				*q++ = '\t';
890Sstevel@tonic-gate 				break;
900Sstevel@tonic-gate 			case 'v':
910Sstevel@tonic-gate 				*q++ = '\v';
920Sstevel@tonic-gate 				break;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 			case 'x':
950Sstevel@tonic-gate 				for (x = 0; (c = *++p) != '\0'; ) {
960Sstevel@tonic-gate 					if (c >= '0' && c <= '9')
970Sstevel@tonic-gate 						x = x * 16 + c - '0';
980Sstevel@tonic-gate 					else if (c >= 'a' && c <= 'f')
990Sstevel@tonic-gate 						x = x * 16 + c - 'a' + 10;
1000Sstevel@tonic-gate 					else if (c >= 'A' && c <= 'F')
1010Sstevel@tonic-gate 						x = x * 16 + c - 'A' + 10;
1020Sstevel@tonic-gate 					else
1030Sstevel@tonic-gate 						break;
1040Sstevel@tonic-gate 				}
1050Sstevel@tonic-gate 				*q++ = (char)x;
1060Sstevel@tonic-gate 				p--;
1070Sstevel@tonic-gate 				break;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 			case '"':
1100Sstevel@tonic-gate 			case '\\':
1110Sstevel@tonic-gate 				*q++ = c;
1120Sstevel@tonic-gate 				break;
1130Sstevel@tonic-gate 			default:
1140Sstevel@tonic-gate 				*q++ = '\\';
1150Sstevel@tonic-gate 				*q++ = c;
1160Sstevel@tonic-gate 			}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 			esc = 0;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		} else {
1210Sstevel@tonic-gate 			if ((esc = c == '\\') == 0)
1220Sstevel@tonic-gate 				*q++ = c;
1230Sstevel@tonic-gate 		}
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	*q = '\0';
1270Sstevel@tonic-gate 	return ((size_t)(q - s));
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * Create a copy of string s in which certain unprintable or special characters
1320Sstevel@tonic-gate  * have been converted to the string representation of their C escape sequence.
1330Sstevel@tonic-gate  * For example, the newline character is expanded to the string "\n".
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate char *
strchr2esc(const char * s,size_t n)1360Sstevel@tonic-gate strchr2esc(const char *s, size_t n)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	const char *p;
1390Sstevel@tonic-gate 	char *q, *s2, c;
1400Sstevel@tonic-gate 	size_t addl = 0;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	for (p = s; p < s + n; p++) {
1430Sstevel@tonic-gate 		switch (c = *p) {
1440Sstevel@tonic-gate 		case '\0':
1450Sstevel@tonic-gate 		case '\a':
1460Sstevel@tonic-gate 		case '\b':
1470Sstevel@tonic-gate 		case '\f':
1480Sstevel@tonic-gate 		case '\n':
1490Sstevel@tonic-gate 		case '\r':
1500Sstevel@tonic-gate 		case '\t':
1510Sstevel@tonic-gate 		case '\v':
1520Sstevel@tonic-gate 		case '"':
1530Sstevel@tonic-gate 		case '\\':
1540Sstevel@tonic-gate 			addl++;		/* 1 add'l char needed to follow \ */
1550Sstevel@tonic-gate 			break;
1560Sstevel@tonic-gate 		case ' ':
1570Sstevel@tonic-gate 			break;
1580Sstevel@tonic-gate 		default:
1590Sstevel@tonic-gate 			if (c < '!' || c > '~')
1600Sstevel@tonic-gate 				addl += 3; /* 3 add'l chars following \ */
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if ((s2 = malloc(n + addl + 1)) == NULL)
1650Sstevel@tonic-gate 		return (NULL);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	for (p = s, q = s2; p < s + n; p++) {
1680Sstevel@tonic-gate 		switch (c = *p) {
1690Sstevel@tonic-gate 		case '\0':
1700Sstevel@tonic-gate 			*q++ = '\\';
1710Sstevel@tonic-gate 			*q++ = '0';
1720Sstevel@tonic-gate 			break;
1730Sstevel@tonic-gate 		case '\a':
1740Sstevel@tonic-gate 			*q++ = '\\';
1750Sstevel@tonic-gate 			*q++ = 'a';
1760Sstevel@tonic-gate 			break;
1770Sstevel@tonic-gate 		case '\b':
1780Sstevel@tonic-gate 			*q++ = '\\';
1790Sstevel@tonic-gate 			*q++ = 'b';
1800Sstevel@tonic-gate 			break;
1810Sstevel@tonic-gate 		case '\f':
1820Sstevel@tonic-gate 			*q++ = '\\';
1830Sstevel@tonic-gate 			*q++ = 'f';
1840Sstevel@tonic-gate 			break;
1850Sstevel@tonic-gate 		case '\n':
1860Sstevel@tonic-gate 			*q++ = '\\';
1870Sstevel@tonic-gate 			*q++ = 'n';
1880Sstevel@tonic-gate 			break;
1890Sstevel@tonic-gate 		case '\r':
1900Sstevel@tonic-gate 			*q++ = '\\';
1910Sstevel@tonic-gate 			*q++ = 'r';
1920Sstevel@tonic-gate 			break;
1930Sstevel@tonic-gate 		case '\t':
1940Sstevel@tonic-gate 			*q++ = '\\';
1950Sstevel@tonic-gate 			*q++ = 't';
1960Sstevel@tonic-gate 			break;
1970Sstevel@tonic-gate 		case '\v':
1980Sstevel@tonic-gate 			*q++ = '\\';
1990Sstevel@tonic-gate 			*q++ = 'v';
2000Sstevel@tonic-gate 			break;
2010Sstevel@tonic-gate 		case '"':
2020Sstevel@tonic-gate 			*q++ = '\\';
2030Sstevel@tonic-gate 			*q++ = '"';
2040Sstevel@tonic-gate 			break;
2050Sstevel@tonic-gate 		case '\\':
2060Sstevel@tonic-gate 			*q++ = '\\';
2070Sstevel@tonic-gate 			*q++ = '\\';
2080Sstevel@tonic-gate 			break;
2090Sstevel@tonic-gate 		case ' ':
2100Sstevel@tonic-gate 			*q++ = c;
2110Sstevel@tonic-gate 			break;
2120Sstevel@tonic-gate 		default:
2130Sstevel@tonic-gate 			if (c < '!' || c > '~') {
2140Sstevel@tonic-gate 				*q++ = '\\';
2150Sstevel@tonic-gate 				*q++ = ((c >> 6) & 3) + '0';
2160Sstevel@tonic-gate 				*q++ = ((c >> 3) & 7) + '0';
2170Sstevel@tonic-gate 				*q++ = (c & 7) + '0';
2180Sstevel@tonic-gate 			} else
2190Sstevel@tonic-gate 				*q++ = c;
2200Sstevel@tonic-gate 		}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		if (c == '\0')
2230Sstevel@tonic-gate 			break; /* don't continue past \0 even if p < s + n */
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	*q = '\0';
2270Sstevel@tonic-gate 	return (s2);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * Return the basename (name after final /) of the given string.  We use
2320Sstevel@tonic-gate  * strbasename rather than basename to avoid conflicting with libgen.h's
2330Sstevel@tonic-gate  * non-const function prototype.
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate const char *
strbasename(const char * s)2360Sstevel@tonic-gate strbasename(const char *s)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	const char *p = strrchr(s, '/');
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (p == NULL)
2410Sstevel@tonic-gate 		return (s);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	return (++p);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * This function tests a string against the regular expression used for idents
2480Sstevel@tonic-gate  * and integers in the D lexer, and should match the superset of RGX_IDENT and
2490Sstevel@tonic-gate  * RGX_INT in dt_lex.l.  If an invalid character is found, the function returns
2500Sstevel@tonic-gate  * a pointer to it.  Otherwise NULL is returned for a valid string.
2510Sstevel@tonic-gate  */
2520Sstevel@tonic-gate const char *
strbadidnum(const char * s)2530Sstevel@tonic-gate strbadidnum(const char *s)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	char *p;
2560Sstevel@tonic-gate 	int c;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if (*s == '\0')
2590Sstevel@tonic-gate 		return (s);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	errno = 0;
2620Sstevel@tonic-gate 	(void) strtoull(s, &p, 0);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (errno == 0 && *p == '\0')
2650Sstevel@tonic-gate 		return (NULL); /* matches RGX_INT */
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	while ((c = *s++) != '\0') {
2680Sstevel@tonic-gate 		if (isalnum(c) == 0 && c != '_' && c != '`')
2690Sstevel@tonic-gate 			return (s - 1);
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	return (NULL); /* matches RGX_IDENT */
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * Determine whether the string contains a glob matching pattern or is just a
2770Sstevel@tonic-gate  * simple string.  See gmatch(3GEN) and sh(1) for the glob syntax definition.
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate int
strisglob(const char * s)2800Sstevel@tonic-gate strisglob(const char *s)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	char c;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	while ((c = *s++) != '\0') {
2850Sstevel@tonic-gate 		if (c == '[' || c == '?' || c == '*' || c == '\\')
2860Sstevel@tonic-gate 			return (1);
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	return (0);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate  * Hyphenate a string in-place by converting any instances of "__" to "-",
2940Sstevel@tonic-gate  * which we use for probe names to improve readability, and return the string.
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate char *
strhyphenate(char * s)2970Sstevel@tonic-gate strhyphenate(char *s)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	char *p, *q;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	for (p = s, q = p + strlen(p); p < q; p++) {
3020Sstevel@tonic-gate 		if (p[0] == '_' && p[1] == '_') {
3030Sstevel@tonic-gate 			p[0] = '-';
3040Sstevel@tonic-gate 			bcopy(p + 2, p + 1, (size_t)(q - p) - 1);
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	return (s);
3090Sstevel@tonic-gate }
310