call2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /**
  2. * @file call2.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. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * call2(string template, list args)
  33. * call2_if(string cond, string template, list args)
  34. * call2_ifelse(string cond, string template, string else_template, list args)
  35. * embcall2(string template)
  36. * embcall2_if(string cond, string template)
  37. * embcall2_ifelse(string cond, string template, string else_template)
  38. * embcall2_multif(string cond1, string template1, ..., [string else_template])
  39. */
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <misc/debug.h>
  43. #include <ncd/NCDModule.h>
  44. #include <ncd/static_strings.h>
  45. #include <ncd/value_utils.h>
  46. #include <generated/blog_channel_ncd_call2.h>
  47. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  48. #define STATE_WORKING 1
  49. #define STATE_UP 2
  50. #define STATE_WAITING 3
  51. #define STATE_TERMINATING 4
  52. #define STATE_NONE 5
  53. struct instance {
  54. NCDModuleInst *i;
  55. NCDModuleProcess process;
  56. int embed;
  57. int state;
  58. };
  59. static void process_handler_event (struct instance *o, int event);
  60. static int process_func_getspecialobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object);
  61. static int caller_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object);
  62. static void func_new_templ (void *vo, NCDModuleInst *i, NCDValRef template_name, NCDValRef args, int embed);
  63. static void instance_free (struct instance *o);
  64. enum {STRING_CALLER};
  65. static struct NCD_string_request strings[] = {
  66. {"_caller"}, {NULL}
  67. };
  68. static void process_handler_event (struct instance *o, int event)
  69. {
  70. switch (event) {
  71. case NCDMODULEPROCESS_EVENT_UP: {
  72. ASSERT(o->state == STATE_WORKING)
  73. // signal up
  74. NCDModuleInst_Backend_Up(o->i);
  75. // set state up
  76. o->state = STATE_UP;
  77. } break;
  78. case NCDMODULEPROCESS_EVENT_DOWN: {
  79. ASSERT(o->state == STATE_UP)
  80. // signal down
  81. NCDModuleInst_Backend_Down(o->i);
  82. // set state waiting
  83. o->state = STATE_WAITING;
  84. } break;
  85. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  86. ASSERT(o->state == STATE_TERMINATING)
  87. // die finally
  88. instance_free(o);
  89. return;
  90. } break;
  91. default: ASSERT(0);
  92. }
  93. }
  94. static int process_func_getspecialobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
  95. {
  96. if (o->embed) {
  97. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  98. }
  99. if (name == strings[STRING_CALLER].id) {
  100. *out_object = NCDObject_Build(-1, o, NULL, (NCDObject_func_getobj)caller_obj_func_getobj);
  101. return 1;
  102. }
  103. return 0;
  104. }
  105. static int caller_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
  106. {
  107. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  108. }
  109. static void func_new_templ (void *vo, NCDModuleInst *i, NCDValRef template_name, NCDValRef args, int embed)
  110. {
  111. ASSERT(NCDVal_IsInvalid(template_name) || NCDVal_IsString(template_name))
  112. ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
  113. ASSERT(embed == !!embed)
  114. struct instance *o = vo;
  115. o->i = i;
  116. // remember embed
  117. o->embed = embed;
  118. if (NCDVal_IsInvalid(template_name) || ncd_is_none(template_name)) {
  119. // signal up
  120. NCDModuleInst_Backend_Up(o->i);
  121. // set state none
  122. o->state = STATE_NONE;
  123. } else {
  124. // create process
  125. if (!NCDModuleProcess_InitValue(&o->process, o->i, template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  126. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  127. goto fail0;
  128. }
  129. // set special functions
  130. NCDModuleProcess_SetSpecialFuncs(&o->process, (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  131. // set state working
  132. o->state = STATE_WORKING;
  133. }
  134. return;
  135. fail0:
  136. NCDModuleInst_Backend_SetError(i);
  137. NCDModuleInst_Backend_Dead(i);
  138. }
  139. static void instance_free (struct instance *o)
  140. {
  141. // free process
  142. if (o->state != STATE_NONE) {
  143. NCDModuleProcess_Free(&o->process);
  144. }
  145. NCDModuleInst_Backend_Dead(o->i);
  146. }
  147. static void func_new_call (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  148. {
  149. NCDValRef template_arg;
  150. NCDValRef args_arg;
  151. if (!NCDVal_ListRead(params->args, 2, &template_arg, &args_arg)) {
  152. ModuleLog(i, BLOG_ERROR, "wrong arity");
  153. goto fail0;
  154. }
  155. if (!NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg)) {
  156. ModuleLog(i, BLOG_ERROR, "wrong type");
  157. goto fail0;
  158. }
  159. func_new_templ(vo, i, template_arg, args_arg, 0);
  160. return;
  161. fail0:
  162. NCDModuleInst_Backend_SetError(i);
  163. NCDModuleInst_Backend_Dead(i);
  164. }
  165. static void func_new_embcall (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  166. {
  167. NCDValRef template_arg;
  168. if (!NCDVal_ListRead(params->args, 1, &template_arg)) {
  169. ModuleLog(i, BLOG_ERROR, "wrong arity");
  170. goto fail0;
  171. }
  172. if (!NCDVal_IsString(template_arg)) {
  173. ModuleLog(i, BLOG_ERROR, "wrong type");
  174. goto fail0;
  175. }
  176. func_new_templ(vo, i, template_arg, NCDVal_NewInvalid(), 1);
  177. return;
  178. fail0:
  179. NCDModuleInst_Backend_SetError(i);
  180. NCDModuleInst_Backend_Dead(i);
  181. }
  182. static void func_new_call_if (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  183. {
  184. NCDValRef cond_arg;
  185. NCDValRef template_arg;
  186. NCDValRef args_arg;
  187. if (!NCDVal_ListRead(params->args, 3, &cond_arg, &template_arg, &args_arg)) {
  188. ModuleLog(i, BLOG_ERROR, "wrong arity");
  189. goto fail0;
  190. }
  191. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg)) {
  192. ModuleLog(i, BLOG_ERROR, "wrong type");
  193. goto fail0;
  194. }
  195. if (ncd_read_boolean(cond_arg)) {
  196. template_arg = NCDVal_NewInvalid();
  197. }
  198. func_new_templ(vo, i, template_arg, args_arg, 0);
  199. return;
  200. fail0:
  201. NCDModuleInst_Backend_SetError(i);
  202. NCDModuleInst_Backend_Dead(i);
  203. }
  204. static void func_new_embcall_if (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  205. {
  206. NCDValRef cond_arg;
  207. NCDValRef template_arg;
  208. if (!NCDVal_ListRead(params->args, 2, &cond_arg, &template_arg)) {
  209. ModuleLog(i, BLOG_ERROR, "wrong arity");
  210. goto fail0;
  211. }
  212. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg)) {
  213. ModuleLog(i, BLOG_ERROR, "wrong type");
  214. goto fail0;
  215. }
  216. if (ncd_read_boolean(cond_arg)) {
  217. template_arg = NCDVal_NewInvalid();
  218. }
  219. func_new_templ(vo, i, template_arg, NCDVal_NewInvalid(), 1);
  220. return;
  221. fail0:
  222. NCDModuleInst_Backend_SetError(i);
  223. NCDModuleInst_Backend_Dead(i);
  224. }
  225. static void func_new_call_ifelse (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  226. {
  227. NCDValRef cond_arg;
  228. NCDValRef template_arg;
  229. NCDValRef else_template_arg;
  230. NCDValRef args_arg;
  231. if (!NCDVal_ListRead(params->args, 4, &cond_arg, &template_arg, &else_template_arg, &args_arg)) {
  232. ModuleLog(i, BLOG_ERROR, "wrong arity");
  233. goto fail0;
  234. }
  235. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg) || !NCDVal_IsString(else_template_arg) || !NCDVal_IsList(args_arg)) {
  236. ModuleLog(i, BLOG_ERROR, "wrong type");
  237. goto fail0;
  238. }
  239. NCDValRef template_value;
  240. if (ncd_read_boolean(cond_arg)) {
  241. template_value = template_arg;
  242. } else {
  243. template_value = else_template_arg;
  244. }
  245. func_new_templ(vo, i, template_value, args_arg, 0);
  246. return;
  247. fail0:
  248. NCDModuleInst_Backend_SetError(i);
  249. NCDModuleInst_Backend_Dead(i);
  250. }
  251. static void func_new_embcall_ifelse (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  252. {
  253. NCDValRef cond_arg;
  254. NCDValRef template_arg;
  255. NCDValRef else_template_arg;
  256. if (!NCDVal_ListRead(params->args, 3, &cond_arg, &template_arg, &else_template_arg)) {
  257. ModuleLog(i, BLOG_ERROR, "wrong arity");
  258. goto fail0;
  259. }
  260. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg) || !NCDVal_IsString(else_template_arg)) {
  261. ModuleLog(i, BLOG_ERROR, "wrong type");
  262. goto fail0;
  263. }
  264. NCDValRef template_value;
  265. if (ncd_read_boolean(cond_arg)) {
  266. template_value = template_arg;
  267. } else {
  268. template_value = else_template_arg;
  269. }
  270. func_new_templ(vo, i, template_value, NCDVal_NewInvalid(), 1);
  271. return;
  272. fail0:
  273. NCDModuleInst_Backend_SetError(i);
  274. NCDModuleInst_Backend_Dead(i);
  275. }
  276. static void func_new_embcall_multif (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  277. {
  278. NCDValRef args = params->args;
  279. NCDValRef template_value = NCDVal_NewInvalid();
  280. size_t count = NCDVal_ListCount(args);
  281. size_t j = 0;
  282. while (j < count) {
  283. NCDValRef arg = NCDVal_ListGet(args, j);
  284. if (j == count - 1) {
  285. if (!NCDVal_IsString(arg)) {
  286. ModuleLog(i, BLOG_ERROR, "bad arguments");
  287. goto fail0;
  288. }
  289. template_value = arg;
  290. break;
  291. }
  292. NCDValRef arg2 = NCDVal_ListGet(args, j + 1);
  293. if (!NCDVal_IsString(arg) || !NCDVal_IsString(arg2)) {
  294. ModuleLog(i, BLOG_ERROR, "bad arguments");
  295. goto fail0;
  296. }
  297. if (ncd_read_boolean(arg)) {
  298. template_value = arg2;
  299. break;
  300. }
  301. j += 2;
  302. }
  303. func_new_templ(vo, i, template_value, NCDVal_NewInvalid(), 1);
  304. return;
  305. fail0:
  306. NCDModuleInst_Backend_SetError(i);
  307. NCDModuleInst_Backend_Dead(i);
  308. }
  309. static void func_die (void *vo)
  310. {
  311. struct instance *o = vo;
  312. ASSERT(o->state != STATE_TERMINATING)
  313. // if none, die now
  314. if (o->state == STATE_NONE) {
  315. instance_free(o);
  316. return;
  317. }
  318. // request process to terminate
  319. NCDModuleProcess_Terminate(&o->process);
  320. // set state terminating
  321. o->state = STATE_TERMINATING;
  322. }
  323. static void func_clean (void *vo)
  324. {
  325. struct instance *o = vo;
  326. if (o->state != STATE_WAITING) {
  327. return;
  328. }
  329. // allow process to continue
  330. NCDModuleProcess_Continue(&o->process);
  331. // set state working
  332. o->state = STATE_WORKING;
  333. }
  334. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  335. {
  336. struct instance *o = vo;
  337. if (o->state == STATE_NONE) {
  338. return 0;
  339. }
  340. return NCDModuleProcess_GetObj(&o->process, name, out_object);
  341. }
  342. static struct NCDModule modules[] = {
  343. {
  344. .type = "call2",
  345. .func_new2 = func_new_call,
  346. .func_die = func_die,
  347. .func_clean = func_clean,
  348. .func_getobj = func_getobj,
  349. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  350. .alloc_size = sizeof(struct instance)
  351. }, {
  352. .type = "call2_if",
  353. .func_new2 = func_new_call_if,
  354. .func_die = func_die,
  355. .func_clean = func_clean,
  356. .func_getobj = func_getobj,
  357. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  358. .alloc_size = sizeof(struct instance)
  359. }, {
  360. .type = "call2_ifelse",
  361. .func_new2 = func_new_call_ifelse,
  362. .func_die = func_die,
  363. .func_clean = func_clean,
  364. .func_getobj = func_getobj,
  365. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  366. .alloc_size = sizeof(struct instance)
  367. }, {
  368. .type = "embcall2",
  369. .func_new2 = func_new_embcall,
  370. .func_die = func_die,
  371. .func_clean = func_clean,
  372. .func_getobj = func_getobj,
  373. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  374. .alloc_size = sizeof(struct instance)
  375. }, {
  376. .type = "embcall2_if",
  377. .func_new2 = func_new_embcall_if,
  378. .func_die = func_die,
  379. .func_clean = func_clean,
  380. .func_getobj = func_getobj,
  381. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  382. .alloc_size = sizeof(struct instance)
  383. }, {
  384. .type = "embcall2_ifelse",
  385. .func_new2 = func_new_embcall_ifelse,
  386. .func_die = func_die,
  387. .func_clean = func_clean,
  388. .func_getobj = func_getobj,
  389. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  390. .alloc_size = sizeof(struct instance)
  391. }, {
  392. .type = "embcall2_multif",
  393. .func_new2 = func_new_embcall_multif,
  394. .func_die = func_die,
  395. .func_clean = func_clean,
  396. .func_getobj = func_getobj,
  397. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  398. .alloc_size = sizeof(struct instance)
  399. }, {
  400. .type = NULL
  401. }
  402. };
  403. const struct NCDModuleGroup ncdmodule_call2 = {
  404. .modules = modules,
  405. .strings = strings
  406. };