command.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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;
  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, clish_help_t *help,
  181. const char * viewid, const char * line, size_t *max_width)
  182. {
  183. const char *name = clish_command__get_name(this);
  184. unsigned index = lub_argv_wordcount(line);
  185. unsigned idx = lub_argv_wordcount(name);
  186. lub_argv_t *argv;
  187. clish_pargv_t *last, *pargv;
  188. unsigned i;
  189. unsigned cnt = 0;
  190. clish_pargv_status_t status = CLISH_LINE_OK;
  191. /* Empty line */
  192. if (0 == index)
  193. return -1;
  194. if (line[strlen(line) - 1] != ' ')
  195. index--;
  196. argv = lub_argv_new(line, 0);
  197. /* get the parameter definition */
  198. last = clish_pargv_create();
  199. pargv = clish_pargv_create();
  200. status = clish_pargv_parse(pargv, this, viewid, this->paramv,
  201. argv, &idx, last, index);
  202. clish_pargv_delete(pargv);
  203. cnt = clish_pargv__get_count(last);
  204. /* Calculate the longest name */
  205. for (i = 0; i < cnt; i++) {
  206. const clish_param_t *param;
  207. const char *name;
  208. unsigned clen = 0;
  209. param = clish_pargv__get_param(last, i);
  210. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(param))
  211. name = clish_param__get_value(param);
  212. else
  213. name = clish_ptype__get_text(clish_param__get_ptype(param));
  214. if (name)
  215. clen = strlen(name);
  216. if (max_width && (clen > *max_width))
  217. *max_width = clen;
  218. clish_param_help(param, help);
  219. }
  220. clish_pargv_delete(last);
  221. lub_argv_delete(argv);
  222. /* It's a completed command */
  223. if (CLISH_LINE_OK == status)
  224. return 0;
  225. /* Incompleted command */
  226. return -1;
  227. }
  228. /*--------------------------------------------------------- */
  229. clish_command_t *clish_command_choose_longest(clish_command_t * cmd1,
  230. clish_command_t * cmd2)
  231. {
  232. unsigned len1 = (cmd1 ? strlen(clish_command__get_name(cmd1)) : 0);
  233. unsigned len2 = (cmd2 ? strlen(clish_command__get_name(cmd2)) : 0);
  234. if (len2 < len1) {
  235. return cmd1;
  236. } else if (len1 < len2) {
  237. return cmd2;
  238. } else {
  239. /* let local view override */
  240. return cmd1;
  241. }
  242. }
  243. /*--------------------------------------------------------- */
  244. int clish_command_diff(const clish_command_t * cmd1,
  245. const clish_command_t * cmd2)
  246. {
  247. if (NULL == cmd1) {
  248. if (NULL != cmd2)
  249. return 1;
  250. else
  251. return 0;
  252. }
  253. if (NULL == cmd2)
  254. return -1;
  255. return lub_string_nocasecmp(clish_command__get_name(cmd1),
  256. clish_command__get_name(cmd2));
  257. }
  258. /*---------------------------------------------------------
  259. * PUBLIC ATTRIBUTES
  260. *--------------------------------------------------------- */
  261. const char *clish_command__get_name(const clish_command_t * this)
  262. {
  263. if (!this)
  264. return NULL;
  265. return this->name;
  266. }
  267. /*--------------------------------------------------------- */
  268. const char *clish_command__get_text(const clish_command_t * this)
  269. {
  270. return this->text;
  271. }
  272. /*--------------------------------------------------------- */
  273. void clish_command__set_action(clish_command_t * this, const char *action)
  274. {
  275. assert(NULL == this->action);
  276. this->action = lub_string_dup(action);
  277. }
  278. /*--------------------------------------------------------- */
  279. const char *clish_command__get_detail(const clish_command_t * this)
  280. {
  281. return this->detail;
  282. }
  283. /*--------------------------------------------------------- */
  284. void clish_command__set_detail(clish_command_t * this, const char *detail)
  285. {
  286. assert(NULL == this->detail);
  287. this->detail = lub_string_dup(detail);
  288. }
  289. /*--------------------------------------------------------- */
  290. char *clish_command__get_action(const clish_command_t * this, void *context)
  291. {
  292. return this->var_expand_fn(this->action, context);
  293. }
  294. /*--------------------------------------------------------- */
  295. void clish_command__set_view(clish_command_t * this, clish_view_t * view)
  296. {
  297. assert(NULL == this->view);
  298. clish_command__force_view(this, view);
  299. }
  300. /*--------------------------------------------------------- */
  301. void clish_command__force_view(clish_command_t * this, clish_view_t * view)
  302. {
  303. this->view = view;
  304. }
  305. /*--------------------------------------------------------- */
  306. clish_view_t *clish_command__get_view(const clish_command_t * this)
  307. {
  308. return this->view;
  309. }
  310. /*--------------------------------------------------------- */
  311. void clish_command__set_viewid(clish_command_t * this, const char *viewid)
  312. {
  313. assert(NULL == this->viewid);
  314. clish_command__force_viewid(this, viewid);
  315. }
  316. /*--------------------------------------------------------- */
  317. void clish_command__force_viewid(clish_command_t * this, const char *viewid)
  318. {
  319. this->viewid = lub_string_dup(viewid);
  320. }
  321. /*--------------------------------------------------------- */
  322. char *clish_command__get_viewid(const clish_command_t * this, void *context)
  323. {
  324. return this->var_expand_fn(this->viewid, context);
  325. }
  326. /*--------------------------------------------------------- */
  327. const clish_param_t *clish_command__get_param(const clish_command_t * this,
  328. unsigned index)
  329. {
  330. return clish_paramv__get_param(this->paramv, index);
  331. }
  332. /*--------------------------------------------------------- */
  333. const char *clish_command__get_suffix(const clish_command_t * this)
  334. {
  335. return lub_string_suffix(this->name);
  336. }
  337. /*--------------------------------------------------------- */
  338. void clish_command__set_builtin(clish_command_t * this, const char *builtin)
  339. {
  340. assert(NULL == this->builtin);
  341. this->builtin = lub_string_dup(builtin);
  342. }
  343. /*--------------------------------------------------------- */
  344. const char *clish_command__get_builtin(const clish_command_t * this)
  345. {
  346. return this->builtin;
  347. }
  348. /*--------------------------------------------------------- */
  349. void
  350. clish_command__set_escape_chars(clish_command_t * this,
  351. const char *escape_chars)
  352. {
  353. assert(NULL == this->escape_chars);
  354. this->escape_chars = lub_string_dup(escape_chars);
  355. }
  356. /*--------------------------------------------------------- */
  357. const char *clish_command__get_escape_chars(const clish_command_t * this)
  358. {
  359. return this->escape_chars;
  360. }
  361. /*--------------------------------------------------------- */
  362. void clish_command__set_args(clish_command_t * this, clish_param_t * args)
  363. {
  364. assert(NULL == this->args);
  365. this->args = args;
  366. }
  367. /*--------------------------------------------------------- */
  368. const clish_param_t *clish_command__get_args(const clish_command_t * this)
  369. {
  370. return this->args;
  371. }
  372. /*--------------------------------------------------------- */
  373. const unsigned clish_command__get_param_count(const clish_command_t * this)
  374. {
  375. return clish_paramv__get_count(this->paramv);
  376. }
  377. /*--------------------------------------------------------- */
  378. clish_paramv_t *clish_command__get_paramv(const clish_command_t * this)
  379. {
  380. return this->paramv;
  381. }
  382. /*--------------------------------------------------------- */
  383. void clish_command__set_pview(clish_command_t * this, clish_view_t * view)
  384. {
  385. this->pview = view;
  386. }
  387. /*--------------------------------------------------------- */
  388. clish_view_t *clish_command__get_pview(const clish_command_t * this)
  389. {
  390. return this->pview;
  391. }
  392. /*--------------------------------------------------------- */
  393. unsigned clish_command__get_depth(const clish_command_t * this)
  394. {
  395. if (!this->pview)
  396. return 0;
  397. return clish_view__get_depth(this->pview);
  398. }
  399. /*--------------------------------------------------------- */
  400. void
  401. clish_command__set_cfg_op(clish_command_t * this,
  402. clish_config_operation_t operation)
  403. {
  404. this->cfg_op = operation;
  405. }
  406. /*--------------------------------------------------------- */
  407. clish_config_operation_t clish_command__get_cfg_op(const clish_command_t * this)
  408. {
  409. return this->cfg_op;
  410. }
  411. /*--------------------------------------------------------- */
  412. void clish_command__set_priority(clish_command_t * this, unsigned short priority)
  413. {
  414. this->priority = priority;
  415. }
  416. /*--------------------------------------------------------- */
  417. unsigned short clish_command__get_priority(const clish_command_t * this)
  418. {
  419. return this->priority;
  420. }
  421. /*--------------------------------------------------------- */
  422. void clish_command__set_pattern(clish_command_t * this, const char *pattern)
  423. {
  424. assert(NULL == this->pattern);
  425. this->pattern = lub_string_dup(pattern);
  426. }
  427. /*--------------------------------------------------------- */
  428. char *clish_command__get_pattern(const clish_command_t * this, void *context)
  429. {
  430. return this->var_expand_fn(this->pattern, context);
  431. }
  432. /*--------------------------------------------------------- */
  433. void clish_command__set_file(clish_command_t * this, const char *file)
  434. {
  435. assert(!this->file);
  436. this->file = lub_string_dup(file);
  437. }
  438. /*--------------------------------------------------------- */
  439. char *clish_command__get_file(const clish_command_t *this, void *context)
  440. {
  441. return this->var_expand_fn(this->file, context);
  442. }
  443. /*--------------------------------------------------------- */
  444. bool_t clish_command__get_splitter(const clish_command_t * this)
  445. {
  446. return this->splitter;
  447. }
  448. /*--------------------------------------------------------- */
  449. void clish_command__set_splitter(clish_command_t * this, bool_t splitter)
  450. {
  451. this->splitter = splitter;
  452. }
  453. /*--------------------------------------------------------- */
  454. void clish_command__set_seq(clish_command_t * this, const char * seq)
  455. {
  456. assert(!this->seq);
  457. this->seq = lub_string_dup(seq);
  458. }
  459. /*--------------------------------------------------------- */
  460. unsigned short clish_command__get_seq(const clish_command_t *this, void *context)
  461. {
  462. unsigned short num = 0;
  463. char *str;
  464. if (!this->seq)
  465. return 0;
  466. str = this->var_expand_fn(this->seq, context);
  467. if (str && (*str != '\0')) {
  468. long val = 0;
  469. char *endptr;
  470. val = strtol(str, &endptr, 0);
  471. if (endptr == str)
  472. num = 0;
  473. else if (val > 0xffff)
  474. num = 0xffff;
  475. else if (val < 0)
  476. num = 0;
  477. else
  478. num = (unsigned short)val;
  479. }
  480. lub_string_free(str);
  481. return num;
  482. }
  483. /*--------------------------------------------------------- */
  484. const char * clish_command__is_seq(const clish_command_t * this)
  485. {
  486. return this->seq;
  487. }
  488. /*--------------------------------------------------------- */
  489. clish_view_restore_t clish_command__get_restore(const clish_command_t * this)
  490. {
  491. if (!this->pview)
  492. return CLISH_RESTORE_NONE;
  493. return clish_view__get_restore(this->pview);
  494. }
  495. /*--------------------------------------------------------- */
  496. bool_t clish_command__get_unique(const clish_command_t * this)
  497. {
  498. return this->unique;
  499. }
  500. /*--------------------------------------------------------- */
  501. void clish_command__set_unique(clish_command_t * this, bool_t unique)
  502. {
  503. this->unique = unique;
  504. }
  505. /*--------------------------------------------------------- */
  506. const clish_command_t * clish_command__get_orig(const clish_command_t * this)
  507. {
  508. if (this->link)
  509. return clish_command__get_orig(this->link);
  510. return this;
  511. }
  512. /*--------------------------------------------------------- */
  513. void clish_command__set_cfg_depth(clish_command_t * this, const char * cfg_depth)
  514. {
  515. assert(!this->cfg_depth);
  516. this->cfg_depth = lub_string_dup(cfg_depth);
  517. }
  518. /*--------------------------------------------------------- */
  519. unsigned clish_command__get_cfg_depth(const clish_command_t *this, void *context)
  520. {
  521. unsigned num = 0;
  522. char *str;
  523. if (!this->cfg_depth)
  524. return clish_command__get_depth(this);
  525. str = this->var_expand_fn(this->cfg_depth, context);
  526. if (str && (*str != '\0')) {
  527. long val = 0;
  528. char *endptr;
  529. val = strtol(str, &endptr, 0);
  530. if (endptr == str)
  531. num = 0;
  532. else if (val > 0xffff)
  533. num = 0xffff;
  534. else if (val < 0)
  535. num = 0;
  536. else
  537. num = (unsigned)val;
  538. }
  539. lub_string_free(str);
  540. return num;
  541. }
  542. /*--------------------------------------------------------- */
  543. bool_t clish_command__get_lock(const clish_command_t * this)
  544. {
  545. return this->lock;
  546. }
  547. /*--------------------------------------------------------- */
  548. void clish_command__set_lock(clish_command_t * this, bool_t lock)
  549. {
  550. this->lock = lock;
  551. }
  552. /*--------------------------------------------------------- */
  553. const char * clish_command__get_shebang(const clish_command_t * this)
  554. {
  555. return this->shebang;
  556. }
  557. /*--------------------------------------------------------- */
  558. void clish_command__set_shebang(clish_command_t * this, const char * shebang)
  559. {
  560. const char *prog = shebang;
  561. const char *prefix = "#!";
  562. assert(!this->shebang);
  563. if (lub_string_nocasestr(shebang, prefix) == shebang)
  564. prog += strlen(prefix);
  565. this->shebang = lub_string_dup(prog);
  566. }
  567. /*--------------------------------------------------------- */
  568. void clish_command__set_alias(clish_command_t * this, const char * alias)
  569. {
  570. assert(!this->alias);
  571. this->alias = lub_string_dup(alias);
  572. }
  573. /*--------------------------------------------------------- */
  574. const char * clish_command__get_alias(const clish_command_t * this)
  575. {
  576. return this->alias;
  577. }
  578. /*--------------------------------------------------------- */
  579. void clish_command__set_alias_view(clish_command_t * this,
  580. clish_view_t * alias_view)
  581. {
  582. this->alias_view = alias_view;
  583. }
  584. /*--------------------------------------------------------- */
  585. clish_view_t * clish_command__get_alias_view(const clish_command_t * this)
  586. {
  587. return this->alias_view;
  588. }
  589. /*--------------------------------------------------------- */
  590. void clish_command__set_dynamic(clish_command_t * this, bool_t dynamic)
  591. {
  592. this->dynamic = dynamic;
  593. }
  594. /*--------------------------------------------------------- */
  595. bool_t clish_command__get_dynamic(const clish_command_t * this)
  596. {
  597. return this->dynamic;
  598. }
  599. /*--------------------------------------------------------- */
  600. const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
  601. {
  602. if (!this->dynamic)
  603. return this;
  604. if (this->link)
  605. return clish_command__get_cmd(this->link);
  606. return NULL;
  607. }
  608. /*--------------------------------------------------------- */
  609. bool_t clish_command__get_interrupt(const clish_command_t * this)
  610. {
  611. return this->interrupt;
  612. }
  613. /*--------------------------------------------------------- */
  614. void clish_command__set_interrupt(clish_command_t * this, bool_t interrupt)
  615. {
  616. this->interrupt = interrupt;
  617. }