[asterisk-commits] tilghman: trunk r160344 - /trunk/apps/app_stack.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Dec 2 12:39:13 CST 2008


Author: tilghman
Date: Tue Dec  2 12:39:12 2008
New Revision: 160344

URL: http://svn.digium.com/view/asterisk?view=rev&rev=160344
Log:
Add LOCAL_PEEK function, as requested by lmadsen.

Modified:
    trunk/apps/app_stack.c

Modified: trunk/apps/app_stack.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_stack.c?view=diff&rev=160344&r1=160343&r2=160344
==============================================================================
--- trunk/apps/app_stack.c (original)
+++ trunk/apps/app_stack.c Tue Dec  2 12:39:12 2008
@@ -142,6 +142,27 @@
 			<ref type="application">Return</ref>
 		</see-also>
 	</function>
+	<function name="LOCAL_PEEK" language="en_US">
+		<synopsis>
+			Retrieve variables hidden by the local gosub stack frame.
+		</synopsis>
+		<syntax>
+			<parameter name="n" required="true" />
+			<parameter name="varname" required="true" />
+		</syntax>
+		<description>
+			<para>Read a variable <replaceable>varname</replaceable> hidden by 
+			<replaceable>n</replaceable> levels of gosub stack frames.  Note that ${LOCAL_PEEK(0,foo)}
+			is the same as ${foo}, since the value of <replaceable>n</replaceable> peeks under 0 levels of
+			stack frames; in other words, 0 is the current level.  If <replaceable>n</replaceable> exceeds
+			the available number of stack frames, then an empty string is returned.</para>
+		</description>
+		<see-also>
+			<ref type="application">Gosub</ref>
+			<ref type="application">GosubIf</ref>
+			<ref type="application">Return</ref>
+		</see-also>
+	</function>
  ***/
 
 static const char *app_gosub = "Gosub";
@@ -469,6 +490,42 @@
 	.name = "LOCAL",
 	.write = local_write,
 	.read = local_read,
+};
+
+static int peek_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+	int found = 0, n;
+	struct ast_var_t *variables;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(n);
+		AST_APP_ARG(name);
+	);
+
+	if (!chan) {
+		ast_log(LOG_ERROR, "LOCAL_PEEK must be called on an active channel\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, data);
+	n = atoi(args.n);
+	*buf = '\0';
+
+	ast_channel_lock(chan);
+	AST_LIST_TRAVERSE(&chan->varshead, variables, entries) {
+		if (!strcmp(args.name, ast_var_name(variables)) && ++found > n) {
+			ast_copy_string(buf, ast_var_value(variables), len);
+			break;
+		}
+	}
+	ast_channel_unlock(chan);
+	return 0;
+}
+
+static struct ast_custom_function peek_function = {
+	.name = "LOCAL_PEEK",
+	.synopsis = "Peeks at variables within the variable stack",
+	.syntax = "LOCAL_PEEK(<n>|<varname>)",
+	.read = peek_read,
 };
 
 static int handle_gosub(struct ast_channel *chan, AGI *agi, int argc, char **argv)
@@ -585,6 +642,7 @@
 	ast_unregister_application(app_gosubif);
 	ast_unregister_application(app_gosub);
 	ast_custom_function_unregister(&local_function);
+	ast_custom_function_unregister(&peek_function);
 
 	return 0;
 }
@@ -607,6 +665,7 @@
 	ast_register_application_xml(app_gosubif, gosubif_exec);
 	ast_register_application_xml(app_gosub, gosub_exec);
 	ast_custom_function_register(&local_function);
+	ast_custom_function_register(&peek_function);
 
 	return 0;
 }




More information about the asterisk-commits mailing list