nspace.c 8.8 KB

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