func_sprintf.c 6.3 KB

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