kaction.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. struct kaction_s {
  11. char *sym_ref; // Text reference to symbol
  12. char *lock; // Named lock
  13. bool_t interrupt;
  14. bool_t interactive;
  15. kaction_cond_e exec_on;
  16. bool_t update_retcode;
  17. //ksym_t *sym; // Symbol
  18. };
  19. // Simple methods
  20. // Sym reference (must be resolved later)
  21. KGET_STR(action, sym_ref);
  22. KSET_STR_ONCE(action, sym_ref);
  23. // Lock
  24. KGET_STR(action, lock);
  25. KSET_STR(action, lock);
  26. // Interrupt
  27. KGET_BOOL(action, interrupt);
  28. KSET_BOOL(action, interrupt);
  29. // Interactive
  30. KGET_BOOL(action, interactive);
  31. KSET_BOOL(action, interactive);
  32. // Exec_on
  33. KGET(action, kaction_cond_e, exec_on);
  34. KSET(action, kaction_cond_e, exec_on);
  35. // Update_retcode
  36. KGET_BOOL(action, update_retcode);
  37. KSET_BOOL(action, update_retcode);
  38. static kaction_t *kaction_new_empty(void)
  39. {
  40. kaction_t *action = NULL;
  41. action = faux_zmalloc(sizeof(*action));
  42. assert(action);
  43. if (!action)
  44. return NULL;
  45. // Initialize
  46. action->sym_ref = NULL;
  47. action->lock = NULL;
  48. action->interrupt = BOOL_FALSE;
  49. action->interactive = BOOL_FALSE;
  50. action->exec_on = KACTION_COND_SUCCESS;
  51. action->update_retcode = BOOL_TRUE;
  52. return action;
  53. }
  54. kaction_t *kaction_new(const iaction_t *info, kaction_error_e *error)
  55. {
  56. kaction_t *action = NULL;
  57. action = kaction_new_empty();
  58. assert(action);
  59. if (!action) {
  60. if (error)
  61. *error = KACTION_ERROR_ALLOC;
  62. return NULL;
  63. }
  64. if (!info)
  65. return action;
  66. if (!kaction_parse(action, info, error)) {
  67. kaction_free(action);
  68. return NULL;
  69. }
  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_free(action);
  79. }
  80. const char *kaction_strerror(kaction_error_e error)
  81. {
  82. const char *str = NULL;
  83. switch (error) {
  84. case KACTION_ERROR_OK:
  85. str = "Ok";
  86. break;
  87. case KACTION_ERROR_INTERNAL:
  88. str = "Internal error";
  89. break;
  90. case KACTION_ERROR_ALLOC:
  91. str = "Memory allocation error";
  92. break;
  93. case KACTION_ERROR_ATTR_SYM:
  94. str = "Illegal 'sym' attribute";
  95. break;
  96. case KACTION_ERROR_ATTR_LOCK:
  97. str = "Illegal 'lock' attribute";
  98. break;
  99. case KACTION_ERROR_ATTR_INTERRUPT:
  100. str = "Illegal 'interrupt' attribute";
  101. break;
  102. case KACTION_ERROR_ATTR_INTERACTIVE:
  103. str = "Illegal 'interactive' attribute";
  104. break;
  105. case KACTION_ERROR_ATTR_UPDATE_RETCODE:
  106. str = "Illegal 'update_retcode' attribute";
  107. break;
  108. default:
  109. str = "Unknown error";
  110. break;
  111. }
  112. return str;
  113. }
  114. bool_t kaction_parse(kaction_t *action, const iaction_t *info, kaction_error_e *error)
  115. {
  116. // Sym
  117. if (!faux_str_is_empty(info->sym)) {
  118. if (!kaction_set_sym_ref(action, info->sym)) {
  119. if (error)
  120. *error = KACTION_ERROR_ATTR_SYM;
  121. return BOOL_FALSE;
  122. }
  123. }
  124. // Lock
  125. if (!faux_str_is_empty(info->lock)) {
  126. if (!kaction_set_lock(action, info->lock)) {
  127. if (error)
  128. *error = KACTION_ERROR_ATTR_LOCK;
  129. return BOOL_FALSE;
  130. }
  131. }
  132. // Interrupt
  133. if (!faux_str_is_empty(info->interrupt)) {
  134. bool_t b = BOOL_FALSE;
  135. if (!faux_conv_str2bool(info->interrupt, &b) ||
  136. !kaction_set_interrupt(action, b)) {
  137. if (error)
  138. *error = KACTION_ERROR_ATTR_INTERRUPT;
  139. return BOOL_FALSE;
  140. }
  141. }
  142. // Interactive
  143. if (!faux_str_is_empty(info->interactive)) {
  144. bool_t b = BOOL_FALSE;
  145. if (!faux_conv_str2bool(info->interactive, &b) ||
  146. !kaction_set_interactive(action, b)) {
  147. if (error)
  148. *error = KACTION_ERROR_ATTR_INTERACTIVE;
  149. return BOOL_FALSE;
  150. }
  151. }
  152. // Exec_on
  153. if (!faux_str_is_empty(info->exec_on)) {
  154. kaction_cond_e c = KACTION_COND_SUCCESS;
  155. if (faux_str_casecmp(info->exec_on, "fail"))
  156. c = KACTION_COND_FAIL;
  157. else if (faux_str_casecmp(info->exec_on, "success"))
  158. c = KACTION_COND_SUCCESS;
  159. else if (faux_str_casecmp(info->exec_on, "always"))
  160. c = KACTION_COND_ALWAYS;
  161. else {
  162. if (error)
  163. *error = KACTION_ERROR_ATTR_EXEC_ON;
  164. return BOOL_FALSE;
  165. }
  166. if (!kaction_set_exec_on(action, c)) {
  167. if (error)
  168. *error = KACTION_ERROR_ATTR_EXEC_ON;
  169. return BOOL_FALSE;
  170. }
  171. }
  172. // Update_retcode
  173. if (!faux_str_is_empty(info->update_retcode)) {
  174. bool_t b = BOOL_FALSE;
  175. if (!faux_conv_str2bool(info->update_retcode, &b) ||
  176. !kaction_set_update_retcode(action, b)) {
  177. if (error)
  178. *error = KACTION_ERROR_ATTR_UPDATE_RETCODE;
  179. return BOOL_FALSE;
  180. }
  181. }
  182. return BOOL_TRUE;
  183. }
  184. kaction_t *kaction_from_iaction(iaction_t *iaction, faux_error_t *error_stack)
  185. {
  186. kaction_t *kaction = NULL;
  187. kaction_error_e kaction_error = KACTION_ERROR_OK;
  188. kaction = kaction_new(iaction, &kaction_error);
  189. if (!kaction) {
  190. char *msg = NULL;
  191. msg = faux_str_sprintf("ACTION : %s",
  192. kaction_strerror(kaction_error));
  193. faux_error_add(error_stack, msg);
  194. faux_str_free(msg);
  195. return NULL;
  196. }
  197. printf("action\n");
  198. return kaction;
  199. }