command.c 13 KB

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