command.c 19 KB

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