nspace.c 8.5 KB

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