2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (c) 2003 Tilghman Lesher. All rights reserved.
8 * Tilghman Lesher <app_cut__v002@the-tilghman.com>
12 * This code is released by the author with no restrictions on usage.
20 #include <asterisk/file.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/options.h>
23 #include <asterisk/channel.h>
24 #include <asterisk/pbx.h>
25 #include <asterisk/module.h>
27 /* Maximum length of any variable */
28 #define MAXRESULT 1024
30 static char *tdesc = "Cuts up variables";
32 static char *app_cut = "Cut";
34 static char *cut_synopsis = "Cut(newvar=varname|delimiter|fieldspec)";
36 static char *cut_descrip =
37 "Cut(newvar=varname,delimiter,field)\n"
38 " newvar - new variable created from result string\n"
39 " varname - variable you want cut\n"
40 " delimiter - defaults to '-'\n"
41 " fieldspec - number of the field you want (1-based offset)\n"
42 " may also be specified as a range (with -)\n"
43 " or group of ranges and fields (with &)\n"
44 " Returns 0 or -1 on hangup or error.\n";
50 static int cut_exec(struct ast_channel *chan, void *data)
54 char *s, *newvar=NULL, *varname=NULL, *delimiter=NULL, *field=NULL;
59 /* Check and parse arguments */
61 s = ast_strdupa((char *)data);
63 newvar = strsep(&s, "=");
64 if (newvar && (newvar[0] != '\0')) {
65 varname = strsep(&s, "|");
66 if (varname && (varname[0] != '\0')) {
67 delimiter = strsep(&s, "|");
69 field = strsep(&s, "|");
77 ast_log(LOG_ERROR, "Out of memory\n");
84 char *tmp = alloca(strlen(varname) + 4);
85 char *tmp2 = alloca(MAXRESULT);
86 char retstring[MAXRESULT];
89 memset(tmp2, 0, MAXRESULT);
90 memset(retstring, 0, MAXRESULT);
93 snprintf(tmp, strlen(varname) + 4, "${%s}", varname);
94 memset(tmp2, 0, sizeof(tmp2));
96 ast_log(LOG_ERROR, "Out of memory");
105 /* String form of the delimiter, for use with strsep(3) */
108 pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
112 while ((tmp2 != NULL) && (field != NULL)) {
113 char *nextgroup = strsep(&field, "&");
114 int num1 = 0, num2 = MAXRESULT;
117 if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) {
118 /* range with both start and end */
119 } else if (sscanf(nextgroup, "-%d", &num2) == 1) {
122 } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
123 /* range with start */
125 } else if (sscanf(nextgroup, "%d", &num1) == 1) {
129 ast_log(LOG_ERROR, "Cut(): Illegal range '%s'\n", nextgroup);
130 ast_log(LOG_ERROR, "Usage: %s\n", cut_synopsis);
134 /* Get to start, if any */
136 while ((tmp2 != (char *)NULL + 1) && (curfieldnum < num1)) {
137 tmp2 = index(tmp2, d) + 1;
142 /* Most frequent problem is the expectation of reordering fields */
143 if ((num1 > 0) && (curfieldnum > num1)) {
144 ast_log(LOG_WARNING, "Cut(): we're already past the field you wanted?\n");
147 /* Re-null tmp2 if we added 1 to NULL */
148 if (tmp2 == (char *)NULL + 1)
151 /* Output fields until we either run out of fields or num2 is reached */
152 while ((tmp2 != NULL) && (curfieldnum <= num2)) {
153 char *tmp3 = strsep(&tmp2, ds);
154 int curlen = strlen(retstring);
156 if (strlen(retstring)) {
157 snprintf(retstring + curlen, MAXRESULT - curlen, "%c%s", d, tmp3);
159 snprintf(retstring, MAXRESULT, "%s", tmp3);
167 pbx_builtin_setvar_helper(chan, newvar, retstring);
170 LOCAL_USER_REMOVE(u);
174 int unload_module(void)
176 STANDARD_HANGUP_LOCALUSERS;
177 return ast_unregister_application(app_cut);
180 int load_module(void)
182 return ast_register_application(app_cut, cut_exec, cut_synopsis, cut_descrip);
185 char *description(void)
193 STANDARD_USECOUNT(res);
199 return ASTERISK_GPL_KEY;