Newer
Older
static int function_eval2(struct ast_channel *chan, const char *cmd, char *data,
struct ast_str **buf, ssize_t buflen)
{
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
return -1;
}
ast_str_substitute_variables(buf, buflen, chan, data);
return 0;
}
static struct ast_custom_function eval_function = {
.name = "EVAL",
.read = function_eval,
static int keypadhash(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
{
char *bufptr, *dataptr;
for (bufptr = buf, dataptr = data; bufptr < buf + buflen - 1; dataptr++) {
if (*dataptr == '\0') {
*bufptr++ = '\0';
break;
} else if (*dataptr == '1') {
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
*bufptr++ = '1';
} else if (strchr("AaBbCc2", *dataptr)) {
*bufptr++ = '2';
} else if (strchr("DdEeFf3", *dataptr)) {
*bufptr++ = '3';
} else if (strchr("GgHhIi4", *dataptr)) {
*bufptr++ = '4';
} else if (strchr("JjKkLl5", *dataptr)) {
*bufptr++ = '5';
} else if (strchr("MmNnOo6", *dataptr)) {
*bufptr++ = '6';
} else if (strchr("PpQqRrSs7", *dataptr)) {
*bufptr++ = '7';
} else if (strchr("TtUuVv8", *dataptr)) {
*bufptr++ = '8';
} else if (strchr("WwXxYyZz9", *dataptr)) {
*bufptr++ = '9';
} else if (*dataptr == '0') {
*bufptr++ = '0';
}
}
buf[buflen - 1] = '\0';
return 0;
}
static struct ast_custom_function keypadhash_function = {
.name = "KEYPADHASH",
.read = keypadhash,
};
static int string_toupper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
{
char *bufptr = buf, *dataptr = data;
while ((bufptr < buf + buflen - 1) && (*bufptr++ = toupper(*dataptr++)));
return 0;
}
static int string_toupper2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t buflen)
{
char *bufptr, *dataptr = data;
if (buflen > -1) {
ast_str_make_space(buf, buflen > 0 ? buflen : strlen(data) + 1);
}
bufptr = ast_str_buffer(*buf);
while ((bufptr < ast_str_buffer(*buf) + ast_str_size(*buf) - 1) && (*bufptr++ = toupper(*dataptr++)));
ast_str_update(*buf);
return 0;
}
static struct ast_custom_function toupper_function = {
.name = "TOUPPER",
.read = string_toupper,
};
static int string_tolower(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
{
char *bufptr = buf, *dataptr = data;
while ((bufptr < buf + buflen - 1) && (*bufptr++ = tolower(*dataptr++)));
return 0;
}
static int string_tolower2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t buflen)
{
char *bufptr, *dataptr = data;
if (buflen > -1) {
ast_str_make_space(buf, buflen > 0 ? buflen : strlen(data) + 1);
}
bufptr = ast_str_buffer(*buf);
while ((bufptr < ast_str_buffer(*buf) + ast_str_size(*buf) - 1) && (*bufptr++ = tolower(*dataptr++)));
ast_str_update(*buf);
return 0;
}
static struct ast_custom_function tolower_function = {
.name = "TOLOWER",
.read = string_tolower,
};
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
static int array_remove(struct ast_channel *chan, const char *cmd, char *var, char *buf, size_t len, int beginning)
{
const char *tmp;
char *after, *before;
char *(*search_func)(const char *s, int c) = beginning ? strchr : strrchr;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(var);
AST_APP_ARG(delimiter);
);
if (!chan) {
ast_log(LOG_WARNING, "%s requires a channel\n", cmd);
return -1;
}
AST_STANDARD_APP_ARGS(args, var);
if (ast_strlen_zero(args.var)) {
ast_log(LOG_WARNING, "%s requires a channel variable name\n", cmd);
return -1;
}
if (args.delimiter && strlen(args.delimiter) != 1) {
ast_log(LOG_WARNING, "%s delimeters should be a single character\n", cmd);
return -1;
}
ast_channel_lock(chan);
if (ast_strlen_zero(tmp = pbx_builtin_getvar_helper(chan, args.var))) {
ast_channel_unlock(chan);
return 0;
}
before = ast_strdupa(tmp);
ast_channel_unlock(chan);
/* Only one entry in array */
if (!(after = search_func(before, S_OR(args.delimiter, ",")[0]))) {
ast_copy_string(buf, before, len);
pbx_builtin_setvar_helper(chan, args.var, "");
} else {
*after++ = '\0';
ast_copy_string(buf, beginning ? before : after, len);
pbx_builtin_setvar_helper(chan, args.var, beginning ? after : before);
}
return 0;
}
static int shift(struct ast_channel *chan, const char *cmd, char *var, char *buf, size_t len)
{
return array_remove(chan, cmd, var, buf, len, 1);
}
static struct ast_custom_function shift_function = {
.name = "SHIFT",
.read = shift,
};
static int pop(struct ast_channel *chan, const char *cmd, char *var, char *buf, size_t len)
{
return array_remove(chan, cmd, var, buf, len, 0);
}
static struct ast_custom_function pop_function = {
.name = "POP",
.read = pop,
};
static int array_insert(struct ast_channel *chan, const char *cmd, char *var, const char *val, int beginning)
{
const char *tmp;
struct ast_str *buf;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(var);
AST_APP_ARG(delimiter);
);
if (!chan) {
ast_log(LOG_WARNING, "%s requires a channel\n", cmd);
return -1;
}
AST_STANDARD_APP_ARGS(args, var);
if (ast_strlen_zero(args.var) || ast_strlen_zero(val)) {
ast_log(LOG_WARNING, "%s requires a variable, and at least one value\n", cmd);
return -1;
}
if (args.delimiter && strlen(args.delimiter) != 1) {
ast_log(LOG_WARNING, "%s delimeters should be a single character\n", cmd);
return -1;
}
if (!(buf = ast_str_create(32))) {
ast_log(LOG_ERROR, "Unable to allocate memory for buffer!\n");
return -1;
}
ast_channel_lock(chan);
if (!(tmp = pbx_builtin_getvar_helper(chan, args.var))) {
ast_str_set(&buf, 0, "%s", val);
} else {
ast_str_append(&buf, 0, "%s%s%s", beginning ? val : tmp, S_OR(args.delimiter, ","), beginning ? tmp : val);
}
ast_channel_unlock(chan);
pbx_builtin_setvar_helper(chan, args.var, ast_str_buffer(buf));
ast_free(buf);
return 0;
}
static int push(struct ast_channel *chan, const char *cmd, char *var, const char *val)
{
return array_insert(chan, cmd, var, val, 0);
}
static struct ast_custom_function push_function = {
.name = "PUSH",
.write = push,
};
static int unshift(struct ast_channel *chan, const char *cmd, char *var, const char *val)
{
return array_insert(chan, cmd, var, val, 1);
}
static struct ast_custom_function unshift_function = {
.name = "UNSHIFT",
.write = unshift,
};
static int unload_module(void)
{
int res = 0;
res |= ast_custom_function_unregister(&fieldqty_function);
res |= ast_custom_function_unregister(&filter_function);
Tilghman Lesher
committed
res |= ast_custom_function_unregister(&listfilter_function);
res |= ast_custom_function_unregister(®ex_function);
res |= ast_custom_function_unregister(&array_function);
res |= ast_custom_function_unregister("e_function);
res |= ast_custom_function_unregister(&len_function);
res |= ast_custom_function_unregister(&strftime_function);
res |= ast_custom_function_unregister(&strptime_function);
res |= ast_custom_function_unregister(&eval_function);
res |= ast_custom_function_unregister(&keypadhash_function);
res |= ast_custom_function_unregister(&hashkeys_function);
res |= ast_custom_function_unregister(&hash_function);
res |= ast_unregister_application(app_clearhash);
res |= ast_custom_function_unregister(&toupper_function);
res |= ast_custom_function_unregister(&tolower_function);
res |= ast_custom_function_unregister(&shift_function);
res |= ast_custom_function_unregister(&pop_function);
res |= ast_custom_function_unregister(&push_function);
res |= ast_custom_function_unregister(&unshift_function);
return res;
}
static int load_module(void)
{
int res = 0;
res |= ast_custom_function_register(&fieldqty_function);
res |= ast_custom_function_register(&filter_function);
Tilghman Lesher
committed
res |= ast_custom_function_register(&listfilter_function);
res |= ast_custom_function_register(®ex_function);
res |= ast_custom_function_register(&array_function);
res |= ast_custom_function_register("e_function);
res |= ast_custom_function_register(&len_function);
res |= ast_custom_function_register(&strftime_function);
res |= ast_custom_function_register(&strptime_function);
res |= ast_custom_function_register(&eval_function);
res |= ast_custom_function_register(&keypadhash_function);
res |= ast_custom_function_register(&hashkeys_function);
res |= ast_custom_function_register(&hash_function);
res |= ast_register_application_xml(app_clearhash, exec_clearhash);
res |= ast_custom_function_register(&toupper_function);
res |= ast_custom_function_register(&tolower_function);
res |= ast_custom_function_register(&shift_function);
res |= ast_custom_function_register(&pop_function);
res |= ast_custom_function_register(&push_function);
res |= ast_custom_function_register(&unshift_function);
return res;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "String handling dialplan functions");