print.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*$Id$
  2. *
  3. * Example ser module, it will just print its string parameter to stdout
  4. *
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. /*
  30. * History:
  31. * --------
  32. * 2003-03-10 module export interface updated to the new format (andrei)
  33. * 2003-03-11 flags export parameter added (janakj)
  34. * 2006-01-07 str export parameter added, overloading test (tma)
  35. */
  36. #include "../../sr_module.h"
  37. #include "../../route_struct.h"
  38. #include "../../str.h"
  39. #include <stdio.h>
  40. MODULE_VERSION
  41. static int print_fixup_f_1(void **param, int param_no);
  42. static int print_fixup_f_2(void **param, int param_no);
  43. static int print_f_0(struct sip_msg*, char*, char*);
  44. static int print_f_1(struct sip_msg*, char*, char*);
  45. static int print_f_2(struct sip_msg*, char*, char*);
  46. static int print_f1(struct sip_msg*, char*, char*);
  47. static int print_f2(struct sip_msg*, char*, char*);
  48. static int print_f3(struct sip_msg*, char*, char*, char*);
  49. static int print_f_var(struct sip_msg*, int argc, action_u_t argv[]);
  50. static int mod_init(void);
  51. /* the parameters are not used, they are only meant as an example*/
  52. char* string_param = 0;
  53. int int_param = 0;
  54. str str_param = STR_STATIC_INIT("");
  55. static cmd_export_t cmds[]={
  56. {"print", print_f_0, 0, 0, REQUEST_ROUTE}, // overload test
  57. {"print", print_f_1, 1, print_fixup_f_1, REQUEST_ROUTE},
  58. {"print", print_f_2, 2, print_fixup_f_2, REQUEST_ROUTE},
  59. {"print1", print_f1, 1, 0, REQUEST_ROUTE},
  60. {"print2", print_f2, 2, fixup_var_str_12, REQUEST_ROUTE},
  61. {"print3", (cmd_function)print_f3, 3, 0, REQUEST_ROUTE},
  62. {"printv", (cmd_function)print_f_var, VAR_PARAM_NO, 0, REQUEST_ROUTE},
  63. {0, 0, 0, 0, 0}
  64. };
  65. static param_export_t params[]={
  66. {"string_param", PARAM_STRING, &string_param},
  67. {"str_param", PARAM_STR, &str_param},
  68. {"int_param", PARAM_INT, &int_param},
  69. {0,0,0}
  70. };
  71. struct module_exports exports = {
  72. "print_stdout",
  73. cmds,
  74. 0, /* RPC methods */
  75. params,
  76. mod_init, /* module initialization function */
  77. 0, /* response function*/
  78. 0, /* destroy function */
  79. 0, /* oncancel function */
  80. 0 /* per-child init function */
  81. };
  82. static int mod_init(void)
  83. {
  84. fprintf(stderr, "print - initializing\n");
  85. DBG("print: string_param = '%s'\n", string_param);
  86. DBG("print: str_param = '%.*s'\n", str_param.len, str_param.s);
  87. DBG("print: int_param = %d\n", int_param);
  88. WARN("this is an example module, it has no practical use\n");
  89. return 0;
  90. }
  91. static int print_fixup_f(void **param, int param_no) {
  92. action_u_t *a;
  93. int n, i;
  94. n = fixup_get_param_count(param, param_no);
  95. for (i=1; i<=n; i++) {
  96. a = fixup_get_param(param, param_no, i);
  97. DBG("param #%d: '%s'\n", i, a->u.string);
  98. }
  99. return 1;
  100. }
  101. static int print_f_0(struct sip_msg* msg, char* s1, char* s2)
  102. {
  103. printf("<null>\n");
  104. return 1;
  105. }
  106. static int print_f_1(struct sip_msg* msg, char* s1, char* s2)
  107. {
  108. printf("%s\n",s1);
  109. return 1;
  110. }
  111. static int print_f_2(struct sip_msg* msg, char* s1, char* s2)
  112. {
  113. printf("%s%s\n",s1, s2);
  114. return 1;
  115. }
  116. static int print_fixup_f_1(void **param, int param_no) {
  117. DBG("print: print_fixup_f_1('%s')\n", (char*)*param);
  118. return print_fixup_f(param, param_no);
  119. }
  120. static int print_fixup_f_2(void **param, int param_no) {
  121. DBG("print: print_fixup_f_2('%s')\n", (char*)*param);
  122. return print_fixup_f(param, param_no);
  123. }
  124. /* 1 parameter, no fixup version */
  125. static int print_f1(struct sip_msg* msg, char* s1, char* not_used)
  126. {
  127. printf("%s\n", s1);
  128. return 1;
  129. }
  130. /* 2 parameters, fparam fixup version */
  131. static int print_f2(struct sip_msg* msg, char* s1, char* s2)
  132. {
  133. str a, b;
  134. if (get_str_fparam(&a, msg, (fparam_t*)s1) !=0 ||
  135. get_str_fparam(&b, msg, (fparam_t*)s2) !=0) {
  136. BUG("get_str_fparam failed\n");
  137. return -1;
  138. }
  139. printf("%.*s%.*s\n", a.len, a.s, b.len, b.s);
  140. return 1;
  141. }
  142. /* 3 parameters, no fixup version */
  143. static int print_f3(struct sip_msg* msg, char* s1, char* s2, char* s3)
  144. {
  145. printf("%s%s%s\n", s1, s2, s3);
  146. return 1;
  147. }
  148. /* variable number of parameters, no fixup version */
  149. static int print_f_var(struct sip_msg* msg, int argc, action_u_t argv[])
  150. {
  151. int i;
  152. for (i = 0; i < argc; i++)
  153. printf("%s", argv[i].u.string);
  154. printf("\n");
  155. return 1;
  156. }