iplugin.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <dlfcn.h>
  7. #include <faux/str.h>
  8. #include <faux/list.h>
  9. #include <faux/conv.h>
  10. #include <faux/error.h>
  11. #include <klish/khelper.h>
  12. #include <klish/kplugin.h>
  13. #include <klish/ksym.h>
  14. #include <klish/iplugin.h>
  15. #define TAG "PLUGIN"
  16. bool_t iplugin_parse(const iplugin_t *info, kplugin_t *plugin,
  17. faux_error_t *error)
  18. {
  19. bool_t retcode = BOOL_TRUE;
  20. if (!info)
  21. return BOOL_FALSE;
  22. if (!plugin)
  23. return BOOL_FALSE;
  24. // ID
  25. if (!faux_str_is_empty(info->id)) {
  26. if (!kplugin_set_id(plugin, info->id)) {
  27. faux_error_add(error, TAG": Illegal 'id' attribute");
  28. retcode = BOOL_FALSE;
  29. }
  30. }
  31. // File
  32. if (!faux_str_is_empty(info->file)) {
  33. if (!kplugin_set_file(plugin, info->file)) {
  34. faux_error_add(error, TAG": Illegal 'file' attribute");
  35. retcode = BOOL_FALSE;
  36. }
  37. }
  38. // Global
  39. if (!faux_str_is_empty(info->global)) {
  40. bool_t b = BOOL_FALSE;
  41. if (!faux_conv_str2bool(info->global, &b) ||
  42. !kplugin_set_global(plugin, b)) {
  43. faux_error_add(error, TAG": Illegal 'global' attribute");
  44. retcode = BOOL_FALSE;
  45. }
  46. }
  47. // Conf
  48. if (!faux_str_is_empty(info->conf)) {
  49. if (!kplugin_set_conf(plugin, info->conf)) {
  50. faux_error_add(error, TAG": Illegal 'conf' attribute");
  51. retcode = BOOL_FALSE;
  52. }
  53. }
  54. return retcode;
  55. }
  56. kplugin_t *iplugin_load(iplugin_t *iplugin, faux_error_t *error)
  57. {
  58. kplugin_t *kplugin = NULL;
  59. if (!iplugin)
  60. return NULL;
  61. // Name [mandatory]
  62. if (faux_str_is_empty(iplugin->name))
  63. return NULL;
  64. kplugin = kplugin_new(iplugin->name);
  65. if (!kplugin) {
  66. faux_error_sprintf(error, TAG" \"%s\": Can't create object",
  67. iplugin->name);
  68. return NULL;
  69. }
  70. if (!iplugin_parse(iplugin, kplugin, error)) {
  71. kplugin_free(kplugin);
  72. return NULL;
  73. }
  74. return kplugin;
  75. }
  76. char *iplugin_deploy(const kplugin_t *kplugin, int level)
  77. {
  78. char *str = NULL;
  79. char *tmp = NULL;
  80. if (!kplugin)
  81. return NULL;
  82. tmp = faux_str_sprintf("%*cPLUGIN {\n", level, ' ');
  83. faux_str_cat(&str, tmp);
  84. faux_str_free(tmp);
  85. attr2ctext(&str, "name", kplugin_name(kplugin), level + 1);
  86. attr2ctext(&str, "id", kplugin_id(kplugin), level + 1);
  87. attr2ctext(&str, "file", kplugin_file(kplugin), level + 1);
  88. attr2ctext(&str, "global", faux_conv_bool2str(kplugin_global(kplugin)), level + 1);
  89. attr2ctext(&str, "conf", kplugin_conf(kplugin), level + 1);
  90. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  91. faux_str_cat(&str, tmp);
  92. faux_str_free(tmp);
  93. return str;
  94. }