command.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. char *clish_command__expand_script(const clish_command_t *this, void *context)
  225. {
  226. return this->var_expand_fn(clish_action__get_script(this->action),
  227. context);
  228. }
  229. /*--------------------------------------------------------- */
  230. void clish_command__set_view(clish_command_t * this, clish_view_t * view)
  231. {
  232. assert(NULL == this->view);
  233. clish_command__force_view(this, view);
  234. }
  235. /*--------------------------------------------------------- */
  236. void clish_command__force_view(clish_command_t * this, clish_view_t * view)
  237. {
  238. this->view = view;
  239. }
  240. /*--------------------------------------------------------- */
  241. clish_view_t *clish_command__get_view(const clish_command_t * this)
  242. {
  243. return this->view;
  244. }
  245. /*--------------------------------------------------------- */
  246. void clish_command__set_viewid(clish_command_t * this, const char *viewid)
  247. {
  248. assert(NULL == this->viewid);
  249. clish_command__force_viewid(this, viewid);
  250. }
  251. /*--------------------------------------------------------- */
  252. void clish_command__force_viewid(clish_command_t * this, const char *viewid)
  253. {
  254. this->viewid = lub_string_dup(viewid);
  255. }
  256. /*--------------------------------------------------------- */
  257. char *clish_command__get_viewid(const clish_command_t * this, void *context)
  258. {
  259. return this->var_expand_fn(this->viewid, context);
  260. }
  261. /*--------------------------------------------------------- */
  262. const clish_param_t *clish_command__get_param(const clish_command_t * this,
  263. unsigned index)
  264. {
  265. return clish_paramv__get_param(this->paramv, index);
  266. }
  267. /*--------------------------------------------------------- */
  268. const char *clish_command__get_suffix(const clish_command_t * this)
  269. {
  270. return lub_string_suffix(this->name);
  271. }
  272. /*--------------------------------------------------------- */
  273. void
  274. clish_command__set_escape_chars(clish_command_t * this,
  275. const char *escape_chars)
  276. {
  277. assert(NULL == this->escape_chars);
  278. this->escape_chars = lub_string_dup(escape_chars);
  279. }
  280. /*--------------------------------------------------------- */
  281. const char *clish_command__get_escape_chars(const clish_command_t * this)
  282. {
  283. return this->escape_chars;
  284. }
  285. /*--------------------------------------------------------- */
  286. void clish_command__set_args(clish_command_t * this, clish_param_t * args)
  287. {
  288. assert(NULL == this->args);
  289. this->args = args;
  290. }
  291. /*--------------------------------------------------------- */
  292. const clish_param_t *clish_command__get_args(const clish_command_t * this)
  293. {
  294. return this->args;
  295. }
  296. /*--------------------------------------------------------- */
  297. const unsigned clish_command__get_param_count(const clish_command_t * this)
  298. {
  299. return clish_paramv__get_count(this->paramv);
  300. }
  301. /*--------------------------------------------------------- */
  302. clish_paramv_t *clish_command__get_paramv(const clish_command_t * this)
  303. {
  304. return this->paramv;
  305. }
  306. /*--------------------------------------------------------- */
  307. void clish_command__set_pview(clish_command_t * this, clish_view_t * view)
  308. {
  309. this->pview = view;
  310. }
  311. /*--------------------------------------------------------- */
  312. clish_view_t *clish_command__get_pview(const clish_command_t * this)
  313. {
  314. return this->pview;
  315. }
  316. /*--------------------------------------------------------- */
  317. unsigned clish_command__get_depth(const clish_command_t * this)
  318. {
  319. if (!this->pview)
  320. return 0;
  321. return clish_view__get_depth(this->pview);
  322. }
  323. /*--------------------------------------------------------- */
  324. void
  325. clish_command__set_cfg_op(clish_command_t * this,
  326. clish_config_operation_t operation)
  327. {
  328. this->cfg_op = operation;
  329. }
  330. /*--------------------------------------------------------- */
  331. clish_config_operation_t clish_command__get_cfg_op(const clish_command_t * this)
  332. {
  333. return this->cfg_op;
  334. }
  335. /*--------------------------------------------------------- */
  336. void clish_command__set_priority(clish_command_t * this, unsigned short priority)
  337. {
  338. this->priority = priority;
  339. }
  340. /*--------------------------------------------------------- */
  341. unsigned short clish_command__get_priority(const clish_command_t * this)
  342. {
  343. return this->priority;
  344. }
  345. /*--------------------------------------------------------- */
  346. void clish_command__set_pattern(clish_command_t * this, const char *pattern)
  347. {
  348. assert(NULL == this->pattern);
  349. this->pattern = lub_string_dup(pattern);
  350. }
  351. /*--------------------------------------------------------- */
  352. char *clish_command__get_pattern(const clish_command_t * this, void *context)
  353. {
  354. return this->var_expand_fn(this->pattern, context);
  355. }
  356. /*--------------------------------------------------------- */
  357. void clish_command__set_file(clish_command_t * this, const char *file)
  358. {
  359. assert(!this->file);
  360. this->file = lub_string_dup(file);
  361. }
  362. /*--------------------------------------------------------- */
  363. char *clish_command__get_file(const clish_command_t *this, void *context)
  364. {
  365. return this->var_expand_fn(this->file, context);
  366. }
  367. /*--------------------------------------------------------- */
  368. bool_t clish_command__get_splitter(const clish_command_t * this)
  369. {
  370. return this->splitter;
  371. }
  372. /*--------------------------------------------------------- */
  373. void clish_command__set_splitter(clish_command_t * this, bool_t splitter)
  374. {
  375. this->splitter = splitter;
  376. }
  377. /*--------------------------------------------------------- */
  378. void clish_command__set_seq(clish_command_t * this, const char * seq)
  379. {
  380. assert(!this->seq);
  381. this->seq = lub_string_dup(seq);
  382. }
  383. /*--------------------------------------------------------- */
  384. unsigned short clish_command__get_seq(const clish_command_t *this, void *context)
  385. {
  386. unsigned short num = 0;
  387. char *str;
  388. if (!this->seq)
  389. return 0;
  390. str = this->var_expand_fn(this->seq, context);
  391. if (str && (*str != '\0')) {
  392. long val = 0;
  393. char *endptr;
  394. val = strtol(str, &endptr, 0);
  395. if (endptr == str)
  396. num = 0;
  397. else if (val > 0xffff)
  398. num = 0xffff;
  399. else if (val < 0)
  400. num = 0;
  401. else
  402. num = (unsigned short)val;
  403. }
  404. lub_string_free(str);
  405. return num;
  406. }
  407. /*--------------------------------------------------------- */
  408. const char * clish_command__is_seq(const clish_command_t * this)
  409. {
  410. return this->seq;
  411. }
  412. /*--------------------------------------------------------- */
  413. clish_view_restore_t clish_command__get_restore(const clish_command_t * this)
  414. {
  415. if (!this->pview)
  416. return CLISH_RESTORE_NONE;
  417. return clish_view__get_restore(this->pview);
  418. }
  419. /*--------------------------------------------------------- */
  420. bool_t clish_command__get_unique(const clish_command_t * this)
  421. {
  422. return this->unique;
  423. }
  424. /*--------------------------------------------------------- */
  425. void clish_command__set_unique(clish_command_t * this, bool_t unique)
  426. {
  427. this->unique = unique;
  428. }
  429. /*--------------------------------------------------------- */
  430. const clish_command_t * clish_command__get_orig(const clish_command_t * this)
  431. {
  432. if (this->link)
  433. return clish_command__get_orig(this->link);
  434. return this;
  435. }
  436. /*--------------------------------------------------------- */
  437. void clish_command__set_cfg_depth(clish_command_t * this, const char * cfg_depth)
  438. {
  439. assert(!this->cfg_depth);
  440. this->cfg_depth = lub_string_dup(cfg_depth);
  441. }
  442. /*--------------------------------------------------------- */
  443. unsigned clish_command__get_cfg_depth(const clish_command_t *this, void *context)
  444. {
  445. unsigned num = 0;
  446. char *str;
  447. if (!this->cfg_depth)
  448. return clish_command__get_depth(this);
  449. str = this->var_expand_fn(this->cfg_depth, context);
  450. if (str && (*str != '\0')) {
  451. long val = 0;
  452. char *endptr;
  453. val = strtol(str, &endptr, 0);
  454. if (endptr == str)
  455. num = 0;
  456. else if (val > 0xffff)
  457. num = 0xffff;
  458. else if (val < 0)
  459. num = 0;
  460. else
  461. num = (unsigned)val;
  462. }
  463. lub_string_free(str);
  464. return num;
  465. }
  466. /*--------------------------------------------------------- */
  467. bool_t clish_command__get_lock(const clish_command_t * this)
  468. {
  469. return this->lock;
  470. }
  471. /*--------------------------------------------------------- */
  472. void clish_command__set_lock(clish_command_t * this, bool_t lock)
  473. {
  474. this->lock = lock;
  475. }
  476. /*--------------------------------------------------------- */
  477. void clish_command__set_alias(clish_command_t * this, const char * alias)
  478. {
  479. assert(!this->alias);
  480. this->alias = lub_string_dup(alias);
  481. }
  482. /*--------------------------------------------------------- */
  483. const char * clish_command__get_alias(const clish_command_t * this)
  484. {
  485. return this->alias;
  486. }
  487. /*--------------------------------------------------------- */
  488. void clish_command__set_alias_view(clish_command_t * this,
  489. clish_view_t * alias_view)
  490. {
  491. this->alias_view = alias_view;
  492. }
  493. /*--------------------------------------------------------- */
  494. clish_view_t * clish_command__get_alias_view(const clish_command_t * this)
  495. {
  496. return this->alias_view;
  497. }
  498. /*--------------------------------------------------------- */
  499. void clish_command__set_dynamic(clish_command_t * this, bool_t dynamic)
  500. {
  501. this->dynamic = dynamic;
  502. }
  503. /*--------------------------------------------------------- */
  504. bool_t clish_command__get_dynamic(const clish_command_t * this)
  505. {
  506. return this->dynamic;
  507. }
  508. /*--------------------------------------------------------- */
  509. const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
  510. {
  511. if (!this->dynamic)
  512. return this;
  513. if (this->link)
  514. return clish_command__get_cmd(this->link);
  515. return NULL;
  516. }
  517. /*--------------------------------------------------------- */
  518. bool_t clish_command__get_interrupt(const clish_command_t * this)
  519. {
  520. return this->interrupt;
  521. }
  522. /*--------------------------------------------------------- */
  523. void clish_command__set_interrupt(clish_command_t * this, bool_t interrupt)
  524. {
  525. this->interrupt = interrupt;
  526. }