kaction.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/conv.h>
  7. #include <faux/list.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kaction.h>
  10. #include <klish/ksym.h>
  11. #include <klish/kplugin.h>
  12. struct kaction_s {
  13. char *sym_ref; // Text reference to symbol
  14. ksym_t *sym; // Symbol itself
  15. kplugin_t *plugin; // Source of symbol
  16. char *lock; // Named lock
  17. bool_t interrupt;
  18. kaction_io_e in;
  19. kaction_io_e out;
  20. kaction_cond_e exec_on;
  21. bool_t update_retcode;
  22. tri_t permanent;
  23. tri_t sync;
  24. char *script;
  25. };
  26. // Simple methods
  27. // Sym reference (must be resolved later)
  28. KGET_STR(action, sym_ref);
  29. KSET_STR_ONCE(action, sym_ref);
  30. // Lock
  31. KGET_STR(action, lock);
  32. KSET_STR(action, lock);
  33. // Interrupt
  34. KGET_BOOL(action, interrupt);
  35. KSET_BOOL(action, interrupt);
  36. // In/Out
  37. KGET(action, kaction_io_e, in);
  38. KSET(action, kaction_io_e, in);
  39. KGET(action, kaction_io_e, out);
  40. KSET(action, kaction_io_e, out);
  41. // Exec_on
  42. KGET(action, kaction_cond_e, exec_on);
  43. KSET(action, kaction_cond_e, exec_on);
  44. // Update_retcode
  45. KGET_BOOL(action, update_retcode);
  46. KSET_BOOL(action, update_retcode);
  47. // Permanent
  48. KGET(action, tri_t, permanent);
  49. KSET(action, tri_t, permanent);
  50. // Sync
  51. KGET(action, tri_t, sync);
  52. KSET(action, tri_t, sync);
  53. // Script
  54. KGET_STR(action, script);
  55. KSET_STR(action, script);
  56. // Symbol
  57. KGET(action, ksym_t *, sym);
  58. KSET(action, ksym_t *, sym);
  59. // Plugin. Source of sym
  60. KGET(action, kplugin_t *, plugin);
  61. KSET(action, kplugin_t *, plugin);
  62. kaction_t *kaction_new(void)
  63. {
  64. kaction_t *action = NULL;
  65. action = faux_zmalloc(sizeof(*action));
  66. assert(action);
  67. if (!action)
  68. return NULL;
  69. // Initialize
  70. action->sym_ref = NULL;
  71. action->lock = NULL;
  72. action->interrupt = BOOL_FALSE;
  73. action->in = KACTION_IO_FALSE;
  74. action->out = KACTION_IO_TRUE;
  75. action->exec_on = KACTION_COND_SUCCESS;
  76. action->update_retcode = BOOL_TRUE;
  77. action->script = NULL;
  78. action->sym = NULL;
  79. action->plugin = NULL;
  80. return action;
  81. }
  82. void kaction_free(kaction_t *action)
  83. {
  84. if (!action)
  85. return;
  86. faux_str_free(action->sym_ref);
  87. faux_str_free(action->lock);
  88. faux_str_free(action->script);
  89. faux_free(action);
  90. }
  91. bool_t kaction_meet_exec_conditions(const kaction_t *action, int current_retcode)
  92. {
  93. bool_t r = BOOL_FALSE; // Default is pessimistic
  94. assert(action);
  95. if (!action)
  96. return BOOL_FALSE;
  97. switch (kaction_exec_on(action)) {
  98. case KACTION_COND_ALWAYS:
  99. r = BOOL_TRUE;
  100. break;
  101. case KACTION_COND_SUCCESS:
  102. if (0 == current_retcode)
  103. r = BOOL_TRUE;
  104. break;
  105. case KACTION_COND_FAIL:
  106. if (current_retcode != 0)
  107. r = BOOL_TRUE;
  108. break;
  109. default:
  110. r = BOOL_FALSE; // NEVER or NONE
  111. }
  112. return r;
  113. }
  114. bool_t kaction_is_permanent(const kaction_t *action)
  115. {
  116. ksym_t *sym = NULL;
  117. tri_t val = TRI_UNDEFINED;
  118. assert(action);
  119. if (!action)
  120. return BOOL_FALSE;
  121. sym = kaction_sym(action);
  122. if (!sym)
  123. return BOOL_FALSE;
  124. val = ksym_permanent(sym);
  125. if (TRI_UNDEFINED == val)
  126. val = kaction_permanent(action);
  127. if (TRI_TRUE == val)
  128. return BOOL_TRUE;
  129. return BOOL_FALSE; // Default if not set
  130. }
  131. bool_t kaction_is_sync(const kaction_t *action)
  132. {
  133. ksym_t *sym = NULL;
  134. tri_t val = TRI_UNDEFINED;
  135. assert(action);
  136. if (!action)
  137. return BOOL_FALSE;
  138. sym = kaction_sym(action);
  139. if (!sym)
  140. return BOOL_FALSE;
  141. val = ksym_sync(sym);
  142. if (TRI_UNDEFINED == val)
  143. val = kaction_sync(action);
  144. if (TRI_TRUE == val)
  145. return BOOL_TRUE;
  146. return BOOL_FALSE; // Default if not set
  147. }