kaction.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_is_permanent(const kaction_t *action)
  84. {
  85. ksym_t *sym = NULL;
  86. tri_t val = TRI_UNDEFINED;
  87. assert(action);
  88. if (!action)
  89. return BOOL_FALSE;
  90. sym = kaction_sym(action);
  91. if (!sym)
  92. return BOOL_FALSE;
  93. val = ksym_permanent(sym);
  94. if (TRI_UNDEFINED == val)
  95. val = kaction_permanent(action);
  96. if (TRI_TRUE == val)
  97. return BOOL_TRUE;
  98. return BOOL_FALSE; // Default if not set
  99. }
  100. bool_t kaction_is_sync(const kaction_t *action)
  101. {
  102. ksym_t *sym = NULL;
  103. tri_t val = TRI_UNDEFINED;
  104. assert(action);
  105. if (!action)
  106. return BOOL_FALSE;
  107. sym = kaction_sym(action);
  108. if (!sym)
  109. return BOOL_FALSE;
  110. val = ksym_sync(sym);
  111. if (TRI_UNDEFINED == val)
  112. val = kaction_sync(action);
  113. if (TRI_TRUE == val)
  114. return BOOL_TRUE;
  115. return BOOL_FALSE; // Default if not set
  116. }