NCDInterpreter.h 5.0 KB

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