NCDInterpreter.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @file NCDInterpreter.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_NCD_INTERPRETER_H
  30. #define BADVPN_NCD_INTERPRETER_H
  31. #include <stddef.h>
  32. #include <misc/debug.h>
  33. #include <base/DebugObject.h>
  34. #include <system/BTime.h>
  35. #include <system/BReactor.h>
  36. #include <ncd/NCDStringIndex.h>
  37. #include <ncd/NCDModuleIndex.h>
  38. #include <ncd/NCDAst.h>
  39. #include <ncd/NCDEvaluator.h>
  40. #include <ncd/NCDInterpProg.h>
  41. #include <ncd/NCDModule.h>
  42. #include <structure/LinkedList1.h>
  43. #ifndef BADVPN_NO_PROCESS
  44. #include <system/BProcess.h>
  45. #endif
  46. #ifndef BADVPN_NO_UDEV
  47. #include <udevmonitor/NCDUdevManager.h>
  48. #endif
  49. #ifndef BADVPN_NO_RANDOM
  50. #include <random/BRandom2.h>
  51. #endif
  52. /**
  53. * Handler called when the interpreter has terminated, and {@link NCDInterpreter_Free}
  54. * can be called.
  55. *
  56. * @param user the user member of struct {@link NCDInterpreter_params}
  57. * @param exit_code the exit code specified in the last interpreter termination request
  58. */
  59. typedef void (*NCDInterpreter_handler_finished) (void *user, int exit_code);
  60. struct NCDInterpreter_params {
  61. // callbacks
  62. NCDInterpreter_handler_finished handler_finished;
  63. void *user;
  64. // options
  65. btime_t retry_time;
  66. char **extra_args;
  67. int num_extra_args;
  68. // possibly shared resources
  69. BReactor *reactor;
  70. #ifndef BADVPN_NO_PROCESS
  71. BProcessManager *manager;
  72. #endif
  73. #ifndef BADVPN_NO_UDEV
  74. NCDUdevManager *umanager;
  75. #endif
  76. #ifndef BADVPN_NO_RANDOM
  77. BRandom2 *random2;
  78. #endif
  79. };
  80. typedef struct {
  81. // parameters
  82. struct NCDInterpreter_params params;
  83. // are we terminating
  84. int terminating;
  85. int main_exit_code;
  86. // string index
  87. NCDStringIndex string_index;
  88. // module index
  89. NCDModuleIndex mindex;
  90. // program AST
  91. NCDProgram program;
  92. // expression evaluator
  93. NCDEvaluator evaluator;
  94. // structure for efficient interpretation
  95. NCDInterpProg iprogram;
  96. // common module parameters
  97. struct NCDModuleInst_params module_params;
  98. struct NCDModuleInst_iparams module_iparams;
  99. // processes
  100. LinkedList1 processes;
  101. DebugObject d_obj;
  102. } NCDInterpreter;
  103. /**
  104. * Initializes and starts the interpreter.
  105. *
  106. * @param o the interpreter
  107. * @param program the program to execute in AST format. The program must
  108. * not contain any 'include' or 'include_guard' directives.
  109. * The interpreter takes ownership of the program, regardless
  110. * of the success of this function.
  111. * @param params other parameters
  112. * @return 1 on success, 0 on failure
  113. */
  114. int NCDInterpreter_Init (NCDInterpreter *o, NCDProgram program, struct NCDInterpreter_params params) WARN_UNUSED;
  115. /**
  116. * Frees the interpreter.
  117. * This may only be called after the interpreter has terminated, i.e.
  118. * the {@link NCDInterpreter_handler_finished} handler has been called.
  119. * Additionally, it can be called right after {@link NCDInterpreter_Init}
  120. * before any of the interpreter's {@link BPendingGroup} jobs have executed.
  121. */
  122. void NCDInterpreter_Free (NCDInterpreter *o);
  123. /**
  124. * Requests termination of the interpreter.
  125. * NOTE: the program can request its own termination, possibly overriding the exit
  126. * code specified here. Expect the program to terminate even if this function was
  127. * not called.
  128. *
  129. * @param o the interpreter
  130. * @param exit_code the exit code to be passed to {@link NCDInterpreter_handler_finished}.
  131. * This overrides any exit code set previously.
  132. */
  133. void NCDInterpreter_RequestShutdown (NCDInterpreter *o, int exit_code);
  134. #endif