nspace.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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(clish_nspace_t *this)
  127. {
  128. clish_nspace_fini(this);
  129. free(this);
  130. }
  131. /*--------------------------------------------------------- */
  132. static const char *clish_nspace_after_prefix(const regex_t *prefix_regex,
  133. const char *line, char **real_prefix)
  134. {
  135. const char *in_line = NULL;
  136. regmatch_t pmatch[1];
  137. int res;
  138. if (!line)
  139. return NULL;
  140. /* Compile regular expression */
  141. res = regexec(prefix_regex, line, 1, pmatch, 0);
  142. if (res || (0 != pmatch[0].rm_so))
  143. return NULL;
  144. /* Empty match */
  145. if (0 == pmatch[0].rm_eo)
  146. return NULL;
  147. in_line = line + pmatch[0].rm_eo;
  148. lub_string_catn(real_prefix, line, pmatch[0].rm_eo);
  149. return in_line;
  150. }
  151. /*--------------------------------------------------------- */
  152. clish_command_t *clish_nspace_find_command(clish_nspace_t * this, const char *name)
  153. {
  154. clish_command_t *cmd = NULL, *retval = NULL;
  155. clish_view_t *view = clish_nspace__get_view(this);
  156. const char *in_line;
  157. char *real_prefix = NULL;
  158. if (!clish_nspace__get_prefix(this))
  159. return clish_view_find_command(view, name, this->inherit);
  160. if (!(in_line = clish_nspace_after_prefix(
  161. clish_nspace__get_prefix_regex(this), name, &real_prefix)))
  162. return NULL;
  163. /* If prefix is followed by space */
  164. if (in_line[0] == ' ')
  165. in_line++;
  166. if (in_line[0] != '\0') {
  167. cmd = clish_view_find_command(view, in_line, this->inherit);
  168. if (!cmd) {
  169. lub_string_free(real_prefix);
  170. return NULL;
  171. }
  172. }
  173. retval = clish_nspace_find_create_command(this, real_prefix, cmd);
  174. lub_string_free(real_prefix);
  175. return retval;
  176. }
  177. /*--------------------------------------------------------- */
  178. const clish_command_t *clish_nspace_find_next_completion(clish_nspace_t * this,
  179. const char *iter_cmd, const char *line,
  180. clish_nspace_visibility_e field)
  181. {
  182. const clish_command_t *cmd = NULL, *retval = NULL;
  183. clish_view_t *view = clish_nspace__get_view(this);
  184. const char *in_iter = "";
  185. const char *in_line;
  186. char *real_prefix = NULL;
  187. if (!clish_nspace__get_prefix(this))
  188. return clish_view_find_next_completion(view, iter_cmd,
  189. line, field, this->inherit);
  190. if (!(in_line = clish_nspace_after_prefix(
  191. clish_nspace__get_prefix_regex(this), line, &real_prefix)))
  192. return NULL;
  193. if (in_line[0] != '\0') {
  194. /* If prefix is followed by space */
  195. if (!isspace(in_line[0])) {
  196. lub_string_free(real_prefix);
  197. return NULL;
  198. }
  199. in_line++;
  200. if (iter_cmd &&
  201. (lub_string_nocasestr(iter_cmd, real_prefix) == iter_cmd) &&
  202. (lub_string_nocasecmp(iter_cmd, real_prefix)))
  203. in_iter = iter_cmd + strlen(real_prefix) + 1;
  204. cmd = clish_view_find_next_completion(view,
  205. in_iter, in_line, field, this->inherit);
  206. if (!cmd) {
  207. lub_string_free(real_prefix);
  208. return NULL;
  209. }
  210. }
  211. /* If prefix was already returned. */
  212. if (!cmd && iter_cmd && !lub_string_nocasecmp(iter_cmd, real_prefix)) {
  213. lub_string_free(real_prefix);
  214. return NULL;
  215. }
  216. retval = clish_nspace_find_create_command(this, real_prefix, cmd);
  217. lub_string_free(real_prefix);
  218. if (retval && iter_cmd &&
  219. lub_string_nocasecmp(iter_cmd, clish_command__get_name(retval)) > 0)
  220. return NULL;
  221. return retval;
  222. }
  223. /*--------------------------------------------------------- */
  224. void clish_nspace_clean_proxy(clish_nspace_t * this)
  225. {
  226. clish_command_t *cmd = NULL;
  227. /* Recursive proxy clean */
  228. clish_view_clean_proxy(this->view);
  229. /* Delete each command proxy held by this nspace */
  230. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  231. /* remove the command from the tree */
  232. lub_bintree_remove(&this->tree, cmd);
  233. /* release the instance */
  234. clish_command_delete(cmd);
  235. }
  236. }
  237. CLISH_SET(nspace, bool_t, help);
  238. CLISH_GET(nspace, bool_t, help);
  239. CLISH_SET(nspace, bool_t, completion);
  240. CLISH_GET(nspace, bool_t, completion);
  241. CLISH_SET(nspace, bool_t, context_help);
  242. CLISH_GET(nspace, bool_t, context_help);
  243. CLISH_SET(nspace, bool_t, inherit);
  244. CLISH_GET(nspace, bool_t, inherit);
  245. CLISH_SET(nspace, clish_view_t *, view);
  246. CLISH_GET(nspace, clish_view_t *, view);
  247. CLISH_SET_STR(nspace, view_name);
  248. CLISH_GET_STR(nspace, view_name);
  249. CLISH_SET_STR(nspace, access);
  250. CLISH_GET_STR(nspace, access);
  251. CLISH_GET_STR(nspace, prefix);
  252. /*--------------------------------------------------------- */
  253. _CLISH_SET_STR_ONCE(nspace, prefix)
  254. {
  255. int res = 0;
  256. assert(inst);
  257. assert(!inst->prefix);
  258. res = regcomp(&inst->prefix_regex, val, REG_EXTENDED | REG_ICASE);
  259. assert(!res);
  260. inst->prefix = lub_string_dup(val);
  261. }
  262. /*--------------------------------------------------------- */
  263. _CLISH_GET(nspace, const regex_t *, prefix_regex)
  264. {
  265. assert(inst);
  266. if (!inst->prefix)
  267. return NULL;
  268. return &inst->prefix_regex;
  269. }
  270. /*--------------------------------------------------------- */
  271. bool_t clish_nspace__get_visibility(const clish_nspace_t * instance,
  272. clish_nspace_visibility_e field)
  273. {
  274. bool_t result = BOOL_FALSE;
  275. switch (field) {
  276. case CLISH_NSPACE_HELP:
  277. result = clish_nspace__get_help(instance);
  278. break;
  279. case CLISH_NSPACE_COMPLETION:
  280. result = clish_nspace__get_completion(instance);
  281. break;
  282. case CLISH_NSPACE_CHELP:
  283. result = clish_nspace__get_context_help(instance);
  284. break;
  285. default:
  286. break;
  287. }
  288. return result;
  289. }