command.c 18 KB

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