command.c 18 KB

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