command.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 {
  150. /* let local view override */
  151. return cmd1;
  152. }
  153. }
  154. /*--------------------------------------------------------- */
  155. int clish_command_diff(const clish_command_t * cmd1,
  156. const clish_command_t * cmd2)
  157. {
  158. if (NULL == cmd1) {
  159. if (NULL != cmd2)
  160. return 1;
  161. else
  162. return 0;
  163. }
  164. if (NULL == cmd2)
  165. return -1;
  166. return lub_string_nocasecmp(clish_command__get_name(cmd1),
  167. clish_command__get_name(cmd2));
  168. }
  169. CLISH_GET_STR(command, name);
  170. CLISH_GET_STR(command, text);
  171. CLISH_SET_STR_ONCE(command, detail);
  172. CLISH_GET_STR(command, detail);
  173. CLISH_GET(command, clish_action_t *, action);
  174. CLISH_GET(command, clish_config_t *, config);
  175. CLISH_SET_STR_ONCE(command, regex_chars);
  176. CLISH_GET_STR(command, regex_chars);
  177. CLISH_SET_STR_ONCE(command, escape_chars);
  178. CLISH_GET_STR(command, escape_chars);
  179. CLISH_SET_STR_ONCE(command, viewname);
  180. CLISH_GET_STR(command, viewname);
  181. CLISH_SET_STR_ONCE(command, viewid);
  182. CLISH_GET_STR(command, viewid);
  183. CLISH_SET_ONCE(command, clish_param_t *, args);
  184. CLISH_GET(command, clish_param_t *, args);
  185. CLISH_GET(command, clish_paramv_t *, paramv);
  186. CLISH_SET(command, clish_view_t *, pview);
  187. CLISH_GET(command, clish_view_t *, pview);
  188. CLISH_SET_STR(command, access);
  189. CLISH_GET_STR(command, access);
  190. CLISH_SET_STR(command, alias);
  191. CLISH_GET_STR(command, alias);
  192. CLISH_SET_STR(command, alias_view);
  193. CLISH_GET_STR(command, alias_view);
  194. CLISH_SET(command, bool_t, internal);
  195. CLISH_GET(command, bool_t, internal);
  196. CLISH_SET(command, bool_t, dynamic);
  197. CLISH_GET(command, bool_t, dynamic);
  198. /*--------------------------------------------------------- */
  199. void clish_command__force_viewname(clish_command_t * this, const char *viewname)
  200. {
  201. if (this->viewname)
  202. lub_string_free(this->viewname);
  203. this->viewname = lub_string_dup(viewname);
  204. }
  205. /*--------------------------------------------------------- */
  206. void clish_command__force_viewid(clish_command_t * this, const char *viewid)
  207. {
  208. if (this->viewid)
  209. lub_string_free(this->viewid);
  210. this->viewid = lub_string_dup(viewid);
  211. }
  212. /*--------------------------------------------------------- */
  213. const clish_param_t *clish_command__get_param(const clish_command_t * this,
  214. unsigned index)
  215. {
  216. return clish_paramv__get_param(this->paramv, index);
  217. }
  218. /*--------------------------------------------------------- */
  219. const char *clish_command__get_suffix(const clish_command_t * this)
  220. {
  221. return lub_string_suffix(this->name);
  222. }
  223. /*--------------------------------------------------------- */
  224. unsigned int clish_command__get_param_count(const clish_command_t * this)
  225. {
  226. return clish_paramv__get_count(this->paramv);
  227. }
  228. /*--------------------------------------------------------- */
  229. int clish_command__get_depth(const clish_command_t * this)
  230. {
  231. if (!this->pview)
  232. return 0;
  233. return clish_view__get_depth(this->pview);
  234. }
  235. /*--------------------------------------------------------- */
  236. clish_view_restore_e clish_command__get_restore(const clish_command_t * this)
  237. {
  238. if (!this->pview)
  239. return CLISH_RESTORE_NONE;
  240. return clish_view__get_restore(this->pview);
  241. }
  242. /*--------------------------------------------------------- */
  243. const clish_command_t * clish_command__get_orig(const clish_command_t * this)
  244. {
  245. if (this->link)
  246. return clish_command__get_orig(this->link);
  247. return this;
  248. }
  249. /*--------------------------------------------------------- */
  250. const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
  251. {
  252. if (!this->dynamic)
  253. return this;
  254. if (this->link)
  255. return clish_command__get_cmd(this->link);
  256. return NULL;
  257. }