]>
Commit | Line | Data |
---|---|---|
59241895 MJ |
1 | /* |
2 | * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application | |
3 | * Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com> | |
4 | * | |
5 | * Version: MPL 1.1 | |
6 | * | |
7 | * The contents of this file are subject to the Mozilla Public License Version | |
8 | * 1.1 (the "License"); you may not use this file except in compliance with | |
9 | * the License. You may obtain a copy of the License at | |
10 | * http://www.mozilla.org/MPL/ | |
11 | * | |
12 | * Software distributed under the License is distributed on an "AS IS" basis, | |
13 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
14 | * for the specific language governing rights and limitations under the | |
15 | * License. | |
16 | * | |
17 | * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application | |
18 | * | |
19 | * The Initial Developer of the Original Code is | |
20 | * Anthony Minessale II <anthmct@yahoo.com> | |
21 | * Portions created by the Initial Developer are Copyright (C) | |
22 | * the Initial Developer. All Rights Reserved. | |
23 | * | |
24 | * Contributor(s): | |
25 | * | |
26 | * Anthony Minessale II <anthmct@yahoo.com> | |
27 | * Simon Capper <skyjunky@sbcglobal.net> | |
28 | * | |
29 | * | |
30 | * switch_xml.c -- XML PARSER | |
31 | * | |
32 | * Derived from ezxml http://ezxml.sourceforge.net | |
33 | * Original Copyright | |
34 | * | |
35 | * Copyright 2004, 2006 Aaron Voisine <aaron@voisine.org> | |
36 | * | |
37 | * Permission is hereby granted, free of charge, to any person obtaining | |
38 | * a copy of this software and associated documentation files (the | |
39 | * "Software"), to deal in the Software without restriction, including | |
40 | * without limitation the rights to use, copy, modify, merge, publish, | |
41 | * distribute, sublicense, and/or sell copies of the Software, and to | |
42 | * permit persons to whom the Software is furnished to do so, subject to | |
43 | * the following conditions: | |
44 | * | |
45 | * The above copyright notice and this permission notice shall be included | |
46 | * in all copies or substantial portions of the Software. | |
47 | * | |
48 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
49 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
50 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
51 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
52 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
53 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
54 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
55 | */ | |
56 | ||
57 | #include <switch.h> | |
58 | #ifndef WIN32 | |
59 | #include <switch_private.h> | |
60 | #include <glob.h> | |
61 | #else /* we're on windoze :( */ | |
62 | /* glob functions at end of this file */ | |
63 | #include <apr_file_io.h> | |
64 | ||
65 | typedef struct { | |
66 | size_t gl_pathc; /* Count of total paths so far. */ | |
67 | size_t gl_matchc; /* Count of paths matching pattern. */ | |
68 | size_t gl_offs; /* Reserved at beginning of gl_pathv. */ | |
69 | int gl_flags; /* Copy of flags parameter to glob. */ | |
70 | char **gl_pathv; /* List of paths matching pattern. */ | |
71 | /* Copy of errfunc parameter to glob. */ | |
72 | int (*gl_errfunc)(const char *, int); | |
73 | } glob_t; | |
74 | ||
75 | /* Believed to have been introduced in 1003.2-1992 */ | |
76 | #define GLOB_APPEND 0x0001 /* Append to output from previous call. */ | |
77 | #define GLOB_DOOFFS 0x0002 /* Use gl_offs. */ | |
78 | #define GLOB_ERR 0x0004 /* Return on error. */ | |
79 | #define GLOB_MARK 0x0008 /* Append / to matching directories. */ | |
80 | #define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */ | |
81 | #define GLOB_NOSORT 0x0020 /* Don't sort. */ | |
82 | ||
83 | /* Error values returned by glob(3) */ | |
84 | #define GLOB_NOSPACE (-1) /* Malloc call failed. */ | |
85 | #define GLOB_ABORTED (-2) /* Unignored error. */ | |
86 | #define GLOB_NOMATCH (-3) /* No match and GLOB_NOCHECK was not set. */ | |
87 | #define GLOB_NOSYS (-4) /* Obsolete: source comptability only. */ | |
88 | ||
89 | #define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */ | |
90 | #define GLOB_MAGCHAR 0x0100 /* Pattern had globbing characters. */ | |
91 | #define GLOB_NOMAGIC 0x0200 /* GLOB_NOCHECK without magic chars (csh). */ | |
92 | #define GLOB_QUOTE 0x0400 /* Quote special chars with \. */ | |
93 | #define GLOB_LIMIT 0x1000 /* limit number of returned paths */ | |
94 | ||
95 | int glob(const char *, int, int (*)(const char *, int), glob_t *); | |
96 | void globfree(glob_t *); | |
97 | ||
98 | #endif | |
99 | #undef HAVE_MMAP | |
100 | #ifdef HAVE_MMAP | |
101 | #include <sys/mman.h> | |
102 | #ifdef __sun | |
103 | extern int madvise(caddr_t, size_t, int); | |
104 | #endif | |
105 | #endif | |
106 | ||
107 | #define SWITCH_XML_WS "\t\r\n " // whitespace | |
108 | #define SWITCH_XML_ERRL 128 // maximum error string length | |
109 | ||
110 | static int preprocess(const char *cwd, const char *file, int write_fd, int rlevel); | |
111 | ||
112 | typedef struct switch_xml_root *switch_xml_root_t; | |
113 | struct switch_xml_root { // additional data for the root tag | |
114 | struct switch_xml xml; // is a super-struct built on top of switch_xml struct | |
115 | switch_xml_t cur; // current xml tree insertion point | |
116 | char *m; // original xml string | |
117 | switch_size_t len; // length of allocated memory for mmap | |
118 | uint8_t dynamic; | |
119 | char *u; // UTF-8 conversion of string if original was UTF-16 | |
120 | char *s; // start of work area | |
121 | char *e; // end of work area | |
122 | char **ent; // general entities (ampersand sequences) | |
123 | char ***attr; // default attributes | |
124 | char ***pi; // processing instructions | |
125 | short standalone; // non-zero if <?xml standalone="yes"?> | |
126 | char err[SWITCH_XML_ERRL]; // error string | |
127 | }; | |
128 | ||
129 | char *SWITCH_XML_NIL[] = { NULL }; // empty, null terminated array of strings | |
130 | ||
131 | struct switch_xml_binding { | |
132 | switch_xml_search_function_t function; | |
133 | switch_xml_section_t sections; | |
134 | void *user_data; | |
135 | struct switch_xml_binding *next; | |
136 | }; | |
137 | ||
138 | ||
139 | static switch_xml_binding_t *BINDINGS = NULL; | |
140 | static switch_xml_t MAIN_XML_ROOT = NULL; | |
c5831538 AM |
141 | static switch_memory_pool_t *XML_MEMORY_POOL = NULL; |
142 | static switch_thread_rwlock_t *RWLOCK = NULL; | |
143 | static switch_thread_rwlock_t *B_RWLOCK = NULL; | |
144 | static switch_mutex_t *XML_LOCK = NULL; | |
f6f7f31c | 145 | |
59241895 MJ |
146 | |
147 | struct xml_section_t { | |
148 | const char *name; | |
149 | //switch_xml_section_t section; | |
150 | uint32_t section; | |
151 | }; | |
152 | ||
153 | static struct xml_section_t SECTIONS[] = { | |
154 | {"result", SWITCH_XML_SECTION_RESULT}, | |
155 | {"config", SWITCH_XML_SECTION_CONFIG}, | |
156 | {"directory", SWITCH_XML_SECTION_DIRECTORY}, | |
157 | {"dialplan", SWITCH_XML_SECTION_DIALPLAN}, | |
158 | {"phrases", SWITCH_XML_SECTION_PHRASES}, | |
159 | {NULL, 0} | |
160 | }; | |
161 | ||
162 | SWITCH_DECLARE(switch_xml_section_t) switch_xml_parse_section_string(const char *str) | |
163 | { | |
164 | size_t x; | |
165 | char buf[1024] = ""; | |
166 | //switch_xml_section_t sections = SWITCH_XML_SECTION_RESULT; | |
167 | uint32_t sections = SWITCH_XML_SECTION_RESULT; | |
168 | ||
169 | if (str) { | |
170 | for (x = 0; x < strlen(str); x++) { | |
171 | buf[x] = (char) tolower((int) str[x]); | |
172 | } | |
173 | for (x = 0;; x++) { | |
174 | if (!SECTIONS[x].name) { | |
175 | break; | |
176 | } | |
177 | if (strstr(buf, SECTIONS[x].name)) { | |
178 | sections |= SECTIONS[x].section; | |
179 | } | |
180 | } | |
181 | } | |
182 | return (switch_xml_section_t) sections; | |
183 | } | |
184 | ||
185 | SWITCH_DECLARE(switch_status_t) switch_xml_unbind_search_function(switch_xml_binding_t **binding) | |
186 | { | |
187 | switch_xml_binding_t *ptr, *last = NULL; | |
188 | switch_status_t status = SWITCH_STATUS_FALSE; | |
189 | ||
8ec9b8f7 AM |
190 | |
191 | switch_thread_rwlock_wrlock(B_RWLOCK); | |
59241895 MJ |
192 | for (ptr = BINDINGS; ptr; ptr = ptr->next) { |
193 | if (ptr == *binding) { | |
194 | if (last) { | |
195 | last->next = (*binding)->next; | |
196 | } else { | |
197 | BINDINGS = (*binding)->next; | |
198 | } | |
199 | status = SWITCH_STATUS_SUCCESS; | |
200 | break; | |
201 | } | |
202 | last = ptr; | |
203 | } | |
8ec9b8f7 | 204 | switch_thread_rwlock_unlock(B_RWLOCK); |
59241895 MJ |
205 | |
206 | return status; | |
207 | } | |
208 | ||
209 | SWITCH_DECLARE(switch_status_t) switch_xml_unbind_search_function_ptr(switch_xml_search_function_t function) | |
210 | { | |
211 | switch_xml_binding_t *ptr, *last = NULL; | |
212 | switch_status_t status = SWITCH_STATUS_FALSE; | |
213 | ||
8ec9b8f7 | 214 | switch_thread_rwlock_wrlock(B_RWLOCK); |
59241895 MJ |
215 | for (ptr = BINDINGS; ptr; ptr = ptr->next) { |
216 | if (ptr->function == function) { | |
217 | if (last) { | |
218 | last->next = ptr->next; | |
219 | } else { | |
220 | BINDINGS = ptr->next; | |
221 | } | |
222 | status = SWITCH_STATUS_SUCCESS; | |
223 | } | |
224 | last = ptr; | |
225 | } | |
8ec9b8f7 | 226 | switch_thread_rwlock_unlock(B_RWLOCK); |
59241895 MJ |
227 | |
228 | return status; | |
229 | } | |
230 | ||
fc6a7ced AM |
231 | SWITCH_DECLARE(void) switch_xml_set_binding_sections(switch_xml_binding_t *binding, switch_xml_section_t sections) |
232 | { | |
233 | switch_assert(binding); | |
234 | binding->sections = sections; | |
235 | } | |
236 | ||
237 | SWITCH_DECLARE(void) switch_xml_set_binding_user_data(switch_xml_binding_t *binding, void *user_data) | |
238 | { | |
239 | switch_assert(binding); | |
240 | binding->user_data = user_data; | |
241 | } | |
242 | ||
446d7285 | 243 | SWITCH_DECLARE(switch_status_t) switch_xml_bind_search_function_ret(switch_xml_search_function_t function, switch_xml_section_t sections, void *user_data, switch_xml_binding_t **ret) |
59241895 MJ |
244 | { |
245 | switch_xml_binding_t *binding = NULL, *ptr = NULL; | |
246 | assert(function != NULL); | |
247 | ||
248 | if (!(binding = (switch_xml_binding_t *) switch_core_alloc(XML_MEMORY_POOL, sizeof(*binding)))) { | |
249 | return SWITCH_STATUS_MEMERR; | |
250 | } | |
251 | ||
252 | binding->function = function; | |
253 | binding->sections = sections; | |
254 | binding->user_data = user_data; | |
255 | ||
8ec9b8f7 | 256 | switch_thread_rwlock_wrlock(B_RWLOCK); |
59241895 MJ |
257 | for (ptr = BINDINGS; ptr && ptr->next; ptr = ptr->next); |
258 | ||
259 | if (ptr) { | |
260 | ptr->next = binding; | |
261 | } else { | |
262 | BINDINGS = binding; | |
263 | } | |
446d7285 AM |
264 | |
265 | if (ret) { | |
266 | *ret = binding; | |
267 | } | |
268 | ||
8ec9b8f7 | 269 | switch_thread_rwlock_unlock(B_RWLOCK); |
59241895 MJ |
270 | |
271 | return SWITCH_STATUS_SUCCESS; | |
272 | } | |
273 | ||
274 | SWITCH_DECLARE(switch_xml_t) switch_xml_find_child(switch_xml_t node, const char *childname, const char *attrname, const char *value) | |
275 | { | |
276 | switch_xml_t p = NULL; | |
277 | ||
278 | if (!(childname && attrname && value)) { | |
279 | return node; | |
280 | } | |
281 | ||
282 | for (p = switch_xml_child(node, childname); p; p = p->next) { | |
283 | const char *aname = switch_xml_attr(p, attrname); | |
284 | if (aname && value && !strcasecmp(aname, value)) { | |
285 | break; | |
286 | } | |
287 | } | |
288 | ||
289 | return p; | |
290 | } | |
291 | ||
292 | // returns the first child tag with the given name or NULL if not found | |
293 | SWITCH_DECLARE(switch_xml_t) switch_xml_child(switch_xml_t xml, const char *name) | |
294 | { | |
295 | xml = (xml) ? xml->child : NULL; | |
296 | while (xml && strcmp(name, xml->name)) | |
297 | xml = xml->sibling; | |
298 | return xml; | |
299 | } | |
300 | ||
301 | // returns the Nth tag with the same name in the same subsection or NULL if not | |
302 | // found | |
303 | switch_xml_t switch_xml_idx(switch_xml_t xml, int idx) | |
304 | { | |
305 | for (; xml && idx; idx--) | |
306 | xml = xml->next; | |
307 | return xml; | |
308 | } | |
309 | ||
310 | // returns the value of the requested tag attribute or "" if not found | |
311 | SWITCH_DECLARE(const char *) switch_xml_attr_soft(switch_xml_t xml, const char *attr) | |
312 | { | |
313 | const char *ret = switch_xml_attr(xml, attr); | |
314 | ||
315 | return ret ? ret : ""; | |
316 | } | |
317 | ||
318 | // returns the value of the requested tag attribute or NULL if not found | |
319 | SWITCH_DECLARE(const char *) switch_xml_attr(switch_xml_t xml, const char *attr) | |
320 | { | |
321 | int i = 0, j = 1; | |
322 | switch_xml_root_t root = (switch_xml_root_t) xml; | |
323 | ||
324 | if (!xml || !xml->attr) | |
325 | return NULL; | |
326 | while (xml->attr[i] && strcmp(attr, xml->attr[i])) | |
327 | i += 2; | |
328 | if (xml->attr[i]) | |
329 | return xml->attr[i + 1]; // found attribute | |
330 | ||
331 | while (root->xml.parent) | |
332 | root = (switch_xml_root_t) root->xml.parent; // root tag | |
333 | for (i = 0; root->attr[i] && strcmp(xml->name, root->attr[i][0]); i++); | |
334 | if (!root->attr[i]) | |
335 | return NULL; // no matching default attributes | |
336 | while (root->attr[i][j] && strcmp(attr, root->attr[i][j])) | |
337 | j += 3; | |
338 | return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; // found default | |
339 | } | |
340 | ||
341 | // same as switch_xml_get but takes an already initialized va_list | |
342 | static switch_xml_t switch_xml_vget(switch_xml_t xml, va_list ap) | |
343 | { | |
344 | char *name = va_arg(ap, char *); | |
345 | int idx = -1; | |
346 | ||
347 | if (name && *name) { | |
348 | idx = va_arg(ap, int); | |
349 | xml = switch_xml_child(xml, name); | |
350 | } | |
351 | return (idx < 0) ? xml : switch_xml_vget(switch_xml_idx(xml, idx), ap); | |
352 | } | |
353 | ||
354 | // Traverses the xml tree to retrieve a specific subtag. Takes a variable | |
355 | // length list of tag names and indexes. The argument list must be terminated | |
356 | // by either an index of -1 or an empty string tag name. Example: | |
357 | // title = switch_xml_get(library, "shelf", 0, "book", 2, "title", -1); | |
358 | // This retrieves the title of the 3rd book on the 1st shelf of library. | |
359 | // Returns NULL if not found. | |
360 | SWITCH_DECLARE(switch_xml_t) switch_xml_get(switch_xml_t xml,...) | |
361 | { | |
362 | va_list ap; | |
363 | switch_xml_t r; | |
364 | ||
365 | va_start(ap, xml); | |
366 | r = switch_xml_vget(xml, ap); | |
367 | va_end(ap); | |
368 | return r; | |
369 | } | |
370 | ||
371 | // returns a null terminated array of processing instructions for the given | |
372 | // target | |
373 | SWITCH_DECLARE(const char **) switch_xml_pi(switch_xml_t xml, const char *target) | |
374 | { | |
375 | switch_xml_root_t root = (switch_xml_root_t) xml; | |
376 | int i = 0; | |
377 | ||
378 | if (!root) | |
379 | return (const char **) SWITCH_XML_NIL; | |
380 | while (root->xml.parent) | |
381 | root = (switch_xml_root_t) root->xml.parent; // root tag | |
382 | while (root->pi[i] && strcmp(target, root->pi[i][0])) | |
383 | i++; // find target | |
384 | return (const char **) ((root->pi[i]) ? root->pi[i] + 1 : SWITCH_XML_NIL); | |
385 | } | |
386 | ||
387 | // set an error string and return root | |
388 | static switch_xml_t switch_xml_err(switch_xml_root_t root, char *s, const char *err, ...) | |
389 | { | |
390 | va_list ap; | |
391 | int line = 1; | |
392 | char *t, fmt[SWITCH_XML_ERRL]; | |
393 | ||
394 | for (t = root->s; t && t < s; t++) | |
395 | if (*t == '\n') | |
396 | line++; | |
397 | switch_snprintf(fmt, SWITCH_XML_ERRL, "[error near line %d]: %s", line, err); | |
398 | ||
399 | va_start(ap, err); | |
400 | vsnprintf(root->err, SWITCH_XML_ERRL, fmt, ap); | |
401 | va_end(ap); | |
402 | ||
403 | return &root->xml; | |
404 | } | |
405 | ||
406 | // Recursively decodes entity and character references and normalizes new lines | |
407 | // ent is a null terminated array of alternating entity names and values. set t | |
408 | // to '&' for general entity decoding, '%' for parameter entity decoding, 'c' | |
409 | // for cdata sections, ' ' for attribute normalization, or '*' for non-cdata | |
410 | // attribute normalization. Returns s, or if the decoded string is longer than | |
411 | // s, returns a malloced string that must be freed. | |
412 | static char *switch_xml_decode(char *s, char **ent, char t) | |
413 | { | |
414 | char *e, *r = s, *m = s; | |
415 | long b, c, d, l; | |
416 | ||
417 | for (; *s; s++) { // normalize line endings | |
418 | while (*s == '\r') { | |
419 | *(s++) = '\n'; | |
420 | if (*s == '\n') | |
421 | memmove(s, (s + 1), strlen(s)); | |
422 | } | |
423 | } | |
424 | ||
425 | for (s = r;;) { | |
426 | while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace((int) (*s))) | |
427 | s++; | |
428 | ||
429 | if (!*s) | |
430 | break; | |
431 | else if (t != 'c' && !strncmp(s, "&#", 2)) { // character reference | |
432 | if (s[2] == 'x') | |
433 | c = strtol(s + 3, &e, 16); // base 16 | |
434 | else | |
435 | c = strtol(s + 2, &e, 10); // base 10 | |
436 | if (!c || *e != ';') { | |
437 | s++; | |
438 | continue; | |
439 | } // not a character ref | |
440 | ||
441 | if (c < 0x80) | |
442 | *(s++) = (char) c; // US-ASCII subset | |
443 | else { // multi-byte UTF-8 sequence | |
444 | for (b = 0, d = c; d; d /= 2) | |
445 | b++; // number of bits in c | |
446 | b = (b - 2) / 5; // number of bytes in payload | |
447 | *(s++) = (char) ((0xFF << (7 - b)) | (c >> (6 * b))); // head | |
448 | while (b) | |
449 | *(s++) = (char) (0x80 | ((c >> (6 * --b)) & 0x3F)); // payload | |
450 | } | |
451 | ||
452 | memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';'))); | |
453 | } else if ((*s == '&' && (t == '&' || t == ' ' || t == '*')) || (*s == '%' && t == '%')) { // entity reference | |
454 | for (b = 0; ent[b] && strncmp(s + 1, ent[b], strlen(ent[b])); b += 2); // find entity in entity list | |
455 | ||
456 | if (ent[b++]) { // found a match | |
457 | if ((c = (long) strlen(ent[b])) - 1 > (e = strchr(s, ';')) - s) { | |
458 | l = (d = (long) (s - r)) + c + (long) strlen(e); // new length | |
459 | if (l) { | |
460 | if (r == m) { | |
461 | char *tmp = (char *)malloc(l); | |
462 | if (tmp) { | |
463 | r = strcpy(tmp, r); | |
464 | } else { | |
465 | if (r) free(r); | |
466 | return NULL; | |
467 | } | |
468 | } else { | |
469 | char *tmp = (char *)realloc(r, l); | |
470 | if (tmp) { | |
471 | r = tmp; | |
472 | } else { | |
473 | if (r) free(r); | |
474 | return NULL; | |
475 | } | |
476 | } | |
477 | } | |
478 | e = strchr((s = r + d), ';'); // fix up pointers | |
479 | } | |
480 | ||
481 | memmove(s + c, e + 1, strlen(e)); // shift rest of string | |
482 | strncpy(s, ent[b], c); // copy in replacement text | |
483 | } else | |
484 | s++; // not a known entity | |
485 | } else if ((t == ' ' || t == '*') && isspace((int) (*s))) | |
486 | *(s++) = ' '; | |
487 | else | |
488 | s++; // no decoding needed | |
489 | } | |
490 | ||
491 | if (t == '*') { // normalize spaces for non-cdata attributes | |
492 | for (s = r; *s; s++) { | |
493 | if ((l = (long) strspn(s, " "))) | |
494 | memmove(s, s + l, strlen(s + l) + 1); | |
495 | while (*s && *s != ' ') | |
496 | s++; | |
497 | } | |
498 | if (--s >= r && *s == ' ') | |
499 | *s = '\0'; // trim any trailing space | |
500 | } | |
501 | return r; | |
502 | } | |
503 | ||
504 | // called when parser finds start of new tag | |
505 | static void switch_xml_open_tag(switch_xml_root_t root, char *name, char **attr) | |
506 | { | |
507 | switch_xml_t xml = root->cur; | |
508 | ||
509 | if (xml->name) | |
510 | xml = switch_xml_add_child(xml, name, strlen(xml->txt)); | |
511 | else | |
512 | xml->name = name; // first open tag | |
513 | ||
514 | xml->attr = attr; | |
515 | root->cur = xml; // update tag insertion point | |
516 | } | |
517 | ||
518 | // called when parser finds character content between open and closing tag | |
519 | static void switch_xml_char_content(switch_xml_root_t root, char *s, switch_size_t len, char t) | |
520 | { | |
521 | switch_xml_t xml = root->cur; | |
522 | char *m = s; | |
523 | switch_size_t l; | |
524 | ||
525 | if (!xml || !xml->name || !len) | |
526 | return; // sanity check | |
527 | ||
528 | s[len] = '\0'; // null terminate text (calling functions anticipate this) | |
529 | len = strlen(s = switch_xml_decode(s, root->ent, t)) + 1; | |
530 | ||
531 | if (!*(xml->txt)) | |
532 | xml->txt = s; // initial character content | |
533 | else { // allocate our own memory and make a copy | |
534 | if ((xml->flags & SWITCH_XML_TXTM)) { // allocate some space | |
535 | char *tmp = (char *)realloc(xml->txt, (l = strlen(xml->txt)) + len); | |
536 | if (tmp) { | |
537 | xml->txt = tmp; | |
538 | } else { | |
539 | return; | |
540 | } | |
541 | } else { | |
542 | char *tmp = (char *)malloc((l = strlen(xml->txt)) + len); | |
543 | if (tmp) { | |
544 | xml->txt = strcpy(tmp, xml->txt); | |
545 | } else { | |
546 | return; | |
547 | } | |
548 | } | |
549 | strcpy(xml->txt + l, s); // add new char content | |
550 | if (s != m) | |
551 | free(s); // free s if it was malloced by switch_xml_decode() | |
552 | } | |
553 | ||
554 | if (xml->txt != m) | |
555 | switch_xml_set_flag(xml, SWITCH_XML_TXTM); | |
556 | } | |
557 | ||
558 | // called when parser finds closing tag | |
559 | static switch_xml_t switch_xml_close_tag(switch_xml_root_t root, char *name, char *s) | |
560 | { | |
561 | if (!root->cur || !root->cur->name || strcmp(name, root->cur->name)) | |
562 | return switch_xml_err(root, s, "unexpected closing tag </%s>", name); | |
563 | ||
564 | root->cur = root->cur->parent; | |
565 | return NULL; | |
566 | } | |
567 | ||
568 | // checks for circular entity references, returns non-zero if no circular | |
569 | // references are found, zero otherwise | |
570 | static int switch_xml_ent_ok(char *name, char *s, char **ent) | |
571 | { | |
572 | int i; | |
573 | ||
574 | for (;; s++) { | |
575 | while (*s && *s != '&') | |
576 | s++; // find next entity reference | |
577 | if (!*s) | |
578 | return 1; | |
579 | if (!strncmp(s + 1, name, strlen(name))) | |
580 | return 0; // circular ref. | |
581 | for (i = 0; ent[i] && strncmp(ent[i], s + 1, strlen(ent[i])); i += 2); | |
582 | if (ent[i] && !switch_xml_ent_ok(name, ent[i + 1], ent)) | |
583 | return 0; | |
584 | } | |
585 | } | |
586 | ||
587 | // called when the parser finds a processing instruction | |
588 | static void switch_xml_proc_inst(switch_xml_root_t root, char *s, switch_size_t len) | |
589 | { | |
590 | int i = 0, j = 1; | |
591 | char *target = s; | |
592 | char **sstmp; | |
593 | char *stmp; | |
594 | ||
595 | s[len] = '\0'; // null terminate instruction | |
596 | if (*(s += strcspn(s, SWITCH_XML_WS))) { | |
597 | *s = '\0'; // null terminate target | |
598 | s += strspn(s + 1, SWITCH_XML_WS) + 1; // skip whitespace after target | |
599 | } | |
600 | ||
601 | if (!root) return; | |
602 | ||
603 | if (!strcmp(target, "xml")) { // <?xml ... ?> | |
604 | if ((s = strstr(s, "standalone")) && !strncmp(s + strspn(s + 10, SWITCH_XML_WS "='\"") + 10, "yes", 3)) | |
605 | root->standalone = 1; | |
606 | return; | |
607 | } | |
608 | ||
609 | if (!root->pi[0]) { | |
610 | root->pi = (char ***)malloc(sizeof(char **)); | |
611 | if (!root->pi) return; | |
612 | *(root->pi) = NULL; //first pi | |
613 | } | |
614 | ||
615 | while (root->pi[i] && strcmp(target, root->pi[i][0])) | |
616 | i++; // find target | |
617 | if (!root->pi[i]) { // new target | |
618 | char ***ssstmp = (char ***)realloc(root->pi, sizeof(char **) * (i + 2)); | |
619 | if (!ssstmp) return; | |
620 | root->pi = ssstmp; | |
621 | if (!root->pi) return; | |
622 | root->pi[i] = (char **)malloc(sizeof(char *) * 3); | |
623 | if (!root->pi[i]) return; | |
624 | root->pi[i][0] = target; | |
625 | root->pi[i][1] = (char *) (root->pi[i + 1] = NULL); // terminate pi list | |
626 | root->pi[i][2] = strdup(""); // empty document position list | |
627 | } | |
628 | ||
629 | while (root->pi[i][j]) | |
630 | j++; // find end of instruction list for this target | |
631 | sstmp = (char **)realloc(root->pi[i], sizeof(char *) * (j + 3)); | |
632 | if (!sstmp) return; | |
633 | root->pi[i] = sstmp; | |
634 | stmp = (char *)realloc(root->pi[i][j + 1], j + 1); | |
635 | if (!stmp) return; | |
636 | root->pi[i][j + 2] = stmp; | |
637 | strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<"); | |
638 | root->pi[i][j + 1] = NULL; // null terminate pi list for this target | |
639 | root->pi[i][j] = s; // set instruction | |
640 | } | |
641 | ||
642 | // called when the parser finds an internal doctype subset | |
643 | static short switch_xml_internal_dtd(switch_xml_root_t root, char *s, switch_size_t len) | |
644 | { | |
645 | char q, *c, *t, *n = NULL, *v, **ent, **pe; | |
646 | int i, j; | |
647 | char **sstmp; | |
648 | ||
649 | pe = (char **)memcpy(malloc(sizeof(SWITCH_XML_NIL)), SWITCH_XML_NIL, sizeof(SWITCH_XML_NIL)); | |
650 | ||
651 | for (s[len] = '\0'; s;) { | |
652 | while (*s && *s != '<' && *s != '%') | |
653 | s++; // find next declaration | |
654 | ||
655 | if (!*s) | |
656 | break; | |
657 | else if (!strncmp(s, "<!ENTITY", 8)) { // parse entity definitions | |
658 | c = s += strspn(s + 8, SWITCH_XML_WS) + 8; // skip white space separator | |
659 | n = s + strspn(s, SWITCH_XML_WS "%"); // find name | |
660 | *(s = n + strcspn(n, SWITCH_XML_WS)) = ';'; // append ; to name | |
661 | ||
662 | v = s + strspn(s + 1, SWITCH_XML_WS) + 1; // find value | |
663 | if ((q = *(v++)) != '"' && q != '\'') { // skip externals | |
664 | s = strchr(s, '>'); | |
665 | continue; | |
666 | } | |
667 | ||
668 | for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++); | |
669 | sstmp = (char **)realloc(ent, (i + 3) * sizeof(char *)); // space for next ent | |
670 | if (!sstmp) { | |
671 | switch_xml_err(root, v, "Allocation Error!"); | |
672 | break; | |
673 | } | |
674 | ent = sstmp; | |
675 | if (*c == '%') | |
676 | pe = ent; | |
677 | else | |
678 | root->ent = ent; | |
679 | ||
680 | *(++s) = '\0'; // null terminate name | |
681 | if ((s = strchr(v, q))) | |
682 | *(s++) = '\0'; // null terminate value | |
683 | ent[i + 1] = switch_xml_decode(v, pe, '%'); // set value | |
684 | ent[i + 2] = NULL; // null terminate entity list | |
685 | if (!switch_xml_ent_ok(n, ent[i + 1], ent)) { // circular reference | |
686 | if (ent[i + 1] != v) | |
687 | free(ent[i + 1]); | |
688 | switch_xml_err(root, v, "circular entity declaration &%s", n); | |
689 | break; | |
690 | } else | |
691 | ent[i] = n; // set entity name | |
692 | } else if (!strncmp(s, "<!ATTLIST", 9)) { // parse default attributes | |
693 | t = s + strspn(s + 9, SWITCH_XML_WS) + 9; // skip whitespace separator | |
694 | if (!*t) { | |
695 | switch_xml_err(root, t, "unclosed <!ATTLIST"); | |
696 | break; | |
697 | } | |
698 | if (*(s = t + strcspn(t, SWITCH_XML_WS ">")) == '>') | |
699 | continue; | |
700 | else | |
701 | *s = '\0'; // null terminate tag name | |
702 | for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++); | |
703 | ||
704 | while (*(n = ++s + strspn(s, SWITCH_XML_WS)) && *n != '>') { | |
705 | if (*(s = n + strcspn(n, SWITCH_XML_WS))) | |
706 | *s = '\0'; // attr name | |
707 | else { | |
708 | switch_xml_err(root, t, "malformed <!ATTLIST"); | |
709 | break; | |
710 | } | |
711 | ||
712 | s += strspn(s + 1, SWITCH_XML_WS) + 1; // find next token | |
713 | c = (strncmp(s, "CDATA", 5)) ? (char *)"*" : (char *)" "; // is it cdata? | |
714 | if (!strncmp(s, "NOTATION", 8)) | |
715 | s += strspn(s + 8, SWITCH_XML_WS) + 8; | |
716 | s = (*s == '(') ? strchr(s, ')') : s + strcspn(s, SWITCH_XML_WS); | |
717 | if (!s) { | |
718 | switch_xml_err(root, t, "malformed <!ATTLIST"); | |
719 | break; | |
720 | } | |
721 | ||
722 | s += strspn(s, SWITCH_XML_WS ")"); // skip white space separator | |
723 | if (!strncmp(s, "#FIXED", 6)) | |
724 | s += strspn(s + 6, SWITCH_XML_WS) + 6; | |
725 | if (*s == '#') { // no default value | |
726 | s += strcspn(s, SWITCH_XML_WS ">") - 1; | |
727 | if (*c == ' ') | |
728 | continue; // cdata is default, nothing to do | |
729 | v = NULL; | |
730 | } else if ((*s == '"' || *s == '\'') && // default value | |
731 | (s = strchr(v = s + 1, *s))) | |
732 | *s = '\0'; | |
733 | else { | |
734 | switch_xml_err(root, t, "malformed <!ATTLIST"); | |
735 | break; | |
736 | } | |
737 | ||
738 | if (!root->attr[i]) { // new tag name | |
739 | root->attr = (!i) ? (char ***)malloc(2 * sizeof(char **)) | |
740 | : (char ***)realloc(root->attr, (i + 2) * sizeof(char **)); | |
741 | root->attr[i] = (char **)malloc(2 * sizeof(char *)); | |
742 | root->attr[i][0] = t; // set tag name | |
743 | root->attr[i][1] = (char *) (root->attr[i + 1] = NULL); | |
744 | } | |
745 | ||
746 | for (j = 1; root->attr[i][j]; j += 3); // find end of list | |
747 | sstmp = (char **)realloc(root->attr[i], (j + 4) * sizeof(char *)); | |
748 | ||
749 | if (!sstmp) { | |
750 | switch_xml_err(root, t, "Allocation Error!"); | |
751 | break; | |
752 | } | |
753 | ||
754 | root->attr[i] = sstmp; | |
755 | root->attr[i][j + 3] = NULL; // null terminate list | |
756 | root->attr[i][j + 2] = c; // is it cdata? | |
757 | root->attr[i][j + 1] = (v) ? switch_xml_decode(v, root->ent, *c) | |
758 | : NULL; | |
759 | root->attr[i][j] = n; // attribute name | |
760 | } | |
761 | } else if (!strncmp(s, "<!--", 4)) | |
762 | s = strstr(s + 4, "-->"); // comments | |
763 | else if (!strncmp(s, "<?", 2)) { // processing instructions | |
764 | if ((s = strstr(c = s + 2, "?>"))) | |
765 | switch_xml_proc_inst(root, c, s++ - c); | |
766 | } else if (*s == '<') | |
767 | s = strchr(s, '>'); // skip other declarations | |
768 | else if (*(s++) == '%' && !root->standalone) | |
769 | break; | |
770 | } | |
771 | ||
772 | free(pe); | |
773 | return !*root->err; | |
774 | } | |
775 | ||
776 | // Converts a UTF-16 string to UTF-8. Returns a new string that must be freed | |
777 | // or NULL if no conversion was needed. | |
778 | static char *switch_xml_str2utf8(char **s, switch_size_t *len) | |
779 | { | |
780 | char *u; | |
781 | switch_size_t l = 0, sl, max = *len; | |
782 | long c, d; | |
783 | int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1; | |
784 | ||
785 | if (be == -1) | |
786 | return NULL; // not UTF-16 | |
787 | ||
788 | u = (char *)malloc(max); | |
789 | for (sl = 2; sl < *len - 1; sl += 2) { | |
790 | c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) //UTF-16BE | |
791 | : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); //UTF-16LE | |
792 | if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { // high-half | |
793 | d = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) | |
794 | : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); | |
795 | c = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000; | |
796 | } | |
797 | ||
798 | while (l + 6 > max) { | |
799 | char *tmp; | |
800 | tmp = (char *)realloc(u, max += SWITCH_XML_BUFSIZE); | |
801 | if (!tmp) return NULL; | |
802 | u = tmp; | |
803 | } | |
804 | if (c < 0x80) | |
805 | u[l++] = (char) c; // US-ASCII subset | |
806 | else { // multi-byte UTF-8 sequence | |
807 | for (b = 0, d = c; d; d /= 2) | |
808 | b++; // bits in c | |
809 | b = (b - 2) / 5; // bytes in payload | |
810 | u[l++] = (char) ((0xFF << (7 - b)) | (c >> (6 * b))); // head | |
811 | while (b) | |
812 | u[l++] = (char) (0x80 | ((c >> (6 * --b)) & 0x3F)); // payload | |
813 | } | |
814 | } | |
815 | return *s = (char *)realloc(u, *len = l); | |
816 | } | |
817 | ||
818 | // frees a tag attribute list | |
819 | static void switch_xml_free_attr(char **attr) | |
820 | { | |
821 | int i = 0; | |
822 | char *m; | |
823 | ||
824 | if (!attr || attr == SWITCH_XML_NIL) | |
825 | return; // nothing to free | |
826 | while (attr[i]) | |
827 | i += 2; // find end of attribute list | |
828 | m = attr[i + 1]; // list of which names and values are malloced | |
829 | for (i = 0; m[i]; i++) { | |
830 | if (m[i] & SWITCH_XML_NAMEM) | |
831 | free(attr[i * 2]); | |
832 | if (m[i] & SWITCH_XML_TXTM) | |
833 | free(attr[(i * 2) + 1]); | |
834 | } | |
835 | free(m); | |
836 | free(attr); | |
837 | } | |
838 | ||
839 | // parse the given xml string and return an switch_xml structure | |
840 | SWITCH_DECLARE(switch_xml_t) switch_xml_parse_str(char *s, switch_size_t len) | |
841 | { | |
842 | switch_xml_root_t root = (switch_xml_root_t) switch_xml_new(NULL); | |
843 | char q, e, *d, **attr, **a = NULL; // initialize a to avoid compile warning | |
844 | int l, i, j; | |
845 | ||
846 | root->m = s; | |
847 | if (!len) | |
848 | return switch_xml_err(root, s, "root tag missing"); | |
849 | root->u = switch_xml_str2utf8(&s, &len); // convert utf-16 to utf-8 | |
850 | root->e = (root->s = s) + len; // record start and end of work area | |
851 | ||
852 | e = s[len - 1]; // save end char | |
853 | s[len - 1] = '\0'; // turn end char into null terminator | |
854 | ||
855 | while (*s && *s != '<') | |
856 | s++; // find first tag | |
857 | if (!*s) | |
858 | return switch_xml_err(root, s, "root tag missing"); | |
859 | ||
860 | for (;;) { | |
861 | attr = (char **) SWITCH_XML_NIL; | |
862 | d = ++s; | |
863 | ||
864 | if (isalpha((int) (*s)) || *s == '_' || *s == ':' || (int8_t) *s < '\0') { // new tag | |
865 | if (!root->cur) | |
866 | return switch_xml_err(root, d, "markup outside of root element"); | |
867 | ||
868 | s += strcspn(s, SWITCH_XML_WS "/>"); | |
869 | while (isspace((int) (*s))) | |
870 | *(s++) = '\0'; // null terminate tag name | |
871 | ||
872 | if (*s && *s != '/' && *s != '>') // find tag in default attr list | |
873 | for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++); | |
874 | ||
875 | for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { // new attrib | |
876 | attr = (l) ? (char **)realloc(attr, (l + 4) * sizeof(char *)) | |
877 | : (char **)malloc(4 * sizeof(char *)); // allocate space | |
878 | attr[l + 3] = (l) ? (char *)realloc(attr[l + 1], (l / 2) + 2) | |
879 | : (char *)malloc(2); // mem for list of maloced vals | |
880 | strcpy(attr[l + 3] + (l / 2), " "); // value is not malloced | |
881 | attr[l + 2] = NULL; // null terminate list | |
882 | attr[l + 1] = (char *)""; // temporary attribute value | |
883 | attr[l] = s; // set attribute name | |
884 | ||
885 | s += strcspn(s, SWITCH_XML_WS "=/>"); | |
886 | if (*s == '=' || isspace((int) (*s))) { | |
887 | *(s++) = '\0'; // null terminate tag attribute name | |
888 | q = *(s += strspn(s, SWITCH_XML_WS "=")); | |
889 | if (q == '"' || q == '\'') { // attribute value | |
890 | attr[l + 1] = ++s; | |
891 | while (*s && *s != q) | |
892 | s++; | |
893 | if (*s) | |
894 | *(s++) = '\0'; // null terminate attribute val | |
895 | else { | |
896 | switch_xml_free_attr(attr); | |
897 | return switch_xml_err(root, d, "missing %c", q); | |
898 | } | |
899 | ||
900 | for (j = 1; a && a[j] && strcmp(a[j], attr[l]); j += 3); | |
901 | attr[l + 1] = switch_xml_decode(attr[l + 1], root->ent, (a && a[j]) ? *a[j + 2] : ' '); | |
902 | if (attr[l + 1] < d || attr[l + 1] > s) | |
903 | attr[l + 3][l / 2] = SWITCH_XML_TXTM; // value malloced | |
904 | } | |
905 | } | |
906 | while (isspace((int) (*s))) | |
907 | s++; | |
908 | } | |
909 | ||
910 | if (*s == '/') { // self closing tag | |
911 | *(s++) = '\0'; | |
912 | if ((*s && *s != '>') || (!*s && e != '>')) { | |
913 | if (l) | |
914 | switch_xml_free_attr(attr); | |
915 | return switch_xml_err(root, d, "missing >"); | |
916 | } | |
917 | switch_xml_open_tag(root, d, attr); | |
918 | switch_xml_close_tag(root, d, s); | |
919 | } else if ((q = *s) == '>' || (!*s && e == '>')) { // open tag | |
920 | *s = '\0'; // temporarily null terminate tag name | |
921 | switch_xml_open_tag(root, d, attr); | |
922 | *s = q; | |
923 | } else { | |
924 | if (l) | |
925 | switch_xml_free_attr(attr); | |
926 | return switch_xml_err(root, d, "missing >"); | |
927 | } | |
928 | } else if (*s == '/') { // close tag | |
929 | s += strcspn(d = s + 1, SWITCH_XML_WS ">") + 1; | |
930 | if (!(q = *s) && e != '>') | |
931 | return switch_xml_err(root, d, "missing >"); | |
932 | *s = '\0'; // temporarily null terminate tag name | |
933 | if (switch_xml_close_tag(root, d, s)) | |
934 | return &root->xml; | |
935 | if (isspace((int) (*s = q))) | |
936 | s += strspn(s, SWITCH_XML_WS); | |
937 | } else if (!strncmp(s, "!--", 3)) { // xml comment | |
938 | if (!(s = strstr(s + 3, "--")) || (*(s += 2) != '>' && *s) || (!*s && e != '>')) | |
939 | return switch_xml_err(root, d, "unclosed <!--"); | |
940 | } else if (!strncmp(s, "![CDATA[", 8)) { // cdata | |
941 | if ((s = strstr(s, "]]>"))) | |
942 | switch_xml_char_content(root, d + 8, (s += 2) - d - 10, 'c'); | |
943 | else | |
944 | return switch_xml_err(root, d, "unclosed <![CDATA["); | |
945 | } else if (!strncmp(s, "!DOCTYPE", 8)) { // dtd | |
946 | for (l = 0; *s && ((!l && *s != '>') || (l && (*s != ']' || *(s + strspn(s + 1, SWITCH_XML_WS) + 1) != '>'))); l = (*s == '[') ? 1 : l) | |
947 | s += strcspn(s + 1, "[]>") + 1; | |
948 | if (!*s && e != '>') | |
949 | return switch_xml_err(root, d, "unclosed <!DOCTYPE"); | |
950 | d = (l) ? strchr(d, '[') + 1 : d; | |
951 | if (l && !switch_xml_internal_dtd(root, d, s++ - d)) | |
952 | return &root->xml; | |
953 | } else if (*s == '?') { // <?...?> processing instructions | |
954 | do { | |
955 | s = strchr(s, '?'); | |
956 | } while (s && *(++s) && *s != '>'); | |
957 | if (!s || (!*s && e != '>')) | |
958 | return switch_xml_err(root, d, "unclosed <?"); | |
959 | else | |
960 | switch_xml_proc_inst(root, d + 1, s - d - 2); | |
961 | } else | |
962 | return switch_xml_err(root, d, "unexpected <"); | |
963 | ||
964 | if (!s || !*s) | |
965 | break; | |
966 | *s = '\0'; | |
967 | d = ++s; | |
968 | if (*s && *s != '<') { // tag character content | |
969 | while (*s && *s != '<') | |
970 | s++; | |
971 | if (*s) | |
972 | switch_xml_char_content(root, d, s - d, '&'); | |
973 | else | |
974 | break; | |
975 | } else if (!*s) | |
976 | break; | |
977 | } | |
978 | ||
979 | if (!root->cur) | |
980 | return &root->xml; | |
981 | else if (!root->cur->name) | |
982 | return switch_xml_err(root, d, "root tag missing"); | |
983 | else | |
984 | return switch_xml_err(root, d, "unclosed tag <%s>", root->cur->name); | |
985 | } | |
986 | ||
987 | // Wrapper for switch_xml_parse_str() that accepts a file stream. Reads the entire | |
988 | // stream into memory and then parses it. For xml files, use switch_xml_parse_file() | |
989 | // or switch_xml_parse_fd() | |
990 | SWITCH_DECLARE(switch_xml_t) switch_xml_parse_fp(FILE * fp) | |
991 | { | |
992 | switch_xml_root_t root; | |
993 | switch_size_t l, len = 0; | |
994 | char *s; | |
995 | ||
996 | if (!(s = (char *)malloc(SWITCH_XML_BUFSIZE))) | |
997 | return NULL; | |
998 | do { | |
999 | len += (l = fread((s + len), 1, SWITCH_XML_BUFSIZE, fp)); | |
1000 | if (l == SWITCH_XML_BUFSIZE) { | |
1001 | char *tmp = (char *)realloc(s, len + SWITCH_XML_BUFSIZE); | |
1002 | if (!tmp) { | |
1003 | free(s); | |
1004 | return NULL; | |
1005 | } | |
1006 | s = tmp; | |
1007 | } | |
1008 | } while (s && l == SWITCH_XML_BUFSIZE); | |
1009 | ||
1010 | if (!s) | |
1011 | return NULL; | |
1012 | root = (switch_xml_root_t) switch_xml_parse_str(s, len); | |
1013 | root->dynamic = 1; // so we know to free s in switch_xml_free() | |
1014 | return &root->xml; | |
1015 | } | |
1016 | ||
1017 | // A wrapper for switch_xml_parse_str() that accepts a file descriptor. First | |
1018 | // attempts to mem map the file. Failing that, reads the file into memory. | |
1019 | // Returns NULL on failure. | |
1020 | SWITCH_DECLARE(switch_xml_t) switch_xml_parse_fd(int fd) | |
1021 | { | |
1022 | switch_xml_root_t root; | |
1023 | struct stat st; | |
1024 | switch_size_t l; | |
1025 | void *m; | |
1026 | ||
1027 | if (fd < 0) | |
1028 | return NULL; | |
1029 | fstat(fd, &st); | |
1030 | ||
1031 | #ifdef HAVE_MMAP | |
1032 | l = (st.st_size + sysconf(_SC_PAGESIZE) - 1) & ~(sysconf(_SC_PAGESIZE) - 1); | |
1033 | if ((m = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) != MAP_FAILED) { | |
1034 | madvise(m, l, MADV_SEQUENTIAL); // optimize for sequential access | |
1035 | root = (switch_xml_root_t) switch_xml_parse_str(m, st.st_size); | |
1036 | madvise(m, root->len = l, MADV_NORMAL); // put it back to normal | |
1037 | } else { // mmap failed, read file into memory | |
1038 | #endif // HAVE_MMAP | |
1039 | m = malloc(st.st_size); | |
1040 | if (!m) return NULL; | |
1041 | l = read(fd, m, st.st_size); | |
1042 | root = (switch_xml_root_t) switch_xml_parse_str((char *)m, l); | |
1043 | root->dynamic = 1; // so we know to free s in switch_xml_free() | |
1044 | #ifdef HAVE_MMAP | |
1045 | } | |
1046 | #endif // HAVE_MMAP | |
1047 | return &root->xml; | |
1048 | } | |
1049 | ||
1050 | static char *expand_vars(char *buf, char *ebuf, switch_size_t elen, switch_size_t *newlen) | |
1051 | { | |
1052 | char *var, *val; | |
1053 | char *rp = buf; | |
1054 | char *wp = ebuf; | |
1055 | char *ep = ebuf + elen - 1; | |
1056 | ||
1057 | if (!(var = strstr(rp, "$${"))) { | |
1058 | *newlen = strlen(buf); | |
1059 | return buf; | |
1060 | } | |
1061 | ||
1062 | while (*rp && wp < ep) { | |
1063 | ||
1064 | if (*rp == '$' && *(rp + 1) == '$' && *(rp + 2) == '{') { | |
1065 | char *e = strchr(rp, '}'); | |
1066 | ||
1067 | if (e) { | |
1068 | rp += 3; | |
1069 | var = rp; | |
1070 | *e++ = '\0'; | |
1071 | rp = e; | |
1072 | if ((val = switch_core_get_variable(var))) { | |
1073 | char *p; | |
1074 | for (p = val; p && *p && wp <= ep; p++) { | |
1075 | *wp++ = *p; | |
1076 | } | |
1077 | } | |
1078 | } | |
1079 | } | |
1080 | ||
1081 | *wp++ = *rp++; | |
1082 | } | |
1083 | *wp++ = '\0'; | |
1084 | *newlen = strlen(ebuf); | |
1085 | ||
1086 | return ebuf; | |
1087 | } | |
1088 | ||
1089 | static int preprocess_glob(const char *cwd, const char *pattern, int write_fd, int rlevel) | |
1090 | { | |
1091 | char *full_path = NULL; | |
1092 | char *dir_path = NULL, *e = NULL; | |
1093 | glob_t glob_data; | |
1094 | size_t n; | |
1095 | ||
1096 | if (!switch_is_file_path(pattern)) { | |
1097 | full_path = switch_mprintf("%s%s%s", cwd, SWITCH_PATH_SEPARATOR, pattern); | |
1098 | pattern = full_path; | |
1099 | } | |
1100 | ||
d0688264 | 1101 | if (glob(pattern, GLOB_NOCHECK, NULL, &glob_data) != 0) { |
59241895 MJ |
1102 | if (stderr) { |
1103 | fprintf(stderr, "Error including %s\n", pattern); | |
1104 | } | |
1105 | goto end; | |
1106 | } | |
1107 | ||
1108 | for (n = 0; n < glob_data.gl_pathc; ++n) { | |
1109 | dir_path = strdup(glob_data.gl_pathv[n]); | |
1110 | switch_assert(dir_path); | |
1111 | if ((e = strrchr(dir_path, *SWITCH_PATH_SEPARATOR))) { | |
1112 | *e = '\0'; | |
1113 | } | |
1114 | if (preprocess(dir_path, glob_data.gl_pathv[n], write_fd, rlevel) < 0) { | |
1115 | const char *reason = strerror(errno); | |
1116 | if (rlevel > 100) { | |
1117 | reason = "Maximum recursion limit reached"; | |
1118 | } | |
1119 | fprintf(stderr, "Error including %s (%s)\n", pattern, reason); | |
1120 | } | |
7ffcb0e6 | 1121 | free(dir_path); |
59241895 MJ |
1122 | } |
1123 | globfree(&glob_data); | |
1124 | ||
1125 | end: | |
1126 | ||
1127 | switch_safe_free(full_path); | |
1128 | ||
1129 | return write_fd; | |
1130 | } | |
1131 | ||
1132 | static int preprocess(const char *cwd, const char *file, int write_fd, int rlevel) | |
1133 | { | |
1134 | int read_fd = -1; | |
1135 | switch_size_t cur = 0, ml = 0; | |
1136 | char *q, *cmd, buf[2048], ebuf[8192]; | |
1137 | char *tcmd, *targ; | |
1138 | ||
1139 | if ((read_fd = open(file, O_RDONLY, 0)) < 0) { | |
1140 | return read_fd; | |
1141 | } | |
1142 | ||
1143 | if (rlevel > 100) { | |
1144 | return -1; | |
1145 | } | |
1146 | ||
1147 | while ((cur = switch_fd_read_line(read_fd, buf, sizeof(buf))) > 0) { | |
1148 | char *arg, *e; | |
1149 | char *bp = expand_vars(buf, ebuf, sizeof(ebuf), &cur); | |
1150 | ||
1151 | /* we ignore <include> or </include> for the sake of validators as well as <?xml version="1.0"?> type stuff */ | |
1152 | if (strstr(buf, "<include>") || strstr(buf, "</include>") || strstr(buf, "<?")) { | |
1153 | continue; | |
1154 | } | |
1155 | ||
1156 | if (ml) { | |
1157 | if ((e = strstr(buf, "-->"))) { | |
1158 | ml = 0; | |
1159 | bp = e + 3; | |
1160 | cur = strlen(bp); | |
1161 | } else { | |
1162 | continue; | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | if ((tcmd = (char *)switch_stristr("X-pre-process", bp))) { | |
1167 | if (*(tcmd-1) != '<') { | |
1168 | continue; | |
1169 | } | |
1170 | if ((e = strstr(tcmd, "/>"))) { | |
1171 | *e += 2; | |
1172 | *e = '\0'; | |
d0688264 | 1173 | if (write(write_fd, e, (unsigned) strlen(e)) != (int) strlen(e)) { |
59241895 MJ |
1174 | switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Short write!\n"); |
1175 | } | |
1176 | } | |
1177 | ||
1178 | if (!(tcmd = (char *)switch_stristr("cmd", tcmd))) { | |
1179 | continue; | |
1180 | } | |
1181 | ||
1182 | if (!(tcmd = (char *)switch_stristr("=", tcmd))) { | |
1183 | continue; | |
1184 | } | |
1185 | ||
1186 | if (!(tcmd = (char *)switch_stristr("\"", tcmd))) { | |
1187 | continue; | |
1188 | } | |
1189 | ||
1190 | tcmd++; | |
1191 | ||
1192 | ||
1193 | if ((e = strchr(tcmd, '"'))) { | |
1194 | *e++ = '\0'; | |
1195 | } | |
1196 | ||
1197 | if (!(targ = (char *)switch_stristr("data", e))) { | |
1198 | continue; | |
1199 | } | |
1200 | ||
1201 | if (!(targ = (char *)switch_stristr("=", targ))) { | |
1202 | continue; | |
1203 | } | |
1204 | ||
1205 | if (!(targ = (char *)switch_stristr("\"", targ))) { | |
1206 | continue; | |
1207 | } | |
1208 | ||
1209 | targ++; | |
1210 | ||
1211 | if ((e = strchr(targ, '"'))) { | |
1212 | *e++ = '\0'; | |
1213 | } | |
1214 | ||
1215 | if (!strcasecmp(tcmd, "set")) { | |
1216 | char *name = (char *)targ; | |
1217 | char *val = strchr(name, '='); | |
1218 | ||
1219 | if (val) { | |
1220 | char *ve = val++; | |
1221 | while (*val && *val == ' ') { | |
1222 | val++; | |
1223 | } | |
1224 | *ve-- = '\0'; | |
1225 | while (*ve && *ve == ' ') { | |
1226 | *ve-- = '\0'; | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | if (name && val) { | |
1231 | switch_core_set_variable(name, val); | |
1232 | } | |
1233 | ||
1234 | } else if (!strcasecmp(tcmd, "include")) { | |
1235 | preprocess_glob(cwd, targ, write_fd, rlevel + 1); | |
1236 | } | |
1237 | ||
1238 | continue; | |
1239 | } | |
1240 | ||
1241 | if ((cmd = strstr(bp, "<!--#"))) { | |
d0688264 | 1242 | if (write(write_fd, bp, (unsigned) (cmd - bp)) != (cmd - bp)) { |
59241895 MJ |
1243 | switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Short write!\n"); |
1244 | } | |
1245 | if ((e = strstr(cmd, "-->"))) { | |
1246 | *e = '\0'; | |
1247 | e += 3; | |
d0688264 | 1248 | if (write(write_fd, e, (unsigned) strlen(e)) != (int) strlen(e)) { |
59241895 MJ |
1249 | switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Short write!\n"); |
1250 | } | |
1251 | } else { | |
1252 | ml++; | |
1253 | } | |
1254 | ||
1255 | cmd += 5; | |
1256 | if ((e = strchr(cmd, '\r')) || (e = strchr(cmd, '\n'))) { | |
1257 | *e = '\0'; | |
1258 | } | |
1259 | ||
1260 | if ((arg = strchr(cmd, ' '))) { | |
1261 | *arg++ = '\0'; | |
1262 | if ((q = strchr(arg, '"'))) { | |
1263 | char *qq = q + 1; | |
1264 | ||
1265 | if ((qq = strchr(qq, '"'))) { | |
1266 | *qq = '\0'; | |
1267 | arg = q + 1; | |
1268 | } | |
1269 | } | |
1270 | ||
1271 | if (!strcasecmp(cmd, "set")) { | |
1272 | char *name = arg; | |
1273 | char *val = strchr(name, '='); | |
1274 | ||
1275 | if (val) { | |
1276 | char *ve = val++; | |
1277 | while (*val && *val == ' ') { | |
1278 | val++; | |
1279 | } | |
1280 | *ve-- = '\0'; | |
1281 | while (*ve && *ve == ' ') { | |
1282 | *ve-- = '\0'; | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | if (name && val) { | |
1287 | switch_core_set_variable(name, val); | |
1288 | } | |
1289 | ||
1290 | } else if (!strcasecmp(cmd, "include")) { | |
1291 | preprocess_glob(cwd, arg, write_fd, rlevel + 1); | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | continue; | |
1296 | } | |
1297 | ||
d0688264 | 1298 | if (write(write_fd, bp, (unsigned) cur) != (int) cur) { |
59241895 MJ |
1299 | switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Short write!\n"); |
1300 | } | |
1301 | } | |
1302 | ||
1303 | close(read_fd); | |
1304 | return write_fd; | |
1305 | } | |
1306 | ||
1307 | SWITCH_DECLARE(switch_xml_t) switch_xml_parse_file_simple(const char *file) | |
1308 | { | |
1309 | int fd = -1; | |
1310 | struct stat st; | |
1311 | switch_size_t l; | |
1312 | void *m; | |
1313 | switch_xml_root_t root; | |
1314 | ||
1315 | if ((fd = open(file, O_RDONLY, 0)) > -1) { | |
1316 | fstat(fd, &st); | |
1317 | m = malloc(st.st_size); | |
1318 | switch_assert(m); | |
1319 | l = read(fd, m, st.st_size); | |
1320 | root = (switch_xml_root_t) switch_xml_parse_str((char *)m, l); | |
1321 | root->dynamic = 1; | |
1322 | close(fd); | |
1323 | return &root->xml; | |
1324 | } | |
1325 | ||
1326 | return NULL; | |
1327 | } | |
1328 | ||
1329 | SWITCH_DECLARE(switch_xml_t) switch_xml_parse_file(const char *file) | |
1330 | { | |
1331 | int fd = -1, write_fd = -1; | |
1332 | switch_xml_t xml = NULL; | |
1333 | char *new_file = NULL; | |
1334 | const char *abs; | |
1335 | ||
1336 | if ((abs = strrchr(file, '/')) || (abs = strrchr(file, '\\'))) { | |
1337 | abs++; | |
1338 | } else { | |
1339 | abs = file; | |
1340 | } | |
1341 | ||
1342 | if (!(new_file = switch_mprintf("%s%s%s.fsxml", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, abs))) { | |
1343 | return NULL; | |
1344 | } | |
1345 | ||
1346 | if ((write_fd = open(new_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) < 0) { | |
1347 | goto done; | |
1348 | } | |
1349 | ||
1350 | if (preprocess(SWITCH_GLOBAL_dirs.conf_dir, file, write_fd, 0) > -1) { | |
1351 | close(write_fd); | |
1352 | write_fd = -1; | |
1353 | if ((fd = open(new_file, O_RDONLY, 0)) > -1) { | |
1354 | if ((xml = switch_xml_parse_fd(fd))) { | |
1355 | xml->free_path = new_file; | |
1356 | new_file = NULL; | |
1357 | } | |
1358 | close(fd); | |
1359 | fd = -1; | |
1360 | } | |
1361 | } | |
1362 | ||
1363 | done: | |
1364 | if (write_fd > -1) { | |
1365 | close(write_fd); | |
1366 | } | |
1367 | if (fd > -1) { | |
1368 | close(fd); | |
1369 | } | |
1370 | switch_safe_free(new_file); | |
1371 | return xml; | |
1372 | } | |
1373 | ||
1374 | SWITCH_DECLARE(switch_status_t) switch_xml_locate(const char *section, | |
1375 | const char *tag_name, | |
1376 | const char *key_name, const char *key_value, switch_xml_t * root, switch_xml_t * node, | |
1377 | switch_event_t *params) | |
1378 | { | |
1379 | switch_xml_t conf = NULL; | |
1380 | switch_xml_t tag = NULL; | |
1381 | switch_xml_t xml = NULL; | |
1382 | switch_xml_binding_t *binding; | |
1383 | uint8_t loops = 0; | |
1384 | ||
8ec9b8f7 | 1385 | switch_thread_rwlock_rdlock(B_RWLOCK); |
59241895 MJ |
1386 | |
1387 | for (binding = BINDINGS; binding; binding = binding->next) { | |
1388 | switch_xml_section_t sections = switch_xml_parse_section_string(section); | |
1389 | ||
1390 | if (binding->sections && !(sections & binding->sections)) { | |
1391 | continue; | |
1392 | } | |
1393 | ||
1394 | if ((xml = binding->function(section, tag_name, key_name, key_value, params, binding->user_data))) { | |
1395 | const char *err = NULL; | |
1396 | ||
1397 | err = switch_xml_error(xml); | |
1398 | if (switch_strlen_zero(err)) { | |
1399 | if ((conf = switch_xml_find_child(xml, "section", "name", "result"))) { | |
1400 | switch_xml_t p; | |
1401 | const char *aname; | |
1402 | ||
1403 | if ((p = switch_xml_child(conf, "result"))) { | |
1404 | aname = switch_xml_attr(p, "status"); | |
1405 | if (aname && !strcasecmp(aname, "not found")) { | |
1406 | switch_xml_free(xml); | |
1407 | xml = NULL; | |
1408 | continue; | |
1409 | } | |
1410 | } | |
1411 | } | |
1412 | break; | |
1413 | } else { | |
1414 | switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error[%s]\n", err); | |
1415 | switch_xml_free(xml); | |
1416 | xml = NULL; | |
1417 | } | |
1418 | } | |
1419 | } | |
8ec9b8f7 | 1420 | switch_thread_rwlock_unlock(B_RWLOCK); |
59241895 MJ |
1421 | |
1422 | for (;;) { | |
1423 | if (!xml) { | |
1424 | if (!(xml = switch_xml_root())) { | |
1425 | *node = NULL; | |
1426 | *root = NULL; | |
1427 | return SWITCH_STATUS_FALSE; | |
1428 | } | |
1429 | } | |
1430 | ||
1431 | if ((conf = switch_xml_find_child(xml, "section", "name", section)) && (tag = switch_xml_find_child(conf, tag_name, key_name, key_value))) { | |
1432 | *node = tag; | |
1433 | *root = xml; | |
1434 | return SWITCH_STATUS_SUCCESS; | |
1435 | } else { | |
1436 | switch_xml_free(xml); | |
1437 | xml = NULL; | |
1438 | *node = NULL; | |
1439 | *root = NULL; | |
1440 | if (loops++ > 1) { | |
1441 | break; | |
1442 | } | |
1443 | } | |
1444 | } | |
1445 | ||
1446 | return SWITCH_STATUS_FALSE; | |
1447 | } | |
1448 | ||
1449 | SWITCH_DECLARE(switch_status_t) switch_xml_locate_domain(const char *domain_name, switch_event_t *params, switch_xml_t *root, switch_xml_t *domain) | |
1450 | { | |
1451 | switch_event_t *my_params = NULL; | |
1452 | switch_status_t status; | |
1453 | *domain = NULL; | |
1454 | ||
1455 | if (!params) { | |
003847dd | 1456 | switch_event_create(&my_params, SWITCH_EVENT_REQUEST_PARAMS); |
59241895 MJ |
1457 | switch_assert(my_params); |
1458 | switch_event_add_header_string(my_params, SWITCH_STACK_BOTTOM, "domain", domain_name); | |
1459 | params = my_params; | |
1460 | } | |
1461 | ||
1462 | status = switch_xml_locate("directory", "domain", "name", domain_name, root, domain, params); | |
1463 | if (my_params) { | |
1464 | switch_event_destroy(&my_params); | |
1465 | } | |
1466 | return status; | |
1467 | } | |
1468 | ||
1469 | SWITCH_DECLARE(switch_status_t) switch_xml_locate_user(const char *key, | |
1470 | const char *user_name, | |
1471 | const char *domain_name, | |
1472 | const char *ip, | |
1473 | switch_xml_t *root, | |
1474 | switch_xml_t *domain, | |
1475 | switch_xml_t *user, | |
1476 | switch_event_t *params) | |
1477 | { | |
5e5847f3 AM |
1478 | switch_status_t status = SWITCH_STATUS_FALSE; |
1479 | switch_event_t *my_params = NULL; | |
1480 | ||
59241895 MJ |
1481 | *root = NULL; |
1482 | *user = NULL; | |
1483 | *domain = NULL; | |
1484 | ||
5e5847f3 | 1485 | if (!params) { |
003847dd | 1486 | switch_event_create(&my_params, SWITCH_EVENT_REQUEST_PARAMS); |
5e5847f3 AM |
1487 | switch_assert(my_params); |
1488 | params = my_params; | |
1489 | } | |
59241895 | 1490 | |
5e5847f3 | 1491 | switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "key", key); |
59241895 | 1492 | |
5e5847f3 AM |
1493 | if (user_name) { |
1494 | switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "user", user_name); | |
1495 | } | |
59241895 | 1496 | |
5e5847f3 AM |
1497 | if (domain_name) { |
1498 | switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "domain", domain_name); | |
59241895 | 1499 | } |
59241895 | 1500 | |
5e5847f3 AM |
1501 | if (ip) { |
1502 | switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "ip", ip); | |
1503 | } | |
1504 | ||
59241895 | 1505 | if ((status = switch_xml_locate_domain(domain_name, params, root, domain)) != SWITCH_STATUS_SUCCESS) { |
5e5847f3 | 1506 | goto end; |
59241895 MJ |
1507 | } |
1508 | ||
a2a748dd AM |
1509 | status = SWITCH_STATUS_FALSE; |
1510 | ||
59241895 MJ |
1511 | if (ip) { |
1512 | if ((*user = switch_xml_find_child(*domain, "user", "ip", ip))) { | |
5e5847f3 AM |
1513 | status = SWITCH_STATUS_SUCCESS; |
1514 | goto end; | |
59241895 MJ |
1515 | } |
1516 | } | |
1517 | ||
1518 | if (user_name) { | |
5e5847f3 | 1519 | if (params != my_params && switch_event_get_header(params, (char *) "mailbox")) { |
59241895 | 1520 | if ((*user = switch_xml_find_child(*domain, "user", "mailbox", user_name))) { |
5e5847f3 AM |
1521 | status = SWITCH_STATUS_SUCCESS; |
1522 | goto end; | |
59241895 MJ |
1523 | } |
1524 | } | |
1525 | ||
1526 | if ((*user = switch_xml_find_child(*domain, "user", key, user_name))) { | |
5e5847f3 AM |
1527 | status = SWITCH_STATUS_SUCCESS; |
1528 | goto end; | |
59241895 | 1529 | } |
59241895 MJ |
1530 | } |
1531 | ||
5e5847f3 AM |
1532 | end: |
1533 | ||
1534 | if (my_params) { | |
1535 | switch_event_destroy(&my_params); | |
1536 | } | |
1537 | ||
1538 | return status; | |
59241895 MJ |
1539 | } |
1540 | ||
1541 | SWITCH_DECLARE(switch_xml_t) switch_xml_root(void) | |
1542 | { | |
59241895 MJ |
1543 | switch_thread_rwlock_rdlock(RWLOCK); |
1544 | return MAIN_XML_ROOT; | |
1545 | } | |
1546 | ||
7e9f64ee AM |
1547 | |
1548 | struct destroy_xml { | |
1549 | switch_xml_t xml; | |
1550 | switch_memory_pool_t *pool; | |
1551 | }; | |
1552 | ||
1553 | static void *SWITCH_THREAD_FUNC destroy_thread(switch_thread_t *thread, void *obj) | |
1554 | { | |
1555 | struct destroy_xml *dx = (struct destroy_xml *) obj; | |
1556 | switch_memory_pool_t *pool = dx->pool; | |
1557 | switch_xml_free(dx->xml); | |
1558 | switch_core_destroy_memory_pool(&pool); | |
1559 | return NULL; | |
1560 | } | |
1561 | ||
1562 | SWITCH_DECLARE(void) switch_xml_free_in_thread(switch_xml_t xml, int stacksize) | |
1563 | { | |
1564 | switch_thread_t *thread; | |
1565 | switch_threadattr_t *thd_attr; | |
1566 | switch_memory_pool_t *pool = NULL; | |
1567 | struct destroy_xml *dx; | |
1568 | ||
1569 | switch_core_new_memory_pool(&pool); | |
1570 | ||
1571 | switch_threadattr_create(&thd_attr, pool); | |
1572 | switch_threadattr_detach_set(thd_attr, 1); | |
1573 | // TBD figure out how much space we need by looking at the xml_t when stacksize == 0 | |
1574 | switch_threadattr_stacksize_set(thd_attr, stacksize); | |
1575 | ||
1576 | dx = switch_core_alloc(pool, sizeof(*dx)); | |
1577 | dx->pool = pool; | |
1578 | dx->xml = xml; | |
1579 | ||
1580 | switch_thread_create(&thread, thd_attr, destroy_thread, dx, pool); | |
1581 | ||
1582 | } | |
1583 | ||
59241895 MJ |
1584 | static char not_so_threadsafe_error_buffer[256] = ""; |
1585 | ||
1586 | SWITCH_DECLARE(switch_xml_t) switch_xml_open_root(uint8_t reload, const char **err) | |
1587 | { | |
1588 | char path_buf[1024]; | |
1589 | uint8_t hasmain = 0, errcnt = 0; | |
1590 | switch_xml_t new_main; | |
1591 | ||
c5831538 | 1592 | switch_mutex_lock(XML_LOCK); |
59241895 MJ |
1593 | |
1594 | if (MAIN_XML_ROOT) { | |
1595 | hasmain++; | |
1596 | ||
1597 | if (!reload) { | |
c5831538 | 1598 | switch_mutex_unlock(XML_LOCK); |
59241895 MJ |
1599 | return switch_xml_root(); |
1600 | } | |
1601 | switch_thread_rwlock_wrlock(RWLOCK); | |
1602 | } | |
1603 | ||
1604 | switch_snprintf(path_buf, sizeof(path_buf), "%s%s%s", SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR, "freeswitch.xml"); | |
1605 | if ((new_main = switch_xml_parse_file(path_buf))) { | |
1606 | *err = switch_xml_error(new_main); | |
1607 | switch_copy_string(not_so_threadsafe_error_buffer, *err, sizeof(not_so_threadsafe_error_buffer)); | |
1608 | *err = not_so_threadsafe_error_buffer; | |
1609 | if (!switch_strlen_zero(*err)) { | |
1610 | switch_xml_free(new_main); | |
1611 | new_main = NULL; | |
1612 | errcnt++; | |
1613 | } else { | |
1614 | switch_xml_t old_root; | |
1615 | *err = "Success"; | |
1616 | old_root = MAIN_XML_ROOT; | |
1617 | MAIN_XML_ROOT = new_main; | |
1618 | switch_set_flag(MAIN_XML_ROOT, SWITCH_XML_ROOT); | |
1619 | switch_xml_free(old_root); | |
7e9f64ee | 1620 | //switch_xml_free_in_thread(old_root); |
59241895 MJ |
1621 | } |
1622 | } else { | |
1623 | *err = "Cannot Open log directory or XML Root!"; | |
1624 | errcnt++; | |
1625 | } | |
1626 | ||
1627 | if (hasmain) { | |
1628 | switch_thread_rwlock_unlock(RWLOCK); | |
1629 | } | |
c5831538 AM |
1630 | |
1631 | switch_mutex_unlock(XML_LOCK); | |
59241895 MJ |
1632 | |
1633 | if (errcnt == 0) { | |
1634 | switch_event_t *event; | |
1635 | if (switch_event_create(&event, SWITCH_EVENT_RELOADXML) == SWITCH_STATUS_SUCCESS) { | |
1636 | if (switch_event_fire(&event) != SWITCH_STATUS_SUCCESS) { | |
1637 | switch_event_destroy(&event); | |
1638 | } | |
1639 | } | |
1640 | return switch_xml_root(); | |
1641 | } | |
1642 | ||
1643 | return NULL; | |
1644 | } | |
1645 | ||
1646 | SWITCH_DECLARE(switch_status_t) switch_xml_init(switch_memory_pool_t *pool, const char **err) | |
1647 | { | |
1648 | switch_xml_t xml; | |
1649 | XML_MEMORY_POOL = pool; | |
1650 | *err = "Success"; | |
1651 | ||
c5831538 | 1652 | switch_mutex_init(&XML_LOCK, SWITCH_MUTEX_NESTED, XML_MEMORY_POOL); |
59241895 | 1653 | switch_thread_rwlock_create(&RWLOCK, XML_MEMORY_POOL); |
8ec9b8f7 | 1654 | switch_thread_rwlock_create(&B_RWLOCK, XML_MEMORY_POOL); |
59241895 MJ |
1655 | |
1656 | assert(pool != NULL); | |
1657 | ||
1658 | if ((xml = switch_xml_open_root(FALSE, err))) { | |
1659 | switch_xml_free(xml); | |
1660 | return SWITCH_STATUS_SUCCESS; | |
1661 | } else { | |
1662 | return SWITCH_STATUS_FALSE; | |
1663 | } | |
1664 | } | |
1665 | ||
1666 | SWITCH_DECLARE(switch_status_t) switch_xml_destroy(void) | |
1667 | { | |
1668 | if (MAIN_XML_ROOT) { | |
1669 | switch_xml_t xml = MAIN_XML_ROOT; | |
1670 | MAIN_XML_ROOT = NULL; | |
1671 | switch_xml_free(xml); | |
1672 | return SWITCH_STATUS_SUCCESS; | |
1673 | } | |
1674 | ||
1675 | return SWITCH_STATUS_FALSE; | |
1676 | } | |
1677 | ||
1678 | SWITCH_DECLARE(switch_xml_t) switch_xml_open_cfg(const char *file_path, switch_xml_t *node, switch_event_t *params) | |
1679 | { | |
1680 | switch_xml_t xml = NULL, cfg = NULL; | |
1681 | ||
1682 | *node = NULL; | |
1683 | ||
1684 | assert(MAIN_XML_ROOT != NULL); | |
1685 | ||
1686 | if (switch_xml_locate("configuration", "configuration", "name", file_path, &xml, &cfg, params) == SWITCH_STATUS_SUCCESS) { | |
1687 | *node = cfg; | |
1688 | } | |
1689 | ||
1690 | return xml; | |
1691 | ||
1692 | } | |
1693 | ||
1694 | // Encodes ampersand sequences appending the results to *dst, reallocating *dst | |
ff3f04f1 | 1695 | // if length exceeds max. a is non-zero for attribute encoding. Returns *dst |
59241895 MJ |
1696 | static char *switch_xml_ampencode(const char *s, switch_size_t len, char **dst, switch_size_t *dlen, switch_size_t *max, short a) |
1697 | { | |
1698 | const char *e = NULL; | |
1699 | int immune = 0; | |
1700 | ||
1701 | if (!(s && *s)) return *dst; | |
1702 | ||
1703 | if (len) { | |
1704 | e = s + len; | |
1705 | } | |
1706 | ||
1707 | while (s != e) { | |
1708 | while (*dlen + 10 > *max) { | |
1709 | char *tmp = (char *)realloc(*dst, *max += SWITCH_XML_BUFSIZE); | |
1710 | if (!tmp) return *dst; | |
1711 | *dst = tmp; | |
1712 | } | |
1713 | ||
1714 | if (immune) { | |
1715 | if (*s == '\0') { | |
1716 | return *dst; | |
1717 | } | |
1718 | (*dst)[(*dlen)++] = *s; | |
1719 | } else | |
1720 | switch (*s) { | |
1721 | case '\0': | |
1722 | return *dst; | |
1723 | case '&': | |
1724 | *dlen += sprintf(*dst + *dlen, "&"); | |
1725 | break; | |
1726 | case '<': | |
1727 | if (*(s+1) == '!') { | |
1728 | (*dst)[(*dlen)++] = *s; | |
1729 | immune++; | |
1730 | break; | |
1731 | } | |
1732 | *dlen += sprintf(*dst + *dlen, "<"); | |
1733 | break; | |
1734 | case '>': | |
1735 | *dlen += sprintf(*dst + *dlen, ">"); | |
1736 | break; | |
1737 | case '"': | |
1738 | *dlen += sprintf(*dst + *dlen, (a) ? """ : "\""); | |
1739 | break; | |
1740 | case '\n': | |
1741 | *dlen += sprintf(*dst + *dlen, (a) ? "
" : "\n"); | |
1742 | break; | |
1743 | case '\t': | |
1744 | *dlen += sprintf(*dst + *dlen, (a) ? "	" : "\t"); | |
1745 | break; | |
1746 | case '\r': | |
1747 | *dlen += sprintf(*dst + *dlen, "
"); | |
1748 | break; | |
1749 | default: | |
1750 | (*dst)[(*dlen)++] = *s; | |
1751 | } | |
1752 | s++; | |
1753 | } | |
1754 | return *dst; | |
1755 | } | |
1756 | ||
1757 | #define XML_INDENT " " | |
1758 | // Recursively converts each tag to xml appending it to *s. Reallocates *s if | |
ff3f04f1 | 1759 | // its length exceeds max. start is the location of the previous tag in the |
59241895 MJ |
1760 | // parent tag's character content. Returns *s. |
1761 | static char *switch_xml_toxml_r(switch_xml_t xml, char **s, switch_size_t *len, switch_size_t *max, switch_size_t start, char ***attr, uint32_t * count) | |
1762 | { | |
1763 | int i, j; | |
1764 | char *txt = (char *)(xml->parent) ? xml->parent->txt : (char *)""; | |
1765 | switch_size_t off = 0; | |
1766 | uint32_t lcount = 0; | |
1767 | ||
1768 | // parent character content up to this tag | |
1769 | *s = switch_xml_ampencode(txt + start, xml->off - start, s, len, max, 0); | |
1770 | ||
1771 | while (*len + strlen(xml->name) + 5 + (strlen(XML_INDENT) * (*count)) + 1 > *max) { // reallocate s | |
1772 | char *tmp = (char *)realloc(*s, *max += SWITCH_XML_BUFSIZE); | |
1773 | if (!tmp) return *s; | |
1774 | *s = tmp; | |
1775 | } | |
1776 | ||
1777 | if (*(*s + (*len) - 1) == '>') { | |
1778 | *len += sprintf(*s + *len, "\n"); // indent | |
1779 | } | |
1780 | for (lcount = 0; lcount < *count; lcount++) { | |
1781 | *len += sprintf(*s + *len, "%s", XML_INDENT); // indent | |
1782 | } | |
1783 | ||
1784 | *len += sprintf(*s + *len, "<%s", xml->name); // open tag | |
1785 | for (i = 0; xml->attr[i]; i += 2) { // tag attributes | |
1786 | if (switch_xml_attr(xml, xml->attr[i]) != xml->attr[i + 1]) | |
1787 | continue; | |
1788 | while (*len + strlen(xml->attr[i]) + 7 + (strlen(XML_INDENT) * (*count)) > *max) { // reallocate s | |
1789 | char *tmp = (char *)realloc(*s, *max += SWITCH_XML_BUFSIZE); | |
1790 | if (!tmp) return *s; | |
1791 | *s = tmp; | |
1792 | } | |
1793 | ||
1794 | *len += sprintf(*s + *len, " %s=\"", xml->attr[i]); | |
1795 | switch_xml_ampencode(xml->attr[i + 1], 0, s, len, max, 1); | |
1796 | *len += sprintf(*s + *len, "\""); | |
1797 | } | |
1798 | ||
1799 | for (i = 0; attr[i] && strcmp(attr[i][0], xml->name); i++); | |
1800 | for (j = 1; attr[i] && attr[i][j]; j += 3) { // default attributes | |
1801 | if (!attr[i][j + 1] || switch_xml_attr(xml, attr[i][j]) != attr[i][j + 1]) | |
1802 | continue; // skip duplicates and non-values | |
1803 | while (*len + strlen(attr[i][j]) + 8 + (strlen(XML_INDENT) * (*count)) > *max) { // reallocate s | |
1804 | char *tmp = (char *)realloc(*s, *max += SWITCH_XML_BUFSIZE); | |
1805 | if (!tmp) return *s; | |
1806 | *s = tmp; | |
1807 | } | |
1808 | ||
1809 | *len += sprintf(*s + *len, " %s=\"", attr[i][j]); | |
1810 | switch_xml_ampencode(attr[i][j + 1], 0, s, len, max, 1); | |
1811 | *len += sprintf(*s + *len, "\""); | |
1812 | } | |
1813 | ||
1814 | *len += sprintf(*s + *len, (xml->child || xml->txt) ? ">" : "/>\n"); | |
1815 | ||
1816 | if (xml->child) { | |
1817 | (*count)++; | |
1818 | *s = switch_xml_toxml_r(xml->child, s, len, max, 0, attr, count); | |
1819 | ||
1820 | } else { | |
1821 | *s = switch_xml_ampencode(xml->txt, 0, s, len, max, 0); //data | |
1822 | } | |
1823 | ||
1824 | while (*len + strlen(xml->name) + 5 + (strlen(XML_INDENT) * (*count)) > *max) { // reallocate s | |
1825 | char *tmp = (char *)realloc(*s, *max += SWITCH_XML_BUFSIZE); | |
1826 | if (!tmp) return *s; | |
1827 | *s = tmp; | |
1828 | } | |
1829 | ||
1830 | if (xml->child || xml->txt) { | |
1831 | if (*(*s + (*len) - 1) == '\n') { | |
1832 | for (lcount = 0; lcount < *count; lcount++) { | |
1833 | *len += sprintf(*s + *len, "%s", XML_INDENT); // indent | |
1834 | } | |
1835 | } | |
1836 | *len += sprintf(*s + (*len), "</%s>\n", xml->name); // close tag | |
1837 | } | |
1838 | ||
1839 | while (txt[off] && off < xml->off) | |
1840 | off++; // make sure off is within bounds | |
1841 | ||
1842 | if (xml->ordered) { | |
1843 | return switch_xml_toxml_r(xml->ordered, s, len, max, off, attr, count); | |
1844 | ||
1845 | } else { | |
1846 | if (*count > 0) | |
1847 | (*count)--; | |
1848 | return switch_xml_ampencode(txt + off, 0, s, len, max, 0); | |
1849 | } | |
1850 | } | |
1851 | ||
1852 | SWITCH_DECLARE(char *) switch_xml_toxml(switch_xml_t xml, switch_bool_t prn_header) | |
1853 | { | |
1854 | char *s; | |
1855 | s = (char *)malloc(SWITCH_XML_BUFSIZE); | |
1856 | return switch_xml_toxml_buf(xml, s, SWITCH_XML_BUFSIZE, 0, prn_header); | |
1857 | } | |
1858 | ||
1859 | // converts an switch_xml structure back to xml, returning a string of xml date that | |
1860 | // must be freed | |
1861 | SWITCH_DECLARE(char *) switch_xml_toxml_buf(switch_xml_t xml, char *buf, switch_size_t buflen, switch_size_t offset, switch_bool_t prn_header) | |
1862 | { | |
1863 | switch_xml_t p = (xml) ? xml->parent : NULL, o = (xml) ? xml->ordered : NULL; | |
1864 | switch_xml_root_t root = (switch_xml_root_t) xml; | |
1865 | switch_size_t len = 0, max = buflen; | |
1866 | char *s, *t, *n, *r; | |
1867 | int i, j, k; | |
1868 | uint32_t count = 0; | |
1869 | ||
1870 | s = buf; | |
1871 | assert(s != NULL); | |
1872 | memset(s, 0, max); | |
1873 | len += offset; | |
1874 | if (prn_header) { | |
1875 | len += sprintf(s + len, "<?xml version=\"1.0\"?>\n"); | |
1876 | } | |
1877 | ||
1878 | if (!xml || !xml->name) { | |
1879 | if (!(r = (char *)realloc(s, len + 1))) { | |
1880 | abort(); | |
1881 | } | |
1882 | return r; | |
1883 | } | |
1884 | ||
1885 | while (root->xml.parent) { | |
1886 | root = (switch_xml_root_t) root->xml.parent; // root tag | |
1887 | } | |
1888 | ||
1889 | for (i = 0; !p && root->pi[i]; i++) { // pre-root processing instructions | |
1890 | for (k = 2; root->pi[i][k - 1]; k++); | |
1891 | for (j = 1; (n = root->pi[i][j]); j++) { | |
1892 | if (root->pi[i][k][j - 1] == '>') { | |
1893 | continue; // not pre-root | |
1894 | } | |
1895 | while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max) { | |
1896 | if (!(r = (char *)realloc(s, max += SWITCH_XML_BUFSIZE))) { | |
1897 | abort(); | |
1898 | } | |
1899 | s = r; | |
1900 | } | |
1901 | len += sprintf(s + len, "<?%s%s%s?>", t, *n ? " " : "", n); | |
1902 | } | |
1903 | } | |
1904 | ||
1905 | xml->parent = xml->ordered = NULL; | |
1906 | s = switch_xml_toxml_r(xml, &s, &len, &max, 0, root->attr, &count); | |
1907 | xml->parent = p; | |
1908 | xml->ordered = o; | |
1909 | ||
1910 | for (i = 0; !p && root->pi[i]; i++) { // post-root processing instructions | |
1911 | for (k = 2; root->pi[i][k - 1]; k++); | |
1912 | for (j = 1; (n = root->pi[i][j]); j++) { | |
1913 | if (root->pi[i][k][j - 1] == '<') { | |
1914 | continue; // not post-root | |
1915 | } | |
1916 | while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max) { | |
1917 | if (!(r = (char *)realloc(s, max += SWITCH_XML_BUFSIZE))) { | |
1918 | abort(); | |
1919 | } | |
1920 | s = r; | |
1921 | } | |
1922 | len += sprintf(s + len, "\n<?%s%s%s?>", t, *n ? " " : "", n); | |
1923 | } | |
1924 | } | |
1925 | ||
1926 | if (!(r = (char *)realloc(s, len + 1))) { | |
1927 | abort(); | |
1928 | } | |
1929 | ||
1930 | return r; | |
1931 | } | |
1932 | ||
1933 | // free the memory allocated for the switch_xml structure | |
1934 | SWITCH_DECLARE(void) switch_xml_free(switch_xml_t xml) | |
1935 | { | |
1936 | switch_xml_root_t root = (switch_xml_root_t) xml; | |
1937 | int i, j; | |
1938 | char **a, *s; | |
1939 | ||
1940 | ||
f6f7f31c | 1941 | if (!xml) { |
59241895 | 1942 | return; |
59241895 MJ |
1943 | } |
1944 | ||
1945 | if (xml == MAIN_XML_ROOT) { | |
f6f7f31c | 1946 | switch_thread_rwlock_unlock(RWLOCK); |
59241895 MJ |
1947 | return; |
1948 | } | |
1949 | ||
1950 | if (xml->free_path) { | |
1951 | if (!switch_stristr("freeswitch.xml.fsxml", xml->free_path)) { | |
1952 | if (unlink(xml->free_path) != 0) { | |
b9e0bd8f | 1953 | switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to delete file [%s]\n", xml->free_path); |
59241895 MJ |
1954 | } |
1955 | } | |
1956 | switch_safe_free(xml->free_path); | |
1957 | } | |
1958 | ||
1959 | switch_xml_free(xml->child); | |
1960 | switch_xml_free(xml->ordered); | |
1961 | ||
1962 | if (!xml->parent) { // free root tag allocations | |
ff3f04f1 | 1963 | for (i = 10; root->ent[i]; i += 2) // 0 - 9 are default entities (<>&"') |
59241895 MJ |
1964 | if ((s = root->ent[i + 1]) < root->s || s > root->e) |
1965 | free(s); | |
1966 | free(root->ent); // free list of general entities | |
1967 | ||
1968 | for (i = 0; (a = root->attr[i]); i++) { | |
1969 | for (j = 1; a[j++]; j += 2) // free malloced attribute values | |
1970 | if (a[j] && (a[j] < root->s || a[j] > root->e)) | |
1971 | free(a[j]); | |
1972 | free(a); | |
1973 | } | |
1974 | if (root->attr[0]) | |
1975 | free(root->attr); // free default attribute list | |
1976 | ||
1977 | for (i = 0; root->pi[i]; i++) { | |
1978 | for (j = 1; root->pi[i][j]; j++); | |
1979 | free(root->pi[i][j + 1]); | |
1980 | free(root->pi[i]); | |
1981 | } | |
1982 | if (root->pi[0]) | |
1983 | free(root->pi); // free processing instructions | |
1984 | ||
1985 | if (root->dynamic == 1) | |
1986 | free(root->m); // malloced xml data | |
1987 | #ifdef HAVE_MMAP | |
1988 | else if (root->len) | |
1989 | munmap(root->m, root->len); // mem mapped xml data | |
1990 | #endif // HAVE_MMAP | |
1991 | if (root->u) | |
1992 | free(root->u); // utf8 conversion | |
1993 | } | |
1994 | ||
1995 | switch_xml_free_attr(xml->attr); // tag attributes | |
1996 | if ((xml->flags & SWITCH_XML_TXTM)) | |
1997 | free(xml->txt); // character content | |
1998 | if ((xml->flags & SWITCH_XML_NAMEM)) | |
1999 | free(xml->name); // tag name | |
2000 | free(xml); | |
2001 | } | |
2002 | ||
2003 | // return parser error message or empty string if none | |
2004 | SWITCH_DECLARE(const char *) switch_xml_error(switch_xml_t xml) | |
2005 | { | |
2006 | while (xml && xml->parent) | |
2007 | xml = xml->parent; // find root tag | |
2008 | return (xml) ? ((switch_xml_root_t) xml)->err : ""; | |
2009 | } | |
2010 | ||
2011 | // returns a new empty switch_xml structure with the given root tag name | |
2012 | SWITCH_DECLARE(switch_xml_t) switch_xml_new(const char *name) | |
2013 | { | |
2014 | static const char *ent[] = { "lt;", "<", "gt;", ">", "quot;", """, | |
2015 | "apos;", "'", "amp;", "&", NULL | |
2016 | }; | |
2017 | switch_xml_root_t root = (switch_xml_root_t) memset(malloc(sizeof(struct switch_xml_root)), | |
2018 | '\0', sizeof(struct switch_xml_root)); | |
2019 | root->xml.name = (char *) name; | |
2020 | root->cur = &root->xml; | |
2021 | strcpy(root->err, root->xml.txt = (char *)""); | |
2022 | root->ent = (char **)memcpy(malloc(sizeof(ent)), ent, sizeof(ent)); | |
2023 | root->attr = root->pi = (char ***) (root->xml.attr = SWITCH_XML_NIL); | |
2024 | return &root->xml; | |
2025 | } | |
2026 | ||
2027 | // inserts an existing tag into an switch_xml structure | |
2028 | SWITCH_DECLARE(switch_xml_t) switch_xml_insert(switch_xml_t xml, switch_xml_t dest, switch_size_t off) | |
2029 | { | |
2030 | switch_xml_t cur, prev, head; | |
2031 | ||
2032 | xml->next = xml->sibling = xml->ordered = NULL; | |
2033 | xml->off = off; | |
2034 | xml->parent = dest; | |
2035 | ||
2036 | if ((head = dest->child)) { // already have sub tags | |
2037 | if (head->off <= off) { // not first subtag | |
2038 | for (cur = head; cur->ordered && cur->ordered->off <= off; cur = cur->ordered); | |
2039 | xml->ordered = cur->ordered; | |
2040 | cur->ordered = xml; | |
2041 | } else { // first subtag | |
2042 | xml->ordered = head; | |
2043 | dest->child = xml; | |
2044 | } | |
2045 | ||
2046 | for (cur = head, prev = NULL; cur && strcmp(cur->name, xml->name); prev = cur, cur = cur->sibling); // find tag type | |
2047 | if (cur && cur->off <= off) { // not first of type | |
2048 | while (cur->next && cur->next->off <= off) | |
2049 | cur = cur->next; | |
2050 | xml->next = cur->next; | |
2051 | cur->next = xml; | |
2052 | } else { // first tag of this type | |
2053 | if (prev && cur) | |
2054 | prev->sibling = cur->sibling; // remove old first | |
2055 | xml->next = cur; // old first tag is now next | |
2056 | for (cur = head, prev = NULL; cur && cur->off <= off; prev = cur, cur = cur->sibling); // new sibling insert point | |
2057 | xml->sibling = cur; | |
2058 | if (prev) | |
2059 | prev->sibling = xml; | |
2060 | } | |
2061 | } else | |
2062 | dest->child = xml; // only sub tag | |
2063 | ||
2064 | return xml; | |
2065 | } | |
2066 | ||
2067 | // Adds a child tag. off is the offset of the child tag relative to the start | |
2068 | // of the parent tag's character content. Returns the child tag | |
2069 | SWITCH_DECLARE(switch_xml_t) switch_xml_add_child(switch_xml_t xml, const char *name, switch_size_t off) | |
2070 | { | |
2071 | switch_xml_t child; | |
2072 | ||
2073 | if (!xml) | |
2074 | return NULL; | |
2075 | child = (switch_xml_t) memset(malloc(sizeof(struct switch_xml)), '\0', sizeof(struct switch_xml)); | |
2076 | child->name = (char *) name; | |
2077 | child->attr = SWITCH_XML_NIL; | |
2078 | child->off = off; | |
2079 | child->parent = xml; | |
2080 | child->txt = (char *)""; | |
2081 | ||
2082 | return switch_xml_insert(child, xml, off); | |
2083 | } | |
2084 | ||
2085 | // sets the character content for the given tag and returns the tag | |
2086 | SWITCH_DECLARE(switch_xml_t) switch_xml_set_txt(switch_xml_t xml, const char *txt) | |
2087 | { | |
2088 | if (!xml) | |
2089 | return NULL; | |
2090 | if (xml->flags & SWITCH_XML_TXTM) | |
2091 | free(xml->txt); // existing txt was malloced | |
2092 | xml->flags &= ~SWITCH_XML_TXTM; | |
2093 | xml->txt = (char *) txt; | |
2094 | return xml; | |
2095 | } | |
2096 | ||
2097 | // Sets the given tag attribute or adds a new attribute if not found. A value | |
2098 | // of NULL will remove the specified attribute. Returns the tag given | |
2099 | SWITCH_DECLARE(switch_xml_t) switch_xml_set_attr(switch_xml_t xml, const char *name, const char *value) | |
2100 | { | |
2101 | int l = 0, c; | |
2102 | ||
2103 | if (!xml) | |
2104 | return NULL; | |
2105 | while (xml->attr[l] && strcmp(xml->attr[l], name)) | |
2106 | l += 2; | |
2107 | if (!xml->attr[l]) { // not found, add as new attribute | |
2108 | if (!value) | |
2109 | return xml; // nothing to do | |
2110 | if (xml->attr == SWITCH_XML_NIL) { // first attribute | |
2111 | xml->attr = (char **)malloc(4 * sizeof(char *)); | |
2112 | if (!xml->attr) return NULL; | |
2113 | xml->attr[1] = strdup(""); // empty list of malloced names/vals | |
2114 | } else { | |
2115 | char **tmp = (char **)realloc(xml->attr, (l + 4) * sizeof(char *)); | |
2116 | if (!tmp) return xml; | |
2117 | xml->attr = tmp; | |
2118 | } | |
2119 | ||
2120 | xml->attr[l] = (char *) name; // set attribute name | |
2121 | xml->attr[l + 2] = NULL; // null terminate attribute list | |
2122 | xml->attr[l + 3] = (char *)realloc(xml->attr[l + 1], (c = (int) strlen(xml->attr[l + 1])) + 2); | |
2123 | strcpy(xml->attr[l + 3] + c, " "); // set name/value as not malloced | |
2124 | if (xml->flags & SWITCH_XML_DUP) | |
2125 | xml->attr[l + 3][c] = SWITCH_XML_NAMEM; | |
2126 | } else if (xml->flags & SWITCH_XML_DUP) | |
2127 | free((char *) name); // name was strduped | |
2128 | ||
2129 | for (c = l; xml->attr[c]; c += 2); // find end of attribute list | |
2130 | if (xml->attr[c + 1][l / 2] & SWITCH_XML_TXTM) | |
2131 | free(xml->attr[l + 1]); //old val | |
2132 | if (xml->flags & SWITCH_XML_DUP) | |
2133 | xml->attr[c + 1][l / 2] |= SWITCH_XML_TXTM; | |
2134 | else | |
2135 | xml->attr[c + 1][l / 2] &= ~SWITCH_XML_TXTM; | |
2136 | ||
2137 | if (value) | |
2138 | xml->attr[l + 1] = (char *) value; // set attribute value | |
2139 | else { // remove attribute | |
2140 | char **tmp; | |
2141 | if (xml->attr[c + 1][l / 2] & SWITCH_XML_NAMEM) | |
2142 | free(xml->attr[l]); | |
2143 | memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char *)); | |
2144 | tmp =(char **)realloc(xml->attr, (c + 2) * sizeof(char *)); | |
2145 | if (!tmp) return xml; | |
2146 | xml->attr = tmp; | |
2147 | memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1, (c / 2) - (l / 2)); // fix list of which name/vals are malloced | |
2148 | } | |
2149 | xml->flags &= ~SWITCH_XML_DUP; // clear strdup() flag | |
2150 | ||
2151 | return xml; | |
2152 | } | |
2153 | ||
2154 | // sets a flag for the given tag and returns the tag | |
2155 | SWITCH_DECLARE(switch_xml_t) switch_xml_set_flag(switch_xml_t xml, switch_xml_flag_t flag) | |
2156 | { | |
2157 | if (xml) | |
2158 | xml->flags |= flag; | |
2159 | return xml; | |
2160 | } | |
2161 | ||
2162 | // removes a tag along with its subtags without freeing its memory | |
2163 | SWITCH_DECLARE(switch_xml_t) switch_xml_cut(switch_xml_t xml) | |
2164 | { | |
2165 | switch_xml_t cur; | |
2166 | ||
2167 | if (!xml) | |
2168 | return NULL; // nothing to do | |
2169 | if (xml->next) | |
2170 | xml->next->sibling = xml->sibling; // patch sibling list | |
2171 | ||
2172 | if (xml->parent) { // not root tag | |
2173 | cur = xml->parent->child; // find head of subtag list | |
2174 | if (cur == xml) | |
2175 | xml->parent->child = xml->ordered; // first subtag | |
2176 | else { // not first subtag | |
2177 | while (cur->ordered != xml) | |
2178 | cur = cur->ordered; | |
2179 | cur->ordered = cur->ordered->ordered; // patch ordered list | |
2180 | ||
2181 | cur = xml->parent->child; // go back to head of subtag list | |
2182 | if (strcmp(cur->name, xml->name)) { // not in first sibling list | |
2183 | while (strcmp(cur->sibling->name, xml->name)) | |
2184 | cur = cur->sibling; | |
2185 | if (cur->sibling == xml) { // first of a sibling list | |
2186 | cur->sibling = (xml->next) ? xml->next : cur->sibling->sibling; | |
2187 | } else | |
2188 | cur = cur->sibling; // not first of a sibling list | |
2189 | } | |
2190 | ||
2191 | while (cur->next && cur->next != xml) | |
2192 | cur = cur->next; | |
2193 | if (cur->next) | |
2194 | cur->next = cur->next->next; // patch next list | |
2195 | } | |
2196 | } | |
2197 | xml->ordered = xml->sibling = xml->next = NULL; // prevent switch_xml_free() from clobbering ordered list | |
2198 | return xml; | |
2199 | } | |
2200 | ||
2201 | #ifdef WIN32 | |
2202 | /* | |
2203 | * globbing functions for windows, part of libc on unix, this code was cut and paste from | |
2204 | * freebsd lib and distilled a bit to work with windows | |
2205 | */ | |
2206 | ||
2207 | /* | |
2208 | * Copyright (c) 1989, 1993 | |
2209 | * The Regents of the University of California. All rights reserved. | |
2210 | * | |
2211 | * This code is derived from software contributed to Berkeley by | |
2212 | * Guido van Rossum. | |
2213 | * | |
2214 | * Redistribution and use in source and binary forms, with or without | |
2215 | * modification, are permitted provided that the following conditions | |
2216 | * are met: | |
2217 | * 1. Redistributions of source code must retain the above copyright | |
2218 | * notice, this list of conditions and the following disclaimer. | |
2219 | * 2. Redistributions in binary form must reproduce the above copyright | |
2220 | * notice, this list of conditions and the following disclaimer in the | |
2221 | * documentation and/or other materials provided with the distribution. | |
2222 | * 4. Neither the name of the University nor the names of its contributors | |
2223 | * may be used to endorse or promote products derived from this software | |
2224 | * without specific prior written permission. | |
2225 | * | |
2226 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
2227 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
2228 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
2229 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
2230 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
2231 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
2232 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
2233 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
2234 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
2235 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
2236 | * SUCH DAMAGE. | |
2237 | */ | |
2238 | ||
2239 | #define DOLLAR '$' | |
2240 | #define DOT '.' | |
2241 | #define EOS '\0' | |
2242 | #define LBRACKET '[' | |
2243 | #define NOT '!' | |
2244 | #define QUESTION '?' | |
2245 | #define RANGE '-' | |
2246 | #define RBRACKET ']' | |
2247 | #define SEP '/' | |
2248 | #define WIN_SEP '\\' | |
2249 | #define STAR '*' | |
2250 | #define TILDE '~' | |
2251 | #define UNDERSCORE '_' | |
2252 | #define LBRACE '{' | |
2253 | #define RBRACE '}' | |
2254 | #define SLASH '/' | |
2255 | #define COMMA ',' | |
2256 | ||
2257 | #define M_QUOTE (char)0x80 | |
2258 | #define M_PROTECT (char)0x40 | |
2259 | #define M_MASK (char)0xff | |
2260 | #define M_ASCII (char)0x7f | |
2261 | ||
2262 | #define CHAR(c) ((char)((c)&M_ASCII)) | |
2263 | #define META(c) ((char)((c)|M_QUOTE)) | |
2264 | #define M_ALL META('*') | |
2265 | #define M_END META(']') | |
2266 | #define M_NOT META('!') | |
2267 | #define M_ONE META('?') | |
2268 | #define M_RNG META('-') | |
2269 | #define M_SET META('[') | |
2270 | #define ismeta(c) (((c)&M_QUOTE) != 0) | |
2271 | ||
2272 | #ifndef MAXPATHLEN | |
2273 | #define MAXPATHLEN 256 | |
2274 | #endif | |
2275 | ||
2276 | static int compare(const void *, const void *); | |
5d4b5c3d RJ |
2277 | static int glob0(const char *, glob_t *, size_t *); |
2278 | static int glob1(char *, glob_t *, size_t *); | |
2279 | static int glob2(char *, char *, char *, char *, glob_t *, size_t *); | |
2280 | static int glob3(char *, char *, char *, char *, char *, glob_t *, size_t *); | |
2281 | static int globextend(const char *, glob_t *, size_t *); | |
59241895 MJ |
2282 | static int match(char *, char *, char *); |
2283 | ||
2284 | #pragma warning(push) | |
2285 | #pragma warning(disable:4310) | |
2286 | ||
2287 | int glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob) | |
2288 | { | |
2289 | const unsigned char *patnext; | |
5d4b5c3d | 2290 | size_t limit; |
59241895 MJ |
2291 | char c; |
2292 | char *bufnext, *bufend, patbuf[MAXPATHLEN]; | |
2293 | ||
2294 | patnext = (unsigned char *) pattern; | |
2295 | if (!(flags & GLOB_APPEND)) { | |
2296 | pglob->gl_pathc = 0; | |
2297 | pglob->gl_pathv = NULL; | |
2298 | if (!(flags & GLOB_DOOFFS)) | |
2299 | pglob->gl_offs = 0; | |
2300 | } | |
2301 | if (flags & GLOB_LIMIT) { | |
2302 | limit = pglob->gl_matchc; | |
2303 | if (limit == 0) | |
2304 | limit = 9999999; | |
2305 | } else | |
2306 | limit = 0; | |
2307 | pglob->gl_flags = flags & ~GLOB_MAGCHAR; | |
2308 | pglob->gl_errfunc = errfunc; | |
2309 | pglob->gl_matchc = 0; | |
2310 | ||
2311 | bufnext = patbuf; | |
2312 | bufend = bufnext + MAXPATHLEN - 1; | |
2313 | while (bufnext < bufend && (c = *patnext++) != EOS) | |
2314 | *bufnext++ = c; | |
2315 | *bufnext = EOS; | |
2316 | ||
2317 | return glob0(patbuf, pglob, &limit); | |
2318 | } | |
2319 | ||
2320 | /* | |
2321 | * The main glob() routine: compiles the pattern (optionally processing | |
2322 | * quotes), calls glob1() to do the real pattern matching, and finally | |
2323 | * sorts the list (unless unsorted operation is requested). Returns 0 | |
2324 | * if things went well, nonzero if errors occurred. | |
2325 | */ | |
5d4b5c3d | 2326 | static int glob0(const char *pattern, glob_t *pglob, size_t *limit) |
59241895 MJ |
2327 | { |
2328 | const char *qpatnext; | |
2329 | int c, err; | |
5d4b5c3d | 2330 | size_t oldpathc; |
59241895 MJ |
2331 | char *bufnext, patbuf[MAXPATHLEN]; |
2332 | ||
2333 | qpatnext = pattern; | |
2334 | oldpathc = pglob->gl_pathc; | |
2335 | bufnext = patbuf; | |
2336 | ||
2337 | /* We don't need to check for buffer overflow any more. */ | |
2338 | while ((c = *qpatnext++) != EOS) { | |
2339 | switch (c) { | |
2340 | case SEP: | |
2341 | *bufnext++ = WIN_SEP; | |
2342 | break; | |
2343 | case LBRACKET: | |
2344 | c = *qpatnext; | |
2345 | if (c == NOT) | |
2346 | ++qpatnext; | |
2347 | if (*qpatnext == EOS || | |
2348 | strchr((char *) qpatnext+1, RBRACKET) == NULL) { | |
2349 | *bufnext++ = LBRACKET; | |
2350 | if (c == NOT) | |
2351 | --qpatnext; | |
2352 | break; | |
2353 | } | |
2354 | *bufnext++ = M_SET; | |
2355 | if (c == NOT) | |
2356 | *bufnext++ = M_NOT; | |
2357 | c = *qpatnext++; | |
2358 | do { | |
2359 | *bufnext++ = CHAR(c); | |
2360 | if (*qpatnext == RANGE && | |
2361 | (c = qpatnext[1]) != RBRACKET) { | |
2362 | *bufnext++ = M_RNG; | |
2363 | *bufnext++ = CHAR(c); | |
2364 | qpatnext += 2; | |
2365 | } | |
2366 | } while ((c = *qpatnext++) != RBRACKET); | |
2367 | pglob->gl_flags |= GLOB_MAGCHAR; | |
2368 | *bufnext++ = M_END; | |
2369 | break; | |
2370 | case QUESTION: | |
2371 | pglob->gl_flags |= GLOB_MAGCHAR; | |
2372 | *bufnext++ = M_ONE; | |
2373 | break; | |
2374 | case STAR: | |
2375 | pglob->gl_flags |= GLOB_MAGCHAR; | |
2376 | /* collapse adjacent stars to one, | |
2377 | * to avoid exponential behavior | |
2378 | */ | |
2379 | if (bufnext == patbuf || bufnext[-1] != M_ALL) | |
2380 | *bufnext++ = M_ALL; | |
2381 | break; | |
2382 | default: | |
2383 | *bufnext++ = CHAR(c); | |
2384 | break; | |
2385 | } | |
2386 | } | |
2387 | *bufnext = EOS; | |
2388 | ||
2389 | if ((err = glob1(patbuf, pglob, limit)) != 0) | |
2390 | return(err); | |
2391 | ||
2392 | /* | |
2393 | * If there was no match we are going to append the pattern | |
2394 | * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified | |
2395 | * and the pattern did not contain any magic characters | |
2396 | * GLOB_NOMAGIC is there just for compatibility with csh. | |
2397 | */ | |
2398 | if (pglob->gl_pathc == oldpathc) { | |
2399 | if (((pglob->gl_flags & GLOB_NOCHECK) || | |
2400 | ((pglob->gl_flags & GLOB_NOMAGIC) && | |
2401 | !(pglob->gl_flags & GLOB_MAGCHAR)))) | |
2402 | return(globextend(pattern, pglob, limit)); | |
2403 | else | |
2404 | return(GLOB_NOMATCH); | |
2405 | } | |
2406 | if (!(pglob->gl_flags & GLOB_NOSORT)) | |
2407 | qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, | |
2408 | pglob->gl_pathc - oldpathc, sizeof(char *), compare); | |
2409 | return(0); | |
2410 | } | |
2411 | ||
2412 | static int compare(const void *p, const void *q) | |
2413 | { | |
2414 | return(strcmp(*(char **)p, *(char **)q)); | |
2415 | } | |
2416 | ||
5d4b5c3d | 2417 | static int glob1(char *pattern, glob_t *pglob, size_t *limit) |
59241895 MJ |
2418 | { |
2419 | char pathbuf[MAXPATHLEN]; | |
2420 | ||
2421 | /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ | |
2422 | if (*pattern == EOS) | |
2423 | return(0); | |
2424 | return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1, | |
2425 | pattern, pglob, limit)); | |
2426 | } | |
2427 | ||
2428 | /* | |
2429 | * The functions glob2 and glob3 are mutually recursive; there is one level | |
2430 | * of recursion for each segment in the pattern that contains one or more | |
2431 | * meta characters. | |
2432 | */ | |
5d4b5c3d | 2433 | static int glob2(char *pathbuf, char *pathend, char *pathend_last, char *pattern, glob_t *pglob, size_t *limit) |
59241895 MJ |
2434 | { |
2435 | struct stat sb; | |
2436 | char *p, *q; | |
2437 | int anymeta; | |
2438 | ||
2439 | /* | |
2440 | * Loop over pattern segments until end of pattern or until | |
2441 | * segment with meta character found. | |
2442 | */ | |
2443 | for (anymeta = 0;;) { | |
2444 | if (*pattern == EOS) { /* End of pattern? */ | |
2445 | *pathend = EOS; | |
2446 | if (stat(pathbuf, &sb)) | |
2447 | return(0); | |
2448 | ||
2449 | if (((pglob->gl_flags & GLOB_MARK) && pathend[-1] != SEP && pathend[-1] != WIN_SEP) && (_S_IFDIR & sb.st_mode) ) { | |
2450 | if (pathend + 1 > pathend_last) | |
2451 | return (GLOB_ABORTED); | |
2452 | *pathend++ = WIN_SEP; | |
2453 | *pathend = EOS; | |
2454 | } | |
2455 | ++pglob->gl_matchc; | |
2456 | return(globextend(pathbuf, pglob, limit)); | |
2457 | } | |
2458 | ||
2459 | /* Find end of next segment, copy tentatively to pathend. */ | |
2460 | q = pathend; | |
2461 | p = pattern; | |
2462 | while (*p != EOS && *p != SEP && *p != WIN_SEP) { | |
2463 | if (ismeta(*p)) | |
2464 | anymeta = 1; | |
2465 | if (q + 1 > pathend_last) | |
2466 | return (GLOB_ABORTED); | |
2467 | *q++ = *p++; | |
2468 | } | |
2469 | ||
2470 | if (!anymeta) { /* No expansion, do next segment. */ | |
2471 | pathend = q; | |
2472 | pattern = p; | |
2473 | while (*pattern == SEP || *pattern == WIN_SEP) { | |
2474 | if (pathend + 1 > pathend_last) | |
2475 | return (GLOB_ABORTED); | |
2476 | *pathend++ = *pattern++; | |
2477 | } | |
2478 | } else /* Need expansion, recurse. */ | |
2479 | return(glob3(pathbuf, pathend, pathend_last, pattern, p, | |
2480 | pglob, limit)); | |
2481 | } | |
2482 | /* NOTREACHED */ | |
2483 | } | |
2484 | ||
5d4b5c3d | 2485 | static int glob3(char *pathbuf, char *pathend, char *pathend_last, char *pattern, char *restpattern, glob_t *pglob, size_t *limit) |
59241895 MJ |
2486 | { |
2487 | int err; | |
2488 | apr_dir_t * dirp; | |
2489 | apr_pool_t * pool; | |
2490 | ||
2491 | apr_pool_create(&pool, NULL); | |
2492 | ||
2493 | if (pathend > pathend_last) | |
2494 | return (GLOB_ABORTED); | |
2495 | *pathend = EOS; | |
2496 | errno = 0; | |
2497 | ||
d0688264 | 2498 | if (apr_dir_open (&dirp, pathbuf, pool) != APR_SUCCESS) { |
59241895 MJ |
2499 | /* TODO: don't call for ENOENT or ENOTDIR? */ |
2500 | apr_pool_destroy(pool); | |
2501 | if (pglob->gl_errfunc) { | |
2502 | if (pglob->gl_errfunc(pathbuf, errno) || | |
2503 | pglob->gl_flags & GLOB_ERR) | |
2504 | return (GLOB_ABORTED); | |
2505 | } | |
2506 | return(0); | |
2507 | } | |
2508 | ||
2509 | err = 0; | |
2510 | ||
2511 | /* Search directory for matching names. */ | |
2512 | while (dirp) | |
2513 | { | |
2514 | apr_finfo_t dp; | |
2515 | unsigned char *sc; | |
2516 | char *dc; | |
2517 | ||
d0688264 | 2518 | if (apr_dir_read(&dp, APR_FINFO_NAME, dirp) != APR_SUCCESS) |
59241895 | 2519 | break; |
d0688264 | 2520 | if (!(dp.valid & APR_FINFO_NAME) || !(dp.name) || !strlen(dp.name)) |
59241895 MJ |
2521 | break; |
2522 | ||
2523 | /* Initial DOT must be matched literally. */ | |
2524 | if (dp.name[0] == DOT && *pattern != DOT) | |
2525 | continue; | |
2526 | dc = pathend; | |
2527 | sc = (unsigned char *) dp.name; | |
2528 | ||
2529 | while (dc < pathend_last && (*dc++ = *sc++) != EOS); | |
2530 | ||
2531 | if (!match(pathend, pattern, restpattern)) { | |
2532 | *pathend = EOS; | |
2533 | continue; | |
2534 | } | |
2535 | err = glob2(pathbuf, --dc, pathend_last, restpattern, | |
2536 | pglob, limit); | |
2537 | if (err) | |
2538 | break; | |
2539 | } | |
2540 | ||
d0688264 | 2541 | if (dirp) |
59241895 MJ |
2542 | apr_dir_close (dirp); |
2543 | apr_pool_destroy(pool); | |
2544 | return(err); | |
2545 | } | |
2546 | ||
2547 | ||
2548 | /* | |
ff3f04f1 | 2549 | * Extend the gl_pathv member of a glob_t structure to accommodate a new item, |
59241895 MJ |
2550 | * add the new item, and update gl_pathc. |
2551 | * | |
2552 | * This assumes the BSD realloc, which only copies the block when its size | |
2553 | * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic | |
2554 | * behavior. | |
2555 | * | |
2556 | * Return 0 if new item added, error code if memory couldn't be allocated. | |
2557 | * | |
2558 | * Invariant of the glob_t structure: | |
2559 | * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and | |
2560 | * gl_pathv points to (gl_offs + gl_pathc + 1) items. | |
2561 | */ | |
5d4b5c3d | 2562 | static int globextend(const char *path, glob_t *pglob, size_t *limit) |
59241895 MJ |
2563 | { |
2564 | char **pathv; | |
2565 | char * copy; | |
5d4b5c3d RJ |
2566 | size_t i; |
2567 | size_t newsize, len; | |
59241895 MJ |
2568 | const char *p; |
2569 | ||
1bbf3a82 | 2570 | if (*limit && pglob->gl_pathc > *limit) { |
59241895 MJ |
2571 | errno = 0; |
2572 | return (GLOB_NOSPACE); | |
2573 | } | |
2574 | ||
2575 | newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); | |
2576 | pathv = pglob->gl_pathv ? | |
2577 | realloc((char *)pglob->gl_pathv, newsize) : | |
2578 | malloc(newsize); | |
2579 | if (pathv == NULL) { | |
2580 | if (pglob->gl_pathv) { | |
2581 | free(pglob->gl_pathv); | |
2582 | pglob->gl_pathv = NULL; | |
2583 | } | |
2584 | return(GLOB_NOSPACE); | |
2585 | } | |
2586 | ||
2587 | if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { | |
2588 | /* first time around -- clear initial gl_offs items */ | |
2589 | pathv += pglob->gl_offs; | |
1bbf3a82 | 2590 | for (i = pglob->gl_offs; i-- > 0; ) |
59241895 MJ |
2591 | *--pathv = NULL; |
2592 | } | |
2593 | pglob->gl_pathv = pathv; | |
2594 | ||
2595 | for (p = path; *p++;) | |
2596 | continue; | |
2597 | len = (size_t)(p - path); | |
2598 | if ((copy = malloc(len)) != NULL) { | |
2599 | memcpy(copy, path, len); | |
2600 | pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; | |
2601 | } | |
2602 | pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; | |
2603 | return(copy == NULL ? GLOB_NOSPACE : 0); | |
2604 | } | |
2605 | ||
2606 | /* | |
2607 | * pattern matching function for filenames. Each occurrence of the * | |
2608 | * pattern causes a recursion level. | |
2609 | */ | |
2610 | static int match(char *name, char *pat, char *patend) | |
2611 | { | |
2612 | int ok, negate_range; | |
2613 | char c, k; | |
2614 | char s1[6]; | |
2615 | ||
2616 | while (pat < patend) { | |
2617 | c = *pat++; | |
2618 | switch (c & M_MASK) { | |
2619 | case M_ALL: | |
2620 | if (pat == patend) | |
2621 | return(1); | |
2622 | do | |
2623 | if (match(name, pat, patend)) | |
2624 | return(1); | |
2625 | while (*name++ != EOS); | |
2626 | return(0); | |
2627 | case M_ONE: | |
2628 | if (*name++ == EOS) | |
2629 | return(0); | |
2630 | break; | |
2631 | case M_SET: | |
2632 | ok = 0; | |
2633 | if ((k = *name++) == EOS) | |
2634 | return(0); | |
2635 | if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) | |
2636 | ++pat; | |
2637 | while (((c = *pat++) & M_MASK) != M_END) | |
2638 | if ((*pat & M_MASK) == M_RNG) { | |
2639 | memset(s1, 0, sizeof(s1)); | |
2640 | s1[0] = c; | |
2641 | s1[2] = k; | |
2642 | s1[4] = pat[1]; | |
2643 | if (strcoll(&s1[0], &s1[2]) <= 0 && strcoll(&s1[2], &s1[4]) <= 0) | |
2644 | ok = 1; | |
2645 | pat += 2; | |
2646 | } else if (c == k) | |
2647 | ok = 1; | |
2648 | if (ok == negate_range) | |
2649 | return(0); | |
2650 | break; | |
2651 | default: | |
2652 | if (*name++ != c) | |
2653 | return(0); | |
2654 | break; | |
2655 | } | |
2656 | } | |
2657 | return(*name == EOS); | |
2658 | } | |
2659 | ||
2660 | /* Free allocated data belonging to a glob_t structure. */ | |
2661 | void globfree(glob_t *pglob) | |
2662 | { | |
5d4b5c3d | 2663 | size_t i; |
59241895 MJ |
2664 | char **pp; |
2665 | ||
2666 | if (pglob->gl_pathv != NULL) { | |
2667 | pp = pglob->gl_pathv + pglob->gl_offs; | |
2668 | for (i = pglob->gl_pathc; i--; ++pp) | |
2669 | if (*pp) | |
2670 | free(*pp); | |
2671 | free(pglob->gl_pathv); | |
2672 | pglob->gl_pathv = NULL; | |
2673 | } | |
2674 | } | |
2675 | #pragma warning(pop) | |
2676 | #endif | |
2677 | ||
2678 | /* For Emacs: | |
2679 | * Local Variables: | |
2680 | * mode:c | |
2681 | * indent-tabs-mode:t | |
2682 | * tab-width:4 | |
2683 | * c-basic-offset:4 | |
2684 | * End: | |
2685 | * For VIM: | |
2686 | * vim:set softtabstop=4 shiftwidth=4 tabstop=4: | |
2687 | */ |