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(varname=varname,delimiter,field)\n"
38 " newvar - result string is set to this variable\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];
88 memset(retstring, 0, MAXRESULT);
91 snprintf(tmp, strlen(varname) + 4, "${%s}", varname);
92 memset(tmp2, 0, sizeof(tmp2));
94 ast_log(LOG_ERROR, "Out of memory");
103 /* String form of the delimiter, for use with strsep(3) */
106 pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
110 while ((tmp2 != NULL) && (field != NULL)) {
111 char *nextgroup = strsep(&field, "&");
112 int num1 = 0, num2 = MAXRESULT;
115 if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) {
116 /* range with both start and end */
117 } else if (sscanf(nextgroup, "-%d", &num2) == 1) {
120 } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
121 /* range with start */
123 } else if (sscanf(nextgroup, "%d", &num1) == 1) {
127 ast_log(LOG_ERROR, "Cut(): Illegal range '%s'\n", nextgroup);
128 ast_log(LOG_ERROR, "Usage: %s\n", cut_synopsis);
132 /* Get to start, if any */
134 while ((tmp2 != (char *)NULL + 1) && (curfieldnum < num1)) {
135 tmp2 = index(tmp2, d) + 1;
140 /* Most frequent problem is the expectation of reordering fields */
141 if ((num1 > 0) && (curfieldnum > num1)) {
142 ast_log(LOG_WARNING, "Cut(): we're already past the field you wanted?\n");
145 /* Re-null tmp2 if we added 1 to NULL */
146 if (tmp2 == (char *)NULL + 1)
149 /* Output fields until we either run out of fields or num2 is reached */
150 while ((tmp2 != NULL) && (curfieldnum <= num2)) {
151 char *tmp3 = strsep(&tmp2, ds);
152 int curlen = strlen(retstring);
154 if (strlen(retstring)) {
155 snprintf(retstring + curlen, MAXRESULT - curlen, "%c%s", d, tmp3);
157 snprintf(retstring, MAXRESULT, "%s", tmp3);
165 pbx_builtin_setvar_helper(chan, newvar, retstring);
168 LOCAL_USER_REMOVE(u);
172 int unload_module(void)
174 STANDARD_HANGUP_LOCALUSERS;
175 return ast_unregister_application(app_cut);
178 int load_module(void)
180 return ast_register_application(app_cut, cut_exec, cut_synopsis, cut_descrip);
183 char *description(void)
191 STANDARD_USECOUNT(res);
197 return ASTERISK_GPL_KEY;