func_cut.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (c) 2003-2006 Tilghman Lesher. All rights reserved.
  5. *
  6. * Tilghman Lesher <app_cut__v003@the-tilghman.com>
  7. *
  8. * This code is released by the author with no restrictions on usage.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. */
  17. /*! \file
  18. *
  19. * \brief CUT function
  20. *
  21. * \author Tilghman Lesher <app_cut__v003@the-tilghman.com>
  22. *
  23. * \ingroup functions
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. #include "asterisk/file.h"
  30. #include "asterisk/channel.h"
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/app.h"
  34. /*** DOCUMENTATION
  35. <function name="SORT" language="en_US">
  36. <synopsis>
  37. Sorts a list of key/vals into a list of keys, based upon the vals.
  38. </synopsis>
  39. <syntax>
  40. <parameter name="keyval" required="true" argsep=":">
  41. <argument name="key1" required="true" />
  42. <argument name="val1" required="true" />
  43. </parameter>
  44. <parameter name="keyvaln" multiple="true" argsep=":">
  45. <argument name="key2" required="true" />
  46. <argument name="val2" required="true" />
  47. </parameter>
  48. </syntax>
  49. <description>
  50. <para>Takes a comma-separated list of keys and values, each separated by a colon, and returns a
  51. comma-separated list of the keys, sorted by their values. Values will be evaluated as
  52. floating-point numbers.</para>
  53. </description>
  54. </function>
  55. <function name="CUT" language="en_US">
  56. <synopsis>
  57. Slices and dices strings, based upon a named delimiter.
  58. </synopsis>
  59. <syntax>
  60. <parameter name="varname" required="true">
  61. <para>Variable you want cut</para>
  62. </parameter>
  63. <parameter name="char-delim" required="true">
  64. <para>Delimiter, defaults to <literal>-</literal></para>
  65. </parameter>
  66. <parameter name="range-spec" required="true">
  67. <para>Number of the field you want (1-based offset), may also be specified as a range (with <literal>-</literal>)
  68. or group of ranges and fields (with <literal>&amp;</literal>)</para>
  69. </parameter>
  70. </syntax>
  71. <description>
  72. <para>Cut out information from a string (<replaceable>varname</replaceable>), based upon a named delimiter.</para>
  73. <example title="The 'varname' parameter can only accept a variable name, not a variable expression">
  74. exten => s,1,Set(foo=${CUT(bar,,2)}); This is correct syntax
  75. exten => s,2,Set(foo=${CUT(${bar},,2)}); This is invalid syntax (unless bar contains the name of another variable)
  76. </example>
  77. </description>
  78. </function>
  79. ***/
  80. struct sortable_keys {
  81. char *key;
  82. float value;
  83. };
  84. static int sort_subroutine(const void *arg1, const void *arg2)
  85. {
  86. const struct sortable_keys *one=arg1, *two=arg2;
  87. if (one->value < two->value)
  88. return -1;
  89. else if (one->value == two->value)
  90. return 0;
  91. else
  92. return 1;
  93. }
  94. #define ERROR_NOARG (-1)
  95. #define ERROR_NOMEM (-2)
  96. #define ERROR_USAGE (-3)
  97. static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
  98. {
  99. char *strings, *ptrkey, *ptrvalue;
  100. int count=1, count2, element_count=0;
  101. struct sortable_keys *sortable_keys;
  102. *buffer = '\0';
  103. if (!data)
  104. return ERROR_NOARG;
  105. strings = ast_strdupa(data);
  106. for (ptrkey = strings; *ptrkey; ptrkey++) {
  107. if (*ptrkey == ',')
  108. count++;
  109. }
  110. sortable_keys = ast_alloca(count * sizeof(struct sortable_keys));
  111. memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
  112. /* Parse each into a struct */
  113. count2 = 0;
  114. while ((ptrkey = strsep(&strings, ","))) {
  115. ptrvalue = strchr(ptrkey, ':');
  116. if (!ptrvalue) {
  117. count--;
  118. continue;
  119. }
  120. *ptrvalue++ = '\0';
  121. sortable_keys[count2].key = ptrkey;
  122. sscanf(ptrvalue, "%30f", &sortable_keys[count2].value);
  123. count2++;
  124. }
  125. /* Sort the structs */
  126. qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
  127. for (count2 = 0; count2 < count; count2++) {
  128. int blen = strlen(buffer);
  129. if (element_count++) {
  130. strncat(buffer + blen, ",", buflen - blen - 1);
  131. blen++;
  132. }
  133. strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
  134. }
  135. return 0;
  136. }
  137. static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen)
  138. {
  139. char *parse, ds[2], *var_expr;
  140. size_t delim_consumed;
  141. struct ast_str *var_value;
  142. AST_DECLARE_APP_ARGS(args,
  143. AST_APP_ARG(varname);
  144. AST_APP_ARG(delimiter);
  145. AST_APP_ARG(field);
  146. );
  147. parse = ast_strdupa(data);
  148. AST_STANDARD_APP_ARGS(args, parse);
  149. /* Check arguments */
  150. if (args.argc < 3) {
  151. return ERROR_NOARG;
  152. }
  153. var_expr = ast_alloca(strlen(args.varname) + 4);
  154. /* Get the value of the variable named in the 1st argument */
  155. snprintf(var_expr, strlen(args.varname) + 4, "${%s}", args.varname);
  156. var_value = ast_str_create(16);
  157. ast_str_substitute_variables(&var_value, 0, chan, var_expr);
  158. /* Copy delimiter from 2nd argument to ds[] possibly decoding backslash escapes */
  159. if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed)) {
  160. ast_copy_string(ds, "-", sizeof(ds));
  161. }
  162. ds[1] = '\0';
  163. if (ast_str_strlen(var_value)) {
  164. int curfieldnum = 1;
  165. char *curfieldptr = ast_str_buffer(var_value);
  166. int out_field_count = 0;
  167. while (curfieldptr != NULL && args.field != NULL) {
  168. char *next_range = strsep(&(args.field), "&");
  169. int start_field, stop_field;
  170. char trashchar;
  171. if (sscanf(next_range, "%30d-%30d", &start_field, &stop_field) == 2) {
  172. /* range with both start and end */
  173. } else if (sscanf(next_range, "-%30d", &stop_field) == 1) {
  174. /* range with end only */
  175. start_field = 1;
  176. } else if ((sscanf(next_range, "%30d%1c", &start_field, &trashchar) == 2) && (trashchar == '-')) {
  177. /* range with start only */
  178. stop_field = INT_MAX;
  179. } else if (sscanf(next_range, "%30d", &start_field) == 1) {
  180. /* single number */
  181. stop_field = start_field;
  182. } else {
  183. /* invalid field spec */
  184. ast_free(var_value);
  185. return ERROR_USAGE;
  186. }
  187. /* Get to start, if not there already */
  188. while (curfieldptr != NULL && curfieldnum < start_field) {
  189. strsep(&curfieldptr, ds);
  190. curfieldnum++;
  191. }
  192. /* Most frequent problem is the expectation of reordering fields */
  193. if (curfieldnum > start_field) {
  194. ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
  195. }
  196. /* Output fields until we either run out of fields or stop_field is reached */
  197. while (curfieldptr != NULL && curfieldnum <= stop_field) {
  198. char *field_value = strsep(&curfieldptr, ds);
  199. ast_str_append(buf, buflen, "%s%s", out_field_count++ ? ds : "", field_value);
  200. curfieldnum++;
  201. }
  202. }
  203. }
  204. ast_free(var_value);
  205. return 0;
  206. }
  207. static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  208. {
  209. int ret = -1;
  210. switch (sort_internal(chan, data, buf, len)) {
  211. case ERROR_NOARG:
  212. ast_log(LOG_ERROR, "SORT() requires an argument\n");
  213. break;
  214. case ERROR_NOMEM:
  215. ast_log(LOG_ERROR, "Out of memory\n");
  216. break;
  217. case 0:
  218. ret = 0;
  219. break;
  220. default:
  221. ast_log(LOG_ERROR, "Unknown internal error\n");
  222. }
  223. return ret;
  224. }
  225. static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  226. {
  227. int ret = -1;
  228. struct ast_str *str = ast_str_create(16);
  229. switch (cut_internal(chan, data, &str, len)) {
  230. case ERROR_NOARG:
  231. ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
  232. break;
  233. case ERROR_NOMEM:
  234. ast_log(LOG_ERROR, "Out of memory\n");
  235. break;
  236. case ERROR_USAGE:
  237. ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
  238. break;
  239. case 0:
  240. ret = 0;
  241. ast_copy_string(buf, ast_str_buffer(str), len);
  242. break;
  243. default:
  244. ast_log(LOG_ERROR, "Unknown internal error\n");
  245. }
  246. ast_free(str);
  247. return ret;
  248. }
  249. static int acf_cut_exec2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  250. {
  251. int ret = -1;
  252. switch (cut_internal(chan, data, buf, len)) {
  253. case ERROR_NOARG:
  254. ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
  255. break;
  256. case ERROR_NOMEM:
  257. ast_log(LOG_ERROR, "Out of memory\n");
  258. break;
  259. case ERROR_USAGE:
  260. ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
  261. break;
  262. case 0:
  263. ret = 0;
  264. break;
  265. default:
  266. ast_log(LOG_ERROR, "Unknown internal error\n");
  267. }
  268. return ret;
  269. }
  270. static struct ast_custom_function acf_sort = {
  271. .name = "SORT",
  272. .read = acf_sort_exec,
  273. };
  274. static struct ast_custom_function acf_cut = {
  275. .name = "CUT",
  276. .read = acf_cut_exec,
  277. .read2 = acf_cut_exec2,
  278. };
  279. static int unload_module(void)
  280. {
  281. int res = 0;
  282. res |= ast_custom_function_unregister(&acf_cut);
  283. res |= ast_custom_function_unregister(&acf_sort);
  284. return res;
  285. }
  286. static int load_module(void)
  287. {
  288. int res = 0;
  289. res |= ast_custom_function_register(&acf_cut);
  290. res |= ast_custom_function_register(&acf_sort);
  291. return res;
  292. }
  293. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");