view.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * view.c
  3. *
  4. * This file provides the implementation of a view class
  5. */
  6. #include "private.h"
  7. #include "clish/variable.h"
  8. #include "lub/argv.h"
  9. #include "lub/string.h"
  10. #include "lub/ctype.h"
  11. #include <assert.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. /*---------------------------------------------------------
  16. * PRIVATE META FUNCTIONS
  17. *--------------------------------------------------------- */
  18. int clish_view_bt_compare(const void *clientnode, const void *clientkey)
  19. {
  20. const clish_view_t *this = clientnode;
  21. const char *key = clientkey;
  22. return strcmp(this->name, key);
  23. }
  24. /*-------------------------------------------------------- */
  25. void clish_view_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  26. {
  27. const clish_view_t *this = clientnode;
  28. /* fill out the opaque key */
  29. strcpy((char *)key, this->name);
  30. }
  31. /*---------------------------------------------------------
  32. * PRIVATE METHODS
  33. *--------------------------------------------------------- */
  34. static void clish_view_init(clish_view_t * this, const char *name, const char *prompt,
  35. clish_var_expand_fn_t *fn)
  36. {
  37. /* set up defaults */
  38. this->name = lub_string_dup(name);
  39. this->prompt = NULL;
  40. this->nspacec = 0;
  41. this->nspacev = NULL;
  42. this->depth = 0;
  43. this->restore = CLISH_RESTORE_NONE;
  44. this->var_expand_fn = fn ? fn : clish_var_expand_default;
  45. /* Be a good binary tree citizen */
  46. lub_bintree_node_init(&this->bt_node);
  47. /* initialise the tree of commands for this view */
  48. lub_bintree_init(&this->tree,
  49. clish_command_bt_offset(),
  50. clish_command_bt_compare, clish_command_bt_getkey);
  51. /* set up the defaults */
  52. clish_view__set_prompt(this, prompt);
  53. }
  54. /*--------------------------------------------------------- */
  55. static void clish_view_fini(clish_view_t * this)
  56. {
  57. clish_command_t *cmd;
  58. unsigned i;
  59. /* delete each command held by this view */
  60. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  61. /* remove the command from the tree */
  62. lub_bintree_remove(&this->tree, cmd);
  63. /* release the instance */
  64. clish_command_delete(cmd);
  65. }
  66. /* free our memory */
  67. lub_string_free(this->name);
  68. this->name = NULL;
  69. lub_string_free(this->prompt);
  70. this->prompt = NULL;
  71. /* finalize each of the namespace instances */
  72. for (i = 0; i < this->nspacec; i++) {
  73. clish_nspace_delete(this->nspacev[i]);
  74. }
  75. /* free the namespace vector */
  76. free(this->nspacev);
  77. this->nspacec = 0;
  78. this->nspacev = NULL;
  79. }
  80. /*---------------------------------------------------------
  81. * PUBLIC META FUNCTIONS
  82. *--------------------------------------------------------- */
  83. size_t clish_view_bt_offset(void)
  84. {
  85. return offsetof(clish_view_t, bt_node);
  86. }
  87. /*--------------------------------------------------------- */
  88. clish_view_t *clish_view_new(const char *name, const char *prompt,
  89. clish_var_expand_fn_t *fn)
  90. {
  91. clish_view_t *this = malloc(sizeof(clish_view_t));
  92. if (this)
  93. clish_view_init(this, name, prompt, fn);
  94. return this;
  95. }
  96. /*---------------------------------------------------------
  97. * PUBLIC METHODS
  98. *--------------------------------------------------------- */
  99. void clish_view_delete(clish_view_t * this)
  100. {
  101. clish_view_fini(this);
  102. free(this);
  103. }
  104. /*--------------------------------------------------------- */
  105. clish_command_t *clish_view_new_command(clish_view_t * this,
  106. const char *name, const char *help)
  107. {
  108. /* allocate the memory for a new parameter definition */
  109. clish_command_t *cmd = clish_command_new(name, help,
  110. this->var_expand_fn);
  111. assert(cmd);
  112. /* if this is a command other than the startup command... */
  113. if (NULL != help) {
  114. /* ...insert it into the binary tree for this view */
  115. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  116. /* inserting a duplicate command is bad */
  117. clish_command_delete(cmd);
  118. cmd = NULL;
  119. }
  120. }
  121. return cmd;
  122. }
  123. /*--------------------------------------------------------- */
  124. /* This method identifies the command (if any) which provides
  125. * the longest match with the specified line of text.
  126. *
  127. * NB this comparison is case insensitive.
  128. *
  129. * this - the view instance upon which to operate
  130. * line - the command line to analyse
  131. */
  132. clish_command_t *clish_view_resolve_prefix(clish_view_t * this,
  133. const char *line, bool_t inherit)
  134. {
  135. clish_command_t *result = NULL, *cmd;
  136. char *buffer = NULL;
  137. lub_argv_t *argv;
  138. unsigned i;
  139. /* create a vector of arguments */
  140. argv = lub_argv_new(line, 0);
  141. for (i = 0; i < lub_argv__get_count(argv); i++) {
  142. /* set our buffer to be that of the first "i" arguments */
  143. lub_string_cat(&buffer, lub_argv__get_arg(argv, i));
  144. /* set the result to the longest match */
  145. cmd = clish_view_find_command(this, buffer, inherit);
  146. /* job done */
  147. if (!cmd)
  148. break;
  149. result = cmd;
  150. /* ready for the next word */
  151. lub_string_cat(&buffer, " ");
  152. }
  153. /* free up our dynamic storage */
  154. lub_string_free(buffer);
  155. lub_argv_delete(argv);
  156. return result;
  157. }
  158. /*--------------------------------------------------------- */
  159. clish_command_t *clish_view_resolve_command(clish_view_t *this,
  160. const char *line, bool_t inherit)
  161. {
  162. clish_command_t *result = clish_view_resolve_prefix(this, line, inherit);
  163. if (result) {
  164. clish_action_t *action = clish_command__get_action(result);
  165. if (!clish_action__get_script(action) &&
  166. (!clish_action__get_builtin(action)) &&
  167. (CLISH_CONFIG_NONE == clish_command__get_cfg_op(result)) &&
  168. (!clish_command__get_view(result))) {
  169. /* if this doesn't do anything we've
  170. * not resolved a command
  171. */
  172. result = NULL;
  173. }
  174. }
  175. return result;
  176. }
  177. /*--------------------------------------------------------- */
  178. clish_command_t *clish_view_find_command(clish_view_t * this,
  179. const char *name, bool_t inherit)
  180. {
  181. clish_command_t *cmd, *result = NULL;
  182. clish_nspace_t *nspace;
  183. unsigned cnt = clish_view__get_nspace_count(this);
  184. int i;
  185. /* Search the current view */
  186. result = lub_bintree_find(&this->tree, name);
  187. /* Make command link from command alias */
  188. result = clish_command_alias_to_link(result);
  189. if (inherit) {
  190. for (i = cnt - 1; i >= 0; i--) {
  191. nspace = clish_view__get_nspace(this, i);
  192. cmd = clish_nspace_find_command(nspace, name);
  193. /* choose the longest match */
  194. result = clish_command_choose_longest(result, cmd);
  195. }
  196. }
  197. return result;
  198. }
  199. /*--------------------------------------------------------- */
  200. static const clish_command_t *find_next_completion(clish_view_t * this,
  201. const char *iter_cmd, const char *line)
  202. {
  203. clish_command_t *cmd;
  204. const char *name = "";
  205. lub_argv_t *largv;
  206. unsigned words;
  207. /* build an argument vector for the line */
  208. largv = lub_argv_new(line, 0);
  209. words = lub_argv__get_count(largv);
  210. /* account for trailing space */
  211. if (!*line || lub_ctype_isspace(line[strlen(line) - 1]))
  212. words++;
  213. if (iter_cmd)
  214. name = iter_cmd;
  215. while ((cmd = lub_bintree_findnext(&this->tree, name))) {
  216. /* Make command link from command alias */
  217. cmd = clish_command_alias_to_link(cmd);
  218. name = clish_command__get_name(cmd);
  219. if (words == lub_argv_wordcount(name)) {
  220. /* only bother with commands of which this line is a prefix */
  221. /* this is a completion */
  222. if (lub_string_nocasestr(name, line) == name)
  223. break;
  224. }
  225. }
  226. /* clean up the dynamic memory */
  227. lub_argv_delete(largv);
  228. return cmd;
  229. }
  230. /*--------------------------------------------------------- */
  231. const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
  232. const char *iter_cmd, const char *line,
  233. clish_nspace_visibility_t field, bool_t inherit)
  234. {
  235. const clish_command_t *result, *cmd;
  236. clish_nspace_t *nspace;
  237. unsigned cnt = clish_view__get_nspace_count(this);
  238. int i;
  239. /* ask local view for next command */
  240. result = find_next_completion(this, iter_cmd, line);
  241. if (!inherit)
  242. return result;
  243. /* ask the imported namespaces for next command */
  244. for (i = cnt - 1; i >= 0; i--) {
  245. nspace = clish_view__get_nspace(this, i);
  246. if (!clish_nspace__get_visibility(nspace, field))
  247. continue;
  248. cmd = clish_nspace_find_next_completion(nspace,
  249. iter_cmd, line, field);
  250. if (clish_command_diff(result, cmd) > 0)
  251. result = cmd;
  252. }
  253. return result;
  254. }
  255. /*--------------------------------------------------------- */
  256. void clish_view_insert_nspace(clish_view_t * this, clish_nspace_t * nspace)
  257. {
  258. size_t new_size = ((this->nspacec + 1) * sizeof(clish_nspace_t *));
  259. clish_nspace_t **tmp;
  260. /* resize the namespace vector */
  261. tmp = realloc(this->nspacev, new_size);
  262. assert(tmp);
  263. this->nspacev = tmp;
  264. /* insert reference to the namespace */
  265. this->nspacev[this->nspacec++] = nspace;
  266. }
  267. /*--------------------------------------------------------- */
  268. void clish_view_clean_proxy(clish_view_t * this)
  269. {
  270. int i;
  271. /* Iterate namespace instances */
  272. for (i = 0; i < this->nspacec; i++) {
  273. clish_nspace_clean_proxy(this->nspacev[i]);
  274. }
  275. }
  276. /*---------------------------------------------------------
  277. * PUBLIC ATTRIBUTES
  278. *--------------------------------------------------------- */
  279. const char *clish_view__get_name(const clish_view_t * this)
  280. {
  281. return this->name;
  282. }
  283. /*--------------------------------------------------------- */
  284. void clish_view__set_prompt(clish_view_t * this, const char *prompt)
  285. {
  286. assert(!this->prompt);
  287. this->prompt = lub_string_dup(prompt);
  288. }
  289. /*--------------------------------------------------------- */
  290. char *clish_view__get_prompt(const clish_view_t *this)
  291. {
  292. return this->prompt;
  293. }
  294. /*--------------------------------------------------------- */
  295. clish_nspace_t *clish_view__get_nspace(const clish_view_t * this,
  296. unsigned index)
  297. {
  298. clish_nspace_t *result = NULL;
  299. if (index < this->nspacec) {
  300. result = this->nspacev[index];
  301. }
  302. return result;
  303. }
  304. /*--------------------------------------------------------- */
  305. const unsigned clish_view__get_nspace_count(const clish_view_t * this)
  306. {
  307. return this->nspacec;
  308. }
  309. /*--------------------------------------------------------- */
  310. void clish_view__set_depth(clish_view_t * this, unsigned depth)
  311. {
  312. this->depth = depth;
  313. }
  314. /*--------------------------------------------------------- */
  315. unsigned clish_view__get_depth(const clish_view_t * this)
  316. {
  317. return this->depth;
  318. }
  319. /*--------------------------------------------------------- */
  320. void clish_view__set_restore(clish_view_t * this,
  321. clish_view_restore_t restore)
  322. {
  323. this->restore = restore;
  324. }
  325. /*--------------------------------------------------------- */
  326. clish_view_restore_t clish_view__get_restore(const clish_view_t * this)
  327. {
  328. return this->restore;
  329. }
  330. /*--------------------------------------------------------- */