view.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. /* try to insert the command */
  86. if(!clish_view_insert_command(this, cmd)) {
  87. /* ignore duplicate */
  88. clish_command_delete(cmd);
  89. cmd = NULL;
  90. }
  91. /* return */
  92. return cmd;
  93. }
  94. /*--------------------------------------------------------- */
  95. bool_t clish_view_insert_command(clish_view_t * this,
  96. clish_command_t *cmd)
  97. {
  98. /* insert command into the binary tree for this view */
  99. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  100. /* duplicate, not inserted */
  101. return BOOL_FALSE;
  102. }
  103. /* set the pview correctly */
  104. clish_command__set_pview(cmd, this);
  105. /* Return */
  106. return BOOL_TRUE;
  107. }
  108. /*--------------------------------------------------------- */
  109. bool_t clish_view_remove_command(clish_view_t * this,
  110. clish_command_t *cmd)
  111. {
  112. void *found = lub_bintree_find(&this->tree, cmd);
  113. /* remove the command from the tree */
  114. lub_bintree_remove(&this->tree, cmd);
  115. /* return true if found */
  116. return found ? BOOL_TRUE : BOOL_FALSE;
  117. }
  118. /*--------------------------------------------------------- */
  119. /* This method identifies the command (if any) which provides
  120. * the longest match with the specified line of text.
  121. *
  122. * NB this comparison is case insensitive.
  123. *
  124. * this - the view instance upon which to operate
  125. * line - the command line to analyse
  126. */
  127. clish_command_t *clish_view_resolve_prefix(clish_view_t * this,
  128. const char *line, bool_t inherit)
  129. {
  130. clish_command_t *result = NULL, *cmd;
  131. char *buffer = NULL;
  132. lub_argv_t *argv;
  133. unsigned i;
  134. /* create a vector of arguments */
  135. argv = lub_argv_new(line, 0);
  136. for (i = 0; i < lub_argv__get_count(argv); i++) {
  137. /* set our buffer to be that of the first "i" arguments */
  138. lub_string_cat(&buffer, lub_argv__get_arg(argv, i));
  139. /* set the result to the longest match */
  140. cmd = clish_view_find_command(this, buffer, inherit);
  141. /* job done */
  142. if (!cmd)
  143. break;
  144. result = cmd;
  145. /* ready for the next word */
  146. lub_string_cat(&buffer, " ");
  147. }
  148. /* free up our dynamic storage */
  149. lub_string_free(buffer);
  150. lub_argv_delete(argv);
  151. return result;
  152. }
  153. /*--------------------------------------------------------- */
  154. clish_command_t *clish_view_resolve_command(clish_view_t *this,
  155. const char *line, bool_t inherit)
  156. {
  157. return clish_view_resolve_prefix(this, line, inherit);
  158. }
  159. /*--------------------------------------------------------- */
  160. clish_command_t *clish_view_find_command(clish_view_t * this,
  161. const char *name, bool_t inherit)
  162. {
  163. clish_command_t *result = NULL;
  164. /* Search the current view */
  165. result = lub_bintree_find(&this->tree, name);
  166. if (inherit) {
  167. lub_list_node_t *iter;
  168. clish_command_t *cmd;
  169. clish_nspace_t *nspace;
  170. /* Iterate elements. It's important to iterate
  171. * items starting from tail bacause the next
  172. * NAMESPACE has higher priority than previous one
  173. * in a case then the both NAMESPACEs have the
  174. * commands with the same name.
  175. */
  176. for(iter = lub_list__get_tail(this->nspaces);
  177. iter; iter = lub_list_node__get_prev(iter)) {
  178. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  179. cmd = clish_nspace_find_command(nspace, name);
  180. /* Choose the longest match */
  181. result = clish_command_choose_longest(result, cmd);
  182. }
  183. }
  184. return result;
  185. }
  186. /*--------------------------------------------------------- */
  187. static const clish_command_t *find_next_completion(clish_view_t * this,
  188. const char *iter_cmd, const char *line)
  189. {
  190. clish_command_t *cmd;
  191. const char *name = "";
  192. lub_argv_t *largv;
  193. unsigned words;
  194. /* build an argument vector for the line */
  195. largv = lub_argv_new(line, 0);
  196. words = lub_argv__get_count(largv);
  197. /* account for trailing space */
  198. if (!*line || lub_ctype_isspace(line[strlen(line) - 1]))
  199. words++;
  200. if (iter_cmd)
  201. name = iter_cmd;
  202. while ((cmd = lub_bintree_findnext(&this->tree, name))) {
  203. name = clish_command__get_name(cmd);
  204. if (words == lub_string_wordcount(name)) {
  205. /* only bother with commands of which this line is a prefix */
  206. /* this is a completion */
  207. if (lub_string_nocasestr(name, line) == name)
  208. break;
  209. }
  210. }
  211. /* clean up the dynamic memory */
  212. lub_argv_delete(largv);
  213. return cmd;
  214. }
  215. /*--------------------------------------------------------- */
  216. const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
  217. const char *iter_cmd, const char *line,
  218. clish_nspace_visibility_e field, bool_t inherit)
  219. {
  220. const clish_command_t *result, *cmd;
  221. clish_nspace_t *nspace;
  222. lub_list_node_t *iter;
  223. /* ask local view for next command */
  224. result = find_next_completion(this, iter_cmd, line);
  225. if (!inherit)
  226. return result;
  227. /* ask the imported namespaces for next command */
  228. /* Iterate elements */
  229. for(iter = lub_list__get_tail(this->nspaces);
  230. iter; iter = lub_list_node__get_prev(iter)) {
  231. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  232. if (!clish_nspace__get_visibility(nspace, field))
  233. continue;
  234. cmd = clish_nspace_find_next_completion(nspace,
  235. iter_cmd, line, field);
  236. if (clish_command_diff(result, cmd) > 0)
  237. result = cmd;
  238. }
  239. return result;
  240. }
  241. /*--------------------------------------------------------- */
  242. void clish_view_insert_nspace(clish_view_t * this, clish_nspace_t * nspace)
  243. {
  244. lub_list_add(this->nspaces, nspace);
  245. clish_nspace__set_pview(nspace, this);
  246. }
  247. /*--------------------------------------------------------- */
  248. void clish_view_clean_proxy(clish_view_t * this)
  249. {
  250. lub_list_node_t *iter;
  251. /* Iterate elements */
  252. for(iter = lub_list__get_head(this->nspaces);
  253. iter; iter = lub_list_node__get_next(iter)) {
  254. clish_nspace_clean_proxy((clish_nspace_t *)lub_list_node__get_data(iter));
  255. }
  256. }
  257. /*--------------------------------------------------------- */
  258. int clish_view_insert_hotkey(const clish_view_t *this, const char *key, const char *cmd)
  259. {
  260. return clish_hotkeyv_insert(this->hotkeys, key, cmd);
  261. }
  262. /*--------------------------------------------------------- */
  263. const char *clish_view_find_hotkey(const clish_view_t *this, int code)
  264. {
  265. return clish_hotkeyv_cmd_by_code(this->hotkeys, code);
  266. }
  267. CLISH_GET(view, lub_list_t *, nspaces);
  268. CLISH_GET_STR(view, name);
  269. CLISH_SET_STR_ONCE(view, prompt);
  270. CLISH_GET_STR(view, prompt);
  271. CLISH_SET_STR(view, access);
  272. CLISH_GET_STR(view, access);
  273. CLISH_SET(view, unsigned int, depth);
  274. CLISH_GET(view, unsigned int, depth);
  275. CLISH_SET(view, clish_view_restore_e, restore);
  276. CLISH_GET(view, clish_view_restore_e, restore);
  277. /*-------------------------------------------------------- */
  278. lub_bintree_t * clish_view__get_tree(clish_view_t *inst)
  279. {
  280. assert(inst);
  281. return &inst->tree;
  282. }
  283. /*--------------------------------------------------------- */
  284. static const char *restore_names[] = {
  285. "none",
  286. "depth",
  287. "view",
  288. };
  289. /*--------------------------------------------------------- */
  290. const char *clish_view_restore__get_name(clish_view_restore_e restore)
  291. {
  292. if (restore >= CLISH_RESTORE_MAX)
  293. return NULL;
  294. return restore_names[restore];
  295. }
  296. /*--------------------------------------------------------- */
  297. clish_view_restore_e clish_view_restore_resolve(const char *name)
  298. {
  299. clish_view_restore_e result = CLISH_RESTORE_NONE;
  300. unsigned int i = 0;
  301. if (!name)
  302. return CLISH_RESTORE_NONE;
  303. for (i = 0; i < CLISH_RESTORE_MAX; i++) {
  304. if (0 == strcmp(name, restore_names[i])) {
  305. result = (clish_view_restore_e)i;
  306. break;
  307. }
  308. }
  309. return result;
  310. }