select_module.xml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
  4. <section id="select_module" xmlns:xi="http://www.w3.org/2001/XInclude">
  5. <sectioninfo>
  6. <revhistory>
  7. <revision>
  8. <revnumber>$Revision$</revnumber>
  9. <date>$Date$</date>
  10. </revision>
  11. </revhistory>
  12. </sectioninfo>
  13. <section><title>Extend select framework with module related functions</title>
  14. <para>
  15. If you want to extend functions which select framework can call with some
  16. dependent on module you have to follow next steps:
  17. <itemizedlist>
  18. <listitem><para>
  19. define working functions
  20. </para>
  21. </listitem>
  22. <listitem><para>
  23. create module's select parsing table
  24. </para>
  25. </listitem>
  26. <listitem><para>
  27. in module's mod_init call register_select_table
  28. </para>
  29. </listitem>
  30. </itemizedlist>
  31. </para>
  32. </section>
  33. <section>
  34. <title>Define your working functions</title>
  35. <para>
  36. Working functions are the piece of code, which is called as when SER needs
  37. get result of select function (defined as @name.foo[2].bar[5]). The working copy
  38. has following definition:
  39. </para>
  40. <para><emphasis>
  41. int <function>select_function_name</function>(str* res, struct select *s, struct sip_msg *msg)
  42. </emphasis>
  43. Pointer to the string result workspace <emphasis>res</emphasis>, don't allocate
  44. memory for the string, but use static buffer. There is no way to free the
  45. allocated memory.
  46. Pointer to the parsed select structure <emphasis>s</emphasis>. Holds all names,
  47. numbers divided by the dot and square bracket notation. Use that if any of the
  48. part used CONSUME_NEXT_STR or CONSUME_NEXT_INT to get the value.
  49. Pointer to the processed message structure <emphasis>msg</emphasis>.
  50. </para>
  51. <para>
  52. <emphasis>FIXUP CALL:</emphasis>
  53. If you set FIXUP_CALL flag for the final
  54. function, the fixup call will be done immediatelly after function resolution.
  55. Such call is indicated with res==NULL &amp;&amp; msg==NULL. Such call can convert any of
  56. the select's parameter into internal data (can hold one pointer), if you do
  57. that, set param_type to SEL_PARAM_DATA.
  58. </para>
  59. <para>
  60. Result code of the function declares if the call was successful and if the result is
  61. valid string or empty string.
  62. </para>
  63. <itemizedlist>
  64. <listitem><para><emphasis>-1</emphasis> error</para></listitem>
  65. <listitem><para><emphasis>0</emphasis> success, res contains non-empty string</para></listitem>
  66. <listitem><para><emphasis>1</emphasis> success, result of select call is empty string (res can be left
  67. unchanged)</para></listitem>
  68. </itemizedlist>
  69. </section>
  70. <section>
  71. <title>Create module's select parsing table</title>
  72. <para>Define static table of select_row_t type and initialize it directly.
  73. </para>
  74. <para>The table is representation of tree (or oriented graph if you want), where
  75. first column represents current (up-to now) resolved function (starting with NULL),
  76. next two columns define if next parameter is integer or particullar string, next
  77. column is new resolved function which will be tested in next round and the last
  78. column is set of flags.
  79. </para>
  80. <programlisting><![CDATA[
  81. static select_row_t test_sel[] = {
  82. { NULL, SEL_PARAM_STR, STR_STATIC_INIT("test"), select_test, 0},
  83. { select_test, SEL_PARAM_STR, STR_STATIC_INIT("value"), select_test_val, 0},
  84. { select_test, SEL_PARAM_STR, STR_STATIC_INIT("echo"), select_test_echo, CONSUME_NEXT_STR},
  85. { NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
  86. };
  87. ]]>
  88. </programlisting>
  89. <para>
  90. So in the previous example, the first line will accept syntax
  91. <emphasis>@test</emphasis> and set the resolved function to select_test. In the
  92. next round, all rows with the select_test in the first column will be used to
  93. match, so the next two lines
  94. are candidates to match depending on the next part of select syntax. If it
  95. matches <emphasis>@test.value</emphasis>, the function is resolved to
  96. select_test_val, if it matches <emphasis>@test.echo.anystring</emphasis>, it is
  97. resolved into select_test_echo. Flag <emphasis>CONSUME_NEXT_STR</emphasis> will
  98. accept any string at the 3rd position. As the <emphasis>OPTIONAL</emphasis> flag
  99. is not present, it won't accept just the <emphasis>@test.echo</emphasis> syntax.
  100. </para>
  101. <para>
  102. The table ends with the <emphasis>NULL</emphasis> in the 1st and 4th column,
  103. other columns are not checked (the notation in the example suits well).
  104. </para>
  105. <para>
  106. At the resolving time all function names must be already defined. For functions which are
  107. not leaves, you can use macro <emphasis>ABSTRACT_F(name)</emphasis> to define empty
  108. function, for working function you can use advance definition using
  109. <emphasis>SELECT_F(name)</emphasis> macro.
  110. </para>
  111. </section>
  112. <!--
  113. TODO:
  114. <section>
  115. <title>Available flag options and their meaning</title>
  116. <para>
  117. <emphasis></emphasis>
  118. </para>
  119. </section>
  120. -->
  121. <section>
  122. <title>Register the created table</title>
  123. <para>
  124. In the module initialization function call <emphasis>register_select_table(table)</emphasis>
  125. where table is the parsing tree/table you have defined in previous step.
  126. This call ensures, that the table will become part of the parsing logic for
  127. all select framework calls defined in the script file or called by another
  128. module's parse_select.
  129. </para>
  130. <para>
  131. <emphasis>NOTE:</emphasis> The tables are inserted into the beginning of the
  132. list, so the core's table (and thus parseable names and definitions) can be
  133. overrided by module's function, if it is defined with the same name. To
  134. avoid such situation, the best practice is to start module's select with the
  135. module's name. E.g in our example code both select functions start
  136. with @test...
  137. </para>
  138. </section>
  139. <section>
  140. <title>Example - module defining select extension</title>
  141. <para>Example module <emphasis>test</emphasis>, which defines two select function.
  142. <itemizedlist>
  143. <listitem><para>
  144. <emphasis>@test.value</emphasis> - returns value passed as modules parameter
  145. "value"
  146. </para></listitem>
  147. <listitem><para>
  148. <emphasis>@test.echo.xxx</emphasis> - returns xxx regardless of what you put
  149. as xxx (shows CONSUME_NEXT_STR option)
  150. </para></listitem>
  151. </itemizedlist>
  152. </para>
  153. <example id="test.c">
  154. <title>test.c</title>
  155. <programlisting><![CDATA[
  156. #include <string.h>
  157. #include "../../sr_module.h"
  158. #include "../../str.h"
  159. #include "../../dprint.h"
  160. #include "../../select.h"
  161. MODULE_VERSION
  162. static cmd_export_t cmds[] = {
  163. {0, 0, 0, 0, 0}
  164. };
  165. static char* value=NULL;
  166. static param_export_t params[] = {
  167. {"value", STR_PARAM, &value},
  168. {0, 0, 0}
  169. };
  170. int select_test_val(str* res, struct select* s, struct sip_msg* msg) {
  171. DBG("SELECT_TEST_VAL called test_val=%s\n", value);
  172. res->s=value;
  173. res->len=strlen(value);
  174. return 0;
  175. }
  176. int select_test_echo(str* res, struct select* s, struct sip_msg* msg) {
  177. DBG("SELECT_TEST_ECHO called\n");
  178. if (s->params[s->n-1].type==SEL_PARAM_STR) {
  179. *res=s->params[s->n-1].v.s;
  180. return 0;
  181. } else
  182. return -1;
  183. }
  184. ABSTRACT_F(select_test)
  185. static select_row_t test_sel[] = {
  186. { NULL, SEL_PARAM_STR, STR_STATIC_INIT("test"), select_test, 0},
  187. { select_test, SEL_PARAM_STR, STR_STATIC_INIT("value"), select_test_val, 0},
  188. { select_test, SEL_PARAM_STR, STR_STATIC_INIT("echo"), select_test_echo, CONSUME_NEXT_STR},
  189. { NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
  190. };
  191. static int init(void) {
  192. register_select_table(test_sel);
  193. return 0;
  194. };
  195. struct module_exports exports = {
  196. "test",
  197. cmds, /* Exported commands */
  198. 0, /* RPC methods */
  199. params, /* Exported parameters */
  200. init, /* module initialization function */
  201. 0, /* response function*/
  202. 0, /* destroy function */
  203. 0, /* oncancel function */
  204. 0 /* per-child init function */
  205. };
  206. ]]>
  207. </programlisting>
  208. </example>
  209. </section>
  210. </section>