kaction.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. struct kaction_s {
  12. char *sym_ref; // Text reference to symbol
  13. ksym_t *sym; // Symbol itself
  14. char *lock; // Named lock
  15. bool_t interrupt;
  16. bool_t interactive;
  17. // bool_t permanent; // Should it be executed on 'dry-run'
  18. // bool_t async;
  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. kaction_t *kaction_new(void)
  57. {
  58. kaction_t *action = NULL;
  59. action = faux_zmalloc(sizeof(*action));
  60. assert(action);
  61. if (!action)
  62. return NULL;
  63. // Initialize
  64. action->sym_ref = NULL;
  65. action->lock = NULL;
  66. action->interrupt = BOOL_FALSE;
  67. action->interactive = BOOL_FALSE;
  68. action->exec_on = KACTION_COND_SUCCESS;
  69. action->update_retcode = BOOL_TRUE;
  70. action->script = NULL;
  71. action->sym = NULL;
  72. return action;
  73. }
  74. void kaction_free(kaction_t *action)
  75. {
  76. if (!action)
  77. return;
  78. faux_str_free(action->sym_ref);
  79. faux_str_free(action->lock);
  80. faux_str_free(action->script);
  81. faux_free(action);
  82. }
  83. bool_t kaction_meet_exec_conditions(const kaction_t *action, int current_retcode)
  84. {
  85. bool_t r = BOOL_FALSE; // Default is pessimistic
  86. assert(action);
  87. if (!action)
  88. return BOOL_FALSE;
  89. switch (kaction_exec_on(action)) {
  90. case KACTION_COND_ALWAYS:
  91. r = BOOL_TRUE;
  92. break;
  93. case KACTION_COND_SUCCESS:
  94. if (0 == current_retcode)
  95. r = BOOL_TRUE;
  96. break;
  97. case KACTION_COND_FAIL:
  98. if (current_retcode != 0)
  99. r = BOOL_TRUE;
  100. break;
  101. default:
  102. r = BOOL_FALSE; // NEVER or NONE
  103. }
  104. return r;
  105. }
  106. bool_t kaction_is_permanent(const kaction_t *action)
  107. {
  108. ksym_t *sym = NULL;
  109. tri_t val = TRI_UNDEFINED;
  110. assert(action);
  111. if (!action)
  112. return BOOL_FALSE;
  113. sym = kaction_sym(action);
  114. if (!sym)
  115. return BOOL_FALSE;
  116. val = ksym_permanent(sym);
  117. if (TRI_UNDEFINED == val)
  118. val = kaction_permanent(action);
  119. if (TRI_TRUE == val)
  120. return BOOL_TRUE;
  121. return BOOL_FALSE; // Default if not set
  122. }
  123. bool_t kaction_is_sync(const kaction_t *action)
  124. {
  125. ksym_t *sym = NULL;
  126. tri_t val = TRI_UNDEFINED;
  127. assert(action);
  128. if (!action)
  129. return BOOL_FALSE;
  130. sym = kaction_sym(action);
  131. if (!sym)
  132. return BOOL_FALSE;
  133. val = ksym_sync(sym);
  134. if (TRI_UNDEFINED == val)
  135. val = kaction_sync(action);
  136. if (TRI_TRUE == val)
  137. return BOOL_TRUE;
  138. return BOOL_FALSE; // Default if not set
  139. }