kaction.c 3.1 KB

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