[asterisk-commits] nadi: branch nadi/trunk-cm r44151 - in /team/nadi/trunk-cm: include/asterisk/...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Oct 2 08:58:44 MST 2006


Author: nadi
Date: Mon Oct  2 10:58:43 2006
New Revision: 44151

URL: http://svn.digium.com/view/asterisk?rev=44151&view=rev
Log:
some new macros in configman.h and fixes

Modified:
    team/nadi/trunk-cm/include/asterisk/configman.h
    team/nadi/trunk-cm/include/asterisk/hash.h
    team/nadi/trunk-cm/res/res_configman.c

Modified: team/nadi/trunk-cm/include/asterisk/configman.h
URL: http://svn.digium.com/view/asterisk/team/nadi/trunk-cm/include/asterisk/configman.h?rev=44151&r1=44150&r2=44151&view=diff
==============================================================================
--- team/nadi/trunk-cm/include/asterisk/configman.h (original)
+++ team/nadi/trunk-cm/include/asterisk/configman.h Mon Oct  2 10:58:43 2006
@@ -24,7 +24,10 @@
 #ifndef _ASTERISK_CONFIGMAN_H
 #define _ASTERISK_CONFIGMAN_H
 
+#include <stdlib.h>
 #include <sys/types.h>
+
+#include "asterisk/strings.h" 
 
 typedef struct cm    cm_t;
 
@@ -80,29 +83,6 @@
  */
 void     cm_destroy (cm_t *cm);
 
-/*! \brief Get a configuration value
- * \param cm_t which cm_t to use
- * \param buf where to put the result
- * \param size size of buf
- * \param sec_id section id to use
- * \param elem_id element id to use
- * \param ... integer or string id. omit this parameter, if the underlying cm_section_t is a single section type
- * Reads a value from the cm_t data structure and copies it into buf.
- *
- * Returns non-zero on error.
- */
-int      cm_get (cm_t *cm, char *buf, size_t size, int sec_id, int elem_id, ...);
-
-/*! \brief Validate an id
- * \param cm_t which cm_t to use
- * \param sec_id section id to use
- * \param ... integer or string id
- * Validates a given id.
- *
- * Returns true on success, zero if either id or sec_id is not valid.
- */
-int      cm_id_valid (cm_t *cm, int sec_id, ...);
-
 /*! \brief Get the subsequent id
  * \param cm_t which cm_t to use
  * \param sec_id section id to use
@@ -120,6 +100,81 @@
  */
 int      cm_get_next_id (cm_t *cm, int sec_id, void *prev, void *next);
 
-int      ast_configman_init (void);
+/*! \brief Get the subsequent id and rewinds if the end was reached
+ * \param cm_t which cm_t to use
+ * \param sec_id section id to use
+ * \param prev pointer to the previous id, if you want to retrieve the first id, give NULL
+ * \param next output pointer to the id after prev
+ * Gets the subsequent id of a given id. This variant succeeds and returns the first
+ * id if the last one was given as prev.
+ *
+ * Returns non-zero if next points to the id after prev, or zero if there
+ * is no subsequent id.
+ */
+int      cm_get_next_id_spin (cm_t *cm, int sec_id, void *prev, void *next);
+
+/*! \brief Validate an id
+ * \param cm_t which cm_t to use
+ * \param sec_id section id to use
+ * \param ... integer or string id
+ * Validates a given id.
+ *
+ * Returns true on success, zero if either id or sec_id is not valid.
+ */
+int      cm_id_valid (cm_t *cm, int sec_id, ...);
+
+/*! \brief Get a configuration value
+ * \param cm_t which cm_t to use
+ * \param buf where to put the result
+ * \param size size of buf
+ * \param sec_id section id to use
+ * \param elem_id element id to use
+ * \param ... integer or string id. omit this parameter, if the underlying cm_section_t is a single section type
+ * Reads a value from the cm_t data structure and copies it into buf.
+ *
+ * Returns non-zero on error.
+ */
+int      cm_get (cm_t *cm, char *buf, size_t size, int sec_id, int elem_id, ...);
+
+#define  cm_get_bool(cm,dest,sec_id,elem_id,...)	\
+({	\
+	char buf[32];	\
+	int retval = 0;		\
+	if (cm_get((cm), buf, sizeof(buf), (sec_id), (elem_id), ##__VA_ARGS__))	\
+		retval = -1;	\
+	else 	\
+		dest = ast_true(buf) ? 1 : 0;	\
+	retval;	\
+})
+
+#define  cm_get_int(cm,dest,sec_id,elem_id,...)	\
+({	\
+	char buf[32];	\
+	int retval = 0;		\
+	if (cm_get((cm), buf, sizeof(buf), (sec_id), (elem_id), ##__VA_ARGS__) ||	\
+		sscanf(buf, "%d", &(dest)) != 1)	\
+		retval = -1;	\
+	retval;	\
+})
+
+#define  cm_get_boolint(cm,dest,val_true,val_false,sec_id,elem_id,...)	\
+({	\
+	char buf[32];	\
+	int retval = 0;		\
+	if (cm_get((cm), buf, sizeof(buf), (sec_id), (elem_id), ##__VA_ARGS__))	\
+		retval = -1;	\
+	else if (sscanf(buf, "%d", &(dest)) != 1)	\
+		dest = ast_true(buf) ? val_true : val_false; \
+	retval;	\
+})
+
+#define  cm_get_strcasecmp(cm,str,sec_id,elem_id,...)	\
+({	\
+	char __buf[128];	\
+	int retval = -1;	\
+	if (!cm_get((cm), __buf, sizeof(__buf), (sec_id), (elem_id), ##__VA_ARGS__))	\
+		retval = strcasecmp((str), __buf);	\
+	retval;	\
+})
 
 #endif

Modified: team/nadi/trunk-cm/include/asterisk/hash.h
URL: http://svn.digium.com/view/asterisk/team/nadi/trunk-cm/include/asterisk/hash.h?rev=44151&r1=44150&r2=44151&view=diff
==============================================================================
--- team/nadi/trunk-cm/include/asterisk/hash.h (original)
+++ team/nadi/trunk-cm/include/asterisk/hash.h Mon Oct  2 10:58:43 2006
@@ -139,23 +139,25 @@
 
 
 
-#define AST_HASH_INSERT_NOLOCK(hash, key, val)	\
-({												\
-	int _pos = (hash)->_hash_f(key, (hash)->_size);	\
-	int _end = _pos;							\
- 	int _re = -1;								\
-	do {										\
-		if (!(hash)->_data[_pos]._used) {		\
-			(hash)->_data[_pos]._key = key;		\
-			(hash)->_data[_pos]._val = val;		\
-			(hash)->_data[_pos]._used = 1;		\
-			++(hash)->_length;					\
- 			_re = 0;							\
-			break;								\
-		}										\
-		_pos = (_pos + 1) % (hash)->_size;		\
-	} while (_pos != _end);						\
-	_re;										\
+#define AST_HASH_INSERT_NOLOCK(hash, key, val)		\
+({													\
+	int __pos = (hash)->_hash_f(key, (hash)->_size);\
+	int __end = __pos;								\
+ 	int __re = -1;									\
+	typeof(val) __valtmp = val;						\
+	if (AST_HASH_LOOKUP_NOLOCK((hash), (key), __valtmp))	\
+		do {										\
+			if (!(hash)->_data[__pos]._used) {		\
+				(hash)->_data[__pos]._key = key;	\
+				(hash)->_data[__pos]._val = val;	\
+				(hash)->_data[__pos]._used = 1;		\
+				++(hash)->_length;					\
+				__re = 0;							\
+				break;								\
+			}										\
+			__pos = (__pos + 1) % (hash)->_size;	\
+		} while (__pos != __end);					\
+	__re;											\
 })
 
 #define AST_HASH_INSERT(hash, key, val)			\
@@ -180,6 +182,7 @@
 			(hash)->_eq_f((hash)->_data[_pos]._key, key)) {	\
 			val = (hash)->_data[_pos]._val;		\
 			_re = 0;							\
+ 			break;								\
  		}										\
 		_pos = (_pos + 1) % (hash)->_size;		\
 	} while (_pos != _end);						\

Modified: team/nadi/trunk-cm/res/res_configman.c
URL: http://svn.digium.com/view/asterisk/team/nadi/trunk-cm/res/res_configman.c?rev=44151&r1=44150&r2=44151&view=diff
==============================================================================
--- team/nadi/trunk-cm/res/res_configman.c (original)
+++ team/nadi/trunk-cm/res/res_configman.c Mon Oct  2 10:58:43 2006
@@ -28,7 +28,6 @@
  */
 
 /* TODO:
- *  - write macros for easier variable access (cm_get_bool, cm_get_int, cm_get_strlist, cm_get_intlist, ...)
  *  - handle return values for *alloc, strdup, ast_cli_register
  */
 
@@ -39,7 +38,6 @@
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
 #include "asterisk/configman.h"
@@ -477,7 +475,8 @@
 
 	LOCK(cm);
 
-	if (cm->state != CM_LOADED || sec_id >= cm->num_secs || sec_id < 0)
+	if (cm->state != CM_LOADED || sec_id >= cm->num_secs || sec_id < 0 ||
+		cm->secs[sec_id].key_type == KTYPE_NONE)
 		goto err2;
 
 	hash = cm->hashes[sec_id];
@@ -487,8 +486,6 @@
 		goto err2;
 
 	switch (cm->secs[sec_id].key_type) {
-	case KTYPE_NONE:
-		goto err2;
 	case KTYPE_INTEGER:
 		if (!prev || *(int *)prev < 0) {
 			*(int *)next = keys[0].i;
@@ -511,12 +508,19 @@
 		}
 		retval = 1;
 		break;
+	case KTYPE_NONE:
+		goto err2;
 	}
 
 err2:
 	UNLOCK(cm);
 err1:
 	return retval;
+}
+
+int cm_get_next_id_spin (cm_t *cm, int sec_id, void *prev, void *next)
+{
+	return cm_get_next_id(cm, sec_id, prev, next) || cm_get_next_id(cm, sec_id, NULL, next);
 }
 
 /* cli commands for modules */
@@ -663,6 +667,9 @@
 	cm_t * cm;
 	char * retval = NULL;
 
+	if (pos != 4)
+		return NULL;
+
 	cm = __get_cm_from_line(line);
 	if (!cm)
 		return NULL;
@@ -708,6 +715,9 @@
 {
 	cm_t * cm;
 	char * retval = NULL;
+
+	if (pos != 4)
+		return NULL;
 
 	cm = __get_cm_from_line(line);
 	if (!cm)
@@ -762,15 +772,17 @@
 	if (!cm)
 		return NULL;
 
-	LOCK(cm);
 	if (pos == 4) {
+		LOCK(cm);
 		retval = __complete_section(cm, word, state);
+		UNLOCK(cm);
 	} else if (pos == 5) {
+		LOCK(cm);
 		sec = __get_sec_from_line(cm, line);
 		if (sec && sec->key_type != KTYPE_NONE)
 			retval = __complete_keys(cm, sec, word, state);
-	}
-	UNLOCK(cm);
+		UNLOCK(cm);
+	}
 
 	return retval;
 }
@@ -799,6 +811,8 @@
 		val = cm->vals[sec_id].rows[row][i];
 		if (!val)
 			val = cm->vals[sec_id].rows[0][i];
+		if (!val)
+			val = "";
 		term_color(wval, val, COLOR_BRWHITE, 0, sizeof(wval));
 		ast_cli(fd, " -> %s: %s\n", cm->secs[sec_id].directives[i].name, wval);
 	}
@@ -1161,9 +1175,8 @@
 	AST_HASH_LOCK(&cm_obj_hash);
 
 	ast_cli(fd, "Modules using configman: %d\n", AST_HASH_LENGTH_NOLOCK(&cm_obj_hash));
-	AST_HASH_TRAVERSE_NOLOCK(&cm_obj_hash, key, cm, i) {
+	AST_HASH_TRAVERSE_NOLOCK(&cm_obj_hash, key, cm, i)
 		ast_cli(fd, " >> %s\n", key);
-	}
 
 	AST_HASH_UNLOCK(&cm_obj_hash);
 



More information about the asterisk-commits mailing list