NCDAst.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. } ifc;
  111. struct {
  112. NCDValue collection;
  113. char *name1;
  114. char *name2;
  115. NCDBlock block;
  116. int is_grabbed;
  117. } foreach;
  118. };
  119. };
  120. struct NCDIf_s {
  121. NCDValue cond;
  122. NCDBlock block;
  123. };
  124. //
  125. #define NCDVALUE_STRING 1
  126. #define NCDVALUE_LIST 2
  127. #define NCDVALUE_MAP 3
  128. #define NCDVALUE_VAR 4
  129. #define NCDVALUE_INVOC 5
  130. #define NCDPROGRAMELEM_PROCESS 1
  131. #define NCDPROGRAMELEM_INCLUDE 2
  132. #define NCDPROGRAMELEM_INCLUDE_GUARD 3
  133. #define NCDSTATEMENT_REG 1
  134. #define NCDSTATEMENT_IF 2
  135. #define NCDSTATEMENT_FOREACH 3
  136. void NCDValue_Free (NCDValue *o);
  137. int NCDValue_Type (NCDValue *o);
  138. int NCDValue_InitString (NCDValue *o, const char *str) WARN_UNUSED;
  139. int NCDValue_InitStringBin (NCDValue *o, const uint8_t *str, size_t len) WARN_UNUSED;
  140. const char * NCDValue_StringValue (NCDValue *o);
  141. size_t NCDValue_StringLength (NCDValue *o);
  142. void NCDValue_InitList (NCDValue *o);
  143. size_t NCDValue_ListCount (NCDValue *o);
  144. int NCDValue_ListAppend (NCDValue *o, NCDValue v) WARN_UNUSED;
  145. int NCDValue_ListPrepend (NCDValue *o, NCDValue v) WARN_UNUSED;
  146. NCDValue * NCDValue_ListFirst (NCDValue *o);
  147. NCDValue * NCDValue_ListNext (NCDValue *o, NCDValue *ev);
  148. void NCDValue_InitMap (NCDValue *o);
  149. size_t NCDValue_MapCount (NCDValue *o);
  150. int NCDValue_MapPrepend (NCDValue *o, NCDValue key, NCDValue val) WARN_UNUSED;
  151. NCDValue * NCDValue_MapFirstKey (NCDValue *o);
  152. NCDValue * NCDValue_MapNextKey (NCDValue *o, NCDValue *ekey);
  153. NCDValue * NCDValue_MapKeyValue (NCDValue *o, NCDValue *ekey);
  154. int NCDValue_InitVar (NCDValue *o, const char *var_name) WARN_UNUSED;
  155. const char * NCDValue_VarName (NCDValue *o);
  156. int NCDValue_InitInvoc (NCDValue *o, NCDValue func, NCDValue arg) WARN_UNUSED;
  157. NCDValue * NCDValue_InvocFunc (NCDValue *o);
  158. NCDValue * NCDValue_InvocArg (NCDValue *o);
  159. void NCDProgram_Init (NCDProgram *o);
  160. void NCDProgram_Free (NCDProgram *o);
  161. NCDProgramElem * NCDProgram_PrependElem (NCDProgram *o, NCDProgramElem elem) WARN_UNUSED;
  162. NCDProgramElem * NCDProgram_FirstElem (NCDProgram *o);
  163. NCDProgramElem * NCDProgram_NextElem (NCDProgram *o, NCDProgramElem *ee);
  164. size_t NCDProgram_NumElems (NCDProgram *o);
  165. int NCDProgram_ContainsElemType (NCDProgram *o, int elem_type);
  166. void NCDProgram_RemoveElem (NCDProgram *o, NCDProgramElem *ee);
  167. int NCDProgram_ReplaceElemWithProgram (NCDProgram *o, NCDProgramElem *ee, NCDProgram replace_prog) WARN_UNUSED;
  168. void NCDProgramElem_InitProcess (NCDProgramElem *o, NCDProcess process);
  169. int NCDProgramElem_InitInclude (NCDProgramElem *o, const char *path_data, size_t path_length) WARN_UNUSED;
  170. int NCDProgramElem_InitIncludeGuard (NCDProgramElem *o, const char *id_data, size_t id_length) WARN_UNUSED;
  171. void NCDProgramElem_Free (NCDProgramElem *o);
  172. int NCDProgramElem_Type (NCDProgramElem *o);
  173. NCDProcess * NCDProgramElem_Process (NCDProgramElem *o);
  174. const char * NCDProgramElem_IncludePathData (NCDProgramElem *o);
  175. size_t NCDProgramElem_IncludePathLength (NCDProgramElem *o);
  176. const char * NCDProgramElem_IncludeGuardIdData (NCDProgramElem *o);
  177. size_t NCDProgramElem_IncludeGuardIdLength (NCDProgramElem *o);
  178. int NCDProcess_Init (NCDProcess *o, int is_template, const char *name, NCDBlock block) WARN_UNUSED;
  179. void NCDProcess_Free (NCDProcess *o);
  180. int NCDProcess_IsTemplate (NCDProcess *o);
  181. const char * NCDProcess_Name (NCDProcess *o);
  182. NCDBlock * NCDProcess_Block (NCDProcess *o);
  183. void NCDBlock_Init (NCDBlock *o);
  184. void NCDBlock_Free (NCDBlock *o);
  185. int NCDBlock_PrependStatement (NCDBlock *o, NCDStatement s) WARN_UNUSED;
  186. int NCDBlock_InsertStatementAfter (NCDBlock *o, NCDStatement *after, NCDStatement s) WARN_UNUSED;
  187. NCDStatement * NCDBlock_ReplaceStatement (NCDBlock *o, NCDStatement *es, NCDStatement s);
  188. NCDStatement * NCDBlock_FirstStatement (NCDBlock *o);
  189. NCDStatement * NCDBlock_NextStatement (NCDBlock *o, NCDStatement *es);
  190. size_t NCDBlock_NumStatements (NCDBlock *o);
  191. int NCDStatement_InitReg (NCDStatement *o, const char *name, const char *objname, const char *cmdname, NCDValue args) WARN_UNUSED;
  192. int NCDStatement_InitIf (NCDStatement *o, const char *name, NCDIfBlock ifblock) WARN_UNUSED;
  193. int NCDStatement_InitForeach (NCDStatement *o, const char *name, NCDValue collection, const char *name1, const char *name2, NCDBlock block) WARN_UNUSED;
  194. void NCDStatement_Free (NCDStatement *o);
  195. int NCDStatement_Type (NCDStatement *o);
  196. const char * NCDStatement_Name (NCDStatement *o);
  197. const char * NCDStatement_RegObjName (NCDStatement *o);
  198. const char * NCDStatement_RegCmdName (NCDStatement *o);
  199. NCDValue * NCDStatement_RegArgs (NCDStatement *o);
  200. NCDIfBlock * NCDStatement_IfBlock (NCDStatement *o);
  201. void NCDStatement_IfAddElse (NCDStatement *o, NCDBlock else_block);
  202. NCDBlock * NCDStatement_IfElse (NCDStatement *o);
  203. NCDBlock NCDStatement_IfGrabElse (NCDStatement *o);
  204. NCDValue * NCDStatement_ForeachCollection (NCDStatement *o);
  205. const char * NCDStatement_ForeachName1 (NCDStatement *o);
  206. const char * NCDStatement_ForeachName2 (NCDStatement *o);
  207. NCDBlock * NCDStatement_ForeachBlock (NCDStatement *o);
  208. void NCDStatement_ForeachGrab (NCDStatement *o, NCDValue *out_collection, NCDBlock *out_block);
  209. void NCDIfBlock_Init (NCDIfBlock *o);
  210. void NCDIfBlock_Free (NCDIfBlock *o);
  211. int NCDIfBlock_PrependIf (NCDIfBlock *o, NCDIf ifc) WARN_UNUSED;
  212. NCDIf * NCDIfBlock_FirstIf (NCDIfBlock *o);
  213. NCDIf * NCDIfBlock_NextIf (NCDIfBlock *o, NCDIf *ei);
  214. NCDIf NCDIfBlock_GrabIf (NCDIfBlock *o, NCDIf *ei);
  215. void NCDIf_Init (NCDIf *o, NCDValue cond, NCDBlock block);
  216. void NCDIf_Free (NCDIf *o);
  217. void NCDIf_FreeGrab (NCDIf *o, NCDValue *out_cond, NCDBlock *out_block);
  218. NCDValue * NCDIf_Cond (NCDIf *o);
  219. NCDBlock * NCDIf_Block (NCDIf *o);
  220. #endif