| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include <ctype.h> |
| 35 |
|
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/channel.h" |
| 38 |
#include "asterisk/pbx.h" |
| 39 |
#include "asterisk/utils.h" |
| 40 |
#include "asterisk/app.h" |
| 41 |
|
| 42 |
AST_THREADSTORAGE(result_buf); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static int acf_sprintf(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 69 |
{ |
| 70 |
#define SPRINTF_FLAG 0 |
| 71 |
#define SPRINTF_WIDTH 1 |
| 72 |
#define SPRINTF_PRECISION 2 |
| 73 |
#define SPRINTF_LENGTH 3 |
| 74 |
#define SPRINTF_CONVERSION 4 |
| 75 |
int i, state = -1, argcount = 0; |
| 76 |
char *formatstart = NULL, *bufptr = buf; |
| 77 |
char formatbuf[256] = ""; |
| 78 |
int tmpi; |
| 79 |
double tmpd; |
| 80 |
AST_DECLARE_APP_ARGS(arg, |
| 81 |
AST_APP_ARG(format); |
| 82 |
AST_APP_ARG(var)[100]; |
| 83 |
); |
| 84 |
|
| 85 |
AST_STANDARD_APP_ARGS(arg, data); |
| 86 |
|
| 87 |
|
| 88 |
for (i = 0; arg.format[i]; i++) { |
| 89 |
switch (state) { |
| 90 |
case SPRINTF_FLAG: |
| 91 |
if (strchr("#0- +'I", arg.format[i])) |
| 92 |
break; |
| 93 |
state = SPRINTF_WIDTH; |
| 94 |
case SPRINTF_WIDTH: |
| 95 |
if (arg.format[i] >= '0' && arg.format[i] <= '9') |
| 96 |
break; |
| 97 |
|
| 98 |
|
| 99 |
if (arg.format[i] == '.') { |
| 100 |
state = SPRINTF_PRECISION; |
| 101 |
} else { |
| 102 |
state = SPRINTF_LENGTH; |
| 103 |
i--; |
| 104 |
} |
| 105 |
break; |
| 106 |
case SPRINTF_PRECISION: |
| 107 |
if (arg.format[i] >= '0' && arg.format[i] <= '9') |
| 108 |
break; |
| 109 |
state = SPRINTF_LENGTH; |
| 110 |
case SPRINTF_LENGTH: |
| 111 |
if (strchr("hl", arg.format[i])) { |
| 112 |
if (arg.format[i + 1] == arg.format[i]) |
| 113 |
i++; |
| 114 |
state = SPRINTF_CONVERSION; |
| 115 |
break; |
| 116 |
} else if (strchr("Lqjzt", arg.format[i])) { |
| 117 |
state = SPRINTF_CONVERSION; |
| 118 |
break; |
| 119 |
} |
| 120 |
state = SPRINTF_CONVERSION; |
| 121 |
case SPRINTF_CONVERSION: |
| 122 |
if (strchr("diouxXc", arg.format[i])) { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
ast_copy_string(formatbuf, formatstart, sizeof(formatbuf)); |
| 127 |
formatbuf[&arg.format[i] - formatstart + 1] = '\0'; |
| 128 |
|
| 129 |
|
| 130 |
if (arg.var[argcount]) { |
| 131 |
if (sscanf(arg.var[argcount++], "%30d", &tmpi) != 1) { |
| 132 |
ast_log(LOG_ERROR, "Argument '%s' is not an integer number for format '%s'\n", arg.var[argcount - 1], formatbuf); |
| 133 |
goto sprintf_fail; |
| 134 |
} |
| 135 |
} else { |
| 136 |
ast_log(LOG_ERROR, "SPRINTF() has more format specifiers than arguments!\n"); |
| 137 |
goto sprintf_fail; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
snprintf(bufptr, buf + len - bufptr, formatbuf, tmpi); |
| 142 |
|
| 143 |
|
| 144 |
bufptr = strchr(buf, '\0'); |
| 145 |
} else if (strchr("eEfFgGaA", arg.format[i])) { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
ast_copy_string(formatbuf, formatstart, sizeof(formatbuf)); |
| 150 |
formatbuf[&arg.format[i] - formatstart + 1] = '\0'; |
| 151 |
|
| 152 |
|
| 153 |
if (arg.var[argcount]) { |
| 154 |
if (sscanf(arg.var[argcount++], "%30lf", &tmpd) != 1) { |
| 155 |
ast_log(LOG_ERROR, "Argument '%s' is not a floating point number for format '%s'\n", arg.var[argcount - 1], formatbuf); |
| 156 |
goto sprintf_fail; |
| 157 |
} |
| 158 |
} else { |
| 159 |
ast_log(LOG_ERROR, "SPRINTF() has more format specifiers than arguments!\n"); |
| 160 |
goto sprintf_fail; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
snprintf(bufptr, buf + len - bufptr, formatbuf, tmpd); |
| 165 |
|
| 166 |
|
| 167 |
bufptr = strchr(buf, '\0'); |
| 168 |
} else if (arg.format[i] == 's') { |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
ast_copy_string(formatbuf, formatstart, sizeof(formatbuf)); |
| 173 |
formatbuf[&arg.format[i] - formatstart + 1] = '\0'; |
| 174 |
|
| 175 |
|
| 176 |
snprintf(bufptr, buf + len - bufptr, formatbuf, arg.var[argcount++]); |
| 177 |
|
| 178 |
|
| 179 |
bufptr = strchr(buf, '\0'); |
| 180 |
} else if (arg.format[i] == '%') { |
| 181 |
|
| 182 |
*bufptr++ = arg.format[i]; |
| 183 |
} else { |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
ast_copy_string(formatbuf, formatstart, sizeof(formatbuf)); |
| 188 |
formatbuf[&arg.format[i] - formatstart + 1] = '\0'; |
| 189 |
|
| 190 |
ast_log(LOG_ERROR, "Format type not supported: '%s' with argument '%s'\n", formatbuf, arg.var[argcount++]); |
| 191 |
goto sprintf_fail; |
| 192 |
} |
| 193 |
state = -1; |
| 194 |
break; |
| 195 |
default: |
| 196 |
if (arg.format[i] == '%') { |
| 197 |
state = SPRINTF_FLAG; |
| 198 |
formatstart = &arg.format[i]; |
| 199 |
break; |
| 200 |
} else { |
| 201 |
|
| 202 |
*bufptr++ = arg.format[i]; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
*bufptr = '\0'; |
| 207 |
return 0; |
| 208 |
sprintf_fail: |
| 209 |
return -1; |
| 210 |
} |
| 211 |
|
| 212 |
static struct ast_custom_function sprintf_function = { |
| 213 |
.name = "SPRINTF", |
| 214 |
.read = acf_sprintf, |
| 215 |
}; |
| 216 |
|
| 217 |
static int unload_module(void) |
| 218 |
{ |
| 219 |
int res = 0; |
| 220 |
|
| 221 |
res |= ast_custom_function_unregister(&sprintf_function); |
| 222 |
|
| 223 |
return res; |
| 224 |
} |
| 225 |
|
| 226 |
static int load_module(void) |
| 227 |
{ |
| 228 |
int res = 0; |
| 229 |
|
| 230 |
res |= ast_custom_function_register(&sprintf_function); |
| 231 |
|
| 232 |
return res; |
| 233 |
} |
| 234 |
|
| 235 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SPRINTF dialplan function"); |