call2.c 14 KB

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