command.c 19 KB

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