emncd.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <generated/blog_channel_ncd.h>
  38. static BReactor reactor;
  39. static int running;
  40. static NCDInterpreter interpreter;
  41. static void interpreter_handler_finished (void *user, int exit_code)
  42. {
  43. ASSERT(running)
  44. fprintf(stderr, "--- interpreter terminated ---\n");
  45. NCDInterpreter_Free(&interpreter);
  46. running = 0;
  47. }
  48. __attribute__((used))
  49. int main ()
  50. {
  51. BLog_InitStderr();
  52. fprintf(stderr, "--- initializing emncd version "GLOBAL_VERSION" ---\n");
  53. BTime_Init();
  54. BReactor_EmscriptenInit(&reactor);
  55. running = 0;
  56. return 0;
  57. }
  58. __attribute__((used))
  59. void emncd_start (const char *program, int loglevel)
  60. {
  61. ASSERT(program);
  62. ASSERT(loglevel >= 0);
  63. ASSERT(loglevel <= BLOG_DEBUG);
  64. if (running) {
  65. fprintf(stderr, "--- cannot start, interpreter is already running! ---\n");
  66. return;
  67. }
  68. fprintf(stderr, "--- starting interpreter ---\n");
  69. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  70. BLog_SetChannelLoglevel(i, loglevel);
  71. }
  72. struct NCDInterpreter_params params;
  73. params.handler_finished = interpreter_handler_finished;
  74. params.user = NULL;
  75. params.retry_time = 5000;
  76. params.extra_args = NULL;
  77. params.num_extra_args = 0;
  78. params.reactor = &reactor;
  79. if (!NCDInterpreter_Init(&interpreter, program, strlen(program), params)) {
  80. fprintf(stderr, "--- failed to initialize the interpreter (syntax error?) ---\n");
  81. return;
  82. }
  83. running = 1;
  84. BReactor_EmscriptenSync(&reactor);
  85. }
  86. __attribute__((used))
  87. void emncd_stop (void)
  88. {
  89. if (!running) {
  90. fprintf(stderr, "--- cannot request termination, interpreter is not running! ---\n");
  91. return;
  92. }
  93. fprintf(stderr, "--- requesting interpreter termination ---\n");
  94. NCDInterpreter_RequestShutdown(&interpreter, 0);
  95. BReactor_EmscriptenSync(&reactor);
  96. }