iplugin.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // Conf
  39. if (!faux_str_is_empty(info->conf)) {
  40. if (!kplugin_set_conf(plugin, info->conf)) {
  41. faux_error_add(error, TAG": Illegal 'conf' attribute");
  42. retcode = BOOL_FALSE;
  43. }
  44. }
  45. return retcode;
  46. }
  47. kplugin_t *iplugin_load(iplugin_t *iplugin, faux_error_t *error)
  48. {
  49. kplugin_t *kplugin = NULL;
  50. if (!iplugin)
  51. return NULL;
  52. // Name [mandatory]
  53. if (faux_str_is_empty(iplugin->name))
  54. return NULL;
  55. kplugin = kplugin_new(iplugin->name);
  56. if (!kplugin) {
  57. faux_error_sprintf(error, TAG" \"%s\": Can't create object",
  58. iplugin->name);
  59. return NULL;
  60. }
  61. if (!iplugin_parse(iplugin, kplugin, error)) {
  62. kplugin_free(kplugin);
  63. return NULL;
  64. }
  65. return kplugin;
  66. }
  67. char *iplugin_deploy(const kplugin_t *kplugin, int level)
  68. {
  69. char *str = NULL;
  70. char *tmp = NULL;
  71. if (!kplugin)
  72. return NULL;
  73. tmp = faux_str_sprintf("%*cPLUGIN {\n", level, ' ');
  74. faux_str_cat(&str, tmp);
  75. faux_str_free(tmp);
  76. attr2ctext(&str, "name", kplugin_name(kplugin), level + 1);
  77. attr2ctext(&str, "id", kplugin_id(kplugin), level + 1);
  78. attr2ctext(&str, "file", kplugin_file(kplugin), level + 1);
  79. attr2ctext(&str, "conf", kplugin_conf(kplugin), level + 1);
  80. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  81. faux_str_cat(&str, tmp);
  82. faux_str_free(tmp);
  83. return str;
  84. }