nspace.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /*---------------------------------------------------------
  15. * PRIVATE METHODS
  16. *--------------------------------------------------------- */
  17. static void clish_nspace_init(clish_nspace_t * this, clish_view_t * view)
  18. {
  19. this->view = view;
  20. /* set up defaults */
  21. this->prefix = NULL;
  22. this->help = BOOL_FALSE;
  23. this->completion = BOOL_TRUE;
  24. this->context_help = BOOL_FALSE;
  25. this->inherit = BOOL_TRUE;
  26. this->prefix_cmd = NULL;
  27. this->proxy_cmd = NULL;
  28. }
  29. /*--------------------------------------------------------- */
  30. static void clish_nspace_fini(clish_nspace_t * this)
  31. {
  32. /* deallocate the memory for this instance */
  33. lub_string_free(this->prefix);
  34. this->prefix = NULL;
  35. if (this->proxy_cmd) {
  36. clish_command_delete(this->proxy_cmd);
  37. this->proxy_cmd = NULL;
  38. }
  39. if (this->prefix_cmd) {
  40. clish_command_delete(this->prefix_cmd);
  41. this->prefix_cmd = NULL;
  42. }
  43. }
  44. /*--------------------------------------------------------- */
  45. clish_command_t * clish_nspace_create_prefix_cmd(clish_nspace_t * this,
  46. const char * name, const char * help)
  47. {
  48. if (this->prefix_cmd) {
  49. clish_command_delete(this->prefix_cmd);
  50. this->prefix_cmd = NULL;
  51. }
  52. return (this->prefix_cmd = clish_command_new(name, help));
  53. }
  54. /*--------------------------------------------------------- */
  55. static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
  56. const char *prefix, const clish_command_t * ref)
  57. {
  58. char *name = NULL;
  59. if (this->proxy_cmd) {
  60. clish_command_delete(this->proxy_cmd);
  61. this->proxy_cmd = NULL;
  62. }
  63. assert(prefix);
  64. if (!ref) {
  65. assert(this->prefix_cmd);
  66. this->proxy_cmd = clish_command_new_link(prefix, this->prefix_cmd);
  67. return this->proxy_cmd;
  68. }
  69. lub_string_catn(&name, prefix, strlen(prefix));
  70. lub_string_catn(&name, " ", 1);
  71. lub_string_catn(&name, clish_command__get_name(ref),
  72. strlen(clish_command__get_name(ref)));
  73. this->proxy_cmd = clish_command_new_link(name, ref);
  74. free(name);
  75. return this->proxy_cmd;
  76. }
  77. /*---------------------------------------------------------
  78. * PUBLIC META FUNCTIONS
  79. *--------------------------------------------------------- */
  80. clish_nspace_t *clish_nspace_new(clish_view_t * view)
  81. {
  82. clish_nspace_t *this = malloc(sizeof(clish_nspace_t));
  83. if (this) {
  84. clish_nspace_init(this, view);
  85. }
  86. return this;
  87. }
  88. /*---------------------------------------------------------
  89. * PUBLIC METHODS
  90. *--------------------------------------------------------- */
  91. void clish_nspace_delete(clish_nspace_t * this)
  92. {
  93. clish_nspace_fini(this);
  94. free(this);
  95. }
  96. /*--------------------------------------------------------- */
  97. static const char *clish_nspace_after_prefix(const char *prefix,
  98. const char *line)
  99. {
  100. const char *in_line = NULL;
  101. regex_t regexp;
  102. regmatch_t pmatch[1];
  103. int res;
  104. if (!line)
  105. return NULL;
  106. /* Compile regular expression */
  107. regcomp(&regexp, prefix, REG_EXTENDED | REG_ICASE);
  108. res = regexec(&regexp, line, 1, pmatch, 0);
  109. regfree(&regexp);
  110. if (res || (-1 == pmatch[0].rm_so))
  111. return NULL;
  112. in_line = line + pmatch[0].rm_eo;
  113. /* If entered line contain only the prefix */
  114. if (in_line[0] == '\0')
  115. return NULL;
  116. /* If prefix is not followed by space */
  117. if (in_line[0] != ' ')
  118. return NULL;
  119. return (in_line + 1);
  120. }
  121. /*--------------------------------------------------------- */
  122. clish_command_t *clish_nspace_find_command(clish_nspace_t * this, const char *name)
  123. {
  124. clish_command_t *cmd = NULL, *cmd_link = NULL;
  125. clish_view_t *view = clish_nspace__get_view(this);
  126. const char *prefix = clish_nspace__get_prefix(this);
  127. const char *in_line;
  128. if (!prefix)
  129. return clish_view_find_command(view, name, this->inherit);
  130. if (!(in_line = clish_nspace_after_prefix(prefix, name)))
  131. return NULL;
  132. cmd = clish_view_find_command(view, in_line, this->inherit);
  133. return clish_nspace_find_create_command(this, prefix, cmd);
  134. }
  135. /*--------------------------------------------------------- */
  136. const clish_command_t *clish_nspace_find_next_completion(clish_nspace_t * this,
  137. const char *iter_cmd, const char *line, clish_nspace_visibility_t field)
  138. {
  139. const clish_command_t *cmd = NULL, *cmd_link = NULL;
  140. clish_view_t *view = clish_nspace__get_view(this);
  141. const char *prefix = clish_nspace__get_prefix(this);
  142. const char *in_iter = "";
  143. const char *in_line;
  144. if (!prefix)
  145. return clish_view_find_next_completion(view, iter_cmd, line, field, this->inherit);
  146. if (!(in_line = clish_nspace_after_prefix(prefix, line)))
  147. return NULL;
  148. if (iter_cmd &&
  149. (lub_string_nocasestr(iter_cmd, prefix) == iter_cmd) &&
  150. (lub_string_nocasecmp(iter_cmd, prefix)))
  151. in_iter = iter_cmd + strlen(prefix) + 1;
  152. cmd = clish_view_find_next_completion(view, in_iter, in_line, field, this->inherit);
  153. if (!cmd)
  154. return NULL;
  155. return clish_nspace_find_create_command(this, prefix, cmd);
  156. }
  157. /*---------------------------------------------------------
  158. * PUBLIC ATTRIBUTES
  159. *--------------------------------------------------------- */
  160. clish_view_t *clish_nspace__get_view(const clish_nspace_t * this)
  161. {
  162. return this->view;
  163. }
  164. /*--------------------------------------------------------- */
  165. void clish_nspace__set_prefix(clish_nspace_t * this, const char *prefix)
  166. {
  167. assert(NULL == this->prefix);
  168. this->prefix = lub_string_dup(prefix);
  169. }
  170. /*--------------------------------------------------------- */
  171. const char *clish_nspace__get_prefix(const clish_nspace_t * this)
  172. {
  173. return this->prefix;
  174. }
  175. /*--------------------------------------------------------- */
  176. void clish_nspace__set_help(clish_nspace_t * this, bool_t help)
  177. {
  178. this->help = help;
  179. }
  180. /*--------------------------------------------------------- */
  181. bool_t clish_nspace__get_help(const clish_nspace_t * this)
  182. {
  183. return this->help;
  184. }
  185. /*--------------------------------------------------------- */
  186. void clish_nspace__set_completion(clish_nspace_t * this, bool_t completion)
  187. {
  188. this->completion = completion;
  189. }
  190. /*--------------------------------------------------------- */
  191. bool_t clish_nspace__get_completion(const clish_nspace_t * this)
  192. {
  193. return this->completion;
  194. }
  195. /*--------------------------------------------------------- */
  196. void clish_nspace__set_context_help(clish_nspace_t * this, bool_t context_help)
  197. {
  198. this->context_help = context_help;
  199. }
  200. /*--------------------------------------------------------- */
  201. bool_t clish_nspace__get_context_help(const clish_nspace_t * this)
  202. {
  203. return this->context_help;
  204. }
  205. /*--------------------------------------------------------- */
  206. void clish_nspace__set_inherit(clish_nspace_t * this, bool_t inherit)
  207. {
  208. this->inherit = inherit;
  209. }
  210. /*--------------------------------------------------------- */
  211. bool_t clish_nspace__get_inherit(const clish_nspace_t * this)
  212. {
  213. return this->inherit;
  214. }
  215. /*--------------------------------------------------------- */
  216. bool_t
  217. clish_nspace__get_visibility(const clish_nspace_t * instance,
  218. clish_nspace_visibility_t field)
  219. {
  220. bool_t result = BOOL_FALSE;
  221. switch (field) {
  222. case CLISH_NSPACE_HELP:
  223. result = clish_nspace__get_help(instance);
  224. break;
  225. case CLISH_NSPACE_COMPLETION:
  226. result = clish_nspace__get_completion(instance);
  227. break;
  228. case CLISH_NSPACE_CHELP:
  229. result = clish_nspace__get_context_help(instance);
  230. break;
  231. }
  232. return result;
  233. }
  234. /*--------------------------------------------------------- */