kplugin.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/list.h>
  7. #include <faux/conv.h>
  8. #include <faux/error.h>
  9. #include <klish/khelper.h>
  10. #include <klish/kplugin.h>
  11. struct kplugin_s {
  12. char *name;
  13. char *id;
  14. char *file;
  15. bool_t global;
  16. char *script;
  17. };
  18. // Simple methods
  19. // Name
  20. KGET_STR(plugin, name);
  21. KSET_STR_ONCE(plugin, name);
  22. // ID
  23. KGET_STR(plugin, id);
  24. KSET_STR(plugin, id);
  25. // File
  26. KGET_STR(plugin, file);
  27. KSET_STR(plugin, file);
  28. // Global
  29. KGET_BOOL(plugin, global);
  30. KSET_BOOL(plugin, global);
  31. // Script
  32. KGET_STR(plugin, script);
  33. KSET_STR(plugin, script);
  34. static kplugin_t *kplugin_new_empty(void)
  35. {
  36. kplugin_t *plugin = NULL;
  37. plugin = faux_zmalloc(sizeof(*plugin));
  38. assert(plugin);
  39. if (!plugin)
  40. return NULL;
  41. // Initialize
  42. plugin->name = NULL;
  43. plugin->id = NULL;
  44. plugin->file = NULL;
  45. plugin->global = BOOL_FALSE;
  46. plugin->script = NULL;
  47. return plugin;
  48. }
  49. kplugin_t *kplugin_new(const iplugin_t *info, kplugin_error_e *error)
  50. {
  51. kplugin_t *plugin = NULL;
  52. plugin = kplugin_new_empty();
  53. assert(plugin);
  54. if (!plugin) {
  55. if (error)
  56. *error = KPLUGIN_ERROR_ALLOC;
  57. return NULL;
  58. }
  59. if (!info)
  60. return plugin;
  61. if (!kplugin_parse(plugin, info, error)) {
  62. kplugin_free(plugin);
  63. return NULL;
  64. }
  65. return plugin;
  66. }
  67. void kplugin_free(kplugin_t *plugin)
  68. {
  69. if (!plugin)
  70. return;
  71. faux_str_free(plugin->name);
  72. faux_str_free(plugin->id);
  73. faux_str_free(plugin->file);
  74. faux_str_free(plugin->script);
  75. faux_free(plugin);
  76. }
  77. const char *kplugin_strerror(kplugin_error_e error)
  78. {
  79. const char *str = NULL;
  80. switch (error) {
  81. case KPLUGIN_ERROR_OK:
  82. str = "Ok";
  83. break;
  84. case KPLUGIN_ERROR_INTERNAL:
  85. str = "Internal error";
  86. break;
  87. case KPLUGIN_ERROR_ALLOC:
  88. str = "Memory allocation error";
  89. break;
  90. case KPLUGIN_ERROR_ATTR_NAME:
  91. str = "Illegal 'name' attribute";
  92. break;
  93. case KPLUGIN_ERROR_ATTR_ID:
  94. str = "Illegal 'id' attribute";
  95. break;
  96. case KPLUGIN_ERROR_ATTR_FILE:
  97. str = "Illegal 'file' attribute";
  98. break;
  99. case KPLUGIN_ERROR_ATTR_GLOBAL:
  100. str = "Illegal 'global' attribute";
  101. break;
  102. case KPLUGIN_ERROR_SCRIPT:
  103. str = "Illegal script";
  104. break;
  105. default:
  106. str = "Unknown error";
  107. break;
  108. }
  109. return str;
  110. }
  111. bool_t kplugin_parse(kplugin_t *plugin, const iplugin_t *info, kplugin_error_e *error)
  112. {
  113. // Name [mandatory]
  114. if (faux_str_is_empty(info->name)) {
  115. if (error)
  116. *error = KPLUGIN_ERROR_ATTR_NAME;
  117. return BOOL_FALSE;
  118. } else {
  119. if (!kplugin_set_name(plugin, info->name)) {
  120. if (error)
  121. *error = KPLUGIN_ERROR_ATTR_NAME;
  122. return BOOL_FALSE;
  123. }
  124. }
  125. // ID
  126. if (!faux_str_is_empty(info->id)) {
  127. if (!kplugin_set_id(plugin, info->id)) {
  128. if (error)
  129. *error = KPLUGIN_ERROR_ATTR_ID;
  130. return BOOL_FALSE;
  131. }
  132. }
  133. // File
  134. if (!faux_str_is_empty(info->file)) {
  135. if (!kplugin_set_file(plugin, info->file)) {
  136. if (error)
  137. *error = KPLUGIN_ERROR_ATTR_FILE;
  138. return BOOL_FALSE;
  139. }
  140. }
  141. // Global
  142. if (!faux_str_is_empty(info->global)) {
  143. bool_t b = BOOL_FALSE;
  144. if (!faux_conv_str2bool(info->global, &b) ||
  145. !kplugin_set_global(plugin, b)) {
  146. if (error)
  147. *error = KPLUGIN_ERROR_ATTR_GLOBAL;
  148. return BOOL_FALSE;
  149. }
  150. }
  151. // Script
  152. if (!faux_str_is_empty(info->script)) {
  153. if (!kplugin_set_script(plugin, info->script)) {
  154. if (error)
  155. *error = KPLUGIN_ERROR_SCRIPT;
  156. return BOOL_FALSE;
  157. }
  158. }
  159. return BOOL_TRUE;
  160. }
  161. kplugin_t *kplugin_from_iplugin(iplugin_t *iplugin, faux_error_t *error_stack)
  162. {
  163. kplugin_t *kplugin = NULL;
  164. kplugin_error_e kplugin_error = KPLUGIN_ERROR_OK;
  165. kplugin = kplugin_new(iplugin, &kplugin_error);
  166. if (!kplugin) {
  167. faux_error_sprintf(error_stack, "PLUGIN \"%s\": %s",
  168. iplugin->name ? iplugin->name : "(null)",
  169. kplugin_strerror(kplugin_error));
  170. return NULL;
  171. }
  172. return kplugin;
  173. }