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 "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_compare(const void *first, const void *second)
  17. {
  18. const clish_view_t *f = (const clish_view_t *)first;
  19. const clish_view_t *s = (const clish_view_t *)second;
  20. return strcmp(f->name, s->name);
  21. }
  22. /*-------------------------------------------------------- */
  23. static void clish_view_init(clish_view_t * this, const char *name)
  24. {
  25. /* set up defaults */
  26. this->name = lub_string_dup(name);
  27. this->prompt = NULL;
  28. this->depth = 0;
  29. this->restore = CLISH_RESTORE_NONE;
  30. this->access = NULL;
  31. /* initialise the tree of commands for this view */
  32. lub_bintree_init(&this->tree,
  33. clish_command_bt_offset(),
  34. clish_command_bt_compare, clish_command_bt_getkey);
  35. /* Initialise the list of namespaces.
  36. * It's important to add new items to the
  37. * tail of list.
  38. */
  39. this->nspaces = lub_list_new(NULL, clish_nspace_delete);
  40. /* Initialize hotkey structures */
  41. this->hotkeys = clish_hotkeyv_new();
  42. }
  43. /*--------------------------------------------------------- */
  44. static void clish_view_fini(clish_view_t * this)
  45. {
  46. clish_command_t *cmd;
  47. /* delete each command held by this view */
  48. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  49. /* remove the command from the tree */
  50. lub_bintree_remove(&this->tree, cmd);
  51. /* release the instance */
  52. clish_command_delete(cmd);
  53. }
  54. /* Free namespaces list */
  55. lub_list_free_all(this->nspaces);
  56. /* Free hotkey structures */
  57. clish_hotkeyv_delete(this->hotkeys);
  58. /* free our memory */
  59. lub_string_free(this->name);
  60. lub_string_free(this->prompt);
  61. lub_string_free(this->access);
  62. }
  63. /*--------------------------------------------------------- */
  64. clish_view_t *clish_view_new(const char *name)
  65. {
  66. clish_view_t *this = malloc(sizeof(clish_view_t));
  67. if (this)
  68. clish_view_init(this, name);
  69. return this;
  70. }
  71. /*-------------------------------------------------------- */
  72. void clish_view_delete(void *data)
  73. {
  74. clish_view_t *this = (clish_view_t *)data;
  75. clish_view_fini(this);
  76. free(this);
  77. }
  78. /*--------------------------------------------------------- */
  79. clish_command_t *clish_view_new_command(clish_view_t * this,
  80. const char *name, const char *help)
  81. {
  82. /* allocate the memory for a new parameter definition */
  83. clish_command_t *cmd = clish_command_new(name, help);
  84. assert(cmd);
  85. /* do not insert startup/wdog command */
  86. if(!help) {
  87. return cmd;
  88. }
  89. /* try to insert the command */
  90. if(!clish_view_insert_command(this, cmd)) {
  91. /* ignore duplicate */
  92. clish_command_delete(cmd);
  93. cmd = NULL;
  94. }
  95. /* return */
  96. return cmd;
  97. }
  98. /*--------------------------------------------------------- */
  99. bool_t clish_view_insert_command(clish_view_t * this,
  100. clish_command_t *cmd)
  101. {
  102. /* insert command into the binary tree for this view */
  103. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  104. /* duplicate, not inserted */
  105. return BOOL_FALSE;
  106. }
  107. /* set the pview correctly */
  108. clish_command__set_pview(cmd, this);
  109. /* Return */
  110. return BOOL_TRUE;
  111. }
  112. /*--------------------------------------------------------- */
  113. bool_t clish_view_remove_command(clish_view_t * this,
  114. clish_command_t *cmd)
  115. {
  116. void *found = lub_bintree_find(&this->tree, cmd);
  117. /* remove the command from the tree */
  118. lub_bintree_remove(&this->tree, cmd);
  119. /* return true if found */
  120. return found ? BOOL_TRUE : BOOL_FALSE;
  121. }
  122. /*--------------------------------------------------------- */
  123. /* This method identifies the command (if any) which provides
  124. * the longest match with the specified line of text.
  125. *
  126. * NB this comparison is case insensitive.
  127. *
  128. * this - the view instance upon which to operate
  129. * line - the command line to analyse
  130. */
  131. clish_command_t *clish_view_resolve_prefix(clish_view_t * this,
  132. const char *line, bool_t inherit)
  133. {
  134. clish_command_t *result = NULL, *cmd;
  135. char *buffer = NULL;
  136. lub_argv_t *argv;
  137. unsigned i;
  138. /* create a vector of arguments */
  139. argv = lub_argv_new(line, 0);
  140. for (i = 0; i < lub_argv__get_count(argv); i++) {
  141. /* set our buffer to be that of the first "i" arguments */
  142. lub_string_cat(&buffer, lub_argv__get_arg(argv, i));
  143. /* set the result to the longest match */
  144. cmd = clish_view_find_command(this, buffer, inherit);
  145. /* job done */
  146. if (!cmd)
  147. break;
  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 (result) {
  163. clish_action_t *action = clish_command__get_action(result);
  164. clish_config_t *config = clish_command__get_config(result);
  165. if (!clish_action__get_script(action) &&
  166. (!clish_action__get_builtin(action)) &&
  167. (CLISH_CONFIG_NONE == clish_config__get_op(config)) &&
  168. (!clish_command__get_param_count(result)) &&
  169. (!clish_command__get_viewname(result))) {
  170. /* if this doesn't do anything we've
  171. * not resolved a command
  172. */
  173. result = NULL;
  174. }
  175. }
  176. return result;
  177. }
  178. /*--------------------------------------------------------- */
  179. clish_command_t *clish_view_find_command(clish_view_t * this,
  180. const char *name, bool_t inherit)
  181. {
  182. clish_command_t *result = NULL;
  183. /* Search the current view */
  184. result = lub_bintree_find(&this->tree, name);
  185. if (inherit) {
  186. lub_list_node_t *iter;
  187. clish_command_t *cmd;
  188. clish_nspace_t *nspace;
  189. /* Iterate elements. It's important to iterate
  190. * items starting from tail bacause the next
  191. * NAMESPACE has higher priority than previous one
  192. * in a case then the both NAMESPACEs have the
  193. * commands with the same name.
  194. */
  195. for(iter = lub_list__get_tail(this->nspaces);
  196. iter; iter = lub_list_node__get_prev(iter)) {
  197. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  198. cmd = clish_nspace_find_command(nspace, name);
  199. /* Choose the longest match */
  200. result = clish_command_choose_longest(result, cmd);
  201. }
  202. }
  203. return result;
  204. }
  205. /*--------------------------------------------------------- */
  206. static const clish_command_t *find_next_completion(clish_view_t * this,
  207. const char *iter_cmd, const char *line)
  208. {
  209. clish_command_t *cmd;
  210. const char *name = "";
  211. lub_argv_t *largv;
  212. unsigned words;
  213. /* build an argument vector for the line */
  214. largv = lub_argv_new(line, 0);
  215. words = lub_argv__get_count(largv);
  216. /* account for trailing space */
  217. if (!*line || lub_ctype_isspace(line[strlen(line) - 1]))
  218. words++;
  219. if (iter_cmd)
  220. name = iter_cmd;
  221. while ((cmd = lub_bintree_findnext(&this->tree, name))) {
  222. name = clish_command__get_name(cmd);
  223. if (words == lub_string_wordcount(name)) {
  224. /* only bother with commands of which this line is a prefix */
  225. /* this is a completion */
  226. if (lub_string_nocasestr(name, line) == name)
  227. break;
  228. }
  229. }
  230. /* clean up the dynamic memory */
  231. lub_argv_delete(largv);
  232. return cmd;
  233. }
  234. /*--------------------------------------------------------- */
  235. const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
  236. const char *iter_cmd, const char *line,
  237. clish_nspace_visibility_e field, bool_t inherit)
  238. {
  239. const clish_command_t *result, *cmd;
  240. clish_nspace_t *nspace;
  241. lub_list_node_t *iter;
  242. /* ask local view for next command */
  243. result = find_next_completion(this, iter_cmd, line);
  244. if (!inherit)
  245. return result;
  246. /* ask the imported namespaces for next command */
  247. /* Iterate elements */
  248. for(iter = lub_list__get_tail(this->nspaces);
  249. iter; iter = lub_list_node__get_prev(iter)) {
  250. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  251. if (!clish_nspace__get_visibility(nspace, field))
  252. continue;
  253. cmd = clish_nspace_find_next_completion(nspace,
  254. iter_cmd, line, field);
  255. if (clish_command_diff(result, cmd) > 0)
  256. result = cmd;
  257. }
  258. return result;
  259. }
  260. /*--------------------------------------------------------- */
  261. void clish_view_insert_nspace(clish_view_t * this, clish_nspace_t * nspace)
  262. {
  263. lub_list_add(this->nspaces, nspace);
  264. clish_nspace__set_pview(nspace, this);
  265. }
  266. /*--------------------------------------------------------- */
  267. void clish_view_clean_proxy(clish_view_t * this)
  268. {
  269. lub_list_node_t *iter;
  270. /* Iterate elements */
  271. for(iter = lub_list__get_head(this->nspaces);
  272. iter; iter = lub_list_node__get_next(iter)) {
  273. clish_nspace_clean_proxy((clish_nspace_t *)lub_list_node__get_data(iter));
  274. }
  275. }
  276. /*--------------------------------------------------------- */
  277. int clish_view_insert_hotkey(const clish_view_t *this, const char *key, const char *cmd)
  278. {
  279. return clish_hotkeyv_insert(this->hotkeys, key, cmd);
  280. }
  281. /*--------------------------------------------------------- */
  282. const char *clish_view_find_hotkey(const clish_view_t *this, int code)
  283. {
  284. return clish_hotkeyv_cmd_by_code(this->hotkeys, code);
  285. }
  286. CLISH_GET(view, lub_list_t *, nspaces);
  287. CLISH_GET_STR(view, name);
  288. CLISH_SET_STR_ONCE(view, prompt);
  289. CLISH_GET_STR(view, prompt);
  290. CLISH_SET_STR(view, access);
  291. CLISH_GET_STR(view, access);
  292. CLISH_SET(view, unsigned int, depth);
  293. CLISH_GET(view, unsigned int, depth);
  294. CLISH_SET(view, clish_view_restore_e, restore);
  295. CLISH_GET(view, clish_view_restore_e, restore);
  296. /*-------------------------------------------------------- */
  297. lub_bintree_t * clish_view__get_tree(clish_view_t *inst)
  298. {
  299. assert(inst);
  300. return &inst->tree;
  301. }
  302. /*--------------------------------------------------------- */
  303. static const char *restore_names[] = {
  304. "none",
  305. "depth",
  306. "view",
  307. };
  308. /*--------------------------------------------------------- */
  309. const char *clish_view_restore__get_name(clish_view_restore_e restore)
  310. {
  311. if (restore >= CLISH_RESTORE_MAX)
  312. return NULL;
  313. return restore_names[restore];
  314. }
  315. /*--------------------------------------------------------- */
  316. clish_view_restore_e clish_view_restore_resolve(const char *name)
  317. {
  318. clish_view_restore_e result = CLISH_RESTORE_NONE;
  319. unsigned int i = 0;
  320. if (!name)
  321. return CLISH_RESTORE_NONE;
  322. for (i = 0; i < CLISH_RESTORE_MAX; i++) {
  323. if (0 == strcmp(name, restore_names[i])) {
  324. result = (clish_view_restore_e)i;
  325. break;
  326. }
  327. }
  328. return result;
  329. }