1
0

command.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * command.c
  3. *
  4. * This file provides the implementation of a command definition
  5. */
  6. #include "private.h"
  7. #include "clish/types.h"
  8. #include "lub/bintree.h"
  9. #include "lub/string.h"
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. static void
  15. clish_command_init(clish_command_t *this, const char *name, const char *text)
  16. {
  17. /* initialise the node part */
  18. this->name = lub_string_dup(name);
  19. this->text = lub_string_dup(text);
  20. /* Be a good binary tree citizen */
  21. lub_bintree_node_init(&this->bt_node);
  22. /* set up defaults */
  23. this->link = NULL;
  24. this->alias = NULL;
  25. this->alias_view = NULL;
  26. this->paramv = clish_paramv_new();
  27. this->viewid = NULL;
  28. this->viewname = NULL;
  29. this->action = clish_action_new();
  30. this->config = clish_config_new();
  31. this->detail = NULL;
  32. this->escape_chars = NULL;
  33. this->regex_chars = NULL;
  34. this->args = NULL;
  35. this->pview = NULL;
  36. this->dynamic = BOOL_FALSE;
  37. this->internal = BOOL_FALSE;
  38. this->access = NULL;
  39. }
  40. /*--------------------------------------------------------- */
  41. static void clish_command_fini(clish_command_t * this)
  42. {
  43. lub_string_free(this->name);
  44. lub_string_free(this->text);
  45. /* Link need not full cleanup */
  46. if (this->link)
  47. return;
  48. /* finalize each of the parameter instances */
  49. clish_paramv_delete(this->paramv);
  50. clish_action_delete(this->action);
  51. clish_config_delete(this->config);
  52. lub_string_free(this->alias);
  53. lub_string_free(this->alias_view);
  54. lub_string_free(this->viewname);
  55. lub_string_free(this->viewid);
  56. lub_string_free(this->detail);
  57. lub_string_free(this->escape_chars);
  58. lub_string_free(this->regex_chars);
  59. lub_string_free(this->access);
  60. if (this->args)
  61. clish_param_delete(this->args);
  62. }
  63. /*--------------------------------------------------------- */
  64. size_t clish_command_bt_offset(void)
  65. {
  66. return offsetof(clish_command_t, bt_node);
  67. }
  68. /*--------------------------------------------------------- */
  69. int clish_command_bt_compare(const void *clientnode, const void *clientkey)
  70. {
  71. const clish_command_t *this = clientnode;
  72. const char *key = clientkey;
  73. return lub_string_nocasecmp(this->name, key);
  74. }
  75. /*--------------------------------------------------------- */
  76. void clish_command_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  77. {
  78. const clish_command_t *this = clientnode;
  79. /* fill out the opaque key */
  80. strcpy((char *)key, this->name);
  81. }
  82. /*--------------------------------------------------------- */
  83. clish_command_t *clish_command_new(const char *name, const char *help)
  84. {
  85. clish_command_t *this = malloc(sizeof(clish_command_t));
  86. if (this)
  87. clish_command_init(this, name, help);
  88. return this;
  89. }
  90. /*--------------------------------------------------------- */
  91. clish_command_t *clish_command_new_link(const char *name,
  92. const char *help, const clish_command_t * ref)
  93. {
  94. if (!ref)
  95. return NULL;
  96. clish_command_t *this = malloc(sizeof(clish_command_t));
  97. assert(this);
  98. /* Copy all fields to the new command-link */
  99. *this = *ref;
  100. /* Initialise the name (other than original name) */
  101. this->name = lub_string_dup(name);
  102. /* Initialise the help (other than original help) */
  103. this->text = lub_string_dup(help);
  104. /* Be a good binary tree citizen */
  105. lub_bintree_node_init(&this->bt_node);
  106. /* It a link to command so set the link flag */
  107. this->link = ref;
  108. return this;
  109. }
  110. /*--------------------------------------------------------- */
  111. clish_command_t * clish_command_alias_to_link(clish_command_t *this, clish_command_t *ref)
  112. {
  113. clish_command_t tmp;
  114. if (!this || !ref)
  115. return NULL;
  116. if (ref->alias) /* The reference is a link too */
  117. return NULL;
  118. memcpy(&tmp, this, sizeof(tmp));
  119. *this = *ref;
  120. memcpy(&this->bt_node, &tmp.bt_node, sizeof(tmp.bt_node));
  121. this->name = lub_string_dup(tmp.name); /* Save an original name */
  122. this->text = lub_string_dup(tmp.text); /* Save an original help */
  123. this->link = ref;
  124. this->pview = tmp.pview; /* Save an original parent view */
  125. clish_command_fini(&tmp);
  126. return this;
  127. }
  128. /*--------------------------------------------------------- */
  129. void clish_command_delete(clish_command_t * this)
  130. {
  131. clish_command_fini(this);
  132. free(this);
  133. }
  134. /*--------------------------------------------------------- */
  135. void clish_command_insert_param(clish_command_t * this, clish_param_t * param)
  136. {
  137. clish_paramv_insert(this->paramv, param);
  138. }
  139. /*--------------------------------------------------------- */
  140. clish_command_t *clish_command_choose_longest(clish_command_t * cmd1,
  141. clish_command_t * cmd2)
  142. {
  143. unsigned len1 = (cmd1 ? strlen(clish_command__get_name(cmd1)) : 0);
  144. unsigned len2 = (cmd2 ? strlen(clish_command__get_name(cmd2)) : 0);
  145. if (len2 < len1) {
  146. return cmd1;
  147. } else if (len1 < len2) {
  148. return cmd2;
  149. } else if (cmd1) {
  150. /* priority to the first one */
  151. return cmd1;
  152. } else {
  153. /* else use the second one */
  154. return cmd2;
  155. }
  156. }
  157. /*--------------------------------------------------------- */
  158. int clish_command_diff(const clish_command_t * cmd1,
  159. const clish_command_t * cmd2)
  160. {
  161. if (NULL == cmd1) {
  162. if (NULL != cmd2)
  163. return 1;
  164. else
  165. return 0;
  166. }
  167. if (NULL == cmd2)
  168. return -1;
  169. return lub_string_nocasecmp(clish_command__get_name(cmd1),
  170. clish_command__get_name(cmd2));
  171. }
  172. CLISH_GET_STR(command, name);
  173. CLISH_GET_STR(command, text);
  174. CLISH_SET_STR_ONCE(command, detail);
  175. CLISH_GET_STR(command, detail);
  176. CLISH_GET(command, clish_action_t *, action);
  177. CLISH_GET(command, clish_config_t *, config);
  178. CLISH_SET_STR_ONCE(command, regex_chars);
  179. CLISH_GET_STR(command, regex_chars);
  180. CLISH_SET_STR_ONCE(command, escape_chars);
  181. CLISH_GET_STR(command, escape_chars);
  182. CLISH_SET_STR_ONCE(command, viewname);
  183. CLISH_GET_STR(command, viewname);
  184. CLISH_SET_STR_ONCE(command, viewid);
  185. CLISH_GET_STR(command, viewid);
  186. CLISH_SET_ONCE(command, clish_param_t *, args);
  187. CLISH_GET(command, clish_param_t *, args);
  188. CLISH_GET(command, clish_paramv_t *, paramv);
  189. CLISH_SET(command, clish_view_t *, pview);
  190. CLISH_GET(command, clish_view_t *, pview);
  191. CLISH_SET_STR(command, access);
  192. CLISH_GET_STR(command, access);
  193. CLISH_SET_STR(command, alias);
  194. CLISH_GET_STR(command, alias);
  195. CLISH_SET_STR(command, alias_view);
  196. CLISH_GET_STR(command, alias_view);
  197. CLISH_SET(command, bool_t, internal);
  198. CLISH_GET(command, bool_t, internal);
  199. CLISH_SET(command, bool_t, dynamic);
  200. CLISH_GET(command, bool_t, dynamic);
  201. /*--------------------------------------------------------- */
  202. void clish_command__force_viewname(clish_command_t * this, const char *viewname)
  203. {
  204. if (this->viewname)
  205. lub_string_free(this->viewname);
  206. this->viewname = lub_string_dup(viewname);
  207. }
  208. /*--------------------------------------------------------- */
  209. void clish_command__force_viewid(clish_command_t * this, const char *viewid)
  210. {
  211. if (this->viewid)
  212. lub_string_free(this->viewid);
  213. this->viewid = lub_string_dup(viewid);
  214. }
  215. /*--------------------------------------------------------- */
  216. const clish_param_t *clish_command__get_param(const clish_command_t * this,
  217. unsigned index)
  218. {
  219. return clish_paramv__get_param(this->paramv, index);
  220. }
  221. /*--------------------------------------------------------- */
  222. const char *clish_command__get_suffix(const clish_command_t * this)
  223. {
  224. return lub_string_suffix(this->name);
  225. }
  226. /*--------------------------------------------------------- */
  227. unsigned int clish_command__get_param_count(const clish_command_t * this)
  228. {
  229. return clish_paramv__get_count(this->paramv);
  230. }
  231. /*--------------------------------------------------------- */
  232. int clish_command__get_depth(const clish_command_t * this)
  233. {
  234. if (!this->pview)
  235. return 0;
  236. return clish_view__get_depth(this->pview);
  237. }
  238. /*--------------------------------------------------------- */
  239. clish_view_restore_e clish_command__get_restore(const clish_command_t * this)
  240. {
  241. if (!this->pview)
  242. return CLISH_RESTORE_NONE;
  243. return clish_view__get_restore(this->pview);
  244. }
  245. /*--------------------------------------------------------- */
  246. const clish_command_t * clish_command__get_orig(const clish_command_t * this)
  247. {
  248. if (this->link)
  249. return clish_command__get_orig(this->link);
  250. return this;
  251. }
  252. /*--------------------------------------------------------- */
  253. const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
  254. {
  255. if (!this->dynamic)
  256. return this;
  257. if (this->link)
  258. return clish_command__get_cmd(this->link);
  259. return NULL;
  260. }
  261. /*--------------------------------------------------------- */
  262. /* Consider command incomplete when it doesn't have any actions to execute.
  263. I.e. has no ACTION, doesn't change VIEW, doesn't have CONFIG tag.
  264. */
  265. bool_t clish_command_is_incomplete(const clish_command_t *this)
  266. {
  267. clish_action_t *action = NULL;
  268. clish_config_t *config = NULL;
  269. assert(this);
  270. if (!this)
  271. return BOOL_TRUE; // Empty command is incomplete
  272. action = clish_command__get_action(this);
  273. config = clish_command__get_config(this);
  274. if (!clish_action__get_script(action) &&
  275. !clish_action__get_builtin(action) &&
  276. (CLISH_CONFIG_NONE == clish_config__get_op(config)) &&
  277. !clish_command__get_param_count(this) &&
  278. !clish_command__get_viewname(this)) {
  279. return BOOL_TRUE;
  280. }
  281. return BOOL_FALSE;
  282. }