nspace.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "lub/ctype.h"
  9. #include <assert.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <regex.h>
  15. #include <ctype.h>
  16. /*--------------------------------------------------------- */
  17. static void clish_nspace_init(clish_nspace_t *this, const char *view_name)
  18. {
  19. this->view_name = NULL;
  20. clish_nspace__set_view_name(this, view_name);
  21. /* Set up defaults */
  22. this->view = 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. this->cmds = lub_list_new(clish_command_compare, clish_command_delete);
  31. }
  32. /*--------------------------------------------------------- */
  33. static void clish_nspace_fini(clish_nspace_t *this)
  34. {
  35. /* Deallocate the memory for this instance */
  36. if (this->prefix) {
  37. free(this->prefix);
  38. regfree(&this->prefix_regex);
  39. }
  40. /* Delete COMMANDs */
  41. lub_list_free_all(this->cmds);
  42. /* Delete prefix pseudo-command */
  43. if (this->prefix_cmd) {
  44. clish_command_delete(this->prefix_cmd);
  45. this->prefix_cmd = NULL;
  46. }
  47. lub_string_free(this->access);
  48. lub_string_free(this->view_name);
  49. }
  50. /*--------------------------------------------------------- */
  51. clish_command_t * clish_nspace_create_prefix_cmd(clish_nspace_t * this,
  52. const char * name, const char * help)
  53. {
  54. if (this->prefix_cmd) {
  55. clish_command_delete(this->prefix_cmd);
  56. this->prefix_cmd = NULL;
  57. }
  58. return (this->prefix_cmd = clish_command_new(name, help));
  59. }
  60. /*--------------------------------------------------------- */
  61. static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
  62. const char *prefix, const clish_command_t * ref)
  63. {
  64. clish_command_t *cmd;
  65. char *name = NULL;
  66. const char *help = NULL;
  67. clish_command_t *tmp = NULL;
  68. const char *str = NULL;
  69. lub_list_node_t *iter = NULL;
  70. assert(prefix);
  71. if (!ref) {
  72. assert(this->prefix_cmd);
  73. name = lub_string_dup(prefix);
  74. ref = this->prefix_cmd;
  75. help = clish_command__get_text(this->prefix_cmd);
  76. } else {
  77. lub_string_catn(&name, prefix, strlen(prefix));
  78. lub_string_catn(&name, " ", 1);
  79. lub_string_catn(&name, clish_command__get_name(ref),
  80. strlen(clish_command__get_name(ref)));
  81. help = clish_command__get_text(ref);
  82. }
  83. /* The command is cached already */
  84. if ((cmd = lub_list_find(this->cmds, clish_command_fn_find_by_name, name))) {
  85. free(name);
  86. return cmd;
  87. }
  88. cmd = clish_command_new_link(name, help, ref);
  89. free(name);
  90. assert(cmd);
  91. /* The command was created dynamically */
  92. clish_command__set_dynamic(cmd, BOOL_TRUE);
  93. /* Delete proxy commands with another prefixes */
  94. iter = lub_list__get_head(this->cmds);
  95. if (iter)
  96. tmp = lub_list_node__get_data(iter);
  97. if (tmp)
  98. str = clish_command__get_name(tmp);
  99. if (str && (lub_string_nocasestr(str, prefix) != str)) {
  100. do {
  101. clish_command_delete(tmp);
  102. lub_list_del(this->cmds, iter);
  103. lub_list_node_free(iter);
  104. iter = lub_list__get_head(this->cmds);
  105. if (iter)
  106. tmp = lub_list_node__get_data(iter);
  107. } while (iter);
  108. }
  109. /* Insert command link into the tree */
  110. if (!lub_list_add(this->cmds, cmd)) {
  111. clish_command_delete(cmd);
  112. cmd = NULL;
  113. }
  114. return cmd;
  115. }
  116. /*--------------------------------------------------------- */
  117. clish_nspace_t *clish_nspace_new(const char *view_name)
  118. {
  119. clish_nspace_t *this = malloc(sizeof(clish_nspace_t));
  120. if (this)
  121. clish_nspace_init(this, view_name);
  122. return this;
  123. }
  124. /*--------------------------------------------------------- */
  125. void clish_nspace_delete(void *data)
  126. {
  127. clish_nspace_t *this = (clish_nspace_t *)data;
  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_resolve_prefix(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_resolve_prefix(view, name);
  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. while (lub_ctype_isspace(in_line[0]))
  165. in_line++;
  166. if (in_line[0] != '\0') {
  167. cmd = clish_view_resolve_prefix(view, in_line);
  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. lub_list_node_t *iter = NULL;
  227. /* Recursive proxy clean */
  228. clish_view_clean_proxy(this->view);
  229. /* Delete each command proxy held by this nspace */
  230. while ((iter = lub_list__get_tail(this->cmds))) {
  231. clish_command_delete(lub_list_node__get_data(iter));
  232. lub_list_del(this->cmds, iter);
  233. lub_list_node_free(iter);
  234. }
  235. }
  236. CLISH_SET(nspace, bool_t, help);
  237. CLISH_GET(nspace, bool_t, help);
  238. CLISH_SET(nspace, bool_t, completion);
  239. CLISH_GET(nspace, bool_t, completion);
  240. CLISH_SET(nspace, bool_t, context_help);
  241. CLISH_GET(nspace, bool_t, context_help);
  242. CLISH_SET(nspace, bool_t, inherit);
  243. CLISH_GET(nspace, bool_t, inherit);
  244. CLISH_SET(nspace, clish_view_t *, view);
  245. CLISH_GET(nspace, clish_view_t *, view);
  246. CLISH_SET_STR(nspace, view_name);
  247. CLISH_GET_STR(nspace, view_name);
  248. CLISH_SET_STR(nspace, access);
  249. CLISH_GET_STR(nspace, access);
  250. CLISH_GET_STR(nspace, prefix);
  251. /*--------------------------------------------------------- */
  252. _CLISH_SET_STR_ONCE(nspace, prefix)
  253. {
  254. int res = 0;
  255. assert(inst);
  256. assert(!inst->prefix);
  257. res = regcomp(&inst->prefix_regex, val, REG_EXTENDED | REG_ICASE);
  258. assert(!res);
  259. inst->prefix = lub_string_dup(val);
  260. }
  261. /*--------------------------------------------------------- */
  262. _CLISH_GET(nspace, const regex_t *, prefix_regex)
  263. {
  264. assert(inst);
  265. if (!inst->prefix)
  266. return NULL;
  267. return &inst->prefix_regex;
  268. }
  269. /*--------------------------------------------------------- */
  270. bool_t clish_nspace__get_visibility(const clish_nspace_t * instance,
  271. clish_nspace_visibility_e field)
  272. {
  273. bool_t result = BOOL_FALSE;
  274. switch (field) {
  275. case CLISH_NSPACE_HELP:
  276. result = clish_nspace__get_help(instance);
  277. break;
  278. case CLISH_NSPACE_COMPLETION:
  279. result = clish_nspace__get_completion(instance);
  280. break;
  281. case CLISH_NSPACE_CHELP:
  282. result = clish_nspace__get_context_help(instance);
  283. break;
  284. default:
  285. break;
  286. }
  287. return result;
  288. }