command.c 12 KB

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