nspace.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * nspace.c
  3. *
  4. * This file provides the implementation of the "nspace" class
  5. */
  6. #include "private.h"
  7. #include "lub/string.h"
  8. #include <assert.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. /*---------------------------------------------------------
  13. * PRIVATE METHODS
  14. *--------------------------------------------------------- */
  15. static void clish_nspace_init(clish_nspace_t * this, clish_view_t * view)
  16. {
  17. this->view = view;
  18. /* set up defaults */
  19. this->prefix = NULL;
  20. this->help = BOOL_FALSE;
  21. this->completion = BOOL_TRUE;
  22. this->context_help = BOOL_FALSE;
  23. this->inherit = BOOL_TRUE;
  24. this->restore = CLISH_RESTORE_NONE;
  25. /* initialise the tree of commands links for this nspace */
  26. lub_bintree_init(&this->tree,
  27. clish_command_bt_offset(),
  28. clish_command_bt_compare, clish_command_bt_getkey);
  29. }
  30. /*--------------------------------------------------------- */
  31. static void clish_nspace_fini(clish_nspace_t * this)
  32. {
  33. clish_command_t *cmd;
  34. /* deallocate the memory for this instance */
  35. lub_string_free(this->prefix);
  36. this->prefix = NULL;
  37. /* delete each command link held by this nspace */
  38. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  39. /* remove the command from the tree */
  40. lub_bintree_remove(&this->tree, cmd);
  41. /* release the instance */
  42. clish_command_delete(cmd);
  43. }
  44. }
  45. /*--------------------------------------------------------- */
  46. static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
  47. const char *prefix,
  48. const clish_command_t *
  49. ref)
  50. {
  51. clish_command_t *cmd;
  52. char *name = NULL;
  53. assert(prefix);
  54. lub_string_catn(&name, prefix, strlen(prefix));
  55. lub_string_catn(&name, " ", 1);
  56. lub_string_catn(&name, clish_command__get_name(ref),
  57. strlen(clish_command__get_name(ref)));
  58. /* The command is cached already */
  59. if (cmd = lub_bintree_find(&this->tree, name)) {
  60. free(name);
  61. return cmd;
  62. }
  63. cmd = clish_command_new_link(name, ref);
  64. free(name);
  65. assert(cmd);
  66. /* Insert command link into the tree */
  67. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  68. clish_command_delete(cmd);
  69. cmd = NULL;
  70. }
  71. return cmd;
  72. }
  73. /*---------------------------------------------------------
  74. * PUBLIC META FUNCTIONS
  75. *--------------------------------------------------------- */
  76. clish_nspace_t *clish_nspace_new(clish_view_t * view)
  77. {
  78. clish_nspace_t *this = malloc(sizeof(clish_nspace_t));
  79. if (this) {
  80. clish_nspace_init(this, view);
  81. }
  82. return this;
  83. }
  84. /*---------------------------------------------------------
  85. * PUBLIC METHODS
  86. *--------------------------------------------------------- */
  87. void clish_nspace_delete(clish_nspace_t * this)
  88. {
  89. clish_nspace_fini(this);
  90. free(this);
  91. }
  92. /*--------------------------------------------------------- */
  93. static const char *clish_nspace_after_prefix(const char *prefix,
  94. const char *line)
  95. {
  96. const char *in_line = NULL;
  97. if (!line)
  98. return NULL;
  99. /* If entered line contain nspace prefix */
  100. if (lub_string_nocasestr(line, prefix) != line)
  101. return NULL;
  102. /* If entered line contain only the prefix */
  103. if (!lub_string_nocasecmp(line, prefix))
  104. return NULL;
  105. /* If prefix is not followed by space */
  106. in_line = line + strlen(prefix);
  107. if (in_line[0] != ' ')
  108. return NULL;
  109. return (in_line + 1);
  110. }
  111. /*--------------------------------------------------------- */
  112. clish_command_t *clish_nspace_find_command(clish_nspace_t * this, const char *name)
  113. {
  114. clish_command_t *cmd = NULL, *cmd_link = NULL;
  115. clish_view_t *view = clish_nspace__get_view(this);
  116. const char *prefix = clish_nspace__get_prefix(this);
  117. const char *in_line;
  118. if (!prefix)
  119. return clish_view_find_command(view, name, this->inherit);
  120. if (!(in_line = clish_nspace_after_prefix(prefix, name)))
  121. return NULL;
  122. cmd = clish_view_find_command(view, in_line, this->inherit);
  123. if (!cmd)
  124. return NULL;
  125. return clish_nspace_find_create_command(this, prefix, cmd);
  126. }
  127. /*--------------------------------------------------------- */
  128. const clish_command_t *clish_nspace_find_next_completion(clish_nspace_t * this,
  129. const char *iter_cmd, const char *line, clish_nspace_visibility_t field)
  130. {
  131. const clish_command_t *cmd = NULL, *cmd_link = NULL;
  132. clish_view_t *view = clish_nspace__get_view(this);
  133. const char *prefix = clish_nspace__get_prefix(this);
  134. const char *in_iter = "";
  135. const char *in_line;
  136. if (!prefix)
  137. return clish_view_find_next_completion(view, iter_cmd, line, field, this->inherit);
  138. if (!(in_line = clish_nspace_after_prefix(prefix, line)))
  139. return NULL;
  140. if (iter_cmd &&
  141. (lub_string_nocasestr(iter_cmd, prefix) == iter_cmd) &&
  142. (lub_string_nocasecmp(iter_cmd, prefix)))
  143. in_iter = iter_cmd + strlen(prefix) + 1;
  144. cmd = clish_view_find_next_completion(view, in_iter, in_line, field, this->inherit);
  145. if (!cmd)
  146. return NULL;
  147. return clish_nspace_find_create_command(this, prefix, cmd);
  148. }
  149. /*---------------------------------------------------------
  150. * PUBLIC ATTRIBUTES
  151. *--------------------------------------------------------- */
  152. clish_view_t *clish_nspace__get_view(const clish_nspace_t * this)
  153. {
  154. return this->view;
  155. }
  156. /*--------------------------------------------------------- */
  157. void clish_nspace__set_prefix(clish_nspace_t * this, const char *prefix)
  158. {
  159. assert(NULL == this->prefix);
  160. this->prefix = lub_string_dup(prefix);
  161. }
  162. /*--------------------------------------------------------- */
  163. const char *clish_nspace__get_prefix(const clish_nspace_t * this)
  164. {
  165. return this->prefix;
  166. }
  167. /*--------------------------------------------------------- */
  168. void clish_nspace__set_help(clish_nspace_t * this, bool_t help)
  169. {
  170. this->help = help;
  171. }
  172. /*--------------------------------------------------------- */
  173. bool_t clish_nspace__get_help(const clish_nspace_t * this)
  174. {
  175. return this->help;
  176. }
  177. /*--------------------------------------------------------- */
  178. void clish_nspace__set_completion(clish_nspace_t * this, bool_t completion)
  179. {
  180. this->completion = completion;
  181. }
  182. /*--------------------------------------------------------- */
  183. bool_t clish_nspace__get_completion(const clish_nspace_t * this)
  184. {
  185. return this->completion;
  186. }
  187. /*--------------------------------------------------------- */
  188. void clish_nspace__set_context_help(clish_nspace_t * this, bool_t context_help)
  189. {
  190. this->context_help = context_help;
  191. }
  192. /*--------------------------------------------------------- */
  193. bool_t clish_nspace__get_context_help(const clish_nspace_t * this)
  194. {
  195. return this->context_help;
  196. }
  197. /*--------------------------------------------------------- */
  198. void clish_nspace__set_inherit(clish_nspace_t * this, bool_t inherit)
  199. {
  200. this->inherit = inherit;
  201. }
  202. /*--------------------------------------------------------- */
  203. bool_t clish_nspace__get_inherit(const clish_nspace_t * this)
  204. {
  205. return this->inherit;
  206. }
  207. /*--------------------------------------------------------- */
  208. bool_t
  209. clish_nspace__get_visibility(const clish_nspace_t * instance,
  210. clish_nspace_visibility_t field)
  211. {
  212. bool_t result = BOOL_FALSE;
  213. switch (field) {
  214. case CLISH_NSPACE_HELP:
  215. result = clish_nspace__get_help(instance);
  216. break;
  217. case CLISH_NSPACE_COMPLETION:
  218. result = clish_nspace__get_completion(instance);
  219. break;
  220. case CLISH_NSPACE_CHELP:
  221. result = clish_nspace__get_context_help(instance);
  222. break;
  223. }
  224. return result;
  225. }
  226. /*--------------------------------------------------------- */
  227. void clish_nspace__set_restore(clish_nspace_t * this,
  228. clish_nspace_restore_t restore)
  229. {
  230. this->restore = restore;
  231. }
  232. /*--------------------------------------------------------- */
  233. clish_nspace_restore_t clish_nspace__get_restore(const clish_nspace_t * this)
  234. {
  235. return this->restore;
  236. }
  237. /*--------------------------------------------------------- */