emncd.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @file emncd.c
  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. #include <stdlib.h>
  30. #include <string.h>
  31. #include <misc/version.h>
  32. #include <misc/debug.h>
  33. #include <base/BLog.h>
  34. #include <system/BTime.h>
  35. #include <system/BReactor.h>
  36. #include <ncd/NCDInterpreter.h>
  37. #include <ncd/NCDConfigParser.h>
  38. #include <generated/blog_channel_ncd.h>
  39. static BReactor reactor;
  40. static int running;
  41. static NCDInterpreter interpreter;
  42. static void interpreter_handler_finished (void *user, int exit_code)
  43. {
  44. ASSERT(running)
  45. fprintf(stderr, "--- interpreter terminated ---\n");
  46. NCDInterpreter_Free(&interpreter);
  47. running = 0;
  48. }
  49. __attribute__((used))
  50. int main ()
  51. {
  52. BLog_InitStderr();
  53. fprintf(stderr, "--- initializing emncd version "GLOBAL_VERSION" ---\n");
  54. BTime_Init();
  55. BReactor_EmscriptenInit(&reactor);
  56. running = 0;
  57. return 0;
  58. }
  59. __attribute__((used))
  60. void emncd_start (const char *program_text, int loglevel)
  61. {
  62. ASSERT(program_text);
  63. ASSERT(loglevel >= 0);
  64. ASSERT(loglevel <= BLOG_DEBUG);
  65. if (running) {
  66. fprintf(stderr, "--- cannot start, interpreter is already running! ---\n");
  67. return;
  68. }
  69. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  70. BLog_SetChannelLoglevel(i, loglevel);
  71. }
  72. // parse program
  73. NCDProgram program;
  74. if (!NCDConfigParser_Parse((char *)program_text ,strlen(program_text), &program)) {
  75. fprintf(stderr, "--- error: failed to parse the program ---\n");
  76. return;
  77. }
  78. // include commands are not implemented currently
  79. if (NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE) || NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE_GUARD)) {
  80. fprintf(stderr, "--- error: include mechanism is not supported in emncd ---\n");
  81. NCDProgram_Free(&program);
  82. return;
  83. }
  84. fprintf(stderr, "--- starting interpreter ---\n");
  85. struct NCDInterpreter_params params;
  86. params.handler_finished = interpreter_handler_finished;
  87. params.user = NULL;
  88. params.retry_time = 5000;
  89. params.extra_args = NULL;
  90. params.num_extra_args = 0;
  91. params.reactor = &reactor;
  92. if (!NCDInterpreter_Init(&interpreter, program, params)) {
  93. fprintf(stderr, "--- failed to initialize the interpreter ---\n");
  94. return;
  95. }
  96. running = 1;
  97. BReactor_EmscriptenSync(&reactor);
  98. }
  99. __attribute__((used))
  100. void emncd_stop (void)
  101. {
  102. if (!running) {
  103. fprintf(stderr, "--- cannot request termination, interpreter is not running! ---\n");
  104. return;
  105. }
  106. fprintf(stderr, "--- requesting interpreter termination ---\n");
  107. NCDInterpreter_RequestShutdown(&interpreter, 0);
  108. BReactor_EmscriptenSync(&reactor);
  109. }