1*e23c41c9SAli Bahrami /*
2*e23c41c9SAli Bahrami * CDDL HEADER START
3*e23c41c9SAli Bahrami *
4*e23c41c9SAli Bahrami * The contents of this file are subject to the terms of the
5*e23c41c9SAli Bahrami * Common Development and Distribution License (the "License").
6*e23c41c9SAli Bahrami * You may not use this file except in compliance with the License.
7*e23c41c9SAli Bahrami *
8*e23c41c9SAli Bahrami * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*e23c41c9SAli Bahrami * or http://www.opensolaris.org/os/licensing.
10*e23c41c9SAli Bahrami * See the License for the specific language governing permissions
11*e23c41c9SAli Bahrami * and limitations under the License.
12*e23c41c9SAli Bahrami *
13*e23c41c9SAli Bahrami * When distributing Covered Code, include this CDDL HEADER in each
14*e23c41c9SAli Bahrami * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*e23c41c9SAli Bahrami * If applicable, add the following below this CDDL HEADER, with the
16*e23c41c9SAli Bahrami * fields enclosed by brackets "[]" replaced with your own identifying
17*e23c41c9SAli Bahrami * information: Portions Copyright [yyyy] [name of copyright owner]
18*e23c41c9SAli Bahrami *
19*e23c41c9SAli Bahrami * CDDL HEADER END
20*e23c41c9SAli Bahrami */
21*e23c41c9SAli Bahrami
22*e23c41c9SAli Bahrami /*
23*e23c41c9SAli Bahrami * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24*e23c41c9SAli Bahrami * Use is subject to license terms.
25*e23c41c9SAli Bahrami */
26*e23c41c9SAli Bahrami
27*e23c41c9SAli Bahrami
28*e23c41c9SAli Bahrami /*
29*e23c41c9SAli Bahrami * General purpse string manipulation routines
30*e23c41c9SAli Bahrami */
31*e23c41c9SAli Bahrami
32*e23c41c9SAli Bahrami #include <stdio.h>
33*e23c41c9SAli Bahrami #include <_conv.h>
34*e23c41c9SAli Bahrami
35*e23c41c9SAli Bahrami
36*e23c41c9SAli Bahrami /*
37*e23c41c9SAli Bahrami * Implementation of isspace() that does not require <ctype.h>
38*e23c41c9SAli Bahrami * or <sys/ctype.h>, appropriate for simple non-localized use.
39*e23c41c9SAli Bahrami */
40*e23c41c9SAli Bahrami int
conv_strproc_isspace(int c)41*e23c41c9SAli Bahrami conv_strproc_isspace(int c)
42*e23c41c9SAli Bahrami {
43*e23c41c9SAli Bahrami return ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n'));
44*e23c41c9SAli Bahrami }
45*e23c41c9SAli Bahrami
46*e23c41c9SAli Bahrami /*
47*e23c41c9SAli Bahrami * Remove leading and trailing whitespace from the given string.
48*e23c41c9SAli Bahrami *
49*e23c41c9SAli Bahrami * entry:
50*e23c41c9SAli Bahrami * str - String to be trimmed
51*e23c41c9SAli Bahrami *
52*e23c41c9SAli Bahrami * exit:
53*e23c41c9SAli Bahrami * The pointer to the trimmed string is returned.
54*e23c41c9SAli Bahrami *
55*e23c41c9SAli Bahrami * note:
56*e23c41c9SAli Bahrami * Leading whitespace is trimmed by advancing the given str pointer,
57*e23c41c9SAli Bahrami * and not by making a copy or allocing more memory. Hence, the caller
58*e23c41c9SAli Bahrami * should retain a copy of the original str pointer if they need to
59*e23c41c9SAli Bahrami * free the original memory or otherwise access it.
60*e23c41c9SAli Bahrami *
61*e23c41c9SAli Bahrami * Trailing whitespace is trimmed by inserting a NULL termination
62*e23c41c9SAli Bahrami * in the position at which the first trailing whitespce character
63*e23c41c9SAli Bahrami * lies. This routine can therefore modify the memory used by
64*e23c41c9SAli Bahrami * the input string.
65*e23c41c9SAli Bahrami */
66*e23c41c9SAli Bahrami char *
conv_strproc_trim(char * str)67*e23c41c9SAli Bahrami conv_strproc_trim(char *str)
68*e23c41c9SAli Bahrami {
69*e23c41c9SAli Bahrami char *tail;
70*e23c41c9SAli Bahrami
71*e23c41c9SAli Bahrami /* Skip leading whitespace */
72*e23c41c9SAli Bahrami while (conv_strproc_isspace(*str))
73*e23c41c9SAli Bahrami str++;
74*e23c41c9SAli Bahrami
75*e23c41c9SAli Bahrami /* Back up over trailing whitespace */
76*e23c41c9SAli Bahrami tail = str + strlen(str);
77*e23c41c9SAli Bahrami while ((tail > str) && conv_strproc_isspace(*(tail - 1)))
78*e23c41c9SAli Bahrami tail--;
79*e23c41c9SAli Bahrami *tail = '\0';
80*e23c41c9SAli Bahrami
81*e23c41c9SAli Bahrami return (str);
82*e23c41c9SAli Bahrami }
83*e23c41c9SAli Bahrami
84*e23c41c9SAli Bahrami /*
85*e23c41c9SAli Bahrami * Given a debug token of the form:
86*e23c41c9SAli Bahrami *
87*e23c41c9SAli Bahrami * token=value
88*e23c41c9SAli Bahrami *
89*e23c41c9SAli Bahrami * extract and return a pointer to the value.
90*e23c41c9SAli Bahrami *
91*e23c41c9SAli Bahrami * entry:
92*e23c41c9SAli Bahrami * str - String to process
93*e23c41c9SAli Bahrami * token_len = Length of the token, not counting the '=' character,
94*e23c41c9SAli Bahrami * or any whitespace between the token and the '='.
95*e23c41c9SAli Bahrami * to_upper - True to convert the returned value to upper case.
96*e23c41c9SAli Bahrami * value - Address of pointer to receive the value string.
97*e23c41c9SAli Bahrami *
98*e23c41c9SAli Bahrami * exit:
99*e23c41c9SAli Bahrami * On success, *value is updated to point at the value string,
100*e23c41c9SAli Bahrami * and True (1) is returned. On failure, False (0) is returned.
101*e23c41c9SAli Bahrami *
102*e23c41c9SAli Bahrami * note:
103*e23c41c9SAli Bahrami * If CONV_SPEXV_F_UCASE is specified, this routine modifies
104*e23c41c9SAli Bahrami * the memory pointed at by str.
105*e23c41c9SAli Bahrami */
106*e23c41c9SAli Bahrami Boolean
conv_strproc_extract_value(char * str,size_t token_len,int flags,const char ** value)107*e23c41c9SAli Bahrami conv_strproc_extract_value(char *str, size_t token_len, int flags,
108*e23c41c9SAli Bahrami const char **value)
109*e23c41c9SAli Bahrami {
110*e23c41c9SAli Bahrami int trim = (flags & CONV_SPEXV_F_NOTRIM) == 0;
111*e23c41c9SAli Bahrami
112*e23c41c9SAli Bahrami /* Skip the token */
113*e23c41c9SAli Bahrami str += token_len;
114*e23c41c9SAli Bahrami
115*e23c41c9SAli Bahrami /*
116*e23c41c9SAli Bahrami * If TRIM, skip whitespace between token and '='
117*e23c41c9SAli Bahrami */
118*e23c41c9SAli Bahrami if (trim)
119*e23c41c9SAli Bahrami while (conv_strproc_isspace(*str))
120*e23c41c9SAli Bahrami str++;
121*e23c41c9SAli Bahrami
122*e23c41c9SAli Bahrami /* If there's not a '=' here, this isn't the token we thought it was */
123*e23c41c9SAli Bahrami if (*str != '=')
124*e23c41c9SAli Bahrami return (FALSE);
125*e23c41c9SAli Bahrami
126*e23c41c9SAli Bahrami str++; /* skip the '=' */
127*e23c41c9SAli Bahrami
128*e23c41c9SAli Bahrami /* if TRIM, skip whitespace following the '=' */
129*e23c41c9SAli Bahrami if (trim)
130*e23c41c9SAli Bahrami while (conv_strproc_isspace(*str))
131*e23c41c9SAli Bahrami str++;
132*e23c41c9SAli Bahrami
133*e23c41c9SAli Bahrami /* Null value and it's not OK? Make it an error. */
134*e23c41c9SAli Bahrami if (((flags & CONV_SPEXV_F_NULLOK) == 0) && (*str == '\0'))
135*e23c41c9SAli Bahrami return (FALSE);
136*e23c41c9SAli Bahrami
137*e23c41c9SAli Bahrami *value = str;
138*e23c41c9SAli Bahrami
139*e23c41c9SAli Bahrami /* Convert to uppercase on request */
140*e23c41c9SAli Bahrami if (flags & CONV_SPEXV_F_UCASE)
141*e23c41c9SAli Bahrami for (; *str; str++)
142*e23c41c9SAli Bahrami if ((*str >= 'a') && (*str <= 'z'))
143*e23c41c9SAli Bahrami *str = *str - ('a' - 'A');
144*e23c41c9SAli Bahrami
145*e23c41c9SAli Bahrami return (TRUE);
146*e23c41c9SAli Bahrami }
147