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