iaction.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <faux/error.h>
  9. #include <klish/khelper.h>
  10. #include <klish/kaction.h>
  11. #include <klish/iaction.h>
  12. #define TAG "ACTION"
  13. bool_t iaction_parse(const iaction_t *info, kaction_t *action, faux_error_t *error)
  14. {
  15. bool_t retcode = BOOL_TRUE;
  16. if (!info)
  17. return BOOL_FALSE;
  18. if (!action)
  19. return BOOL_FALSE;
  20. // Sym
  21. if (!faux_str_is_empty(info->sym)) {
  22. if (!kaction_set_sym_ref(action, info->sym)) {
  23. faux_error_add(error, TAG": Illegal 'sym' attribute");
  24. retcode = BOOL_FALSE;
  25. }
  26. }
  27. // Lock
  28. if (!faux_str_is_empty(info->lock)) {
  29. if (!kaction_set_lock(action, info->lock)) {
  30. faux_error_add(error, TAG": Illegal 'lock' attribute");
  31. retcode = BOOL_FALSE;
  32. }
  33. }
  34. // Interrupt
  35. if (!faux_str_is_empty(info->interrupt)) {
  36. bool_t b = BOOL_FALSE;
  37. if (!faux_conv_str2bool(info->interrupt, &b) ||
  38. !kaction_set_interrupt(action, b)) {
  39. faux_error_add(error, TAG": Illegal 'interrupt' attribute");
  40. retcode = BOOL_FALSE;
  41. }
  42. }
  43. // Interactive
  44. if (!faux_str_is_empty(info->interactive)) {
  45. bool_t b = BOOL_FALSE;
  46. if (!faux_conv_str2bool(info->interactive, &b) ||
  47. !kaction_set_interactive(action, b)) {
  48. faux_error_add(error, TAG": Illegal 'interactive' attribute");
  49. retcode = BOOL_FALSE;
  50. }
  51. }
  52. // Exec_on
  53. if (!faux_str_is_empty(info->exec_on)) {
  54. kaction_cond_e c = KACTION_COND_NONE;
  55. if (!faux_str_casecmp(info->exec_on, "fail"))
  56. c = KACTION_COND_FAIL;
  57. else if (!faux_str_casecmp(info->exec_on, "success"))
  58. c = KACTION_COND_SUCCESS;
  59. else if (!faux_str_casecmp(info->exec_on, "always"))
  60. c = KACTION_COND_ALWAYS;
  61. else if (!faux_str_casecmp(info->exec_on, "never"))
  62. c = KACTION_COND_NEVER;
  63. if ((KACTION_COND_NONE == c) || !kaction_set_exec_on(action, c)) {
  64. faux_error_add(error, TAG": Illegal 'exec_on' attribute");
  65. retcode = BOOL_FALSE;
  66. }
  67. }
  68. // Update_retcode
  69. if (!faux_str_is_empty(info->update_retcode)) {
  70. bool_t b = BOOL_FALSE;
  71. if (!faux_conv_str2bool(info->update_retcode, &b) ||
  72. !kaction_set_update_retcode(action, b)) {
  73. faux_error_add(error, TAG": Illegal 'update_retcode' attribute");
  74. retcode = BOOL_FALSE;
  75. }
  76. }
  77. // Permanent
  78. if (!faux_str_is_empty(info->permanent)) {
  79. tri_t b = TRI_UNDEFINED;
  80. if (!faux_conv_str2tri(info->permanent, &b) ||
  81. !kaction_set_permanent(action, b)) {
  82. faux_error_add(error, TAG": Illegal 'permanent' attribute");
  83. retcode = BOOL_FALSE;
  84. }
  85. }
  86. // Sync
  87. if (!faux_str_is_empty(info->sync)) {
  88. tri_t b = TRI_UNDEFINED;
  89. if (!faux_conv_str2tri(info->sync, &b) ||
  90. !kaction_set_sync(action, b)) {
  91. faux_error_add(error, TAG": Illegal 'sync' attribute");
  92. retcode = BOOL_FALSE;
  93. }
  94. }
  95. // Script
  96. if (!faux_str_is_empty(info->script)) {
  97. if (!kaction_set_script(action, info->script)) {
  98. faux_error_add(error, TAG": Illegal 'script' attribute");
  99. retcode = BOOL_FALSE;
  100. }
  101. }
  102. return retcode;
  103. }
  104. kaction_t *iaction_load(const iaction_t *iaction, faux_error_t *error)
  105. {
  106. kaction_t *kaction = NULL;
  107. kaction = kaction_new();
  108. if (!kaction) {
  109. faux_error_add(error, TAG": Can't create object");
  110. return NULL;
  111. }
  112. if (!iaction_parse(iaction, kaction, error)) {
  113. kaction_free(kaction);
  114. return NULL;
  115. }
  116. return kaction;
  117. }
  118. char *iaction_deploy(const kaction_t *kaction, int level)
  119. {
  120. char *str = NULL;
  121. char *tmp = NULL;
  122. char *exec_on = NULL;
  123. if (!kaction)
  124. return NULL;
  125. tmp = faux_str_sprintf("%*cACTION {\n", level, ' ');
  126. faux_str_cat(&str, tmp);
  127. faux_str_free(tmp);
  128. attr2ctext(&str, "sym", kaction_sym_ref(kaction), level + 1);
  129. attr2ctext(&str, "lock", kaction_lock(kaction), level + 1);
  130. attr2ctext(&str, "interrupt", faux_conv_bool2str(kaction_interrupt(kaction)), level + 1);
  131. attr2ctext(&str, "interactive", faux_conv_bool2str(kaction_interactive(kaction)), level + 1);
  132. // Exec_on
  133. switch (kaction_exec_on(kaction)) {
  134. case KACTION_COND_FAIL:
  135. exec_on = "fail";
  136. break;
  137. case KACTION_COND_SUCCESS:
  138. exec_on = "success";
  139. break;
  140. case KACTION_COND_ALWAYS:
  141. exec_on = "always";
  142. break;
  143. case KACTION_COND_NEVER:
  144. exec_on = "never";
  145. break;
  146. default:
  147. exec_on = NULL;
  148. }
  149. attr2ctext(&str, "exec_on", exec_on, level + 1);
  150. attr2ctext(&str, "update_retcode", faux_conv_bool2str(kaction_update_retcode(kaction)), level + 1);
  151. attr2ctext(&str, "permanent", faux_conv_tri2str(kaction_permanent(kaction)), level + 1);
  152. attr2ctext(&str, "sync", faux_conv_tri2str(kaction_sync(kaction)), level + 1);
  153. attr2ctext(&str, "script", kaction_script(kaction), level + 1);
  154. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  155. faux_str_cat(&str, tmp);
  156. faux_str_free(tmp);
  157. return str;
  158. }