8158 Want named threads API
[unleashed.git] / usr / src / lib / libdtrace / common / dt_string.c
blob782d66c2b8a6fd8d3c1b83c47ffd95085d178e75
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <strings.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <ctype.h>
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.
38 size_t
39 stresc2chr(char *s)
41 char *p, *q, c;
42 int esc = 0;
43 int x;
45 for (p = q = s; (c = *p) != '\0'; p++) {
46 if (esc) {
47 switch (c) {
48 case '0':
49 case '1':
50 case '2':
51 case '3':
52 case '4':
53 case '5':
54 case '6':
55 case '7':
56 c -= '0';
57 p++;
59 if (*p >= '0' && *p <= '7') {
60 c = c * 8 + *p++ - '0';
62 if (*p >= '0' && *p <= '7')
63 c = c * 8 + *p - '0';
64 else
65 p--;
66 } else
67 p--;
69 *q++ = c;
70 break;
72 case 'a':
73 *q++ = '\a';
74 break;
75 case 'b':
76 *q++ = '\b';
77 break;
78 case 'f':
79 *q++ = '\f';
80 break;
81 case 'n':
82 *q++ = '\n';
83 break;
84 case 'r':
85 *q++ = '\r';
86 break;
87 case 't':
88 *q++ = '\t';
89 break;
90 case 'v':
91 *q++ = '\v';
92 break;
94 case 'x':
95 for (x = 0; (c = *++p) != '\0'; ) {
96 if (c >= '0' && c <= '9')
97 x = x * 16 + c - '0';
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;
102 else
103 break;
105 *q++ = (char)x;
106 p--;
107 break;
109 case '"':
110 case '\\':
111 *q++ = c;
112 break;
113 default:
114 *q++ = '\\';
115 *q++ = c;
118 esc = 0;
120 } else {
121 if ((esc = c == '\\') == 0)
122 *q++ = c;
126 *q = '\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".
135 char *
136 strchr2esc(const char *s, size_t n)
138 const char *p;
139 char *q, *s2, c;
140 size_t addl = 0;
142 for (p = s; p < s + n; p++) {
143 switch (c = *p) {
144 case '\0':
145 case '\a':
146 case '\b':
147 case '\f':
148 case '\n':
149 case '\r':
150 case '\t':
151 case '\v':
152 case '"':
153 case '\\':
154 addl++; /* 1 add'l char needed to follow \ */
155 break;
156 case ' ':
157 break;
158 default:
159 if (c < '!' || c > '~')
160 addl += 3; /* 3 add'l chars following \ */
164 if ((s2 = malloc(n + addl + 1)) == NULL)
165 return (NULL);
167 for (p = s, q = s2; p < s + n; p++) {
168 switch (c = *p) {
169 case '\0':
170 *q++ = '\\';
171 *q++ = '0';
172 break;
173 case '\a':
174 *q++ = '\\';
175 *q++ = 'a';
176 break;
177 case '\b':
178 *q++ = '\\';
179 *q++ = 'b';
180 break;
181 case '\f':
182 *q++ = '\\';
183 *q++ = 'f';
184 break;
185 case '\n':
186 *q++ = '\\';
187 *q++ = 'n';
188 break;
189 case '\r':
190 *q++ = '\\';
191 *q++ = 'r';
192 break;
193 case '\t':
194 *q++ = '\\';
195 *q++ = 't';
196 break;
197 case '\v':
198 *q++ = '\\';
199 *q++ = 'v';
200 break;
201 case '"':
202 *q++ = '\\';
203 *q++ = '"';
204 break;
205 case '\\':
206 *q++ = '\\';
207 *q++ = '\\';
208 break;
209 case ' ':
210 *q++ = c;
211 break;
212 default:
213 if (c < '!' || c > '~') {
214 *q++ = '\\';
215 *q++ = ((c >> 6) & 3) + '0';
216 *q++ = ((c >> 3) & 7) + '0';
217 *q++ = (c & 7) + '0';
218 } else
219 *q++ = c;
222 if (c == '\0')
223 break; /* don't continue past \0 even if p < s + n */
226 *q = '\0';
227 return (s2);
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.
235 const char *
236 strbasename(const char *s)
238 const char *p = strrchr(s, '/');
240 if (p == NULL)
241 return (s);
243 return (++p);
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.
252 const char *
253 strbadidnum(const char *s)
255 char *p;
256 int c;
258 if (*s == '\0')
259 return (s);
261 errno = 0;
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 != '`')
269 return (s - 1);
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)
282 char c;
284 while ((c = *s++) != '\0') {
285 if (c == '[' || c == '?' || c == '*' || c == '\\')
286 return (1);
289 return (0);
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.
296 char *
297 strhyphenate(char *s)
299 char *p, *q;
301 for (p = s, q = p + strlen(p); p < q; p++) {
302 if (p[0] == '_' && p[1] == '_') {
303 p[0] = '-';
304 bcopy(p + 2, p + 1, (size_t)(q - p) - 1);
308 return (s);