kaction_parse.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #define TAG "ACTION"
  12. bool_t kaction_parse(kaction_t *action, const iaction_t *info, faux_error_t *error)
  13. {
  14. bool_t retcode = BOOL_TRUE;
  15. // Sym
  16. if (!faux_str_is_empty(info->sym)) {
  17. if (!kaction_set_sym_ref(action, info->sym)) {
  18. faux_error_add(error, TAG": Illegal 'sym' attribute");
  19. retcode = BOOL_FALSE;
  20. }
  21. }
  22. // Lock
  23. if (!faux_str_is_empty(info->lock)) {
  24. if (!kaction_set_lock(action, info->lock)) {
  25. faux_error_add(error, TAG": Illegal 'lock' attribute");
  26. retcode = BOOL_FALSE;
  27. }
  28. }
  29. // Interrupt
  30. if (!faux_str_is_empty(info->interrupt)) {
  31. bool_t b = BOOL_FALSE;
  32. if (!faux_conv_str2bool(info->interrupt, &b) ||
  33. !kaction_set_interrupt(action, b)) {
  34. faux_error_add(error, TAG": Illegal 'interrupt' attribute");
  35. retcode = BOOL_FALSE;
  36. }
  37. }
  38. // Interactive
  39. if (!faux_str_is_empty(info->interactive)) {
  40. bool_t b = BOOL_FALSE;
  41. if (!faux_conv_str2bool(info->interactive, &b) ||
  42. !kaction_set_interactive(action, b)) {
  43. faux_error_add(error, TAG": Illegal 'interactive' attribute");
  44. retcode = BOOL_FALSE;
  45. }
  46. }
  47. // Exec_on
  48. if (!faux_str_is_empty(info->exec_on)) {
  49. kaction_cond_e c = KACTION_COND_NONE;
  50. if (faux_str_casecmp(info->exec_on, "fail"))
  51. c = KACTION_COND_FAIL;
  52. else if (faux_str_casecmp(info->exec_on, "success"))
  53. c = KACTION_COND_SUCCESS;
  54. else if (faux_str_casecmp(info->exec_on, "always"))
  55. c = KACTION_COND_ALWAYS;
  56. if ((KACTION_COND_NONE == c) || !kaction_set_exec_on(action, c)) {
  57. faux_error_add(error, TAG": Illegal 'exec_on' attribute");
  58. retcode = BOOL_FALSE;
  59. }
  60. }
  61. // Update_retcode
  62. if (!faux_str_is_empty(info->update_retcode)) {
  63. bool_t b = BOOL_FALSE;
  64. if (!faux_conv_str2bool(info->update_retcode, &b) ||
  65. !kaction_set_update_retcode(action, b)) {
  66. faux_error_add(error, TAG": Illegal 'update_retcode' attribute");
  67. retcode = BOOL_FALSE;
  68. }
  69. }
  70. // Script
  71. if (!faux_str_is_empty(info->script)) {
  72. if (!kaction_set_script(action, info->script)) {
  73. faux_error_add(error, TAG": Illegal 'script' attribute");
  74. retcode = BOOL_FALSE;
  75. }
  76. }
  77. return retcode;
  78. }
  79. kaction_t *kaction_from_iaction(iaction_t *iaction, faux_error_t *error)
  80. {
  81. kaction_t *kaction = NULL;
  82. kaction = kaction_new();
  83. if (!kaction) {
  84. faux_error_add(error, TAG": Can't create object");
  85. return NULL;
  86. }
  87. if (!kaction_parse(kaction, iaction, error)) {
  88. kaction_free(kaction);
  89. return NULL;
  90. }
  91. return kaction;
  92. }