view.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. this->name = lub_string_dup(name);
  26. this->prompt = NULL;
  27. this->depth = 0;
  28. this->restore = CLISH_RESTORE_NONE;
  29. this->access = NULL;
  30. /* Init COMMAND list */
  31. this->cmds = lub_list_new(clish_command_compare, clish_command_delete);
  32. /* Init VAR list */
  33. this->vars = lub_list_new(clish_var_compare, clish_var_delete);
  34. /* Initialise the list of NAMESPACEs.
  35. * It's important to add new items to the
  36. * tail of list.
  37. */
  38. this->nspaces = lub_list_new(NULL, clish_nspace_delete);
  39. /* Initialize hotkey structures */
  40. this->hotkeys = clish_hotkeyv_new();
  41. }
  42. /*--------------------------------------------------------- */
  43. static void clish_view_fini(clish_view_t * this)
  44. {
  45. /* Free COMMAND list */
  46. lub_list_free_all(this->cmds);
  47. /* Free VAR list */
  48. lub_list_free_all(this->vars);
  49. /* Free NAMESPACE list */
  50. lub_list_free_all(this->nspaces);
  51. /* Free hotkey structures */
  52. clish_hotkeyv_delete(this->hotkeys);
  53. lub_string_free(this->name);
  54. lub_string_free(this->prompt);
  55. lub_string_free(this->access);
  56. }
  57. /*--------------------------------------------------------- */
  58. clish_view_t *clish_view_new(const char *name)
  59. {
  60. clish_view_t *this = malloc(sizeof(clish_view_t));
  61. if (this)
  62. clish_view_init(this, name);
  63. return this;
  64. }
  65. /*-------------------------------------------------------- */
  66. void clish_view_delete(void *data)
  67. {
  68. clish_view_t *this = (clish_view_t *)data;
  69. clish_view_fini(this);
  70. free(this);
  71. }
  72. /*--------------------------------------------------------- */
  73. /* Create new command and add it to list of commands */
  74. clish_command_t *clish_view_new_command(clish_view_t *this,
  75. const char *name, const char *help)
  76. {
  77. clish_command_t *cmd = clish_command_new(name, help);
  78. assert(cmd);
  79. clish_command__set_pview(cmd, this);
  80. if (!lub_list_add_uniq(this->cmds, cmd)) {
  81. clish_command_delete(cmd);
  82. cmd = NULL;
  83. }
  84. return cmd;
  85. }
  86. /*--------------------------------------------------------- */
  87. static int cmd_resolve(const void *key, const void *data)
  88. {
  89. const char *line = (const char *)key;
  90. const clish_command_t *d = (const clish_command_t *)data;
  91. const char *name = clish_command__get_name(d);
  92. size_t eq = lub_string_equal_part(line, name, BOOL_FALSE);
  93. if (eq == strlen(name))
  94. if ((line[eq] == '\0') || lub_ctype_isspace(line[eq]))
  95. return 0;
  96. return lub_string_nocasecmp(line, name);
  97. }
  98. /*--------------------------------------------------------- */
  99. /* This method identifies the command (if any) which provides
  100. * the longest match with the specified line of text.
  101. *
  102. * NB this comparison is case insensitive.
  103. *
  104. * this - the view instance upon which to operate
  105. * line - the command line to analyse
  106. *
  107. * Begin to search from tail of sorted list of cmds. So
  108. * the first match will be the longest command.
  109. */
  110. clish_command_t *clish_view_resolve_prefix(clish_view_t *this,
  111. const char *line)
  112. {
  113. lub_list_node_t *iter;
  114. clish_command_t *res = NULL;
  115. res = lub_list_rfind(this->cmds, cmd_resolve, line);
  116. /* Iterate elements. It's important to iterate
  117. * items starting from tail because the next
  118. * NAMESPACE has higher priority than previous one
  119. * in a case then the both NAMESPACEs have the
  120. * commands with the same name.
  121. */
  122. for (iter = lub_list__get_tail(this->nspaces);
  123. iter; iter = lub_list_node__get_prev(iter)) {
  124. clish_nspace_t *nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  125. clish_command_t *cmd = clish_nspace_resolve_prefix(nspace, line);
  126. /* Choose the longest match */
  127. res = clish_command_choose_longest(res, cmd);
  128. }
  129. return res;
  130. }
  131. #if 0 // WORK
  132. clish_command_t *clish_view_resolve_prefix(clish_view_t *this,
  133. const char *line, bool_t inherit)
  134. {
  135. clish_command_t *result = NULL, *cmd;
  136. char *buffer = NULL;
  137. lub_argv_t *argv;
  138. unsigned int i;
  139. /* create a vector of arguments */
  140. argv = lub_argv_new(line, 0);
  141. for (i = 0; i < lub_argv__get_count(argv); i++) {
  142. /* set our buffer to be that of the first "i" arguments */
  143. lub_string_cat(&buffer, lub_argv__get_arg(argv, i));
  144. /* set the result to the longest match */
  145. cmd = clish_view_find_command(this, buffer, inherit);
  146. /* job done */
  147. if (!cmd)
  148. break;
  149. result = cmd;
  150. /* ready for the next word */
  151. lub_string_cat(&buffer, " ");
  152. }
  153. /* free up our dynamic storage */
  154. lub_string_free(buffer);
  155. lub_argv_delete(argv);
  156. return result;
  157. }
  158. #endif
  159. /*--------------------------------------------------------- */
  160. clish_command_t *clish_view_resolve_command(clish_view_t *this,
  161. const char *line)
  162. {
  163. clish_command_t *result = clish_view_resolve_prefix(this, line);
  164. if (result) {
  165. clish_action_t *action = clish_command__get_action(result);
  166. clish_config_t *config = clish_command__get_config(result);
  167. if (!clish_action__get_script(action) &&
  168. (!clish_action__get_builtin(action)) &&
  169. (CLISH_CONFIG_NONE == clish_config__get_op(config)) &&
  170. (!clish_command__get_param_count(result)) &&
  171. (!clish_command__get_viewname(result))) {
  172. /* if this doesn't do anything we've
  173. * not resolved a command
  174. */
  175. result = NULL;
  176. }
  177. }
  178. return result;
  179. }
  180. /*--------------------------------------------------------- */
  181. /* Search for local (without NAMESPACEs) command by name */
  182. clish_command_t *clish_view_find_command(clish_view_t *this,
  183. const char *name)
  184. {
  185. return lub_list_find(this->cmds, clish_command_fn_find_by_name, name);
  186. }
  187. /*--------------------------------------------------------- */
  188. static const clish_command_t *find_next_completion(clish_view_t *this,
  189. const char *iter_cmd, const char *line)
  190. {
  191. #if 0 // WORK
  192. clish_command_t *cmd;
  193. const char *name = "";
  194. lub_argv_t *largv;
  195. unsigned words;
  196. /* build an argument vector for the line */
  197. largv = lub_argv_new(line, 0);
  198. words = lub_argv__get_count(largv);
  199. /* account for trailing space */
  200. if (!*line || lub_ctype_isspace(line[strlen(line) - 1]))
  201. words++;
  202. if (iter_cmd)
  203. name = iter_cmd;
  204. while ((cmd = lub_bintree_findnext(&this->tree, name))) {
  205. name = clish_command__get_name(cmd);
  206. if (words == lub_string_wordcount(name)) {
  207. /* only bother with commands of which this line is a prefix */
  208. /* this is a completion */
  209. if (lub_string_nocasestr(name, line) == name)
  210. break;
  211. }
  212. }
  213. /* clean up the dynamic memory */
  214. lub_argv_delete(largv);
  215. return cmd;
  216. #endif
  217. return NULL;
  218. }
  219. /*--------------------------------------------------------- */
  220. const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
  221. const char *iter_cmd, const char *line,
  222. clish_nspace_visibility_e field, bool_t inherit)
  223. {
  224. const clish_command_t *result, *cmd;
  225. clish_nspace_t *nspace;
  226. lub_list_node_t *iter;
  227. /* ask local view for next command */
  228. result = find_next_completion(this, iter_cmd, line);
  229. if (!inherit)
  230. return result;
  231. /* ask the imported namespaces for next command */
  232. /* Iterate elements */
  233. for(iter = lub_list__get_tail(this->nspaces);
  234. iter; iter = lub_list_node__get_prev(iter)) {
  235. nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
  236. if (!clish_nspace__get_visibility(nspace, field))
  237. continue;
  238. cmd = clish_nspace_find_next_completion(nspace,
  239. iter_cmd, line, field);
  240. if (clish_command_diff(result, cmd) > 0)
  241. result = cmd;
  242. }
  243. return result;
  244. }
  245. /*--------------------------------------------------------- */
  246. void clish_view_insert_nspace(clish_view_t * this, clish_nspace_t * nspace)
  247. {
  248. lub_list_add(this->nspaces, nspace);
  249. }
  250. /*--------------------------------------------------------- */
  251. void clish_view_clean_proxy(clish_view_t * this)
  252. {
  253. lub_list_node_t *iter;
  254. /* Iterate elements */
  255. for(iter = lub_list__get_head(this->nspaces);
  256. iter; iter = lub_list_node__get_next(iter)) {
  257. clish_nspace_clean_proxy((clish_nspace_t *)lub_list_node__get_data(iter));
  258. }
  259. }
  260. /*--------------------------------------------------------- */
  261. int clish_view_insert_hotkey(const clish_view_t *this, const char *key, const char *cmd)
  262. {
  263. return clish_hotkeyv_insert(this->hotkeys, key, cmd);
  264. }
  265. /*--------------------------------------------------------- */
  266. const char *clish_view_find_hotkey(const clish_view_t *this, int code)
  267. {
  268. return clish_hotkeyv_cmd_by_code(this->hotkeys, code);
  269. }
  270. CLISH_GET(view, lub_list_t *, nspaces);
  271. CLISH_GET(view, lub_list_t *, cmds);
  272. CLISH_GET(view, lub_list_t *, vars);
  273. CLISH_GET_STR(view, name);
  274. CLISH_SET_STR_ONCE(view, prompt);
  275. CLISH_GET_STR(view, prompt);
  276. CLISH_SET_STR(view, access);
  277. CLISH_GET_STR(view, access);
  278. CLISH_SET(view, unsigned int, depth);
  279. CLISH_GET(view, unsigned int, depth);
  280. CLISH_SET(view, clish_view_restore_e, restore);
  281. CLISH_GET(view, clish_view_restore_e, restore);