kaction.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. bool_t interactive;
  19. kaction_cond_e exec_on;
  20. bool_t update_retcode;
  21. tri_t permanent;
  22. tri_t sync;
  23. char *script;
  24. };
  25. // Simple methods
  26. // Sym reference (must be resolved later)
  27. KGET_STR(action, sym_ref);
  28. KSET_STR_ONCE(action, sym_ref);
  29. // Lock
  30. KGET_STR(action, lock);
  31. KSET_STR(action, lock);
  32. // Interrupt
  33. KGET_BOOL(action, interrupt);
  34. KSET_BOOL(action, interrupt);
  35. // Interactive
  36. KGET_BOOL(action, interactive);
  37. KSET_BOOL(action, interactive);
  38. // Exec_on
  39. KGET(action, kaction_cond_e, exec_on);
  40. KSET(action, kaction_cond_e, exec_on);
  41. // Update_retcode
  42. KGET_BOOL(action, update_retcode);
  43. KSET_BOOL(action, update_retcode);
  44. // Permanent
  45. KGET(action, tri_t, permanent);
  46. KSET(action, tri_t, permanent);
  47. // Sync
  48. KGET(action, tri_t, sync);
  49. KSET(action, tri_t, sync);
  50. // Script
  51. KGET_STR(action, script);
  52. KSET_STR(action, script);
  53. // Symbol
  54. KGET(action, ksym_t *, sym);
  55. KSET(action, ksym_t *, sym);
  56. // Plugin. Source of sym
  57. KGET(action, kplugin_t *, plugin);
  58. KSET(action, kplugin_t *, plugin);
  59. kaction_t *kaction_new(void)
  60. {
  61. kaction_t *action = NULL;
  62. action = faux_zmalloc(sizeof(*action));
  63. assert(action);
  64. if (!action)
  65. return NULL;
  66. // Initialize
  67. action->sym_ref = NULL;
  68. action->lock = NULL;
  69. action->interrupt = BOOL_FALSE;
  70. action->interactive = BOOL_FALSE;
  71. action->exec_on = KACTION_COND_SUCCESS;
  72. action->update_retcode = BOOL_TRUE;
  73. action->script = NULL;
  74. action->sym = NULL;
  75. action->plugin = NULL;
  76. return action;
  77. }
  78. void kaction_free(kaction_t *action)
  79. {
  80. if (!action)
  81. return;
  82. faux_str_free(action->sym_ref);
  83. faux_str_free(action->lock);
  84. faux_str_free(action->script);
  85. faux_free(action);
  86. }
  87. bool_t kaction_meet_exec_conditions(const kaction_t *action, int current_retcode)
  88. {
  89. bool_t r = BOOL_FALSE; // Default is pessimistic
  90. assert(action);
  91. if (!action)
  92. return BOOL_FALSE;
  93. switch (kaction_exec_on(action)) {
  94. case KACTION_COND_ALWAYS:
  95. r = BOOL_TRUE;
  96. break;
  97. case KACTION_COND_SUCCESS:
  98. if (0 == current_retcode)
  99. r = BOOL_TRUE;
  100. break;
  101. case KACTION_COND_FAIL:
  102. if (current_retcode != 0)
  103. r = BOOL_TRUE;
  104. break;
  105. default:
  106. r = BOOL_FALSE; // NEVER or NONE
  107. }
  108. return r;
  109. }
  110. bool_t kaction_is_permanent(const kaction_t *action)
  111. {
  112. ksym_t *sym = NULL;
  113. tri_t val = TRI_UNDEFINED;
  114. assert(action);
  115. if (!action)
  116. return BOOL_FALSE;
  117. sym = kaction_sym(action);
  118. if (!sym)
  119. return BOOL_FALSE;
  120. val = ksym_permanent(sym);
  121. if (TRI_UNDEFINED == val)
  122. val = kaction_permanent(action);
  123. if (TRI_TRUE == val)
  124. return BOOL_TRUE;
  125. return BOOL_FALSE; // Default if not set
  126. }
  127. bool_t kaction_is_sync(const kaction_t *action)
  128. {
  129. ksym_t *sym = NULL;
  130. tri_t val = TRI_UNDEFINED;
  131. assert(action);
  132. if (!action)
  133. return BOOL_FALSE;
  134. sym = kaction_sym(action);
  135. if (!sym)
  136. return BOOL_FALSE;
  137. val = ksym_sync(sym);
  138. if (TRI_UNDEFINED == val)
  139. val = kaction_sync(action);
  140. if (TRI_TRUE == val)
  141. return BOOL_TRUE;
  142. return BOOL_FALSE; // Default if not set
  143. }