4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
31 #include <dt_string.h>
34 * Transform string s inline, converting each embedded C escape sequence string
35 * to the corresponding character. For example, the substring "\n" is replaced
36 * by an inline '\n' character. The length of the resulting string is returned.
45 for (p = q = s; (c = *p) != '\0'; p++) {
59 if (*p >= '0' && *p <= '7') {
60 c = c * 8 + *p++ - '0';
62 if (*p >= '0' && *p <= '7')
95 for (x = 0; (c = *++p) != '\0'; ) {
96 if (c >= '0' && c <= '9')
98 else if (c >= 'a' && c <= 'f')
99 x = x * 16 + c - 'a' + 10;
100 else if (c >= 'A' && c <= 'F')
101 x = x * 16 + c - 'A' + 10;
121 if ((esc = c == '\\') == 0)
127 return ((size_t)(q - s));
131 * Create a copy of string s in which certain unprintable or special characters
132 * have been converted to the string representation of their C escape sequence.
133 * For example, the newline character is expanded to the string "\n".
136 strchr2esc(const char *s, size_t n)
142 for (p = s; p < s + n; p++) {
154 addl++; /* 1 add'l char needed to follow \ */
159 if (c < '!' || c > '~')
160 addl += 3; /* 3 add'l chars following \ */
164 if ((s2 = malloc(n + addl + 1)) == NULL)
167 for (p = s, q = s2; p < s + n; p++) {
213 if (c < '!' || c > '~') {
215 *q++ = ((c >> 6) & 3) + '0';
216 *q++ = ((c >> 3) & 7) + '0';
217 *q++ = (c & 7) + '0';
223 break; /* don't continue past \0 even if p < s + n */
231 * Return the basename (name after final /) of the given string. We use
232 * strbasename rather than basename to avoid conflicting with libgen.h's
233 * non-const function prototype.
236 strbasename(const char *s)
238 const char *p = strrchr(s, '/');
247 * This function tests a string against the regular expression used for idents
248 * and integers in the D lexer, and should match the superset of RGX_IDENT and
249 * RGX_INT in dt_lex.l. If an invalid character is found, the function returns
250 * a pointer to it. Otherwise NULL is returned for a valid string.
253 strbadidnum(const char *s)
262 (void) strtoull(s, &p, 0);
264 if (errno == 0 && *p == '\0')
265 return (NULL); /* matches RGX_INT */
267 while ((c = *s++) != '\0') {
268 if (isalnum(c) == 0 && c != '_' && c != '`')
272 return (NULL); /* matches RGX_IDENT */
276 * Determine whether the string contains a glob matching pattern or is just a
277 * simple string. See gmatch(3GEN) and sh(1) for the glob syntax definition.
280 strisglob(const char *s)
284 while ((c = *s++) != '\0') {
285 if (c == '[' || c == '?' || c == '*' || c == '\\')
293 * Hyphenate a string in-place by converting any instances of "__" to "-",
294 * which we use for probe names to improve readability, and return the string.
297 strhyphenate(char *s)
301 for (p = s, q = p + strlen(p); p < q; p++) {
302 if (p[0] == '_' && p[1] == '_') {
304 bcopy(p + 2, p + 1, (size_t)(q - p) - 1);