NCDAst.h 8.5 KB

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