1
0

command.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. this->access = NULL;
  43. }
  44. /*--------------------------------------------------------- */
  45. static void clish_command_fini(clish_command_t * this)
  46. {
  47. lub_string_free(this->name);
  48. lub_string_free(this->text);
  49. /* Link need not full cleanup */
  50. if (this->link)
  51. return;
  52. /* finalize each of the parameter instances */
  53. clish_paramv_delete(this->paramv);
  54. clish_action_delete(this->action);
  55. clish_config_delete(this->config);
  56. lub_string_free(this->alias);
  57. lub_string_free(this->alias_view);
  58. lub_string_free(this->viewname);
  59. lub_string_free(this->viewid);
  60. lub_string_free(this->detail);
  61. lub_string_free(this->escape_chars);
  62. lub_string_free(this->regex_chars);
  63. lub_string_free(this->access);
  64. if (this->args)
  65. clish_param_delete(this->args);
  66. }
  67. /*---------------------------------------------------------
  68. * PUBLIC META FUNCTIONS
  69. *--------------------------------------------------------- */
  70. size_t clish_command_bt_offset(void)
  71. {
  72. return offsetof(clish_command_t, bt_node);
  73. }
  74. /*--------------------------------------------------------- */
  75. int clish_command_bt_compare(const void *clientnode, const void *clientkey)
  76. {
  77. const clish_command_t *this = clientnode;
  78. const char *key = clientkey;
  79. return lub_string_nocasecmp(this->name, key);
  80. }
  81. /*--------------------------------------------------------- */
  82. void clish_command_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  83. {
  84. const clish_command_t *this = clientnode;
  85. /* fill out the opaque key */
  86. strcpy((char *)key, this->name);
  87. }
  88. /*--------------------------------------------------------- */
  89. clish_command_t *clish_command_new(const char *name, const char *help)
  90. {
  91. clish_command_t *this = malloc(sizeof(clish_command_t));
  92. if (this)
  93. clish_command_init(this, name, help);
  94. return this;
  95. }
  96. /*--------------------------------------------------------- */
  97. clish_command_t *clish_command_new_link(const char *name,
  98. const char *help, const clish_command_t * ref)
  99. {
  100. if (!ref)
  101. return NULL;
  102. clish_command_t *this = malloc(sizeof(clish_command_t));
  103. assert(this);
  104. /* Copy all fields to the new command-link */
  105. *this = *ref;
  106. /* Initialise the name (other than original name) */
  107. this->name = lub_string_dup(name);
  108. /* Initialise the help (other than original help) */
  109. this->text = lub_string_dup(help);
  110. /* Be a good binary tree citizen */
  111. lub_bintree_node_init(&this->bt_node);
  112. /* It a link to command so set the link flag */
  113. this->link = ref;
  114. return this;
  115. }
  116. /*--------------------------------------------------------- */
  117. clish_command_t * clish_command_alias_to_link(clish_command_t *this, clish_command_t *ref)
  118. {
  119. clish_command_t tmp;
  120. if (!this || !ref)
  121. return NULL;
  122. if (ref->alias) /* The reference is a link too */
  123. return NULL;
  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); /* Save an original name */
  128. this->text = lub_string_dup(tmp.text); /* Save an original help */
  129. this->link = ref;
  130. this->pview = tmp.pview; /* Save an original parent view */
  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. this = this; /* Happy compiler */
  151. return 0;
  152. }
  153. /*--------------------------------------------------------- */
  154. clish_command_t *clish_command_choose_longest(clish_command_t * cmd1,
  155. clish_command_t * cmd2)
  156. {
  157. unsigned len1 = (cmd1 ? strlen(clish_command__get_name(cmd1)) : 0);
  158. unsigned len2 = (cmd2 ? strlen(clish_command__get_name(cmd2)) : 0);
  159. if (len2 < len1) {
  160. return cmd1;
  161. } else if (len1 < len2) {
  162. return cmd2;
  163. } else {
  164. /* let local view override */
  165. return cmd1;
  166. }
  167. }
  168. /*--------------------------------------------------------- */
  169. int clish_command_diff(const clish_command_t * cmd1,
  170. const clish_command_t * cmd2)
  171. {
  172. if (NULL == cmd1) {
  173. if (NULL != cmd2)
  174. return 1;
  175. else
  176. return 0;
  177. }
  178. if (NULL == cmd2)
  179. return -1;
  180. return lub_string_nocasecmp(clish_command__get_name(cmd1),
  181. clish_command__get_name(cmd2));
  182. }
  183. /*---------------------------------------------------------
  184. * PUBLIC ATTRIBUTES
  185. *--------------------------------------------------------- */
  186. const char *clish_command__get_name(const clish_command_t * this)
  187. {
  188. if (!this)
  189. return NULL;
  190. return this->name;
  191. }
  192. /*--------------------------------------------------------- */
  193. const char *clish_command__get_text(const clish_command_t * this)
  194. {
  195. return this->text;
  196. }
  197. /*--------------------------------------------------------- */
  198. const char *clish_command__get_detail(const clish_command_t * this)
  199. {
  200. return this->detail;
  201. }
  202. /*--------------------------------------------------------- */
  203. void clish_command__set_detail(clish_command_t * this, const char *detail)
  204. {
  205. assert(NULL == this->detail);
  206. this->detail = lub_string_dup(detail);
  207. }
  208. /*--------------------------------------------------------- */
  209. clish_action_t *clish_command__get_action(const clish_command_t *this)
  210. {
  211. return this->action;
  212. }
  213. /*--------------------------------------------------------- */
  214. clish_config_t *clish_command__get_config(const clish_command_t *this)
  215. {
  216. return this->config;
  217. }
  218. /*--------------------------------------------------------- */
  219. void clish_command__set_viewname(clish_command_t * this, const char *viewname)
  220. {
  221. assert(NULL == this->viewname);
  222. clish_command__force_viewname(this, viewname);
  223. }
  224. /*--------------------------------------------------------- */
  225. void clish_command__force_viewname(clish_command_t * this, const char *viewname)
  226. {
  227. if (this->viewname)
  228. lub_string_free(this->viewname);
  229. this->viewname = lub_string_dup(viewname);
  230. }
  231. /*--------------------------------------------------------- */
  232. char *clish_command__get_viewname(const clish_command_t * this)
  233. {
  234. return this->viewname;
  235. }
  236. /*--------------------------------------------------------- */
  237. void clish_command__set_viewid(clish_command_t * this, const char *viewid)
  238. {
  239. assert(NULL == this->viewid);
  240. clish_command__force_viewid(this, viewid);
  241. }
  242. /*--------------------------------------------------------- */
  243. void clish_command__force_viewid(clish_command_t * this, const char *viewid)
  244. {
  245. if (this->viewid)
  246. lub_string_free(this->viewid);
  247. this->viewid = lub_string_dup(viewid);
  248. }
  249. /*--------------------------------------------------------- */
  250. char *clish_command__get_viewid(const clish_command_t * this)
  251. {
  252. return this->viewid;
  253. }
  254. /*--------------------------------------------------------- */
  255. const clish_param_t *clish_command__get_param(const clish_command_t * this,
  256. unsigned index)
  257. {
  258. return clish_paramv__get_param(this->paramv, index);
  259. }
  260. /*--------------------------------------------------------- */
  261. const char *clish_command__get_suffix(const clish_command_t * this)
  262. {
  263. return lub_string_suffix(this->name);
  264. }
  265. /*--------------------------------------------------------- */
  266. void
  267. clish_command__set_escape_chars(clish_command_t * this,
  268. const char *escape_chars)
  269. {
  270. assert(!this->escape_chars);
  271. this->escape_chars = lub_string_dup(escape_chars);
  272. }
  273. /*--------------------------------------------------------- */
  274. const char *clish_command__get_escape_chars(const clish_command_t * this)
  275. {
  276. return this->escape_chars;
  277. }
  278. /*--------------------------------------------------------- */
  279. void clish_command__set_regex_chars(clish_command_t *this,
  280. const char *escape_chars)
  281. {
  282. assert(!this->regex_chars);
  283. this->regex_chars = lub_string_dup(escape_chars);
  284. }
  285. /*--------------------------------------------------------- */
  286. const char *clish_command__get_regex_chars(const clish_command_t *this)
  287. {
  288. return this->regex_chars;
  289. }
  290. /*--------------------------------------------------------- */
  291. void clish_command__set_args(clish_command_t * this, clish_param_t * args)
  292. {
  293. assert(NULL == this->args);
  294. this->args = args;
  295. }
  296. /*--------------------------------------------------------- */
  297. clish_param_t *clish_command__get_args(const clish_command_t * this)
  298. {
  299. return this->args;
  300. }
  301. /*--------------------------------------------------------- */
  302. unsigned int clish_command__get_param_count(const clish_command_t * this)
  303. {
  304. return clish_paramv__get_count(this->paramv);
  305. }
  306. /*--------------------------------------------------------- */
  307. clish_paramv_t *clish_command__get_paramv(const clish_command_t * this)
  308. {
  309. return this->paramv;
  310. }
  311. /*--------------------------------------------------------- */
  312. void clish_command__set_pview(clish_command_t * this, clish_view_t * view)
  313. {
  314. this->pview = view;
  315. }
  316. /*--------------------------------------------------------- */
  317. clish_view_t *clish_command__get_pview(const clish_command_t * this)
  318. {
  319. return this->pview;
  320. }
  321. /*--------------------------------------------------------- */
  322. int clish_command__get_depth(const clish_command_t * this)
  323. {
  324. if (!this->pview)
  325. return 0;
  326. return clish_view__get_depth(this->pview);
  327. }
  328. /*--------------------------------------------------------- */
  329. clish_view_restore_e clish_command__get_restore(const clish_command_t * this)
  330. {
  331. if (!this->pview)
  332. return CLISH_RESTORE_NONE;
  333. return clish_view__get_restore(this->pview);
  334. }
  335. /*--------------------------------------------------------- */
  336. const clish_command_t * clish_command__get_orig(const clish_command_t * this)
  337. {
  338. if (this->link)
  339. return clish_command__get_orig(this->link);
  340. return this;
  341. }
  342. /*--------------------------------------------------------- */
  343. bool_t clish_command__get_lock(const clish_command_t * this)
  344. {
  345. return this->lock;
  346. }
  347. /*--------------------------------------------------------- */
  348. void clish_command__set_lock(clish_command_t * this, bool_t lock)
  349. {
  350. this->lock = lock;
  351. }
  352. /*--------------------------------------------------------- */
  353. void clish_command__set_alias(clish_command_t * this, const char * alias)
  354. {
  355. if (this->alias)
  356. lub_string_free(this->alias);
  357. this->alias = lub_string_dup(alias);
  358. }
  359. /*--------------------------------------------------------- */
  360. const char * clish_command__get_alias(const clish_command_t * this)
  361. {
  362. return this->alias;
  363. }
  364. /*--------------------------------------------------------- */
  365. void clish_command__set_alias_view(clish_command_t *this,
  366. const char *alias_view)
  367. {
  368. if (this->alias_view)
  369. lub_string_free(this->alias_view);
  370. this->alias_view = lub_string_dup(alias_view);
  371. }
  372. /*--------------------------------------------------------- */
  373. const char * clish_command__get_alias_view(const clish_command_t * this)
  374. {
  375. return this->alias_view;
  376. }
  377. /*--------------------------------------------------------- */
  378. void clish_command__set_dynamic(clish_command_t * this, bool_t dynamic)
  379. {
  380. this->dynamic = dynamic;
  381. }
  382. /*--------------------------------------------------------- */
  383. bool_t clish_command__get_dynamic(const clish_command_t * this)
  384. {
  385. return this->dynamic;
  386. }
  387. /*--------------------------------------------------------- */
  388. const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
  389. {
  390. if (!this->dynamic)
  391. return this;
  392. if (this->link)
  393. return clish_command__get_cmd(this->link);
  394. return NULL;
  395. }
  396. /*--------------------------------------------------------- */
  397. bool_t clish_command__get_interrupt(const clish_command_t * this)
  398. {
  399. return this->interrupt;
  400. }
  401. /*--------------------------------------------------------- */
  402. void clish_command__set_interrupt(clish_command_t * this, bool_t interrupt)
  403. {
  404. this->interrupt = interrupt;
  405. }
  406. /*--------------------------------------------------------- */
  407. void clish_command__set_access(clish_command_t *this, const char *access)
  408. {
  409. if (this->access)
  410. lub_string_free(this->access);
  411. this->access = lub_string_dup(access);
  412. }
  413. /*--------------------------------------------------------- */
  414. char *clish_command__get_access(const clish_command_t *this)
  415. {
  416. return this->access;
  417. }