1
0

view.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 "lub/list.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. {
  36. /* set up defaults */
  37. this->name = lub_string_dup(name);
  38. this->prompt = NULL;
  39. this->depth = 0;
  40. this->restore = CLISH_RESTORE_NONE;
  41. this->access = NULL;
  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. /* Initialise the list of namespaces.
  49. * It's important to add new items to the
  50. * tail of list.
  51. */
  52. this->nspaces = lub_list_new(NULL);
  53. /* set up the defaults */
  54. clish_view__set_prompt(this, prompt);
  55. /* Initialize hotkey structures */
  56. this->hotkeys = clish_hotkeyv_new();
  57. }
  58. /*--------------------------------------------------------- */
  59. static void clish_view_fini(clish_view_t * this)
  60. {
  61. clish_command_t *cmd;
  62. lub_list_node_t *iter;
  63. clish_nspace_t *nspace;
  64. /* delete each command held by this view */
  65. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  66. /* remove the command from the tree */
  67. lub_bintree_remove(&this->tree, cmd);
  68. /* release the instance */
  69. clish_command_delete(cmd);
  70. }
  71. /* Free namespaces list */
  72. while ((iter = lub_list__get_head(this->nspaces))) {
  73. /* Remove the nspace from the list */
  74. lub_list_del(this->nspaces, iter);
  75. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  76. lub_list_node_free(iter);
  77. /* Free the instance */
  78. clish_nspace_delete(nspace);
  79. }
  80. lub_list_free(this->nspaces);
  81. /* Free hotkey structures */
  82. clish_hotkeyv_delete(this->hotkeys);
  83. /* free our memory */
  84. lub_string_free(this->name);
  85. lub_string_free(this->prompt);
  86. lub_string_free(this->access);
  87. }
  88. /*---------------------------------------------------------
  89. * PUBLIC META FUNCTIONS
  90. *--------------------------------------------------------- */
  91. size_t clish_view_bt_offset(void)
  92. {
  93. return offsetof(clish_view_t, bt_node);
  94. }
  95. /*--------------------------------------------------------- */
  96. clish_view_t *clish_view_new(const char *name, const char *prompt)
  97. {
  98. clish_view_t *this = malloc(sizeof(clish_view_t));
  99. if (this)
  100. clish_view_init(this, name, prompt);
  101. return this;
  102. }
  103. /*---------------------------------------------------------
  104. * PUBLIC METHODS
  105. *--------------------------------------------------------- */
  106. void clish_view_delete(clish_view_t * this)
  107. {
  108. clish_view_fini(this);
  109. free(this);
  110. }
  111. /*--------------------------------------------------------- */
  112. clish_command_t *clish_view_new_command(clish_view_t * this,
  113. const char *name, const char *help)
  114. {
  115. /* allocate the memory for a new parameter definition */
  116. clish_command_t *cmd = clish_command_new(name, help);
  117. assert(cmd);
  118. /* if this is a command other than the startup command... */
  119. if (NULL != help) {
  120. /* ...insert it into the binary tree for this view */
  121. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  122. /* inserting a duplicate command is bad */
  123. clish_command_delete(cmd);
  124. cmd = NULL;
  125. }
  126. }
  127. return cmd;
  128. }
  129. /*--------------------------------------------------------- */
  130. /* This method identifies the command (if any) which provides
  131. * the longest match with the specified line of text.
  132. *
  133. * NB this comparison is case insensitive.
  134. *
  135. * this - the view instance upon which to operate
  136. * line - the command line to analyse
  137. */
  138. clish_command_t *clish_view_resolve_prefix(clish_view_t * this,
  139. const char *line, bool_t inherit)
  140. {
  141. clish_command_t *result = NULL, *cmd;
  142. char *buffer = NULL;
  143. lub_argv_t *argv;
  144. unsigned i;
  145. /* create a vector of arguments */
  146. argv = lub_argv_new(line, 0);
  147. for (i = 0; i < lub_argv__get_count(argv); i++) {
  148. /* set our buffer to be that of the first "i" arguments */
  149. lub_string_cat(&buffer, lub_argv__get_arg(argv, i));
  150. /* set the result to the longest match */
  151. cmd = clish_view_find_command(this, buffer, inherit);
  152. /* job done */
  153. if (!cmd)
  154. break;
  155. result = cmd;
  156. /* ready for the next word */
  157. lub_string_cat(&buffer, " ");
  158. }
  159. /* free up our dynamic storage */
  160. lub_string_free(buffer);
  161. lub_argv_delete(argv);
  162. return result;
  163. }
  164. /*--------------------------------------------------------- */
  165. clish_command_t *clish_view_resolve_command(clish_view_t *this,
  166. const char *line, bool_t inherit)
  167. {
  168. clish_command_t *result = clish_view_resolve_prefix(this, line, inherit);
  169. if (result) {
  170. clish_action_t *action = clish_command__get_action(result);
  171. clish_config_t *config = clish_command__get_config(result);
  172. if (!clish_action__get_script(action) &&
  173. (!clish_action__get_builtin(action)) &&
  174. (CLISH_CONFIG_NONE == clish_config__get_op(config)) &&
  175. (!clish_command__get_param_count(result)) &&
  176. (!clish_command__get_viewname(result))) {
  177. /* if this doesn't do anything we've
  178. * not resolved a command
  179. */
  180. result = NULL;
  181. }
  182. }
  183. return result;
  184. }
  185. /*--------------------------------------------------------- */
  186. clish_command_t *clish_view_find_command(clish_view_t * this,
  187. const char *name, bool_t inherit)
  188. {
  189. clish_command_t *result = NULL;
  190. /* Search the current view */
  191. result = lub_bintree_find(&this->tree, name);
  192. if (inherit) {
  193. lub_list_node_t *iter;
  194. clish_command_t *cmd;
  195. clish_nspace_t *nspace;
  196. /* Iterate elements. It's important to iterate
  197. * items starting from tail bacause the next
  198. * NAMESPACE has higher priority than previous one
  199. * in a case then the both NAMESPACEs have the
  200. * commands with the same name.
  201. */
  202. for(iter = lub_list__get_tail(this->nspaces);
  203. iter; iter = lub_list_node__get_prev(iter)) {
  204. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  205. cmd = clish_nspace_find_command(nspace, name);
  206. /* Choose the longest match */
  207. result = clish_command_choose_longest(result, cmd);
  208. }
  209. }
  210. return result;
  211. }
  212. /*--------------------------------------------------------- */
  213. static const clish_command_t *find_next_completion(clish_view_t * this,
  214. const char *iter_cmd, const char *line)
  215. {
  216. clish_command_t *cmd;
  217. const char *name = "";
  218. lub_argv_t *largv;
  219. unsigned words;
  220. /* build an argument vector for the line */
  221. largv = lub_argv_new(line, 0);
  222. words = lub_argv__get_count(largv);
  223. /* account for trailing space */
  224. if (!*line || lub_ctype_isspace(line[strlen(line) - 1]))
  225. words++;
  226. if (iter_cmd)
  227. name = iter_cmd;
  228. while ((cmd = lub_bintree_findnext(&this->tree, name))) {
  229. name = clish_command__get_name(cmd);
  230. if (words == lub_string_wordcount(name)) {
  231. /* only bother with commands of which this line is a prefix */
  232. /* this is a completion */
  233. if (lub_string_nocasestr(name, line) == name)
  234. break;
  235. }
  236. }
  237. /* clean up the dynamic memory */
  238. lub_argv_delete(largv);
  239. return cmd;
  240. }
  241. /*--------------------------------------------------------- */
  242. const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
  243. const char *iter_cmd, const char *line,
  244. clish_nspace_visibility_t field, bool_t inherit)
  245. {
  246. const clish_command_t *result, *cmd;
  247. clish_nspace_t *nspace;
  248. lub_list_node_t *iter;
  249. /* ask local view for next command */
  250. result = find_next_completion(this, iter_cmd, line);
  251. if (!inherit)
  252. return result;
  253. /* ask the imported namespaces for next command */
  254. /* Iterate elements */
  255. for(iter = lub_list__get_tail(this->nspaces);
  256. iter; iter = lub_list_node__get_prev(iter)) {
  257. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  258. if (!clish_nspace__get_visibility(nspace, field))
  259. continue;
  260. cmd = clish_nspace_find_next_completion(nspace,
  261. iter_cmd, line, field);
  262. if (clish_command_diff(result, cmd) > 0)
  263. result = cmd;
  264. }
  265. return result;
  266. }
  267. /*--------------------------------------------------------- */
  268. void clish_view_insert_nspace(clish_view_t * this, clish_nspace_t * nspace)
  269. {
  270. lub_list_add(this->nspaces, nspace);
  271. }
  272. /*--------------------------------------------------------- */
  273. void clish_view_clean_proxy(clish_view_t * this)
  274. {
  275. lub_list_node_t *iter;
  276. /* Iterate elements */
  277. for(iter = lub_list__get_head(this->nspaces);
  278. iter; iter = lub_list_node__get_next(iter)) {
  279. clish_nspace_clean_proxy((clish_nspace_t *)lub_list_node__get_data(iter));
  280. }
  281. }
  282. /*---------------------------------------------------------
  283. * PUBLIC ATTRIBUTES
  284. *--------------------------------------------------------- */
  285. lub_bintree_t * clish_view__get_command_tree(clish_view_t *this)
  286. {
  287. return &this->tree;
  288. }
  289. /*--------------------------------------------------------- */
  290. lub_list_t * clish_view__get_nspace_tree(clish_view_t *this)
  291. {
  292. return this->nspaces;
  293. }
  294. /*--------------------------------------------------------- */
  295. const char *clish_view__get_name(const clish_view_t * this)
  296. {
  297. return this->name;
  298. }
  299. /*--------------------------------------------------------- */
  300. void clish_view__set_prompt(clish_view_t * this, const char *prompt)
  301. {
  302. assert(!this->prompt);
  303. this->prompt = lub_string_dup(prompt);
  304. }
  305. /*--------------------------------------------------------- */
  306. char *clish_view__get_prompt(const clish_view_t *this)
  307. {
  308. return this->prompt;
  309. }
  310. /*--------------------------------------------------------- */
  311. void clish_view__set_depth(clish_view_t * this, unsigned depth)
  312. {
  313. this->depth = depth;
  314. }
  315. /*--------------------------------------------------------- */
  316. unsigned clish_view__get_depth(const clish_view_t * this)
  317. {
  318. return this->depth;
  319. }
  320. /*--------------------------------------------------------- */
  321. void clish_view__set_restore(clish_view_t * this,
  322. clish_view_restore_t restore)
  323. {
  324. this->restore = restore;
  325. }
  326. /*--------------------------------------------------------- */
  327. clish_view_restore_t clish_view__get_restore(const clish_view_t * this)
  328. {
  329. return this->restore;
  330. }
  331. /*--------------------------------------------------------- */
  332. int clish_view_insert_hotkey(const clish_view_t *this, const char *key, const char *cmd)
  333. {
  334. return clish_hotkeyv_insert(this->hotkeys, key, cmd);
  335. }
  336. /*--------------------------------------------------------- */
  337. const char *clish_view_find_hotkey(const clish_view_t *this, int code)
  338. {
  339. return clish_hotkeyv_cmd_by_code(this->hotkeys, code);
  340. }
  341. /*--------------------------------------------------------- */
  342. void clish_view__set_access(clish_view_t *this, const char *access)
  343. {
  344. if (this->access)
  345. lub_string_free(this->access);
  346. this->access = lub_string_dup(access);
  347. }
  348. /*--------------------------------------------------------- */
  349. char *clish_view__get_access(const clish_view_t *this)
  350. {
  351. return this->access;
  352. }
  353. /*--------------------------------------------------------- */