view.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * view.c
  3. *
  4. * This file provides the implementation of a view class
  5. */
  6. #include "private.h"
  7. #include "clish/variable.h"
  8. #include "lub/argv.h"
  9. #include "lub/string.h"
  10. #include "lub/ctype.h"
  11. #include <assert.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. /*---------------------------------------------------------
  16. * PRIVATE META FUNCTIONS
  17. *--------------------------------------------------------- */
  18. int clish_view_bt_compare(const void *clientnode, const void *clientkey)
  19. {
  20. const clish_view_t *this = clientnode;
  21. const char *key = clientkey;
  22. return strcmp(this->name, key);
  23. }
  24. /*-------------------------------------------------------- */
  25. void clish_view_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  26. {
  27. const clish_view_t *this = clientnode;
  28. /* fill out the opaque key */
  29. strcpy((char *)key, this->name);
  30. }
  31. /*---------------------------------------------------------
  32. * PRIVATE METHODS
  33. *--------------------------------------------------------- */
  34. static void clish_view_init(clish_view_t * this, const char *name, const char *prompt)
  35. {
  36. /* set up defaults */
  37. this->name = lub_string_dup(name);
  38. this->prompt = NULL;
  39. this->nspacec = 0;
  40. this->nspacev = NULL;
  41. this->depth = 0;
  42. this->restore = CLISH_RESTORE_NONE;
  43. /* Be a good binary tree citizen */
  44. lub_bintree_node_init(&this->bt_node);
  45. /* initialise the tree of commands for this view */
  46. lub_bintree_init(&this->tree,
  47. clish_command_bt_offset(),
  48. clish_command_bt_compare, clish_command_bt_getkey);
  49. /* set up the defaults */
  50. clish_view__set_prompt(this, prompt);
  51. }
  52. /*--------------------------------------------------------- */
  53. static void clish_view_fini(clish_view_t * this)
  54. {
  55. clish_command_t *cmd;
  56. unsigned i;
  57. /* delete each command held by this view */
  58. while ((cmd = lub_bintree_findfirst(&this->tree))) {
  59. /* remove the command from the tree */
  60. lub_bintree_remove(&this->tree, cmd);
  61. /* release the instance */
  62. clish_command_delete(cmd);
  63. }
  64. /* free our memory */
  65. lub_string_free(this->name);
  66. this->name = NULL;
  67. lub_string_free(this->prompt);
  68. this->prompt = NULL;
  69. /* finalize each of the namespace instances */
  70. for (i = 0; i < this->nspacec; i++) {
  71. clish_nspace_delete(this->nspacev[i]);
  72. }
  73. /* free the namespace vector */
  74. free(this->nspacev);
  75. this->nspacec = 0;
  76. this->nspacev = NULL;
  77. }
  78. /*---------------------------------------------------------
  79. * PUBLIC META FUNCTIONS
  80. *--------------------------------------------------------- */
  81. size_t clish_view_bt_offset(void)
  82. {
  83. return offsetof(clish_view_t, bt_node);
  84. }
  85. /*--------------------------------------------------------- */
  86. clish_view_t *clish_view_new(const char *name, const char *prompt)
  87. {
  88. clish_view_t *this = malloc(sizeof(clish_view_t));
  89. if (this)
  90. clish_view_init(this, name, prompt);
  91. return this;
  92. }
  93. /*---------------------------------------------------------
  94. * PUBLIC METHODS
  95. *--------------------------------------------------------- */
  96. void clish_view_delete(clish_view_t * this)
  97. {
  98. clish_view_fini(this);
  99. free(this);
  100. }
  101. /*--------------------------------------------------------- */
  102. clish_command_t *clish_view_new_command(clish_view_t * this,
  103. const char *name, const char *help)
  104. {
  105. /* allocate the memory for a new parameter definition */
  106. clish_command_t *cmd = clish_command_new(name, help);
  107. assert(cmd);
  108. /* if this is a command other than the startup command... */
  109. if (NULL != help) {
  110. /* ...insert it into the binary tree for this view */
  111. if (-1 == lub_bintree_insert(&this->tree, cmd)) {
  112. /* inserting a duplicate command is bad */
  113. clish_command_delete(cmd);
  114. cmd = NULL;
  115. }
  116. }
  117. return cmd;
  118. }
  119. /*--------------------------------------------------------- */
  120. /* This method identifies the command (if any) which provides
  121. * the longest match with the specified line of text.
  122. *
  123. * NB this comparison is case insensitive.
  124. *
  125. * this - the view instance upon which to operate
  126. * line - the command line to analyse
  127. */
  128. clish_command_t *clish_view_resolve_prefix(clish_view_t * this,
  129. const char *line, bool_t inherit)
  130. {
  131. clish_command_t *result = NULL, *cmd;
  132. char *buffer = NULL;
  133. lub_argv_t *argv;
  134. unsigned i;
  135. /* create a vector of arguments */
  136. argv = lub_argv_new(line, 0);
  137. for (i = 0; i < lub_argv__get_count(argv); i++) {
  138. /* set our buffer to be that of the first "i" arguments */
  139. lub_string_cat(&buffer, lub_argv__get_arg(argv, i));
  140. /* set the result to the longest match */
  141. cmd = clish_view_find_command(this, buffer, inherit);
  142. /* job done */
  143. if (!cmd)
  144. break;
  145. result = cmd;
  146. /* ready for the next word */
  147. lub_string_cat(&buffer, " ");
  148. }
  149. /* free up our dynamic storage */
  150. lub_string_free(buffer);
  151. lub_argv_delete(argv);
  152. return result;
  153. }
  154. /*--------------------------------------------------------- */
  155. clish_command_t *clish_view_resolve_command(clish_view_t *this,
  156. const char *line, bool_t inherit)
  157. {
  158. clish_command_t *result = clish_view_resolve_prefix(this, line, inherit);
  159. if (result) {
  160. clish_action_t *action = clish_command__get_action(result);
  161. clish_config_t *config = clish_command__get_config(result);
  162. if (!clish_action__get_script(action) &&
  163. (!clish_action__get_builtin(action)) &&
  164. (CLISH_CONFIG_NONE == clish_config__get_op(config)) &&
  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. assert(!this->prompt);
  284. this->prompt = lub_string_dup(prompt);
  285. }
  286. /*--------------------------------------------------------- */
  287. char *clish_view__get_prompt(const clish_view_t *this)
  288. {
  289. return this->prompt;
  290. }
  291. /*--------------------------------------------------------- */
  292. clish_nspace_t *clish_view__get_nspace(const clish_view_t * this,
  293. unsigned index)
  294. {
  295. clish_nspace_t *result = NULL;
  296. if (index < this->nspacec) {
  297. result = this->nspacev[index];
  298. }
  299. return result;
  300. }
  301. /*--------------------------------------------------------- */
  302. const unsigned clish_view__get_nspace_count(const clish_view_t * this)
  303. {
  304. return this->nspacec;
  305. }
  306. /*--------------------------------------------------------- */
  307. void clish_view__set_depth(clish_view_t * this, unsigned depth)
  308. {
  309. this->depth = depth;
  310. }
  311. /*--------------------------------------------------------- */
  312. unsigned clish_view__get_depth(const clish_view_t * this)
  313. {
  314. return this->depth;
  315. }
  316. /*--------------------------------------------------------- */
  317. void clish_view__set_restore(clish_view_t * this,
  318. clish_view_restore_t restore)
  319. {
  320. this->restore = restore;
  321. }
  322. /*--------------------------------------------------------- */
  323. clish_view_restore_t clish_view__get_restore(const clish_view_t * this)
  324. {
  325. return this->restore;
  326. }
  327. /*--------------------------------------------------------- */