NCDAst.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 NCDProgramElem_s NCDProgramElem;
  38. typedef struct NCDProcess_s NCDProcess;
  39. typedef struct NCDBlock_s NCDBlock;
  40. typedef struct NCDStatement_s NCDStatement;
  41. typedef struct NCDIfBlock_s NCDIfBlock;
  42. typedef struct NCDIf_s NCDIf;
  43. struct NCDValue_s {
  44. int type;
  45. union {
  46. struct {
  47. uint8_t *string;
  48. size_t string_len;
  49. };
  50. struct {
  51. LinkedList1 list;
  52. size_t list_count;
  53. };
  54. struct {
  55. LinkedList1 map_list;
  56. size_t map_count;
  57. };
  58. struct {
  59. char *var_name;
  60. };
  61. struct {
  62. NCDValue *invoc_func;
  63. NCDValue *invoc_arg;
  64. };
  65. };
  66. };
  67. struct NCDProgram_s {
  68. LinkedList1 elems_list;
  69. size_t num_elems;
  70. };
  71. struct NCDBlock_s {
  72. LinkedList1 statements_list;
  73. size_t count;
  74. };
  75. struct NCDProcess_s {
  76. int is_template;
  77. char *name;
  78. NCDBlock block;
  79. };
  80. struct NCDProgramElem_s {
  81. int type;
  82. union {
  83. NCDProcess process;
  84. struct {
  85. char *path_data;
  86. size_t path_length;
  87. } include;
  88. struct {
  89. char *id_data;
  90. size_t id_length;
  91. } include_guard;
  92. };
  93. };
  94. struct NCDIfBlock_s {
  95. LinkedList1 ifs_list;
  96. };
  97. struct NCDStatement_s {
  98. int type;
  99. char *name;
  100. union {
  101. struct {
  102. char *objname;
  103. char *cmdname;
  104. NCDValue args;
  105. } reg;
  106. struct {
  107. NCDIfBlock ifblock;
  108. int have_else;
  109. NCDBlock else_block;
  110. int iftype;
  111. } ifc;
  112. struct {
  113. NCDValue collection;
  114. char *name1;
  115. char *name2;
  116. NCDBlock block;
  117. int is_grabbed;
  118. } foreach;
  119. struct {
  120. NCDBlock block;
  121. int is_grabbed;
  122. } block;
  123. };
  124. };
  125. struct NCDIf_s {
  126. NCDValue cond;
  127. NCDBlock block;
  128. };
  129. //
  130. #define NCDVALUE_STRING 1
  131. #define NCDVALUE_LIST 2
  132. #define NCDVALUE_MAP 3
  133. #define NCDVALUE_VAR 4
  134. #define NCDVALUE_INVOC 5
  135. #define NCDPROGRAMELEM_PROCESS 1
  136. #define NCDPROGRAMELEM_INCLUDE 2
  137. #define NCDPROGRAMELEM_INCLUDE_GUARD 3
  138. #define NCDSTATEMENT_REG 1
  139. #define NCDSTATEMENT_IF 2
  140. #define NCDSTATEMENT_FOREACH 3
  141. #define NCDSTATEMENT_BLOCK 4
  142. #define NCDIFTYPE_IF 1
  143. #define NCDIFTYPE_DO 2
  144. void NCDValue_Free (NCDValue *o);
  145. int NCDValue_Type (NCDValue *o);
  146. int NCDValue_InitString (NCDValue *o, const char *str) WARN_UNUSED;
  147. int NCDValue_InitStringBin (NCDValue *o, const uint8_t *str, size_t len) WARN_UNUSED;
  148. const char * NCDValue_StringValue (NCDValue *o);
  149. size_t NCDValue_StringLength (NCDValue *o);
  150. void NCDValue_InitList (NCDValue *o);
  151. size_t NCDValue_ListCount (NCDValue *o);
  152. int NCDValue_ListAppend (NCDValue *o, NCDValue v) WARN_UNUSED;
  153. int NCDValue_ListPrepend (NCDValue *o, NCDValue v) WARN_UNUSED;
  154. NCDValue * NCDValue_ListFirst (NCDValue *o);
  155. NCDValue * NCDValue_ListNext (NCDValue *o, NCDValue *ev);
  156. void NCDValue_InitMap (NCDValue *o);
  157. size_t NCDValue_MapCount (NCDValue *o);
  158. int NCDValue_MapPrepend (NCDValue *o, NCDValue key, NCDValue val) WARN_UNUSED;
  159. NCDValue * NCDValue_MapFirstKey (NCDValue *o);
  160. NCDValue * NCDValue_MapNextKey (NCDValue *o, NCDValue *ekey);
  161. NCDValue * NCDValue_MapKeyValue (NCDValue *o, NCDValue *ekey);
  162. int NCDValue_InitVar (NCDValue *o, const char *var_name) WARN_UNUSED;
  163. const char * NCDValue_VarName (NCDValue *o);
  164. int NCDValue_InitInvoc (NCDValue *o, NCDValue func, NCDValue arg) WARN_UNUSED;
  165. NCDValue * NCDValue_InvocFunc (NCDValue *o);
  166. NCDValue * NCDValue_InvocArg (NCDValue *o);
  167. void NCDProgram_Init (NCDProgram *o);
  168. void NCDProgram_Free (NCDProgram *o);
  169. NCDProgramElem * NCDProgram_PrependElem (NCDProgram *o, NCDProgramElem elem) WARN_UNUSED;
  170. NCDProgramElem * NCDProgram_FirstElem (NCDProgram *o);
  171. NCDProgramElem * NCDProgram_NextElem (NCDProgram *o, NCDProgramElem *ee);
  172. size_t NCDProgram_NumElems (NCDProgram *o);
  173. int NCDProgram_ContainsElemType (NCDProgram *o, int elem_type);
  174. void NCDProgram_RemoveElem (NCDProgram *o, NCDProgramElem *ee);
  175. int NCDProgram_ReplaceElemWithProgram (NCDProgram *o, NCDProgramElem *ee, NCDProgram replace_prog) WARN_UNUSED;
  176. void NCDProgramElem_InitProcess (NCDProgramElem *o, NCDProcess process);
  177. int NCDProgramElem_InitInclude (NCDProgramElem *o, const char *path_data, size_t path_length) WARN_UNUSED;
  178. int NCDProgramElem_InitIncludeGuard (NCDProgramElem *o, const char *id_data, size_t id_length) WARN_UNUSED;
  179. void NCDProgramElem_Free (NCDProgramElem *o);
  180. int NCDProgramElem_Type (NCDProgramElem *o);
  181. NCDProcess * NCDProgramElem_Process (NCDProgramElem *o);
  182. const char * NCDProgramElem_IncludePathData (NCDProgramElem *o);
  183. size_t NCDProgramElem_IncludePathLength (NCDProgramElem *o);
  184. const char * NCDProgramElem_IncludeGuardIdData (NCDProgramElem *o);
  185. size_t NCDProgramElem_IncludeGuardIdLength (NCDProgramElem *o);
  186. int NCDProcess_Init (NCDProcess *o, int is_template, const char *name, NCDBlock block) WARN_UNUSED;
  187. void NCDProcess_Free (NCDProcess *o);
  188. int NCDProcess_IsTemplate (NCDProcess *o);
  189. const char * NCDProcess_Name (NCDProcess *o);
  190. NCDBlock * NCDProcess_Block (NCDProcess *o);
  191. void NCDBlock_Init (NCDBlock *o);
  192. void NCDBlock_Free (NCDBlock *o);
  193. int NCDBlock_PrependStatement (NCDBlock *o, NCDStatement s) WARN_UNUSED;
  194. int NCDBlock_InsertStatementAfter (NCDBlock *o, NCDStatement *after, NCDStatement s) WARN_UNUSED;
  195. NCDStatement * NCDBlock_ReplaceStatement (NCDBlock *o, NCDStatement *es, NCDStatement s);
  196. NCDStatement * NCDBlock_FirstStatement (NCDBlock *o);
  197. NCDStatement * NCDBlock_NextStatement (NCDBlock *o, NCDStatement *es);
  198. size_t NCDBlock_NumStatements (NCDBlock *o);
  199. int NCDStatement_InitReg (NCDStatement *o, const char *name, const char *objname, const char *cmdname, NCDValue args) WARN_UNUSED;
  200. int NCDStatement_InitIf (NCDStatement *o, const char *name, NCDIfBlock ifblock, int iftype) WARN_UNUSED;
  201. int NCDStatement_InitForeach (NCDStatement *o, const char *name, NCDValue collection, const char *name1, const char *name2, NCDBlock block) WARN_UNUSED;
  202. int NCDStatement_InitBlock (NCDStatement *o, const char *name, NCDBlock block) WARN_UNUSED;
  203. void NCDStatement_Free (NCDStatement *o);
  204. int NCDStatement_Type (NCDStatement *o);
  205. const char * NCDStatement_Name (NCDStatement *o);
  206. const char * NCDStatement_RegObjName (NCDStatement *o);
  207. const char * NCDStatement_RegCmdName (NCDStatement *o);
  208. NCDValue * NCDStatement_RegArgs (NCDStatement *o);
  209. int NCDStatement_IfType (NCDStatement *o);
  210. NCDIfBlock * NCDStatement_IfBlock (NCDStatement *o);
  211. void NCDStatement_IfAddElse (NCDStatement *o, NCDBlock else_block);
  212. NCDBlock * NCDStatement_IfElse (NCDStatement *o);
  213. NCDBlock NCDStatement_IfGrabElse (NCDStatement *o);
  214. NCDValue * NCDStatement_ForeachCollection (NCDStatement *o);
  215. const char * NCDStatement_ForeachName1 (NCDStatement *o);
  216. const char * NCDStatement_ForeachName2 (NCDStatement *o);
  217. NCDBlock * NCDStatement_ForeachBlock (NCDStatement *o);
  218. void NCDStatement_ForeachGrab (NCDStatement *o, NCDValue *out_collection, NCDBlock *out_block);
  219. NCDBlock * NCDStatement_BlockBlock (NCDStatement *o);
  220. NCDBlock NCDStatement_BlockGrabBlock (NCDStatement *o);
  221. void NCDIfBlock_Init (NCDIfBlock *o);
  222. void NCDIfBlock_Free (NCDIfBlock *o);
  223. int NCDIfBlock_PrependIf (NCDIfBlock *o, NCDIf ifc) WARN_UNUSED;
  224. NCDIf * NCDIfBlock_FirstIf (NCDIfBlock *o);
  225. NCDIf * NCDIfBlock_NextIf (NCDIfBlock *o, NCDIf *ei);
  226. NCDIf NCDIfBlock_GrabIf (NCDIfBlock *o, NCDIf *ei);
  227. void NCDIf_Init (NCDIf *o, NCDValue cond, NCDBlock block);
  228. void NCDIf_InitBlock (NCDIf *o, NCDBlock block);
  229. void NCDIf_Free (NCDIf *o);
  230. void NCDIf_FreeGrab (NCDIf *o, NCDValue *out_cond, NCDBlock *out_block);
  231. NCDBlock NCDIf_FreeGrabBlock (NCDIf *o);
  232. NCDValue * NCDIf_Cond (NCDIf *o);
  233. NCDBlock * NCDIf_Block (NCDIf *o);
  234. #endif