[Freeswitch-svn] [commit] r5745 - in freeswitch/trunk/src: . include

Freeswitch SVN anthm at freeswitch.org
Sun Sep 23 11:19:05 EDT 2007


Author: anthm
Date: Sun Sep 23 11:19:04 2007
New Revision: 5745

Modified:
   freeswitch/trunk/src/include/switch_apr.h
   freeswitch/trunk/src/switch_apr.c
   freeswitch/trunk/src/switch_xml.c

Log:
globs on #includes
no leading seperator indicates realitive
to the default conf dir.

examples:
<!--#include "profiles/*"-->
<!--#include "profiles/a*.xml"-->
<!--#include "/tmp/somedir/*"-->




Modified: freeswitch/trunk/src/include/switch_apr.h
==============================================================================
--- freeswitch/trunk/src/include/switch_apr.h	(original)
+++ freeswitch/trunk/src/include/switch_apr.h	Sun Sep 23 11:19:04 2007
@@ -742,6 +742,20 @@
 
 typedef struct switch_dir switch_dir_t;
 
+struct switch_array_header_t {
+    /** The pool the array is allocated out of */
+    switch_memory_pool_t *pool;
+    /** The amount of memory allocated for each element of the array */
+    int elt_size;
+    /** The number of active elements in the array */
+    int nelts;
+    /** The number of elements allocated in the array */
+    int nalloc;
+    /** The elements in the array */
+    char *elts;
+};
+typedef struct switch_array_header_t switch_array_header_t;
+
 SWITCH_DECLARE(switch_status_t) switch_dir_open(switch_dir_t **new_dir, const char *dirname, switch_memory_pool_t *pool);
 SWITCH_DECLARE(switch_status_t) switch_dir_close(switch_dir_t *thedir);
 SWITCH_DECLARE(const char *) switch_dir_next_file(switch_dir_t *thedir, char *buf, switch_size_t len);
@@ -1143,6 +1157,8 @@
 */
 SWITCH_DECLARE(switch_status_t) switch_socket_create_pollfd(switch_pollfd_t ** poll, switch_socket_t * sock, int16_t flags, switch_memory_pool_t *pool);
 
+SWITCH_DECLARE(switch_status_t) switch_match_glob(const char *pattern, switch_array_header_t **result, switch_memory_pool_t *p);
+
 
  /** @} */
 

Modified: freeswitch/trunk/src/switch_apr.c
==============================================================================
--- freeswitch/trunk/src/switch_apr.c	(original)
+++ freeswitch/trunk/src/switch_apr.c	Sun Sep 23 11:19:04 2007
@@ -53,6 +53,9 @@
 #define APR_WANT_STDIO
 #define APR_WANT_STRFUNC
 #include <apr_want.h>
+#include <apr_file_info.h>
+#include <apr_fnmatch.h>
+#include <apr_tables.h>
 
 /* apr_vformatter_buff_t definition*/
 #include <apr_lib.h>
@@ -714,7 +717,12 @@
 #endif
 }
 
+SWITCH_DECLARE(switch_status_t) switch_match_glob(const char *pattern, switch_array_header_t **result, switch_memory_pool_t *p)
+{
+	return apr_match_glob(pattern, (apr_array_header_t **)result, p);
+}
 
+													
 /* For Emacs:
  * Local Variables:
  * mode:c

Modified: freeswitch/trunk/src/switch_xml.c
==============================================================================
--- freeswitch/trunk/src/switch_xml.c	(original)
+++ freeswitch/trunk/src/switch_xml.c	Sun Sep 23 11:19:04 2007
@@ -68,6 +68,8 @@
 #define SWITCH_XML_WS   "\t\r\n "	// whitespace
 #define SWITCH_XML_ERRL 128		// maximum error string length
 
+static int preprocess(const char *file, int write_fd, int rlevel);
+
 typedef struct switch_xml_root *switch_xml_root_t;
 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
@@ -933,6 +935,57 @@
 
 }
 
+/* for apr file and directory handling */
+#include <apr_file_io.h>
+
+static int preprocess_glob(const char *pattern, int write_fd, int rlevel)
+{
+
+	switch_array_header_t *result;
+	switch_memory_pool_t *pool;
+	char **list;
+	int i;
+	char *p, *dir_path = NULL;
+
+	switch_core_new_memory_pool(&pool);
+	assert(pool != NULL);
+	
+	if (switch_is_file_path(pattern)) {
+		dir_path = switch_core_strdup(pool, pattern);
+		if ((p = strrchr(dir_path, *SWITCH_PATH_SEPARATOR))) {
+			*p = '\0';
+		}
+	} else {
+		dir_path = SWITCH_GLOBAL_dirs.conf_dir;
+		p = switch_core_sprintf(pool, "%s%s%s", SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR, pattern);
+		pattern = p;
+	}
+
+	switch_match_glob(pattern, &result, pool);
+
+	list = (char **)result->elts;
+
+    for (i = 0; i < result->nelts; i++) {
+		char *path = list[i];
+
+		if (strcmp(path, ".") && strcmp(path, "..")) {
+			p = switch_core_sprintf(pool, "%s%s%s", dir_path, SWITCH_PATH_SEPARATOR, path);
+			if (preprocess(p, write_fd, rlevel) < 0) {
+				const char *reason = strerror(errno);
+				if (rlevel > 100) {
+					reason = "Maximum recursion limit reached";
+				}
+				fprintf(stderr, "Error including %s (%s)\n", p, reason);
+			}
+		}
+	}
+
+	switch_core_destroy_memory_pool(&pool);
+	
+
+	return write_fd;
+}
+
 static int preprocess(const char *file, int write_fd, int rlevel)
 {
 	int read_fd = -1;
@@ -1016,17 +1069,8 @@
 					}
 
 				} else if (!strcasecmp(cmd, "include")) {
-					char *fme = NULL, *ifile = arg;
-
-					if (!switch_is_file_path(ifile)) {
-						fme = switch_mprintf("%s%s%s", SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR, arg);
-						ifile = fme;
-					}
-					if (preprocess(ifile, write_fd, rlevel + 1) < 0) {
-						fprintf(stderr, "Error including %s (%s)\n", ifile, strerror(errno));
-					}
-					switch_safe_free(fme);
-				}				/* else NO OP */
+					preprocess_glob(arg, write_fd, rlevel + 1);
+				}
 			}
 
 			continue;



More information about the Freeswitch-svn mailing list