iaction.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if ((KACTION_COND_NONE == c) || !kaction_set_exec_on(action, c)) {
  62. faux_error_add(error, TAG": Illegal 'exec_on' attribute");
  63. retcode = BOOL_FALSE;
  64. }
  65. }
  66. // Update_retcode
  67. if (!faux_str_is_empty(info->update_retcode)) {
  68. bool_t b = BOOL_FALSE;
  69. if (!faux_conv_str2bool(info->update_retcode, &b) ||
  70. !kaction_set_update_retcode(action, b)) {
  71. faux_error_add(error, TAG": Illegal 'update_retcode' attribute");
  72. retcode = BOOL_FALSE;
  73. }
  74. }
  75. // Permanent
  76. if (!faux_str_is_empty(info->permanent)) {
  77. tri_t b = TRI_UNDEFINED;
  78. if (!faux_conv_str2tri(info->permanent, &b) ||
  79. !kaction_set_permanent(action, b)) {
  80. faux_error_add(error, TAG": Illegal 'permanent' attribute");
  81. retcode = BOOL_FALSE;
  82. }
  83. }
  84. // Sync
  85. if (!faux_str_is_empty(info->sync)) {
  86. tri_t b = TRI_UNDEFINED;
  87. if (!faux_conv_str2tri(info->sync, &b) ||
  88. !kaction_set_sync(action, b)) {
  89. faux_error_add(error, TAG": Illegal 'sync' attribute");
  90. retcode = BOOL_FALSE;
  91. }
  92. }
  93. // Script
  94. if (!faux_str_is_empty(info->script)) {
  95. if (!kaction_set_script(action, info->script)) {
  96. faux_error_add(error, TAG": Illegal 'script' attribute");
  97. retcode = BOOL_FALSE;
  98. }
  99. }
  100. return retcode;
  101. }
  102. kaction_t *iaction_load(const iaction_t *iaction, faux_error_t *error)
  103. {
  104. kaction_t *kaction = NULL;
  105. kaction = kaction_new();
  106. if (!kaction) {
  107. faux_error_add(error, TAG": Can't create object");
  108. return NULL;
  109. }
  110. if (!iaction_parse(iaction, kaction, error)) {
  111. kaction_free(kaction);
  112. return NULL;
  113. }
  114. return kaction;
  115. }
  116. char *iaction_deploy(const kaction_t *kaction, int level)
  117. {
  118. char *str = NULL;
  119. char *tmp = NULL;
  120. char *exec_on = NULL;
  121. if (!kaction)
  122. return NULL;
  123. tmp = faux_str_sprintf("%*cACTION {\n", level, ' ');
  124. faux_str_cat(&str, tmp);
  125. faux_str_free(tmp);
  126. attr2ctext(&str, "sym", kaction_sym_ref(kaction), level + 1);
  127. attr2ctext(&str, "lock", kaction_lock(kaction), level + 1);
  128. attr2ctext(&str, "interrupt", faux_conv_bool2str(kaction_interrupt(kaction)), level + 1);
  129. attr2ctext(&str, "interactive", faux_conv_bool2str(kaction_interactive(kaction)), level + 1);
  130. // Exec_on
  131. switch (kaction_exec_on(kaction)) {
  132. case KACTION_COND_FAIL:
  133. exec_on = "fail";
  134. break;
  135. case KACTION_COND_SUCCESS:
  136. exec_on = "success";
  137. break;
  138. case KACTION_COND_ALWAYS:
  139. exec_on = "always";
  140. break;
  141. default:
  142. exec_on = NULL;
  143. }
  144. attr2ctext(&str, "exec_on", exec_on, level + 1);
  145. attr2ctext(&str, "update_retcode", faux_conv_bool2str(kaction_update_retcode(kaction)), level + 1);
  146. attr2ctext(&str, "permanent", faux_conv_tri2str(kaction_permanent(kaction)), level + 1);
  147. attr2ctext(&str, "sync", faux_conv_tri2str(kaction_sync(kaction)), level + 1);
  148. attr2ctext(&str, "script", kaction_script(kaction), level + 1);
  149. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  150. faux_str_cat(&str, tmp);
  151. faux_str_free(tmp);
  152. return str;
  153. }