Add Tilghman's app_cut
authorMark Spencer <markster@digium.com>
Wed, 22 Oct 2003 13:34:59 +0000 (13:34 +0000)
committerMark Spencer <markster@digium.com>
Wed, 22 Oct 2003 13:34:59 +0000 (13:34 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@1658 65c4cc65-6c06-0410-ace0-fbb531ad65f3

apps/Makefile
apps/app_cut.c [new file with mode: 0755]

index fa3591a..b2ed7b4 100755 (executable)
@@ -24,7 +24,7 @@ APPS=app_dial.so app_playback.so app_voicemail.so app_directory.so app_intercom.
      app_authenticate.so app_softhangup.so app_lookupblacklist.so \
      app_waitforring.so app_privacy.so app_db.so app_chanisavail.so \
      app_enumlookup.so app_voicemail2.so app_transfer.so app_setcidnum.so app_cdr.so \
-     app_hasnewvoicemail.so app_sayunixtime.so
+     app_hasnewvoicemail.so app_sayunixtime.so app_cut.so
 
 #APPS+=app_sql_postgres.so
 #APPS+=app_sql_odbc.so
diff --git a/apps/app_cut.c b/apps/app_cut.c
new file mode 100755 (executable)
index 0000000..f046689
--- /dev/null
@@ -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;
+}