command.c 16 KB

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