1
0

view.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <assert.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. /*---------------------------------------------------------
  15. * PRIVATE META FUNCTIONS
  16. *--------------------------------------------------------- */
  17. int clish_view_bt_compare(const void *clientnode, const void *clientkey)
  18. {
  19. const clish_view_t *this = clientnode;
  20. const char *key = clientkey;
  21. return strcmp(this->name, key);
  22. }
  23. /*-------------------------------------------------------- */
  24. void clish_view_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  25. {
  26. const clish_view_t *this = clientnode;
  27. /* fill out the opaque key */
  28. strcpy((char *)key, this->name);
  29. }
  30. /*---------------------------------------------------------
  31. * PRIVATE METHODS
  32. *--------------------------------------------------------- */
  33. static void clish_view_init(clish_view_t * this, const char *name, const char *prompt)
  34. {
  35. /* set up defaults */
  36. this->name = lub_string_dup(name);
  37. this->prompt = NULL;
  38. this->nspacec = 0;
  39. this->nspacev = NULL;
  40. this->depth = 0;
  41. this->restore = CLISH_RESTORE_NONE;
  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. /* set up the defaults */
  49. clish_view__set_prompt(this, prompt);
  50. /* Initialize hotkey structures */
  51. this->hotkeys = clish_hotkeyv_new();
  52. }
  53. /*--------------------------------------------------------- */
  54. static void clish_view_fini(clish_view_t * this)
  55. {
  56. clish_command_t *cmd;
  57. unsigned i;
  58. /* delete each command held by this view */
  59. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  60. /* remove the command from the tree */
  61. lub_bintree_remove(&this->tree, cmd);
  62. /* release the instance */
  63. clish_command_delete(cmd);
  64. }
  65. /* free our memory */
  66. lub_string_free(this->name);
  67. this->name = NULL;
  68. lub_string_free(this->prompt);
  69. this->prompt = NULL;
  70. /* finalize each of the namespace instances */
  71. for (i = 0; i < this->nspacec; i++) {
  72. clish_nspace_delete(this->nspacev[i]);
  73. }
  74. /* Free hotkey structures */
  75. clish_hotkeyv_delete(this->hotkeys);
  76. /* free the namespace vector */
  77. free(this->nspacev);
  78. this->nspacec = 0;
  79. this->nspacev = NULL;
  80. }
  81. /*---------------------------------------------------------
  82. * PUBLIC META FUNCTIONS
  83. *--------------------------------------------------------- */
  84. size_t clish_view_bt_offset(void)
  85. {
  86. return offsetof(clish_view_t, bt_node);
  87. }
  88. /*--------------------------------------------------------- */
  89. clish_view_t *clish_view_new(const char *name, const char *prompt)
  90. {
  91. clish_view_t *this = malloc(sizeof(clish_view_t));
  92. if (this)
  93. clish_view_init(this, name, prompt);
  94. return this;
  95. }
  96. /*---------------------------------------------------------
  97. * PUBLIC METHODS
  98. *--------------------------------------------------------- */
  99. void clish_view_delete(clish_view_t * this)
  100. {
  101. clish_view_fini(this);
  102. free(this);
  103. }
  104. /*--------------------------------------------------------- */
  105. clish_command_t *clish_view_new_command(clish_view_t * this,
  106. const char *name, const char *help)
  107. {
  108. /* allocate the memory for a new parameter definition */
  109. clish_command_t *cmd = clish_command_new(name, help);
  110. assert(cmd);
  111. /* if this is a command other than the startup command... */
  112. if (NULL != help) {
  113. /* ...insert it into the binary tree for this view */
  114. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  115. /* inserting a duplicate command is bad */
  116. clish_command_delete(cmd);
  117. cmd = NULL;
  118. }
  119. }
  120. return cmd;
  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_view(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 *cmd, *result = NULL;
  183. clish_nspace_t *nspace;
  184. unsigned cnt = clish_view__get_nspace_count(this);
  185. int i;
  186. /* Search the current view */
  187. result = lub_bintree_find(&this->tree, name);
  188. /* Make command link from command alias */
  189. result = clish_command_alias_to_link(result);
  190. if (inherit) {
  191. for (i = cnt - 1; i >= 0; i--) {
  192. nspace = clish_view__get_nspace(this, i);
  193. cmd = clish_nspace_find_command(nspace, name);
  194. /* choose the longest match */
  195. result = clish_command_choose_longest(result, cmd);
  196. }
  197. }
  198. return result;
  199. }
  200. /*--------------------------------------------------------- */
  201. static const clish_command_t *find_next_completion(clish_view_t * this,
  202. const char *iter_cmd, const char *line)
  203. {
  204. clish_command_t *cmd;
  205. const char *name = "";
  206. lub_argv_t *largv;
  207. unsigned words;
  208. /* build an argument vector for the line */
  209. largv = lub_argv_new(line, 0);
  210. words = lub_argv__get_count(largv);
  211. /* account for trailing space */
  212. if (!*line || lub_ctype_isspace(line[strlen(line) - 1]))
  213. words++;
  214. if (iter_cmd)
  215. name = iter_cmd;
  216. while ((cmd = lub_bintree_findnext(&this->tree, name))) {
  217. /* Make command link from command alias */
  218. cmd = clish_command_alias_to_link(cmd);
  219. name = clish_command__get_name(cmd);
  220. if (words == lub_argv_wordcount(name)) {
  221. /* only bother with commands of which this line is a prefix */
  222. /* this is a completion */
  223. if (lub_string_nocasestr(name, line) == name)
  224. break;
  225. }
  226. }
  227. /* clean up the dynamic memory */
  228. lub_argv_delete(largv);
  229. return cmd;
  230. }
  231. /*--------------------------------------------------------- */
  232. const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
  233. const char *iter_cmd, const char *line,
  234. clish_nspace_visibility_t field, bool_t inherit)
  235. {
  236. const clish_command_t *result, *cmd;
  237. clish_nspace_t *nspace;
  238. unsigned cnt = clish_view__get_nspace_count(this);
  239. int i;
  240. /* ask local view for next command */
  241. result = find_next_completion(this, iter_cmd, line);
  242. if (!inherit)
  243. return result;
  244. /* ask the imported namespaces for next command */
  245. for (i = cnt - 1; i >= 0; i--) {
  246. nspace = clish_view__get_nspace(this, i);
  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. size_t new_size = ((this->nspacec + 1) * sizeof(clish_nspace_t *));
  260. clish_nspace_t **tmp;
  261. /* resize the namespace vector */
  262. tmp = realloc(this->nspacev, new_size);
  263. assert(tmp);
  264. this->nspacev = tmp;
  265. /* insert reference to the namespace */
  266. this->nspacev[this->nspacec++] = nspace;
  267. }
  268. /*--------------------------------------------------------- */
  269. void clish_view_clean_proxy(clish_view_t * this)
  270. {
  271. int i;
  272. /* Iterate namespace instances */
  273. for (i = 0; i < this->nspacec; i++) {
  274. clish_nspace_clean_proxy(this->nspacev[i]);
  275. }
  276. }
  277. /*---------------------------------------------------------
  278. * PUBLIC ATTRIBUTES
  279. *--------------------------------------------------------- */
  280. const char *clish_view__get_name(const clish_view_t * this)
  281. {
  282. return this->name;
  283. }
  284. /*--------------------------------------------------------- */
  285. void clish_view__set_prompt(clish_view_t * this, const char *prompt)
  286. {
  287. assert(!this->prompt);
  288. this->prompt = lub_string_dup(prompt);
  289. }
  290. /*--------------------------------------------------------- */
  291. char *clish_view__get_prompt(const clish_view_t *this)
  292. {
  293. return this->prompt;
  294. }
  295. /*--------------------------------------------------------- */
  296. clish_nspace_t *clish_view__get_nspace(const clish_view_t * this,
  297. unsigned index)
  298. {
  299. clish_nspace_t *result = NULL;
  300. if (index < this->nspacec) {
  301. result = this->nspacev[index];
  302. }
  303. return result;
  304. }
  305. /*--------------------------------------------------------- */
  306. unsigned int clish_view__get_nspace_count(const clish_view_t * this)
  307. {
  308. return this->nspacec;
  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. /*--------------------------------------------------------- */