[asterisk-commits] branch russell/config-macros - r7279 in /team/russell/config-macros: channels...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Dec 1 20:42:13 CST 2005


Author: russell
Date: Thu Dec  1 20:42:11 2005
New Revision: 7279

URL: http://svn.digium.com/view/asterisk?rev=7279&view=rev
Log:
move the configuration parsing macros out of chan_oss and put them into
config.h with some modifications and documentation. Update the config parsing
in chan_oss to reflect the changes.

Modified:
    team/russell/config-macros/channels/chan_oss.c
    team/russell/config-macros/include/asterisk/config.h

Modified: team/russell/config-macros/channels/chan_oss.c
URL: http://svn.digium.com/view/asterisk/team/russell/config-macros/channels/chan_oss.c?rev=7279&r1=7278&r2=7279&view=diff
==============================================================================
--- team/russell/config-macros/channels/chan_oss.c (original)
+++ team/russell/config-macros/channels/chan_oss.c Thu Dec  1 20:42:11 2005
@@ -109,34 +109,6 @@
 .. and so on for the other cards.
 
  */
-
-/*
- * Helper macros to parse config arguments. They will go in a common
- * header file if their usage is globally accepted. In the meantime,
- * we define them here. Typical usage is as below.
- * Remember to open a block right before M_START (as it declares
- * some variables) and use the M_* macros WITHOUT A SEMICOLON:
- *
- *	{
- *		M_START(v->name, v->value) 
- *
- *		M_BOOL("dothis", x->flag1)
- *		M_STR("name", x->somestring)
- *		M_F("bar", some_c_code)
- *		M_END(some_final_statement)
- *		... other code in the block
- *	}
- *
- * XXX NOTE these macros should NOT be replicated in other parts of asterisk. 
- * Likely we will come up with a better way of doing config file parsing.
- */
-#define M_START(var, val) \
-        char *__s = var; char *__val = val;
-#define M_END(x)   x;
-#define M_F(tag, f)			if (!strcasecmp((__s), tag)) { f; } else
-#define M_BOOL(tag, dst)	M_F(tag, (dst) = ast_true(__val) )
-#define M_UINT(tag, dst)	M_F(tag, (dst) = strtoul(__val, NULL, 0) )
-#define M_STR(tag, dst)		M_F(tag, ast_copy_string(dst, __val, sizeof(dst)))
 
 /*
  * The following parameters are used in the driver:
@@ -1264,7 +1236,7 @@
  * invalid or dangerous values (the string is used as argument for
  * system("mixer %s")
  */
-static void store_mixer(struct chan_oss_pvt *o, char *s)
+static void store_mixer(struct chan_oss_pvt *o, const char *s)
 {
 	int i;
 
@@ -1284,7 +1256,7 @@
 /*
  * store the callerid components
  */
-static void store_callerid(struct chan_oss_pvt *o, char *s)
+static void store_callerid(struct chan_oss_pvt *o, const char *s)
 {
 	ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
 }
@@ -1294,7 +1266,6 @@
  */
 static struct chan_oss_pvt * store_config(struct ast_config *cfg, char *ctg)
 {
-	struct ast_variable *v;
 	struct chan_oss_pvt *o;
 
 	if (ctg == NULL) {
@@ -1316,23 +1287,22 @@
 
 	o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */
 	/* fill other fields from configuration */
-	for (v = ast_variable_browse(cfg, ctg);v; v=v->next) {
-		M_START(v->name, v->value);
-
-		M_BOOL("autoanswer", o->autoanswer)
-		M_BOOL("autohangup", o->autohangup)
-		M_BOOL("overridecontext", o->overridecontext)
-		M_STR("device", o->device)
-		M_UINT("frags", o->frags)
-		M_UINT("debug", oss_debug)
-		M_UINT("queuesize", o->queuesize)
-		M_STR("context", o->ctx)
-		M_STR("language", o->language)
-		M_STR("extension", o->ext)
-		M_F("mixer", store_mixer(o, v->value))
-		M_F("callerid", store_callerid(o, v->value))
-		M_END(;);
-	}
+	
+	AST_CONFIG_PARSE_BEGIN(cfg, ctg)
+		AST_CONFIG_BOOL("autoanswer", o->autoanswer)
+		AST_CONFIG_BOOL("autohangup", o->autohangup)
+		AST_CONFIG_BOOL("overridecontext", o->overridecontext)
+		AST_CONFIG_STR("device", o->device)
+		AST_CONFIG_UINT("frags", o->frags)
+		AST_CONFIG_UINT("debug", oss_debug)
+		AST_CONFIG_UINT("queuesize", o->queuesize)
+		AST_CONFIG_STR("context", o->ctx)
+		AST_CONFIG_STR("language", o->language)
+		AST_CONFIG_STR("extension", o->ext)
+		AST_CONFIG_CUSTOM("mixer", store_mixer(o, val))
+		AST_CONFIG_CUSTOM("callerid", store_callerid(o, val))
+	AST_CONFIG_PARSE_END	
+
 	if (ast_strlen_zero(o->device))
 		ast_copy_string(o->device, DEV_DSP, sizeof(o->device));
 	if (o->mixer_cmd) {

Modified: team/russell/config-macros/include/asterisk/config.h
URL: http://svn.digium.com/view/asterisk/team/russell/config-macros/include/asterisk/config.h?rev=7279&r1=7278&r2=7279&view=diff
==============================================================================
--- team/russell/config-macros/include/asterisk/config.h (original)
+++ team/russell/config-macros/include/asterisk/config.h Thu Dec  1 20:42:11 2005
@@ -187,6 +187,80 @@
 
 struct ast_config *ast_config_internal_load(const char *configfile, struct ast_config *cfg);
 
+/*!
+  The following are a set of macros to help with the parsing of an Asterisk
+  configuration file.  This how to use them...
+
+  All of the option parsing is done inside of these two macros:
+	AST_CONFIG_PARSE_BEGIN(cfg, context)
+		-- option parsing happens in here --
+	AST_CONFIG_PARSE_END
+  'cfg' is a pointer to a 'struct ast_config' that has already been initialized
+  prior to this point.  'context' is a string that indicates which context to
+  read from the config, 'cfg'.
+ 
+  Inside of these macros, all of the options to look for will be listed. There
+  are seperate macros to indicate different interpretations of the value that
+  has been specified in the configuration file.
+
+  For example, if the option's value string should be copied directly into a
+  character array, the following macro would be used:
+	AST_CONFIG_STR("context", peer->context)
+
+  There are also cases where another function should be called should an option
+  be found in a configuration file. It would also be common in this case to
+  need to be able to access to option's value directly. This can be done with
+  the variable 'val', which is defined in AST_CONFIG_PARSE_BEGIN.
+  For example:
+	AST_CONFIG_CUSTOM("mixer", store_mixer(o, val))
+
+  NOTE: It is very important that semicolons *ARE NOT* specified after the use
+  of any of these macros.
+*/
+
+/*!
+  \brief If 'tag' matches the current option, execute the function 'f'.
+*/
+#define AST_CONFIG_CUSTOM(tag, f)	if (!strcasecmp((__v->name), tag)) { f; } else
+
+/*!
+  \brief If 'tag' matches the current option, set 'dst' to the result of ast_true() 
+*/
+#define AST_CONFIG_BOOL(tag, dst)	AST_CONFIG_CUSTOM(tag, (dst) = ast_true(val) )
+
+/*!
+  \brief If 'tag' matches the current option, interpret the value as an unsigned
+         integer and store it in 'dst'.
+*/
+#define AST_CONFIG_UINT(tag, dst)	AST_CONFIG_CUSTOM(tag, (dst) = strtoul(val, NULL, 0) )
+
+/*!
+  \brief If 'tag' matches the current option, copy the value directly to 'dst'.
+*/
+#define AST_CONFIG_STR(tag, dst)	AST_CONFIG_CUSTOM(tag, ast_copy_string(dst, val, sizeof(dst)))
+
+/*!
+  \brief Begin parsing options from the given context in the configuration file
+         that has already been loaded.
+
+  If at any point during the parsing of configuration options, the value of of
+  the option needs to be directly accessed, it can be done with the variable,
+  'val'.
+*/
+#define AST_CONFIG_PARSE_BEGIN(cfg,context) \
+	do { \
+		struct ast_variable *__v; \
+		const char *val; \
+		for (__v = ast_variable_browse(cfg, context); __v; __v = __v->next) { \
+			val = __v->value;
+
+/*!
+  \brief End Config parsing.
+
+  This is used after a call to AST_CONFIG_PARSE_BEGIN.
+*/
+#define AST_CONFIG_PARSE_END ; } } while (0);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif



More information about the asterisk-commits mailing list