command.c 12 KB

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