[asterisk-commits] rizzo: trunk r48553 - /trunk/main/manager.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Dec 18 04:28:38 MST 2006


Author: rizzo
Date: Mon Dec 18 05:28:37 2006
New Revision: 48553

URL: http://svn.digium.com/view/asterisk?view=rev&rev=48553
Log:
Replace ast_build_string with ast_str_*().

On passing remove presumably duplicate code to generate
the message for the manager_hooks:

in the previous version, the message was almost the same as the one sent
to regular sessions, with the exception of the empty line at the end, and
a few (presumably unintentional) differences e.g.  timestamps,
debugging, and lowercase headers for "event" and "privilege".

now we reuse the same message as before.


Modified:
    trunk/main/manager.c

Modified: trunk/main/manager.c
URL: http://svn.digium.com/view/asterisk/trunk/main/manager.c?view=diff&rev=48553&r1=48552&r2=48553
==============================================================================
--- trunk/main/manager.c (original)
+++ trunk/main/manager.c Mon Dec 18 05:28:37 2006
@@ -114,12 +114,6 @@
 static int num_sessions = 0;
 
 static int manager_debug;	/*!< enable some debugging code in the manager */
-
-AST_THREADSTORAGE(manager_event_buf);
-#define MANAGER_EVENT_BUF_INITSIZE   256
-
-AST_THREADSTORAGE(astman_append_buf);
-#define ASTMAN_APPEND_BUF_INITSIZE   256
 
 /*!
  * Descriptor for a manager session, either on the AMI socket or over HTTP.
@@ -302,24 +296,23 @@
 };
 
 /*! \brief Convert authority code to a list of options */
-static char *authority_to_str(int authority, char *res, int reslen)
+static char *authority_to_str(int authority, struct ast_str **res)
 {
 	int i;
-	char *dst = res, *sep = "";
-	size_t len = reslen;
-
-	res[0] = '\0';
+	char *sep = "";
+
+	(*res)->used = 0;
 	for (i = 0; i < (sizeof(perms) / sizeof(perms[0])) - 1; i++) {
 		if (authority & perms[i].num) {
-			ast_build_string(&dst, &len, "%s%s", sep, perms[i].label);
+			ast_str_append(res, 0, "%s%s", sep, perms[i].label);
 			sep = ",";
 		}
 	}
 
-	if (ast_strlen_zero(res))	/* replace empty string with something sensible */
-		ast_copy_string(res, "<none>", reslen);
-
-	return res;
+	if ((*res)->used == 0)	/* replace empty string with something sensible */
+		ast_str_append(res, 0, "<none>");
+
+	return (*res)->str;
 }
 
 /*! Tells you if smallstr exists inside bigstr
@@ -423,7 +416,7 @@
 static int handle_showmancmd(int fd, int argc, char *argv[])
 {
 	struct manager_action *cur;
-	char authority[80];
+	struct ast_str *authority = ast_str_alloca(80);
 	int num;
 
 	if (argc != 4)
@@ -435,7 +428,7 @@
 			if (!strcasecmp(cur->action, argv[num])) {
 				ast_cli(fd, "Action: %s\nSynopsis: %s\nPrivilege: %s\n%s\n",
 					cur->action, cur->synopsis,
-					authority_to_str(cur->authority, authority, sizeof(authority) -1),
+					authority_to_str(cur->authority, &authority),
 					S_OR(cur->description, "") );
 			}
 		}
@@ -535,7 +528,7 @@
 static int handle_showmancmds(int fd, int argc, char *argv[])
 {
 	struct manager_action *cur;
-	char authority[80];
+	struct ast_str *authority = ast_str_alloca(80);
 	char *format = "  %-15.15s  %-15.15s  %-55.55s\n";
 
 	ast_cli(fd, format, "Action", "Privilege", "Synopsis");
@@ -543,7 +536,7 @@
 
 	ast_mutex_lock(&actionlock);
 	for (cur = first_action; cur; cur = cur->next) /* Walk the list of actions */
-		ast_cli(fd, format, cur->action, authority_to_str(cur->authority, authority, sizeof(authority) -1), cur->synopsis);
+		ast_cli(fd, format, cur->action, authority_to_str(cur->authority, &authority), cur->synopsis);
 	ast_mutex_unlock(&actionlock);
 
 	return RESULT_SUCCESS;
@@ -763,7 +756,11 @@
 	return n < 0 ? -1 : 0;
 }
 
-/*
+/* XXX see if it can be moved inside the function */
+AST_THREADSTORAGE(astman_append_buf);
+#define ASTMAN_APPEND_BUF_INITSIZE   256
+
+/*!
  * utility functions for creating AMI replies
  */
 void astman_append(struct mansession *s, const char *fmt, ...)
@@ -1228,13 +1225,14 @@
 static int action_listcommands(struct mansession *s, struct message *m)
 {
 	struct manager_action *cur;
-	char temp[BUFSIZ];
+	struct ast_str *temp = ast_str_alloca(BUFSIZ); /* XXX very large ? */
 
 	astman_start_ack(s, m);
 	ast_mutex_lock(&actionlock);
 	for (cur = first_action; cur; cur = cur->next) { /* Walk the list of actions */
 		if ((s->writeperm & cur->authority) == cur->authority)
-			astman_append(s, "%s: %s (Priv: %s)\r\n", cur->action, cur->synopsis, authority_to_str(cur->authority, temp, sizeof(temp)));
+			astman_append(s, "%s: %s (Priv: %s)\r\n",
+				cur->action, cur->synopsis, authority_to_str(cur->authority, &temp));
 	}
 	ast_mutex_unlock(&actionlock);
 	astman_append(s, "\r\n");
@@ -2267,14 +2265,18 @@
 	return 0;
 }
 
+/* XXX see if can be moved inside the function */
+AST_THREADSTORAGE(manager_event_buf);
+#define MANAGER_EVENT_BUF_INITSIZE   256
+
 /*! \brief  manager_event: Send AMI event to client */
 int __manager_event(int category, const char *event,
 	const char *file, int line, const char *func, const char *fmt, ...)
 {
 	struct mansession *s;
 	struct manager_custom_hook *hook;
-	char auth[80];
-	char tmp[4096] = "";
+	struct ast_str *auth = ast_str_alloca(80);
+	const char *cat_str;
 	va_list ap;
 	struct timeval now;
 	struct ast_str *buf;
@@ -2286,9 +2288,10 @@
 	if (!(buf = ast_str_thread_get(&manager_event_buf, MANAGER_EVENT_BUF_INITSIZE)))
 		return -1;
 
+	cat_str = authority_to_str(category, &auth);
 	ast_str_set(&buf, 0,
 			"Event: %s\r\nPrivilege: %s\r\n",
-			 event, authority_to_str(category, auth, sizeof(auth)));
+			 event, cat_str);
 
 	if (timestampevents) {
 		now = ast_tvnow();
@@ -2325,16 +2328,8 @@
 
 	AST_RWLIST_RDLOCK(&manager_hooks);
 	if (!AST_RWLIST_EMPTY(&manager_hooks)) {
-		char *p;
-		int len;
-		snprintf(tmp, sizeof(tmp), "event: %s\r\nprivilege: %s\r\n", event, authority_to_str(category, tmp, sizeof(tmp)));
-                len = strlen(tmp);
-                p = tmp + len;
-                va_start(ap, fmt);
-                vsnprintf(p, sizeof(tmp) - len, fmt, ap);
-                va_end(ap);
 		AST_RWLIST_TRAVERSE(&manager_hooks, hook, list) {
-			hook->helper(category, event, tmp);
+			hook->helper(category, event, buf->str);
 		}
 	}
 	AST_RWLIST_UNLOCK(&manager_hooks);



More information about the asterisk-commits mailing list