1
0

nspace.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. 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. }
  57. /*--------------------------------------------------------- */
  58. clish_command_t * clish_nspace_create_prefix_cmd(clish_nspace_t * this,
  59. const char * name, const char * help)
  60. {
  61. if (this->prefix_cmd) {
  62. clish_command_delete(this->prefix_cmd);
  63. this->prefix_cmd = NULL;
  64. }
  65. return (this->prefix_cmd = clish_command_new(name, help));
  66. }
  67. /*--------------------------------------------------------- */
  68. static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
  69. const char *prefix, const clish_command_t * ref)
  70. {
  71. clish_command_t *cmd;
  72. char *name = NULL;
  73. const char *help = NULL;
  74. clish_command_t *tmp = NULL;
  75. const char *str = NULL;
  76. assert(prefix);
  77. if (!ref) {
  78. assert(this->prefix_cmd);
  79. name = lub_string_dup(prefix);
  80. ref = this->prefix_cmd;
  81. help = clish_command__get_text(this->prefix_cmd);
  82. } else {
  83. lub_string_catn(&name, prefix, strlen(prefix));
  84. lub_string_catn(&name, " ", 1);
  85. lub_string_catn(&name, clish_command__get_name(ref),
  86. strlen(clish_command__get_name(ref)));
  87. help = clish_command__get_text(ref);
  88. }
  89. /* The command is cached already */
  90. if ((cmd = lub_bintree_find(&this->tree, name))) {
  91. free(name);
  92. return cmd;
  93. }
  94. cmd = clish_command_new_link(name, help, ref);
  95. free(name);
  96. assert(cmd);
  97. /* The command was created dynamically */
  98. clish_command__set_dynamic(cmd, BOOL_TRUE);
  99. /* Delete proxy commands with another prefixes */
  100. tmp = lub_bintree_findfirst(&this->tree);
  101. if (tmp)
  102. str = clish_command__get_name(tmp);
  103. if (str && (lub_string_nocasestr(str, prefix) != str)) {
  104. do {
  105. lub_bintree_remove(&this->tree, tmp);
  106. clish_command_delete(tmp);
  107. } while ((tmp = lub_bintree_findfirst(&this->tree)));
  108. }
  109. /* Insert command link into the tree */
  110. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  111. clish_command_delete(cmd);
  112. cmd = NULL;
  113. }
  114. return cmd;
  115. }
  116. /*---------------------------------------------------------
  117. * PUBLIC META FUNCTIONS
  118. *--------------------------------------------------------- */
  119. clish_nspace_t *clish_nspace_new(clish_view_t * view)
  120. {
  121. clish_nspace_t *this = malloc(sizeof(clish_nspace_t));
  122. if (this)
  123. clish_nspace_init(this, view);
  124. return this;
  125. }
  126. /*---------------------------------------------------------
  127. * PUBLIC METHODS
  128. *--------------------------------------------------------- */
  129. void clish_nspace_delete(clish_nspace_t * this)
  130. {
  131. clish_nspace_fini(this);
  132. free(this);
  133. }
  134. /*--------------------------------------------------------- */
  135. static const char *clish_nspace_after_prefix(const regex_t *prefix_regex,
  136. const char *line, char **real_prefix)
  137. {
  138. const char *in_line = NULL;
  139. regmatch_t pmatch[1];
  140. int res;
  141. if (!line)
  142. return NULL;
  143. /* Compile regular expression */
  144. res = regexec(prefix_regex, line, 1, pmatch, 0);
  145. if (res || (0 != pmatch[0].rm_so))
  146. return NULL;
  147. /* Empty match */
  148. if (0 == pmatch[0].rm_eo)
  149. return NULL;
  150. in_line = line + pmatch[0].rm_eo;
  151. lub_string_catn(real_prefix, line, pmatch[0].rm_eo);
  152. return in_line;
  153. }
  154. /*--------------------------------------------------------- */
  155. clish_command_t *clish_nspace_find_command(clish_nspace_t * this, const char *name)
  156. {
  157. clish_command_t *cmd = NULL, *retval = NULL;
  158. clish_view_t *view = clish_nspace__get_view(this);
  159. const char *in_line;
  160. char *real_prefix = NULL;
  161. if (!clish_nspace__get_prefix(this))
  162. return clish_view_find_command(view, name, this->inherit);
  163. if (!(in_line = clish_nspace_after_prefix(
  164. clish_nspace__get_prefix_regex(this), name, &real_prefix)))
  165. return NULL;
  166. /* If prefix is followed by space */
  167. if (in_line[0] == ' ')
  168. in_line++;
  169. if (in_line[0] != '\0') {
  170. cmd = clish_view_find_command(view, in_line, this->inherit);
  171. if (!cmd) {
  172. lub_string_free(real_prefix);
  173. return NULL;
  174. }
  175. }
  176. retval = clish_nspace_find_create_command(this, real_prefix, cmd);
  177. lub_string_free(real_prefix);
  178. return retval;
  179. }
  180. /*--------------------------------------------------------- */
  181. const clish_command_t *clish_nspace_find_next_completion(clish_nspace_t * this,
  182. const char *iter_cmd, const char *line,
  183. clish_nspace_visibility_t field)
  184. {
  185. const clish_command_t *cmd = NULL, *retval = NULL;
  186. clish_view_t *view = clish_nspace__get_view(this);
  187. const char *in_iter = "";
  188. const char *in_line;
  189. char *real_prefix = NULL;
  190. if (!clish_nspace__get_prefix(this))
  191. return clish_view_find_next_completion(view, iter_cmd,
  192. line, field, this->inherit);
  193. if (!(in_line = clish_nspace_after_prefix(
  194. clish_nspace__get_prefix_regex(this), line, &real_prefix)))
  195. return NULL;
  196. if (in_line[0] != '\0') {
  197. /* If prefix is followed by space */
  198. if (!isspace(in_line[0])) {
  199. lub_string_free(real_prefix);
  200. return NULL;
  201. }
  202. in_line++;
  203. if (iter_cmd &&
  204. (lub_string_nocasestr(iter_cmd, real_prefix) == iter_cmd) &&
  205. (lub_string_nocasecmp(iter_cmd, real_prefix)))
  206. in_iter = iter_cmd + strlen(real_prefix) + 1;
  207. cmd = clish_view_find_next_completion(view,
  208. in_iter, in_line, field, this->inherit);
  209. if (!cmd) {
  210. lub_string_free(real_prefix);
  211. return NULL;
  212. }
  213. }
  214. /* If prefix was already returned. */
  215. if (!cmd && iter_cmd && !lub_string_nocasecmp(iter_cmd, real_prefix)) {
  216. lub_string_free(real_prefix);
  217. return NULL;
  218. }
  219. retval = clish_nspace_find_create_command(this, real_prefix, cmd);
  220. lub_string_free(real_prefix);
  221. if (retval && iter_cmd &&
  222. lub_string_nocasecmp(iter_cmd, clish_command__get_name(retval)) > 0)
  223. return NULL;
  224. return retval;
  225. }
  226. /*--------------------------------------------------------- */
  227. void clish_nspace_clean_proxy(clish_nspace_t * this)
  228. {
  229. clish_command_t *cmd = NULL;
  230. /* Recursive proxy clean */
  231. clish_view_clean_proxy(this->view);
  232. /* Delete each command proxy held by this nspace */
  233. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  234. /* remove the command from the tree */
  235. lub_bintree_remove(&this->tree, cmd);
  236. /* release the instance */
  237. clish_command_delete(cmd);
  238. }
  239. }
  240. /*---------------------------------------------------------
  241. * PUBLIC ATTRIBUTES
  242. *--------------------------------------------------------- */
  243. clish_view_t *clish_nspace__get_view(const clish_nspace_t * this)
  244. {
  245. return this->view;
  246. }
  247. /*--------------------------------------------------------- */
  248. void clish_nspace__set_prefix(clish_nspace_t * this, const char *prefix)
  249. {
  250. int res = 0;
  251. assert(!this->prefix);
  252. res = regcomp(&this->prefix_regex, prefix, REG_EXTENDED | REG_ICASE);
  253. assert(!res);
  254. this->prefix = lub_string_dup(prefix);
  255. }
  256. /*--------------------------------------------------------- */
  257. const char *clish_nspace__get_prefix(const clish_nspace_t * this)
  258. {
  259. return this->prefix;
  260. }
  261. /*--------------------------------------------------------- */
  262. const regex_t *clish_nspace__get_prefix_regex(const clish_nspace_t * this)
  263. {
  264. if (!this->prefix)
  265. return NULL;
  266. return &this->prefix_regex;
  267. }
  268. /*--------------------------------------------------------- */
  269. void clish_nspace__set_help(clish_nspace_t * this, bool_t help)
  270. {
  271. this->help = help;
  272. }
  273. /*--------------------------------------------------------- */
  274. bool_t clish_nspace__get_help(const clish_nspace_t * this)
  275. {
  276. return this->help;
  277. }
  278. /*--------------------------------------------------------- */
  279. void clish_nspace__set_completion(clish_nspace_t * this, bool_t completion)
  280. {
  281. this->completion = completion;
  282. }
  283. /*--------------------------------------------------------- */
  284. bool_t clish_nspace__get_completion(const clish_nspace_t * this)
  285. {
  286. return this->completion;
  287. }
  288. /*--------------------------------------------------------- */
  289. void clish_nspace__set_context_help(clish_nspace_t * this, bool_t context_help)
  290. {
  291. this->context_help = context_help;
  292. }
  293. /*--------------------------------------------------------- */
  294. bool_t clish_nspace__get_context_help(const clish_nspace_t * this)
  295. {
  296. return this->context_help;
  297. }
  298. /*--------------------------------------------------------- */
  299. void clish_nspace__set_inherit(clish_nspace_t * this, bool_t inherit)
  300. {
  301. this->inherit = inherit;
  302. }
  303. /*--------------------------------------------------------- */
  304. bool_t clish_nspace__get_inherit(const clish_nspace_t * this)
  305. {
  306. return this->inherit;
  307. }
  308. /*--------------------------------------------------------- */
  309. bool_t clish_nspace__get_visibility(const clish_nspace_t * instance,
  310. clish_nspace_visibility_t field)
  311. {
  312. bool_t result = BOOL_FALSE;
  313. switch (field) {
  314. case CLISH_NSPACE_HELP:
  315. result = clish_nspace__get_help(instance);
  316. break;
  317. case CLISH_NSPACE_COMPLETION:
  318. result = clish_nspace__get_completion(instance);
  319. break;
  320. case CLISH_NSPACE_CHELP:
  321. result = clish_nspace__get_context_help(instance);
  322. break;
  323. default:
  324. break;
  325. }
  326. return result;
  327. }
  328. /*--------------------------------------------------------- */
  329. void clish_nspace__set_access(clish_nspace_t *this, const char *access)
  330. {
  331. if (this->access)
  332. lub_string_free(this->access);
  333. this->access = lub_string_dup(access);
  334. }
  335. /*--------------------------------------------------------- */
  336. char *clish_nspace__get_access(const clish_nspace_t *this)
  337. {
  338. return this->access;
  339. }
  340. /*--------------------------------------------------------- */