view.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. clish_command_t *result = clish_view_resolve_prefix(this, line, inherit);
  158. if (result) {
  159. clish_action_t *action = clish_command__get_action(result);
  160. clish_config_t *config = clish_command__get_config(result);
  161. if (!clish_action__get_script(action) &&
  162. (!clish_action__get_builtin(action)) &&
  163. (CLISH_CONFIG_NONE == clish_config__get_op(config)) &&
  164. (!clish_command__get_param_count(result)) &&
  165. (!clish_command__get_viewname(result))) {
  166. /* if this doesn't do anything we've
  167. * not resolved a command
  168. */
  169. result = NULL;
  170. }
  171. }
  172. return result;
  173. }
  174. /*--------------------------------------------------------- */
  175. clish_command_t *clish_view_find_command(clish_view_t * this,
  176. const char *name, bool_t inherit)
  177. {
  178. clish_command_t *result = NULL;
  179. /* Search the current view */
  180. result = lub_bintree_find(&this->tree, name);
  181. if (inherit) {
  182. lub_list_node_t *iter;
  183. clish_command_t *cmd;
  184. clish_nspace_t *nspace;
  185. /* Iterate elements. It's important to iterate
  186. * items starting from tail bacause the next
  187. * NAMESPACE has higher priority than previous one
  188. * in a case then the both NAMESPACEs have the
  189. * commands with the same name.
  190. */
  191. for(iter = lub_list__get_tail(this->nspaces);
  192. iter; iter = lub_list_node__get_prev(iter)) {
  193. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  194. cmd = clish_nspace_find_command(nspace, name);
  195. /* Choose the longest match */
  196. result = clish_command_choose_longest(result, cmd);
  197. }
  198. }
  199. return result;
  200. }
  201. /*--------------------------------------------------------- */
  202. static const clish_command_t *find_next_completion(clish_view_t * this,
  203. const char *iter_cmd, const char *line)
  204. {
  205. clish_command_t *cmd;
  206. const char *name = "";
  207. lub_argv_t *largv;
  208. unsigned words;
  209. /* build an argument vector for the line */
  210. largv = lub_argv_new(line, 0);
  211. words = lub_argv__get_count(largv);
  212. /* account for trailing space */
  213. if (!*line || lub_ctype_isspace(line[strlen(line) - 1]))
  214. words++;
  215. if (iter_cmd)
  216. name = iter_cmd;
  217. while ((cmd = lub_bintree_findnext(&this->tree, name))) {
  218. name = clish_command__get_name(cmd);
  219. if (words == lub_string_wordcount(name)) {
  220. /* only bother with commands of which this line is a prefix */
  221. /* this is a completion */
  222. if (lub_string_nocasestr(name, line) == name)
  223. break;
  224. }
  225. }
  226. /* clean up the dynamic memory */
  227. lub_argv_delete(largv);
  228. return cmd;
  229. }
  230. /*--------------------------------------------------------- */
  231. const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
  232. const char *iter_cmd, const char *line,
  233. clish_nspace_visibility_e field, bool_t inherit)
  234. {
  235. const clish_command_t *result, *cmd;
  236. clish_nspace_t *nspace;
  237. lub_list_node_t *iter;
  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. /* Iterate elements */
  244. for(iter = lub_list__get_tail(this->nspaces);
  245. iter; iter = lub_list_node__get_prev(iter)) {
  246. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  247. if (!clish_nspace__get_visibility(nspace, field))
  248. continue;
  249. cmd = clish_nspace_find_next_completion(nspace,
  250. iter_cmd, line, field);
  251. if (clish_command_diff(result, cmd) > 0)
  252. result = cmd;
  253. }
  254. return result;
  255. }
  256. /*--------------------------------------------------------- */
  257. void clish_view_insert_nspace(clish_view_t * this, clish_nspace_t * nspace)
  258. {
  259. lub_list_add(this->nspaces, nspace);
  260. clish_nspace__set_pview(nspace, this);
  261. }
  262. /*--------------------------------------------------------- */
  263. void clish_view_clean_proxy(clish_view_t * this)
  264. {
  265. lub_list_node_t *iter;
  266. /* Iterate elements */
  267. for(iter = lub_list__get_head(this->nspaces);
  268. iter; iter = lub_list_node__get_next(iter)) {
  269. clish_nspace_clean_proxy((clish_nspace_t *)lub_list_node__get_data(iter));
  270. }
  271. }
  272. /*--------------------------------------------------------- */
  273. int clish_view_insert_hotkey(const clish_view_t *this, const char *key, const char *cmd)
  274. {
  275. return clish_hotkeyv_insert(this->hotkeys, key, cmd);
  276. }
  277. /*--------------------------------------------------------- */
  278. const char *clish_view_find_hotkey(const clish_view_t *this, int code)
  279. {
  280. return clish_hotkeyv_cmd_by_code(this->hotkeys, code);
  281. }
  282. CLISH_GET(view, lub_list_t *, nspaces);
  283. CLISH_GET_STR(view, name);
  284. CLISH_SET_STR_ONCE(view, prompt);
  285. CLISH_GET_STR(view, prompt);
  286. CLISH_SET_STR(view, access);
  287. CLISH_GET_STR(view, access);
  288. CLISH_SET(view, unsigned int, depth);
  289. CLISH_GET(view, unsigned int, depth);
  290. CLISH_SET(view, clish_view_restore_e, restore);
  291. CLISH_GET(view, clish_view_restore_e, restore);
  292. /*-------------------------------------------------------- */
  293. lub_bintree_t * clish_view__get_tree(clish_view_t *inst)
  294. {
  295. assert(inst);
  296. return &inst->tree;
  297. }
  298. /*--------------------------------------------------------- */
  299. static const char *restore_names[] = {
  300. "none",
  301. "depth",
  302. "view",
  303. };
  304. /*--------------------------------------------------------- */
  305. const char *clish_view_restore__get_name(clish_view_restore_e restore)
  306. {
  307. if (restore >= CLISH_RESTORE_MAX)
  308. return NULL;
  309. return restore_names[restore];
  310. }
  311. /*--------------------------------------------------------- */
  312. clish_view_restore_e clish_view_restore_resolve(const char *name)
  313. {
  314. clish_view_restore_e result = CLISH_RESTORE_NONE;
  315. unsigned int i = 0;
  316. if (!name)
  317. return CLISH_RESTORE_NONE;
  318. for (i = 0; i < CLISH_RESTORE_MAX; i++) {
  319. if (0 == strcmp(name, restore_names[i])) {
  320. result = (clish_view_restore_e)i;
  321. break;
  322. }
  323. }
  324. return result;
  325. }