command.c 8.2 KB

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