command.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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->detail = NULL;
  37. this->escape_chars = NULL;
  38. this->args = NULL;
  39. this->pview = NULL;
  40. this->lock = BOOL_TRUE;
  41. this->interrupt = BOOL_FALSE;
  42. this->dynamic = BOOL_FALSE;
  43. /* CONFIG params */
  44. this->cfg_op = CLISH_CONFIG_NONE;
  45. this->priority = 0; /* medium priority by default */
  46. this->pattern = NULL;
  47. this->file = NULL;
  48. this->splitter = BOOL_TRUE;
  49. this->seq = NULL;
  50. this->unique = BOOL_TRUE;
  51. this->cfg_depth = NULL;
  52. }
  53. /*--------------------------------------------------------- */
  54. static void clish_command_fini(clish_command_t * this)
  55. {
  56. lub_string_free(this->name);
  57. lub_string_free(this->text);
  58. /* Link need not full cleanup */
  59. if (this->link)
  60. return;
  61. /* finalize each of the parameter instances */
  62. clish_paramv_delete(this->paramv);
  63. lub_string_free(this->alias);
  64. lub_string_free(this->viewid);
  65. clish_action_delete(this->action);
  66. lub_string_free(this->detail);
  67. lub_string_free(this->escape_chars);
  68. if (this->args)
  69. clish_param_delete(this->args);
  70. lub_string_free(this->pattern);
  71. lub_string_free(this->file);
  72. lub_string_free(this->seq);
  73. lub_string_free(this->cfg_depth);
  74. }
  75. /*---------------------------------------------------------
  76. * PUBLIC META FUNCTIONS
  77. *--------------------------------------------------------- */
  78. size_t clish_command_bt_offset(void)
  79. {
  80. return offsetof(clish_command_t, bt_node);
  81. }
  82. /*--------------------------------------------------------- */
  83. int clish_command_bt_compare(const void *clientnode, const void *clientkey)
  84. {
  85. const clish_command_t *this = clientnode;
  86. const char *key = clientkey;
  87. return lub_string_nocasecmp(this->name, key);
  88. }
  89. /*--------------------------------------------------------- */
  90. void clish_command_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  91. {
  92. const clish_command_t *this = clientnode;
  93. /* fill out the opaque key */
  94. strcpy((char *)key, this->name);
  95. }
  96. /*--------------------------------------------------------- */
  97. clish_command_t *clish_command_new(const char *name, const char *help,
  98. clish_var_expand_fn_t *fn)
  99. {
  100. clish_command_t *this = malloc(sizeof(clish_command_t));
  101. if (this)
  102. clish_command_init(this, name, help, fn);
  103. return this;
  104. }
  105. /*--------------------------------------------------------- */
  106. clish_command_t *clish_command_new_link(const char *name,
  107. const char *help, const clish_command_t * ref)
  108. {
  109. if (!ref)
  110. return NULL;
  111. clish_command_t *this = malloc(sizeof(clish_command_t));
  112. assert(this);
  113. /* Copy all fields to the new command-link */
  114. *this = *ref;
  115. /* Initialise the name (other than original name) */
  116. this->name = lub_string_dup(name);
  117. /* Initialise the name (other than original name) */
  118. this->text = lub_string_dup(help);
  119. /* Be a good binary tree citizen */
  120. lub_bintree_node_init(&this->bt_node);
  121. /* It a link to command so set the link flag */
  122. this->link = ref;
  123. return this;
  124. }
  125. /*--------------------------------------------------------- */
  126. clish_command_t * clish_command_alias_to_link(clish_command_t * this)
  127. {
  128. clish_command_t * ref;
  129. clish_command_t tmp;
  130. if (!this || !this->alias)
  131. return this;
  132. assert(this->alias_view);
  133. ref = clish_view_find_command(this->alias_view, this->alias, BOOL_FALSE);
  134. if (!ref)
  135. return this;
  136. memcpy(&tmp, this, sizeof(tmp));
  137. *this = *ref;
  138. memcpy(&this->bt_node, &tmp.bt_node, sizeof(tmp.bt_node));
  139. this->name = lub_string_dup(tmp.name);
  140. this->text = lub_string_dup(tmp.text);
  141. this->link = ref;
  142. clish_command_fini(&tmp);
  143. return this;
  144. }
  145. /*---------------------------------------------------------
  146. * PUBLIC METHODS
  147. *--------------------------------------------------------- */
  148. void clish_command_delete(clish_command_t * this)
  149. {
  150. clish_command_fini(this);
  151. free(this);
  152. }
  153. /*--------------------------------------------------------- */
  154. void clish_command_insert_param(clish_command_t * this, clish_param_t * param)
  155. {
  156. clish_paramv_insert(this->paramv, param);
  157. }
  158. /*--------------------------------------------------------- */
  159. int clish_command_help(const clish_command_t *this)
  160. {
  161. return 0;
  162. }
  163. /*--------------------------------------------------------- */
  164. clish_command_t *clish_command_choose_longest(clish_command_t * cmd1,
  165. clish_command_t * cmd2)
  166. {
  167. unsigned len1 = (cmd1 ? strlen(clish_command__get_name(cmd1)) : 0);
  168. unsigned len2 = (cmd2 ? strlen(clish_command__get_name(cmd2)) : 0);
  169. if (len2 < len1) {
  170. return cmd1;
  171. } else if (len1 < len2) {
  172. return cmd2;
  173. } else {
  174. /* let local view override */
  175. return cmd1;
  176. }
  177. }
  178. /*--------------------------------------------------------- */
  179. int clish_command_diff(const clish_command_t * cmd1,
  180. const clish_command_t * cmd2)
  181. {
  182. if (NULL == cmd1) {
  183. if (NULL != cmd2)
  184. return 1;
  185. else
  186. return 0;
  187. }
  188. if (NULL == cmd2)
  189. return -1;
  190. return lub_string_nocasecmp(clish_command__get_name(cmd1),
  191. clish_command__get_name(cmd2));
  192. }
  193. /*---------------------------------------------------------
  194. * PUBLIC ATTRIBUTES
  195. *--------------------------------------------------------- */
  196. const char *clish_command__get_name(const clish_command_t * this)
  197. {
  198. if (!this)
  199. return NULL;
  200. return this->name;
  201. }
  202. /*--------------------------------------------------------- */
  203. const char *clish_command__get_text(const clish_command_t * this)
  204. {
  205. return this->text;
  206. }
  207. /*--------------------------------------------------------- */
  208. const char *clish_command__get_detail(const clish_command_t * this)
  209. {
  210. return this->detail;
  211. }
  212. /*--------------------------------------------------------- */
  213. void clish_command__set_detail(clish_command_t * this, const char *detail)
  214. {
  215. assert(NULL == this->detail);
  216. this->detail = lub_string_dup(detail);
  217. }
  218. /*--------------------------------------------------------- */
  219. clish_action_t *clish_command__get_action(const clish_command_t *this)
  220. {
  221. return this->action;
  222. }
  223. /*--------------------------------------------------------- */
  224. void clish_command__set_view(clish_command_t * this, clish_view_t * view)
  225. {
  226. assert(NULL == this->view);
  227. clish_command__force_view(this, view);
  228. }
  229. /*--------------------------------------------------------- */
  230. void clish_command__force_view(clish_command_t * this, clish_view_t * view)
  231. {
  232. this->view = view;
  233. }
  234. /*--------------------------------------------------------- */
  235. clish_view_t *clish_command__get_view(const clish_command_t * this)
  236. {
  237. return this->view;
  238. }
  239. /*--------------------------------------------------------- */
  240. void clish_command__set_viewid(clish_command_t * this, const char *viewid)
  241. {
  242. assert(NULL == this->viewid);
  243. clish_command__force_viewid(this, viewid);
  244. }
  245. /*--------------------------------------------------------- */
  246. void clish_command__force_viewid(clish_command_t * this, const char *viewid)
  247. {
  248. this->viewid = lub_string_dup(viewid);
  249. }
  250. /*--------------------------------------------------------- */
  251. char *clish_command__get_viewid(const clish_command_t * this, void *context)
  252. {
  253. return this->var_expand_fn(this->viewid, context);
  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(NULL == 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_args(clish_command_t * this, clish_param_t * args)
  281. {
  282. assert(NULL == this->args);
  283. this->args = args;
  284. }
  285. /*--------------------------------------------------------- */
  286. const clish_param_t *clish_command__get_args(const clish_command_t * this)
  287. {
  288. return this->args;
  289. }
  290. /*--------------------------------------------------------- */
  291. const unsigned clish_command__get_param_count(const clish_command_t * this)
  292. {
  293. return clish_paramv__get_count(this->paramv);
  294. }
  295. /*--------------------------------------------------------- */
  296. clish_paramv_t *clish_command__get_paramv(const clish_command_t * this)
  297. {
  298. return this->paramv;
  299. }
  300. /*--------------------------------------------------------- */
  301. void clish_command__set_pview(clish_command_t * this, clish_view_t * view)
  302. {
  303. this->pview = view;
  304. }
  305. /*--------------------------------------------------------- */
  306. clish_view_t *clish_command__get_pview(const clish_command_t * this)
  307. {
  308. return this->pview;
  309. }
  310. /*--------------------------------------------------------- */
  311. unsigned clish_command__get_depth(const clish_command_t * this)
  312. {
  313. if (!this->pview)
  314. return 0;
  315. return clish_view__get_depth(this->pview);
  316. }
  317. /*--------------------------------------------------------- */
  318. void
  319. clish_command__set_cfg_op(clish_command_t * this,
  320. clish_config_operation_t operation)
  321. {
  322. this->cfg_op = operation;
  323. }
  324. /*--------------------------------------------------------- */
  325. clish_config_operation_t clish_command__get_cfg_op(const clish_command_t * this)
  326. {
  327. return this->cfg_op;
  328. }
  329. /*--------------------------------------------------------- */
  330. void clish_command__set_priority(clish_command_t * this, unsigned short priority)
  331. {
  332. this->priority = priority;
  333. }
  334. /*--------------------------------------------------------- */
  335. unsigned short clish_command__get_priority(const clish_command_t * this)
  336. {
  337. return this->priority;
  338. }
  339. /*--------------------------------------------------------- */
  340. void clish_command__set_pattern(clish_command_t * this, const char *pattern)
  341. {
  342. assert(NULL == this->pattern);
  343. this->pattern = lub_string_dup(pattern);
  344. }
  345. /*--------------------------------------------------------- */
  346. char *clish_command__get_pattern(const clish_command_t * this, void *context)
  347. {
  348. return this->var_expand_fn(this->pattern, context);
  349. }
  350. /*--------------------------------------------------------- */
  351. void clish_command__set_file(clish_command_t * this, const char *file)
  352. {
  353. assert(!this->file);
  354. this->file = lub_string_dup(file);
  355. }
  356. /*--------------------------------------------------------- */
  357. char *clish_command__get_file(const clish_command_t *this, void *context)
  358. {
  359. return this->var_expand_fn(this->file, context);
  360. }
  361. /*--------------------------------------------------------- */
  362. bool_t clish_command__get_splitter(const clish_command_t * this)
  363. {
  364. return this->splitter;
  365. }
  366. /*--------------------------------------------------------- */
  367. void clish_command__set_splitter(clish_command_t * this, bool_t splitter)
  368. {
  369. this->splitter = splitter;
  370. }
  371. /*--------------------------------------------------------- */
  372. void clish_command__set_seq(clish_command_t * this, const char * seq)
  373. {
  374. assert(!this->seq);
  375. this->seq = lub_string_dup(seq);
  376. }
  377. /*--------------------------------------------------------- */
  378. unsigned short clish_command__get_seq(const clish_command_t *this, void *context)
  379. {
  380. unsigned short num = 0;
  381. char *str;
  382. if (!this->seq)
  383. return 0;
  384. str = this->var_expand_fn(this->seq, context);
  385. if (str && (*str != '\0')) {
  386. long val = 0;
  387. char *endptr;
  388. val = strtol(str, &endptr, 0);
  389. if (endptr == str)
  390. num = 0;
  391. else if (val > 0xffff)
  392. num = 0xffff;
  393. else if (val < 0)
  394. num = 0;
  395. else
  396. num = (unsigned short)val;
  397. }
  398. lub_string_free(str);
  399. return num;
  400. }
  401. /*--------------------------------------------------------- */
  402. const char * clish_command__is_seq(const clish_command_t * this)
  403. {
  404. return this->seq;
  405. }
  406. /*--------------------------------------------------------- */
  407. clish_view_restore_t clish_command__get_restore(const clish_command_t * this)
  408. {
  409. if (!this->pview)
  410. return CLISH_RESTORE_NONE;
  411. return clish_view__get_restore(this->pview);
  412. }
  413. /*--------------------------------------------------------- */
  414. bool_t clish_command__get_unique(const clish_command_t * this)
  415. {
  416. return this->unique;
  417. }
  418. /*--------------------------------------------------------- */
  419. void clish_command__set_unique(clish_command_t * this, bool_t unique)
  420. {
  421. this->unique = unique;
  422. }
  423. /*--------------------------------------------------------- */
  424. const clish_command_t * clish_command__get_orig(const clish_command_t * this)
  425. {
  426. if (this->link)
  427. return clish_command__get_orig(this->link);
  428. return this;
  429. }
  430. /*--------------------------------------------------------- */
  431. void clish_command__set_cfg_depth(clish_command_t * this, const char * cfg_depth)
  432. {
  433. assert(!this->cfg_depth);
  434. this->cfg_depth = lub_string_dup(cfg_depth);
  435. }
  436. /*--------------------------------------------------------- */
  437. unsigned clish_command__get_cfg_depth(const clish_command_t *this, void *context)
  438. {
  439. unsigned num = 0;
  440. char *str;
  441. if (!this->cfg_depth)
  442. return clish_command__get_depth(this);
  443. str = this->var_expand_fn(this->cfg_depth, context);
  444. if (str && (*str != '\0')) {
  445. long val = 0;
  446. char *endptr;
  447. val = strtol(str, &endptr, 0);
  448. if (endptr == str)
  449. num = 0;
  450. else if (val > 0xffff)
  451. num = 0xffff;
  452. else if (val < 0)
  453. num = 0;
  454. else
  455. num = (unsigned)val;
  456. }
  457. lub_string_free(str);
  458. return num;
  459. }
  460. /*--------------------------------------------------------- */
  461. bool_t clish_command__get_lock(const clish_command_t * this)
  462. {
  463. return this->lock;
  464. }
  465. /*--------------------------------------------------------- */
  466. void clish_command__set_lock(clish_command_t * this, bool_t lock)
  467. {
  468. this->lock = lock;
  469. }
  470. /*--------------------------------------------------------- */
  471. void clish_command__set_alias(clish_command_t * this, const char * alias)
  472. {
  473. assert(!this->alias);
  474. this->alias = lub_string_dup(alias);
  475. }
  476. /*--------------------------------------------------------- */
  477. const char * clish_command__get_alias(const clish_command_t * this)
  478. {
  479. return this->alias;
  480. }
  481. /*--------------------------------------------------------- */
  482. void clish_command__set_alias_view(clish_command_t * this,
  483. clish_view_t * alias_view)
  484. {
  485. this->alias_view = alias_view;
  486. }
  487. /*--------------------------------------------------------- */
  488. clish_view_t * clish_command__get_alias_view(const clish_command_t * this)
  489. {
  490. return this->alias_view;
  491. }
  492. /*--------------------------------------------------------- */
  493. void clish_command__set_dynamic(clish_command_t * this, bool_t dynamic)
  494. {
  495. this->dynamic = dynamic;
  496. }
  497. /*--------------------------------------------------------- */
  498. bool_t clish_command__get_dynamic(const clish_command_t * this)
  499. {
  500. return this->dynamic;
  501. }
  502. /*--------------------------------------------------------- */
  503. const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
  504. {
  505. if (!this->dynamic)
  506. return this;
  507. if (this->link)
  508. return clish_command__get_cmd(this->link);
  509. return NULL;
  510. }
  511. /*--------------------------------------------------------- */
  512. bool_t clish_command__get_interrupt(const clish_command_t * this)
  513. {
  514. return this->interrupt;
  515. }
  516. /*--------------------------------------------------------- */
  517. void clish_command__set_interrupt(clish_command_t * this, bool_t interrupt)
  518. {
  519. this->interrupt = interrupt;
  520. }