1
0

nspace.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. * PRIVATE METHODS
  17. *--------------------------------------------------------- */
  18. static void clish_nspace_init(clish_nspace_t * this, clish_view_t * view)
  19. {
  20. this->view = view;
  21. /* set up defaults */
  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. /* initialise the tree of commands links for this nspace */
  29. lub_bintree_init(&this->tree,
  30. clish_command_bt_offset(),
  31. clish_command_bt_compare, clish_command_bt_getkey);
  32. }
  33. /*--------------------------------------------------------- */
  34. static void clish_nspace_fini(clish_nspace_t * this)
  35. {
  36. clish_command_t *cmd;
  37. /* deallocate the memory for this instance */
  38. if (this->prefix) {
  39. free(this->prefix);
  40. regfree(&this->prefix_regex);
  41. }
  42. /* delete each command link held by this nspace */
  43. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  44. /* remove the command from the tree */
  45. lub_bintree_remove(&this->tree, cmd);
  46. /* release the instance */
  47. clish_command_delete(cmd);
  48. }
  49. /* Delete prefix pseudo-command */
  50. if (this->prefix_cmd) {
  51. clish_command_delete(this->prefix_cmd);
  52. this->prefix_cmd = NULL;
  53. }
  54. }
  55. /*--------------------------------------------------------- */
  56. clish_command_t * clish_nspace_create_prefix_cmd(clish_nspace_t * this,
  57. const char * name, const char * help)
  58. {
  59. if (this->prefix_cmd) {
  60. clish_command_delete(this->prefix_cmd);
  61. this->prefix_cmd = NULL;
  62. }
  63. return (this->prefix_cmd = clish_command_new(name, help));
  64. }
  65. /*--------------------------------------------------------- */
  66. static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
  67. const char *prefix, const clish_command_t * ref)
  68. {
  69. clish_command_t *cmd;
  70. char *name = NULL;
  71. const char *help = NULL;
  72. clish_command_t *tmp = NULL;
  73. const char *str = NULL;
  74. assert(prefix);
  75. if (!ref) {
  76. assert(this->prefix_cmd);
  77. name = lub_string_dup(prefix);
  78. ref = this->prefix_cmd;
  79. help = clish_command__get_text(this->prefix_cmd);
  80. } else {
  81. lub_string_catn(&name, prefix, strlen(prefix));
  82. lub_string_catn(&name, " ", 1);
  83. lub_string_catn(&name, clish_command__get_name(ref),
  84. strlen(clish_command__get_name(ref)));
  85. help = clish_command__get_text(ref);
  86. }
  87. /* The command is cached already */
  88. if ((cmd = lub_bintree_find(&this->tree, name))) {
  89. free(name);
  90. return cmd;
  91. }
  92. cmd = clish_command_new_link(name, help, ref);
  93. free(name);
  94. assert(cmd);
  95. /* The command was created dynamically */
  96. clish_command__set_dynamic(cmd, BOOL_TRUE);
  97. /* Delete proxy commands with another prefixes */
  98. tmp = lub_bintree_findfirst(&this->tree);
  99. if (tmp)
  100. str = clish_command__get_name(tmp);
  101. if (str && (lub_string_nocasestr(str, prefix) != str)) {
  102. do {
  103. lub_bintree_remove(&this->tree, tmp);
  104. clish_command_delete(tmp);
  105. } while ((tmp = lub_bintree_findfirst(&this->tree)));
  106. }
  107. /* Insert command link into the tree */
  108. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  109. clish_command_delete(cmd);
  110. cmd = NULL;
  111. }
  112. return cmd;
  113. }
  114. /*---------------------------------------------------------
  115. * PUBLIC META FUNCTIONS
  116. *--------------------------------------------------------- */
  117. clish_nspace_t *clish_nspace_new(clish_view_t * view)
  118. {
  119. clish_nspace_t *this = malloc(sizeof(clish_nspace_t));
  120. if (this)
  121. clish_nspace_init(this, view);
  122. return this;
  123. }
  124. /*---------------------------------------------------------
  125. * PUBLIC METHODS
  126. *--------------------------------------------------------- */
  127. void clish_nspace_delete(clish_nspace_t * this)
  128. {
  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_t 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. /*---------------------------------------------------------
  239. * PUBLIC ATTRIBUTES
  240. *--------------------------------------------------------- */
  241. clish_view_t *clish_nspace__get_view(const clish_nspace_t * this)
  242. {
  243. return this->view;
  244. }
  245. /*--------------------------------------------------------- */
  246. void clish_nspace__set_prefix(clish_nspace_t * this, const char *prefix)
  247. {
  248. int res = 0;
  249. assert(!this->prefix);
  250. res = regcomp(&this->prefix_regex, prefix, REG_EXTENDED | REG_ICASE);
  251. assert(!res);
  252. this->prefix = lub_string_dup(prefix);
  253. }
  254. /*--------------------------------------------------------- */
  255. const char *clish_nspace__get_prefix(const clish_nspace_t * this)
  256. {
  257. return this->prefix;
  258. }
  259. /*--------------------------------------------------------- */
  260. const regex_t *clish_nspace__get_prefix_regex(const clish_nspace_t * this)
  261. {
  262. if (!this->prefix)
  263. return NULL;
  264. return &this->prefix_regex;
  265. }
  266. /*--------------------------------------------------------- */
  267. void clish_nspace__set_help(clish_nspace_t * this, bool_t help)
  268. {
  269. this->help = help;
  270. }
  271. /*--------------------------------------------------------- */
  272. bool_t clish_nspace__get_help(const clish_nspace_t * this)
  273. {
  274. return this->help;
  275. }
  276. /*--------------------------------------------------------- */
  277. void clish_nspace__set_completion(clish_nspace_t * this, bool_t completion)
  278. {
  279. this->completion = completion;
  280. }
  281. /*--------------------------------------------------------- */
  282. bool_t clish_nspace__get_completion(const clish_nspace_t * this)
  283. {
  284. return this->completion;
  285. }
  286. /*--------------------------------------------------------- */
  287. void clish_nspace__set_context_help(clish_nspace_t * this, bool_t context_help)
  288. {
  289. this->context_help = context_help;
  290. }
  291. /*--------------------------------------------------------- */
  292. bool_t clish_nspace__get_context_help(const clish_nspace_t * this)
  293. {
  294. return this->context_help;
  295. }
  296. /*--------------------------------------------------------- */
  297. void clish_nspace__set_inherit(clish_nspace_t * this, bool_t inherit)
  298. {
  299. this->inherit = inherit;
  300. }
  301. /*--------------------------------------------------------- */
  302. bool_t clish_nspace__get_inherit(const clish_nspace_t * this)
  303. {
  304. return this->inherit;
  305. }
  306. /*--------------------------------------------------------- */
  307. bool_t clish_nspace__get_visibility(const clish_nspace_t * instance,
  308. clish_nspace_visibility_t field)
  309. {
  310. bool_t result = BOOL_FALSE;
  311. switch (field) {
  312. case CLISH_NSPACE_HELP:
  313. result = clish_nspace__get_help(instance);
  314. break;
  315. case CLISH_NSPACE_COMPLETION:
  316. result = clish_nspace__get_completion(instance);
  317. break;
  318. case CLISH_NSPACE_CHELP:
  319. result = clish_nspace__get_context_help(instance);
  320. break;
  321. default:
  322. break;
  323. }
  324. return result;
  325. }
  326. /*--------------------------------------------------------- */