NCDInterpreter.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. typedef void (*NCDInterpreter_handler_finished) (void *user, int exit_code);
  54. struct NCDInterpreter_params {
  55. // callbacks
  56. NCDInterpreter_handler_finished handler_finished;
  57. void *user;
  58. // options
  59. btime_t retry_time;
  60. char **extra_args;
  61. int num_extra_args;
  62. // possibly shared resources
  63. BReactor *reactor;
  64. #ifndef BADVPN_NO_PROCESS
  65. BProcessManager *manager;
  66. #endif
  67. #ifndef BADVPN_NO_UDEV
  68. NCDUdevManager *umanager;
  69. #endif
  70. #ifndef BADVPN_NO_RANDOM
  71. BRandom2 *random2;
  72. #endif
  73. };
  74. typedef struct {
  75. // parameters
  76. struct NCDInterpreter_params params;
  77. // are we terminating
  78. int terminating;
  79. int main_exit_code;
  80. // string index
  81. NCDStringIndex string_index;
  82. // method index
  83. NCDMethodIndex method_index;
  84. // module index
  85. NCDModuleIndex mindex;
  86. // program AST
  87. NCDProgram program;
  88. // placeholder database
  89. NCDPlaceholderDb placeholder_db;
  90. // structure for efficient interpretation
  91. NCDInterpProg iprogram;
  92. // common module parameters
  93. struct NCDModuleInst_params module_params;
  94. struct NCDModuleInst_iparams module_iparams;
  95. // number of modules we have found and inited
  96. size_t num_inited_modules;
  97. // processes
  98. LinkedList1 processes;
  99. DebugObject d_obj;
  100. } NCDInterpreter;
  101. int NCDInterpreter_Init (NCDInterpreter *o, const char *program, size_t program_len, struct NCDInterpreter_params params) WARN_UNUSED;
  102. void NCDInterpreter_Free (NCDInterpreter *o);
  103. void NCDInterpreter_RequestShutdown (NCDInterpreter *o, int exit_code);
  104. #endif