nspace.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. clish_command_t *tmp = NULL;
  69. lub_bintree_iterator_t iter;
  70. const char *str = NULL;
  71. assert(prefix);
  72. if (!ref) {
  73. assert(this->prefix_cmd);
  74. name = lub_string_dup(prefix);
  75. ref = this->prefix_cmd;
  76. } else {
  77. lub_string_catn(&name, prefix, strlen(prefix));
  78. lub_string_catn(&name, " ", 1);
  79. lub_string_catn(&name, clish_command__get_name(ref),
  80. strlen(clish_command__get_name(ref)));
  81. }
  82. /* The command is cached already */
  83. if (cmd = lub_bintree_find(&this->tree, name)) {
  84. free(name);
  85. return cmd;
  86. }
  87. cmd = clish_command_new_link(name, ref);
  88. free(name);
  89. assert(cmd);
  90. /* Delete proxy commands with another prefixes */
  91. tmp = lub_bintree_findfirst(&this->tree);
  92. if (tmp)
  93. str = clish_command__get_name(tmp);
  94. if (str && (lub_string_nocasestr(str, prefix) != str)) {
  95. do {
  96. lub_bintree_remove(&this->tree, tmp);
  97. clish_command_delete(tmp);
  98. } while ((tmp = lub_bintree_findfirst(&this->tree)));
  99. }
  100. /* Insert command link into the tree */
  101. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  102. clish_command_delete(cmd);
  103. cmd = NULL;
  104. }
  105. return cmd;
  106. }
  107. /*---------------------------------------------------------
  108. * PUBLIC META FUNCTIONS
  109. *--------------------------------------------------------- */
  110. clish_nspace_t *clish_nspace_new(clish_view_t * view)
  111. {
  112. clish_nspace_t *this = malloc(sizeof(clish_nspace_t));
  113. if (this) {
  114. clish_nspace_init(this, view);
  115. }
  116. return this;
  117. }
  118. /*---------------------------------------------------------
  119. * PUBLIC METHODS
  120. *--------------------------------------------------------- */
  121. void clish_nspace_delete(clish_nspace_t * this)
  122. {
  123. clish_nspace_fini(this);
  124. free(this);
  125. }
  126. /*--------------------------------------------------------- */
  127. static const char *clish_nspace_after_prefix(const char *prefix,
  128. const char *line, char **real_prefix)
  129. {
  130. const char *in_line = NULL;
  131. regex_t regexp;
  132. regmatch_t pmatch[1];
  133. int res;
  134. if (!line)
  135. return NULL;
  136. /* Compile regular expression */
  137. regcomp(&regexp, prefix, REG_EXTENDED | REG_ICASE);
  138. res = regexec(&regexp, line, 1, pmatch, 0);
  139. regfree(&regexp);
  140. if (res || (0 != pmatch[0].rm_so))
  141. return NULL;
  142. /* Empty match */
  143. if (0 == pmatch[0].rm_eo)
  144. return NULL;
  145. in_line = line + pmatch[0].rm_eo;
  146. lub_string_catn(real_prefix, line, pmatch[0].rm_eo);
  147. return in_line;
  148. }
  149. /*--------------------------------------------------------- */
  150. clish_command_t *clish_nspace_find_command(clish_nspace_t * this, const char *name)
  151. {
  152. clish_command_t *cmd = NULL, *retval = NULL;
  153. clish_view_t *view = clish_nspace__get_view(this);
  154. const char *prefix = clish_nspace__get_prefix(this);
  155. const char *in_line;
  156. char *real_prefix = NULL;
  157. if (!prefix)
  158. return clish_view_find_command(view, name, this->inherit);
  159. if (!(in_line = clish_nspace_after_prefix(prefix, name, &real_prefix)))
  160. return NULL;
  161. /* If prefix is followed by space */
  162. if (in_line[0] == ' ')
  163. in_line++;
  164. if (in_line[0] != '\0') {
  165. cmd = clish_view_find_command(view, in_line, this->inherit);
  166. if (!cmd) {
  167. lub_string_free(real_prefix);
  168. return NULL;
  169. }
  170. }
  171. retval = clish_nspace_find_create_command(this, real_prefix, cmd);
  172. lub_string_free(real_prefix);
  173. return retval;
  174. }
  175. /*--------------------------------------------------------- */
  176. const clish_command_t *clish_nspace_find_next_completion(clish_nspace_t * this,
  177. const char *iter_cmd, const char *line,
  178. clish_nspace_visibility_t field)
  179. {
  180. const clish_command_t *cmd = NULL, *retval = NULL;
  181. clish_view_t *view = clish_nspace__get_view(this);
  182. const char *prefix = clish_nspace__get_prefix(this);
  183. const char *in_iter = "";
  184. const char *in_line;
  185. char *real_prefix = NULL;
  186. if (!prefix)
  187. return clish_view_find_next_completion(view, iter_cmd,
  188. line, field, this->inherit);
  189. if (!(in_line = clish_nspace_after_prefix(prefix, line, &real_prefix)))
  190. return NULL;
  191. if (in_line[0] != '\0') {
  192. /* If prefix is followed by space */
  193. if (in_line[0] == ' ')
  194. in_line++;
  195. if (iter_cmd &&
  196. (lub_string_nocasestr(iter_cmd, real_prefix) == iter_cmd) &&
  197. (lub_string_nocasecmp(iter_cmd, real_prefix)))
  198. in_iter = iter_cmd + strlen(real_prefix) + 1;
  199. cmd = clish_view_find_next_completion(view,
  200. in_iter, in_line, field, this->inherit);
  201. if (!cmd) {
  202. lub_string_free(real_prefix);
  203. return NULL;
  204. }
  205. }
  206. /* If prefix was already returned. */
  207. if (!cmd && iter_cmd && !lub_string_nocasecmp(iter_cmd, real_prefix)) {
  208. lub_string_free(real_prefix);
  209. return NULL;
  210. }
  211. retval = clish_nspace_find_create_command(this, real_prefix, cmd);
  212. lub_string_free(real_prefix);
  213. return retval;
  214. }
  215. /*--------------------------------------------------------- */
  216. void clish_nspace_clean_proxy(clish_nspace_t * this)
  217. {
  218. clish_command_t *cmd = NULL;
  219. /* Recursive proxy clean */
  220. clish_view_clean_proxy(this->view);
  221. /* Delete each command proxy held by this nspace */
  222. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  223. /* remove the command from the tree */
  224. lub_bintree_remove(&this->tree, cmd);
  225. /* release the instance */
  226. clish_command_delete(cmd);
  227. }
  228. }
  229. /*---------------------------------------------------------
  230. * PUBLIC ATTRIBUTES
  231. *--------------------------------------------------------- */
  232. clish_view_t *clish_nspace__get_view(const clish_nspace_t * this)
  233. {
  234. return this->view;
  235. }
  236. /*--------------------------------------------------------- */
  237. void clish_nspace__set_prefix(clish_nspace_t * this, const char *prefix)
  238. {
  239. assert(NULL == this->prefix);
  240. this->prefix = lub_string_dup(prefix);
  241. }
  242. /*--------------------------------------------------------- */
  243. const char *clish_nspace__get_prefix(const clish_nspace_t * this)
  244. {
  245. return this->prefix;
  246. }
  247. /*--------------------------------------------------------- */
  248. void clish_nspace__set_help(clish_nspace_t * this, bool_t help)
  249. {
  250. this->help = help;
  251. }
  252. /*--------------------------------------------------------- */
  253. bool_t clish_nspace__get_help(const clish_nspace_t * this)
  254. {
  255. return this->help;
  256. }
  257. /*--------------------------------------------------------- */
  258. void clish_nspace__set_completion(clish_nspace_t * this, bool_t completion)
  259. {
  260. this->completion = completion;
  261. }
  262. /*--------------------------------------------------------- */
  263. bool_t clish_nspace__get_completion(const clish_nspace_t * this)
  264. {
  265. return this->completion;
  266. }
  267. /*--------------------------------------------------------- */
  268. void clish_nspace__set_context_help(clish_nspace_t * this, bool_t context_help)
  269. {
  270. this->context_help = context_help;
  271. }
  272. /*--------------------------------------------------------- */
  273. bool_t clish_nspace__get_context_help(const clish_nspace_t * this)
  274. {
  275. return this->context_help;
  276. }
  277. /*--------------------------------------------------------- */
  278. void clish_nspace__set_inherit(clish_nspace_t * this, bool_t inherit)
  279. {
  280. this->inherit = inherit;
  281. }
  282. /*--------------------------------------------------------- */
  283. bool_t clish_nspace__get_inherit(const clish_nspace_t * this)
  284. {
  285. return this->inherit;
  286. }
  287. /*--------------------------------------------------------- */
  288. bool_t
  289. clish_nspace__get_visibility(const clish_nspace_t * instance,
  290. clish_nspace_visibility_t field)
  291. {
  292. bool_t result = BOOL_FALSE;
  293. switch (field) {
  294. case CLISH_NSPACE_HELP:
  295. result = clish_nspace__get_help(instance);
  296. break;
  297. case CLISH_NSPACE_COMPLETION:
  298. result = clish_nspace__get_completion(instance);
  299. break;
  300. case CLISH_NSPACE_CHELP:
  301. result = clish_nspace__get_context_help(instance);
  302. break;
  303. }
  304. return result;
  305. }
  306. /*--------------------------------------------------------- */