1
0

command.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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->lock = BOOL_TRUE;
  37. this->interrupt = BOOL_FALSE;
  38. this->dynamic = BOOL_FALSE;
  39. this->internal = BOOL_FALSE;
  40. this->access = NULL;
  41. }
  42. /*--------------------------------------------------------- */
  43. static void clish_command_fini(clish_command_t * this)
  44. {
  45. lub_string_free(this->name);
  46. lub_string_free(this->text);
  47. /* Link need not full cleanup */
  48. if (this->link)
  49. return;
  50. /* finalize each of the parameter instances */
  51. clish_paramv_delete(this->paramv);
  52. clish_action_delete(this->action);
  53. clish_config_delete(this->config);
  54. lub_string_free(this->alias);
  55. lub_string_free(this->alias_view);
  56. lub_string_free(this->viewname);
  57. lub_string_free(this->viewid);
  58. lub_string_free(this->detail);
  59. lub_string_free(this->escape_chars);
  60. lub_string_free(this->regex_chars);
  61. lub_string_free(this->access);
  62. if (this->args)
  63. clish_param_delete(this->args);
  64. }
  65. /*--------------------------------------------------------- */
  66. size_t clish_command_bt_offset(void)
  67. {
  68. return offsetof(clish_command_t, bt_node);
  69. }
  70. /*--------------------------------------------------------- */
  71. int clish_command_bt_compare(const void *clientnode, const void *clientkey)
  72. {
  73. const clish_command_t *this = clientnode;
  74. const char *key = clientkey;
  75. return lub_string_nocasecmp(this->name, key);
  76. }
  77. /*--------------------------------------------------------- */
  78. void clish_command_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  79. {
  80. const clish_command_t *this = clientnode;
  81. /* fill out the opaque key */
  82. strcpy((char *)key, this->name);
  83. }
  84. /*--------------------------------------------------------- */
  85. clish_command_t *clish_command_new(const char *name, const char *help)
  86. {
  87. clish_command_t *this = malloc(sizeof(clish_command_t));
  88. if (this)
  89. clish_command_init(this, name, help);
  90. return this;
  91. }
  92. /*--------------------------------------------------------- */
  93. clish_command_t *clish_command_new_link(const char *name,
  94. const char *help, const clish_command_t * ref)
  95. {
  96. if (!ref)
  97. return NULL;
  98. clish_command_t *this = malloc(sizeof(clish_command_t));
  99. assert(this);
  100. /* Copy all fields to the new command-link */
  101. *this = *ref;
  102. /* Initialise the name (other than original name) */
  103. this->name = lub_string_dup(name);
  104. /* Initialise the help (other than original help) */
  105. this->text = lub_string_dup(help);
  106. /* Be a good binary tree citizen */
  107. lub_bintree_node_init(&this->bt_node);
  108. /* It a link to command so set the link flag */
  109. this->link = ref;
  110. return this;
  111. }
  112. /*--------------------------------------------------------- */
  113. clish_command_t * clish_command_alias_to_link(clish_command_t *this, clish_command_t *ref)
  114. {
  115. clish_command_t tmp;
  116. if (!this || !ref)
  117. return NULL;
  118. if (ref->alias) /* The reference is a link too */
  119. return NULL;
  120. memcpy(&tmp, this, sizeof(tmp));
  121. *this = *ref;
  122. memcpy(&this->bt_node, &tmp.bt_node, sizeof(tmp.bt_node));
  123. this->name = lub_string_dup(tmp.name); /* Save an original name */
  124. this->text = lub_string_dup(tmp.text); /* Save an original help */
  125. this->link = ref;
  126. this->pview = tmp.pview; /* Save an original parent view */
  127. clish_command_fini(&tmp);
  128. return this;
  129. }
  130. /*--------------------------------------------------------- */
  131. void clish_command_delete(clish_command_t * this)
  132. {
  133. clish_command_fini(this);
  134. free(this);
  135. }
  136. /*--------------------------------------------------------- */
  137. void clish_command_insert_param(clish_command_t * this, clish_param_t * param)
  138. {
  139. clish_paramv_insert(this->paramv, param);
  140. }
  141. /*--------------------------------------------------------- */
  142. int clish_command_help(const clish_command_t *this)
  143. {
  144. this = this; /* Happy compiler */
  145. return 0;
  146. }
  147. /*--------------------------------------------------------- */
  148. clish_command_t *clish_command_choose_longest(clish_command_t * cmd1,
  149. clish_command_t * cmd2)
  150. {
  151. unsigned len1 = (cmd1 ? strlen(clish_command__get_name(cmd1)) : 0);
  152. unsigned len2 = (cmd2 ? strlen(clish_command__get_name(cmd2)) : 0);
  153. if (len2 < len1) {
  154. return cmd1;
  155. } else if (len1 < len2) {
  156. return cmd2;
  157. } else {
  158. /* let local view override */
  159. return cmd1;
  160. }
  161. }
  162. /*--------------------------------------------------------- */
  163. int clish_command_diff(const clish_command_t * cmd1,
  164. const clish_command_t * cmd2)
  165. {
  166. if (NULL == cmd1) {
  167. if (NULL != cmd2)
  168. return 1;
  169. else
  170. return 0;
  171. }
  172. if (NULL == cmd2)
  173. return -1;
  174. return lub_string_nocasecmp(clish_command__get_name(cmd1),
  175. clish_command__get_name(cmd2));
  176. }
  177. CLISH_GET_STR(command, name);
  178. CLISH_GET_STR(command, text);
  179. CLISH_SET_STR_ONCE(command, detail);
  180. CLISH_GET_STR(command, detail);
  181. CLISH_GET(command, clish_action_t *, action);
  182. CLISH_GET(command, clish_config_t *, config);
  183. CLISH_SET_STR_ONCE(command, regex_chars);
  184. CLISH_GET_STR(command, regex_chars);
  185. CLISH_SET_STR_ONCE(command, escape_chars);
  186. CLISH_GET_STR(command, escape_chars);
  187. CLISH_SET_STR_ONCE(command, viewname);
  188. CLISH_GET_STR(command, viewname);
  189. CLISH_SET_STR_ONCE(command, viewid);
  190. CLISH_GET_STR(command, viewid);
  191. CLISH_SET_ONCE(command, clish_param_t *, args);
  192. CLISH_GET(command, clish_param_t *, args);
  193. CLISH_GET(command, clish_paramv_t *, paramv);
  194. CLISH_SET(command, clish_view_t *, pview);
  195. CLISH_GET(command, clish_view_t *, pview);
  196. CLISH_SET_STR(command, access);
  197. CLISH_GET_STR(command, access);
  198. CLISH_SET_STR(command, alias);
  199. CLISH_GET_STR(command, alias);
  200. CLISH_SET_STR(command, alias_view);
  201. CLISH_GET_STR(command, alias_view);
  202. CLISH_SET(command, bool_t, interrupt);
  203. CLISH_GET(command, bool_t, interrupt);
  204. CLISH_SET(command, bool_t, internal);
  205. CLISH_GET(command, bool_t, internal);
  206. CLISH_SET(command, bool_t, dynamic);
  207. CLISH_GET(command, bool_t, dynamic);
  208. CLISH_SET(command, bool_t, lock);
  209. CLISH_GET(command, bool_t, lock);
  210. /*--------------------------------------------------------- */
  211. void clish_command__force_viewname(clish_command_t * this, const char *viewname)
  212. {
  213. if (this->viewname)
  214. lub_string_free(this->viewname);
  215. this->viewname = lub_string_dup(viewname);
  216. }
  217. /*--------------------------------------------------------- */
  218. void clish_command__force_viewid(clish_command_t * this, const char *viewid)
  219. {
  220. if (this->viewid)
  221. lub_string_free(this->viewid);
  222. this->viewid = lub_string_dup(viewid);
  223. }
  224. /*--------------------------------------------------------- */
  225. const clish_param_t *clish_command__get_param(const clish_command_t * this,
  226. unsigned index)
  227. {
  228. return clish_paramv__get_param(this->paramv, index);
  229. }
  230. /*--------------------------------------------------------- */
  231. const char *clish_command__get_suffix(const clish_command_t * this)
  232. {
  233. return lub_string_suffix(this->name);
  234. }
  235. /*--------------------------------------------------------- */
  236. unsigned int clish_command__get_param_count(const clish_command_t * this)
  237. {
  238. return clish_paramv__get_count(this->paramv);
  239. }
  240. /*--------------------------------------------------------- */
  241. int clish_command__get_depth(const clish_command_t * this)
  242. {
  243. if (!this->pview)
  244. return 0;
  245. return clish_view__get_depth(this->pview);
  246. }
  247. /*--------------------------------------------------------- */
  248. clish_view_restore_e clish_command__get_restore(const clish_command_t * this)
  249. {
  250. if (!this->pview)
  251. return CLISH_RESTORE_NONE;
  252. return clish_view__get_restore(this->pview);
  253. }
  254. /*--------------------------------------------------------- */
  255. const clish_command_t * clish_command__get_orig(const clish_command_t * this)
  256. {
  257. if (this->link)
  258. return clish_command__get_orig(this->link);
  259. return this;
  260. }
  261. /*--------------------------------------------------------- */
  262. const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
  263. {
  264. if (!this->dynamic)
  265. return this;
  266. if (this->link)
  267. return clish_command__get_cmd(this->link);
  268. return NULL;
  269. }