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> 32*12902SBryan.Cantrill@Sun.COM #include <dt_impl.h> 330Sstevel@tonic-gate 340Sstevel@tonic-gate /* 350Sstevel@tonic-gate * Create a copy of string s, but only duplicate the first n bytes. 360Sstevel@tonic-gate */ 370Sstevel@tonic-gate char * 380Sstevel@tonic-gate strndup(const char *s, size_t n) 390Sstevel@tonic-gate { 400Sstevel@tonic-gate char *s2 = malloc(n + 1); 410Sstevel@tonic-gate 42*12902SBryan.Cantrill@Sun.COM if (s2 == NULL) 43*12902SBryan.Cantrill@Sun.COM longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 44*12902SBryan.Cantrill@Sun.COM 450Sstevel@tonic-gate (void) strncpy(s2, s, n); 460Sstevel@tonic-gate s2[n] = '\0'; 470Sstevel@tonic-gate return (s2); 480Sstevel@tonic-gate } 490Sstevel@tonic-gate 500Sstevel@tonic-gate /* 510Sstevel@tonic-gate * Transform string s inline, converting each embedded C escape sequence string 520Sstevel@tonic-gate * to the corresponding character. For example, the substring "\n" is replaced 530Sstevel@tonic-gate * by an inline '\n' character. The length of the resulting string is returned. 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate size_t 560Sstevel@tonic-gate stresc2chr(char *s) 570Sstevel@tonic-gate { 580Sstevel@tonic-gate char *p, *q, c; 590Sstevel@tonic-gate int esc = 0; 600Sstevel@tonic-gate int x; 610Sstevel@tonic-gate 620Sstevel@tonic-gate for (p = q = s; (c = *p) != '\0'; p++) { 630Sstevel@tonic-gate if (esc) { 640Sstevel@tonic-gate switch (c) { 650Sstevel@tonic-gate case '0': 660Sstevel@tonic-gate case '1': 670Sstevel@tonic-gate case '2': 680Sstevel@tonic-gate case '3': 690Sstevel@tonic-gate case '4': 700Sstevel@tonic-gate case '5': 710Sstevel@tonic-gate case '6': 720Sstevel@tonic-gate case '7': 730Sstevel@tonic-gate c -= '0'; 740Sstevel@tonic-gate p++; 750Sstevel@tonic-gate 760Sstevel@tonic-gate if (*p >= '0' && *p <= '7') { 770Sstevel@tonic-gate c = c * 8 + *p++ - '0'; 780Sstevel@tonic-gate 790Sstevel@tonic-gate if (*p >= '0' && *p <= '7') 800Sstevel@tonic-gate c = c * 8 + *p - '0'; 810Sstevel@tonic-gate else 820Sstevel@tonic-gate p--; 830Sstevel@tonic-gate } else 840Sstevel@tonic-gate p--; 850Sstevel@tonic-gate 860Sstevel@tonic-gate *q++ = c; 870Sstevel@tonic-gate break; 880Sstevel@tonic-gate 890Sstevel@tonic-gate case 'a': 900Sstevel@tonic-gate *q++ = '\a'; 910Sstevel@tonic-gate break; 920Sstevel@tonic-gate case 'b': 930Sstevel@tonic-gate *q++ = '\b'; 940Sstevel@tonic-gate break; 950Sstevel@tonic-gate case 'f': 960Sstevel@tonic-gate *q++ = '\f'; 970Sstevel@tonic-gate break; 980Sstevel@tonic-gate case 'n': 990Sstevel@tonic-gate *q++ = '\n'; 1000Sstevel@tonic-gate break; 1010Sstevel@tonic-gate case 'r': 1020Sstevel@tonic-gate *q++ = '\r'; 1030Sstevel@tonic-gate break; 1040Sstevel@tonic-gate case 't': 1050Sstevel@tonic-gate *q++ = '\t'; 1060Sstevel@tonic-gate break; 1070Sstevel@tonic-gate case 'v': 1080Sstevel@tonic-gate *q++ = '\v'; 1090Sstevel@tonic-gate break; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate case 'x': 1120Sstevel@tonic-gate for (x = 0; (c = *++p) != '\0'; ) { 1130Sstevel@tonic-gate if (c >= '0' && c <= '9') 1140Sstevel@tonic-gate x = x * 16 + c - '0'; 1150Sstevel@tonic-gate else if (c >= 'a' && c <= 'f') 1160Sstevel@tonic-gate x = x * 16 + c - 'a' + 10; 1170Sstevel@tonic-gate else if (c >= 'A' && c <= 'F') 1180Sstevel@tonic-gate x = x * 16 + c - 'A' + 10; 1190Sstevel@tonic-gate else 1200Sstevel@tonic-gate break; 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate *q++ = (char)x; 1230Sstevel@tonic-gate p--; 1240Sstevel@tonic-gate break; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate case '"': 1270Sstevel@tonic-gate case '\\': 1280Sstevel@tonic-gate *q++ = c; 1290Sstevel@tonic-gate break; 1300Sstevel@tonic-gate default: 1310Sstevel@tonic-gate *q++ = '\\'; 1320Sstevel@tonic-gate *q++ = c; 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate esc = 0; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate } else { 1380Sstevel@tonic-gate if ((esc = c == '\\') == 0) 1390Sstevel@tonic-gate *q++ = c; 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate *q = '\0'; 1440Sstevel@tonic-gate return ((size_t)(q - s)); 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate /* 1480Sstevel@tonic-gate * Create a copy of string s in which certain unprintable or special characters 1490Sstevel@tonic-gate * have been converted to the string representation of their C escape sequence. 1500Sstevel@tonic-gate * For example, the newline character is expanded to the string "\n". 1510Sstevel@tonic-gate */ 1520Sstevel@tonic-gate char * 1530Sstevel@tonic-gate strchr2esc(const char *s, size_t n) 1540Sstevel@tonic-gate { 1550Sstevel@tonic-gate const char *p; 1560Sstevel@tonic-gate char *q, *s2, c; 1570Sstevel@tonic-gate size_t addl = 0; 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate for (p = s; p < s + n; p++) { 1600Sstevel@tonic-gate switch (c = *p) { 1610Sstevel@tonic-gate case '\0': 1620Sstevel@tonic-gate case '\a': 1630Sstevel@tonic-gate case '\b': 1640Sstevel@tonic-gate case '\f': 1650Sstevel@tonic-gate case '\n': 1660Sstevel@tonic-gate case '\r': 1670Sstevel@tonic-gate case '\t': 1680Sstevel@tonic-gate case '\v': 1690Sstevel@tonic-gate case '"': 1700Sstevel@tonic-gate case '\\': 1710Sstevel@tonic-gate addl++; /* 1 add'l char needed to follow \ */ 1720Sstevel@tonic-gate break; 1730Sstevel@tonic-gate case ' ': 1740Sstevel@tonic-gate break; 1750Sstevel@tonic-gate default: 1760Sstevel@tonic-gate if (c < '!' || c > '~') 1770Sstevel@tonic-gate addl += 3; /* 3 add'l chars following \ */ 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate if ((s2 = malloc(n + addl + 1)) == NULL) 1820Sstevel@tonic-gate return (NULL); 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate for (p = s, q = s2; p < s + n; p++) { 1850Sstevel@tonic-gate switch (c = *p) { 1860Sstevel@tonic-gate case '\0': 1870Sstevel@tonic-gate *q++ = '\\'; 1880Sstevel@tonic-gate *q++ = '0'; 1890Sstevel@tonic-gate break; 1900Sstevel@tonic-gate case '\a': 1910Sstevel@tonic-gate *q++ = '\\'; 1920Sstevel@tonic-gate *q++ = 'a'; 1930Sstevel@tonic-gate break; 1940Sstevel@tonic-gate case '\b': 1950Sstevel@tonic-gate *q++ = '\\'; 1960Sstevel@tonic-gate *q++ = 'b'; 1970Sstevel@tonic-gate break; 1980Sstevel@tonic-gate case '\f': 1990Sstevel@tonic-gate *q++ = '\\'; 2000Sstevel@tonic-gate *q++ = 'f'; 2010Sstevel@tonic-gate break; 2020Sstevel@tonic-gate case '\n': 2030Sstevel@tonic-gate *q++ = '\\'; 2040Sstevel@tonic-gate *q++ = 'n'; 2050Sstevel@tonic-gate break; 2060Sstevel@tonic-gate case '\r': 2070Sstevel@tonic-gate *q++ = '\\'; 2080Sstevel@tonic-gate *q++ = 'r'; 2090Sstevel@tonic-gate break; 2100Sstevel@tonic-gate case '\t': 2110Sstevel@tonic-gate *q++ = '\\'; 2120Sstevel@tonic-gate *q++ = 't'; 2130Sstevel@tonic-gate break; 2140Sstevel@tonic-gate case '\v': 2150Sstevel@tonic-gate *q++ = '\\'; 2160Sstevel@tonic-gate *q++ = 'v'; 2170Sstevel@tonic-gate break; 2180Sstevel@tonic-gate case '"': 2190Sstevel@tonic-gate *q++ = '\\'; 2200Sstevel@tonic-gate *q++ = '"'; 2210Sstevel@tonic-gate break; 2220Sstevel@tonic-gate case '\\': 2230Sstevel@tonic-gate *q++ = '\\'; 2240Sstevel@tonic-gate *q++ = '\\'; 2250Sstevel@tonic-gate break; 2260Sstevel@tonic-gate case ' ': 2270Sstevel@tonic-gate *q++ = c; 2280Sstevel@tonic-gate break; 2290Sstevel@tonic-gate default: 2300Sstevel@tonic-gate if (c < '!' || c > '~') { 2310Sstevel@tonic-gate *q++ = '\\'; 2320Sstevel@tonic-gate *q++ = ((c >> 6) & 3) + '0'; 2330Sstevel@tonic-gate *q++ = ((c >> 3) & 7) + '0'; 2340Sstevel@tonic-gate *q++ = (c & 7) + '0'; 2350Sstevel@tonic-gate } else 2360Sstevel@tonic-gate *q++ = c; 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate if (c == '\0') 2400Sstevel@tonic-gate break; /* don't continue past \0 even if p < s + n */ 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate *q = '\0'; 2440Sstevel@tonic-gate return (s2); 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * Return the basename (name after final /) of the given string. We use 2490Sstevel@tonic-gate * strbasename rather than basename to avoid conflicting with libgen.h's 2500Sstevel@tonic-gate * non-const function prototype. 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate const char * 2530Sstevel@tonic-gate strbasename(const char *s) 2540Sstevel@tonic-gate { 2550Sstevel@tonic-gate const char *p = strrchr(s, '/'); 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate if (p == NULL) 2580Sstevel@tonic-gate return (s); 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate return (++p); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * This function tests a string against the regular expression used for idents 2650Sstevel@tonic-gate * and integers in the D lexer, and should match the superset of RGX_IDENT and 2660Sstevel@tonic-gate * RGX_INT in dt_lex.l. If an invalid character is found, the function returns 2670Sstevel@tonic-gate * a pointer to it. Otherwise NULL is returned for a valid string. 2680Sstevel@tonic-gate */ 2690Sstevel@tonic-gate const char * 2700Sstevel@tonic-gate strbadidnum(const char *s) 2710Sstevel@tonic-gate { 2720Sstevel@tonic-gate char *p; 2730Sstevel@tonic-gate int c; 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate if (*s == '\0') 2760Sstevel@tonic-gate return (s); 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate errno = 0; 2790Sstevel@tonic-gate (void) strtoull(s, &p, 0); 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate if (errno == 0 && *p == '\0') 2820Sstevel@tonic-gate return (NULL); /* matches RGX_INT */ 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate while ((c = *s++) != '\0') { 2850Sstevel@tonic-gate if (isalnum(c) == 0 && c != '_' && c != '`') 2860Sstevel@tonic-gate return (s - 1); 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate return (NULL); /* matches RGX_IDENT */ 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * Determine whether the string contains a glob matching pattern or is just a 2940Sstevel@tonic-gate * simple string. See gmatch(3GEN) and sh(1) for the glob syntax definition. 2950Sstevel@tonic-gate */ 2960Sstevel@tonic-gate int 2970Sstevel@tonic-gate strisglob(const char *s) 2980Sstevel@tonic-gate { 2990Sstevel@tonic-gate char c; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate while ((c = *s++) != '\0') { 3020Sstevel@tonic-gate if (c == '[' || c == '?' || c == '*' || c == '\\') 3030Sstevel@tonic-gate return (1); 3040Sstevel@tonic-gate } 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate return (0); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate /* 3100Sstevel@tonic-gate * Hyphenate a string in-place by converting any instances of "__" to "-", 3110Sstevel@tonic-gate * which we use for probe names to improve readability, and return the string. 3120Sstevel@tonic-gate */ 3130Sstevel@tonic-gate char * 3140Sstevel@tonic-gate strhyphenate(char *s) 3150Sstevel@tonic-gate { 3160Sstevel@tonic-gate char *p, *q; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate for (p = s, q = p + strlen(p); p < q; p++) { 3190Sstevel@tonic-gate if (p[0] == '_' && p[1] == '_') { 3200Sstevel@tonic-gate p[0] = '-'; 3210Sstevel@tonic-gate bcopy(p + 2, p + 1, (size_t)(q - p) - 1); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate return (s); 3260Sstevel@tonic-gate } 327