nspace.c 7.3 KB

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