command.c 17 KB

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