[Asterisk-Users] how to use #include to all files in /etc/asterisk/customdir ?

Tzafrir Cohen tzafrir at cohens.org.il
Tue Nov 8 09:53:31 MST 2005


On Tue, Nov 08, 2005 at 05:59:23PM +0300, Ivan Vershigora wrote:
> how to use #include to all files in /etc/asterisk/customdir   ?
> in v1.0.9
> 
> #include /etc/asterisk/customdir/*.conf
> 
> doesnt work

That is indeed a new feature in 1.2. 

To use it in Asterisk 1.0, apply the attached patch (or use the packages
from Xorcom Rapid ;-) )

-- 
Tzafrir Cohen         | tzafrir at jbr.cohens.org.il | VIM is
http://tzafrir.org.il |                           | a Mutt's  
tzafrir at cohens.org.il |                           |  best
ICQ# 16849755         |                           | friend
-------------- next part --------------
#! /bin/sh /usr/share/dpatch/dpatch-run
## 80_rapid-globinclude.dpatch by  <tzafrir at boole.in.xorcom.com>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: No description.

@DPATCH@
diff -urNad asterisk-1.0.5/config.c /tmp/dpep.dFRSis/asterisk-1.0.5/config.c
--- asterisk-1.0.5/config.c	2004-09-03 18:36:13.000000000 +0300
+++ /tmp/dpep.dFRSis/asterisk-1.0.5/config.c	2005-01-27 13:40:39.000000000 +0200
@@ -17,6 +17,10 @@
 #include <string.h>
 #include <errno.h>
 #include <time.h>
+#define AST_INCLUDE_GLOB 1
+#ifdef AST_INCLUDE_GLOB
+# include <glob.h>
+#endif
 #include <asterisk/config.h>
 #include <asterisk/config_pvt.h>
 #include <asterisk/cli.h>
@@ -778,6 +782,83 @@
 	} else {
 		snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
 	}
+#ifdef AST_INCLUDE_GLOB
+	{
+		/*
+		 * The following block tries to glob-expand the file name from 'fn'. 
+		 * If at least one match is found, loop over found matches and set
+		 * 'fn' to each of them in turn.
+		 *
+		 * In order to make the patch as least intrusive as possible it:
+		 *
+		 * 1. re-uses the variable 'fn'. This can be confusing, I guess.
+		 * 2. does not touch the indentation of code between its two parts.
+		 *    Just keep in mind that the first part opens two scopes which 
+		 *    are closed by the second part
+		 */
+		int glob_ret;
+		glob_t globbuf; /*pretend it is initilized to silent gcc */
+		globbuf.gl_offs = 0;
+		/* Options for glob(3):
+		 * GLOB_NOCHECK: 
+		 *   If no pattern matches, to return the original pattern.
+		 *   See remark on ahndling of GLOB_NOMATCH
+		 *
+		 * GLOB_BRACE:
+		 *   Allows '{a,b}' style brace expansion. This is a GNU extension and not 
+		 *   part of POSIX
+		 *
+		 * Some options currently not used but worth considering:
+		 * GLOB_ERR:
+		 *   Return upon read error (because a directory does not have 
+		 *   read permission, for example)
+		 *
+		 * GLOB_NOMAGIC:
+		 *   The pattern is returned if it contains no metacharacters. A GNU 
+		 *   extension.
+		 */
+		glob_ret = glob(fn, GLOB_NOCHECK|GLOB_BRACE, NULL, &globbuf);
+		ast_log(LOG_DEBUG,
+					"Glob Expansion Results: Return: %d, Number of results: %d,"
+					" (config file: %s, full path: %s\n",
+					glob_ret, globbuf.gl_pathc, configfile, fn);
+
+		
+		if (glob_ret==GLOB_NOMATCH){
+			/* The globbing found no matching file name.
+			 * 
+			 * Warn, but continue. Chances are that the user will get 
+			 * an error message later on fopen.
+			 *
+			 * Would it help to remember this error?
+			 */
+			ast_log(LOG_WARNING,
+					"Glob Expansion of pattern '%s' ('%s') failed: no match. "
+					"But I'll use the original pattern.\n",
+					configfile, fn);
+		}
+		if (glob_ret==GLOB_NOSPACE){
+			ast_log(LOG_WARNING,
+					"Glob Expansion of pattern '%s' ('%s') failed: Not enough memory\n",
+					configfile, fn);
+		} else if (glob_ret==GLOB_ABORTED){
+			ast_log(LOG_WARNING,
+					"Glob Expansion of pattern '%s' ('%s) failed: Read error\n",
+					configfile, fn);
+		} else  {
+			ast_log(LOG_DEBUG,
+					"Glob Expansion of pattern '%s' ('%s'): Success. "
+					"Starting to loop\n",
+					configfile, fn);
+	
+			/* loop over expanded files 
+			 * overrides the original value of fn.
+			 */
+			int i;
+			for (i=0; i<globbuf.gl_pathc; i++) {
+				strncpy(fn,globbuf.gl_pathv[i], sizeof(fn)-1);
+				ast_log(LOG_DEBUG, "Now trying file '%s'\n", fn);
+#endif
 	if ((option_verbose > 1) && !option_debug) {
 		ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
 		fflush(stdout);
@@ -819,6 +900,12 @@
 		else if (option_verbose > 1)
 			ast_verbose( "Not found (%s)\n", strerror(errno));
 	}
+#ifdef AST_INCLUDE_GLOB
+			}
+			globfree(&globbuf);
+		}
+	}
+#endif
 #ifdef PRESERVE_COMMENTS
 	if (master) {
 		/* Keep trailing comments */


More information about the asterisk-users mailing list