nspace.c 8.0 KB

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