1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5210767Srpaulo * Common Development and Distribution License (the "License").
6210767Srpaulo * You may not use this file except in compliance with the License.
7178479Sjb *
8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178479Sjb * or http://www.opensolaris.org/os/licensing.
10178479Sjb * See the License for the specific language governing permissions
11178479Sjb * and limitations under the License.
12178479Sjb *
13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178479Sjb * If applicable, add the following below this CDDL HEADER, with the
16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178479Sjb *
19178479Sjb * CDDL HEADER END
20178479Sjb */
21210767Srpaulo
22178479Sjb/*
23210767Srpaulo * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24178479Sjb */
25178479Sjb
26178479Sjb#include <strings.h>
27178479Sjb#include <stdlib.h>
28178479Sjb#include <errno.h>
29178479Sjb#include <ctype.h>
30178479Sjb
31178479Sjb#include <dt_string.h>
32178479Sjb
33178479Sjb/*
34178479Sjb * Transform string s inline, converting each embedded C escape sequence string
35178479Sjb * to the corresponding character.  For example, the substring "\n" is replaced
36178479Sjb * by an inline '\n' character.  The length of the resulting string is returned.
37178479Sjb */
38178479Sjbsize_t
39178479Sjbstresc2chr(char *s)
40178479Sjb{
41178479Sjb	char *p, *q, c;
42178479Sjb	int esc = 0;
43178479Sjb	int x;
44178479Sjb
45178479Sjb	for (p = q = s; (c = *p) != '\0'; p++) {
46178479Sjb		if (esc) {
47178479Sjb			switch (c) {
48178479Sjb			case '0':
49178479Sjb			case '1':
50178479Sjb			case '2':
51178479Sjb			case '3':
52178479Sjb			case '4':
53178479Sjb			case '5':
54178479Sjb			case '6':
55178479Sjb			case '7':
56178479Sjb				c -= '0';
57178479Sjb				p++;
58178479Sjb
59178479Sjb				if (*p >= '0' && *p <= '7') {
60178479Sjb					c = c * 8 + *p++ - '0';
61178479Sjb
62178479Sjb					if (*p >= '0' && *p <= '7')
63178479Sjb						c = c * 8 + *p - '0';
64178479Sjb					else
65178479Sjb						p--;
66178479Sjb				} else
67178479Sjb					p--;
68178479Sjb
69178479Sjb				*q++ = c;
70178479Sjb				break;
71178479Sjb
72178479Sjb			case 'a':
73178479Sjb				*q++ = '\a';
74178479Sjb				break;
75178479Sjb			case 'b':
76178479Sjb				*q++ = '\b';
77178479Sjb				break;
78178479Sjb			case 'f':
79178479Sjb				*q++ = '\f';
80178479Sjb				break;
81178479Sjb			case 'n':
82178479Sjb				*q++ = '\n';
83178479Sjb				break;
84178479Sjb			case 'r':
85178479Sjb				*q++ = '\r';
86178479Sjb				break;
87178479Sjb			case 't':
88178479Sjb				*q++ = '\t';
89178479Sjb				break;
90178479Sjb			case 'v':
91178479Sjb				*q++ = '\v';
92178479Sjb				break;
93178479Sjb
94178479Sjb			case 'x':
95178479Sjb				for (x = 0; (c = *++p) != '\0'; ) {
96178479Sjb					if (c >= '0' && c <= '9')
97178479Sjb						x = x * 16 + c - '0';
98178479Sjb					else if (c >= 'a' && c <= 'f')
99178479Sjb						x = x * 16 + c - 'a' + 10;
100178479Sjb					else if (c >= 'A' && c <= 'F')
101178479Sjb						x = x * 16 + c - 'A' + 10;
102178479Sjb					else
103178479Sjb						break;
104178479Sjb				}
105178479Sjb				*q++ = (char)x;
106178479Sjb				p--;
107178479Sjb				break;
108178479Sjb
109178479Sjb			case '"':
110178479Sjb			case '\\':
111178479Sjb				*q++ = c;
112178479Sjb				break;
113178479Sjb			default:
114178479Sjb				*q++ = '\\';
115178479Sjb				*q++ = c;
116178479Sjb			}
117178479Sjb
118178479Sjb			esc = 0;
119178479Sjb
120178479Sjb		} else {
121178479Sjb			if ((esc = c == '\\') == 0)
122178479Sjb				*q++ = c;
123178479Sjb		}
124178479Sjb	}
125178479Sjb
126178479Sjb	*q = '\0';
127178479Sjb	return ((size_t)(q - s));
128178479Sjb}
129178479Sjb
130178479Sjb/*
131178479Sjb * Create a copy of string s in which certain unprintable or special characters
132178479Sjb * have been converted to the string representation of their C escape sequence.
133178479Sjb * For example, the newline character is expanded to the string "\n".
134178479Sjb */
135178479Sjbchar *
136178479Sjbstrchr2esc(const char *s, size_t n)
137178479Sjb{
138178479Sjb	const char *p;
139178479Sjb	char *q, *s2, c;
140178479Sjb	size_t addl = 0;
141178479Sjb
142178479Sjb	for (p = s; p < s + n; p++) {
143178479Sjb		switch (c = *p) {
144178479Sjb		case '\0':
145178479Sjb		case '\a':
146178479Sjb		case '\b':
147178479Sjb		case '\f':
148178479Sjb		case '\n':
149178479Sjb		case '\r':
150178479Sjb		case '\t':
151178479Sjb		case '\v':
152178479Sjb		case '"':
153178479Sjb		case '\\':
154178479Sjb			addl++;		/* 1 add'l char needed to follow \ */
155178479Sjb			break;
156178479Sjb		case ' ':
157178479Sjb			break;
158178479Sjb		default:
159178479Sjb			if (c < '!' || c > '~')
160178479Sjb				addl += 3; /* 3 add'l chars following \ */
161178479Sjb		}
162178479Sjb	}
163178479Sjb
164178479Sjb	if ((s2 = malloc(n + addl + 1)) == NULL)
165178479Sjb		return (NULL);
166178479Sjb
167178479Sjb	for (p = s, q = s2; p < s + n; p++) {
168178479Sjb		switch (c = *p) {
169178479Sjb		case '\0':
170178479Sjb			*q++ = '\\';
171178479Sjb			*q++ = '0';
172178479Sjb			break;
173178479Sjb		case '\a':
174178479Sjb			*q++ = '\\';
175178479Sjb			*q++ = 'a';
176178479Sjb			break;
177178479Sjb		case '\b':
178178479Sjb			*q++ = '\\';
179178479Sjb			*q++ = 'b';
180178479Sjb			break;
181178479Sjb		case '\f':
182178479Sjb			*q++ = '\\';
183178479Sjb			*q++ = 'f';
184178479Sjb			break;
185178479Sjb		case '\n':
186178479Sjb			*q++ = '\\';
187178479Sjb			*q++ = 'n';
188178479Sjb			break;
189178479Sjb		case '\r':
190178479Sjb			*q++ = '\\';
191178479Sjb			*q++ = 'r';
192178479Sjb			break;
193178479Sjb		case '\t':
194178479Sjb			*q++ = '\\';
195178479Sjb			*q++ = 't';
196178479Sjb			break;
197178479Sjb		case '\v':
198178479Sjb			*q++ = '\\';
199178479Sjb			*q++ = 'v';
200178479Sjb			break;
201178479Sjb		case '"':
202178479Sjb			*q++ = '\\';
203178479Sjb			*q++ = '"';
204178479Sjb			break;
205178479Sjb		case '\\':
206178479Sjb			*q++ = '\\';
207178479Sjb			*q++ = '\\';
208178479Sjb			break;
209178479Sjb		case ' ':
210178479Sjb			*q++ = c;
211178479Sjb			break;
212178479Sjb		default:
213178479Sjb			if (c < '!' || c > '~') {
214178479Sjb				*q++ = '\\';
215178479Sjb				*q++ = ((c >> 6) & 3) + '0';
216178479Sjb				*q++ = ((c >> 3) & 7) + '0';
217178479Sjb				*q++ = (c & 7) + '0';
218178479Sjb			} else
219178479Sjb				*q++ = c;
220178479Sjb		}
221178479Sjb
222178479Sjb		if (c == '\0')
223178479Sjb			break; /* don't continue past \0 even if p < s + n */
224178479Sjb	}
225178479Sjb
226178479Sjb	*q = '\0';
227178479Sjb	return (s2);
228178479Sjb}
229178479Sjb
230178479Sjb/*
231178479Sjb * Return the basename (name after final /) of the given string.  We use
232178479Sjb * strbasename rather than basename to avoid conflicting with libgen.h's
233178479Sjb * non-const function prototype.
234178479Sjb */
235178479Sjbconst char *
236178479Sjbstrbasename(const char *s)
237178479Sjb{
238178479Sjb	const char *p = strrchr(s, '/');
239178479Sjb
240178479Sjb	if (p == NULL)
241178479Sjb		return (s);
242178479Sjb
243178479Sjb	return (++p);
244178479Sjb}
245178479Sjb
246178479Sjb/*
247178479Sjb * This function tests a string against the regular expression used for idents
248178479Sjb * and integers in the D lexer, and should match the superset of RGX_IDENT and
249178479Sjb * RGX_INT in dt_lex.l.  If an invalid character is found, the function returns
250178479Sjb * a pointer to it.  Otherwise NULL is returned for a valid string.
251178479Sjb */
252178479Sjbconst char *
253178479Sjbstrbadidnum(const char *s)
254178479Sjb{
255178479Sjb	char *p;
256178479Sjb	int c;
257178479Sjb
258178479Sjb	if (*s == '\0')
259178479Sjb		return (s);
260178479Sjb
261178479Sjb	errno = 0;
262178479Sjb	(void) strtoull(s, &p, 0);
263178479Sjb
264178479Sjb	if (errno == 0 && *p == '\0')
265178479Sjb		return (NULL); /* matches RGX_INT */
266178479Sjb
267178479Sjb	while ((c = *s++) != '\0') {
268178479Sjb		if (isalnum(c) == 0 && c != '_' && c != '`')
269178479Sjb			return (s - 1);
270178479Sjb	}
271178479Sjb
272178479Sjb	return (NULL); /* matches RGX_IDENT */
273178479Sjb}
274178479Sjb
275178479Sjb/*
276178479Sjb * Determine whether the string contains a glob matching pattern or is just a
277178479Sjb * simple string.  See gmatch(3GEN) and sh(1) for the glob syntax definition.
278178479Sjb */
279178479Sjbint
280178479Sjbstrisglob(const char *s)
281178479Sjb{
282178479Sjb	char c;
283178479Sjb
284178479Sjb	while ((c = *s++) != '\0') {
285178479Sjb		if (c == '[' || c == '?' || c == '*' || c == '\\')
286178479Sjb			return (1);
287178479Sjb	}
288178479Sjb
289178479Sjb	return (0);
290178479Sjb}
291178479Sjb
292178479Sjb/*
293178479Sjb * Hyphenate a string in-place by converting any instances of "__" to "-",
294178479Sjb * which we use for probe names to improve readability, and return the string.
295178479Sjb */
296178479Sjbchar *
297178479Sjbstrhyphenate(char *s)
298178479Sjb{
299178479Sjb	char *p, *q;
300178479Sjb
301178479Sjb	for (p = s, q = p + strlen(p); p < q; p++) {
302178479Sjb		if (p[0] == '_' && p[1] == '_') {
303178479Sjb			p[0] = '-';
304178479Sjb			bcopy(p + 2, p + 1, (size_t)(q - p) - 1);
305178479Sjb		}
306178479Sjb	}
307178479Sjb
308178479Sjb	return (s);
309178479Sjb}
310