NCDAst.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <misc/debug.h>
  32. #include <structure/LinkedList1.h>
  33. #include <ncd/NCDValue.h>
  34. typedef struct NCDProgram_s NCDProgram;
  35. typedef struct NCDProcess_s NCDProcess;
  36. typedef struct NCDBlock_s NCDBlock;
  37. typedef struct NCDStatement_s NCDStatement;
  38. typedef struct NCDIfBlock_s NCDIfBlock;
  39. typedef struct NCDIf_s NCDIf;
  40. struct NCDProgram_s {
  41. LinkedList1 processes_list;
  42. size_t num_processes;
  43. };
  44. struct NCDBlock_s {
  45. LinkedList1 statements_list;
  46. size_t count;
  47. };
  48. struct NCDProcess_s {
  49. int is_template;
  50. char *name;
  51. NCDBlock block;
  52. };
  53. struct NCDIfBlock_s {
  54. LinkedList1 ifs_list;
  55. };
  56. struct NCDStatement_s {
  57. int type;
  58. char *name;
  59. union {
  60. struct {
  61. char *objname;
  62. char *cmdname;
  63. NCDValue args;
  64. } reg;
  65. struct {
  66. NCDIfBlock ifblock;
  67. int have_else;
  68. NCDBlock else_block;
  69. } ifc;
  70. struct {
  71. NCDValue collection;
  72. char *name1;
  73. char *name2;
  74. NCDBlock block;
  75. int is_grabbed;
  76. } foreach;
  77. };
  78. };
  79. struct NCDIf_s {
  80. NCDValue cond;
  81. NCDBlock block;
  82. };
  83. struct ProgramProcess {
  84. LinkedList1Node processes_list_node;
  85. NCDProcess p;
  86. };
  87. struct BlockStatement {
  88. LinkedList1Node statements_list_node;
  89. NCDStatement s;
  90. };
  91. struct IfBlockIf {
  92. LinkedList1Node ifs_list_node;
  93. NCDIf ifc;
  94. };
  95. //
  96. #define NCDSTATEMENT_REG 1
  97. #define NCDSTATEMENT_IF 2
  98. #define NCDSTATEMENT_FOREACH 3
  99. void NCDProgram_Init (NCDProgram *o);
  100. void NCDProgram_Free (NCDProgram *o);
  101. NCDProcess * NCDProgram_PrependProcess (NCDProgram *o, NCDProcess p) WARN_UNUSED;
  102. NCDProcess * NCDProgram_FirstProcess (NCDProgram *o);
  103. NCDProcess * NCDProgram_NextProcess (NCDProgram *o, NCDProcess *ep);
  104. size_t NCDProgram_NumProcesses (NCDProgram *o);
  105. int NCDProcess_Init (NCDProcess *o, int is_template, const char *name, NCDBlock block) WARN_UNUSED;
  106. void NCDProcess_Free (NCDProcess *o);
  107. int NCDProcess_IsTemplate (NCDProcess *o);
  108. const char * NCDProcess_Name (NCDProcess *o);
  109. NCDBlock * NCDProcess_Block (NCDProcess *o);
  110. void NCDBlock_Init (NCDBlock *o);
  111. void NCDBlock_Free (NCDBlock *o);
  112. int NCDBlock_PrependStatement (NCDBlock *o, NCDStatement s) WARN_UNUSED;
  113. int NCDBlock_InsertStatementAfter (NCDBlock *o, NCDStatement *after, NCDStatement s) WARN_UNUSED;
  114. NCDStatement * NCDBlock_ReplaceStatement (NCDBlock *o, NCDStatement *es, NCDStatement s);
  115. NCDStatement * NCDBlock_FirstStatement (NCDBlock *o);
  116. NCDStatement * NCDBlock_NextStatement (NCDBlock *o, NCDStatement *es);
  117. size_t NCDBlock_NumStatements (NCDBlock *o);
  118. int NCDStatement_InitReg (NCDStatement *o, const char *name, const char *objname, const char *cmdname, NCDValue args) WARN_UNUSED;
  119. int NCDStatement_InitIf (NCDStatement *o, const char *name, NCDIfBlock ifblock) WARN_UNUSED;
  120. int NCDStatement_InitForeach (NCDStatement *o, const char *name, NCDValue collection, const char *name1, const char *name2, NCDBlock block) WARN_UNUSED;
  121. void NCDStatement_Free (NCDStatement *o);
  122. int NCDStatement_Type (NCDStatement *o);
  123. const char * NCDStatement_Name (NCDStatement *o);
  124. const char * NCDStatement_RegObjName (NCDStatement *o);
  125. const char * NCDStatement_RegCmdName (NCDStatement *o);
  126. NCDValue * NCDStatement_RegArgs (NCDStatement *o);
  127. NCDIfBlock * NCDStatement_IfBlock (NCDStatement *o);
  128. void NCDStatement_IfAddElse (NCDStatement *o, NCDBlock else_block);
  129. NCDBlock * NCDStatement_IfElse (NCDStatement *o);
  130. NCDBlock NCDStatement_IfGrabElse (NCDStatement *o);
  131. NCDValue * NCDStatement_ForeachCollection (NCDStatement *o);
  132. const char * NCDStatement_ForeachName1 (NCDStatement *o);
  133. const char * NCDStatement_ForeachName2 (NCDStatement *o);
  134. NCDBlock * NCDStatement_ForeachBlock (NCDStatement *o);
  135. void NCDStatement_ForeachGrab (NCDStatement *o, NCDValue *out_collection, NCDBlock *out_block);
  136. void NCDIfBlock_Init (NCDIfBlock *o);
  137. void NCDIfBlock_Free (NCDIfBlock *o);
  138. int NCDIfBlock_PrependIf (NCDIfBlock *o, NCDIf ifc) WARN_UNUSED;
  139. NCDIf * NCDIfBlock_FirstIf (NCDIfBlock *o);
  140. NCDIf * NCDIfBlock_NextIf (NCDIfBlock *o, NCDIf *ei);
  141. NCDIf NCDIfBlock_GrabIf (NCDIfBlock *o, NCDIf *ei);
  142. void NCDIf_Init (NCDIf *o, NCDValue cond, NCDBlock block);
  143. void NCDIf_Free (NCDIf *o);
  144. void NCDIf_FreeGrab (NCDIf *o, NCDValue *out_cond, NCDBlock *out_block);
  145. NCDValue * NCDIf_Cond (NCDIf *o);
  146. NCDBlock * NCDIf_Block (NCDIf *o);
  147. #endif