nspace.c 9.8 KB

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