1
0

view.c 11 KB

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