iaction.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // Script
  76. if (!faux_str_is_empty(info->script)) {
  77. if (!kaction_set_script(action, info->script)) {
  78. faux_error_add(error, TAG": Illegal 'script' attribute");
  79. retcode = BOOL_FALSE;
  80. }
  81. }
  82. return retcode;
  83. }
  84. kaction_t *iaction_load(const iaction_t *iaction, faux_error_t *error)
  85. {
  86. kaction_t *kaction = NULL;
  87. kaction = kaction_new();
  88. if (!kaction) {
  89. faux_error_add(error, TAG": Can't create object");
  90. return NULL;
  91. }
  92. if (!iaction_parse(iaction, kaction, error)) {
  93. kaction_free(kaction);
  94. return NULL;
  95. }
  96. return kaction;
  97. }
  98. char *iaction_deploy(const kaction_t *kaction, int level)
  99. {
  100. char *str = NULL;
  101. char *tmp = NULL;
  102. char *exec_on = NULL;
  103. if (!kaction)
  104. return NULL;
  105. tmp = faux_str_sprintf("%*cACTION {\n", level, ' ');
  106. faux_str_cat(&str, tmp);
  107. faux_str_free(tmp);
  108. attr2ctext(&str, "sym", kaction_sym_ref(kaction), level + 1);
  109. attr2ctext(&str, "lock", kaction_lock(kaction), level + 1);
  110. attr2ctext(&str, "interrupt", faux_conv_bool2str(kaction_interrupt(kaction)), level + 1);
  111. attr2ctext(&str, "interactive", faux_conv_bool2str(kaction_interactive(kaction)), level + 1);
  112. // Exec_on
  113. switch (kaction_exec_on(kaction)) {
  114. case KACTION_COND_FAIL:
  115. exec_on = "fail";
  116. break;
  117. case KACTION_COND_SUCCESS:
  118. exec_on = "success";
  119. break;
  120. case KACTION_COND_ALWAYS:
  121. exec_on = "always";
  122. break;
  123. default:
  124. exec_on = NULL;
  125. }
  126. attr2ctext(&str, "exec_on", exec_on, level + 1);
  127. attr2ctext(&str, "update_retcode", faux_conv_bool2str(kaction_update_retcode(kaction)), level + 1);
  128. attr2ctext(&str, "script", kaction_script(kaction), level + 1);
  129. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  130. faux_str_cat(&str, tmp);
  131. faux_str_free(tmp);
  132. return str;
  133. }