view.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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_view(result))) {
  165. /* if this doesn't do anything we've
  166. * not resolved a command
  167. */
  168. result = NULL;
  169. }
  170. }
  171. return result;
  172. }
  173. /*--------------------------------------------------------- */
  174. clish_command_t *clish_view_find_command(clish_view_t * this,
  175. const char *name, bool_t inherit)
  176. {
  177. clish_command_t *cmd, *result = NULL;
  178. clish_nspace_t *nspace;
  179. unsigned cnt = clish_view__get_nspace_count(this);
  180. int i;
  181. /* Search the current view */
  182. result = lub_bintree_find(&this->tree, name);
  183. /* Make command link from command alias */
  184. result = clish_command_alias_to_link(result);
  185. if (inherit) {
  186. for (i = cnt - 1; i >= 0; i--) {
  187. nspace = clish_view__get_nspace(this, i);
  188. cmd = clish_nspace_find_command(nspace, name);
  189. /* choose the longest match */
  190. result = clish_command_choose_longest(result, cmd);
  191. }
  192. }
  193. return result;
  194. }
  195. /*--------------------------------------------------------- */
  196. static const clish_command_t *find_next_completion(clish_view_t * this,
  197. const char *iter_cmd, const char *line)
  198. {
  199. clish_command_t *cmd;
  200. const char *name = "";
  201. lub_argv_t *largv;
  202. unsigned words;
  203. /* build an argument vector for the line */
  204. largv = lub_argv_new(line, 0);
  205. words = lub_argv__get_count(largv);
  206. /* account for trailing space */
  207. if (!*line || lub_ctype_isspace(line[strlen(line) - 1]))
  208. words++;
  209. if (iter_cmd)
  210. name = iter_cmd;
  211. while ((cmd = lub_bintree_findnext(&this->tree, name))) {
  212. /* Make command link from command alias */
  213. cmd = clish_command_alias_to_link(cmd);
  214. name = clish_command__get_name(cmd);
  215. if (words == lub_argv_wordcount(name)) {
  216. /* only bother with commands of which this line is a prefix */
  217. /* this is a completion */
  218. if (lub_string_nocasestr(name, line) == name)
  219. break;
  220. }
  221. }
  222. /* clean up the dynamic memory */
  223. lub_argv_delete(largv);
  224. return cmd;
  225. }
  226. /*--------------------------------------------------------- */
  227. const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
  228. const char *iter_cmd, const char *line,
  229. clish_nspace_visibility_t field, bool_t inherit)
  230. {
  231. const clish_command_t *result, *cmd;
  232. clish_nspace_t *nspace;
  233. unsigned cnt = clish_view__get_nspace_count(this);
  234. int i;
  235. /* ask local view for next command */
  236. result = find_next_completion(this, iter_cmd, line);
  237. if (!inherit)
  238. return result;
  239. /* ask the imported namespaces for next command */
  240. for (i = cnt - 1; i >= 0; i--) {
  241. nspace = clish_view__get_nspace(this, i);
  242. if (!clish_nspace__get_visibility(nspace, field))
  243. continue;
  244. cmd = clish_nspace_find_next_completion(nspace,
  245. iter_cmd, line, field);
  246. if (clish_command_diff(result, cmd) > 0)
  247. result = cmd;
  248. }
  249. return result;
  250. }
  251. /*--------------------------------------------------------- */
  252. void clish_view_insert_nspace(clish_view_t * this, clish_nspace_t * nspace)
  253. {
  254. size_t new_size = ((this->nspacec + 1) * sizeof(clish_nspace_t *));
  255. clish_nspace_t **tmp;
  256. /* resize the namespace vector */
  257. tmp = realloc(this->nspacev, new_size);
  258. assert(tmp);
  259. this->nspacev = tmp;
  260. /* insert reference to the namespace */
  261. this->nspacev[this->nspacec++] = nspace;
  262. }
  263. /*--------------------------------------------------------- */
  264. void clish_view_clean_proxy(clish_view_t * this)
  265. {
  266. int i;
  267. /* Iterate namespace instances */
  268. for (i = 0; i < this->nspacec; i++) {
  269. clish_nspace_clean_proxy(this->nspacev[i]);
  270. }
  271. }
  272. /*---------------------------------------------------------
  273. * PUBLIC ATTRIBUTES
  274. *--------------------------------------------------------- */
  275. const char *clish_view__get_name(const clish_view_t * this)
  276. {
  277. return this->name;
  278. }
  279. /*--------------------------------------------------------- */
  280. void clish_view__set_prompt(clish_view_t * this, const char *prompt)
  281. {
  282. assert(!this->prompt);
  283. this->prompt = lub_string_dup(prompt);
  284. }
  285. /*--------------------------------------------------------- */
  286. char *clish_view__get_prompt(const clish_view_t *this)
  287. {
  288. return this->prompt;
  289. }
  290. /*--------------------------------------------------------- */
  291. clish_nspace_t *clish_view__get_nspace(const clish_view_t * this,
  292. unsigned index)
  293. {
  294. clish_nspace_t *result = NULL;
  295. if (index < this->nspacec) {
  296. result = this->nspacev[index];
  297. }
  298. return result;
  299. }
  300. /*--------------------------------------------------------- */
  301. unsigned int clish_view__get_nspace_count(const clish_view_t * this)
  302. {
  303. return this->nspacec;
  304. }
  305. /*--------------------------------------------------------- */
  306. void clish_view__set_depth(clish_view_t * this, unsigned depth)
  307. {
  308. this->depth = depth;
  309. }
  310. /*--------------------------------------------------------- */
  311. unsigned clish_view__get_depth(const clish_view_t * this)
  312. {
  313. return this->depth;
  314. }
  315. /*--------------------------------------------------------- */
  316. void clish_view__set_restore(clish_view_t * this,
  317. clish_view_restore_t restore)
  318. {
  319. this->restore = restore;
  320. }
  321. /*--------------------------------------------------------- */
  322. clish_view_restore_t clish_view__get_restore(const clish_view_t * this)
  323. {
  324. return this->restore;
  325. }
  326. /*--------------------------------------------------------- */