NCDAst.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. };
  71. };
  72. struct NCDIf_s {
  73. NCDValue cond;
  74. NCDBlock block;
  75. };
  76. struct ProgramProcess {
  77. LinkedList1Node processes_list_node;
  78. NCDProcess p;
  79. };
  80. struct BlockStatement {
  81. LinkedList1Node statements_list_node;
  82. NCDStatement s;
  83. };
  84. struct IfBlockIf {
  85. LinkedList1Node ifs_list_node;
  86. NCDIf ifc;
  87. };
  88. //
  89. #define NCDSTATEMENT_REG 1
  90. #define NCDSTATEMENT_IF 2
  91. void NCDProgram_Init (NCDProgram *o);
  92. void NCDProgram_Free (NCDProgram *o);
  93. NCDProcess * NCDProgram_PrependProcess (NCDProgram *o, NCDProcess p) WARN_UNUSED;
  94. NCDProcess * NCDProgram_FirstProcess (NCDProgram *o);
  95. NCDProcess * NCDProgram_NextProcess (NCDProgram *o, NCDProcess *ep);
  96. size_t NCDProgram_NumProcesses (NCDProgram *o);
  97. int NCDProcess_Init (NCDProcess *o, int is_template, const char *name, NCDBlock block) WARN_UNUSED;
  98. void NCDProcess_Free (NCDProcess *o);
  99. int NCDProcess_IsTemplate (NCDProcess *o);
  100. const char * NCDProcess_Name (NCDProcess *o);
  101. NCDBlock * NCDProcess_Block (NCDProcess *o);
  102. void NCDBlock_Init (NCDBlock *o);
  103. void NCDBlock_Free (NCDBlock *o);
  104. int NCDBlock_PrependStatement (NCDBlock *o, NCDStatement s) WARN_UNUSED;
  105. int NCDBlock_InsertStatementAfter (NCDBlock *o, NCDStatement *after, NCDStatement s) WARN_UNUSED;
  106. NCDStatement * NCDBlock_ReplaceStatement (NCDBlock *o, NCDStatement *es, NCDStatement s);
  107. NCDStatement * NCDBlock_FirstStatement (NCDBlock *o);
  108. NCDStatement * NCDBlock_NextStatement (NCDBlock *o, NCDStatement *es);
  109. size_t NCDBlock_NumStatements (NCDBlock *o);
  110. int NCDStatement_InitReg (NCDStatement *o, const char *name, const char *objname, const char *cmdname, NCDValue args) WARN_UNUSED;
  111. int NCDStatement_InitIf (NCDStatement *o, const char *name, NCDIfBlock ifblock) WARN_UNUSED;
  112. void NCDStatement_Free (NCDStatement *o);
  113. int NCDStatement_Type (NCDStatement *o);
  114. const char * NCDStatement_Name (NCDStatement *o);
  115. const char * NCDStatement_RegObjName (NCDStatement *o);
  116. const char * NCDStatement_RegCmdName (NCDStatement *o);
  117. NCDValue * NCDStatement_RegArgs (NCDStatement *o);
  118. NCDIfBlock * NCDStatement_IfBlock (NCDStatement *o);
  119. void NCDStatement_IfAddElse (NCDStatement *o, NCDBlock else_block);
  120. NCDBlock * NCDStatement_IfElse (NCDStatement *o);
  121. NCDBlock NCDStatement_IfGrabElse (NCDStatement *o);
  122. void NCDIfBlock_Init (NCDIfBlock *o);
  123. void NCDIfBlock_Free (NCDIfBlock *o);
  124. int NCDIfBlock_PrependIf (NCDIfBlock *o, NCDIf ifc) WARN_UNUSED;
  125. NCDIf * NCDIfBlock_FirstIf (NCDIfBlock *o);
  126. NCDIf * NCDIfBlock_NextIf (NCDIfBlock *o, NCDIf *ei);
  127. NCDIf NCDIfBlock_GrabIf (NCDIfBlock *o, NCDIf *ei);
  128. void NCDIf_Init (NCDIf *o, NCDValue cond, NCDBlock block);
  129. void NCDIf_Free (NCDIf *o);
  130. void NCDIf_FreeGrab (NCDIf *o, NCDValue *out_cond, NCDBlock *out_block);
  131. NCDValue * NCDIf_Cond (NCDIf *o);
  132. NCDBlock * NCDIf_Block (NCDIf *o);
  133. #endif