func_sprintf.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005-2006, Digium, Inc.
  5. * Portions Copyright (C) 2005, Tilghman Lesher. All rights reserved.
  6. * Portions Copyright (C) 2005, Anthony Minessale II
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief String manipulation dialplan functions
  21. *
  22. * \author Tilghman Lesher
  23. * \author Anthony Minessale II
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. #include <ctype.h>
  31. #include "asterisk/module.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/utils.h"
  35. #include "asterisk/app.h"
  36. AST_THREADSTORAGE(result_buf);
  37. /*** DOCUMENTATION
  38. <function name="SPRINTF" language="en_US">
  39. <synopsis>
  40. Format a variable according to a format string.
  41. </synopsis>
  42. <syntax>
  43. <parameter name="format" required="true" />
  44. <parameter name="arg1" required="true" />
  45. <parameter name="arg2" multiple="true" />
  46. <parameter name="argN" />
  47. </syntax>
  48. <description>
  49. <para>Parses the format string specified and returns a string matching
  50. that format. Supports most options found in <emphasis>sprintf(3)</emphasis>.
  51. Returns a shortened string if a format specifier is not recognized.</para>
  52. </description>
  53. <see-also>
  54. <ref type="manpage">sprintf(3)</ref>
  55. </see-also>
  56. </function>
  57. ***/
  58. static int acf_sprintf(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  59. {
  60. #define SPRINTF_FLAG 0
  61. #define SPRINTF_WIDTH 1
  62. #define SPRINTF_PRECISION 2
  63. #define SPRINTF_LENGTH 3
  64. #define SPRINTF_CONVERSION 4
  65. int i, state = -1, argcount = 0;
  66. char *formatstart = NULL, *bufptr = buf;
  67. char formatbuf[256] = "";
  68. int tmpi;
  69. double tmpd;
  70. AST_DECLARE_APP_ARGS(arg,
  71. AST_APP_ARG(format);
  72. AST_APP_ARG(var)[100];
  73. );
  74. AST_STANDARD_APP_ARGS(arg, data);
  75. /* Scan the format, converting each argument into the requisite format type. */
  76. for (i = 0; arg.format[i]; i++) {
  77. switch (state) {
  78. case SPRINTF_FLAG:
  79. if (strchr("#0- +'I", arg.format[i]))
  80. break;
  81. state = SPRINTF_WIDTH;
  82. case SPRINTF_WIDTH:
  83. if (arg.format[i] >= '0' && arg.format[i] <= '9')
  84. break;
  85. /* Next character must be a period to go into a precision */
  86. if (arg.format[i] == '.') {
  87. state = SPRINTF_PRECISION;
  88. } else {
  89. state = SPRINTF_LENGTH;
  90. i--;
  91. }
  92. break;
  93. case SPRINTF_PRECISION:
  94. if (arg.format[i] >= '0' && arg.format[i] <= '9')
  95. break;
  96. state = SPRINTF_LENGTH;
  97. case SPRINTF_LENGTH:
  98. if (strchr("hl", arg.format[i])) {
  99. if (arg.format[i + 1] == arg.format[i])
  100. i++;
  101. state = SPRINTF_CONVERSION;
  102. break;
  103. } else if (strchr("Lqjzt", arg.format[i])) {
  104. state = SPRINTF_CONVERSION;
  105. break;
  106. }
  107. state = SPRINTF_CONVERSION;
  108. case SPRINTF_CONVERSION:
  109. if (strchr("diouxXc", arg.format[i])) {
  110. /* Integer */
  111. /* Isolate this format alone */
  112. ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
  113. formatbuf[&arg.format[i] - formatstart + 1] = '\0';
  114. /* Convert the argument into the required type */
  115. if (arg.var[argcount]) {
  116. if (sscanf(arg.var[argcount++], "%30d", &tmpi) != 1) {
  117. ast_log(LOG_ERROR, "Argument '%s' is not an integer number for format '%s'\n", arg.var[argcount - 1], formatbuf);
  118. goto sprintf_fail;
  119. }
  120. } else {
  121. ast_log(LOG_ERROR, "SPRINTF() has more format specifiers than arguments!\n");
  122. goto sprintf_fail;
  123. }
  124. /* Format the argument */
  125. snprintf(bufptr, buf + len - bufptr, formatbuf, tmpi);
  126. /* Update the position of the next parameter to print */
  127. bufptr = strchr(buf, '\0');
  128. } else if (strchr("eEfFgGaA", arg.format[i])) {
  129. /* Double */
  130. /* Isolate this format alone */
  131. ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
  132. formatbuf[&arg.format[i] - formatstart + 1] = '\0';
  133. /* Convert the argument into the required type */
  134. if (arg.var[argcount]) {
  135. if (sscanf(arg.var[argcount++], "%30lf", &tmpd) != 1) {
  136. ast_log(LOG_ERROR, "Argument '%s' is not a floating point number for format '%s'\n", arg.var[argcount - 1], formatbuf);
  137. goto sprintf_fail;
  138. }
  139. } else {
  140. ast_log(LOG_ERROR, "SPRINTF() has more format specifiers than arguments!\n");
  141. goto sprintf_fail;
  142. }
  143. /* Format the argument */
  144. snprintf(bufptr, buf + len - bufptr, formatbuf, tmpd);
  145. /* Update the position of the next parameter to print */
  146. bufptr = strchr(buf, '\0');
  147. } else if (arg.format[i] == 's') {
  148. /* String */
  149. /* Isolate this format alone */
  150. ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
  151. formatbuf[&arg.format[i] - formatstart + 1] = '\0';
  152. /* Format the argument */
  153. snprintf(bufptr, buf + len - bufptr, formatbuf, arg.var[argcount++]);
  154. /* Update the position of the next parameter to print */
  155. bufptr = strchr(buf, '\0');
  156. } else if (arg.format[i] == '%') {
  157. /* Literal data to copy */
  158. *bufptr++ = arg.format[i];
  159. } else {
  160. /* Not supported */
  161. /* Isolate this format alone */
  162. ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
  163. formatbuf[&arg.format[i] - formatstart + 1] = '\0';
  164. ast_log(LOG_ERROR, "Format type not supported: '%s' with argument '%s'\n", formatbuf, arg.var[argcount++]);
  165. goto sprintf_fail;
  166. }
  167. state = -1;
  168. break;
  169. default:
  170. if (arg.format[i] == '%') {
  171. state = SPRINTF_FLAG;
  172. formatstart = &arg.format[i];
  173. break;
  174. } else {
  175. /* Literal data to copy */
  176. *bufptr++ = arg.format[i];
  177. }
  178. }
  179. }
  180. *bufptr = '\0';
  181. return 0;
  182. sprintf_fail:
  183. return -1;
  184. }
  185. static struct ast_custom_function sprintf_function = {
  186. .name = "SPRINTF",
  187. .read = acf_sprintf,
  188. };
  189. static int unload_module(void)
  190. {
  191. int res = 0;
  192. res |= ast_custom_function_unregister(&sprintf_function);
  193. return res;
  194. }
  195. static int load_module(void)
  196. {
  197. int res = 0;
  198. res |= ast_custom_function_register(&sprintf_function);
  199. return res;
  200. }
  201. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SPRINTF dialplan function");