[svn-commits] eliel: branch group/appdocsxml r151324 - /team/group/appdocsxml/main/pbx.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Mon Oct 20 18:13:35 CDT 2008


Author: eliel
Date: Mon Oct 20 18:13:34 2008
New Revision: 151324

URL: http://svn.digium.com/view/asterisk?view=rev&rev=151324
Log:
Fix some of the things pointed by russellb on the reviewboard.
 - Change #define with const vars (and add some comments).
 - Move the LIST to a RWLIST.
 - Move the alloca()ted strings to ast_malloc() and free them all when done.
 - Constant values inside a structure must be set as 'const'.

Modified:
    team/group/appdocsxml/main/pbx.c

Modified: team/group/appdocsxml/main/pbx.c
URL: http://svn.digium.com/view/asterisk/team/group/appdocsxml/main/pbx.c?view=diff&rev=151324&r1=151323&r2=151324
==============================================================================
--- team/group/appdocsxml/main/pbx.c (original)
+++ team/group/appdocsxml/main/pbx.c Mon Oct 20 18:13:34 2008
@@ -824,18 +824,25 @@
 };
 
 #ifdef XML_DOCUMENTATION
-#define DEFAULT_DOCUMENTATION_LANGUAGE "en_US"
-#define XMLDOC_TEXT_COLUMNS 74
-#define XMLDOC_MAX_DIFF 5
+/*! \brief Default documentation language. */
+static const char default_documentation_language[] = "en_US";
+/*! \brief Number of columns to print when showing the XML documentation with a 
+ *         'core show application/function *' CLI command. Used in text wrapping.*/
+static const int xmldoc_text_columns = 74;
+/*! \brief This is a value that we will use to let the wrapping mechanism move the cursor
+ *         backward and forward xmldoc_max_diff positions before cutting the middle of a
+ *         word, trying to find a space or a \n. */
+static const int xmldoc_max_diff = 5;
 /*! \brief XML documentation language. */
 static char documentation_language[80];
-/*! \brief XML documentation tree. */
+/*! \brief XML documentation tree, here we will allocate the pointers to the
+ *         ast_xml_docs open. */
 struct documentation_tree {
 	char *filename;
 	ast_xml_doc *doc;
-	AST_LIST_ENTRY(documentation_tree) entry;
+	AST_RWLIST_ENTRY(documentation_tree) entry;
 };
-static AST_LIST_HEAD_STATIC(xmldoc_tree, documentation_tree);
+static AST_RWLIST_HEAD_STATIC(xmldoc_tree, documentation_tree);
 #endif
 
 static int pbx_builtin_answer(struct ast_channel *, void *);
@@ -3005,38 +3012,13 @@
 	if (!(acf = ast_custom_function_find(a->argv[3]))) {
 		ast_cli(a->fd, "No function by that name registered.\n");
 		return CLI_FAILURE;
-
-	}
-#ifndef XML_DOCUMENTATION
-	if (acf->synopsis)
-		synopsis_size = strlen(acf->synopsis) + 23;
-	else
-		synopsis_size = strlen("Not available") + 23;
-	synopsis = alloca(synopsis_size);
-
-	if (acf->desc)
-		description_size = strlen(acf->desc) + 23;
-	else
-		description_size = strlen("Not available") + 23;
-	description = alloca(description_size);
-
-	if (acf->arguments)
-		arguments_size = strlen(acf->arguments) + 23;
-	else
-		arguments_size = strlen("Not available") + 23;
-	arguments = alloca(arguments_size);
-
-	if (acf->seealso)
-		seealso_size = strlen(acf->seealso) + 23;
-	else
-		seealso_size = strlen("Not available") + 23;
-	seealso = alloca(seealso_size);	
-#endif
+	}
+
 	if (acf->syntax)
 		syntax_size = strlen(acf->syntax) + 23;
 	else
 		syntax_size = strlen("Not available") + 23;
-	syntax = alloca(syntax_size);
+	syntax = ast_malloc(syntax_size);
 
 	snprintf(info, 64 + AST_MAX_APP, "\n  -= Info about function '%s' =- \n\n", acf->name);
 	term_color(infotitle, info, COLOR_MAGENTA, 0, 64 + AST_MAX_APP + 22);
@@ -3045,37 +3027,51 @@
 	term_color(syntitle, "[Synopsis]\n", COLOR_MAGENTA, 0, 40);
 	term_color(destitle, "[Description]\n", COLOR_MAGENTA, 0, 40);
 	term_color(seealsotitle, "[See Also]\n", COLOR_MAGENTA, 0, 40);
-	term_color(syntax,
-		   acf->syntax ? acf->syntax : "Not available",
-		   COLOR_CYAN, 0, syntax_size);
+	term_color(syntax, acf->syntax ? acf->syntax : "Not available", COLOR_CYAN, 0, syntax_size);
 #ifdef XML_DOCUMENTATION
 	arguments = xmldoc_colorization(acf->arguments ? acf->arguments : "Not available", COLOR_CYAN, COLOR_BLACK);
 	synopsis = xmldoc_colorization(acf->synopsis ? acf->synopsis : "Not available", COLOR_CYAN, COLOR_BLACK);
 	description = xmldoc_colorization(acf->desc ? acf->desc : "Not available", COLOR_CYAN, COLOR_BLACK);
 	seealso = xmldoc_colorization(acf->seealso ? acf->seealso : "Not available", COLOR_CYAN, COLOR_BLACK);
 #else
-	term_color(arguments,
-		  acf->arguments ? acf->arguments : "Not available",
-		  COLOR_CYAN, 0, arguments_size);
-	term_color(synopsis,
-		  acf->synopsis ? acf->synopsis : "Not available",
-		  COLOR_CYAN, 0, synopsis_size);
-	term_color(description,
-		  acf->desc ? acf->desc : "Not available",
-		  COLOR_CYAN, 0, description_size);
-	term_color(seealso,
-		  acf->seealso ? acf->seealso : "Not available",
-		  COLOR_CYAN, 0, seealso_size);
+	if (acf->synopsis)
+		synopsis_size = strlen(acf->synopsis) + 23;
+	else
+		synopsis_size = strlen("Not available") + 23;
+	synopsis = ast_malloc(synopsis_size);
+
+	if (acf->desc)
+		description_size = strlen(acf->desc) + 23;
+	else
+		description_size = strlen("Not available") + 23;
+	description = ast_malloc(description_size);
+
+	if (acf->arguments)
+		arguments_size = strlen(acf->arguments) + 23;
+	else
+		arguments_size = strlen("Not available") + 23;
+	arguments = ast_malloc(arguments_size);
+
+	if (acf->seealso)
+		seealso_size = strlen(acf->seealso) + 23;
+	else
+		seealso_size = strlen("Not available") + 23;
+	seealso = ast_malloc(seealso_size);
+
+	term_color(arguments, acf->arguments ? acf->arguments : "Not available", COLOR_CYAN, 0, arguments_size);
+	term_color(synopsis, acf->synopsis ? acf->synopsis : "Not available", COLOR_CYAN, 0, synopsis_size);
+	term_color(description, acf->desc ? acf->desc : "Not available", COLOR_CYAN, 0, description_size);
+	term_color(seealso, acf->seealso ? acf->seealso : "Not available", COLOR_CYAN, 0, seealso_size);
 #endif
 
 	ast_cli(a->fd,"%s%s%s\n\n%s%s\n\n%s%s\n\n%s%s\n\n%s%s\n", infotitle, stxtitle, syntax, argtitle, arguments, 
 		syntitle, synopsis, destitle, description, seealsotitle, seealso);
-#ifdef XML_DOCUMENTATION
+
 	ast_free(arguments);
 	ast_free(synopsis);
 	ast_free(description);
 	ast_free(seealso);
-#endif
+	ast_free(syntax);
 
 	return CLI_SUCCESS;
 }
@@ -3125,12 +3121,12 @@
 
 #ifdef XML_DOCUMENTATION
 static const struct strcolorized_tags {
-	char *init;		/*!< Replace initial tag with this string. */
-	char *end;		/*!< Replace end tag with this string. */
-	int colorfg;		/*!< Foreground color. */
-	int colorbg;		/*!< Background color. */
-	char *inittag;		/*!< Initial tag description. */
-	char *endtag;		/*!< Ending tag description. */
+	const char *init;		/*!< Replace initial tag with this string. */
+	const char *end;		/*!< Replace end tag with this string. */
+	const int colorfg;		/*!< Foreground color. */
+	const int colorbg;		/*!< Background color. */
+	const char *inittag;		/*!< Initial tag description. */
+	const char *endtag;		/*!< Ending tag description. */
 } colorized_tags[] = {
 	{ "<", ">", COLOR_GREEN, COLOR_BLACK, "<replaceable>", "</replaceable>" },
 	{ "\'", "\'", COLOR_BLUE, COLOR_BLACK, "<literal>", "</literal>" },
@@ -3148,9 +3144,9 @@
 };
 
 static const struct strspecial_tags {
-	char *tagname;		/*!< Special tag name. */
-	char *init;		/*!< Print this at the beginning. */
-	char *end;		/*!< Print this at the end. */
+	const char *tagname;		/*!< Special tag name. */
+	const char *init;		/*!< Print this at the beginning. */
+	const char *end;		/*!< Print this at the end. */
 } special_tags[] = {
 	{ "note", "<note>NOTE:</note> ", "" },
 	{ "warning", "<warning>WARNING!!!:</warning> ", "" }
@@ -3399,7 +3395,7 @@
 
 	/* Wrap the text, notice that string wrap will avoid cutting an ESC sequence. */
 	if (colorized) {
-		wrapped = xmldoc_string_wrap(colorized, XMLDOC_TEXT_COLUMNS, XMLDOC_MAX_DIFF);
+		wrapped = xmldoc_string_wrap(colorized, xmldoc_text_columns, xmldoc_max_diff);
 		ast_free(colorized);
 	}
 
@@ -3460,7 +3456,7 @@
 	struct documentation_tree *doctree;
 	char *lang;
 
-	AST_LIST_LOCK(&xmldoc_tree);
+	AST_RWLIST_RDLOCK(&xmldoc_tree);
 	AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) {
 		/* the core xml documents have priority over thirdparty document. */
 		node = ast_xml_get_root(doctree->doc);
@@ -3485,16 +3481,16 @@
 			node = ast_xml_get_root(doctree->doc);
 			if (node->AST_XML_CHILD) {
 				if ((node = ast_xml_find_element(node->AST_XML_CHILD, type, "name", name))) {
-					AST_LIST_UNLOCK(&xmldoc_tree);
+					AST_RWLIST_UNLOCK(&xmldoc_tree);
 					return node;
 				}
 			}
 		} else {
-			AST_LIST_UNLOCK(&xmldoc_tree);
+			AST_RWLIST_UNLOCK(&xmldoc_tree);
 			return node;
 		}
 	}
-	AST_LIST_UNLOCK(&xmldoc_tree);
+	AST_RWLIST_UNLOCK(&xmldoc_tree);
 
 	return NULL;
 }
@@ -6310,7 +6306,7 @@
 	glob_t globbuf;
 
 	/* setup default XML documentation language */
-	snprintf(documentation_language, sizeof(documentation_language), DEFAULT_DOCUMENTATION_LANGUAGE);
+	snprintf(documentation_language, sizeof(documentation_language), default_documentation_language);
 
 	if (!(cfg = ast_config_load("asterisk.conf", cnfflags))) {
 		ast_log(LOG_ERROR, "No asterisk.conf? That cannot be good.\n");
@@ -6343,7 +6339,7 @@
 	}
 	ast_free(xmlpattern);
 
-	AST_LIST_LOCK(&xmldoc_tree);
+	AST_RWLIST_WRLOCK(&xmldoc_tree);
 	/* loop over expanded files */
 	for (i = 0; i < globbuf.gl_pathc; i++) {
 		tmpdoc = NULL;
@@ -6373,9 +6369,9 @@
 		}
 		doc_tree->doc = tmpdoc;
 		doc_tree->filename = ast_strdup(globbuf.gl_pathv[i]);
-		AST_LIST_INSERT_TAIL(&xmldoc_tree, doc_tree, entry);
-	}
-	AST_LIST_UNLOCK(&xmldoc_tree);
+		AST_RWLIST_INSERT_TAIL(&xmldoc_tree, doc_tree, entry);
+	}
+	AST_RWLIST_UNLOCK(&xmldoc_tree);
 
 	globfree(&globbuf);
 
@@ -6468,10 +6464,10 @@
 				char infotitle[64 + AST_MAX_APP + 22], syntitle[40], destitle[40], stxtitle[40], argtitle[40];
 				char seealsotitle[40];
 				char info[64 + AST_MAX_APP], *synopsis = NULL, *description = NULL, *syntax = NULL, *arguments = NULL;
-				char *seealso;
+				char *seealso = NULL;
 				int syntax_size;
 #ifndef XML_DOCUMENTATION
-				int synopsis_size, description_size, arguments_size, seealso_size; 
+				int synopsis_size, description_size, arguments_size, seealso_size;
 #endif
 
 				no_registered_app = 0;
@@ -6480,32 +6476,31 @@
 					synopsis_size = strlen(aa->synopsis) + 23;
 				else
 					synopsis_size = strlen("Not available") + 23;
-				synopsis = alloca(synopsis_size);
+				synopsis = ast_malloc(synopsis_size);
 
 				if (aa->description)
 					description_size = strlen(aa->description) + 23;
 				else
 					description_size = strlen("Not available") + 23;
-				description = alloca(description_size);
+				description = ast_malloc(description_size);
+
+				if (aa->arguments)
+					arguments_size = strlen(aa->arguments) + 23;
+				else
+					arguments_size = strlen("Not available") + 23;
+				arguments = ast_malloc(arguments_size);
+
+				if (aa->seealso)
+					seealso_size = strlen(aa->seealso) + 23;
+				else
+					seealso_size = strlen("Not available") + 23;
+				seealso = ast_malloc(seealso_size);
 #endif
 				if (aa->syntax)
 					syntax_size = strlen(aa->syntax) + 23;
 				else
 					syntax_size = strlen("Not available") + 23;
-				syntax = alloca(syntax_size);
-#ifndef XML_DOCUMENTATION
-				if (aa->arguments)
-					arguments_size = strlen(aa->arguments) + 23;
-				else
-					arguments_size = strlen("Not available") + 23;
-				arguments = alloca(arguments_size);
-			
-				if (aa->seealso)
-					seealso_size = strlen(aa->seealso) + 23;
-				else
-					seealso_size = strlen("Not available") + 23;
-				seealso = alloca(seealso_size);
-#endif
+				syntax = ast_malloc(syntax_size);
 
 #ifdef XML_DOCUMENTATION
 				if (syntax) {
@@ -6520,37 +6515,20 @@
 					term_color(destitle, "[Description]\n", COLOR_MAGENTA, 0, 40);
 					term_color(seealsotitle, "[See Also]\n", COLOR_MAGENTA, 0, 40);
 #ifdef XML_DOCUMENTATION
-					description = xmldoc_colorization(aa->description ? aa->description : "Not available", COLOR_CYAN, COLOR_BLACK);
-					arguments = xmldoc_colorization(aa->arguments ? aa->arguments : "Not available", COLOR_CYAN, COLOR_BLACK);
-					synopsis = xmldoc_colorization(aa->synopsis ? aa->synopsis : "Not available", COLOR_CYAN, COLOR_BLACK);
-					seealso = xmldoc_colorization(aa->seealso ? aa->seealso : "Not available", COLOR_CYAN, COLOR_BLACK);
+					description = xmldoc_colorization(S_OR(aa->description, "Not available"), COLOR_CYAN, COLOR_BLACK);
+					arguments = xmldoc_colorization(S_OR(aa->arguments, "Not available"), COLOR_CYAN, COLOR_BLACK);
+					synopsis = xmldoc_colorization(S_OR(aa->synopsis, "Not available"), COLOR_CYAN, COLOR_BLACK);
+					seealso = xmldoc_colorization(S_OR(aa->seealso, "Not available"), COLOR_CYAN, COLOR_BLACK);
 #else
-					term_color(synopsis,
-									aa->synopsis ? aa->synopsis : "Not available",
-									COLOR_CYAN, 0, synopsis_size);
-
-					term_color(description,
-									aa->description ? aa->description : "Not available",
-									COLOR_CYAN, 0, description_size);
-					term_color(arguments,
-									aa->arguments ? aa->arguments : "Not available",
-									COLOR_CYAN, 0, arguments_size);
-					term_color(seealso,
-									aa->seealso ? aa->seealso : "Not available",
-									COLOR_CYAN, 0, seealso_size);
+					term_color(synopsis, S_OR(aa->synopsis, "Not available"), COLOR_CYAN, 0, synopsis_size);
+					term_color(description, S_OR(aa->description, "Not available"),	COLOR_CYAN, 0, description_size);
+					term_color(arguments, S_OR(aa->arguments, "Not available"), COLOR_CYAN, 0, arguments_size);
+					term_color(seealso, S_OR(aa->seealso, "Not available"), COLOR_CYAN, 0, seealso_size);
 #endif
-					term_color(syntax,
-									aa->syntax ? aa->syntax : "Not available",
-									COLOR_CYAN, 0, syntax_size);
+					term_color(syntax, S_OR(aa->syntax, "Not available"), COLOR_CYAN, 0, syntax_size);
 
 					ast_cli(a->fd,"%s%s%s\n\n%s%s\n\n%s%s\n\n%s%s\n\n%s%s\n", infotitle, stxtitle, syntax, argtitle, 
 						arguments, syntitle, synopsis, destitle, description, seealsotitle, seealso);
-#ifdef XML_DOCUMENTATION
-					ast_free(description);
-					ast_free(arguments);
-					ast_free(synopsis);
-					ast_free(seealso);
-#endif
 				} else {
 					/* ... one of our applications, show info ...*/
 					ast_cli(a->fd,"\n  -= Info about application '%s' =- \n\n"
@@ -6560,12 +6538,17 @@
 						"[Description]\n%s\n"
 						"[See Also]\n%s\n",
 						aa->name,
-						aa->syntax ? aa->syntax : "Not available",
-						aa->arguments ? aa->arguments : "Not available",
-						aa->synopsis ? aa->synopsis : "Not available",
-						aa->description ? aa->description : "Not available",
-						aa->seealso ? aa->seealso : "Not available");
+						S_OR(aa->syntax, "Not available"),
+						S_OR(aa->arguments, "Not available"),
+						S_OR(aa->synopsis, "Not available"),
+						S_OR(aa->description, "Not available"),
+						S_OR(aa->seealso, "Not available"));
 				}
+				ast_free(description);
+				ast_free(arguments);
+				ast_free(synopsis);
+				ast_free(seealso);
+				ast_free(syntax);
 			}
 		}
 	}




More information about the svn-commits mailing list