view.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. }
  51. /*--------------------------------------------------------- */
  52. static void clish_view_fini(clish_view_t * this)
  53. {
  54. clish_command_t *cmd;
  55. unsigned i;
  56. /* delete each command held by this view */
  57. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  58. /* remove the command from the tree */
  59. lub_bintree_remove(&this->tree, cmd);
  60. /* release the instance */
  61. clish_command_delete(cmd);
  62. }
  63. /* free our memory */
  64. lub_string_free(this->name);
  65. this->name = NULL;
  66. lub_string_free(this->prompt);
  67. this->prompt = NULL;
  68. /* finalize each of the namespace instances */
  69. for (i = 0; i < this->nspacec; i++) {
  70. clish_nspace_delete(this->nspacev[i]);
  71. }
  72. /* free the namespace vector */
  73. free(this->nspacev);
  74. this->nspacec = 0;
  75. this->nspacev = NULL;
  76. }
  77. /*---------------------------------------------------------
  78. * PUBLIC META FUNCTIONS
  79. *--------------------------------------------------------- */
  80. size_t clish_view_bt_offset(void)
  81. {
  82. return offsetof(clish_view_t, bt_node);
  83. }
  84. /*--------------------------------------------------------- */
  85. clish_view_t *clish_view_new(const char *name, const char *prompt)
  86. {
  87. clish_view_t *this = malloc(sizeof(clish_view_t));
  88. if (this)
  89. clish_view_init(this, name, prompt);
  90. return this;
  91. }
  92. /*---------------------------------------------------------
  93. * PUBLIC METHODS
  94. *--------------------------------------------------------- */
  95. void clish_view_delete(clish_view_t * this)
  96. {
  97. clish_view_fini(this);
  98. free(this);
  99. }
  100. /*--------------------------------------------------------- */
  101. clish_command_t *clish_view_new_command(clish_view_t * this,
  102. const char *name, const char *help)
  103. {
  104. /* allocate the memory for a new parameter definition */
  105. clish_command_t *cmd = clish_command_new(name, help);
  106. assert(cmd);
  107. /* if this is a command other than the startup command... */
  108. if (NULL != help) {
  109. /* ...insert it into the binary tree for this view */
  110. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  111. /* inserting a duplicate command is bad */
  112. clish_command_delete(cmd);
  113. cmd = NULL;
  114. }
  115. }
  116. return cmd;
  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_view(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 *cmd, *result = NULL;
  179. clish_nspace_t *nspace;
  180. unsigned cnt = clish_view__get_nspace_count(this);
  181. int i;
  182. /* Search the current view */
  183. result = lub_bintree_find(&this->tree, name);
  184. /* Make command link from command alias */
  185. result = clish_command_alias_to_link(result);
  186. if (inherit) {
  187. for (i = cnt - 1; i >= 0; i--) {
  188. nspace = clish_view__get_nspace(this, i);
  189. cmd = clish_nspace_find_command(nspace, name);
  190. /* choose the longest match */
  191. result = clish_command_choose_longest(result, cmd);
  192. }
  193. }
  194. return result;
  195. }
  196. /*--------------------------------------------------------- */
  197. static const clish_command_t *find_next_completion(clish_view_t * this,
  198. const char *iter_cmd, const char *line)
  199. {
  200. clish_command_t *cmd;
  201. const char *name = "";
  202. lub_argv_t *largv;
  203. unsigned words;
  204. /* build an argument vector for the line */
  205. largv = lub_argv_new(line, 0);
  206. words = lub_argv__get_count(largv);
  207. /* account for trailing space */
  208. if (!*line || lub_ctype_isspace(line[strlen(line) - 1]))
  209. words++;
  210. if (iter_cmd)
  211. name = iter_cmd;
  212. while ((cmd = lub_bintree_findnext(&this->tree, name))) {
  213. /* Make command link from command alias */
  214. cmd = clish_command_alias_to_link(cmd);
  215. name = clish_command__get_name(cmd);
  216. if (words == lub_argv_wordcount(name)) {
  217. /* only bother with commands of which this line is a prefix */
  218. /* this is a completion */
  219. if (lub_string_nocasestr(name, line) == name)
  220. break;
  221. }
  222. }
  223. /* clean up the dynamic memory */
  224. lub_argv_delete(largv);
  225. return cmd;
  226. }
  227. /*--------------------------------------------------------- */
  228. const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
  229. const char *iter_cmd, const char *line,
  230. clish_nspace_visibility_t field, bool_t inherit)
  231. {
  232. const clish_command_t *result, *cmd;
  233. clish_nspace_t *nspace;
  234. unsigned cnt = clish_view__get_nspace_count(this);
  235. int i;
  236. /* ask local view for next command */
  237. result = find_next_completion(this, iter_cmd, line);
  238. if (!inherit)
  239. return result;
  240. /* ask the imported namespaces for next command */
  241. for (i = cnt - 1; i >= 0; i--) {
  242. nspace = clish_view__get_nspace(this, i);
  243. if (!clish_nspace__get_visibility(nspace, field))
  244. continue;
  245. cmd = clish_nspace_find_next_completion(nspace,
  246. iter_cmd, line, field);
  247. if (clish_command_diff(result, cmd) > 0)
  248. result = cmd;
  249. }
  250. return result;
  251. }
  252. /*--------------------------------------------------------- */
  253. void clish_view_insert_nspace(clish_view_t * this, clish_nspace_t * nspace)
  254. {
  255. size_t new_size = ((this->nspacec + 1) * sizeof(clish_nspace_t *));
  256. clish_nspace_t **tmp;
  257. /* resize the namespace vector */
  258. tmp = realloc(this->nspacev, new_size);
  259. assert(tmp);
  260. this->nspacev = tmp;
  261. /* insert reference to the namespace */
  262. this->nspacev[this->nspacec++] = nspace;
  263. }
  264. /*--------------------------------------------------------- */
  265. void clish_view_clean_proxy(clish_view_t * this)
  266. {
  267. int i;
  268. /* Iterate namespace instances */
  269. for (i = 0; i < this->nspacec; i++) {
  270. clish_nspace_clean_proxy(this->nspacev[i]);
  271. }
  272. }
  273. /*---------------------------------------------------------
  274. * PUBLIC ATTRIBUTES
  275. *--------------------------------------------------------- */
  276. const char *clish_view__get_name(const clish_view_t * this)
  277. {
  278. return this->name;
  279. }
  280. /*--------------------------------------------------------- */
  281. void clish_view__set_prompt(clish_view_t * this, const char *prompt)
  282. {
  283. if (prompt && *prompt) {
  284. assert(!this->prompt);
  285. this->prompt = lub_string_dup(prompt);
  286. }
  287. }
  288. /*--------------------------------------------------------- */
  289. char *clish_view__get_prompt(const clish_view_t *this)
  290. {
  291. return this->prompt;
  292. }
  293. /*--------------------------------------------------------- */
  294. clish_nspace_t *clish_view__get_nspace(const clish_view_t * this,
  295. unsigned index)
  296. {
  297. clish_nspace_t *result = NULL;
  298. if (index < this->nspacec) {
  299. result = this->nspacev[index];
  300. }
  301. return result;
  302. }
  303. /*--------------------------------------------------------- */
  304. unsigned int clish_view__get_nspace_count(const clish_view_t * this)
  305. {
  306. return this->nspacec;
  307. }
  308. /*--------------------------------------------------------- */
  309. void clish_view__set_depth(clish_view_t * this, unsigned depth)
  310. {
  311. this->depth = depth;
  312. }
  313. /*--------------------------------------------------------- */
  314. unsigned clish_view__get_depth(const clish_view_t * this)
  315. {
  316. return this->depth;
  317. }
  318. /*--------------------------------------------------------- */
  319. void clish_view__set_restore(clish_view_t * this,
  320. clish_view_restore_t restore)
  321. {
  322. this->restore = restore;
  323. }
  324. /*--------------------------------------------------------- */
  325. clish_view_restore_t clish_view__get_restore(const clish_view_t * this)
  326. {
  327. return this->restore;
  328. }
  329. /*--------------------------------------------------------- */