NCDAst.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @file NCDAst.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_NCDAST_H
  30. #define BADVPN_NCDAST_H
  31. #include <stdint.h>
  32. #include <stddef.h>
  33. #include <misc/debug.h>
  34. #include <structure/LinkedList1.h>
  35. typedef struct NCDValue_s NCDValue;
  36. typedef struct NCDProgram_s NCDProgram;
  37. typedef struct NCDProcess_s NCDProcess;
  38. typedef struct NCDBlock_s NCDBlock;
  39. typedef struct NCDStatement_s NCDStatement;
  40. typedef struct NCDIfBlock_s NCDIfBlock;
  41. typedef struct NCDIf_s NCDIf;
  42. struct NCDValue_s {
  43. int type;
  44. union {
  45. struct {
  46. uint8_t *string;
  47. size_t string_len;
  48. };
  49. struct {
  50. LinkedList1 list;
  51. size_t list_count;
  52. };
  53. struct {
  54. LinkedList1 map_list;
  55. size_t map_count;
  56. };
  57. struct {
  58. char *var_name;
  59. };
  60. };
  61. };
  62. struct NCDProgram_s {
  63. LinkedList1 processes_list;
  64. size_t num_processes;
  65. };
  66. struct NCDBlock_s {
  67. LinkedList1 statements_list;
  68. size_t count;
  69. };
  70. struct NCDProcess_s {
  71. int is_template;
  72. char *name;
  73. NCDBlock block;
  74. };
  75. struct NCDIfBlock_s {
  76. LinkedList1 ifs_list;
  77. };
  78. struct NCDStatement_s {
  79. int type;
  80. char *name;
  81. union {
  82. struct {
  83. char *objname;
  84. char *cmdname;
  85. NCDValue args;
  86. } reg;
  87. struct {
  88. NCDIfBlock ifblock;
  89. int have_else;
  90. NCDBlock else_block;
  91. } ifc;
  92. struct {
  93. NCDValue collection;
  94. char *name1;
  95. char *name2;
  96. NCDBlock block;
  97. int is_grabbed;
  98. } foreach;
  99. };
  100. };
  101. struct NCDIf_s {
  102. NCDValue cond;
  103. NCDBlock block;
  104. };
  105. struct ProgramProcess {
  106. LinkedList1Node processes_list_node;
  107. NCDProcess p;
  108. };
  109. struct BlockStatement {
  110. LinkedList1Node statements_list_node;
  111. NCDStatement s;
  112. };
  113. struct IfBlockIf {
  114. LinkedList1Node ifs_list_node;
  115. NCDIf ifc;
  116. };
  117. //
  118. #define NCDVALUE_STRING 1
  119. #define NCDVALUE_LIST 2
  120. #define NCDVALUE_MAP 3
  121. #define NCDVALUE_VAR 4
  122. #define NCDSTATEMENT_REG 1
  123. #define NCDSTATEMENT_IF 2
  124. #define NCDSTATEMENT_FOREACH 3
  125. void NCDValue_Free (NCDValue *o);
  126. int NCDValue_Type (NCDValue *o);
  127. int NCDValue_InitString (NCDValue *o, const char *str) WARN_UNUSED;
  128. int NCDValue_InitStringBin (NCDValue *o, const uint8_t *str, size_t len) WARN_UNUSED;
  129. const char * NCDValue_StringValue (NCDValue *o);
  130. size_t NCDValue_StringLength (NCDValue *o);
  131. void NCDValue_InitList (NCDValue *o);
  132. size_t NCDValue_ListCount (NCDValue *o);
  133. int NCDValue_ListAppend (NCDValue *o, NCDValue v) WARN_UNUSED;
  134. int NCDValue_ListPrepend (NCDValue *o, NCDValue v) WARN_UNUSED;
  135. NCDValue * NCDValue_ListFirst (NCDValue *o);
  136. NCDValue * NCDValue_ListNext (NCDValue *o, NCDValue *ev);
  137. void NCDValue_InitMap (NCDValue *o);
  138. size_t NCDValue_MapCount (NCDValue *o);
  139. int NCDValue_MapPrepend (NCDValue *o, NCDValue key, NCDValue val) WARN_UNUSED;
  140. NCDValue * NCDValue_MapFirstKey (NCDValue *o);
  141. NCDValue * NCDValue_MapNextKey (NCDValue *o, NCDValue *ekey);
  142. NCDValue * NCDValue_MapKeyValue (NCDValue *o, NCDValue *ekey);
  143. int NCDValue_InitVar (NCDValue *o, const char *var_name) WARN_UNUSED;
  144. const char * NCDValue_VarName (NCDValue *o);
  145. void NCDProgram_Init (NCDProgram *o);
  146. void NCDProgram_Free (NCDProgram *o);
  147. NCDProcess * NCDProgram_PrependProcess (NCDProgram *o, NCDProcess p) WARN_UNUSED;
  148. NCDProcess * NCDProgram_FirstProcess (NCDProgram *o);
  149. NCDProcess * NCDProgram_NextProcess (NCDProgram *o, NCDProcess *ep);
  150. size_t NCDProgram_NumProcesses (NCDProgram *o);
  151. int NCDProcess_Init (NCDProcess *o, int is_template, const char *name, NCDBlock block) WARN_UNUSED;
  152. void NCDProcess_Free (NCDProcess *o);
  153. int NCDProcess_IsTemplate (NCDProcess *o);
  154. const char * NCDProcess_Name (NCDProcess *o);
  155. NCDBlock * NCDProcess_Block (NCDProcess *o);
  156. void NCDBlock_Init (NCDBlock *o);
  157. void NCDBlock_Free (NCDBlock *o);
  158. int NCDBlock_PrependStatement (NCDBlock *o, NCDStatement s) WARN_UNUSED;
  159. int NCDBlock_InsertStatementAfter (NCDBlock *o, NCDStatement *after, NCDStatement s) WARN_UNUSED;
  160. NCDStatement * NCDBlock_ReplaceStatement (NCDBlock *o, NCDStatement *es, NCDStatement s);
  161. NCDStatement * NCDBlock_FirstStatement (NCDBlock *o);
  162. NCDStatement * NCDBlock_NextStatement (NCDBlock *o, NCDStatement *es);
  163. size_t NCDBlock_NumStatements (NCDBlock *o);
  164. int NCDStatement_InitReg (NCDStatement *o, const char *name, const char *objname, const char *cmdname, NCDValue args) WARN_UNUSED;
  165. int NCDStatement_InitIf (NCDStatement *o, const char *name, NCDIfBlock ifblock) WARN_UNUSED;
  166. int NCDStatement_InitForeach (NCDStatement *o, const char *name, NCDValue collection, const char *name1, const char *name2, NCDBlock block) WARN_UNUSED;
  167. void NCDStatement_Free (NCDStatement *o);
  168. int NCDStatement_Type (NCDStatement *o);
  169. const char * NCDStatement_Name (NCDStatement *o);
  170. const char * NCDStatement_RegObjName (NCDStatement *o);
  171. const char * NCDStatement_RegCmdName (NCDStatement *o);
  172. NCDValue * NCDStatement_RegArgs (NCDStatement *o);
  173. NCDIfBlock * NCDStatement_IfBlock (NCDStatement *o);
  174. void NCDStatement_IfAddElse (NCDStatement *o, NCDBlock else_block);
  175. NCDBlock * NCDStatement_IfElse (NCDStatement *o);
  176. NCDBlock NCDStatement_IfGrabElse (NCDStatement *o);
  177. NCDValue * NCDStatement_ForeachCollection (NCDStatement *o);
  178. const char * NCDStatement_ForeachName1 (NCDStatement *o);
  179. const char * NCDStatement_ForeachName2 (NCDStatement *o);
  180. NCDBlock * NCDStatement_ForeachBlock (NCDStatement *o);
  181. void NCDStatement_ForeachGrab (NCDStatement *o, NCDValue *out_collection, NCDBlock *out_block);
  182. void NCDIfBlock_Init (NCDIfBlock *o);
  183. void NCDIfBlock_Free (NCDIfBlock *o);
  184. int NCDIfBlock_PrependIf (NCDIfBlock *o, NCDIf ifc) WARN_UNUSED;
  185. NCDIf * NCDIfBlock_FirstIf (NCDIfBlock *o);
  186. NCDIf * NCDIfBlock_NextIf (NCDIfBlock *o, NCDIf *ei);
  187. NCDIf NCDIfBlock_GrabIf (NCDIfBlock *o, NCDIf *ei);
  188. void NCDIf_Init (NCDIf *o, NCDValue cond, NCDBlock block);
  189. void NCDIf_Free (NCDIf *o);
  190. void NCDIf_FreeGrab (NCDIf *o, NCDValue *out_cond, NCDBlock *out_block);
  191. NCDValue * NCDIf_Cond (NCDIf *o);
  192. NCDBlock * NCDIf_Block (NCDIf *o);
  193. #endif