[Asterisk-cvs] asterisk/apps app_cut.c,NONE,1.1 Makefile,1.21,1.22

markster at lists.digium.com markster at lists.digium.com
Wed Oct 22 08:08:58 CDT 2003


Update of /usr/cvsroot/asterisk/apps
In directory mongoose.digium.com:/tmp/cvs-serv22658/apps

Modified Files:
	Makefile 
Added Files:
	app_cut.c 
Log Message:
Add Tilghman's app_cut


--- NEW FILE: app_cut.c ---
/*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * Cut application
 *
 * Copyright (c) 2003 Tilghman Lesher.  All rights reserved.
 *
 * Tilghman Lesher <app_cut__v002 at the-tilghman.com>
 *
 * $Id: app_cut.c,v 1.1 2003/10/22 13:34:59 markster Exp $
 *
 * 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;
}

Index: Makefile
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/Makefile,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- Makefile	4 Oct 2003 22:08:02 -0000	1.21
+++ Makefile	22 Oct 2003 13:34:59 -0000	1.22
@@ -24,7 +24,7 @@
      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




More information about the svn-commits mailing list