command.c 18 KB

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