Print this page
OS-2444 richmond hardware maps need to support ivy bridge
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/fm/topo/libtopo/common/topo_string.c
+++ new/usr/src/lib/fm/topo/libtopo/common/topo_string.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 -
27 -#pragma ident "%Z%%M% %I% %E% SMI"
26 +/*
27 + * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 + */
28 29
29 30 #include <strings.h>
30 31 #include <ctype.h>
31 32 #include <fm/libtopo.h>
32 33 #include <fm/topo_mod.h>
33 34 #include <topo_alloc.h>
34 35
35 36 char *
36 37 topo_hdl_strdup(topo_hdl_t *thp, const char *s)
37 38 {
38 39 char *p;
39 40
40 41 if (s != NULL)
41 42 p = topo_hdl_alloc(thp, strlen(s) + 1);
42 43 else
43 44 p = NULL;
44 45
45 46 if (p != NULL)
46 47 (void) strcpy(p, s);
47 48
48 49 return (p);
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
49 50 }
50 51
51 52 void
52 53 topo_hdl_strfree(topo_hdl_t *thp, char *s)
53 54 {
54 55 if (s != NULL)
55 56 topo_hdl_free(thp, s, strlen(s) + 1);
56 57 }
57 58
58 59 char *
60 +topo_hdl_strsplit(topo_hdl_t *hdl, const char *input, const char *sep,
61 + char **lastp)
62 +{
63 + size_t seplen = strlen(sep);
64 + const char *scanstart;
65 + char *token;
66 + char *ret;
67 +
68 + if (input != NULL) {
69 + /*
70 + * Start scanning at beginning of input:
71 + */
72 + scanstart = input;
73 + } else {
74 + /*
75 + * If we have already finished scanning, return NULL.
76 + */
77 + if (*lastp == NULL)
78 + return (NULL);
79 +
80 + /*
81 + * Otherwise, start scanning where we left off:
82 + */
83 + scanstart = *lastp;
84 + }
85 +
86 + token = strstr(scanstart, sep);
87 + if (token != NULL) {
88 + /*
89 + * We still have a separator, so advance the next-start
90 + * pointer past it:
91 + */
92 + *lastp = token + seplen;
93 + /*
94 + * Copy out this element:
95 + */
96 + ret = topo_hdl_alloc(hdl, token - scanstart + 1);
97 + (void) strncpy(ret, scanstart, token - scanstart);
98 + ret[token - scanstart] = '\0';
99 + } else {
100 + /*
101 + * We have no separator, so this is the last element:
102 + */
103 + *lastp = NULL;
104 + ret = topo_hdl_strdup(hdl, scanstart);
105 + }
106 +
107 + return (ret);
108 +}
109 +
110 +char *
59 111 topo_mod_strdup(topo_mod_t *mod, const char *s)
60 112 {
61 113 return (topo_hdl_strdup(mod->tm_hdl, s));
62 114 }
63 115
64 116 void
65 117 topo_mod_strfree(topo_mod_t *mod, char *s)
66 118 {
67 119 topo_hdl_strfree(mod->tm_hdl, s);
68 120 }
69 121
122 +char *
123 +topo_mod_strsplit(topo_mod_t *mod, const char *input, const char *sep,
124 + char **lastp)
125 +{
126 + return (topo_hdl_strsplit(mod->tm_hdl, input, sep, lastp));
127 +}
128 +
70 129 const char *
71 130 topo_strbasename(const char *s)
72 131 {
73 132 const char *p = strrchr(s, '/');
74 133
75 134 if (p == NULL)
76 135 return (s);
77 136
78 137 return (++p);
79 138 }
80 139
81 140 char *
82 141 topo_strdirname(char *s)
83 142 {
84 143 static char slash[] = "/";
85 144 static char dot[] = ".";
86 145 char *p;
87 146
88 147 if (s == NULL || *s == '\0')
89 148 return (dot);
90 149
91 150 for (p = s + strlen(s); p != s && *--p == '/'; )
92 151 continue;
93 152
94 153 if (p == s && *p == '/')
95 154 return (slash);
96 155
97 156 while (p != s) {
98 157 if (*--p == '/') {
99 158 while (*p == '/' && p != s)
100 159 p--;
101 160 *++p = '\0';
102 161 return (s);
103 162 }
104 163 }
105 164
106 165 return (dot);
107 166 }
108 167
109 168 ulong_t
110 169 topo_strhash(const char *key)
111 170 {
112 171 ulong_t g, h = 0;
113 172 const char *p;
114 173
115 174 for (p = key; *p != '\0'; p++) {
116 175 h = (h << 4) + *p;
117 176
118 177 if ((g = (h & 0xf0000000)) != 0) {
119 178 h ^= (g >> 24);
120 179 h ^= g;
121 180 }
122 181 }
123 182
124 183 return (h);
125 184 }
126 185
127 186 /*
128 187 * Transform string s inline, converting each embedded C escape sequence string
129 188 * to the corresponding character. For example, the substring "\n" is replaced
130 189 * by an inline '\n' character. The length of the resulting string is returned.
131 190 */
132 191 size_t
133 192 topo_stresc2chr(char *s)
134 193 {
135 194 char *p, *q, c;
136 195 int esc = 0;
137 196 int x;
138 197
139 198 for (p = q = s; (c = *p) != '\0'; p++) {
140 199 if (esc) {
141 200 switch (c) {
142 201 case '0':
143 202 case '1':
144 203 case '2':
145 204 case '3':
146 205 case '4':
147 206 case '5':
148 207 case '6':
149 208 case '7':
150 209 c -= '0';
151 210 p++;
152 211
153 212 if (*p >= '0' && *p <= '7') {
154 213 c = c * 8 + *p++ - '0';
155 214
156 215 if (*p >= '0' && *p <= '7')
157 216 c = c * 8 + *p - '0';
158 217 else
159 218 p--;
160 219 } else
161 220 p--;
162 221
163 222 *q++ = c;
164 223 break;
165 224
166 225 case 'a':
167 226 *q++ = '\a';
168 227 break;
169 228 case 'b':
170 229 *q++ = '\b';
171 230 break;
172 231 case 'f':
173 232 *q++ = '\f';
174 233 break;
175 234 case 'n':
176 235 *q++ = '\n';
177 236 break;
178 237 case 'r':
179 238 *q++ = '\r';
180 239 break;
181 240 case 't':
182 241 *q++ = '\t';
183 242 break;
184 243 case 'v':
185 244 *q++ = '\v';
186 245 break;
187 246
188 247 case 'x':
189 248 for (x = 0; (c = *++p) != '\0'; ) {
190 249 if (c >= '0' && c <= '9')
191 250 x = x * 16 + c - '0';
192 251 else if (c >= 'a' && c <= 'f')
193 252 x = x * 16 + c - 'a' + 10;
194 253 else if (c >= 'A' && c <= 'F')
195 254 x = x * 16 + c - 'A' + 10;
196 255 else
197 256 break;
198 257 }
199 258 *q++ = (char)x;
200 259 p--;
201 260 break;
202 261
203 262 case '"':
204 263 case '\\':
205 264 *q++ = c;
206 265 break;
207 266 default:
208 267 *q++ = '\\';
209 268 *q++ = c;
210 269 }
211 270
212 271 esc = 0;
213 272
214 273 } else {
215 274 if ((esc = c == '\\') == 0)
216 275 *q++ = c;
217 276 }
218 277 }
219 278
220 279 *q = '\0';
221 280 return ((size_t)(q - s));
222 281 }
223 282
224 283 int
225 284 topo_strmatch(const char *s, const char *p)
226 285 {
227 286 char c;
228 287
229 288 if (p == NULL)
230 289 return (0);
231 290
232 291 if (s == NULL)
233 292 s = ""; /* treat NULL string as the empty string */
234 293
235 294 do {
236 295 if ((c = *p++) == '\0')
237 296 return (*s == '\0');
238 297
239 298 if (c == '*') {
240 299 while (*p == '*')
241 300 p++; /* consecutive *'s can be collapsed */
242 301
243 302 if (*p == '\0')
244 303 return (1);
245 304
246 305 while (*s != '\0') {
247 306 if (topo_strmatch(s++, p) != 0)
248 307 return (1);
249 308 }
250 309
251 310 return (0);
252 311 }
253 312 } while (c == *s++);
254 313
255 314 return (0);
256 315 }
↓ open down ↓ |
177 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX