[Asterisk-code-review] core/pbx: dialplan show - display filename/line# (asterisk[master])

Jonathan R. Rose asteriskteam at digium.com
Wed Dec 14 16:35:09 CST 2016


Jonathan R. Rose has uploaded a new change for review. ( https://gerrit.asterisk.org/4633 )

Change subject: core/pbx: dialplan show - display filename/line#
......................................................................

core/pbx: dialplan show - display filename/line#

Adds the ability for extensions to be registered to include filename and
line number so that dialplan show output can show the filename and line
number of a config file responsible for generating a given extension.

This only affects config modules that are written to use the new extension
registering functions. In this patch, that only includes pbx_config, so
extensions registered in extensions.conf and any included extension will
be shown in this manner. Extensions registered in this manner will show
the filename and line number *instead* of the registrar.

ASTERISK-26658 #close
Reported by: Jonathan R. Rose

Change-Id: Ieccc6abccdff34ed5c7da3511fd24972b8f2dd30
---
M CHANGES
M include/asterisk/pbx.h
M main/pbx.c
M pbx/pbx_config.c
4 files changed, 134 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/33/4633/1

diff --git a/CHANGES b/CHANGES
index 4c3d8db..ad16a40 100644
--- a/CHANGES
+++ b/CHANGES
@@ -68,6 +68,12 @@
    created channel using options parameter (like app_dial) B() and b().  This
    allows for adding variables to newly created channel or, e.g. setting callerid.
 
+CLI Commands
+------------------
+ * 'dialplan show' output will now show [config_file:line_number] instead of
+   [registrar] when that information is available. Currently only extensions
+   registered by pbx_config when loading/reloading will use this format.
+
 ------------------------------------------------------------------------------
 --- Functionality changes from Asterisk 14.2.0 to Asterisk 14.3.0 ------------
 ------------------------------------------------------------------------------
diff --git a/include/asterisk/pbx.h b/include/asterisk/pbx.h
index 00ca4ac..3dce7dc 100644
--- a/include/asterisk/pbx.h
+++ b/include/asterisk/pbx.h
@@ -512,6 +512,26 @@
 	const char *application, void *data, void (*datad)(void *), const char *registrar);
 
 /*!
+ * \brief Add an extension as with ast_add_extension2, but include file name and line number
+ *        to help users track the origin of extensions. Recommended for config files that add
+ *        PBX extensions.
+ * \since 15.0.0
+ *
+ * \param registrar_file Configuration file name which will appear in dialplan show cli output
+ *                       for this extension. It's recommended for consumers of this function to
+ *                       strip full paths to just filenames.
+ *
+ * \param registrar_line Line number indicating what line of the configuration file is responsible
+ *                       for adding this extension.
+ *
+ * \note For details about the rest of the arguments, check ast_add_extension()
+ */
+int ast_add_extension2_with_lines(struct ast_context *con, int replace, const char *extension,
+	int priority, const char *label, const char *callerid,
+	const char *application, void *data, void (*datad)(void *), const char *registrar,
+	const char *registrar_file, int registrar_line);
+
+/*!
  * \brief Same as ast_add_extension2, but assumes you have already locked context
  * \since 12.0.0
  *
@@ -521,6 +541,19 @@
 int ast_add_extension2_nolock(struct ast_context *con, int replace, const char *extension,
 	int priority, const char *label, const char *callerid,
 	const char *application, void *data, void (*datad)(void *), const char *registrar);
+
+/*!
+ * \brief Sames as ast_add_extension2_nolock, but includes file name and line number in the
+ *        same manner as ast_add_extension2_with_lines.
+ * \since 15.0.0
+ *
+ * \note con must be write locked prior to calling. For details about hte arguments,
+ *       check ast_add_extension() and ast_add_extension2_with_lines()
+ */
+int ast_add_extension2_nolock_with_lines(struct ast_context *con, int replace,
+	const char *extension, int priority, const char *label, const char *callerid,
+	const char *application, void *data, void (*datad)(void *), const char *registrar,
+	const char *registrar_file, int registrar_line);
 
 /*!
  * \brief Map devstate to an extension state.
@@ -1266,6 +1299,23 @@
 const char *ast_get_switch_registrar(const struct ast_sw *sw);
 /*! @} */
 
+/*! 
+ * \brief Get name of configuration file used by registrar to register this extension
+ *
+ * \retval NULL if registrar did not indicate config file when registering the extension
+ * \retval name of the file used to register the extension
+ */
+const char *ast_get_extension_registrar_file(struct ast_exten *e);
+
+/*!
+ * \brief Get line number of configuration file used by registrar to register this extension
+ *
+ * \retval -1 if the line wasn't indicated when the extension was registered
+ * \retval 0 or positive integer indicating what line in the config file was responsible for
+ *           registering the extension.
+ */
+int ast_get_extension_registrar_line(struct ast_exten *e);
+
 /*! @name Walking functions ... */
 /*! @{ */
 struct ast_context *ast_walk_contexts(struct ast_context *con);
diff --git a/main/pbx.c b/main/pbx.c
index 208c230..a12ff13 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -251,6 +251,8 @@
 	struct ast_hashtab *peer_table;    /*!< Priorities list in hashtab form -- only on the head of the peer list */
 	struct ast_hashtab *peer_label_table; /*!< labeled priorities in the peers -- only on the head of the peer list */
 	const char *registrar;		/*!< Registrar */
+	const char *registrar_file;     /*!< File name used to register extension */
+	int registrar_line;             /*!< Line number the extension was registered in text */
 	struct ast_exten *next;		/*!< Extension with a greater ID */
 	char stuff[0];
 };
@@ -650,7 +652,8 @@
 static int ast_add_extension2_lockopt(struct ast_context *con,
 	int replace, const char *extension, int priority, const char *label, const char *callerid,
 	const char *application, void *data, void (*datad)(void *),
-	const char *registrar, int lock_context);
+	const char *registrar, int lock_context,
+	const char *registrar_file, int registrar_line);
 static struct ast_context *find_context_locked(const char *context);
 static struct ast_context *find_context(const char *context);
 static void get_device_state_causing_channels(struct ao2_container *c);
@@ -5438,6 +5441,21 @@
 	}
 }
 
+/*! \brief Writes CLI output of a single extension for show dialplan */
+static void show_dialplan_helper_extension_output(int fd, char *buf1, char *buf2, struct ast_exten *exten)
+{
+	if (ast_get_extension_registrar_file(exten)) {
+		ast_cli(fd, "  %-17s %-45s [%s:%d]\n",
+			buf1, buf2,
+			ast_get_extension_registrar_file(exten),
+			ast_get_extension_registrar_line(exten));
+		return;
+	}
+
+	ast_cli(fd, "  %-17s %-45s [%s]\n",
+		buf1, buf2, ast_get_extension_registrar(exten));
+}
+
 /* XXX not verified */
 static int show_dialplan_helper(int fd, const char *context, const char *exten, struct dialplan_counters *dpc, const struct ast_include *rinclude, int includecount, const char *includes[])
 {
@@ -5515,8 +5533,7 @@
 
 			print_ext(e, buf2, sizeof(buf2));
 
-			ast_cli(fd, "  %-17s %-45s [%s]\n", buf, buf2,
-				ast_get_extension_registrar(e));
+			show_dialplan_helper_extension_output(fd, buf, buf2, e);
 
 			dpc->total_exten++;
 			/* walk next extension peers */
@@ -5530,8 +5547,7 @@
 					buf[0] = '\0';
 				print_ext(p, buf2, sizeof(buf2));
 
-				ast_cli(fd,"  %-17s %-45s [%s]\n", buf, buf2,
-					ast_get_extension_registrar(p));
+				show_dialplan_helper_extension_output(fd, buf, buf2, p);
 			}
 		}
 
@@ -6355,8 +6371,9 @@
 
 				dupdstr = ast_strdup(prio_item->data);
 
-				res1 = ast_add_extension2(new, 0, prio_item->name, prio_item->priority, prio_item->label,
-										  prio_item->matchcid ? prio_item->cidmatch : NULL, prio_item->app, dupdstr, ast_free_ptr, prio_item->registrar);
+				res1 = ast_add_extension2_with_lines(new, 0, prio_item->name, prio_item->priority, prio_item->label,
+										  prio_item->matchcid ? prio_item->cidmatch : NULL, prio_item->app, dupdstr, ast_free_ptr, prio_item->registrar,
+										  prio_item->registrar_file, prio_item->registrar_line);
 				if (!res1 && new_exten_item && new_prio_item){
 					ast_verb(3,"Dropping old dialplan item %s/%s/%d [%s(%s)] (registrar=%s) due to conflict with new dialplan\n",
 							context->name, prio_item->name, prio_item->priority, prio_item->app, (char*)prio_item->data, prio_item->registrar);
@@ -6867,7 +6884,7 @@
 	c = find_context(context);
 	if (c) {
 		ret = ast_add_extension2_lockopt(c, replace, extension, priority, label, callerid,
-			application, data, datad, registrar, 1);
+			application, data, datad, registrar, 1, NULL, -1);
 	}
 
 	return ret;
@@ -7193,7 +7210,17 @@
 	const char *registrar)
 {
 	return ast_add_extension2_lockopt(con, replace, extension, priority, label, callerid,
-		application, data, datad, registrar, 1);
+		application, data, datad, registrar, 1, NULL, -1);
+}
+
+int ast_add_extension2_with_lines(struct ast_context *con,
+	int replace, const char *extension, int priority, const char *label, const char *callerid,
+	const char *application, void *data, void (*datad)(void *),
+	const char *registrar, const char *registrar_file, int registrar_line)
+{
+	return ast_add_extension2_lockopt(con, replace, extension, priority, label, callerid,
+		application, data, datad, registrar, 1, registrar_file, registrar_line);
+
 }
 
 int ast_add_extension2_nolock(struct ast_context *con,
@@ -7202,9 +7229,17 @@
 	const char *registrar)
 {
 	return ast_add_extension2_lockopt(con, replace, extension, priority, label, callerid,
-		application, data, datad, registrar, 0);
+		application, data, datad, registrar, 0, NULL, -1);
 }
 
+int ast_add_extension2_nolock_with_lines(struct ast_context *con, int replace,
+	const char *extension, int priority, const char *label, const char *callerid,
+	const char *application, void *data, void (*datad)(void *), const char *registrar,
+	const char *registrar_file, int registrar_line)
+{
+	return ast_add_extension2_lockopt(con, replace, extension, priority, label, callerid,
+		application, data, datad, registrar, 0, registrar_file, registrar_line);
+}
 
 /*!
  * \brief Same as ast_add_extension2() but controls the context locking.
@@ -7216,7 +7251,7 @@
 static int ast_add_extension2_lockopt(struct ast_context *con,
 	int replace, const char *extension, int priority, const char *label, const char *callerid,
 	const char *application, void *data, void (*datad)(void *),
-	const char *registrar, int lock_context)
+	const char *registrar, int lock_context, const char *registrar_file, int registrar_line)
 {
 	/*
 	 * Sort extensions (or patterns) according to the rules indicated above.
@@ -7287,6 +7322,9 @@
 	} else {
 		length ++;	/* just the '\0' */
 	}
+	if (registrar_file) {
+		length += strlen(registrar_file) + 1;
+	}
 
 	/* Be optimistic:  Build the extension structure first */
 	if (!(tmp = ast_calloc(1, length)))
@@ -7326,12 +7364,22 @@
 		*p++ = '\0';
 		tmp->matchcid = AST_EXT_MATCHCID_OFF;
 	}
+
+	if (registrar_file) {
+		tmp->registrar_file = p;
+		strcpy(p, registrar_file);
+		p += strlen(registrar_file) + 1;
+	} else {
+		tmp->registrar_file = NULL;
+	}
+
 	tmp->app = p;
 	strcpy(p, application);
 	tmp->parent = con;
 	tmp->data = data;
 	tmp->datad = datad;
 	tmp->registrar = registrar;
+	tmp->registrar_line = registrar_line;
 
 	if (lock_context) {
 		ast_wrlock_context(con);
@@ -8485,6 +8533,16 @@
 	return e ? e->registrar : NULL;
 }
 
+const char *ast_get_extension_registrar_file(struct ast_exten *e)
+{
+	return e ? e->registrar_file : NULL;
+}
+
+int ast_get_extension_registrar_line(struct ast_exten *e)
+{
+	return e ? e->registrar_line : -1;
+}
+
 int ast_get_extension_matchcid(struct ast_exten *e)
 {
 	return e ? e->matchcid : 0;
diff --git a/pbx/pbx_config.c b/pbx/pbx_config.c
index 384bbc7..f5f95d4 100644
--- a/pbx/pbx_config.c
+++ b/pbx/pbx_config.c
@@ -1855,6 +1855,7 @@
 
 				appl = ast_skip_blanks(appl);
 				if (ipri) {
+					const char *registrar_file;
 					if (plus) {
 						ipri += atoi(plus);
 					}
@@ -1864,7 +1865,14 @@
 							"The use of '%s' for an extension is strongly discouraged and can have unexpected behavior.  Please use '_X%c' instead at line %d of %s\n",
 							realext, realext[1], v->lineno, vfile);
 					}
-					if (ast_add_extension2(con, 0, realext, ipri, label, cidmatch, appl, ast_strdup(data), ast_free_ptr, registrar)) {
+					/* Don't include full path if the configuration file includes slashes */
+					registrar_file = strrchr(vfile, '/');
+					if (!registrar_file) {
+						registrar_file = vfile;
+					} else {
+						registrar_file++; /* Skip past the end slash */
+					}
+					if (ast_add_extension2_with_lines(con, 0, realext, ipri, label, cidmatch, appl, ast_strdup(data), ast_free_ptr, registrar, registrar_file, v->lineno)) {
 						ast_log(LOG_WARNING,
 							"Unable to register extension at line %d of %s\n",
 							v->lineno, vfile);

-- 
To view, visit https://gerrit.asterisk.org/4633
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ieccc6abccdff34ed5c7da3511fd24972b8f2dd30
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Jonathan R. Rose <jonathan.rose at motorolasolutions.com>



More information about the asterisk-code-review mailing list