aboutsummaryrefslogtreecommitdiffstats
path: root/apps/app_cut.c
diff options
context:
space:
mode:
authormarkster <markster@f38db490-d61c-443f-a65b-d21fe96a405b>2003-10-22 13:34:59 +0000
committermarkster <markster@f38db490-d61c-443f-a65b-d21fe96a405b>2003-10-22 13:34:59 +0000
commit041e8e51463b28a78493da975542eb8911c9aaab (patch)
tree099035f28b58cd71c13fef50d8fdb44514d50962 /apps/app_cut.c
parent9564ebe2cd6fbc966289afdf566003f715afe392 (diff)
Add Tilghman's app_cut
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@1658 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'apps/app_cut.c')
-rwxr-xr-xapps/app_cut.c198
1 files changed, 198 insertions, 0 deletions
diff --git a/apps/app_cut.c b/apps/app_cut.c
new file mode 100755
index 000000000..f04668929
--- /dev/null
+++ b/apps/app_cut.c
@@ -0,0 +1,198 @@
+/*
+ * Asterisk -- A telephony toolkit for Linux.
+ *
+ * Cut application
+ *
+ * Copyright (c) 2003 Tilghman Lesher. All rights reserved.
+ *
+ * Tilghman Lesher <app_cut__v002@the-tilghman.com>
+ *
+ * $Id$
+ *
+ * This code is released by the author with no restrictions on usage.
+ *
+ */
+
+#include <asterisk/file.h>
+#include <asterisk/logger.h>
+#include <asterisk/options.h>
+#include <asterisk/channel.h>
+#include <asterisk/pbx.h>
+#include <asterisk/module.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+/* Maximum length of any variable */
+#define MAXRESULT 1024
+
+static char *tdesc = "Cuts up variables";
+
+static char *app_cut = "Cut";
+
+static char *cut_synopsis = "Cut(newvar=varname|delimiter|fieldspec)";
+
+static char *cut_descrip =
+"Cut(varname=varname,delimiter,field)\n"
+" newvar - result string is set to this variable\n"
+" varname - variable you want cut\n"
+" delimiter - defaults to -\n"
+" fieldspec - number of the field you want (1-based offset)\n"
+" may also be specified as a range (with -)\n"
+" or group of ranges and fields (with &)\n"
+" Returns 0 or -1 on hangup or error.\n";
+
+STANDARD_LOCAL_USER;
+
+LOCAL_USER_DECL;
+
+static int cut_exec(struct ast_channel *chan, void *data)
+{
+ int res=0;
+ struct localuser *u;
+ char *s, *newvar=NULL, *varname=NULL, *delimiter=NULL, *field=NULL;
+ int args_okay = 0;
+
+ LOCAL_USER_ADD(u);
+
+ /* Check and parse arguments */
+ if (data) {
+ s = strdupa((char *)data);
+ if (s) {
+ newvar = strsep(&s, "=");
+ if (newvar && (newvar[0] != '\0')) {
+ varname = strsep(&s, "|");
+ if (varname && (varname[0] != '\0')) {
+ delimiter = strsep(&s, "|");
+ if (delimiter) {
+ field = strsep(&s, "|");
+ if (field) {
+ args_okay = 1;
+ }
+ }
+ }
+ }
+ } else {
+ ast_log(LOG_ERROR, "Out of memory\n");
+ res = -1;
+ }
+ }
+
+ if (args_okay) {
+ char d, ds[2];
+ char *tmp = alloca(strlen(varname) + 4);
+ char *tmp2 = alloca(MAXRESULT);
+ char retstring[MAXRESULT];
+
+ memset(retstring, 0, MAXRESULT);
+
+ if (tmp && tmp2) {
+ snprintf(tmp, strlen(varname) + 4, "${%s}", varname);
+ memset(tmp2, 0, sizeof(tmp2));
+ } else {
+ ast_log(LOG_ERROR, "Out of memory");
+ return -1;
+ }
+
+ if (delimiter[0])
+ d = delimiter[0];
+ else
+ d = '-';
+
+ /* String form of the delimiter, for use with strsep(3) */
+ sprintf(ds,"%c",d);
+
+ pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
+
+ if (tmp2) {
+ int curfieldnum = 1;
+ while ((tmp2 != NULL) && (field != NULL)) {
+ char *nextgroup = strsep(&field, "&");
+ int num1 = 0, num2 = MAXRESULT;
+ char trashchar;
+
+ if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) {
+ /* range with both start and end */
+ } else if (sscanf(nextgroup, "-%d", &num2) == 1) {
+ /* range with end */
+ num1 = 0;
+ } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
+ /* range with start */
+ num2 = MAXRESULT;
+ } else if (sscanf(nextgroup, "%d", &num1) == 1) {
+ /* single number */
+ num2 = num1;
+ } else {
+ ast_log(LOG_ERROR, "Cut(): Illegal range '%s'\n", nextgroup);
+ ast_log(LOG_ERROR, "Usage: %s\n", cut_synopsis);
+ return -1;
+ }
+
+ /* Get to start, if any */
+ if (num1 > 0) {
+ while ((tmp2 != NULL + 1) && (curfieldnum < num1)) {
+ tmp2 = index(tmp2, d) + 1;
+ curfieldnum++;
+ }
+ }
+
+ /* Most frequent problem is the expectation of reordering fields */
+ if ((num1 > 0) && (curfieldnum > num1)) {
+ ast_log(LOG_WARNING, "Cut(): we're already past the field you wanted?\n");
+ }
+
+ /* Re-null tmp2 if we added 1 to NULL */
+ if (tmp2 == NULL + 1)
+ tmp2 = NULL;
+
+ /* Output fields until we either run out of fields or num2 is reached */
+ while ((tmp2 != NULL) && (curfieldnum <= num2)) {
+ char *tmp3 = strsep(&tmp2, ds);
+ int curlen = strlen(retstring);
+
+ if (strlen(retstring)) {
+ snprintf(retstring + curlen, MAXRESULT - curlen, "%c%s", d, tmp3);
+ } else {
+ snprintf(retstring, MAXRESULT, "%s", tmp3);
+ }
+
+ curfieldnum++;
+ }
+ }
+ }
+
+ pbx_builtin_setvar_helper(chan, newvar, retstring);
+ }
+
+ LOCAL_USER_REMOVE(u);
+ return res;
+}
+
+int unload_module(void)
+{
+ STANDARD_HANGUP_LOCALUSERS;
+ return ast_unregister_application(app_cut);
+}
+
+int load_module(void)
+{
+ return ast_register_application(app_cut, cut_exec, cut_synopsis, cut_descrip);
+}
+
+char *description(void)
+{
+ return tdesc;
+}
+
+int usecount(void)
+{
+ int res;
+ STANDARD_USECOUNT(res);
+ return res;
+}
+
+char *key()
+{
+ return ASTERISK_GPL_KEY;
+}