nspace.c 9.5 KB

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