command.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 "clish/variable.h"
  9. #include "lub/bintree.h"
  10. #include "lub/string.h"
  11. #include <assert.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. /*---------------------------------------------------------
  16. * PRIVATE METHODS
  17. *--------------------------------------------------------- */
  18. static void
  19. clish_command_init(clish_command_t *this, const char *name, const char *text)
  20. {
  21. /* initialise the node part */
  22. this->name = lub_string_dup(name);
  23. this->text = lub_string_dup(text);
  24. /* Be a good binary tree citizen */
  25. lub_bintree_node_init(&this->bt_node);
  26. /* set up defaults */
  27. this->link = NULL;
  28. this->alias = NULL;
  29. this->alias_view = NULL;
  30. this->paramv = clish_paramv_new();
  31. this->viewid = NULL;
  32. this->view = NULL;
  33. this->action = clish_action_new();
  34. this->config = clish_config_new();
  35. this->detail = NULL;
  36. this->escape_chars = NULL;
  37. this->args = NULL;
  38. this->pview = NULL;
  39. this->lock = BOOL_TRUE;
  40. this->interrupt = BOOL_FALSE;
  41. this->dynamic = BOOL_FALSE;
  42. }
  43. /*--------------------------------------------------------- */
  44. static void clish_command_fini(clish_command_t * this)
  45. {
  46. lub_string_free(this->name);
  47. lub_string_free(this->text);
  48. /* Link need not full cleanup */
  49. if (this->link)
  50. return;
  51. /* finalize each of the parameter instances */
  52. clish_paramv_delete(this->paramv);
  53. lub_string_free(this->alias);
  54. lub_string_free(this->viewid);
  55. clish_action_delete(this->action);
  56. clish_config_delete(this->config);
  57. lub_string_free(this->detail);
  58. lub_string_free(this->escape_chars);
  59. if (this->args)
  60. clish_param_delete(this->args);
  61. }
  62. /*---------------------------------------------------------
  63. * PUBLIC META FUNCTIONS
  64. *--------------------------------------------------------- */
  65. size_t clish_command_bt_offset(void)
  66. {
  67. return offsetof(clish_command_t, bt_node);
  68. }
  69. /*--------------------------------------------------------- */
  70. int clish_command_bt_compare(const void *clientnode, const void *clientkey)
  71. {
  72. const clish_command_t *this = clientnode;
  73. const char *key = clientkey;
  74. return lub_string_nocasecmp(this->name, key);
  75. }
  76. /*--------------------------------------------------------- */
  77. void clish_command_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  78. {
  79. const clish_command_t *this = clientnode;
  80. /* fill out the opaque key */
  81. strcpy((char *)key, this->name);
  82. }
  83. /*--------------------------------------------------------- */
  84. clish_command_t *clish_command_new(const char *name, const char *help)
  85. {
  86. clish_command_t *this = malloc(sizeof(clish_command_t));
  87. if (this)
  88. clish_command_init(this, name, help);
  89. return this;
  90. }
  91. /*--------------------------------------------------------- */
  92. clish_command_t *clish_command_new_link(const char *name,
  93. const char *help, const clish_command_t * ref)
  94. {
  95. if (!ref)
  96. return NULL;
  97. clish_command_t *this = malloc(sizeof(clish_command_t));
  98. assert(this);
  99. /* Copy all fields to the new command-link */
  100. *this = *ref;
  101. /* Initialise the name (other than original name) */
  102. this->name = lub_string_dup(name);
  103. /* Initialise the name (other than original name) */
  104. this->text = lub_string_dup(help);
  105. /* Be a good binary tree citizen */
  106. lub_bintree_node_init(&this->bt_node);
  107. /* It a link to command so set the link flag */
  108. this->link = ref;
  109. return this;
  110. }
  111. /*--------------------------------------------------------- */
  112. clish_command_t * clish_command_alias_to_link(clish_command_t * this)
  113. {
  114. clish_command_t * ref;
  115. clish_command_t tmp;
  116. if (!this || !this->alias)
  117. return this;
  118. assert(this->alias_view);
  119. ref = clish_view_find_command(this->alias_view, this->alias, BOOL_FALSE);
  120. if (!ref)
  121. return this;
  122. memcpy(&tmp, this, sizeof(tmp));
  123. *this = *ref;
  124. memcpy(&this->bt_node, &tmp.bt_node, sizeof(tmp.bt_node));
  125. this->name = lub_string_dup(tmp.name);
  126. this->text = lub_string_dup(tmp.text);
  127. this->link = ref;
  128. clish_command_fini(&tmp);
  129. return this;
  130. }
  131. /*---------------------------------------------------------
  132. * PUBLIC METHODS
  133. *--------------------------------------------------------- */
  134. void clish_command_delete(clish_command_t * this)
  135. {
  136. clish_command_fini(this);
  137. free(this);
  138. }
  139. /*--------------------------------------------------------- */
  140. void clish_command_insert_param(clish_command_t * this, clish_param_t * param)
  141. {
  142. clish_paramv_insert(this->paramv, param);
  143. }
  144. /*--------------------------------------------------------- */
  145. int clish_command_help(const clish_command_t *this)
  146. {
  147. return 0;
  148. }
  149. /*--------------------------------------------------------- */
  150. clish_command_t *clish_command_choose_longest(clish_command_t * cmd1,
  151. clish_command_t * cmd2)
  152. {
  153. unsigned len1 = (cmd1 ? strlen(clish_command__get_name(cmd1)) : 0);
  154. unsigned len2 = (cmd2 ? strlen(clish_command__get_name(cmd2)) : 0);
  155. if (len2 < len1) {
  156. return cmd1;
  157. } else if (len1 < len2) {
  158. return cmd2;
  159. } else {
  160. /* let local view override */
  161. return cmd1;
  162. }
  163. }
  164. /*--------------------------------------------------------- */
  165. int clish_command_diff(const clish_command_t * cmd1,
  166. const clish_command_t * cmd2)
  167. {
  168. if (NULL == cmd1) {
  169. if (NULL != cmd2)
  170. return 1;
  171. else
  172. return 0;
  173. }
  174. if (NULL == cmd2)
  175. return -1;
  176. return lub_string_nocasecmp(clish_command__get_name(cmd1),
  177. clish_command__get_name(cmd2));
  178. }
  179. /*---------------------------------------------------------
  180. * PUBLIC ATTRIBUTES
  181. *--------------------------------------------------------- */
  182. const char *clish_command__get_name(const clish_command_t * this)
  183. {
  184. if (!this)
  185. return NULL;
  186. return this->name;
  187. }
  188. /*--------------------------------------------------------- */
  189. const char *clish_command__get_text(const clish_command_t * this)
  190. {
  191. return this->text;
  192. }
  193. /*--------------------------------------------------------- */
  194. const char *clish_command__get_detail(const clish_command_t * this)
  195. {
  196. return this->detail;
  197. }
  198. /*--------------------------------------------------------- */
  199. void clish_command__set_detail(clish_command_t * this, const char *detail)
  200. {
  201. assert(NULL == this->detail);
  202. this->detail = lub_string_dup(detail);
  203. }
  204. /*--------------------------------------------------------- */
  205. clish_action_t *clish_command__get_action(const clish_command_t *this)
  206. {
  207. return this->action;
  208. }
  209. /*--------------------------------------------------------- */
  210. clish_config_t *clish_command__get_config(const clish_command_t *this)
  211. {
  212. return this->config;
  213. }
  214. /*--------------------------------------------------------- */
  215. void clish_command__set_view(clish_command_t * this, clish_view_t * view)
  216. {
  217. assert(NULL == this->view);
  218. clish_command__force_view(this, view);
  219. }
  220. /*--------------------------------------------------------- */
  221. void clish_command__force_view(clish_command_t * this, clish_view_t * view)
  222. {
  223. this->view = view;
  224. }
  225. /*--------------------------------------------------------- */
  226. clish_view_t *clish_command__get_view(const clish_command_t * this)
  227. {
  228. return this->view;
  229. }
  230. /*--------------------------------------------------------- */
  231. void clish_command__set_viewid(clish_command_t * this, const char *viewid)
  232. {
  233. assert(NULL == this->viewid);
  234. clish_command__force_viewid(this, viewid);
  235. }
  236. /*--------------------------------------------------------- */
  237. void clish_command__force_viewid(clish_command_t * this, const char *viewid)
  238. {
  239. this->viewid = lub_string_dup(viewid);
  240. }
  241. /*--------------------------------------------------------- */
  242. char *clish_command__get_viewid(const clish_command_t * this)
  243. {
  244. return this->viewid;
  245. }
  246. /*--------------------------------------------------------- */
  247. const clish_param_t *clish_command__get_param(const clish_command_t * this,
  248. unsigned index)
  249. {
  250. return clish_paramv__get_param(this->paramv, index);
  251. }
  252. /*--------------------------------------------------------- */
  253. const char *clish_command__get_suffix(const clish_command_t * this)
  254. {
  255. return lub_string_suffix(this->name);
  256. }
  257. /*--------------------------------------------------------- */
  258. void
  259. clish_command__set_escape_chars(clish_command_t * this,
  260. const char *escape_chars)
  261. {
  262. assert(NULL == this->escape_chars);
  263. this->escape_chars = lub_string_dup(escape_chars);
  264. }
  265. /*--------------------------------------------------------- */
  266. const char *clish_command__get_escape_chars(const clish_command_t * this)
  267. {
  268. return this->escape_chars;
  269. }
  270. /*--------------------------------------------------------- */
  271. void clish_command__set_args(clish_command_t * this, clish_param_t * args)
  272. {
  273. assert(NULL == this->args);
  274. this->args = args;
  275. }
  276. /*--------------------------------------------------------- */
  277. const clish_param_t *clish_command__get_args(const clish_command_t * this)
  278. {
  279. return this->args;
  280. }
  281. /*--------------------------------------------------------- */
  282. const unsigned clish_command__get_param_count(const clish_command_t * this)
  283. {
  284. return clish_paramv__get_count(this->paramv);
  285. }
  286. /*--------------------------------------------------------- */
  287. clish_paramv_t *clish_command__get_paramv(const clish_command_t * this)
  288. {
  289. return this->paramv;
  290. }
  291. /*--------------------------------------------------------- */
  292. void clish_command__set_pview(clish_command_t * this, clish_view_t * view)
  293. {
  294. this->pview = view;
  295. }
  296. /*--------------------------------------------------------- */
  297. clish_view_t *clish_command__get_pview(const clish_command_t * this)
  298. {
  299. return this->pview;
  300. }
  301. /*--------------------------------------------------------- */
  302. unsigned clish_command__get_depth(const clish_command_t * this)
  303. {
  304. if (!this->pview)
  305. return 0;
  306. return clish_view__get_depth(this->pview);
  307. }
  308. /*--------------------------------------------------------- */
  309. clish_view_restore_t clish_command__get_restore(const clish_command_t * this)
  310. {
  311. if (!this->pview)
  312. return CLISH_RESTORE_NONE;
  313. return clish_view__get_restore(this->pview);
  314. }
  315. /*--------------------------------------------------------- */
  316. const clish_command_t * clish_command__get_orig(const clish_command_t * this)
  317. {
  318. if (this->link)
  319. return clish_command__get_orig(this->link);
  320. return this;
  321. }
  322. /*--------------------------------------------------------- */
  323. bool_t clish_command__get_lock(const clish_command_t * this)
  324. {
  325. return this->lock;
  326. }
  327. /*--------------------------------------------------------- */
  328. void clish_command__set_lock(clish_command_t * this, bool_t lock)
  329. {
  330. this->lock = lock;
  331. }
  332. /*--------------------------------------------------------- */
  333. void clish_command__set_alias(clish_command_t * this, const char * alias)
  334. {
  335. assert(!this->alias);
  336. this->alias = lub_string_dup(alias);
  337. }
  338. /*--------------------------------------------------------- */
  339. const char * clish_command__get_alias(const clish_command_t * this)
  340. {
  341. return this->alias;
  342. }
  343. /*--------------------------------------------------------- */
  344. void clish_command__set_alias_view(clish_command_t * this,
  345. clish_view_t * alias_view)
  346. {
  347. this->alias_view = alias_view;
  348. }
  349. /*--------------------------------------------------------- */
  350. clish_view_t * clish_command__get_alias_view(const clish_command_t * this)
  351. {
  352. return this->alias_view;
  353. }
  354. /*--------------------------------------------------------- */
  355. void clish_command__set_dynamic(clish_command_t * this, bool_t dynamic)
  356. {
  357. this->dynamic = dynamic;
  358. }
  359. /*--------------------------------------------------------- */
  360. bool_t clish_command__get_dynamic(const clish_command_t * this)
  361. {
  362. return this->dynamic;
  363. }
  364. /*--------------------------------------------------------- */
  365. const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
  366. {
  367. if (!this->dynamic)
  368. return this;
  369. if (this->link)
  370. return clish_command__get_cmd(this->link);
  371. return NULL;
  372. }
  373. /*--------------------------------------------------------- */
  374. bool_t clish_command__get_interrupt(const clish_command_t * this)
  375. {
  376. return this->interrupt;
  377. }
  378. /*--------------------------------------------------------- */
  379. void clish_command__set_interrupt(clish_command_t * this, bool_t interrupt)
  380. {
  381. this->interrupt = interrupt;
  382. }