func_cut.c 8.6 KB

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