app_cut.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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__v002@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 = "Cut(newvar=varname|delimiter|fieldspec)";
  30. static char *cut_descrip =
  31. "Cut(newvar=varname,delimiter,field)\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 *tmp2 = alloca(MAXRESULT);
  74. char retstring[MAXRESULT];
  75. if (tmp2)
  76. memset(tmp2, 0, MAXRESULT);
  77. memset(retstring, 0, MAXRESULT);
  78. if (tmp && tmp2) {
  79. snprintf(tmp, strlen(varname) + 4, "${%s}", varname);
  80. memset(tmp2, 0, sizeof(tmp2));
  81. } else {
  82. ast_log(LOG_ERROR, "Out of memory");
  83. return -1;
  84. }
  85. if (delimiter[0])
  86. d = delimiter[0];
  87. else
  88. d = '-';
  89. /* String form of the delimiter, for use with strsep(3) */
  90. sprintf(ds,"%c",d);
  91. pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
  92. if (tmp2) {
  93. int curfieldnum = 1;
  94. while ((tmp2 != NULL) && (field != NULL)) {
  95. char *nextgroup = strsep(&field, "&");
  96. int num1 = 0, num2 = MAXRESULT;
  97. char trashchar;
  98. if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) {
  99. /* range with both start and end */
  100. } else if (sscanf(nextgroup, "-%d", &num2) == 1) {
  101. /* range with end */
  102. num1 = 0;
  103. } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
  104. /* range with start */
  105. num2 = MAXRESULT;
  106. } else if (sscanf(nextgroup, "%d", &num1) == 1) {
  107. /* single number */
  108. num2 = num1;
  109. } else {
  110. ast_log(LOG_ERROR, "Cut(): Illegal range '%s'\n", nextgroup);
  111. ast_log(LOG_ERROR, "Usage: %s\n", cut_synopsis);
  112. return -1;
  113. }
  114. /* Get to start, if any */
  115. if (num1 > 0) {
  116. while ((tmp2 != (char *)NULL + 1) && (curfieldnum < num1)) {
  117. tmp2 = index(tmp2, d) + 1;
  118. curfieldnum++;
  119. }
  120. }
  121. /* Most frequent problem is the expectation of reordering fields */
  122. if ((num1 > 0) && (curfieldnum > num1)) {
  123. ast_log(LOG_WARNING, "Cut(): we're already past the field you wanted?\n");
  124. }
  125. /* Re-null tmp2 if we added 1 to NULL */
  126. if (tmp2 == (char *)NULL + 1)
  127. tmp2 = NULL;
  128. /* Output fields until we either run out of fields or num2 is reached */
  129. while ((tmp2 != NULL) && (curfieldnum <= num2)) {
  130. char *tmp3 = strsep(&tmp2, ds);
  131. int curlen = strlen(retstring);
  132. if (strlen(retstring)) {
  133. snprintf(retstring + curlen, MAXRESULT - curlen, "%c%s", d, tmp3);
  134. } else {
  135. snprintf(retstring, MAXRESULT, "%s", tmp3);
  136. }
  137. curfieldnum++;
  138. }
  139. }
  140. }
  141. pbx_builtin_setvar_helper(chan, newvar, retstring);
  142. }
  143. LOCAL_USER_REMOVE(u);
  144. return res;
  145. }
  146. int unload_module(void)
  147. {
  148. STANDARD_HANGUP_LOCALUSERS;
  149. return ast_unregister_application(app_cut);
  150. }
  151. int load_module(void)
  152. {
  153. return ast_register_application(app_cut, cut_exec, cut_synopsis, cut_descrip);
  154. }
  155. char *description(void)
  156. {
  157. return tdesc;
  158. }
  159. int usecount(void)
  160. {
  161. int res;
  162. STANDARD_USECOUNT(res);
  163. return res;
  164. }
  165. char *key()
  166. {
  167. return ASTERISK_GPL_KEY;
  168. }