app_cut.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Cut application
  5. *
  6. * Copyright (c) 2003 Tilghman Lesher. All rights reserved.
  7. *
  8. * Tilghman Lesher <app_cut__v003@the-tilghman.com>
  9. *
  10. * $Id$
  11. *
  12. * This code is released by the author with no restrictions on usage.
  13. *
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <asterisk/file.h>
  20. #include <asterisk/logger.h>
  21. #include <asterisk/options.h>
  22. #include <asterisk/channel.h>
  23. #include <asterisk/pbx.h>
  24. #include <asterisk/module.h>
  25. /* Maximum length of any variable */
  26. #define MAXRESULT 1024
  27. static char *tdesc = "Cuts up variables";
  28. static char *app_cut = "Cut";
  29. static char *cut_synopsis = "Splits a variable's content using the specified delimiter";
  30. static char *cut_descrip =
  31. "Usage: Cut(newvar=varname,delimiter,fieldspec)\n"
  32. " newvar - new variable created from result string\n"
  33. " varname - variable you want cut\n"
  34. " delimiter - defaults to '-'\n"
  35. " fieldspec - number of the field you want (1-based offset)\n"
  36. " may also be specified as a range (with -)\n"
  37. " or group of ranges and fields (with &)\n"
  38. " Returns 0 or -1 on hangup or error.\n";
  39. STANDARD_LOCAL_USER;
  40. LOCAL_USER_DECL;
  41. static int cut_exec(struct ast_channel *chan, void *data)
  42. {
  43. int res=0;
  44. struct localuser *u;
  45. char *s, *newvar=NULL, *varname=NULL, *delimiter=NULL, *field=NULL;
  46. int args_okay = 0;
  47. LOCAL_USER_ADD(u);
  48. /* Check and parse arguments */
  49. if (data) {
  50. s = ast_strdupa((char *)data);
  51. if (s) {
  52. newvar = strsep(&s, "=");
  53. if (newvar && (newvar[0] != '\0')) {
  54. varname = strsep(&s, "|");
  55. if (varname && (varname[0] != '\0')) {
  56. delimiter = strsep(&s, "|");
  57. if (delimiter) {
  58. field = strsep(&s, "|");
  59. if (field) {
  60. args_okay = 1;
  61. }
  62. }
  63. }
  64. }
  65. } else {
  66. ast_log(LOG_ERROR, "Out of memory\n");
  67. res = -1;
  68. }
  69. }
  70. if (args_okay) {
  71. char d, ds[2];
  72. char *tmp = alloca(strlen(varname) + 4);
  73. char varvalue[MAXRESULT], *tmp2=varvalue;
  74. char retstring[MAXRESULT];
  75. memset(retstring, 0, MAXRESULT);
  76. if (tmp) {
  77. snprintf(tmp, strlen(varname) + 4, "${%s}", varname);
  78. memset(varvalue, 0, sizeof(varvalue));
  79. } else {
  80. ast_log(LOG_ERROR, "Out of memory");
  81. return -1;
  82. }
  83. if (delimiter[0])
  84. d = delimiter[0];
  85. else
  86. d = '-';
  87. /* String form of the delimiter, for use with strsep(3) */
  88. snprintf(ds, sizeof(ds), "%c", d);
  89. pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
  90. if (tmp2) {
  91. int curfieldnum = 1;
  92. while ((tmp2 != NULL) && (field != NULL)) {
  93. char *nextgroup = strsep(&field, "&");
  94. int num1 = 0, num2 = MAXRESULT;
  95. char trashchar;
  96. if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) {
  97. /* range with both start and end */
  98. } else if (sscanf(nextgroup, "-%d", &num2) == 1) {
  99. /* range with end */
  100. num1 = 0;
  101. } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
  102. /* range with start */
  103. num2 = MAXRESULT;
  104. } else if (sscanf(nextgroup, "%d", &num1) == 1) {
  105. /* single number */
  106. num2 = num1;
  107. } else {
  108. ast_log(LOG_ERROR, "Cut(): Illegal range '%s'\n", nextgroup);
  109. ast_log(LOG_ERROR, "Usage: %s\n", cut_synopsis);
  110. return -1;
  111. }
  112. /* Get to start, if any */
  113. if (num1 > 0) {
  114. while ((tmp2 != (char *)NULL + 1) && (curfieldnum < num1)) {
  115. tmp2 = index(tmp2, d) + 1;
  116. curfieldnum++;
  117. }
  118. }
  119. /* Most frequent problem is the expectation of reordering fields */
  120. if ((num1 > 0) && (curfieldnum > num1)) {
  121. ast_log(LOG_WARNING, "Cut(): we're already past the field you wanted?\n");
  122. }
  123. /* Re-null tmp2 if we added 1 to NULL */
  124. if (tmp2 == (char *)NULL + 1)
  125. tmp2 = NULL;
  126. /* Output fields until we either run out of fields or num2 is reached */
  127. while ((tmp2 != NULL) && (curfieldnum <= num2)) {
  128. char *tmp3 = strsep(&tmp2, ds);
  129. int curlen = strlen(retstring);
  130. if (strlen(retstring)) {
  131. snprintf(retstring + curlen, MAXRESULT - curlen, "%c%s", d, tmp3);
  132. } else {
  133. snprintf(retstring, MAXRESULT, "%s", tmp3);
  134. }
  135. curfieldnum++;
  136. }
  137. }
  138. }
  139. pbx_builtin_setvar_helper(chan, newvar, retstring);
  140. }
  141. LOCAL_USER_REMOVE(u);
  142. return res;
  143. }
  144. int unload_module(void)
  145. {
  146. STANDARD_HANGUP_LOCALUSERS;
  147. return ast_unregister_application(app_cut);
  148. }
  149. int load_module(void)
  150. {
  151. return ast_register_application(app_cut, cut_exec, cut_synopsis, cut_descrip);
  152. }
  153. char *description(void)
  154. {
  155. return tdesc;
  156. }
  157. int usecount(void)
  158. {
  159. int res;
  160. STANDARD_USECOUNT(res);
  161. return res;
  162. }
  163. char *key()
  164. {
  165. return ASTERISK_GPL_KEY;
  166. }