app_eval.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Eval application
  5. *
  6. * Copyright (c) 2004 Tilghman Lesher. All rights reserved.
  7. *
  8. * Tilghman Lesher <app_eval__v001@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 = "Reevaluates strings";
  28. static char *app_eval = "Eval";
  29. static char *eval_synopsis = "Evaluates a string";
  30. static char *eval_descrip =
  31. "Usage: Eval(newvar=somestring)\n"
  32. " Normally Asterisk evaluates variables inline. But what if you want to\n"
  33. "store variable offsets in a database, to be evaluated later? Eval is\n"
  34. "the answer, by allowing a string to be evaluated twice in the dialplan,\n"
  35. "the first time as part of the normal dialplan, and the second using Eval.\n";
  36. STANDARD_LOCAL_USER;
  37. LOCAL_USER_DECL;
  38. static int eval_exec(struct ast_channel *chan, void *data)
  39. {
  40. int res=0;
  41. struct localuser *u;
  42. char *s, *newvar=NULL, tmp[MAXRESULT];
  43. LOCAL_USER_ADD(u);
  44. /* Check and parse arguments */
  45. if (data) {
  46. s = ast_strdupa((char *)data);
  47. if (s) {
  48. newvar = strsep(&s, "=");
  49. if (newvar && (newvar[0] != '\0')) {
  50. memset(tmp, 0, MAXRESULT);
  51. pbx_substitute_variables_helper(chan, s, tmp, MAXRESULT - 1);
  52. pbx_builtin_setvar_helper(chan, newvar, tmp);
  53. }
  54. } else {
  55. ast_log(LOG_ERROR, "Out of memory\n");
  56. res = -1;
  57. }
  58. }
  59. LOCAL_USER_REMOVE(u);
  60. return res;
  61. }
  62. int unload_module(void)
  63. {
  64. STANDARD_HANGUP_LOCALUSERS;
  65. return ast_unregister_application(app_eval);
  66. }
  67. int load_module(void)
  68. {
  69. return ast_register_application(app_eval, eval_exec, eval_synopsis, eval_descrip);
  70. }
  71. char *description(void)
  72. {
  73. return tdesc;
  74. }
  75. int usecount(void)
  76. {
  77. int res;
  78. STANDARD_USECOUNT(res);
  79. return res;
  80. }
  81. char *key()
  82. {
  83. return ASTERISK_GPL_KEY;
  84. }