1
0

nspace.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #include <ctype.h>
  15. /*--------------------------------------------------------- */
  16. static void clish_nspace_init(clish_nspace_t *this, const char *view_name)
  17. {
  18. this->view_name = NULL;
  19. clish_nspace__set_view_name(this, view_name);
  20. /* Set up defaults */
  21. this->view = NULL;
  22. this->prefix = NULL;
  23. this->help = BOOL_FALSE;
  24. this->completion = BOOL_TRUE;
  25. this->context_help = BOOL_FALSE;
  26. this->inherit = BOOL_TRUE;
  27. this->prefix_cmd = NULL;
  28. this->access = NULL;
  29. /* initialise the tree of commands links for this nspace */
  30. lub_bintree_init(&this->tree,
  31. clish_command_bt_offset(),
  32. clish_command_bt_compare, clish_command_bt_getkey);
  33. }
  34. /*--------------------------------------------------------- */
  35. static void clish_nspace_fini(clish_nspace_t *this)
  36. {
  37. clish_command_t *cmd;
  38. /* deallocate the memory for this instance */
  39. if (this->prefix) {
  40. free(this->prefix);
  41. regfree(&this->prefix_regex);
  42. }
  43. /* delete each command link held by this nspace */
  44. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  45. /* remove the command from the tree */
  46. lub_bintree_remove(&this->tree, cmd);
  47. /* release the instance */
  48. clish_command_delete(cmd);
  49. }
  50. /* Delete prefix pseudo-command */
  51. if (this->prefix_cmd) {
  52. clish_command_delete(this->prefix_cmd);
  53. this->prefix_cmd = NULL;
  54. }
  55. lub_string_free(this->access);
  56. lub_string_free(this->view_name);
  57. }
  58. /*--------------------------------------------------------- */
  59. clish_command_t * clish_nspace_create_prefix_cmd(clish_nspace_t * this,
  60. const char * name, const char * help)
  61. {
  62. if (this->prefix_cmd) {
  63. clish_command_delete(this->prefix_cmd);
  64. this->prefix_cmd = NULL;
  65. }
  66. return (this->prefix_cmd = clish_command_new(name, help));
  67. }
  68. /*--------------------------------------------------------- */
  69. static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
  70. const char *prefix, const clish_command_t * ref)
  71. {
  72. clish_command_t *cmd;
  73. char *name = NULL;
  74. const char *help = NULL;
  75. clish_command_t *tmp = NULL;
  76. const char *str = NULL;
  77. assert(prefix);
  78. if (!ref) {
  79. assert(this->prefix_cmd);
  80. name = lub_string_dup(prefix);
  81. ref = this->prefix_cmd;
  82. help = clish_command__get_text(this->prefix_cmd);
  83. } else {
  84. lub_string_catn(&name, prefix, strlen(prefix));
  85. lub_string_catn(&name, " ", 1);
  86. lub_string_catn(&name, clish_command__get_name(ref),
  87. strlen(clish_command__get_name(ref)));
  88. help = clish_command__get_text(ref);
  89. }
  90. /* The command is cached already */
  91. if ((cmd = lub_bintree_find(&this->tree, name))) {
  92. free(name);
  93. return cmd;
  94. }
  95. cmd = clish_command_new_link(name, help, ref);
  96. free(name);
  97. assert(cmd);
  98. /* The command was created dynamically */
  99. clish_command__set_dynamic(cmd, BOOL_TRUE);
  100. /* Delete proxy commands with another prefixes */
  101. tmp = lub_bintree_findfirst(&this->tree);
  102. if (tmp)
  103. str = clish_command__get_name(tmp);
  104. if (str && (lub_string_nocasestr(str, prefix) != str)) {
  105. do {
  106. lub_bintree_remove(&this->tree, tmp);
  107. clish_command_delete(tmp);
  108. } while ((tmp = lub_bintree_findfirst(&this->tree)));
  109. }
  110. /* Insert command link into the tree */
  111. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  112. clish_command_delete(cmd);
  113. cmd = NULL;
  114. }
  115. return cmd;
  116. }
  117. /*--------------------------------------------------------- */
  118. clish_nspace_t *clish_nspace_new(const char *view_name)
  119. {
  120. clish_nspace_t *this = malloc(sizeof(clish_nspace_t));
  121. if (this)
  122. clish_nspace_init(this, view_name);
  123. return this;
  124. }
  125. /*--------------------------------------------------------- */
  126. void clish_nspace_delete(void *data)
  127. {
  128. clish_nspace_t *this = (clish_nspace_t *)data;
  129. clish_nspace_fini(this);
  130. free(this);
  131. }
  132. /*--------------------------------------------------------- */
  133. static const char *clish_nspace_after_prefix(const regex_t *prefix_regex,
  134. const char *line, char **real_prefix)
  135. {
  136. const char *in_line = NULL;
  137. regmatch_t pmatch[1];
  138. int res;
  139. if (!line)
  140. return NULL;
  141. /* Compile regular expression */
  142. res = regexec(prefix_regex, line, 1, pmatch, 0);
  143. if (res || (0 != pmatch[0].rm_so))
  144. return NULL;
  145. /* Empty match */
  146. if (0 == pmatch[0].rm_eo)
  147. return NULL;
  148. in_line = line + pmatch[0].rm_eo;
  149. lub_string_catn(real_prefix, line, pmatch[0].rm_eo);
  150. return in_line;
  151. }
  152. /*--------------------------------------------------------- */
  153. clish_command_t *clish_nspace_find_command(clish_nspace_t * this, const char *name)
  154. {
  155. clish_command_t *cmd = NULL, *retval = NULL;
  156. clish_view_t *view = clish_nspace__get_view(this);
  157. const char *in_line;
  158. char *real_prefix = NULL;
  159. if (!clish_nspace__get_prefix(this))
  160. return clish_view_find_command(view, name, this->inherit);
  161. if (!(in_line = clish_nspace_after_prefix(
  162. clish_nspace__get_prefix_regex(this), name, &real_prefix)))
  163. return NULL;
  164. /* If prefix is followed by space */
  165. if (in_line[0] == ' ')
  166. in_line++;
  167. if (in_line[0] != '\0') {
  168. cmd = clish_view_find_command(view, in_line, this->inherit);
  169. if (!cmd) {
  170. lub_string_free(real_prefix);
  171. return NULL;
  172. }
  173. }
  174. retval = clish_nspace_find_create_command(this, real_prefix, cmd);
  175. lub_string_free(real_prefix);
  176. return retval;
  177. }
  178. /*--------------------------------------------------------- */
  179. const clish_command_t *clish_nspace_find_next_completion(clish_nspace_t * this,
  180. const char *iter_cmd, const char *line,
  181. clish_nspace_visibility_e field)
  182. {
  183. const clish_command_t *cmd = NULL, *retval = NULL;
  184. clish_view_t *view = clish_nspace__get_view(this);
  185. const char *in_iter = "";
  186. const char *in_line;
  187. char *real_prefix = NULL;
  188. if (!clish_nspace__get_prefix(this))
  189. return clish_view_find_next_completion(view, iter_cmd,
  190. line, field, this->inherit);
  191. if (!(in_line = clish_nspace_after_prefix(
  192. clish_nspace__get_prefix_regex(this), line, &real_prefix)))
  193. return NULL;
  194. if (in_line[0] != '\0') {
  195. /* If prefix is followed by space */
  196. if (!isspace(in_line[0])) {
  197. lub_string_free(real_prefix);
  198. return NULL;
  199. }
  200. in_line++;
  201. if (iter_cmd &&
  202. (lub_string_nocasestr(iter_cmd, real_prefix) == iter_cmd) &&
  203. (lub_string_nocasecmp(iter_cmd, real_prefix)))
  204. in_iter = iter_cmd + strlen(real_prefix) + 1;
  205. cmd = clish_view_find_next_completion(view,
  206. in_iter, in_line, field, this->inherit);
  207. if (!cmd) {
  208. lub_string_free(real_prefix);
  209. return NULL;
  210. }
  211. }
  212. /* If prefix was already returned. */
  213. if (!cmd && iter_cmd && !lub_string_nocasecmp(iter_cmd, real_prefix)) {
  214. lub_string_free(real_prefix);
  215. return NULL;
  216. }
  217. retval = clish_nspace_find_create_command(this, real_prefix, cmd);
  218. lub_string_free(real_prefix);
  219. if (retval && iter_cmd &&
  220. lub_string_nocasecmp(iter_cmd, clish_command__get_name(retval)) > 0)
  221. return NULL;
  222. return retval;
  223. }
  224. /*--------------------------------------------------------- */
  225. void clish_nspace_clean_proxy(clish_nspace_t * this)
  226. {
  227. clish_command_t *cmd = NULL;
  228. /* Recursive proxy clean */
  229. clish_view_clean_proxy(this->view);
  230. /* Delete each command proxy held by this nspace */
  231. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  232. /* remove the command from the tree */
  233. lub_bintree_remove(&this->tree, cmd);
  234. /* release the instance */
  235. clish_command_delete(cmd);
  236. }
  237. }
  238. CLISH_SET(nspace, bool_t, help);
  239. CLISH_GET(nspace, bool_t, help);
  240. CLISH_SET(nspace, bool_t, completion);
  241. CLISH_GET(nspace, bool_t, completion);
  242. CLISH_SET(nspace, bool_t, context_help);
  243. CLISH_GET(nspace, bool_t, context_help);
  244. CLISH_SET(nspace, bool_t, inherit);
  245. CLISH_GET(nspace, bool_t, inherit);
  246. CLISH_SET(nspace, clish_view_t *, view);
  247. CLISH_GET(nspace, clish_view_t *, view);
  248. CLISH_SET_STR(nspace, view_name);
  249. CLISH_GET_STR(nspace, view_name);
  250. CLISH_SET_STR(nspace, access);
  251. CLISH_GET_STR(nspace, access);
  252. CLISH_GET_STR(nspace, prefix);
  253. /*--------------------------------------------------------- */
  254. _CLISH_SET_STR_ONCE(nspace, prefix)
  255. {
  256. int res = 0;
  257. assert(inst);
  258. assert(!inst->prefix);
  259. res = regcomp(&inst->prefix_regex, val, REG_EXTENDED | REG_ICASE);
  260. assert(!res);
  261. inst->prefix = lub_string_dup(val);
  262. }
  263. /*--------------------------------------------------------- */
  264. _CLISH_GET(nspace, const regex_t *, prefix_regex)
  265. {
  266. assert(inst);
  267. if (!inst->prefix)
  268. return NULL;
  269. return &inst->prefix_regex;
  270. }
  271. /*--------------------------------------------------------- */
  272. bool_t clish_nspace__get_visibility(const clish_nspace_t * instance,
  273. clish_nspace_visibility_e field)
  274. {
  275. bool_t result = BOOL_FALSE;
  276. switch (field) {
  277. case CLISH_NSPACE_HELP:
  278. result = clish_nspace__get_help(instance);
  279. break;
  280. case CLISH_NSPACE_COMPLETION:
  281. result = clish_nspace__get_completion(instance);
  282. break;
  283. case CLISH_NSPACE_CHELP:
  284. result = clish_nspace__get_context_help(instance);
  285. break;
  286. default:
  287. break;
  288. }
  289. return result;
  290. }