nspace.c 11 KB

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