view.c 11 KB

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