view.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. int clish_view_bt_compare(const void *clientnode, const void *clientkey)
  17. {
  18. const clish_view_t *this = clientnode;
  19. const char *key = clientkey;
  20. return strcmp(this->name, key);
  21. }
  22. /*-------------------------------------------------------- */
  23. void clish_view_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  24. {
  25. const clish_view_t *this = clientnode;
  26. /* fill out the opaque key */
  27. strcpy((char *)key, this->name);
  28. }
  29. /*-------------------------------------------------------- */
  30. static void clish_view_init(clish_view_t * this, const char *name, const char *prompt)
  31. {
  32. /* set up defaults */
  33. this->name = lub_string_dup(name);
  34. this->prompt = NULL;
  35. this->depth = 0;
  36. this->restore = CLISH_RESTORE_NONE;
  37. this->access = NULL;
  38. /* Be a good binary tree citizen */
  39. lub_bintree_node_init(&this->bt_node);
  40. /* initialise the tree of commands for this view */
  41. lub_bintree_init(&this->tree,
  42. clish_command_bt_offset(),
  43. clish_command_bt_compare, clish_command_bt_getkey);
  44. /* Initialise the list of namespaces.
  45. * It's important to add new items to the
  46. * tail of list.
  47. */
  48. this->nspaces = lub_list_new(NULL);
  49. /* set up the defaults */
  50. clish_view__set_prompt(this, prompt);
  51. /* Initialize hotkey structures */
  52. this->hotkeys = clish_hotkeyv_new();
  53. }
  54. /*--------------------------------------------------------- */
  55. static void clish_view_fini(clish_view_t * this)
  56. {
  57. clish_command_t *cmd;
  58. lub_list_node_t *iter;
  59. clish_nspace_t *nspace;
  60. /* delete each command held by this view */
  61. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  62. /* remove the command from the tree */
  63. lub_bintree_remove(&this->tree, cmd);
  64. /* release the instance */
  65. clish_command_delete(cmd);
  66. }
  67. /* Free namespaces list */
  68. while ((iter = lub_list__get_head(this->nspaces))) {
  69. /* Remove the nspace from the list */
  70. lub_list_del(this->nspaces, iter);
  71. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  72. lub_list_node_free(iter);
  73. /* Free the instance */
  74. clish_nspace_delete(nspace);
  75. }
  76. lub_list_free(this->nspaces);
  77. /* Free hotkey structures */
  78. clish_hotkeyv_delete(this->hotkeys);
  79. /* free our memory */
  80. lub_string_free(this->name);
  81. lub_string_free(this->prompt);
  82. lub_string_free(this->access);
  83. }
  84. /*-------------------------------------------------------- */
  85. size_t clish_view_bt_offset(void)
  86. {
  87. return offsetof(clish_view_t, bt_node);
  88. }
  89. /*--------------------------------------------------------- */
  90. clish_view_t *clish_view_new(const char *name, const char *prompt)
  91. {
  92. clish_view_t *this = malloc(sizeof(clish_view_t));
  93. if (this)
  94. clish_view_init(this, name, prompt);
  95. return this;
  96. }
  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. /* job done */
  145. if (!cmd)
  146. break;
  147. result = cmd;
  148. /* ready for the next word */
  149. lub_string_cat(&buffer, " ");
  150. }
  151. /* free up our dynamic storage */
  152. lub_string_free(buffer);
  153. lub_argv_delete(argv);
  154. return result;
  155. }
  156. /*--------------------------------------------------------- */
  157. clish_command_t *clish_view_resolve_command(clish_view_t *this,
  158. const char *line, bool_t inherit)
  159. {
  160. clish_command_t *result = clish_view_resolve_prefix(this, line, inherit);
  161. if (result) {
  162. clish_action_t *action = clish_command__get_action(result);
  163. clish_config_t *config = clish_command__get_config(result);
  164. if (!clish_action__get_script(action) &&
  165. (!clish_action__get_builtin(action)) &&
  166. (CLISH_CONFIG_NONE == clish_config__get_op(config)) &&
  167. (!clish_command__get_param_count(result)) &&
  168. (!clish_command__get_viewname(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 *result = NULL;
  182. /* Search the current view */
  183. result = lub_bintree_find(&this->tree, name);
  184. if (inherit) {
  185. lub_list_node_t *iter;
  186. clish_command_t *cmd;
  187. clish_nspace_t *nspace;
  188. /* Iterate elements. It's important to iterate
  189. * items starting from tail bacause the next
  190. * NAMESPACE has higher priority than previous one
  191. * in a case then the both NAMESPACEs have the
  192. * commands with the same name.
  193. */
  194. for(iter = lub_list__get_tail(this->nspaces);
  195. iter; iter = lub_list_node__get_prev(iter)) {
  196. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  197. cmd = clish_nspace_find_command(nspace, name);
  198. /* Choose the longest match */
  199. result = clish_command_choose_longest(result, cmd);
  200. }
  201. }
  202. return result;
  203. }
  204. /*--------------------------------------------------------- */
  205. static const clish_command_t *find_next_completion(clish_view_t * this,
  206. const char *iter_cmd, const char *line)
  207. {
  208. clish_command_t *cmd;
  209. const char *name = "";
  210. lub_argv_t *largv;
  211. unsigned words;
  212. /* build an argument vector for the line */
  213. largv = lub_argv_new(line, 0);
  214. words = lub_argv__get_count(largv);
  215. /* account for trailing space */
  216. if (!*line || lub_ctype_isspace(line[strlen(line) - 1]))
  217. words++;
  218. if (iter_cmd)
  219. name = iter_cmd;
  220. while ((cmd = lub_bintree_findnext(&this->tree, name))) {
  221. name = clish_command__get_name(cmd);
  222. if (words == lub_string_wordcount(name)) {
  223. /* only bother with commands of which this line is a prefix */
  224. /* this is a completion */
  225. if (lub_string_nocasestr(name, line) == name)
  226. break;
  227. }
  228. }
  229. /* clean up the dynamic memory */
  230. lub_argv_delete(largv);
  231. return cmd;
  232. }
  233. /*--------------------------------------------------------- */
  234. const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
  235. const char *iter_cmd, const char *line,
  236. clish_nspace_visibility_e field, bool_t inherit)
  237. {
  238. const clish_command_t *result, *cmd;
  239. clish_nspace_t *nspace;
  240. lub_list_node_t *iter;
  241. /* ask local view for next command */
  242. result = find_next_completion(this, iter_cmd, line);
  243. if (!inherit)
  244. return result;
  245. /* ask the imported namespaces for next command */
  246. /* Iterate elements */
  247. for(iter = lub_list__get_tail(this->nspaces);
  248. iter; iter = lub_list_node__get_prev(iter)) {
  249. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  250. if (!clish_nspace__get_visibility(nspace, field))
  251. continue;
  252. cmd = clish_nspace_find_next_completion(nspace,
  253. iter_cmd, line, field);
  254. if (clish_command_diff(result, cmd) > 0)
  255. result = cmd;
  256. }
  257. return result;
  258. }
  259. /*--------------------------------------------------------- */
  260. void clish_view_insert_nspace(clish_view_t * this, clish_nspace_t * nspace)
  261. {
  262. lub_list_add(this->nspaces, nspace);
  263. }
  264. /*--------------------------------------------------------- */
  265. void clish_view_clean_proxy(clish_view_t * this)
  266. {
  267. lub_list_node_t *iter;
  268. /* Iterate elements */
  269. for(iter = lub_list__get_head(this->nspaces);
  270. iter; iter = lub_list_node__get_next(iter)) {
  271. clish_nspace_clean_proxy((clish_nspace_t *)lub_list_node__get_data(iter));
  272. }
  273. }
  274. /*--------------------------------------------------------- */
  275. int clish_view_insert_hotkey(const clish_view_t *this, const char *key, const char *cmd)
  276. {
  277. return clish_hotkeyv_insert(this->hotkeys, key, cmd);
  278. }
  279. /*--------------------------------------------------------- */
  280. const char *clish_view_find_hotkey(const clish_view_t *this, int code)
  281. {
  282. return clish_hotkeyv_cmd_by_code(this->hotkeys, code);
  283. }
  284. CLISH_GET(view, lub_list_t *, nspaces);
  285. CLISH_GET_STR(view, name);
  286. CLISH_SET_STR_ONCE(view, prompt);
  287. CLISH_GET_STR(view, prompt);
  288. CLISH_SET_STR(view, access);
  289. CLISH_GET_STR(view, access);
  290. CLISH_SET(view, unsigned int, depth);
  291. CLISH_GET(view, unsigned int, depth);
  292. CLISH_SET(view, clish_view_restore_e, restore);
  293. CLISH_GET(view, clish_view_restore_e, restore);
  294. /*-------------------------------------------------------- */
  295. lub_bintree_t * clish_view__get_tree(clish_view_t *inst)
  296. {
  297. assert(inst);
  298. return &inst->tree;
  299. }