call2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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_DeadError(i);
  141. }
  142. static void instance_free (struct instance *o)
  143. {
  144. // free process
  145. if (o->state != STATE_NONE) {
  146. NCDModuleProcess_Free(&o->process);
  147. }
  148. NCDModuleInst_Backend_Dead(o->i);
  149. }
  150. static void func_new_call (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  151. {
  152. NCDValRef template_arg;
  153. NCDValRef args_arg;
  154. if (!NCDVal_ListRead(params->args, 2, &template_arg, &args_arg)) {
  155. ModuleLog(i, BLOG_ERROR, "wrong arity");
  156. goto fail0;
  157. }
  158. if (!NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg)) {
  159. ModuleLog(i, BLOG_ERROR, "wrong type");
  160. goto fail0;
  161. }
  162. func_new_templ(vo, i, template_arg, args_arg, 0);
  163. return;
  164. fail0:
  165. NCDModuleInst_Backend_DeadError(i);
  166. }
  167. static void func_new_embcall (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  168. {
  169. NCDValRef template_arg;
  170. if (!NCDVal_ListRead(params->args, 1, &template_arg)) {
  171. ModuleLog(i, BLOG_ERROR, "wrong arity");
  172. goto fail0;
  173. }
  174. if (!NCDVal_IsString(template_arg)) {
  175. ModuleLog(i, BLOG_ERROR, "wrong type");
  176. goto fail0;
  177. }
  178. func_new_templ(vo, i, template_arg, NCDVal_NewInvalid(), 1);
  179. return;
  180. fail0:
  181. NCDModuleInst_Backend_DeadError(i);
  182. }
  183. static void func_new_call_if (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  184. {
  185. NCDValRef cond_arg;
  186. NCDValRef template_arg;
  187. NCDValRef args_arg;
  188. if (!NCDVal_ListRead(params->args, 3, &cond_arg, &template_arg, &args_arg)) {
  189. ModuleLog(i, BLOG_ERROR, "wrong arity");
  190. goto fail0;
  191. }
  192. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg)) {
  193. ModuleLog(i, BLOG_ERROR, "wrong type");
  194. goto fail0;
  195. }
  196. if (ncd_read_boolean(cond_arg)) {
  197. template_arg = NCDVal_NewInvalid();
  198. }
  199. func_new_templ(vo, i, template_arg, args_arg, 0);
  200. return;
  201. fail0:
  202. NCDModuleInst_Backend_DeadError(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_DeadError(i);
  223. }
  224. static void func_new_call_ifelse (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  225. {
  226. NCDValRef cond_arg;
  227. NCDValRef template_arg;
  228. NCDValRef else_template_arg;
  229. NCDValRef args_arg;
  230. if (!NCDVal_ListRead(params->args, 4, &cond_arg, &template_arg, &else_template_arg, &args_arg)) {
  231. ModuleLog(i, BLOG_ERROR, "wrong arity");
  232. goto fail0;
  233. }
  234. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg) || !NCDVal_IsString(else_template_arg) || !NCDVal_IsList(args_arg)) {
  235. ModuleLog(i, BLOG_ERROR, "wrong type");
  236. goto fail0;
  237. }
  238. NCDValRef template_value;
  239. if (ncd_read_boolean(cond_arg)) {
  240. template_value = template_arg;
  241. } else {
  242. template_value = else_template_arg;
  243. }
  244. func_new_templ(vo, i, template_value, args_arg, 0);
  245. return;
  246. fail0:
  247. NCDModuleInst_Backend_DeadError(i);
  248. }
  249. static void func_new_embcall_ifelse (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  250. {
  251. NCDValRef cond_arg;
  252. NCDValRef template_arg;
  253. NCDValRef else_template_arg;
  254. if (!NCDVal_ListRead(params->args, 3, &cond_arg, &template_arg, &else_template_arg)) {
  255. ModuleLog(i, BLOG_ERROR, "wrong arity");
  256. goto fail0;
  257. }
  258. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg) || !NCDVal_IsString(else_template_arg)) {
  259. ModuleLog(i, BLOG_ERROR, "wrong type");
  260. goto fail0;
  261. }
  262. NCDValRef template_value;
  263. if (ncd_read_boolean(cond_arg)) {
  264. template_value = template_arg;
  265. } else {
  266. template_value = else_template_arg;
  267. }
  268. func_new_templ(vo, i, template_value, NCDVal_NewInvalid(), 1);
  269. return;
  270. fail0:
  271. NCDModuleInst_Backend_DeadError(i);
  272. }
  273. static void func_new_embcall_multif (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  274. {
  275. NCDValRef args = params->args;
  276. NCDValRef template_value = NCDVal_NewInvalid();
  277. size_t count = NCDVal_ListCount(args);
  278. size_t j = 0;
  279. while (j < count) {
  280. NCDValRef arg = NCDVal_ListGet(args, j);
  281. if (j == count - 1) {
  282. if (!NCDVal_IsString(arg)) {
  283. ModuleLog(i, BLOG_ERROR, "bad arguments");
  284. goto fail0;
  285. }
  286. template_value = arg;
  287. break;
  288. }
  289. NCDValRef arg2 = NCDVal_ListGet(args, j + 1);
  290. if (!NCDVal_IsString(arg) || !NCDVal_IsString(arg2)) {
  291. ModuleLog(i, BLOG_ERROR, "bad arguments");
  292. goto fail0;
  293. }
  294. if (ncd_read_boolean(arg)) {
  295. template_value = arg2;
  296. break;
  297. }
  298. j += 2;
  299. }
  300. func_new_templ(vo, i, template_value, NCDVal_NewInvalid(), 1);
  301. return;
  302. fail0:
  303. NCDModuleInst_Backend_DeadError(i);
  304. }
  305. static void func_die (void *vo)
  306. {
  307. struct instance *o = vo;
  308. ASSERT(o->state != STATE_TERMINATING)
  309. // if none, die now
  310. if (o->state == STATE_NONE) {
  311. instance_free(o);
  312. return;
  313. }
  314. // request process to terminate
  315. NCDModuleProcess_Terminate(&o->process);
  316. // set state terminating
  317. o->state = STATE_TERMINATING;
  318. }
  319. static void func_clean (void *vo)
  320. {
  321. struct instance *o = vo;
  322. if (o->state != STATE_WAITING) {
  323. return;
  324. }
  325. // allow process to continue
  326. NCDModuleProcess_Continue(&o->process);
  327. // set state working
  328. o->state = STATE_WORKING;
  329. }
  330. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  331. {
  332. struct instance *o = vo;
  333. if (o->state == STATE_NONE) {
  334. return 0;
  335. }
  336. return NCDModuleProcess_GetObj(&o->process, name, out_object);
  337. }
  338. static struct NCDModule modules[] = {
  339. {
  340. .type = "call2",
  341. .func_new2 = func_new_call,
  342. .func_die = func_die,
  343. .func_clean = func_clean,
  344. .func_getobj = func_getobj,
  345. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  346. .alloc_size = sizeof(struct instance)
  347. }, {
  348. .type = "call2_if",
  349. .func_new2 = func_new_call_if,
  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_ifelse",
  357. .func_new2 = func_new_call_ifelse,
  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 = "embcall2",
  365. .func_new2 = func_new_embcall,
  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_if",
  373. .func_new2 = func_new_embcall_if,
  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_ifelse",
  381. .func_new2 = func_new_embcall_ifelse,
  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_multif",
  389. .func_new2 = func_new_embcall_multif,
  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 = NULL
  397. }
  398. };
  399. const struct NCDModuleGroup ncdmodule_call2 = {
  400. .modules = modules,
  401. .strings = strings
  402. };