2008-07-08 21:52:38 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
2011-01-05 16:08:55 +00:00
|
|
|
* Copyright (C) 2005-2011, Anthony Minessale II <anthm@freeswitch.org>
|
2008-07-08 21:52:38 +00:00
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2008-07-08 21:52:38 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2008-07-08 21:52:38 +00:00
|
|
|
* Simon Capper <skyjunky@sbcglobal.net>
|
2011-04-20 17:09:03 +00:00
|
|
|
* Marc Olivier Chouinard <mochouinard@moctel.com>
|
2008-07-08 21:52:38 +00:00
|
|
|
*
|
|
|
|
* switch_xml.c -- XML PARSER
|
|
|
|
*
|
|
|
|
* Derived from ezxml http://ezxml.sourceforge.net
|
|
|
|
* Original Copyright
|
|
|
|
*
|
|
|
|
* Copyright 2004, 2006 Aaron Voisine <aaron@voisine.org>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <switch.h>
|
|
|
|
#ifndef WIN32
|
2011-03-29 23:05:05 +00:00
|
|
|
#include <sys/wait.h>
|
2008-07-08 21:52:38 +00:00
|
|
|
#include <switch_private.h>
|
|
|
|
#include <glob.h>
|
|
|
|
#else /* we're on windoze :( */
|
|
|
|
/* glob functions at end of this file */
|
|
|
|
#include <apr_file_io.h>
|
|
|
|
|
|
|
|
typedef struct {
|
2010-02-06 03:38:24 +00:00
|
|
|
size_t gl_pathc; /* Count of total paths so far. */
|
|
|
|
size_t gl_matchc; /* Count of paths matching pattern. */
|
|
|
|
size_t gl_offs; /* Reserved at beginning of gl_pathv. */
|
|
|
|
int gl_flags; /* Copy of flags parameter to glob. */
|
|
|
|
char **gl_pathv; /* List of paths matching pattern. */
|
2008-07-08 21:52:38 +00:00
|
|
|
/* Copy of errfunc parameter to glob. */
|
2010-02-06 03:38:24 +00:00
|
|
|
int (*gl_errfunc) (const char *, int);
|
2008-07-08 21:52:38 +00:00
|
|
|
} glob_t;
|
|
|
|
|
|
|
|
/* Believed to have been introduced in 1003.2-1992 */
|
2009-03-05 04:04:41 +00:00
|
|
|
#define GLOB_APPEND 0x0001 /* Append to output from previous call. */
|
|
|
|
#define GLOB_DOOFFS 0x0002 /* Use gl_offs. */
|
|
|
|
#define GLOB_ERR 0x0004 /* Return on error. */
|
|
|
|
#define GLOB_MARK 0x0008 /* Append / to matching directories. */
|
2008-07-08 21:52:38 +00:00
|
|
|
#define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */
|
2009-03-05 04:04:41 +00:00
|
|
|
#define GLOB_NOSORT 0x0020 /* Don't sort. */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
/* Error values returned by glob(3) */
|
|
|
|
#define GLOB_NOSPACE (-1) /* Malloc call failed. */
|
|
|
|
#define GLOB_ABORTED (-2) /* Unignored error. */
|
|
|
|
#define GLOB_NOMATCH (-3) /* No match and GLOB_NOCHECK was not set. */
|
2009-03-05 04:04:41 +00:00
|
|
|
#define GLOB_NOSYS (-4) /* Obsolete: source comptability only. */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
#define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */
|
|
|
|
#define GLOB_MAGCHAR 0x0100 /* Pattern had globbing characters. */
|
|
|
|
#define GLOB_NOMAGIC 0x0200 /* GLOB_NOCHECK without magic chars (csh). */
|
2009-03-05 04:04:41 +00:00
|
|
|
#define GLOB_QUOTE 0x0400 /* Quote special chars with \. */
|
|
|
|
#define GLOB_LIMIT 0x1000 /* limit number of returned paths */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
int glob(const char *, int, int (*)(const char *, int), glob_t *);
|
2009-03-05 04:04:41 +00:00
|
|
|
void globfree(glob_t *);
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
#define SWITCH_XML_WS "\t\r\n " /* whitespace */
|
2010-02-06 03:38:24 +00:00
|
|
|
#define SWITCH_XML_ERRL 128 /* maximum error string length */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2011-12-16 04:36:20 +00:00
|
|
|
/* Use UTF-8 as the general encoding */
|
|
|
|
#define USE_UTF_8_ENCODING SWITCH_TRUE
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
static int preprocess(const char *cwd, const char *file, int write_fd, int rlevel);
|
|
|
|
|
|
|
|
typedef struct switch_xml_root *switch_xml_root_t;
|
2009-03-05 04:04:41 +00:00
|
|
|
struct switch_xml_root { /* additional data for the root tag */
|
|
|
|
struct switch_xml xml; /* is a super-struct built on top of switch_xml struct */
|
|
|
|
switch_xml_t cur; /* current xml tree insertion point */
|
|
|
|
char *m; /* original xml string */
|
2011-03-18 20:45:37 +00:00
|
|
|
switch_size_t len; /* length of allocated memory */
|
2009-03-16 19:24:12 +00:00
|
|
|
uint8_t dynamic; /* Free the original string when calling switch_xml_free */
|
2009-03-05 04:04:41 +00:00
|
|
|
char *u; /* UTF-8 conversion of string if original was UTF-16 */
|
|
|
|
char *s; /* start of work area */
|
|
|
|
char *e; /* end of work area */
|
|
|
|
char **ent; /* general entities (ampersand sequences) */
|
|
|
|
char ***attr; /* default attributes */
|
|
|
|
char ***pi; /* processing instructions */
|
|
|
|
short standalone; /* non-zero if <?xml standalone="yes"?> */
|
|
|
|
char err[SWITCH_XML_ERRL]; /* error string */
|
2008-07-08 21:52:38 +00:00
|
|
|
};
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
char *SWITCH_XML_NIL[] = { NULL }; /* empty, null terminated array of strings */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
struct switch_xml_binding {
|
|
|
|
switch_xml_search_function_t function;
|
|
|
|
switch_xml_section_t sections;
|
|
|
|
void *user_data;
|
|
|
|
struct switch_xml_binding *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static switch_xml_binding_t *BINDINGS = NULL;
|
|
|
|
static switch_xml_t MAIN_XML_ROOT = NULL;
|
2008-09-09 23:14:15 +00:00
|
|
|
static switch_memory_pool_t *XML_MEMORY_POOL = NULL;
|
2011-03-31 18:43:36 +00:00
|
|
|
|
2008-09-09 23:14:15 +00:00
|
|
|
static switch_thread_rwlock_t *B_RWLOCK = NULL;
|
2011-03-31 18:43:36 +00:00
|
|
|
static switch_mutex_t *XML_LOCK = NULL;
|
2011-05-23 22:15:32 +00:00
|
|
|
static switch_mutex_t *CACHE_MUTEX = NULL;
|
2011-03-31 18:43:36 +00:00
|
|
|
static switch_mutex_t *REFLOCK = NULL;
|
2011-03-31 21:28:12 +00:00
|
|
|
static switch_mutex_t *FILE_LOCK = NULL;
|
2009-02-25 00:56:17 +00:00
|
|
|
static switch_mutex_t *XML_GEN_LOCK = NULL;
|
2011-03-31 18:43:36 +00:00
|
|
|
|
2011-06-29 16:30:31 +00:00
|
|
|
SWITCH_DECLARE_NONSTD(switch_xml_t) __switch_xml_open_root(uint8_t reload, const char **err, void *user_data);
|
2011-06-28 13:05:12 +00:00
|
|
|
|
|
|
|
static switch_xml_open_root_function_t XML_OPEN_ROOT_FUNCTION = (switch_xml_open_root_function_t)__switch_xml_open_root;
|
|
|
|
static void *XML_OPEN_ROOT_FUNCTION_USER_DATA = NULL;
|
|
|
|
|
2011-05-23 22:15:32 +00:00
|
|
|
static switch_hash_t *CACHE_HASH = NULL;
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
struct xml_section_t {
|
|
|
|
const char *name;
|
2009-03-05 04:04:41 +00:00
|
|
|
/* switch_xml_section_t section; */
|
2008-07-08 21:52:38 +00:00
|
|
|
uint32_t section;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct xml_section_t SECTIONS[] = {
|
|
|
|
{"result", SWITCH_XML_SECTION_RESULT},
|
|
|
|
{"config", SWITCH_XML_SECTION_CONFIG},
|
|
|
|
{"directory", SWITCH_XML_SECTION_DIRECTORY},
|
|
|
|
{"dialplan", SWITCH_XML_SECTION_DIALPLAN},
|
|
|
|
{"phrases", SWITCH_XML_SECTION_PHRASES},
|
2011-09-21 19:31:10 +00:00
|
|
|
{"chatplan", SWITCH_XML_SECTION_CHATPLAN},
|
2008-07-08 21:52:38 +00:00
|
|
|
{NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_xml_section_t) switch_xml_parse_section_string(const char *str)
|
|
|
|
{
|
|
|
|
size_t x;
|
|
|
|
char buf[1024] = "";
|
2009-03-05 04:04:41 +00:00
|
|
|
/*switch_xml_section_t sections = SWITCH_XML_SECTION_RESULT; */
|
2008-07-08 21:52:38 +00:00
|
|
|
uint32_t sections = SWITCH_XML_SECTION_RESULT;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (str) {
|
|
|
|
for (x = 0; x < strlen(str); x++) {
|
|
|
|
buf[x] = (char) tolower((int) str[x]);
|
|
|
|
}
|
|
|
|
for (x = 0;; x++) {
|
|
|
|
if (!SECTIONS[x].name) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strstr(buf, SECTIONS[x].name)) {
|
|
|
|
sections |= SECTIONS[x].section;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (switch_xml_section_t) sections;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_unbind_search_function(switch_xml_binding_t **binding)
|
|
|
|
{
|
|
|
|
switch_xml_binding_t *ptr, *last = NULL;
|
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
|
|
|
|
2008-09-09 22:47:37 +00:00
|
|
|
|
|
|
|
switch_thread_rwlock_wrlock(B_RWLOCK);
|
2008-07-08 21:52:38 +00:00
|
|
|
for (ptr = BINDINGS; ptr; ptr = ptr->next) {
|
|
|
|
if (ptr == *binding) {
|
|
|
|
if (last) {
|
|
|
|
last->next = (*binding)->next;
|
|
|
|
} else {
|
|
|
|
BINDINGS = (*binding)->next;
|
|
|
|
}
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last = ptr;
|
|
|
|
}
|
2008-09-09 22:47:37 +00:00
|
|
|
switch_thread_rwlock_unlock(B_RWLOCK);
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_unbind_search_function_ptr(switch_xml_search_function_t function)
|
|
|
|
{
|
|
|
|
switch_xml_binding_t *ptr, *last = NULL;
|
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
|
|
|
|
2008-09-09 22:47:37 +00:00
|
|
|
switch_thread_rwlock_wrlock(B_RWLOCK);
|
2008-07-08 21:52:38 +00:00
|
|
|
for (ptr = BINDINGS; ptr; ptr = ptr->next) {
|
|
|
|
if (ptr->function == function) {
|
|
|
|
if (last) {
|
|
|
|
last->next = ptr->next;
|
|
|
|
} else {
|
|
|
|
BINDINGS = ptr->next;
|
|
|
|
}
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
last = ptr;
|
|
|
|
}
|
2008-09-09 22:47:37 +00:00
|
|
|
switch_thread_rwlock_unlock(B_RWLOCK);
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2008-11-14 16:54:54 +00:00
|
|
|
SWITCH_DECLARE(void) switch_xml_set_binding_sections(switch_xml_binding_t *binding, switch_xml_section_t sections)
|
|
|
|
{
|
|
|
|
switch_assert(binding);
|
|
|
|
binding->sections = sections;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(void) switch_xml_set_binding_user_data(switch_xml_binding_t *binding, void *user_data)
|
|
|
|
{
|
|
|
|
switch_assert(binding);
|
|
|
|
binding->user_data = user_data;
|
|
|
|
}
|
|
|
|
|
2008-11-14 17:05:09 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_section_t) switch_xml_get_binding_sections(switch_xml_binding_t *binding)
|
|
|
|
{
|
|
|
|
return binding->sections;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(void *) switch_xml_get_binding_user_data(switch_xml_binding_t *binding)
|
|
|
|
{
|
|
|
|
return binding->user_data;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_bind_search_function_ret(switch_xml_search_function_t function,
|
2010-02-06 03:38:24 +00:00
|
|
|
switch_xml_section_t sections, void *user_data, switch_xml_binding_t **ret_binding)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
switch_xml_binding_t *binding = NULL, *ptr = NULL;
|
|
|
|
assert(function != NULL);
|
|
|
|
|
|
|
|
if (!(binding = (switch_xml_binding_t *) switch_core_alloc(XML_MEMORY_POOL, sizeof(*binding)))) {
|
|
|
|
return SWITCH_STATUS_MEMERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
binding->function = function;
|
|
|
|
binding->sections = sections;
|
|
|
|
binding->user_data = user_data;
|
|
|
|
|
2008-09-09 22:47:37 +00:00
|
|
|
switch_thread_rwlock_wrlock(B_RWLOCK);
|
2008-07-08 21:52:38 +00:00
|
|
|
for (ptr = BINDINGS; ptr && ptr->next; ptr = ptr->next);
|
|
|
|
|
|
|
|
if (ptr) {
|
|
|
|
ptr->next = binding;
|
|
|
|
} else {
|
|
|
|
BINDINGS = binding;
|
|
|
|
}
|
2008-11-14 02:46:28 +00:00
|
|
|
|
2008-11-14 18:01:27 +00:00
|
|
|
if (ret_binding) {
|
|
|
|
*ret_binding = binding;
|
2008-11-14 02:46:28 +00:00
|
|
|
}
|
|
|
|
|
2008-09-09 22:47:37 +00:00
|
|
|
switch_thread_rwlock_unlock(B_RWLOCK);
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_find_child(switch_xml_t node, const char *childname, const char *attrname, const char *value)
|
|
|
|
{
|
|
|
|
switch_xml_t p = NULL;
|
|
|
|
|
|
|
|
if (!(childname && attrname && value)) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = switch_xml_child(node, childname); p; p = p->next) {
|
|
|
|
const char *aname = switch_xml_attr(p, attrname);
|
|
|
|
if (aname && value && !strcasecmp(aname, value)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_find_child_multi(switch_xml_t node, const char *childname,...)
|
2008-12-23 17:36:50 +00:00
|
|
|
{
|
|
|
|
switch_xml_t p = NULL;
|
2010-02-06 03:38:24 +00:00
|
|
|
const char *names[256] = { 0 };
|
|
|
|
const char *vals[256] = { 0 };
|
2008-12-23 17:36:50 +00:00
|
|
|
int x, i = 0;
|
|
|
|
va_list ap;
|
2008-12-23 18:17:32 +00:00
|
|
|
const char *attrname, *value = NULL;
|
2008-12-23 17:36:50 +00:00
|
|
|
|
|
|
|
va_start(ap, childname);
|
2010-02-06 03:38:24 +00:00
|
|
|
|
|
|
|
while (i < 255) {
|
2008-12-23 17:36:50 +00:00
|
|
|
if ((attrname = va_arg(ap, const char *))) {
|
|
|
|
value = va_arg(ap, const char *);
|
|
|
|
}
|
|
|
|
if (attrname && value) {
|
|
|
|
names[i] = attrname;
|
|
|
|
vals[i] = value;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (!(childname && i)) {
|
|
|
|
return node;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-12-23 17:36:50 +00:00
|
|
|
for (p = switch_xml_child(node, childname); p; p = p->next) {
|
|
|
|
for (x = 0; x < i; x++) {
|
|
|
|
if (names[x] && vals[x]) {
|
|
|
|
const char *aname = switch_xml_attr(p, names[x]);
|
|
|
|
|
|
|
|
if (aname) {
|
|
|
|
if (*vals[x] == '!') {
|
|
|
|
const char *sval = vals[x] + 1;
|
|
|
|
if (sval && strcasecmp(aname, sval)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!strcasecmp(aname, vals[x])) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
done:
|
2008-12-23 17:36:50 +00:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* returns the first child tag with the given name or NULL if not found */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_child(switch_xml_t xml, const char *name)
|
|
|
|
{
|
|
|
|
xml = (xml) ? xml->child : NULL;
|
|
|
|
while (xml && strcmp(name, xml->name))
|
|
|
|
xml = xml->sibling;
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* returns the Nth tag with the same name in the same subsection or NULL if not found */
|
2008-07-08 21:52:38 +00:00
|
|
|
switch_xml_t switch_xml_idx(switch_xml_t xml, int idx)
|
|
|
|
{
|
|
|
|
for (; xml && idx; idx--)
|
|
|
|
xml = xml->next;
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* returns the value of the requested tag attribute or "" if not found */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(const char *) switch_xml_attr_soft(switch_xml_t xml, const char *attr)
|
|
|
|
{
|
|
|
|
const char *ret = switch_xml_attr(xml, attr);
|
|
|
|
|
|
|
|
return ret ? ret : "";
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* returns the value of the requested tag attribute or NULL if not found */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(const char *) switch_xml_attr(switch_xml_t xml, const char *attr)
|
|
|
|
{
|
|
|
|
int i = 0, j = 1;
|
|
|
|
switch_xml_root_t root = (switch_xml_root_t) xml;
|
|
|
|
|
|
|
|
if (!xml || !xml->attr)
|
|
|
|
return NULL;
|
2009-05-15 21:07:18 +00:00
|
|
|
while (xml->attr[i] && attr && strcmp(attr, xml->attr[i]))
|
2008-07-08 21:52:38 +00:00
|
|
|
i += 2;
|
|
|
|
if (xml->attr[i])
|
2009-03-05 04:04:41 +00:00
|
|
|
return xml->attr[i + 1]; /* found attribute */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
while (root->xml.parent)
|
2009-03-05 04:04:41 +00:00
|
|
|
root = (switch_xml_root_t) root->xml.parent; /* root tag */
|
2010-02-11 15:54:41 +00:00
|
|
|
|
|
|
|
/* Make sure root is really a switch_xml_root_t (Issues with switch_xml_toxml) */
|
|
|
|
if (!root->xml.is_switch_xml_root_t) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-26 11:02:26 +00:00
|
|
|
if (!root->attr) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-15 21:07:18 +00:00
|
|
|
for (i = 0; root->attr[i] && xml->name && strcmp(xml->name, root->attr[i][0]); i++);
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!root->attr[i])
|
2009-03-05 04:04:41 +00:00
|
|
|
return NULL; /* no matching default attributes */
|
2009-05-15 21:07:18 +00:00
|
|
|
while (root->attr[i][j] && attr && strcmp(attr, root->attr[i][j]))
|
2008-07-08 21:52:38 +00:00
|
|
|
j += 3;
|
2009-03-05 04:04:41 +00:00
|
|
|
return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; /* found default */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* same as switch_xml_get but takes an already initialized va_list */
|
2008-07-08 21:52:38 +00:00
|
|
|
static switch_xml_t switch_xml_vget(switch_xml_t xml, va_list ap)
|
|
|
|
{
|
|
|
|
char *name = va_arg(ap, char *);
|
|
|
|
int idx = -1;
|
|
|
|
|
|
|
|
if (name && *name) {
|
|
|
|
idx = va_arg(ap, int);
|
|
|
|
xml = switch_xml_child(xml, name);
|
|
|
|
}
|
|
|
|
return (idx < 0) ? xml : switch_xml_vget(switch_xml_idx(xml, idx), ap);
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* Traverses the xml tree to retrieve a specific subtag. Takes a variable
|
|
|
|
length list of tag names and indexes. The argument list must be terminated
|
|
|
|
by either an index of -1 or an empty string tag name. Example:
|
|
|
|
title = switch_xml_get(library, "shelf", 0, "book", 2, "title", -1);
|
|
|
|
This retrieves the title of the 3rd book on the 1st shelf of library.
|
|
|
|
Returns NULL if not found. */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_get(switch_xml_t xml,...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
switch_xml_t r;
|
|
|
|
|
|
|
|
va_start(ap, xml);
|
|
|
|
r = switch_xml_vget(xml, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* returns a null terminated array of processing instructions for the given target */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(const char **) switch_xml_pi(switch_xml_t xml, const char *target)
|
|
|
|
{
|
|
|
|
switch_xml_root_t root = (switch_xml_root_t) xml;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (!root)
|
|
|
|
return (const char **) SWITCH_XML_NIL;
|
|
|
|
while (root->xml.parent)
|
2009-03-05 04:04:41 +00:00
|
|
|
root = (switch_xml_root_t) root->xml.parent; /* root tag */
|
2010-04-26 11:02:26 +00:00
|
|
|
if (!root || !root->pi) {
|
|
|
|
return (const char **) SWITCH_XML_NIL;
|
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
while (root->pi[i] && strcmp(target, root->pi[i][0]))
|
2009-03-05 04:04:41 +00:00
|
|
|
i++; /* find target */
|
2008-07-08 21:52:38 +00:00
|
|
|
return (const char **) ((root->pi[i]) ? root->pi[i] + 1 : SWITCH_XML_NIL);
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* set an error string and return root */
|
2008-07-08 21:52:38 +00:00
|
|
|
static switch_xml_t switch_xml_err(switch_xml_root_t root, char *s, const char *err, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int line = 1;
|
|
|
|
char *t, fmt[SWITCH_XML_ERRL];
|
|
|
|
|
2010-04-26 11:02:26 +00:00
|
|
|
if (!root || !root->s) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
for (t = root->s; t && t < s; t++)
|
|
|
|
if (*t == '\n')
|
|
|
|
line++;
|
|
|
|
switch_snprintf(fmt, SWITCH_XML_ERRL, "[error near line %d]: %s", line, err);
|
|
|
|
|
|
|
|
va_start(ap, err);
|
|
|
|
vsnprintf(root->err, SWITCH_XML_ERRL, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return &root->xml;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* Recursively decodes entity and character references and normalizes new lines
|
|
|
|
ent is a null terminated array of alternating entity names and values. set t
|
|
|
|
to '&' for general entity decoding, '%' for parameter entity decoding, 'c'
|
|
|
|
for cdata sections, ' ' for attribute normalization, or '*' for non-cdata
|
|
|
|
attribute normalization. Returns s, or if the decoded string is longer than
|
|
|
|
s, returns a malloced string that must be freed. */
|
2008-07-08 21:52:38 +00:00
|
|
|
static char *switch_xml_decode(char *s, char **ent, char t)
|
|
|
|
{
|
|
|
|
char *e, *r = s, *m = s;
|
|
|
|
long b, c, d, l;
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
for (; *s; s++) { /* normalize line endings */
|
2008-07-08 21:52:38 +00:00
|
|
|
while (*s == '\r') {
|
|
|
|
*(s++) = '\n';
|
|
|
|
if (*s == '\n')
|
|
|
|
memmove(s, (s + 1), strlen(s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (s = r;;) {
|
2010-01-25 15:16:43 +00:00
|
|
|
while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace((unsigned char) (*s)))
|
2008-07-08 21:52:38 +00:00
|
|
|
s++;
|
|
|
|
|
|
|
|
if (!*s)
|
|
|
|
break;
|
2009-03-05 04:04:41 +00:00
|
|
|
else if (t != 'c' && !strncmp(s, "&#", 2)) { /* character reference */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (s[2] == 'x')
|
2009-03-05 04:04:41 +00:00
|
|
|
c = strtol(s + 3, &e, 16); /* base 16 */
|
2008-07-08 21:52:38 +00:00
|
|
|
else
|
2009-03-05 04:04:41 +00:00
|
|
|
c = strtol(s + 2, &e, 10); /* base 10 */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!c || *e != ';') {
|
|
|
|
s++;
|
|
|
|
continue;
|
2010-02-06 03:38:24 +00:00
|
|
|
}
|
|
|
|
/* not a character ref */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (c < 0x80)
|
2009-03-05 04:04:41 +00:00
|
|
|
*(s++) = (char) c; /* US-ASCII subset */
|
2010-02-06 03:38:24 +00:00
|
|
|
else { /* multi-byte UTF-8 sequence */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (b = 0, d = c; d; d /= 2)
|
2010-02-06 03:38:24 +00:00
|
|
|
b++; /* number of bits in c */
|
2009-03-05 04:04:41 +00:00
|
|
|
b = (b - 2) / 5; /* number of bytes in payload */
|
|
|
|
*(s++) = (char) ((0xFF << (7 - b)) | (c >> (6 * b))); /* head */
|
2008-07-08 21:52:38 +00:00
|
|
|
while (b)
|
2009-03-05 04:04:41 +00:00
|
|
|
*(s++) = (char) (0x80 | ((c >> (6 * --b)) & 0x3F)); /* payload */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
|
2009-03-05 04:04:41 +00:00
|
|
|
} else if ((*s == '&' && (t == '&' || t == ' ' || t == '*')) || (*s == '%' && t == '%')) { /* entity reference */
|
|
|
|
for (b = 0; ent[b] && strncmp(s + 1, ent[b], strlen(ent[b])); b += 2); /* find entity in entity list */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
if (ent[b++]) { /* found a match */
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((c = (long) strlen(ent[b])) - 1 > (e = strchr(s, ';')) - s) {
|
2009-03-05 04:04:41 +00:00
|
|
|
l = (d = (long) (s - r)) + c + (long) strlen(e); /* new length */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (l) {
|
|
|
|
if (r == m) {
|
2010-02-06 03:38:24 +00:00
|
|
|
char *tmp = (char *) malloc(l);
|
2008-07-08 21:52:38 +00:00
|
|
|
if (tmp) {
|
|
|
|
r = strcpy(tmp, r);
|
|
|
|
} else {
|
2010-02-06 03:38:24 +00:00
|
|
|
if (r)
|
|
|
|
free(r);
|
2008-07-08 21:52:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
2010-02-06 03:38:24 +00:00
|
|
|
char *tmp = (char *) realloc(r, l);
|
2008-07-08 21:52:38 +00:00
|
|
|
if (tmp) {
|
|
|
|
r = tmp;
|
|
|
|
} else {
|
2010-02-06 03:38:24 +00:00
|
|
|
if (r)
|
|
|
|
free(r);
|
2008-07-08 21:52:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-05 04:04:41 +00:00
|
|
|
e = strchr((s = r + d), ';'); /* fix up pointers */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
memmove(s + c, e + 1, strlen(e)); /* shift rest of string */
|
|
|
|
strncpy(s, ent[b], c); /* copy in replacement text */
|
2008-07-08 21:52:38 +00:00
|
|
|
} else
|
2009-03-05 04:04:41 +00:00
|
|
|
s++; /* not a known entity */
|
2008-07-08 21:52:38 +00:00
|
|
|
} else if ((t == ' ' || t == '*') && isspace((int) (*s)))
|
|
|
|
*(s++) = ' ';
|
|
|
|
else
|
2009-03-05 04:04:41 +00:00
|
|
|
s++; /* no decoding needed */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
if (t == '*') { /* normalize spaces for non-cdata attributes */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (s = r; *s; s++) {
|
|
|
|
if ((l = (long) strspn(s, " ")))
|
|
|
|
memmove(s, s + l, strlen(s + l) + 1);
|
|
|
|
while (*s && *s != ' ')
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (--s >= r && *s == ' ')
|
2009-03-05 04:04:41 +00:00
|
|
|
*s = '\0'; /* trim any trailing space */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* called when parser finds start of new tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
static void switch_xml_open_tag(switch_xml_root_t root, char *name, char **attr)
|
|
|
|
{
|
2010-04-26 11:02:26 +00:00
|
|
|
switch_xml_t xml;
|
|
|
|
|
|
|
|
if (!root || !root->cur) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
xml = root->cur;
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
if (xml->name)
|
|
|
|
xml = switch_xml_add_child(xml, name, strlen(xml->txt));
|
|
|
|
else
|
2009-03-05 04:04:41 +00:00
|
|
|
xml->name = name; /* first open tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
xml->attr = attr;
|
2009-03-05 04:04:41 +00:00
|
|
|
root->cur = xml; /* update tag insertion point */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* called when parser finds character content between open and closing tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
static void switch_xml_char_content(switch_xml_root_t root, char *s, switch_size_t len, char t)
|
|
|
|
{
|
2010-04-26 11:02:26 +00:00
|
|
|
switch_xml_t xml;
|
2008-07-08 21:52:38 +00:00
|
|
|
char *m = s;
|
|
|
|
switch_size_t l;
|
|
|
|
|
2010-04-26 11:02:26 +00:00
|
|
|
if (!root || !root->cur) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
xml = root->cur;
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!xml || !xml->name || !len)
|
2009-03-05 04:04:41 +00:00
|
|
|
return; /* sanity check */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
s[len] = '\0'; /* null terminate text (calling functions anticipate this) */
|
2008-07-08 21:52:38 +00:00
|
|
|
len = strlen(s = switch_xml_decode(s, root->ent, t)) + 1;
|
|
|
|
|
|
|
|
if (!*(xml->txt))
|
2009-03-05 04:04:41 +00:00
|
|
|
xml->txt = s; /* initial character content */
|
|
|
|
else { /* allocate our own memory and make a copy */
|
2010-02-06 03:38:24 +00:00
|
|
|
if ((xml->flags & SWITCH_XML_TXTM)) { /* allocate some space */
|
|
|
|
char *tmp = (char *) realloc(xml->txt, (l = strlen(xml->txt)) + len);
|
2008-07-08 21:52:38 +00:00
|
|
|
if (tmp) {
|
|
|
|
xml->txt = tmp;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2010-02-06 03:38:24 +00:00
|
|
|
char *tmp = (char *) malloc((l = strlen(xml->txt)) + len);
|
2008-07-08 21:52:38 +00:00
|
|
|
if (tmp) {
|
|
|
|
xml->txt = strcpy(tmp, xml->txt);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-03-05 04:04:41 +00:00
|
|
|
strcpy(xml->txt + l, s); /* add new char content */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (s != m)
|
2009-03-05 04:04:41 +00:00
|
|
|
free(s); /* free s if it was malloced by switch_xml_decode() */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (xml->txt != m)
|
|
|
|
switch_xml_set_flag(xml, SWITCH_XML_TXTM);
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* called when parser finds closing tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
static switch_xml_t switch_xml_close_tag(switch_xml_root_t root, char *name, char *s)
|
|
|
|
{
|
2010-04-26 11:02:26 +00:00
|
|
|
if (!root || !root->cur || !root->cur->name || strcmp(name, root->cur->name))
|
2008-07-08 21:52:38 +00:00
|
|
|
return switch_xml_err(root, s, "unexpected closing tag </%s>", name);
|
|
|
|
|
|
|
|
root->cur = root->cur->parent;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* checks for circular entity references, returns non-zero if no circular
|
|
|
|
references are found, zero otherwise */
|
2008-07-08 21:52:38 +00:00
|
|
|
static int switch_xml_ent_ok(char *name, char *s, char **ent)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (;; s++) {
|
|
|
|
while (*s && *s != '&')
|
2009-03-05 04:04:41 +00:00
|
|
|
s++; /* find next entity reference */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!*s)
|
|
|
|
return 1;
|
|
|
|
if (!strncmp(s + 1, name, strlen(name)))
|
2009-03-05 04:04:41 +00:00
|
|
|
return 0; /* circular ref. */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (i = 0; ent[i] && strncmp(ent[i], s + 1, strlen(ent[i])); i += 2);
|
|
|
|
if (ent[i] && !switch_xml_ent_ok(name, ent[i + 1], ent))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* called when the parser finds a processing instruction */
|
2008-07-08 21:52:38 +00:00
|
|
|
static void switch_xml_proc_inst(switch_xml_root_t root, char *s, switch_size_t len)
|
|
|
|
{
|
|
|
|
int i = 0, j = 1;
|
|
|
|
char *target = s;
|
|
|
|
char **sstmp;
|
|
|
|
char *stmp;
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
s[len] = '\0'; /* null terminate instruction */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (*(s += strcspn(s, SWITCH_XML_WS))) {
|
2009-03-05 04:04:41 +00:00
|
|
|
*s = '\0'; /* null terminate target */
|
|
|
|
s += strspn(s + 1, SWITCH_XML_WS) + 1; /* skip whitespace after target */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!root)
|
|
|
|
return;
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
if (!strcmp(target, "xml")) { /* <?xml ... ?> */
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((s = strstr(s, "standalone")) && !strncmp(s + strspn(s + 10, SWITCH_XML_WS "='\"") + 10, "yes", 3))
|
|
|
|
root->standalone = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-26 11:02:26 +00:00
|
|
|
if (!root->pi || !root->pi[0]) {
|
2010-02-06 03:38:24 +00:00
|
|
|
root->pi = (char ***) malloc(sizeof(char **));
|
|
|
|
if (!root->pi)
|
|
|
|
return;
|
|
|
|
*(root->pi) = NULL; /* first pi */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (root->pi[i] && strcmp(target, root->pi[i][0]))
|
2009-03-05 04:04:41 +00:00
|
|
|
i++; /* find target */
|
|
|
|
if (!root->pi[i]) { /* new target */
|
2010-02-06 03:38:24 +00:00
|
|
|
char ***ssstmp = (char ***) realloc(root->pi, sizeof(char **) * (i + 2));
|
|
|
|
if (!ssstmp)
|
|
|
|
return;
|
|
|
|
root->pi = ssstmp;
|
|
|
|
if (!root->pi)
|
|
|
|
return;
|
|
|
|
root->pi[i] = (char **) malloc(sizeof(char *) * 3);
|
|
|
|
if (!root->pi[i])
|
|
|
|
return;
|
2008-07-08 21:52:38 +00:00
|
|
|
root->pi[i][0] = target;
|
2009-03-05 04:04:41 +00:00
|
|
|
root->pi[i][1] = (char *) (root->pi[i + 1] = NULL); /* terminate pi list */
|
|
|
|
root->pi[i][2] = strdup(""); /* empty document position list */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (root->pi[i][j])
|
2009-03-05 04:04:41 +00:00
|
|
|
j++; /* find end of instruction list for this target */
|
2010-02-06 03:38:24 +00:00
|
|
|
sstmp = (char **) realloc(root->pi[i], sizeof(char *) * (j + 3));
|
|
|
|
if (!sstmp)
|
|
|
|
return;
|
2008-07-08 21:52:38 +00:00
|
|
|
root->pi[i] = sstmp;
|
2010-02-06 03:38:24 +00:00
|
|
|
stmp = (char *) realloc(root->pi[i][j + 1], j + 1);
|
|
|
|
if (!stmp)
|
|
|
|
return;
|
|
|
|
root->pi[i][j + 2] = stmp;
|
2008-07-08 21:52:38 +00:00
|
|
|
strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<");
|
2009-03-05 04:04:41 +00:00
|
|
|
root->pi[i][j + 1] = NULL; /* null terminate pi list for this target */
|
|
|
|
root->pi[i][j] = s; /* set instruction */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* called when the parser finds an internal doctype subset */
|
2008-07-08 21:52:38 +00:00
|
|
|
static short switch_xml_internal_dtd(switch_xml_root_t root, char *s, switch_size_t len)
|
|
|
|
{
|
|
|
|
char q, *c, *t, *n = NULL, *v, **ent, **pe;
|
|
|
|
int i, j;
|
|
|
|
char **sstmp;
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
pe = (char **) memcpy(malloc(sizeof(SWITCH_XML_NIL)), SWITCH_XML_NIL, sizeof(SWITCH_XML_NIL));
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
for (s[len] = '\0'; s;) {
|
|
|
|
while (*s && *s != '<' && *s != '%')
|
2009-03-05 04:04:41 +00:00
|
|
|
s++; /* find next declaration */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
if (!*s)
|
|
|
|
break;
|
2010-02-06 03:38:24 +00:00
|
|
|
else if (!strncmp(s, "<!ENTITY", 8)) { /* parse entity definitions */
|
2009-03-05 04:04:41 +00:00
|
|
|
c = s += strspn(s + 8, SWITCH_XML_WS) + 8; /* skip white space separator */
|
2010-02-06 03:38:24 +00:00
|
|
|
n = s + strspn(s, SWITCH_XML_WS "%"); /* find name */
|
2009-03-05 04:04:41 +00:00
|
|
|
*(s = n + strcspn(n, SWITCH_XML_WS)) = ';'; /* append ; to name */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
v = s + strspn(s + 1, SWITCH_XML_WS) + 1; /* find value */
|
2010-02-06 03:38:24 +00:00
|
|
|
if ((q = *(v++)) != '"' && q != '\'') { /* skip externals */
|
2008-07-08 21:52:38 +00:00
|
|
|
s = strchr(s, '>');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++);
|
2010-02-06 03:38:24 +00:00
|
|
|
sstmp = (char **) realloc(ent, (i + 3) * sizeof(char *)); /* space for next ent */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!sstmp) {
|
|
|
|
switch_xml_err(root, v, "Allocation Error!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ent = sstmp;
|
|
|
|
if (*c == '%')
|
|
|
|
pe = ent;
|
|
|
|
else
|
|
|
|
root->ent = ent;
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
*(++s) = '\0'; /* null terminate name */
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((s = strchr(v, q)))
|
2009-03-05 04:04:41 +00:00
|
|
|
*(s++) = '\0'; /* null terminate value */
|
|
|
|
ent[i + 1] = switch_xml_decode(v, pe, '%'); /* set value */
|
|
|
|
ent[i + 2] = NULL; /* null terminate entity list */
|
|
|
|
if (!switch_xml_ent_ok(n, ent[i + 1], ent)) { /* circular reference */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (ent[i + 1] != v)
|
|
|
|
free(ent[i + 1]);
|
|
|
|
switch_xml_err(root, v, "circular entity declaration &%s", n);
|
|
|
|
break;
|
|
|
|
} else
|
2009-03-05 04:04:41 +00:00
|
|
|
ent[i] = n; /* set entity name */
|
|
|
|
} else if (!strncmp(s, "<!ATTLIST", 9)) { /* parse default attributes */
|
|
|
|
t = s + strspn(s + 9, SWITCH_XML_WS) + 9; /* skip whitespace separator */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!*t) {
|
|
|
|
switch_xml_err(root, t, "unclosed <!ATTLIST");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*(s = t + strcspn(t, SWITCH_XML_WS ">")) == '>')
|
|
|
|
continue;
|
|
|
|
else
|
2009-03-05 04:04:41 +00:00
|
|
|
*s = '\0'; /* null terminate tag name */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++);
|
|
|
|
|
2009-06-05 21:55:02 +00:00
|
|
|
//while (*(n = ++s + strspn(s, SWITCH_XML_WS)) && *n != '>') {
|
|
|
|
// gcc 4.4 you are a creep
|
2010-02-06 03:38:24 +00:00
|
|
|
for (;;) {
|
2009-06-05 21:55:02 +00:00
|
|
|
s++;
|
|
|
|
if (!(*(n = s + strspn(s, SWITCH_XML_WS)) && *n != '>')) {
|
|
|
|
break;
|
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
if (*(s = n + strcspn(n, SWITCH_XML_WS)))
|
2009-03-05 04:04:41 +00:00
|
|
|
*s = '\0'; /* attr name */
|
2008-07-08 21:52:38 +00:00
|
|
|
else {
|
|
|
|
switch_xml_err(root, t, "malformed <!ATTLIST");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
s += strspn(s + 1, SWITCH_XML_WS) + 1; /* find next token */
|
2010-02-06 03:38:24 +00:00
|
|
|
c = (strncmp(s, "CDATA", 5)) ? (char *) "*" : (char *) " "; /* is it cdata? */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!strncmp(s, "NOTATION", 8))
|
|
|
|
s += strspn(s + 8, SWITCH_XML_WS) + 8;
|
|
|
|
s = (*s == '(') ? strchr(s, ')') : s + strcspn(s, SWITCH_XML_WS);
|
|
|
|
if (!s) {
|
|
|
|
switch_xml_err(root, t, "malformed <!ATTLIST");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
s += strspn(s, SWITCH_XML_WS ")"); /* skip white space separator */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!strncmp(s, "#FIXED", 6))
|
|
|
|
s += strspn(s + 6, SWITCH_XML_WS) + 6;
|
2009-03-05 04:04:41 +00:00
|
|
|
if (*s == '#') { /* no default value */
|
2008-07-08 21:52:38 +00:00
|
|
|
s += strcspn(s, SWITCH_XML_WS ">") - 1;
|
|
|
|
if (*c == ' ')
|
2009-03-05 04:04:41 +00:00
|
|
|
continue; /* cdata is default, nothing to do */
|
2008-07-08 21:52:38 +00:00
|
|
|
v = NULL;
|
2009-03-05 04:04:41 +00:00
|
|
|
} else if ((*s == '"' || *s == '\'') && /* default value */
|
2008-07-08 21:52:38 +00:00
|
|
|
(s = strchr(v = s + 1, *s)))
|
|
|
|
*s = '\0';
|
|
|
|
else {
|
|
|
|
switch_xml_err(root, t, "malformed <!ATTLIST");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
if (!root->attr[i]) { /* new tag name */
|
2010-02-06 03:38:24 +00:00
|
|
|
root->attr = (!i) ? (char ***) malloc(2 * sizeof(char **))
|
|
|
|
: (char ***) realloc(root->attr, (i + 2) * sizeof(char **));
|
|
|
|
root->attr[i] = (char **) malloc(2 * sizeof(char *));
|
2009-03-05 04:04:41 +00:00
|
|
|
root->attr[i][0] = t; /* set tag name */
|
2008-07-08 21:52:38 +00:00
|
|
|
root->attr[i][1] = (char *) (root->attr[i + 1] = NULL);
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
for (j = 1; root->attr[i][j]; j += 3); /* find end of list */
|
2010-02-06 03:38:24 +00:00
|
|
|
sstmp = (char **) realloc(root->attr[i], (j + 4) * sizeof(char *));
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
if (!sstmp) {
|
|
|
|
switch_xml_err(root, t, "Allocation Error!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
root->attr[i] = sstmp;
|
2009-03-05 04:04:41 +00:00
|
|
|
root->attr[i][j + 3] = NULL; /* null terminate list */
|
2010-02-06 03:38:24 +00:00
|
|
|
root->attr[i][j + 2] = c; /* is it cdata? */
|
2009-03-05 04:04:41 +00:00
|
|
|
root->attr[i][j + 1] = (v) ? switch_xml_decode(v, root->ent, *c) : NULL;
|
2010-02-06 03:38:24 +00:00
|
|
|
root->attr[i][j] = n; /* attribute name */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
} else if (!strncmp(s, "<!--", 4))
|
2010-02-06 03:38:24 +00:00
|
|
|
s = strstr(s + 4, "-->"); /* comments */
|
|
|
|
else if (!strncmp(s, "<?", 2)) { /* processing instructions */
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((s = strstr(c = s + 2, "?>")))
|
|
|
|
switch_xml_proc_inst(root, c, s++ - c);
|
|
|
|
} else if (*s == '<')
|
2010-02-06 03:38:24 +00:00
|
|
|
s = strchr(s, '>'); /* skip other declarations */
|
2008-07-08 21:52:38 +00:00
|
|
|
else if (*(s++) == '%' && !root->standalone)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(pe);
|
|
|
|
return !*root->err;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* Converts a UTF-16 string to UTF-8. Returns a new string that must be freed
|
|
|
|
or NULL if no conversion was needed. */
|
2008-07-08 21:52:38 +00:00
|
|
|
static char *switch_xml_str2utf8(char **s, switch_size_t *len)
|
|
|
|
{
|
|
|
|
char *u;
|
|
|
|
switch_size_t l = 0, sl, max = *len;
|
|
|
|
long c, d;
|
|
|
|
int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1;
|
|
|
|
|
|
|
|
if (be == -1)
|
2009-03-05 04:04:41 +00:00
|
|
|
return NULL; /* not UTF-16 */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
u = (char *) malloc(max);
|
2008-07-08 21:52:38 +00:00
|
|
|
for (sl = 2; sl < *len - 1; sl += 2) {
|
2009-03-05 04:04:41 +00:00
|
|
|
c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) /* UTF-16BE */
|
2010-02-06 03:38:24 +00:00
|
|
|
: (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); /* UTF-16LE */
|
2009-03-05 04:04:41 +00:00
|
|
|
if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { /* high-half */
|
2008-07-08 21:52:38 +00:00
|
|
|
d = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF)
|
|
|
|
: (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF);
|
|
|
|
c = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (l + 6 > max) {
|
|
|
|
char *tmp;
|
2010-02-06 03:38:24 +00:00
|
|
|
tmp = (char *) realloc(u, max += SWITCH_XML_BUFSIZE);
|
|
|
|
if (!tmp)
|
|
|
|
return NULL;
|
2008-07-08 21:52:38 +00:00
|
|
|
u = tmp;
|
|
|
|
}
|
|
|
|
if (c < 0x80)
|
2009-03-05 04:04:41 +00:00
|
|
|
u[l++] = (char) c; /* US-ASCII subset */
|
|
|
|
else { /* multi-byte UTF-8 sequence */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (b = 0, d = c; d; d /= 2)
|
2009-03-05 04:04:41 +00:00
|
|
|
b++; /* bits in c */
|
|
|
|
b = (b - 2) / 5; /* bytes in payload */
|
|
|
|
u[l++] = (char) ((0xFF << (7 - b)) | (c >> (6 * b))); /* head */
|
2008-07-08 21:52:38 +00:00
|
|
|
while (b)
|
2009-03-05 04:04:41 +00:00
|
|
|
u[l++] = (char) (0x80 | ((c >> (6 * --b)) & 0x3F)); /* payload */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
return *s = (char *) realloc(u, *len = l);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* frees a tag attribute list */
|
2008-07-08 21:52:38 +00:00
|
|
|
static void switch_xml_free_attr(char **attr)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
char *m;
|
|
|
|
|
|
|
|
if (!attr || attr == SWITCH_XML_NIL)
|
2009-03-05 04:04:41 +00:00
|
|
|
return; /* nothing to free */
|
2008-07-08 21:52:38 +00:00
|
|
|
while (attr[i])
|
2009-03-05 04:04:41 +00:00
|
|
|
i += 2; /* find end of attribute list */
|
|
|
|
m = attr[i + 1]; /* list of which names and values are malloced */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (i = 0; m[i]; i++) {
|
|
|
|
if (m[i] & SWITCH_XML_NAMEM)
|
|
|
|
free(attr[i * 2]);
|
|
|
|
if (m[i] & SWITCH_XML_TXTM)
|
|
|
|
free(attr[(i * 2) + 1]);
|
|
|
|
}
|
|
|
|
free(m);
|
|
|
|
free(attr);
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_parse_str_dynamic(char *s, switch_bool_t dup)
|
2009-05-07 21:40:21 +00:00
|
|
|
{
|
|
|
|
switch_xml_root_t root;
|
2009-05-08 04:33:58 +00:00
|
|
|
char *data;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2009-05-08 04:33:58 +00:00
|
|
|
switch_assert(s);
|
|
|
|
data = dup ? strdup(s) : s;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2009-05-07 21:40:21 +00:00
|
|
|
if ((root = (switch_xml_root_t) switch_xml_parse_str(data, strlen(data)))) {
|
2010-02-06 03:38:24 +00:00
|
|
|
root->dynamic = 1; /* Make sure we free the memory is switch_xml_free() */
|
2009-05-07 21:40:21 +00:00
|
|
|
return &root->xml;
|
|
|
|
} else {
|
2009-05-08 04:33:58 +00:00
|
|
|
if (dup) {
|
|
|
|
free(data);
|
|
|
|
}
|
2009-05-07 21:40:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* parse the given xml string and return an switch_xml structure */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_parse_str(char *s, switch_size_t len)
|
|
|
|
{
|
|
|
|
switch_xml_root_t root = (switch_xml_root_t) switch_xml_new(NULL);
|
2009-03-05 04:04:41 +00:00
|
|
|
char q, e, *d, **attr, **a = NULL; /* initialize a to avoid compile warning */
|
2008-07-08 21:52:38 +00:00
|
|
|
int l, i, j;
|
|
|
|
|
|
|
|
root->m = s;
|
|
|
|
if (!len)
|
|
|
|
return switch_xml_err(root, s, "root tag missing");
|
2009-03-05 04:04:41 +00:00
|
|
|
root->u = switch_xml_str2utf8(&s, &len); /* convert utf-16 to utf-8 */
|
|
|
|
root->e = (root->s = s) + len; /* record start and end of work area */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
e = s[len - 1]; /* save end char */
|
|
|
|
s[len - 1] = '\0'; /* turn end char into null terminator */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
while (*s && *s != '<')
|
2009-03-05 04:04:41 +00:00
|
|
|
s++; /* find first tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!*s)
|
|
|
|
return switch_xml_err(root, s, "root tag missing");
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
attr = (char **) SWITCH_XML_NIL;
|
|
|
|
d = ++s;
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (isalpha((int) (*s)) || *s == '_' || *s == ':' || (int8_t) * s < '\0') { /* new tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!root->cur)
|
|
|
|
return switch_xml_err(root, d, "markup outside of root element");
|
|
|
|
|
|
|
|
s += strcspn(s, SWITCH_XML_WS "/>");
|
|
|
|
while (isspace((int) (*s)))
|
2010-02-06 03:38:24 +00:00
|
|
|
*(s++) = '\0'; /* null terminate tag name */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
if (*s && *s != '/' && *s != '>') /* find tag in default attr list */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++);
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { /* new attrib */
|
2010-02-06 03:38:24 +00:00
|
|
|
attr = (l) ? (char **) realloc(attr, (l + 4) * sizeof(char *))
|
|
|
|
: (char **) malloc(4 * sizeof(char *)); /* allocate space */
|
|
|
|
attr[l + 3] = (l) ? (char *) realloc(attr[l + 1], (l / 2) + 2)
|
|
|
|
: (char *) malloc(2); /* mem for list of maloced vals */
|
|
|
|
strcpy(attr[l + 3] + (l / 2), " "); /* value is not malloced */
|
|
|
|
attr[l + 2] = NULL; /* null terminate list */
|
|
|
|
attr[l + 1] = (char *) ""; /* temporary attribute value */
|
|
|
|
attr[l] = s; /* set attribute name */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
s += strcspn(s, SWITCH_XML_WS "=/>");
|
|
|
|
if (*s == '=' || isspace((int) (*s))) {
|
2009-03-05 04:04:41 +00:00
|
|
|
*(s++) = '\0'; /* null terminate tag attribute name */
|
2008-07-08 21:52:38 +00:00
|
|
|
q = *(s += strspn(s, SWITCH_XML_WS "="));
|
2009-03-05 04:04:41 +00:00
|
|
|
if (q == '"' || q == '\'') { /* attribute value */
|
2008-07-08 21:52:38 +00:00
|
|
|
attr[l + 1] = ++s;
|
|
|
|
while (*s && *s != q)
|
|
|
|
s++;
|
|
|
|
if (*s)
|
2009-03-05 04:04:41 +00:00
|
|
|
*(s++) = '\0'; /* null terminate attribute val */
|
2008-07-08 21:52:38 +00:00
|
|
|
else {
|
|
|
|
switch_xml_free_attr(attr);
|
|
|
|
return switch_xml_err(root, d, "missing %c", q);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 1; a && a[j] && strcmp(a[j], attr[l]); j += 3);
|
|
|
|
attr[l + 1] = switch_xml_decode(attr[l + 1], root->ent, (a && a[j]) ? *a[j + 2] : ' ');
|
|
|
|
if (attr[l + 1] < d || attr[l + 1] > s)
|
2009-03-05 04:04:41 +00:00
|
|
|
attr[l + 3][l / 2] = SWITCH_XML_TXTM; /* value malloced */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (isspace((int) (*s)))
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
if (*s == '/') { /* self closing tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
*(s++) = '\0';
|
|
|
|
if ((*s && *s != '>') || (!*s && e != '>')) {
|
|
|
|
if (l)
|
|
|
|
switch_xml_free_attr(attr);
|
|
|
|
return switch_xml_err(root, d, "missing >");
|
|
|
|
}
|
|
|
|
switch_xml_open_tag(root, d, attr);
|
|
|
|
switch_xml_close_tag(root, d, s);
|
2009-03-05 04:04:41 +00:00
|
|
|
} else if ((q = *s) == '>' || (!*s && e == '>')) { /* open tag */
|
|
|
|
*s = '\0'; /* temporarily null terminate tag name */
|
2008-07-08 21:52:38 +00:00
|
|
|
switch_xml_open_tag(root, d, attr);
|
|
|
|
*s = q;
|
|
|
|
} else {
|
|
|
|
if (l)
|
|
|
|
switch_xml_free_attr(attr);
|
|
|
|
return switch_xml_err(root, d, "missing >");
|
|
|
|
}
|
2009-03-05 04:04:41 +00:00
|
|
|
} else if (*s == '/') { /* close tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
s += strcspn(d = s + 1, SWITCH_XML_WS ">") + 1;
|
|
|
|
if (!(q = *s) && e != '>')
|
|
|
|
return switch_xml_err(root, d, "missing >");
|
2009-03-05 04:04:41 +00:00
|
|
|
*s = '\0'; /* temporarily null terminate tag name */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (switch_xml_close_tag(root, d, s))
|
|
|
|
return &root->xml;
|
|
|
|
if (isspace((int) (*s = q)))
|
|
|
|
s += strspn(s, SWITCH_XML_WS);
|
2009-03-05 04:04:41 +00:00
|
|
|
} else if (!strncmp(s, "!--", 3)) { /* xml comment */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!(s = strstr(s + 3, "--")) || (*(s += 2) != '>' && *s) || (!*s && e != '>'))
|
|
|
|
return switch_xml_err(root, d, "unclosed <!--");
|
2009-03-05 04:04:41 +00:00
|
|
|
} else if (!strncmp(s, "![CDATA[", 8)) { /* cdata */
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((s = strstr(s, "]]>")))
|
|
|
|
switch_xml_char_content(root, d + 8, (s += 2) - d - 10, 'c');
|
|
|
|
else
|
|
|
|
return switch_xml_err(root, d, "unclosed <![CDATA[");
|
2009-03-05 04:04:41 +00:00
|
|
|
} else if (!strncmp(s, "!DOCTYPE", 8)) { /* dtd */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (l = 0; *s && ((!l && *s != '>') || (l && (*s != ']' || *(s + strspn(s + 1, SWITCH_XML_WS) + 1) != '>'))); l = (*s == '[') ? 1 : l)
|
|
|
|
s += strcspn(s + 1, "[]>") + 1;
|
|
|
|
if (!*s && e != '>')
|
|
|
|
return switch_xml_err(root, d, "unclosed <!DOCTYPE");
|
|
|
|
d = (l) ? strchr(d, '[') + 1 : d;
|
|
|
|
if (l && !switch_xml_internal_dtd(root, d, s++ - d))
|
|
|
|
return &root->xml;
|
2009-03-05 04:04:41 +00:00
|
|
|
} else if (*s == '?') { /* <?...?> processing instructions */
|
2008-07-08 21:52:38 +00:00
|
|
|
do {
|
|
|
|
s = strchr(s, '?');
|
|
|
|
} while (s && *(++s) && *s != '>');
|
|
|
|
if (!s || (!*s && e != '>'))
|
|
|
|
return switch_xml_err(root, d, "unclosed <?");
|
|
|
|
else
|
|
|
|
switch_xml_proc_inst(root, d + 1, s - d - 2);
|
|
|
|
} else
|
|
|
|
return switch_xml_err(root, d, "unexpected <");
|
|
|
|
|
|
|
|
if (!s || !*s)
|
|
|
|
break;
|
|
|
|
*s = '\0';
|
|
|
|
d = ++s;
|
2009-03-05 04:04:41 +00:00
|
|
|
if (*s && *s != '<') { /* tag character content */
|
2008-07-08 21:52:38 +00:00
|
|
|
while (*s && *s != '<')
|
|
|
|
s++;
|
|
|
|
if (*s)
|
|
|
|
switch_xml_char_content(root, d, s - d, '&');
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} else if (!*s)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!root->cur)
|
|
|
|
return &root->xml;
|
|
|
|
else if (!root->cur->name)
|
|
|
|
return switch_xml_err(root, d, "root tag missing");
|
|
|
|
else
|
|
|
|
return switch_xml_err(root, d, "unclosed tag <%s>", root->cur->name);
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* Wrapper for switch_xml_parse_str() that accepts a file stream. Reads the entire
|
|
|
|
stream into memory and then parses it. For xml files, use switch_xml_parse_file()
|
|
|
|
or switch_xml_parse_fd() */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_parse_fp(FILE * fp)
|
|
|
|
{
|
|
|
|
switch_xml_root_t root;
|
|
|
|
switch_size_t l, len = 0;
|
|
|
|
char *s;
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(s = (char *) malloc(SWITCH_XML_BUFSIZE)))
|
2008-07-08 21:52:38 +00:00
|
|
|
return NULL;
|
|
|
|
do {
|
|
|
|
len += (l = fread((s + len), 1, SWITCH_XML_BUFSIZE, fp));
|
|
|
|
if (l == SWITCH_XML_BUFSIZE) {
|
2011-06-02 03:36:19 +00:00
|
|
|
char *tmp = s;
|
|
|
|
s = (char *) realloc(s, len + SWITCH_XML_BUFSIZE);
|
|
|
|
if (!s) {
|
|
|
|
free(tmp);
|
2008-07-08 21:52:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (s && l == SWITCH_XML_BUFSIZE);
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
root = (switch_xml_root_t) switch_xml_parse_str(s, len);
|
2009-03-05 04:04:41 +00:00
|
|
|
root->dynamic = 1; /* so we know to free s in switch_xml_free() */
|
2008-07-08 21:52:38 +00:00
|
|
|
return &root->xml;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* A wrapper for switch_xml_parse_str() that accepts a file descriptor. First
|
|
|
|
attempts to mem map the file. Failing that, reads the file into memory.
|
|
|
|
Returns NULL on failure. */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_parse_fd(int fd)
|
|
|
|
{
|
|
|
|
switch_xml_root_t root;
|
|
|
|
struct stat st;
|
|
|
|
switch_size_t l;
|
|
|
|
void *m;
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
fstat(fd, &st);
|
|
|
|
|
2010-06-03 17:34:28 +00:00
|
|
|
if (!st.st_size) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:45:37 +00:00
|
|
|
m = malloc(st.st_size);
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
l = read(fd, m, st.st_size);
|
|
|
|
if (!l || !(root = (switch_xml_root_t) switch_xml_parse_str((char *) m, l))) {
|
|
|
|
free(m);
|
|
|
|
return NULL;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
2011-03-18 20:45:37 +00:00
|
|
|
root->dynamic = 1; /* so we know to free s in switch_xml_free() */
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
return &root->xml;
|
|
|
|
}
|
|
|
|
|
2009-05-26 18:04:22 +00:00
|
|
|
static char *expand_vars(char *buf, char *ebuf, switch_size_t elen, switch_size_t *newlen, const char **err)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
char *var, *val;
|
|
|
|
char *rp = buf;
|
|
|
|
char *wp = ebuf;
|
|
|
|
char *ep = ebuf + elen - 1;
|
|
|
|
|
|
|
|
if (!(var = strstr(rp, "$${"))) {
|
|
|
|
*newlen = strlen(buf);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*rp && wp < ep) {
|
|
|
|
|
|
|
|
if (*rp == '$' && *(rp + 1) == '$' && *(rp + 2) == '{') {
|
2009-05-19 15:38:45 +00:00
|
|
|
char *e = switch_find_end_paren(rp + 2, '{', '}');
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
if (e) {
|
|
|
|
rp += 3;
|
|
|
|
var = rp;
|
|
|
|
*e++ = '\0';
|
|
|
|
rp = e;
|
2011-02-02 21:43:26 +00:00
|
|
|
if ((val = switch_core_get_variable_dup(var))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
char *p;
|
|
|
|
for (p = val; p && *p && wp <= ep; p++) {
|
|
|
|
*wp++ = *p;
|
|
|
|
}
|
2011-02-02 21:43:26 +00:00
|
|
|
free(val);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
2009-05-19 15:38:45 +00:00
|
|
|
continue;
|
2009-05-26 18:04:22 +00:00
|
|
|
} else if (err) {
|
|
|
|
*err = "unterminated ${var}";
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*wp++ = *rp++;
|
|
|
|
}
|
|
|
|
*wp++ = '\0';
|
|
|
|
*newlen = strlen(ebuf);
|
|
|
|
|
|
|
|
return ebuf;
|
|
|
|
}
|
|
|
|
|
2008-11-15 02:28:20 +00:00
|
|
|
static int preprocess_exec(const char *cwd, const char *command, int write_fd, int rlevel)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
2008-11-15 22:24:27 +00:00
|
|
|
char message[] = "<!-- exec not implemented in windows yet -->";
|
2008-11-15 02:28:20 +00:00
|
|
|
|
|
|
|
if (write(write_fd, message, sizeof(message)) < 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int fds[2], pid = 0;
|
|
|
|
|
|
|
|
if (pipe(fds)) {
|
|
|
|
goto end;
|
2010-02-06 03:38:24 +00:00
|
|
|
} else { /* good to go */
|
2008-11-15 02:28:20 +00:00
|
|
|
pid = fork();
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (pid < 0) { /* ok maybe not */
|
2008-11-15 02:28:20 +00:00
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
|
|
|
goto end;
|
2010-02-06 03:38:24 +00:00
|
|
|
} else if (pid) { /* parent */
|
2008-11-15 02:28:20 +00:00
|
|
|
char buf[1024] = "";
|
|
|
|
int bytes;
|
|
|
|
close(fds[1]);
|
|
|
|
while ((bytes = read(fds[0], buf, sizeof(buf))) > 0) {
|
|
|
|
if (write(write_fd, buf, bytes) <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fds[0]);
|
2011-03-29 23:05:05 +00:00
|
|
|
waitpid(pid, NULL, 0);
|
2010-02-06 03:38:24 +00:00
|
|
|
} else { /* child */
|
2008-11-15 02:28:20 +00:00
|
|
|
close(fds[0]);
|
|
|
|
dup2(fds[1], STDOUT_FILENO);
|
2008-11-17 22:03:49 +00:00
|
|
|
switch_system(command, SWITCH_TRUE);
|
2008-11-15 02:28:20 +00:00
|
|
|
close(fds[1]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-02-06 03:38:24 +00:00
|
|
|
end:
|
2008-11-15 02:28:20 +00:00
|
|
|
|
|
|
|
return write_fd;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-11-15 02:28:20 +00:00
|
|
|
}
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
static int preprocess_glob(const char *cwd, const char *pattern, int write_fd, int rlevel)
|
|
|
|
{
|
|
|
|
char *full_path = NULL;
|
|
|
|
char *dir_path = NULL, *e = NULL;
|
2010-02-06 03:38:24 +00:00
|
|
|
glob_t glob_data;
|
2008-07-08 21:52:38 +00:00
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (!switch_is_file_path(pattern)) {
|
|
|
|
full_path = switch_mprintf("%s%s%s", cwd, SWITCH_PATH_SEPARATOR, pattern);
|
|
|
|
pattern = full_path;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
|
|
|
if (glob(pattern, GLOB_NOCHECK, NULL, &glob_data) != 0) {
|
2011-02-01 06:33:08 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error including %s\n", pattern);
|
2008-07-08 21:52:38 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < glob_data.gl_pathc; ++n) {
|
|
|
|
dir_path = strdup(glob_data.gl_pathv[n]);
|
|
|
|
switch_assert(dir_path);
|
|
|
|
if ((e = strrchr(dir_path, *SWITCH_PATH_SEPARATOR))) {
|
|
|
|
*e = '\0';
|
|
|
|
}
|
|
|
|
if (preprocess(dir_path, glob_data.gl_pathv[n], write_fd, rlevel) < 0) {
|
|
|
|
if (rlevel > 100) {
|
2011-02-01 06:33:08 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error including %s (Maximum recursion limit reached)\n", pattern);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-22 17:00:16 +00:00
|
|
|
free(dir_path);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
globfree(&glob_data);
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
end:
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
switch_safe_free(full_path);
|
|
|
|
|
|
|
|
return write_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int preprocess(const char *cwd, const char *file, int write_fd, int rlevel)
|
|
|
|
{
|
|
|
|
int read_fd = -1;
|
|
|
|
switch_size_t cur = 0, ml = 0;
|
|
|
|
char *q, *cmd, buf[2048], ebuf[8192];
|
|
|
|
char *tcmd, *targ;
|
2009-05-26 18:04:22 +00:00
|
|
|
int line = 0;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((read_fd = open(file, O_RDONLY, 0)) < 0) {
|
2009-04-08 07:02:16 +00:00
|
|
|
const char *reason = strerror(errno);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldnt open %s (%s)\n", file, reason);
|
2008-07-08 21:52:38 +00:00
|
|
|
return read_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rlevel > 100) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((cur = switch_fd_read_line(read_fd, buf, sizeof(buf))) > 0) {
|
|
|
|
char *arg, *e;
|
2009-05-26 18:04:22 +00:00
|
|
|
const char *err = NULL;
|
|
|
|
char *bp = expand_vars(buf, ebuf, sizeof(ebuf), &cur, &err);
|
|
|
|
line++;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2009-06-02 22:03:33 +00:00
|
|
|
if (err) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error [%s] in file %s line %d\n", err, file, line);
|
2009-05-26 18:04:22 +00:00
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
/* we ignore <include> or </include> for the sake of validators as well as <?xml version="1.0"?> type stuff */
|
|
|
|
if (strstr(buf, "<include>") || strstr(buf, "</include>") || strstr(buf, "<?")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ml) {
|
|
|
|
if ((e = strstr(buf, "-->"))) {
|
|
|
|
ml = 0;
|
|
|
|
bp = e + 3;
|
|
|
|
cur = strlen(bp);
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if ((tcmd = (char *) switch_stristr("X-pre-process", bp))) {
|
|
|
|
if (*(tcmd - 1) != '<') {
|
2008-07-08 21:52:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((e = strstr(tcmd, "/>"))) {
|
|
|
|
*e += 2;
|
|
|
|
*e = '\0';
|
2008-10-06 19:21:59 +00:00
|
|
|
if (write(write_fd, e, (unsigned) strlen(e)) != (int) strlen(e)) {
|
2008-07-08 21:52:38 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Short write!\n");
|
|
|
|
}
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
|
|
|
if (!(tcmd = (char *) switch_stristr("cmd", tcmd))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(tcmd = (char *) switch_stristr("=", tcmd))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(tcmd = (char *) switch_stristr("\"", tcmd))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
tcmd++;
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((e = strchr(tcmd, '"'))) {
|
|
|
|
*e++ = '\0';
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(targ = (char *) switch_stristr("data", e))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(targ = (char *) switch_stristr("=", targ))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(targ = (char *) switch_stristr("\"", targ))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
targ++;
|
|
|
|
|
|
|
|
if ((e = strchr(targ, '"'))) {
|
|
|
|
*e++ = '\0';
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!strcasecmp(tcmd, "set")) {
|
2010-02-06 03:38:24 +00:00
|
|
|
char *name = (char *) targ;
|
2008-07-08 21:52:38 +00:00
|
|
|
char *val = strchr(name, '=');
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (val) {
|
|
|
|
char *ve = val++;
|
|
|
|
while (*val && *val == ' ') {
|
2010-02-06 03:38:24 +00:00
|
|
|
val++;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
*ve-- = '\0';
|
|
|
|
while (*ve && *ve == ' ') {
|
|
|
|
*ve-- = '\0';
|
|
|
|
}
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (name && val) {
|
|
|
|
switch_core_set_variable(name, val);
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
} else if (!strcasecmp(tcmd, "include")) {
|
|
|
|
preprocess_glob(cwd, targ, write_fd, rlevel + 1);
|
2008-11-15 02:28:20 +00:00
|
|
|
} else if (!strcasecmp(tcmd, "exec")) {
|
|
|
|
preprocess_exec(cwd, targ, write_fd, rlevel + 1);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((cmd = strstr(bp, "<!--#"))) {
|
2008-10-06 19:21:59 +00:00
|
|
|
if (write(write_fd, bp, (unsigned) (cmd - bp)) != (cmd - bp)) {
|
2008-07-08 21:52:38 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Short write!\n");
|
|
|
|
}
|
|
|
|
if ((e = strstr(cmd, "-->"))) {
|
|
|
|
*e = '\0';
|
|
|
|
e += 3;
|
2008-10-06 19:21:59 +00:00
|
|
|
if (write(write_fd, e, (unsigned) strlen(e)) != (int) strlen(e)) {
|
2008-07-08 21:52:38 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Short write!\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ml++;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd += 5;
|
|
|
|
if ((e = strchr(cmd, '\r')) || (e = strchr(cmd, '\n'))) {
|
|
|
|
*e = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((arg = strchr(cmd, ' '))) {
|
|
|
|
*arg++ = '\0';
|
|
|
|
if ((q = strchr(arg, '"'))) {
|
|
|
|
char *qq = q + 1;
|
|
|
|
|
|
|
|
if ((qq = strchr(qq, '"'))) {
|
|
|
|
*qq = '\0';
|
|
|
|
arg = q + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "set")) {
|
|
|
|
char *name = arg;
|
|
|
|
char *val = strchr(name, '=');
|
|
|
|
|
|
|
|
if (val) {
|
|
|
|
char *ve = val++;
|
|
|
|
while (*val && *val == ' ') {
|
|
|
|
val++;
|
|
|
|
}
|
|
|
|
*ve-- = '\0';
|
|
|
|
while (*ve && *ve == ' ') {
|
|
|
|
*ve-- = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name && val) {
|
|
|
|
switch_core_set_variable(name, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (!strcasecmp(cmd, "include")) {
|
|
|
|
preprocess_glob(cwd, arg, write_fd, rlevel + 1);
|
2008-11-15 02:28:20 +00:00
|
|
|
} else if (!strcasecmp(cmd, "exec")) {
|
|
|
|
preprocess_exec(cwd, arg, write_fd, rlevel + 1);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-10-06 19:21:59 +00:00
|
|
|
if (write(write_fd, bp, (unsigned) cur) != (int) cur) {
|
2008-07-08 21:52:38 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Short write!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(read_fd);
|
|
|
|
return write_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_parse_file_simple(const char *file)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
struct stat st;
|
|
|
|
switch_size_t l;
|
|
|
|
void *m;
|
|
|
|
switch_xml_root_t root;
|
|
|
|
|
|
|
|
if ((fd = open(file, O_RDONLY, 0)) > -1) {
|
|
|
|
fstat(fd, &st);
|
2010-05-14 16:27:29 +00:00
|
|
|
if (!st.st_size) goto error;
|
2008-07-08 21:52:38 +00:00
|
|
|
m = malloc(st.st_size);
|
|
|
|
switch_assert(m);
|
2010-05-14 16:27:29 +00:00
|
|
|
if (!(l = read(fd, m, st.st_size))) goto error;
|
|
|
|
if (!(root = (switch_xml_root_t) switch_xml_parse_str((char *) m, l))) goto error;
|
2008-07-08 21:52:38 +00:00
|
|
|
root->dynamic = 1;
|
|
|
|
close(fd);
|
|
|
|
return &root->xml;
|
|
|
|
}
|
2010-05-14 16:27:29 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing File [%s]\n", file);
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_parse_file(const char *file)
|
|
|
|
{
|
|
|
|
int fd = -1, write_fd = -1;
|
|
|
|
switch_xml_t xml = NULL;
|
|
|
|
char *new_file = NULL;
|
2010-01-06 15:57:36 +00:00
|
|
|
const char *abs, *absw;
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
abs = strrchr(file, '/');
|
|
|
|
absw = strrchr(file, '\\');
|
2010-01-06 15:57:36 +00:00
|
|
|
if (abs || absw) {
|
|
|
|
abs > absw ? abs++ : (abs = ++absw);
|
2008-07-08 21:52:38 +00:00
|
|
|
} else {
|
|
|
|
abs = file;
|
|
|
|
}
|
2011-03-31 21:28:12 +00:00
|
|
|
|
|
|
|
switch_mutex_lock(FILE_LOCK);
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!(new_file = switch_mprintf("%s%s%s.fsxml", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, abs))) {
|
2011-03-31 21:28:12 +00:00
|
|
|
goto done;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((write_fd = open(new_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) < 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preprocess(SWITCH_GLOBAL_dirs.conf_dir, file, write_fd, 0) > -1) {
|
|
|
|
close(write_fd);
|
|
|
|
write_fd = -1;
|
|
|
|
if ((fd = open(new_file, O_RDONLY, 0)) > -1) {
|
|
|
|
if ((xml = switch_xml_parse_fd(fd))) {
|
|
|
|
xml->free_path = new_file;
|
|
|
|
new_file = NULL;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2011-03-31 21:28:12 +00:00
|
|
|
|
|
|
|
switch_mutex_unlock(FILE_LOCK);
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (write_fd > -1) {
|
|
|
|
close(write_fd);
|
|
|
|
}
|
2011-03-31 21:28:12 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (fd > -1) {
|
|
|
|
close(fd);
|
|
|
|
}
|
2011-03-31 21:28:12 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
switch_safe_free(new_file);
|
2011-03-31 21:28:12 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_locate(const char *section,
|
|
|
|
const char *tag_name,
|
2010-02-06 03:38:24 +00:00
|
|
|
const char *key_name,
|
|
|
|
const char *key_value,
|
|
|
|
switch_xml_t *root, switch_xml_t *node, switch_event_t *params, switch_bool_t clone)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
switch_xml_t conf = NULL;
|
|
|
|
switch_xml_t tag = NULL;
|
|
|
|
switch_xml_t xml = NULL;
|
|
|
|
switch_xml_binding_t *binding;
|
|
|
|
uint8_t loops = 0;
|
2009-06-10 05:03:07 +00:00
|
|
|
switch_xml_section_t sections = BINDINGS ? switch_xml_parse_section_string(section) : 0;
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2008-09-09 22:47:37 +00:00
|
|
|
switch_thread_rwlock_rdlock(B_RWLOCK);
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
for (binding = BINDINGS; binding; binding = binding->next) {
|
|
|
|
if (binding->sections && !(sections & binding->sections)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((xml = binding->function(section, tag_name, key_name, key_value, params, binding->user_data))) {
|
|
|
|
const char *err = NULL;
|
|
|
|
|
|
|
|
err = switch_xml_error(xml);
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(err)) {
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((conf = switch_xml_find_child(xml, "section", "name", "result"))) {
|
|
|
|
switch_xml_t p;
|
|
|
|
const char *aname;
|
|
|
|
|
|
|
|
if ((p = switch_xml_child(conf, "result"))) {
|
|
|
|
aname = switch_xml_attr(p, "status");
|
|
|
|
if (aname && !strcasecmp(aname, "not found")) {
|
|
|
|
switch_xml_free(xml);
|
|
|
|
xml = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error[%s]\n", err);
|
|
|
|
switch_xml_free(xml);
|
|
|
|
xml = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-09 22:47:37 +00:00
|
|
|
switch_thread_rwlock_unlock(B_RWLOCK);
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!xml) {
|
|
|
|
if (!(xml = switch_xml_root())) {
|
|
|
|
*node = NULL;
|
|
|
|
*root = NULL;
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((conf = switch_xml_find_child(xml, "section", "name", section)) && (tag = switch_xml_find_child(conf, tag_name, key_name, key_value))) {
|
2009-02-12 00:27:07 +00:00
|
|
|
if (clone) {
|
2009-05-07 21:40:21 +00:00
|
|
|
char *x = switch_xml_toxml(tag, SWITCH_FALSE);
|
2009-02-12 00:27:07 +00:00
|
|
|
switch_assert(x);
|
2010-02-06 03:38:24 +00:00
|
|
|
*node = *root = switch_xml_parse_str_dynamic(x, SWITCH_FALSE); /* x will be free()'d in switch_xml_free() */
|
2009-02-12 00:27:07 +00:00
|
|
|
switch_xml_free(xml);
|
|
|
|
} else {
|
|
|
|
*node = tag;
|
|
|
|
*root = xml;
|
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
} else {
|
|
|
|
switch_xml_free(xml);
|
|
|
|
xml = NULL;
|
|
|
|
*node = NULL;
|
|
|
|
*root = NULL;
|
|
|
|
if (loops++ > 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
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)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
switch_event_t *my_params = NULL;
|
|
|
|
switch_status_t status;
|
|
|
|
*domain = NULL;
|
|
|
|
|
|
|
|
if (!params) {
|
2008-10-02 17:10:05 +00:00
|
|
|
switch_event_create(&my_params, SWITCH_EVENT_REQUEST_PARAMS);
|
2008-07-08 21:52:38 +00:00
|
|
|
switch_assert(my_params);
|
|
|
|
switch_event_add_header_string(my_params, SWITCH_STACK_BOTTOM, "domain", domain_name);
|
|
|
|
params = my_params;
|
|
|
|
}
|
|
|
|
|
2009-02-12 00:27:07 +00:00
|
|
|
status = switch_xml_locate("directory", "domain", "name", domain_name, root, domain, params, SWITCH_FALSE);
|
2008-07-08 21:52:38 +00:00
|
|
|
if (my_params) {
|
|
|
|
switch_event_destroy(&my_params);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2008-12-23 17:36:50 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_locate_group(const char *group_name,
|
2010-02-06 03:38:24 +00:00
|
|
|
const char *domain_name,
|
|
|
|
switch_xml_t *root, switch_xml_t *domain, switch_xml_t *group, switch_event_t *params)
|
2008-12-23 17:36:50 +00:00
|
|
|
{
|
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
|
|
|
switch_event_t *my_params = NULL;
|
|
|
|
switch_xml_t groups = NULL;
|
|
|
|
|
|
|
|
*root = NULL;
|
|
|
|
*group = NULL;
|
|
|
|
*domain = NULL;
|
|
|
|
|
|
|
|
if (!params) {
|
|
|
|
switch_event_create(&my_params, SWITCH_EVENT_REQUEST_PARAMS);
|
|
|
|
switch_assert(my_params);
|
|
|
|
params = my_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group_name) {
|
|
|
|
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "group_name", group_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (domain_name) {
|
|
|
|
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "domain", domain_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status = switch_xml_locate_domain(domain_name, params, root, domain)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-12-23 17:36:50 +00:00
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
|
|
|
|
if ((groups = switch_xml_child(*domain, "groups"))) {
|
|
|
|
if ((*group = switch_xml_find_child(groups, "group", "name", group_name))) {
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
end:
|
2008-12-23 17:36:50 +00:00
|
|
|
|
|
|
|
if (my_params) {
|
|
|
|
switch_event_destroy(&my_params);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
static switch_status_t find_user_in_tag(switch_xml_t tag, const char *ip, const char *user_name,
|
|
|
|
const char *key, switch_event_t *params, switch_xml_t *user)
|
2008-12-23 17:36:50 +00:00
|
|
|
{
|
|
|
|
const char *type = "!pointer";
|
|
|
|
const char *val;
|
|
|
|
|
|
|
|
if (params && (val = switch_event_get_header(params, "user_type"))) {
|
|
|
|
if (!strcasecmp(val, "any")) {
|
|
|
|
type = NULL;
|
|
|
|
} else {
|
|
|
|
type = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ip) {
|
|
|
|
if ((*user = switch_xml_find_child_multi(tag, "user", "ip", ip, "type", type, NULL))) {
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
}
|
2008-12-23 17:36:50 +00:00
|
|
|
|
|
|
|
if (user_name) {
|
2009-05-18 17:06:13 +00:00
|
|
|
if (!strcasecmp(key, "id")) {
|
|
|
|
if ((*user = switch_xml_find_child_multi(tag, "user", key, user_name, "number-alias", user_name, "type", type, NULL))) {
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((*user = switch_xml_find_child_multi(tag, "user", key, user_name, "type", type, NULL))) {
|
2008-12-23 17:36:50 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-12-23 17:36:50 +00:00
|
|
|
return SWITCH_STATUS_FALSE;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-12-23 17:36:50 +00:00
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_locate_user_in_domain(const char *user_name, switch_xml_t domain, switch_xml_t *user, switch_xml_t *ingroup)
|
2008-12-29 22:26:10 +00:00
|
|
|
{
|
|
|
|
switch_xml_t group = NULL, groups = NULL, users = NULL;
|
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-12-29 22:26:10 +00:00
|
|
|
if ((groups = switch_xml_child(domain, "groups"))) {
|
|
|
|
for (group = switch_xml_child(groups, "group"); group; group = group->next) {
|
|
|
|
if ((users = switch_xml_child(group, "users"))) {
|
|
|
|
if ((status = find_user_in_tag(users, NULL, user_name, "id", NULL, user)) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
if (ingroup) {
|
|
|
|
*ingroup = group;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2008-12-23 17:36:50 +00:00
|
|
|
|
2010-02-11 17:32:22 +00:00
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_dup(switch_xml_t xml)
|
|
|
|
{
|
|
|
|
char *x = switch_xml_toxml(xml, SWITCH_FALSE);
|
|
|
|
return switch_xml_parse_str_dynamic(x, SWITCH_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void do_merge(switch_xml_t in, switch_xml_t src, const char *container, const char *tag_name)
|
|
|
|
{
|
|
|
|
switch_xml_t itag, tag, param, iparam, iitag;
|
|
|
|
|
|
|
|
if (!(itag = switch_xml_child(in, container))) {
|
|
|
|
itag = switch_xml_add_child_d(in, container, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tag = switch_xml_child(src, container))) {
|
|
|
|
for (param = switch_xml_child(tag, tag_name); param; param = param->next) {
|
|
|
|
const char *var = switch_xml_attr(param, "name");
|
|
|
|
const char *val = switch_xml_attr(param, "value");
|
|
|
|
|
|
|
|
int go = 1;
|
|
|
|
|
|
|
|
for (iparam = switch_xml_child(itag, tag_name); iparam; iparam = iparam->next) {
|
|
|
|
const char *ivar = switch_xml_attr(iparam, "name");
|
|
|
|
|
|
|
|
if (var && ivar && !strcasecmp(var, ivar)) {
|
|
|
|
go = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (go) {
|
|
|
|
iitag = switch_xml_add_child_d(itag, tag_name, 0);
|
2010-02-15 17:40:13 +00:00
|
|
|
switch_xml_set_attr_d(iitag, "name", var);
|
|
|
|
switch_xml_set_attr_d(iitag, "value", val);
|
2010-02-11 17:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_DECLARE(void) switch_xml_merge_user(switch_xml_t user, switch_xml_t domain, switch_xml_t group)
|
|
|
|
{
|
|
|
|
|
|
|
|
do_merge(user, group, "params", "param");
|
|
|
|
do_merge(user, group, "variables", "variable");
|
|
|
|
do_merge(user, domain, "params", "param");
|
|
|
|
do_merge(user, domain, "variables", "variable");
|
|
|
|
}
|
|
|
|
|
2011-05-23 22:15:32 +00:00
|
|
|
SWITCH_DECLARE(uint32_t) switch_xml_clear_user_cache(const char *key, const char *user_name, const char *domain_name)
|
|
|
|
{
|
|
|
|
switch_hash_index_t *hi;
|
|
|
|
void *val;
|
|
|
|
const void *var;
|
|
|
|
char mega_key[1024];
|
|
|
|
int r = 0;
|
|
|
|
switch_xml_t lookup;
|
|
|
|
|
|
|
|
switch_mutex_lock(CACHE_MUTEX);
|
|
|
|
|
|
|
|
if (key && user_name && domain_name) {
|
|
|
|
switch_snprintf(mega_key, sizeof(mega_key), "%s%s%s", key, user_name, domain_name);
|
|
|
|
|
|
|
|
if ((lookup = switch_core_hash_find(CACHE_HASH, mega_key))) {
|
|
|
|
switch_core_hash_delete(CACHE_HASH, mega_key);
|
|
|
|
switch_xml_free(lookup);
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2011-05-24 03:51:43 +00:00
|
|
|
while ((hi = switch_hash_first(NULL, CACHE_HASH))) {
|
2011-05-23 22:15:32 +00:00
|
|
|
switch_hash_this(hi, &var, NULL, &val);
|
|
|
|
switch_xml_free(val);
|
|
|
|
switch_core_hash_delete(CACHE_HASH, var);
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_mutex_unlock(CACHE_MUTEX);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static switch_status_t switch_xml_locate_user_cache(const char *key, const char *user_name, const char *domain_name, switch_xml_t *user)
|
|
|
|
{
|
|
|
|
char mega_key[1024];
|
|
|
|
switch_xml_t lookup;
|
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
|
|
|
|
|
|
|
switch_snprintf(mega_key, sizeof(mega_key), "%s%s%s", key, user_name, domain_name);
|
|
|
|
|
|
|
|
switch_mutex_lock(CACHE_MUTEX);
|
|
|
|
if ((lookup = switch_core_hash_find(CACHE_HASH, mega_key))) {
|
|
|
|
*user = switch_xml_dup(lookup);
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
switch_mutex_unlock(CACHE_MUTEX);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void switch_xml_user_cache(const char *key, const char *user_name, const char *domain_name, switch_xml_t user)
|
|
|
|
{
|
|
|
|
char mega_key[1024];
|
|
|
|
switch_xml_t lookup;
|
|
|
|
|
|
|
|
switch_snprintf(mega_key, sizeof(mega_key), "%s%s%s", key, user_name, domain_name);
|
|
|
|
switch_mutex_lock(CACHE_MUTEX);
|
|
|
|
if ((lookup = switch_core_hash_find(CACHE_HASH, mega_key))) {
|
|
|
|
switch_core_hash_delete(CACHE_HASH, mega_key);
|
|
|
|
switch_xml_free(lookup);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_core_hash_insert(CACHE_HASH, mega_key, switch_xml_dup(user));
|
|
|
|
switch_mutex_unlock(CACHE_MUTEX);
|
|
|
|
}
|
|
|
|
|
2010-02-11 17:32:22 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_locate_user_merged(const char *key, const char *user_name, const char *domain_name,
|
|
|
|
const char *ip, switch_xml_t *user, switch_event_t *params)
|
|
|
|
{
|
|
|
|
switch_xml_t xml, domain, group, x_user, x_user_dup;
|
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
|
|
|
|
2011-05-23 22:15:32 +00:00
|
|
|
if ((status = switch_xml_locate_user_cache(key, user_name, domain_name, &x_user)) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
*user = x_user;
|
|
|
|
} else if ((status = switch_xml_locate_user(key, user_name, domain_name, ip, &xml, &domain, &x_user, &group, params)) == SWITCH_STATUS_SUCCESS) {
|
2010-02-11 17:32:22 +00:00
|
|
|
x_user_dup = switch_xml_dup(x_user);
|
|
|
|
switch_xml_merge_user(x_user_dup, domain, group);
|
2011-05-23 22:15:32 +00:00
|
|
|
if (switch_true(switch_xml_attr(x_user_dup, "cacheable"))) {
|
|
|
|
switch_xml_user_cache(key, user_name, domain_name, x_user_dup);
|
|
|
|
}
|
2010-02-11 17:32:22 +00:00
|
|
|
*user = x_user_dup;
|
2010-02-15 17:40:13 +00:00
|
|
|
switch_xml_free(xml);
|
2010-02-11 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_locate_user(const char *key,
|
|
|
|
const char *user_name,
|
2010-02-06 03:38:24 +00:00
|
|
|
const char *domain_name,
|
|
|
|
const char *ip,
|
2008-07-08 21:52:38 +00:00
|
|
|
switch_xml_t *root,
|
2010-02-06 03:38:24 +00:00
|
|
|
switch_xml_t *domain, switch_xml_t *user, switch_xml_t *ingroup, switch_event_t *params)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
2008-07-29 15:42:05 +00:00
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
2011-04-22 21:43:29 +00:00
|
|
|
switch_event_t *my_params = NULL;
|
2008-12-23 17:36:50 +00:00
|
|
|
switch_xml_t group = NULL, groups = NULL, users = NULL;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
*root = NULL;
|
|
|
|
*user = NULL;
|
|
|
|
*domain = NULL;
|
|
|
|
|
2008-12-23 19:47:50 +00:00
|
|
|
if (ingroup) {
|
|
|
|
*ingroup = NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-29 15:42:05 +00:00
|
|
|
if (!params) {
|
2008-10-02 17:10:05 +00:00
|
|
|
switch_event_create(&my_params, SWITCH_EVENT_REQUEST_PARAMS);
|
2008-07-29 15:42:05 +00:00
|
|
|
switch_assert(my_params);
|
|
|
|
params = my_params;
|
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2008-07-29 15:42:05 +00:00
|
|
|
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "key", key);
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2008-07-29 15:42:05 +00:00
|
|
|
if (user_name) {
|
|
|
|
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "user", user_name);
|
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2008-07-29 15:42:05 +00:00
|
|
|
if (domain_name) {
|
|
|
|
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "domain", domain_name);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2008-07-29 15:42:05 +00:00
|
|
|
if (ip) {
|
|
|
|
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "ip", ip);
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((status = switch_xml_locate_domain(domain_name, params, root, domain)) != SWITCH_STATUS_SUCCESS) {
|
2008-07-29 15:42:05 +00:00
|
|
|
goto end;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-08-06 19:56:44 +00:00
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
|
2008-12-23 17:36:50 +00:00
|
|
|
if ((groups = switch_xml_child(*domain, "groups"))) {
|
|
|
|
for (group = switch_xml_child(groups, "group"); group; group = group->next) {
|
|
|
|
if ((users = switch_xml_child(group, "users"))) {
|
|
|
|
if ((status = find_user_in_tag(users, ip, user_name, key, params, user)) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
if (ingroup) {
|
|
|
|
*ingroup = group;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-23 17:36:50 +00:00
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2008-12-23 17:36:50 +00:00
|
|
|
if (status != SWITCH_STATUS_SUCCESS) {
|
|
|
|
status = find_user_in_tag(*domain, ip, user_name, key, params, user);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
|
|
|
end:
|
2008-07-29 15:42:05 +00:00
|
|
|
|
|
|
|
if (my_params) {
|
|
|
|
switch_event_destroy(&my_params);
|
|
|
|
}
|
|
|
|
|
2009-02-24 23:30:17 +00:00
|
|
|
if (status != SWITCH_STATUS_SUCCESS && root && *root) {
|
|
|
|
switch_xml_free(*root);
|
|
|
|
*root = NULL;
|
|
|
|
*domain = NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-29 15:42:05 +00:00
|
|
|
return status;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_root(void)
|
|
|
|
{
|
2011-03-31 18:43:36 +00:00
|
|
|
switch_xml_t xml;
|
2011-03-18 20:41:27 +00:00
|
|
|
|
2011-03-31 18:43:36 +00:00
|
|
|
switch_mutex_lock(REFLOCK);
|
|
|
|
xml = MAIN_XML_ROOT;
|
|
|
|
xml->refs++;
|
|
|
|
switch_mutex_unlock(REFLOCK);
|
|
|
|
|
|
|
|
return xml;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2008-07-30 18:54:37 +00:00
|
|
|
struct destroy_xml {
|
|
|
|
switch_xml_t xml;
|
|
|
|
switch_memory_pool_t *pool;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *SWITCH_THREAD_FUNC destroy_thread(switch_thread_t *thread, void *obj)
|
|
|
|
{
|
|
|
|
struct destroy_xml *dx = (struct destroy_xml *) obj;
|
|
|
|
switch_memory_pool_t *pool = dx->pool;
|
|
|
|
switch_xml_free(dx->xml);
|
|
|
|
switch_core_destroy_memory_pool(&pool);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(void) switch_xml_free_in_thread(switch_xml_t xml, int stacksize)
|
|
|
|
{
|
|
|
|
switch_thread_t *thread;
|
2010-02-06 03:38:24 +00:00
|
|
|
switch_threadattr_t *thd_attr;
|
2008-07-30 18:54:37 +00:00
|
|
|
switch_memory_pool_t *pool = NULL;
|
|
|
|
struct destroy_xml *dx;
|
|
|
|
|
|
|
|
switch_core_new_memory_pool(&pool);
|
|
|
|
|
|
|
|
switch_threadattr_create(&thd_attr, pool);
|
2010-02-06 03:38:24 +00:00
|
|
|
switch_threadattr_detach_set(thd_attr, 1);
|
2009-03-05 04:04:41 +00:00
|
|
|
/* TBD figure out how much space we need by looking at the xml_t when stacksize == 0 */
|
2010-02-06 03:38:24 +00:00
|
|
|
switch_threadattr_stacksize_set(thd_attr, stacksize);
|
2008-07-30 18:54:37 +00:00
|
|
|
|
|
|
|
dx = switch_core_alloc(pool, sizeof(*dx));
|
|
|
|
dx->pool = pool;
|
|
|
|
dx->xml = xml;
|
|
|
|
|
|
|
|
switch_thread_create(&thread, thd_attr, destroy_thread, dx, pool);
|
|
|
|
}
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
static char not_so_threadsafe_error_buffer[256] = "";
|
|
|
|
|
2011-06-28 13:05:12 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_set_root(switch_xml_t new_main)
|
|
|
|
{
|
|
|
|
switch_xml_t old_root = NULL;
|
|
|
|
|
|
|
|
switch_mutex_lock(REFLOCK);
|
|
|
|
|
|
|
|
old_root = MAIN_XML_ROOT;
|
|
|
|
MAIN_XML_ROOT = new_main;
|
|
|
|
switch_set_flag(MAIN_XML_ROOT, SWITCH_XML_ROOT);
|
|
|
|
MAIN_XML_ROOT->refs++;
|
|
|
|
|
|
|
|
if (old_root) {
|
|
|
|
if (old_root->refs) {
|
|
|
|
old_root->refs--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!old_root->refs) {
|
|
|
|
switch_xml_free(old_root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_mutex_unlock(REFLOCK);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_set_open_root_function(switch_xml_open_root_function_t func, void *user_data)
|
|
|
|
{
|
|
|
|
if (XML_LOCK) {
|
|
|
|
switch_mutex_lock(XML_LOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
XML_OPEN_ROOT_FUNCTION = func;
|
|
|
|
XML_OPEN_ROOT_FUNCTION_USER_DATA = user_data;
|
|
|
|
|
|
|
|
if (XML_LOCK) {
|
|
|
|
switch_mutex_unlock(XML_LOCK);
|
|
|
|
}
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_open_root(uint8_t reload, const char **err)
|
|
|
|
{
|
|
|
|
switch_xml_t root = NULL;
|
|
|
|
|
|
|
|
switch_mutex_lock(XML_LOCK);
|
|
|
|
|
|
|
|
if (XML_OPEN_ROOT_FUNCTION) {
|
|
|
|
root = XML_OPEN_ROOT_FUNCTION(reload, err, XML_OPEN_ROOT_FUNCTION_USER_DATA);
|
|
|
|
}
|
|
|
|
switch_mutex_unlock(XML_LOCK);
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2011-06-29 16:30:31 +00:00
|
|
|
SWITCH_DECLARE_NONSTD(switch_xml_t) __switch_xml_open_root(uint8_t reload, const char **err, void *user_data)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
char path_buf[1024];
|
2010-12-01 15:54:28 +00:00
|
|
|
uint8_t errcnt = 0;
|
2011-03-31 18:43:36 +00:00
|
|
|
switch_xml_t new_main, r = NULL;
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2011-03-31 18:43:36 +00:00
|
|
|
if (MAIN_XML_ROOT) {
|
|
|
|
if (!reload) {
|
|
|
|
r = switch_xml_root();
|
|
|
|
goto done;
|
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch_snprintf(path_buf, sizeof(path_buf), "%s%s%s", SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR, "freeswitch.xml");
|
|
|
|
if ((new_main = switch_xml_parse_file(path_buf))) {
|
|
|
|
*err = switch_xml_error(new_main);
|
|
|
|
switch_copy_string(not_so_threadsafe_error_buffer, *err, sizeof(not_so_threadsafe_error_buffer));
|
|
|
|
*err = not_so_threadsafe_error_buffer;
|
2009-10-23 16:03:42 +00:00
|
|
|
if (!zstr(*err)) {
|
2008-07-08 21:52:38 +00:00
|
|
|
switch_xml_free(new_main);
|
|
|
|
new_main = NULL;
|
|
|
|
errcnt++;
|
|
|
|
} else {
|
|
|
|
*err = "Success";
|
2011-06-28 13:05:12 +00:00
|
|
|
switch_xml_set_root(new_main);
|
2011-03-18 20:41:27 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*err = "Cannot Open log directory or XML Root!";
|
|
|
|
errcnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errcnt == 0) {
|
|
|
|
switch_event_t *event;
|
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_RELOADXML) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
if (switch_event_fire(&event) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
}
|
|
|
|
}
|
2009-02-23 18:33:59 +00:00
|
|
|
r = switch_xml_root();
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2011-06-28 13:05:12 +00:00
|
|
|
done:
|
2011-03-31 18:43:36 +00:00
|
|
|
|
2009-02-23 18:33:59 +00:00
|
|
|
return r;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2010-09-16 22:38:06 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_reload(const char **err)
|
|
|
|
{
|
|
|
|
switch_xml_t xml_root;
|
|
|
|
|
|
|
|
if ((xml_root = switch_xml_open_root(1, err))) {
|
|
|
|
switch_xml_free(xml_root);
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_init(switch_memory_pool_t *pool, const char **err)
|
|
|
|
{
|
|
|
|
switch_xml_t xml;
|
|
|
|
XML_MEMORY_POOL = pool;
|
|
|
|
*err = "Success";
|
|
|
|
|
2011-05-23 22:15:32 +00:00
|
|
|
switch_mutex_init(&CACHE_MUTEX, SWITCH_MUTEX_NESTED, XML_MEMORY_POOL);
|
2011-03-31 18:43:36 +00:00
|
|
|
switch_mutex_init(&XML_LOCK, SWITCH_MUTEX_NESTED, XML_MEMORY_POOL);
|
|
|
|
switch_mutex_init(&REFLOCK, SWITCH_MUTEX_NESTED, XML_MEMORY_POOL);
|
2011-03-31 21:28:12 +00:00
|
|
|
switch_mutex_init(&FILE_LOCK, SWITCH_MUTEX_NESTED, XML_MEMORY_POOL);
|
2009-02-25 00:56:17 +00:00
|
|
|
switch_mutex_init(&XML_GEN_LOCK, SWITCH_MUTEX_NESTED, XML_MEMORY_POOL);
|
2011-05-23 22:15:32 +00:00
|
|
|
switch_core_hash_init(&CACHE_HASH, XML_MEMORY_POOL);
|
2011-03-31 18:43:36 +00:00
|
|
|
|
2008-09-09 22:47:37 +00:00
|
|
|
switch_thread_rwlock_create(&B_RWLOCK, XML_MEMORY_POOL);
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
assert(pool != NULL);
|
|
|
|
|
|
|
|
if ((xml = switch_xml_open_root(FALSE, err))) {
|
|
|
|
switch_xml_free(xml);
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
} else {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_destroy(void)
|
|
|
|
{
|
2009-02-23 18:33:59 +00:00
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
2011-03-31 21:28:12 +00:00
|
|
|
|
2011-05-23 22:15:32 +00:00
|
|
|
|
2011-03-31 18:43:36 +00:00
|
|
|
switch_mutex_lock(XML_LOCK);
|
2011-03-31 21:28:12 +00:00
|
|
|
switch_mutex_lock(REFLOCK);
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (MAIN_XML_ROOT) {
|
|
|
|
switch_xml_t xml = MAIN_XML_ROOT;
|
|
|
|
MAIN_XML_ROOT = NULL;
|
|
|
|
switch_xml_free(xml);
|
2009-02-23 18:33:59 +00:00
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2011-03-31 18:43:36 +00:00
|
|
|
switch_mutex_unlock(XML_LOCK);
|
2011-03-31 21:28:12 +00:00
|
|
|
switch_mutex_unlock(REFLOCK);
|
2009-02-23 18:33:59 +00:00
|
|
|
|
2011-05-23 22:15:32 +00:00
|
|
|
switch_xml_clear_user_cache(NULL, NULL, NULL);
|
|
|
|
|
|
|
|
switch_core_hash_destroy(&CACHE_HASH);
|
|
|
|
|
2009-02-23 18:33:59 +00:00
|
|
|
return status;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_open_cfg(const char *file_path, switch_xml_t *node, switch_event_t *params)
|
|
|
|
{
|
|
|
|
switch_xml_t xml = NULL, cfg = NULL;
|
|
|
|
|
|
|
|
*node = NULL;
|
|
|
|
|
|
|
|
assert(MAIN_XML_ROOT != NULL);
|
|
|
|
|
2009-02-12 00:27:07 +00:00
|
|
|
if (switch_xml_locate("configuration", "configuration", "name", file_path, &xml, &cfg, params, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
|
2008-07-08 21:52:38 +00:00
|
|
|
*node = cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return xml;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* Encodes ampersand sequences appending the results to *dst, reallocating *dst
|
|
|
|
if length exceeds max. a is non-zero for attribute encoding. Returns *dst */
|
2008-07-08 21:52:38 +00:00
|
|
|
static char *switch_xml_ampencode(const char *s, switch_size_t len, char **dst, switch_size_t *dlen, switch_size_t *max, short a)
|
|
|
|
{
|
|
|
|
const char *e = NULL;
|
|
|
|
int immune = 0;
|
2011-12-16 04:36:20 +00:00
|
|
|
int expecting_x_utf_8_char = 0;
|
|
|
|
int unicode_char = 0x000000;
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(s && *s))
|
|
|
|
return *dst;
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
if (len) {
|
|
|
|
e = s + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (s != e) {
|
|
|
|
while (*dlen + 10 > *max) {
|
2010-02-06 03:38:24 +00:00
|
|
|
char *tmp = (char *) realloc(*dst, *max += SWITCH_XML_BUFSIZE);
|
|
|
|
if (!tmp)
|
|
|
|
return *dst;
|
2008-07-08 21:52:38 +00:00
|
|
|
*dst = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (immune) {
|
|
|
|
if (*s == '\0') {
|
|
|
|
return *dst;
|
|
|
|
}
|
|
|
|
(*dst)[(*dlen)++] = *s;
|
2010-02-06 03:38:24 +00:00
|
|
|
} else
|
2008-07-08 21:52:38 +00:00
|
|
|
switch (*s) {
|
2010-02-06 03:38:24 +00:00
|
|
|
case '\0':
|
|
|
|
return *dst;
|
|
|
|
case '&':
|
|
|
|
*dlen += sprintf(*dst + *dlen, "&");
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
if (*(s + 1) == '!') {
|
|
|
|
(*dst)[(*dlen)++] = *s;
|
|
|
|
immune++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*dlen += sprintf(*dst + *dlen, "<");
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
*dlen += sprintf(*dst + *dlen, ">");
|
2008-07-08 21:52:38 +00:00
|
|
|
break;
|
2010-02-06 03:38:24 +00:00
|
|
|
case '"':
|
|
|
|
*dlen += sprintf(*dst + *dlen, (a) ? """ : "\"");
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
*dlen += sprintf(*dst + *dlen, (a) ? "
" : "\n");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
*dlen += sprintf(*dst + *dlen, (a) ? "	" : "\t");
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
*dlen += sprintf(*dst + *dlen, "
");
|
|
|
|
break;
|
|
|
|
default:
|
2011-12-16 04:36:20 +00:00
|
|
|
if (USE_UTF_8_ENCODING && expecting_x_utf_8_char == 0 && ((*s >> 8) & 0x01)) {
|
|
|
|
int num = 1;
|
|
|
|
for (;num<4;num++) {
|
|
|
|
if (! ((*s >> (7-num)) & 0x01)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (num) {
|
|
|
|
case 2:
|
|
|
|
unicode_char = *s & 0x1f;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
unicode_char = *s & 0x0f;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
unicode_char = *s & 0x07;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid UTF-8 Initial charactere, skip it\n");
|
|
|
|
/* ERROR HERE */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
expecting_x_utf_8_char = num - 1;
|
|
|
|
|
|
|
|
} else if (USE_UTF_8_ENCODING && expecting_x_utf_8_char > 0) {
|
|
|
|
if (((*s >> 6) & 0x03) == 0x2) {
|
|
|
|
|
|
|
|
unicode_char = unicode_char << 6;
|
|
|
|
unicode_char = unicode_char | (*s & 0x3f);
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid UTF-8 character to ampersand, skip it\n");
|
|
|
|
expecting_x_utf_8_char = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
expecting_x_utf_8_char--;
|
|
|
|
if (expecting_x_utf_8_char == 0) {
|
|
|
|
*dlen += sprintf(*dst + *dlen, "&#x%X;", unicode_char);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(*dst)[(*dlen)++] = *s;
|
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return *dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define XML_INDENT " "
|
2009-03-05 04:04:41 +00:00
|
|
|
/* Recursively converts each tag to xml appending it to *s. Reallocates *s if
|
|
|
|
its length exceeds max. start is the location of the previous tag in the
|
|
|
|
parent tag's character content. Returns *s. */
|
2010-02-06 03:38:24 +00:00
|
|
|
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)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
2009-03-20 14:31:15 +00:00
|
|
|
char *txt;
|
|
|
|
switch_size_t off;
|
|
|
|
uint32_t lcount;
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
tailrecurse:
|
2009-03-20 14:31:15 +00:00
|
|
|
off = 0;
|
|
|
|
lcount = 0;
|
2010-02-06 03:38:24 +00:00
|
|
|
txt = (char *) (xml->parent) ? xml->parent->txt : (char *) "";
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* parent character content up to this tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
*s = switch_xml_ampencode(txt + start, xml->off - start, s, len, max, 0);
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
while (*len + strlen(xml->name) + 5 + (strlen(XML_INDENT) * (*count)) + 1 > *max) { /* reallocate s */
|
2011-06-02 03:36:19 +00:00
|
|
|
char *tmp = *s;
|
|
|
|
*s = (char *) realloc(*s, *max += SWITCH_XML_BUFSIZE);
|
|
|
|
if (!*s)
|
|
|
|
return tmp;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 20:28:22 +00:00
|
|
|
if (*len && *(*s + (*len) - 1) == '>') {
|
2009-03-05 04:04:41 +00:00
|
|
|
*len += sprintf(*s + *len, "\n"); /* indent */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
for (lcount = 0; lcount < *count; lcount++) {
|
2009-03-05 04:04:41 +00:00
|
|
|
*len += sprintf(*s + *len, "%s", XML_INDENT); /* indent */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
*len += sprintf(*s + *len, "<%s", xml->name); /* open tag */
|
|
|
|
for (i = 0; xml->attr[i]; i += 2) { /* tag attributes */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (switch_xml_attr(xml, xml->attr[i]) != xml->attr[i + 1])
|
|
|
|
continue;
|
2009-03-05 04:04:41 +00:00
|
|
|
while (*len + strlen(xml->attr[i]) + 7 + (strlen(XML_INDENT) * (*count)) > *max) { /* reallocate s */
|
2010-02-06 03:38:24 +00:00
|
|
|
char *tmp = (char *) realloc(*s, *max += SWITCH_XML_BUFSIZE);
|
|
|
|
if (!tmp)
|
|
|
|
return *s;
|
2008-07-08 21:52:38 +00:00
|
|
|
*s = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
*len += sprintf(*s + *len, " %s=\"", xml->attr[i]);
|
|
|
|
switch_xml_ampencode(xml->attr[i + 1], 0, s, len, max, 1);
|
|
|
|
*len += sprintf(*s + *len, "\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; attr[i] && strcmp(attr[i][0], xml->name); i++);
|
2009-03-05 04:04:41 +00:00
|
|
|
for (j = 1; attr[i] && attr[i][j]; j += 3) { /* default attributes */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!attr[i][j + 1] || switch_xml_attr(xml, attr[i][j]) != attr[i][j + 1])
|
2009-03-05 04:04:41 +00:00
|
|
|
continue; /* skip duplicates and non-values */
|
|
|
|
while (*len + strlen(attr[i][j]) + 8 + (strlen(XML_INDENT) * (*count)) > *max) { /* reallocate s */
|
2010-02-06 03:38:24 +00:00
|
|
|
char *tmp = (char *) realloc(*s, *max += SWITCH_XML_BUFSIZE);
|
|
|
|
if (!tmp)
|
|
|
|
return *s;
|
2008-07-08 21:52:38 +00:00
|
|
|
*s = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
*len += sprintf(*s + *len, " %s=\"", attr[i][j]);
|
|
|
|
switch_xml_ampencode(attr[i][j + 1], 0, s, len, max, 1);
|
|
|
|
*len += sprintf(*s + *len, "\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
*len += sprintf(*s + *len, (xml->child || xml->txt) ? ">" : "/>\n");
|
|
|
|
|
|
|
|
if (xml->child) {
|
|
|
|
(*count)++;
|
|
|
|
*s = switch_xml_toxml_r(xml->child, s, len, max, 0, attr, count);
|
|
|
|
|
|
|
|
} else {
|
2009-03-05 04:04:41 +00:00
|
|
|
*s = switch_xml_ampencode(xml->txt, 0, s, len, max, 0); /* data */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
while (*len + strlen(xml->name) + 5 + (strlen(XML_INDENT) * (*count)) > *max) { /* reallocate s */
|
2011-06-02 03:36:19 +00:00
|
|
|
char *tmp = *s;
|
|
|
|
*s = (char *) realloc(*s, *max += SWITCH_XML_BUFSIZE);
|
|
|
|
if (!*s)
|
|
|
|
return tmp;
|
2010-02-06 03:38:24 +00:00
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
if (xml->child || xml->txt) {
|
|
|
|
if (*(*s + (*len) - 1) == '\n') {
|
|
|
|
for (lcount = 0; lcount < *count; lcount++) {
|
2009-03-05 04:04:41 +00:00
|
|
|
*len += sprintf(*s + *len, "%s", XML_INDENT); /* indent */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-05 04:04:41 +00:00
|
|
|
*len += sprintf(*s + (*len), "</%s>\n", xml->name); /* close tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (txt[off] && off < xml->off)
|
2009-03-05 04:04:41 +00:00
|
|
|
off++; /* make sure off is within bounds */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
if (xml->ordered) {
|
2009-03-20 14:31:15 +00:00
|
|
|
xml = xml->ordered;
|
|
|
|
start = off;
|
|
|
|
goto tailrecurse;
|
|
|
|
/*
|
2008-07-08 21:52:38 +00:00
|
|
|
return switch_xml_toxml_r(xml->ordered, s, len, max, off, attr, count);
|
2009-03-20 14:31:15 +00:00
|
|
|
*/
|
2008-07-08 21:52:38 +00:00
|
|
|
} else {
|
|
|
|
if (*count > 0)
|
|
|
|
(*count)--;
|
|
|
|
return switch_xml_ampencode(txt + off, 0, s, len, max, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(char *) switch_xml_toxml(switch_xml_t xml, switch_bool_t prn_header)
|
|
|
|
{
|
2009-02-25 02:22:07 +00:00
|
|
|
char *r, *s;
|
|
|
|
switch_mutex_lock(XML_GEN_LOCK);
|
2010-02-06 03:38:24 +00:00
|
|
|
s = (char *) malloc(SWITCH_XML_BUFSIZE);
|
2009-05-15 21:07:18 +00:00
|
|
|
switch_assert(s);
|
2009-02-25 02:22:07 +00:00
|
|
|
r = switch_xml_toxml_buf(xml, s, SWITCH_XML_BUFSIZE, 0, prn_header);
|
|
|
|
switch_mutex_unlock(XML_GEN_LOCK);
|
|
|
|
return r;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* converts an switch_xml structure back to xml, returning a string of xml date that
|
|
|
|
must be freed */
|
2008-07-08 21:52:38 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
switch_xml_t p = (xml) ? xml->parent : NULL, o = (xml) ? xml->ordered : NULL;
|
|
|
|
switch_xml_root_t root = (switch_xml_root_t) xml;
|
|
|
|
switch_size_t len = 0, max = buflen;
|
|
|
|
char *s, *t, *n, *r;
|
|
|
|
int i, j, k;
|
|
|
|
uint32_t count = 0;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
s = buf;
|
|
|
|
assert(s != NULL);
|
|
|
|
memset(s, 0, max);
|
|
|
|
len += offset;
|
|
|
|
if (prn_header) {
|
|
|
|
len += sprintf(s + len, "<?xml version=\"1.0\"?>\n");
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!xml || !xml->name) {
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(r = (char *) realloc(s, len + 1))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (root->xml.parent) {
|
2009-03-05 04:04:41 +00:00
|
|
|
root = (switch_xml_root_t) root->xml.parent; /* root tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
for (i = 0; !p && root->pi[i]; i++) { /* pre-root processing instructions */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (k = 2; root->pi[i][k - 1]; k++);
|
|
|
|
for (j = 1; (n = root->pi[i][j]); j++) {
|
|
|
|
if (root->pi[i][k][j - 1] == '>') {
|
2009-03-05 04:04:41 +00:00
|
|
|
continue; /* not pre-root */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max) {
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(r = (char *) realloc(s, max += SWITCH_XML_BUFSIZE))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
s = r;
|
|
|
|
}
|
|
|
|
len += sprintf(s + len, "<?%s%s%s?>", t, *n ? " " : "", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xml->parent = xml->ordered = NULL;
|
|
|
|
s = switch_xml_toxml_r(xml, &s, &len, &max, 0, root->attr, &count);
|
|
|
|
xml->parent = p;
|
|
|
|
xml->ordered = o;
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
for (i = 0; !p && root->pi[i]; i++) { /* post-root processing instructions */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (k = 2; root->pi[i][k - 1]; k++);
|
|
|
|
for (j = 1; (n = root->pi[i][j]); j++) {
|
|
|
|
if (root->pi[i][k][j - 1] == '<') {
|
2009-03-05 04:04:41 +00:00
|
|
|
continue; /* not post-root */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max) {
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(r = (char *) realloc(s, max += SWITCH_XML_BUFSIZE))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
s = r;
|
|
|
|
}
|
|
|
|
len += sprintf(s + len, "\n<?%s%s%s?>", t, *n ? " " : "", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!(r = (char *) realloc(s, len + 1))) {
|
2008-07-08 21:52:38 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* free the memory allocated for the switch_xml structure */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(void) switch_xml_free(switch_xml_t xml)
|
|
|
|
{
|
2009-03-20 14:31:15 +00:00
|
|
|
switch_xml_root_t root;
|
2008-07-08 21:52:38 +00:00
|
|
|
int i, j;
|
|
|
|
char **a, *s;
|
2009-03-20 14:31:15 +00:00
|
|
|
switch_xml_t orig_xml;
|
2011-03-31 18:43:36 +00:00
|
|
|
int refs = 0;
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2011-03-31 18:43:36 +00:00
|
|
|
tailrecurse:
|
2010-02-06 03:38:24 +00:00
|
|
|
root = (switch_xml_root_t) xml;
|
2008-10-13 19:25:52 +00:00
|
|
|
if (!xml) {
|
2008-07-08 21:52:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:41:27 +00:00
|
|
|
if (switch_test_flag(xml, SWITCH_XML_ROOT)) {
|
2011-03-31 18:43:36 +00:00
|
|
|
switch_mutex_lock(REFLOCK);
|
2011-03-18 20:41:27 +00:00
|
|
|
|
2011-03-31 18:43:36 +00:00
|
|
|
if (xml->refs) {
|
|
|
|
xml->refs--;
|
|
|
|
refs = xml->refs;
|
2011-03-18 20:41:27 +00:00
|
|
|
}
|
2011-03-31 18:43:36 +00:00
|
|
|
switch_mutex_unlock(REFLOCK);
|
2011-03-18 20:41:27 +00:00
|
|
|
}
|
|
|
|
|
2011-03-31 18:43:36 +00:00
|
|
|
if (refs) {
|
2008-07-08 21:52:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xml->free_path) {
|
|
|
|
if (!switch_stristr("freeswitch.xml.fsxml", xml->free_path)) {
|
|
|
|
if (unlink(xml->free_path) != 0) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to delete file [%s]\n", xml->free_path);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
switch_safe_free(xml->free_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_xml_free(xml->child);
|
2010-02-06 03:38:24 +00:00
|
|
|
/*switch_xml_free(xml->ordered); */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
if (!xml->parent) { /* free root tag allocations */
|
2009-05-15 21:07:18 +00:00
|
|
|
#if (_MSC_VER >= 1400) // VC8+
|
2010-02-06 03:38:24 +00:00
|
|
|
__analysis_assume(sizeof(root->ent) > 44); /* tail recursion confuses code analysis */
|
2009-05-15 21:07:18 +00:00
|
|
|
#endif
|
2009-03-05 04:04:41 +00:00
|
|
|
for (i = 10; root->ent[i]; i += 2) /* 0 - 9 are default entities (<>&"') */
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((s = root->ent[i + 1]) < root->s || s > root->e)
|
|
|
|
free(s);
|
2009-03-05 04:04:41 +00:00
|
|
|
free(root->ent); /* free list of general entities */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
for (i = 0; (a = root->attr[i]); i++) {
|
2009-03-05 04:04:41 +00:00
|
|
|
for (j = 1; a[j++]; j += 2) /* free malloced attribute values */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (a[j] && (a[j] < root->s || a[j] > root->e))
|
|
|
|
free(a[j]);
|
|
|
|
free(a);
|
|
|
|
}
|
|
|
|
if (root->attr[0])
|
2009-03-05 04:04:41 +00:00
|
|
|
free(root->attr); /* free default attribute list */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
for (i = 0; root->pi[i]; i++) {
|
|
|
|
for (j = 1; root->pi[i][j]; j++);
|
|
|
|
free(root->pi[i][j + 1]);
|
|
|
|
free(root->pi[i]);
|
|
|
|
}
|
|
|
|
if (root->pi[0])
|
2009-03-05 04:04:41 +00:00
|
|
|
free(root->pi); /* free processing instructions */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
if (root->dynamic == 1)
|
2009-03-05 04:04:41 +00:00
|
|
|
free(root->m); /* malloced xml data */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (root->u)
|
2009-03-05 04:04:41 +00:00
|
|
|
free(root->u); /* utf8 conversion */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
switch_xml_free_attr(xml->attr); /* tag attributes */
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((xml->flags & SWITCH_XML_TXTM))
|
2009-03-05 04:04:41 +00:00
|
|
|
free(xml->txt); /* character content */
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((xml->flags & SWITCH_XML_NAMEM))
|
2009-03-05 04:04:41 +00:00
|
|
|
free(xml->name); /* tag name */
|
2009-03-20 14:31:15 +00:00
|
|
|
if (xml->ordered) {
|
|
|
|
orig_xml = xml;
|
|
|
|
xml = xml->ordered;
|
|
|
|
free(orig_xml);
|
|
|
|
goto tailrecurse;
|
|
|
|
}
|
2008-07-08 21:52:38 +00:00
|
|
|
free(xml);
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* return parser error message or empty string if none */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(const char *) switch_xml_error(switch_xml_t xml)
|
|
|
|
{
|
|
|
|
while (xml && xml->parent)
|
2009-03-05 04:04:41 +00:00
|
|
|
xml = xml->parent; /* find root tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
return (xml) ? ((switch_xml_root_t) xml)->err : "";
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* returns a new empty switch_xml structure with the given root tag name */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_new(const char *name)
|
|
|
|
{
|
|
|
|
static const char *ent[] = { "lt;", "<", "gt;", ">", "quot;", """,
|
|
|
|
"apos;", "'", "amp;", "&", NULL
|
|
|
|
};
|
2009-05-27 04:21:51 +00:00
|
|
|
switch_xml_root_t root = (switch_xml_root_t) malloc(sizeof(struct switch_xml_root));
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!root)
|
|
|
|
return NULL;
|
2009-05-27 04:21:51 +00:00
|
|
|
memset(root, '\0', sizeof(struct switch_xml_root));
|
2008-07-08 21:52:38 +00:00
|
|
|
root->xml.name = (char *) name;
|
|
|
|
root->cur = &root->xml;
|
2010-02-06 03:38:24 +00:00
|
|
|
strcpy(root->err, root->xml.txt = (char *) "");
|
|
|
|
root->ent = (char **) memcpy(malloc(sizeof(ent)), ent, sizeof(ent));
|
2008-07-08 21:52:38 +00:00
|
|
|
root->attr = root->pi = (char ***) (root->xml.attr = SWITCH_XML_NIL);
|
2010-02-11 15:54:41 +00:00
|
|
|
root->xml.is_switch_xml_root_t = SWITCH_TRUE;
|
2008-07-08 21:52:38 +00:00
|
|
|
return &root->xml;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* inserts an existing tag into an switch_xml structure */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_insert(switch_xml_t xml, switch_xml_t dest, switch_size_t off)
|
|
|
|
{
|
|
|
|
switch_xml_t cur, prev, head;
|
|
|
|
|
|
|
|
xml->next = xml->sibling = xml->ordered = NULL;
|
|
|
|
xml->off = off;
|
|
|
|
xml->parent = dest;
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
if ((head = dest->child)) { /* already have sub tags */
|
|
|
|
if (head->off <= off) { /* not first subtag */
|
2008-07-08 21:52:38 +00:00
|
|
|
for (cur = head; cur->ordered && cur->ordered->off <= off; cur = cur->ordered);
|
|
|
|
xml->ordered = cur->ordered;
|
|
|
|
cur->ordered = xml;
|
2009-03-05 04:04:41 +00:00
|
|
|
} else { /* first subtag */
|
2008-07-08 21:52:38 +00:00
|
|
|
xml->ordered = head;
|
|
|
|
dest->child = xml;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
for (cur = head, prev = NULL; cur && strcmp(cur->name, xml->name); prev = cur, cur = cur->sibling); /* find tag type */
|
|
|
|
if (cur && cur->off <= off) { /* not first of type */
|
2008-07-08 21:52:38 +00:00
|
|
|
while (cur->next && cur->next->off <= off)
|
|
|
|
cur = cur->next;
|
|
|
|
xml->next = cur->next;
|
|
|
|
cur->next = xml;
|
2009-03-05 04:04:41 +00:00
|
|
|
} else { /* first tag of this type */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (prev && cur)
|
2009-03-05 04:04:41 +00:00
|
|
|
prev->sibling = cur->sibling; /* remove old first */
|
|
|
|
xml->next = cur; /* old first tag is now next */
|
|
|
|
for (cur = head, prev = NULL; cur && cur->off <= off; prev = cur, cur = cur->sibling); /* new sibling insert point */
|
2008-07-08 21:52:38 +00:00
|
|
|
xml->sibling = cur;
|
|
|
|
if (prev)
|
|
|
|
prev->sibling = xml;
|
|
|
|
}
|
|
|
|
} else
|
2009-03-05 04:04:41 +00:00
|
|
|
dest->child = xml; /* only sub tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* Adds a child tag. off is the offset of the child tag relative to the start
|
|
|
|
of the parent tag's character content. Returns the child tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_add_child(switch_xml_t xml, const char *name, switch_size_t off)
|
|
|
|
{
|
|
|
|
switch_xml_t child;
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!xml)
|
|
|
|
return NULL;
|
|
|
|
if (!(child = (switch_xml_t) malloc(sizeof(struct switch_xml))))
|
|
|
|
return NULL;
|
2009-05-27 04:21:51 +00:00
|
|
|
memset(child, '\0', sizeof(struct switch_xml));
|
2008-07-08 21:52:38 +00:00
|
|
|
child->name = (char *) name;
|
|
|
|
child->attr = SWITCH_XML_NIL;
|
|
|
|
child->off = off;
|
|
|
|
child->parent = xml;
|
2010-02-06 03:38:24 +00:00
|
|
|
child->txt = (char *) "";
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
return switch_xml_insert(child, xml, off);
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* sets the character content for the given tag and returns the tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_set_txt(switch_xml_t xml, const char *txt)
|
|
|
|
{
|
|
|
|
if (!xml)
|
|
|
|
return NULL;
|
|
|
|
if (xml->flags & SWITCH_XML_TXTM)
|
2009-03-05 04:04:41 +00:00
|
|
|
free(xml->txt); /* existing txt was malloced */
|
2008-07-08 21:52:38 +00:00
|
|
|
xml->flags &= ~SWITCH_XML_TXTM;
|
|
|
|
xml->txt = (char *) txt;
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* Sets the given tag attribute or adds a new attribute if not found. A value
|
|
|
|
of NULL will remove the specified attribute. Returns the tag given */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_set_attr(switch_xml_t xml, const char *name, const char *value)
|
|
|
|
{
|
|
|
|
int l = 0, c;
|
|
|
|
|
|
|
|
if (!xml)
|
|
|
|
return NULL;
|
|
|
|
while (xml->attr[l] && strcmp(xml->attr[l], name))
|
|
|
|
l += 2;
|
2009-03-05 04:04:41 +00:00
|
|
|
if (!xml->attr[l]) { /* not found, add as new attribute */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!value)
|
2009-03-05 04:04:41 +00:00
|
|
|
return xml; /* nothing to do */
|
|
|
|
if (xml->attr == SWITCH_XML_NIL) { /* first attribute */
|
2010-02-06 03:38:24 +00:00
|
|
|
xml->attr = (char **) malloc(4 * sizeof(char *));
|
|
|
|
if (!xml->attr)
|
|
|
|
return NULL;
|
2009-03-05 04:04:41 +00:00
|
|
|
xml->attr[1] = strdup(""); /* empty list of malloced names/vals */
|
2008-07-08 21:52:38 +00:00
|
|
|
} else {
|
2010-02-06 03:38:24 +00:00
|
|
|
char **tmp = (char **) realloc(xml->attr, (l + 4) * sizeof(char *));
|
|
|
|
if (!tmp)
|
|
|
|
return xml;
|
2008-07-08 21:52:38 +00:00
|
|
|
xml->attr = tmp;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
xml->attr[l] = (char *) name; /* set attribute name */
|
|
|
|
xml->attr[l + 2] = NULL; /* null terminate attribute list */
|
2010-02-06 03:38:24 +00:00
|
|
|
xml->attr[l + 3] = (char *) realloc(xml->attr[l + 1], (c = (int) strlen(xml->attr[l + 1])) + 2);
|
2009-03-05 04:04:41 +00:00
|
|
|
strcpy(xml->attr[l + 3] + c, " "); /* set name/value as not malloced */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (xml->flags & SWITCH_XML_DUP)
|
|
|
|
xml->attr[l + 3][c] = SWITCH_XML_NAMEM;
|
|
|
|
} else if (xml->flags & SWITCH_XML_DUP)
|
2009-03-05 04:04:41 +00:00
|
|
|
free((char *) name); /* name was strduped */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
for (c = l; xml->attr[c]; c += 2); /* find end of attribute list */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (xml->attr[c + 1][l / 2] & SWITCH_XML_TXTM)
|
2009-03-05 04:04:41 +00:00
|
|
|
free(xml->attr[l + 1]); /* old val */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (xml->flags & SWITCH_XML_DUP)
|
|
|
|
xml->attr[c + 1][l / 2] |= SWITCH_XML_TXTM;
|
|
|
|
else
|
|
|
|
xml->attr[c + 1][l / 2] &= ~SWITCH_XML_TXTM;
|
|
|
|
|
|
|
|
if (value)
|
2009-03-05 04:04:41 +00:00
|
|
|
xml->attr[l + 1] = (char *) value; /* set attribute value */
|
|
|
|
else { /* remove attribute */
|
2008-07-08 21:52:38 +00:00
|
|
|
char **tmp;
|
|
|
|
if (xml->attr[c + 1][l / 2] & SWITCH_XML_NAMEM)
|
|
|
|
free(xml->attr[l]);
|
|
|
|
memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char *));
|
2010-02-06 03:38:24 +00:00
|
|
|
tmp = (char **) realloc(xml->attr, (c + 2) * sizeof(char *));
|
|
|
|
if (!tmp)
|
|
|
|
return xml;
|
2008-07-08 21:52:38 +00:00
|
|
|
xml->attr = tmp;
|
2009-03-05 04:04:41 +00:00
|
|
|
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 */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
2009-03-05 04:04:41 +00:00
|
|
|
xml->flags &= ~SWITCH_XML_DUP; /* clear strdup() flag */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* sets a flag for the given tag and returns the tag */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_set_flag(switch_xml_t xml, switch_xml_flag_t flag)
|
|
|
|
{
|
|
|
|
if (xml)
|
|
|
|
xml->flags |= flag;
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
/* removes a tag along with its subtags without freeing its memory */
|
2008-07-08 21:52:38 +00:00
|
|
|
SWITCH_DECLARE(switch_xml_t) switch_xml_cut(switch_xml_t xml)
|
|
|
|
{
|
|
|
|
switch_xml_t cur;
|
|
|
|
|
|
|
|
if (!xml)
|
2009-03-05 04:04:41 +00:00
|
|
|
return NULL; /* nothing to do */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (xml->next)
|
2009-03-05 04:04:41 +00:00
|
|
|
xml->next->sibling = xml->sibling; /* patch sibling list */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
if (xml->parent) { /* not root tag */
|
|
|
|
cur = xml->parent->child; /* find head of subtag list */
|
2008-07-08 21:52:38 +00:00
|
|
|
if (cur == xml)
|
2009-03-05 04:04:41 +00:00
|
|
|
xml->parent->child = xml->ordered; /* first subtag */
|
|
|
|
else { /* not first subtag */
|
2008-07-08 21:52:38 +00:00
|
|
|
while (cur->ordered != xml)
|
|
|
|
cur = cur->ordered;
|
2009-03-05 04:04:41 +00:00
|
|
|
cur->ordered = cur->ordered->ordered; /* patch ordered list */
|
2008-07-08 21:52:38 +00:00
|
|
|
|
2009-03-05 04:04:41 +00:00
|
|
|
cur = xml->parent->child; /* go back to head of subtag list */
|
|
|
|
if (strcmp(cur->name, xml->name)) { /* not in first sibling list */
|
2008-07-08 21:52:38 +00:00
|
|
|
while (strcmp(cur->sibling->name, xml->name))
|
|
|
|
cur = cur->sibling;
|
2009-03-05 04:04:41 +00:00
|
|
|
if (cur->sibling == xml) { /* first of a sibling list */
|
2008-07-08 21:52:38 +00:00
|
|
|
cur->sibling = (xml->next) ? xml->next : cur->sibling->sibling;
|
|
|
|
} else
|
2009-03-05 04:04:41 +00:00
|
|
|
cur = cur->sibling; /* not first of a sibling list */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (cur->next && cur->next != xml)
|
|
|
|
cur = cur->next;
|
|
|
|
if (cur->next)
|
2009-03-05 04:04:41 +00:00
|
|
|
cur->next = cur->next->next; /* patch next list */
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-05 04:04:41 +00:00
|
|
|
xml->ordered = xml->sibling = xml->next = NULL; /* prevent switch_xml_free() from clobbering ordered list */
|
2008-07-08 21:52:38 +00:00
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
2011-11-07 20:31:25 +00:00
|
|
|
SWITCH_DECLARE(int) switch_xml_std_datetime_check(switch_xml_t xcond, int *offset)
|
|
|
|
{
|
2010-04-26 09:06:30 +00:00
|
|
|
|
2010-09-17 04:21:19 +00:00
|
|
|
const char *xdt = switch_xml_attr(xcond, "date-time");
|
2010-04-26 09:06:30 +00:00
|
|
|
const char *xyear = switch_xml_attr(xcond, "year");
|
|
|
|
const char *xyday = switch_xml_attr(xcond, "yday");
|
|
|
|
const char *xmon = switch_xml_attr(xcond, "mon");
|
|
|
|
const char *xmday = switch_xml_attr(xcond, "mday");
|
|
|
|
const char *xweek = switch_xml_attr(xcond, "week");
|
|
|
|
const char *xmweek = switch_xml_attr(xcond, "mweek");
|
|
|
|
const char *xwday = switch_xml_attr(xcond, "wday");
|
|
|
|
const char *xhour = switch_xml_attr(xcond, "hour");
|
|
|
|
const char *xminute = switch_xml_attr(xcond, "minute");
|
|
|
|
const char *xminday = switch_xml_attr(xcond, "minute-of-day");
|
2010-09-17 04:07:30 +00:00
|
|
|
const char *xtod = switch_xml_attr(xcond, "time-of-day");
|
2011-11-07 20:31:25 +00:00
|
|
|
const char *tzoff = switch_xml_attr(xcond, "tz-offset");
|
|
|
|
int loffset = 0;
|
2010-04-26 09:06:30 +00:00
|
|
|
|
|
|
|
switch_time_t ts = switch_micro_time_now();
|
|
|
|
int time_match = -1;
|
|
|
|
switch_time_exp_t tm;
|
|
|
|
|
2011-11-07 20:31:25 +00:00
|
|
|
if (!zstr(tzoff) && switch_is_number(tzoff)) {
|
|
|
|
loffset = atoi(tzoff);
|
|
|
|
offset = &loffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (offset) {
|
|
|
|
switch_time_exp_tz(&tm, ts, *offset);
|
|
|
|
} else {
|
|
|
|
switch_time_exp_lt(&tm, ts);
|
|
|
|
}
|
2010-04-26 09:06:30 +00:00
|
|
|
|
2010-09-17 04:21:19 +00:00
|
|
|
if (time_match && xdt) {
|
|
|
|
char tmpdate[80];
|
|
|
|
switch_size_t retsize;
|
|
|
|
switch_strftime(tmpdate, &retsize, sizeof(tmpdate), "%Y-%m-%d %H:%M:%S", &tm);
|
|
|
|
time_match = switch_fulldate_cmp(xdt, &ts);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
|
|
|
|
"XML DateTime Check: date time[%s] =~ %s (%s)\n", tmpdate, xdt, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
2010-04-26 09:06:30 +00:00
|
|
|
if (time_match && xyear) {
|
|
|
|
int test = tm.tm_year + 1900;
|
|
|
|
time_match = switch_number_cmp(xyear, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime Check: year[%d] =~ %s (%s)\n", test, xyear, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_match && xyday) {
|
|
|
|
int test = tm.tm_yday + 1;
|
|
|
|
time_match = switch_number_cmp(xyday, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime Check: day of year[%d] =~ %s (%s)\n", test, xyday, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_match && xmon) {
|
|
|
|
int test = tm.tm_mon + 1;
|
|
|
|
time_match = switch_number_cmp(xmon, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime Check: month[%d] =~ %s (%s)\n", test, xmon, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_match && xmday) {
|
|
|
|
int test = tm.tm_mday;
|
|
|
|
time_match = switch_number_cmp(xmday, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime Check: day of month[%d] =~ %s (%s)\n", test, xmday, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_match && xweek) {
|
|
|
|
int test = (int) (tm.tm_yday / 7 + 1);
|
|
|
|
time_match = switch_number_cmp(xweek, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime Check: week of year[%d] =~ %s (%s)\n", test, xweek, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
if (time_match && xweek) {
|
|
|
|
int test = (int) (tm.tm_yday / 7 + 1);
|
|
|
|
time_match = switch_number_cmp(xweek, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime Check: week of year[%d] =~ %s (%s)\n", test, xweek, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_match && xmweek) {
|
|
|
|
/* calculate the day of the week of the first of the month (0-6) */
|
|
|
|
int firstdow = (int) (7 - (tm.tm_mday - (tm.tm_wday + 1)) % 7) % 7;
|
|
|
|
/* calculate the week of the month (1-6)*/
|
|
|
|
int test = (int) ceil((tm.tm_mday + firstdow) / 7.0);
|
|
|
|
time_match = switch_number_cmp(xmweek, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime: week of month[%d] =~ %s (%s)\n", test, xmweek, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_match && xwday) {
|
|
|
|
int test = tm.tm_wday + 1;
|
2010-09-17 04:26:27 +00:00
|
|
|
time_match = switch_dow_cmp(xwday, test);
|
2010-04-26 09:06:30 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
2010-09-17 04:26:27 +00:00
|
|
|
"XML DateTime Check: day of week[%s] =~ %s (%s)\n", switch_dow_int2str(test), xwday, time_match ? "PASS" : "FAIL");
|
2010-04-26 09:06:30 +00:00
|
|
|
}
|
|
|
|
if (time_match && xhour) {
|
|
|
|
int test = tm.tm_hour;
|
|
|
|
time_match = switch_number_cmp(xhour, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime Check: hour[%d] =~ %s (%s)\n", test, xhour, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_match && xminute) {
|
|
|
|
int test = tm.tm_min;
|
|
|
|
time_match = switch_number_cmp(xminute, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime Check: minute[%d] =~ %s (%s)\n", test, xminute, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_match && xminday) {
|
|
|
|
int test = (tm.tm_hour * 60) + (tm.tm_min + 1);
|
|
|
|
time_match = switch_number_cmp(xminday, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime Check: minute of day[%d] =~ %s (%s)\n", test, xminday, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
2010-09-17 04:07:30 +00:00
|
|
|
if (time_match && xtod) {
|
|
|
|
int test = (tm.tm_hour * 60 * 60) + (tm.tm_min * 60) + tm.tm_sec;
|
|
|
|
char tmpdate[10];
|
2010-10-25 19:21:39 +00:00
|
|
|
switch_snprintf(tmpdate, 10, "%d:%d:%d", tm.tm_hour, tm.tm_min, tm.tm_sec);
|
2010-09-17 04:07:30 +00:00
|
|
|
time_match = switch_tod_cmp(xtod, test);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG9,
|
|
|
|
"XML DateTime Check: time of day[%s] =~ %s (%s)\n", tmpdate, xtod, time_match ? "PASS" : "FAIL");
|
|
|
|
}
|
|
|
|
|
2010-04-26 09:06:30 +00:00
|
|
|
return time_match;
|
|
|
|
}
|
|
|
|
|
2011-04-20 17:09:03 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_xml_locate_language(switch_xml_t *root, switch_xml_t *node, switch_event_t *params, switch_xml_t *language, switch_xml_t *phrases, switch_xml_t *macros, const char *str_language) {
|
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
|
|
|
|
|
|
|
if (switch_xml_locate("languages", NULL, NULL, NULL, root, node, params, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_xml_t sub_macros;
|
|
|
|
|
|
|
|
if (switch_xml_locate("phrases", NULL, NULL, NULL, root, node, params, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of languages and phrases failed.\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!(sub_macros = switch_xml_child(*node, "macros"))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find macros tag.\n");
|
|
|
|
switch_xml_free(*root);
|
|
|
|
*root = NULL;
|
|
|
|
*node = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!(*language = switch_xml_find_child(sub_macros, "language", "name", str_language))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find language %s.\n", str_language);
|
|
|
|
switch_xml_free(*root);
|
|
|
|
*root = NULL;
|
|
|
|
*node = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
*macros = *language;
|
|
|
|
} else {
|
|
|
|
if (!(*language = switch_xml_find_child(*node, "language", "name", str_language))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find language %s.\n", str_language);
|
|
|
|
switch_xml_free(*root);
|
|
|
|
*root = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!(*phrases = switch_xml_child(*language, "phrases"))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find phrases tag.\n");
|
|
|
|
switch_xml_free(*root);
|
|
|
|
*root = NULL;
|
|
|
|
*node = NULL;
|
|
|
|
*language = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(*macros = switch_xml_child(*phrases, "macros"))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find macros tag.\n");
|
|
|
|
switch_xml_free(*root);
|
|
|
|
*root = NULL;
|
|
|
|
*node = NULL;
|
|
|
|
*language = NULL;
|
|
|
|
*phrases = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return status;
|
|
|
|
}
|
2010-04-26 09:06:30 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
/*
|
|
|
|
* globbing functions for windows, part of libc on unix, this code was cut and paste from
|
|
|
|
* freebsd lib and distilled a bit to work with windows
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Guido van Rossum.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DOLLAR '$'
|
|
|
|
#define DOT '.'
|
|
|
|
#define EOS '\0'
|
|
|
|
#define LBRACKET '['
|
|
|
|
#define NOT '!'
|
|
|
|
#define QUESTION '?'
|
|
|
|
#define RANGE '-'
|
|
|
|
#define RBRACKET ']'
|
|
|
|
#define SEP '/'
|
|
|
|
#define WIN_SEP '\\'
|
|
|
|
#define STAR '*'
|
|
|
|
#define TILDE '~'
|
|
|
|
#define UNDERSCORE '_'
|
|
|
|
#define LBRACE '{'
|
|
|
|
#define RBRACE '}'
|
|
|
|
#define SLASH '/'
|
|
|
|
#define COMMA ','
|
|
|
|
|
|
|
|
#define M_QUOTE (char)0x80
|
|
|
|
#define M_PROTECT (char)0x40
|
|
|
|
#define M_MASK (char)0xff
|
|
|
|
#define M_ASCII (char)0x7f
|
|
|
|
|
|
|
|
#define CHAR(c) ((char)((c)&M_ASCII))
|
|
|
|
#define META(c) ((char)((c)|M_QUOTE))
|
|
|
|
#define M_ALL META('*')
|
|
|
|
#define M_END META(']')
|
|
|
|
#define M_NOT META('!')
|
|
|
|
#define M_ONE META('?')
|
|
|
|
#define M_RNG META('-')
|
|
|
|
#define M_SET META('[')
|
|
|
|
#define ismeta(c) (((c)&M_QUOTE) != 0)
|
|
|
|
|
|
|
|
#ifndef MAXPATHLEN
|
|
|
|
#define MAXPATHLEN 256
|
|
|
|
#endif
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
static int compare(const void *, const void *);
|
|
|
|
static int glob0(const char *, glob_t *, size_t *);
|
|
|
|
static int glob1(char *, glob_t *, size_t *);
|
|
|
|
static int glob2(char *, char *, char *, char *, glob_t *, size_t *);
|
|
|
|
static int glob3(char *, char *, char *, char *, char *, glob_t *, size_t *);
|
|
|
|
static int globextend(const char *, glob_t *, size_t *);
|
|
|
|
static int match(char *, char *, char *);
|
2008-07-08 21:52:38 +00:00
|
|
|
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4310)
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
int glob(const char *pattern, int flags, int (*errfunc) (const char *, int), glob_t *pglob)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
const unsigned char *patnext;
|
2008-08-15 03:34:29 +00:00
|
|
|
size_t limit;
|
2008-07-08 21:52:38 +00:00
|
|
|
char c;
|
|
|
|
char *bufnext, *bufend, patbuf[MAXPATHLEN];
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
patnext = (unsigned char *) pattern;
|
|
|
|
if (!(flags & GLOB_APPEND)) {
|
|
|
|
pglob->gl_pathc = 0;
|
|
|
|
pglob->gl_pathv = NULL;
|
|
|
|
if (!(flags & GLOB_DOOFFS))
|
|
|
|
pglob->gl_offs = 0;
|
|
|
|
}
|
|
|
|
if (flags & GLOB_LIMIT) {
|
|
|
|
limit = pglob->gl_matchc;
|
|
|
|
if (limit == 0)
|
|
|
|
limit = 9999999;
|
|
|
|
} else
|
|
|
|
limit = 0;
|
|
|
|
pglob->gl_flags = flags & ~GLOB_MAGCHAR;
|
|
|
|
pglob->gl_errfunc = errfunc;
|
|
|
|
pglob->gl_matchc = 0;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
bufnext = patbuf;
|
|
|
|
bufend = bufnext + MAXPATHLEN - 1;
|
|
|
|
while (bufnext < bufend && (c = *patnext++) != EOS)
|
|
|
|
*bufnext++ = c;
|
|
|
|
*bufnext = EOS;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
return glob0(patbuf, pglob, &limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The main glob() routine: compiles the pattern (optionally processing
|
|
|
|
* quotes), calls glob1() to do the real pattern matching, and finally
|
|
|
|
* sorts the list (unless unsorted operation is requested). Returns 0
|
|
|
|
* if things went well, nonzero if errors occurred.
|
|
|
|
*/
|
2008-08-15 03:34:29 +00:00
|
|
|
static int glob0(const char *pattern, glob_t *pglob, size_t *limit)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
const char *qpatnext;
|
|
|
|
int c, err;
|
2008-08-15 03:34:29 +00:00
|
|
|
size_t oldpathc;
|
2008-07-08 21:52:38 +00:00
|
|
|
char *bufnext, patbuf[MAXPATHLEN];
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
qpatnext = pattern;
|
|
|
|
oldpathc = pglob->gl_pathc;
|
|
|
|
bufnext = patbuf;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
/* We don't need to check for buffer overflow any more. */
|
|
|
|
while ((c = *qpatnext++) != EOS) {
|
|
|
|
switch (c) {
|
2010-02-06 03:38:24 +00:00
|
|
|
case SEP:
|
|
|
|
*bufnext++ = WIN_SEP;
|
|
|
|
break;
|
|
|
|
case LBRACKET:
|
|
|
|
c = *qpatnext;
|
|
|
|
if (c == NOT)
|
|
|
|
++qpatnext;
|
|
|
|
if (*qpatnext == EOS || strchr((char *) qpatnext + 1, RBRACKET) == NULL) {
|
|
|
|
*bufnext++ = LBRACKET;
|
2008-07-08 21:52:38 +00:00
|
|
|
if (c == NOT)
|
2010-02-06 03:38:24 +00:00
|
|
|
--qpatnext;
|
2008-07-08 21:52:38 +00:00
|
|
|
break;
|
2010-02-06 03:38:24 +00:00
|
|
|
}
|
|
|
|
*bufnext++ = M_SET;
|
|
|
|
if (c == NOT)
|
|
|
|
*bufnext++ = M_NOT;
|
|
|
|
c = *qpatnext++;
|
|
|
|
do {
|
2008-07-08 21:52:38 +00:00
|
|
|
*bufnext++ = CHAR(c);
|
2010-02-06 03:38:24 +00:00
|
|
|
if (*qpatnext == RANGE && (c = qpatnext[1]) != RBRACKET) {
|
|
|
|
*bufnext++ = M_RNG;
|
|
|
|
*bufnext++ = CHAR(c);
|
|
|
|
qpatnext += 2;
|
|
|
|
}
|
|
|
|
} while ((c = *qpatnext++) != RBRACKET);
|
|
|
|
pglob->gl_flags |= GLOB_MAGCHAR;
|
|
|
|
*bufnext++ = M_END;
|
|
|
|
break;
|
|
|
|
case QUESTION:
|
|
|
|
pglob->gl_flags |= GLOB_MAGCHAR;
|
|
|
|
*bufnext++ = M_ONE;
|
|
|
|
break;
|
|
|
|
case STAR:
|
|
|
|
pglob->gl_flags |= GLOB_MAGCHAR;
|
|
|
|
/* collapse adjacent stars to one,
|
|
|
|
* to avoid exponential behavior
|
|
|
|
*/
|
|
|
|
if (bufnext == patbuf || bufnext[-1] != M_ALL)
|
|
|
|
*bufnext++ = M_ALL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*bufnext++ = CHAR(c);
|
|
|
|
break;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*bufnext = EOS;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((err = glob1(patbuf, pglob, limit)) != 0)
|
2010-02-06 03:38:24 +00:00
|
|
|
return (err);
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
/*
|
|
|
|
* If there was no match we are going to append the pattern
|
|
|
|
* if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
|
|
|
|
* and the pattern did not contain any magic characters
|
|
|
|
* GLOB_NOMAGIC is there just for compatibility with csh.
|
|
|
|
*/
|
|
|
|
if (pglob->gl_pathc == oldpathc) {
|
2010-02-06 03:38:24 +00:00
|
|
|
if (((pglob->gl_flags & GLOB_NOCHECK) || ((pglob->gl_flags & GLOB_NOMAGIC) && !(pglob->gl_flags & GLOB_MAGCHAR))))
|
|
|
|
return (globextend(pattern, pglob, limit));
|
2008-07-08 21:52:38 +00:00
|
|
|
else
|
2010-02-06 03:38:24 +00:00
|
|
|
return (GLOB_NOMATCH);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
if (!(pglob->gl_flags & GLOB_NOSORT))
|
2010-02-06 03:38:24 +00:00
|
|
|
qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, pglob->gl_pathc - oldpathc, sizeof(char *), compare);
|
|
|
|
return (0);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int compare(const void *p, const void *q)
|
|
|
|
{
|
2010-02-06 03:38:24 +00:00
|
|
|
return (strcmp(*(char **) p, *(char **) q));
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2008-08-15 03:34:29 +00:00
|
|
|
static int glob1(char *pattern, glob_t *pglob, size_t *limit)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
char pathbuf[MAXPATHLEN];
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
|
|
|
|
if (*pattern == EOS)
|
2010-02-06 03:38:24 +00:00
|
|
|
return (0);
|
|
|
|
return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1, pattern, pglob, limit));
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The functions glob2 and glob3 are mutually recursive; there is one level
|
|
|
|
* of recursion for each segment in the pattern that contains one or more
|
|
|
|
* meta characters.
|
|
|
|
*/
|
2008-08-15 03:34:29 +00:00
|
|
|
static int glob2(char *pathbuf, char *pathend, char *pathend_last, char *pattern, glob_t *pglob, size_t *limit)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
char *p, *q;
|
|
|
|
int anymeta;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
/*
|
|
|
|
* Loop over pattern segments until end of pattern or until
|
|
|
|
* segment with meta character found.
|
|
|
|
*/
|
|
|
|
for (anymeta = 0;;) {
|
2010-02-06 03:38:24 +00:00
|
|
|
if (*pattern == EOS) { /* End of pattern? */
|
2008-07-08 21:52:38 +00:00
|
|
|
*pathend = EOS;
|
|
|
|
if (stat(pathbuf, &sb))
|
2010-02-06 03:38:24 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (((pglob->gl_flags & GLOB_MARK) && pathend[-1] != SEP && pathend[-1] != WIN_SEP) && (_S_IFDIR & sb.st_mode)) {
|
2008-07-08 21:52:38 +00:00
|
|
|
if (pathend + 1 > pathend_last)
|
|
|
|
return (GLOB_ABORTED);
|
|
|
|
*pathend++ = WIN_SEP;
|
|
|
|
*pathend = EOS;
|
|
|
|
}
|
|
|
|
++pglob->gl_matchc;
|
2010-02-06 03:38:24 +00:00
|
|
|
return (globextend(pathbuf, pglob, limit));
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
/* Find end of next segment, copy tentatively to pathend. */
|
|
|
|
q = pathend;
|
|
|
|
p = pattern;
|
|
|
|
while (*p != EOS && *p != SEP && *p != WIN_SEP) {
|
|
|
|
if (ismeta(*p))
|
|
|
|
anymeta = 1;
|
|
|
|
if (q + 1 > pathend_last)
|
|
|
|
return (GLOB_ABORTED);
|
|
|
|
*q++ = *p++;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
|
|
|
if (!anymeta) { /* No expansion, do next segment. */
|
2008-07-08 21:52:38 +00:00
|
|
|
pathend = q;
|
|
|
|
pattern = p;
|
|
|
|
while (*pattern == SEP || *pattern == WIN_SEP) {
|
|
|
|
if (pathend + 1 > pathend_last)
|
|
|
|
return (GLOB_ABORTED);
|
|
|
|
*pathend++ = *pattern++;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
} else /* Need expansion, recurse. */
|
|
|
|
return (glob3(pathbuf, pathend, pathend_last, pattern, p, pglob, limit));
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2008-08-15 03:34:29 +00:00
|
|
|
static int glob3(char *pathbuf, char *pathend, char *pathend_last, char *pattern, char *restpattern, glob_t *pglob, size_t *limit)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
int err;
|
2010-02-06 03:38:24 +00:00
|
|
|
apr_dir_t *dirp;
|
|
|
|
apr_pool_t *pool;
|
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
apr_pool_create(&pool, NULL);
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (pathend > pathend_last)
|
|
|
|
return (GLOB_ABORTED);
|
|
|
|
*pathend = EOS;
|
|
|
|
errno = 0;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
|
|
|
if (apr_dir_open(&dirp, pathbuf, pool) != APR_SUCCESS) {
|
2008-07-08 21:52:38 +00:00
|
|
|
/* TODO: don't call for ENOENT or ENOTDIR? */
|
|
|
|
apr_pool_destroy(pool);
|
|
|
|
if (pglob->gl_errfunc) {
|
2010-02-06 03:38:24 +00:00
|
|
|
if (pglob->gl_errfunc(pathbuf, errno) || pglob->gl_flags & GLOB_ERR)
|
2008-07-08 21:52:38 +00:00
|
|
|
return (GLOB_ABORTED);
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
return (0);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
err = 0;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
/* Search directory for matching names. */
|
2010-02-06 03:38:24 +00:00
|
|
|
while (dirp) {
|
2008-07-08 21:52:38 +00:00
|
|
|
apr_finfo_t dp;
|
|
|
|
unsigned char *sc;
|
|
|
|
char *dc;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-10-06 19:21:59 +00:00
|
|
|
if (apr_dir_read(&dp, APR_FINFO_NAME, dirp) != APR_SUCCESS)
|
2008-07-08 21:52:38 +00:00
|
|
|
break;
|
2008-10-06 19:21:59 +00:00
|
|
|
if (!(dp.valid & APR_FINFO_NAME) || !(dp.name) || !strlen(dp.name))
|
2008-07-08 21:52:38 +00:00
|
|
|
break;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
/* Initial DOT must be matched literally. */
|
|
|
|
if (dp.name[0] == DOT && *pattern != DOT)
|
|
|
|
continue;
|
|
|
|
dc = pathend;
|
|
|
|
sc = (unsigned char *) dp.name;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
while (dc < pathend_last && (*dc++ = *sc++) != EOS);
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (!match(pathend, pattern, restpattern)) {
|
|
|
|
*pathend = EOS;
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
err = glob2(pathbuf, --dc, pathend_last, restpattern, pglob, limit);
|
2008-07-08 21:52:38 +00:00
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
|
|
|
if (dirp)
|
|
|
|
apr_dir_close(dirp);
|
2008-07-08 21:52:38 +00:00
|
|
|
apr_pool_destroy(pool);
|
2010-02-06 03:38:24 +00:00
|
|
|
return (err);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2008-10-07 01:43:03 +00:00
|
|
|
* Extend the gl_pathv member of a glob_t structure to accommodate a new item,
|
2008-07-08 21:52:38 +00:00
|
|
|
* add the new item, and update gl_pathc.
|
|
|
|
*
|
|
|
|
* This assumes the BSD realloc, which only copies the block when its size
|
|
|
|
* crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
|
|
|
|
* behavior.
|
|
|
|
*
|
|
|
|
* Return 0 if new item added, error code if memory couldn't be allocated.
|
|
|
|
*
|
|
|
|
* Invariant of the glob_t structure:
|
|
|
|
* Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
|
|
|
|
* gl_pathv points to (gl_offs + gl_pathc + 1) items.
|
|
|
|
*/
|
2008-08-15 03:34:29 +00:00
|
|
|
static int globextend(const char *path, glob_t *pglob, size_t *limit)
|
2008-07-08 21:52:38 +00:00
|
|
|
{
|
|
|
|
char **pathv;
|
2010-02-06 03:38:24 +00:00
|
|
|
char *copy;
|
2008-08-15 03:34:29 +00:00
|
|
|
size_t i;
|
|
|
|
size_t newsize, len;
|
2008-07-08 21:52:38 +00:00
|
|
|
const char *p;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-08-17 23:08:10 +00:00
|
|
|
if (*limit && pglob->gl_pathc > *limit) {
|
2008-07-08 21:52:38 +00:00
|
|
|
errno = 0;
|
|
|
|
return (GLOB_NOSPACE);
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
|
2010-02-06 03:38:24 +00:00
|
|
|
pathv = pglob->gl_pathv ? realloc((char *) pglob->gl_pathv, newsize) : malloc(newsize);
|
2008-07-08 21:52:38 +00:00
|
|
|
if (pathv == NULL) {
|
|
|
|
if (pglob->gl_pathv) {
|
|
|
|
free(pglob->gl_pathv);
|
|
|
|
pglob->gl_pathv = NULL;
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
return (GLOB_NOSPACE);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
|
|
|
|
/* first time around -- clear initial gl_offs items */
|
|
|
|
pathv += pglob->gl_offs;
|
2010-02-06 03:38:24 +00:00
|
|
|
for (i = pglob->gl_offs; i-- > 0;)
|
2008-07-08 21:52:38 +00:00
|
|
|
*--pathv = NULL;
|
|
|
|
}
|
|
|
|
pglob->gl_pathv = pathv;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
for (p = path; *p++;)
|
|
|
|
continue;
|
2010-02-06 03:38:24 +00:00
|
|
|
len = (size_t) (p - path);
|
2008-07-08 21:52:38 +00:00
|
|
|
if ((copy = malloc(len)) != NULL) {
|
|
|
|
memcpy(copy, path, len);
|
|
|
|
pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
|
|
|
|
}
|
|
|
|
pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
|
2010-02-06 03:38:24 +00:00
|
|
|
return (copy == NULL ? GLOB_NOSPACE : 0);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pattern matching function for filenames. Each occurrence of the *
|
|
|
|
* pattern causes a recursion level.
|
|
|
|
*/
|
|
|
|
static int match(char *name, char *pat, char *patend)
|
|
|
|
{
|
|
|
|
int ok, negate_range;
|
|
|
|
char c, k;
|
|
|
|
char s1[6];
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
while (pat < patend) {
|
|
|
|
c = *pat++;
|
|
|
|
switch (c & M_MASK) {
|
2010-02-06 03:38:24 +00:00
|
|
|
case M_ALL:
|
|
|
|
if (pat == patend)
|
|
|
|
return (1);
|
|
|
|
do
|
|
|
|
if (match(name, pat, patend))
|
|
|
|
return (1);
|
|
|
|
while (*name++ != EOS);
|
|
|
|
return (0);
|
|
|
|
case M_ONE:
|
|
|
|
if (*name++ == EOS)
|
|
|
|
return (0);
|
|
|
|
break;
|
|
|
|
case M_SET:
|
|
|
|
ok = 0;
|
|
|
|
if ((k = *name++) == EOS)
|
|
|
|
return (0);
|
|
|
|
if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
|
|
|
|
++pat;
|
|
|
|
while (((c = *pat++) & M_MASK) != M_END)
|
|
|
|
if ((*pat & M_MASK) == M_RNG) {
|
|
|
|
memset(s1, 0, sizeof(s1));
|
|
|
|
s1[0] = c;
|
|
|
|
s1[2] = k;
|
|
|
|
s1[4] = pat[1];
|
|
|
|
if (strcoll(&s1[0], &s1[2]) <= 0 && strcoll(&s1[2], &s1[4]) <= 0)
|
2008-07-08 21:52:38 +00:00
|
|
|
ok = 1;
|
2010-02-06 03:38:24 +00:00
|
|
|
pat += 2;
|
|
|
|
} else if (c == k)
|
|
|
|
ok = 1;
|
|
|
|
if (ok == negate_range)
|
|
|
|
return (0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (*name++ != c)
|
|
|
|
return (0);
|
|
|
|
break;
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
return (*name == EOS);
|
2008-07-08 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free allocated data belonging to a glob_t structure. */
|
|
|
|
void globfree(glob_t *pglob)
|
|
|
|
{
|
2008-08-15 03:34:29 +00:00
|
|
|
size_t i;
|
2008-07-08 21:52:38 +00:00
|
|
|
char **pp;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
if (pglob->gl_pathv != NULL) {
|
|
|
|
pp = pglob->gl_pathv + pglob->gl_offs;
|
|
|
|
for (i = pglob->gl_pathc; i--; ++pp)
|
|
|
|
if (*pp)
|
|
|
|
free(*pp);
|
|
|
|
free(pglob->gl_pathv);
|
|
|
|
pglob->gl_pathv = NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-07-08 21:52:38 +00:00
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
|
|
|
* indent-tabs-mode:t
|
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
|
|
|
*/
|