shell_tinyrl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * shell_tinyrl.c
  3. *
  4. * This is a specialisation of the tinyrl_t class which maps the readline
  5. * functionality to the CLISH environment.
  6. */
  7. #include "private.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <assert.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <ctype.h>
  14. #include "tinyrl/tinyrl.h"
  15. #include "tinyrl/history.h"
  16. #include "lub/string.h"
  17. /*-------------------------------------------------------- */
  18. static void clish_shell_renew_prompt(clish_shell_t *this)
  19. {
  20. clish_context_t prompt_context;
  21. char *prompt = NULL;
  22. const clish_view_t *view;
  23. char *str = NULL;
  24. /* Create appropriate context */
  25. memset(&prompt_context, 0, sizeof(prompt_context));
  26. prompt_context.shell = this;
  27. /* Obtain the prompt */
  28. view = clish_shell__get_view(this);
  29. assert(view);
  30. lub_string_cat(&str, "${_PROMPT_PREFIX}");
  31. lub_string_cat(&str, clish_view__get_prompt(view));
  32. lub_string_cat(&str, "${_PROMPT_SUFFIX}");
  33. prompt = clish_shell_expand(str, SHELL_VAR_NONE, &prompt_context);
  34. assert(prompt);
  35. lub_string_free(str);
  36. tinyrl__set_prompt(this->tinyrl, prompt);
  37. lub_string_free(prompt);
  38. }
  39. /*-------------------------------------------------------- */
  40. static bool_t clish_shell_tinyrl_key_help(tinyrl_t * this, int key)
  41. {
  42. bool_t result = BOOL_TRUE;
  43. if (tinyrl_is_quoting(this)) {
  44. /* if we are in the middle of a quote then simply enter a space */
  45. result = tinyrl_insert_text(this, "?");
  46. } else {
  47. /* get the context */
  48. clish_context_t *context = tinyrl__get_context(this);
  49. tinyrl_crlf(this);
  50. clish_shell_help(context->shell, tinyrl__get_line(this));
  51. tinyrl_crlf(this);
  52. tinyrl_reset_line_state(this);
  53. }
  54. /* keep the compiler happy */
  55. key = key;
  56. return result;
  57. }
  58. /*lint +e818 */
  59. /*-------------------------------------------------------- */
  60. /*
  61. * Expand the current line with any history substitutions
  62. */
  63. static clish_pargv_status_t clish_shell_tinyrl_expand(tinyrl_t * this)
  64. {
  65. clish_pargv_status_t status = CLISH_LINE_OK;
  66. int rtn;
  67. char *buffer;
  68. return status;
  69. /* first of all perform any history substitutions */
  70. rtn = tinyrl_history_expand(tinyrl__get_history(this),
  71. tinyrl__get_line(this), &buffer);
  72. switch (rtn) {
  73. case -1:
  74. /* error in expansion */
  75. status = CLISH_BAD_HISTORY;
  76. break;
  77. case 0:
  78. /*no expansion */
  79. break;
  80. case 1:
  81. /* expansion occured correctly */
  82. tinyrl_replace_line(this, buffer, 1);
  83. break;
  84. case 2:
  85. /* just display line */
  86. tinyrl_printf(this, "\n%s", buffer);
  87. free(buffer);
  88. buffer = NULL;
  89. break;
  90. default:
  91. break;
  92. }
  93. free(buffer);
  94. return status;
  95. }
  96. /*-------------------------------------------------------- */
  97. /*
  98. * This is a CLISH specific completion function.
  99. * If the current prefix is not a recognised prefix then
  100. * an error is flagged.
  101. * If it is a recognisable prefix then possible completions are displayed
  102. * or a unique completion is inserted.
  103. */
  104. static tinyrl_match_e clish_shell_tinyrl_complete(tinyrl_t * this)
  105. {
  106. /* context_t *context = tinyrl__get_context(this); */
  107. tinyrl_match_e status;
  108. /* first of all perform any history expansion */
  109. (void)clish_shell_tinyrl_expand(this);
  110. /* perform normal completion */
  111. status = tinyrl_complete(this);
  112. switch (status) {
  113. case TINYRL_NO_MATCH:
  114. if (BOOL_FALSE == tinyrl_is_completion_error_over(this)) {
  115. /* The user hasn't even entered a valid prefix! */
  116. /* tinyrl_crlf(this);
  117. clish_shell_help(context->shell,
  118. tinyrl__get_line(this));
  119. tinyrl_crlf(this);
  120. tinyrl_reset_line_state(this);
  121. */ }
  122. break;
  123. default:
  124. /* the default completion function will have prompted for completions as
  125. * necessary
  126. */
  127. break;
  128. }
  129. return status;
  130. }
  131. /*--------------------------------------------------------- */
  132. static bool_t clish_shell_tinyrl_key_space(tinyrl_t * this, int key)
  133. {
  134. bool_t result = BOOL_FALSE;
  135. tinyrl_match_e status;
  136. clish_context_t *context = tinyrl__get_context(this);
  137. const char *line = tinyrl__get_line(this);
  138. clish_pargv_status_t arg_status;
  139. const clish_command_t *cmd = NULL;
  140. clish_pargv_t *pargv = NULL;
  141. if(tinyrl_is_empty(this)) {
  142. /* ignore space at the begining of the line, don't display commands */
  143. return BOOL_TRUE;
  144. } else if (tinyrl_is_quoting(this)) {
  145. /* if we are in the middle of a quote then simply enter a space */
  146. result = BOOL_TRUE;
  147. } else {
  148. /* Find out if current line is legal. It can be
  149. * fully completed or partially completed.
  150. */
  151. arg_status = clish_shell_parse(context->shell,
  152. line, &cmd, &pargv);
  153. if (pargv)
  154. clish_pargv_delete(pargv);
  155. switch (arg_status) {
  156. case CLISH_LINE_OK:
  157. case CLISH_LINE_PARTIAL:
  158. if (' ' != line[strlen(line) - 1])
  159. result = BOOL_TRUE;
  160. break;
  161. default:
  162. break;
  163. }
  164. /* If current line is illegal try to make auto-comletion. */
  165. if (!result) {
  166. /* perform word completion */
  167. status = clish_shell_tinyrl_complete(this);
  168. switch (status) {
  169. case TINYRL_NO_MATCH:
  170. case TINYRL_AMBIGUOUS:
  171. /* ambiguous result signal an issue */
  172. break;
  173. case TINYRL_COMPLETED_AMBIGUOUS:
  174. /* perform word completion again in case we just did case
  175. modification the first time */
  176. status = clish_shell_tinyrl_complete(this);
  177. if (status == TINYRL_MATCH_WITH_EXTENSIONS) {
  178. /* all is well with the world just enter a space */
  179. result = BOOL_TRUE;
  180. }
  181. break;
  182. case TINYRL_MATCH:
  183. case TINYRL_MATCH_WITH_EXTENSIONS:
  184. case TINYRL_COMPLETED_MATCH:
  185. /* all is well with the world just enter a space */
  186. result = BOOL_TRUE;
  187. break;
  188. }
  189. }
  190. }
  191. if (result)
  192. result = tinyrl_insert_text(this, " ");
  193. /* keep compiler happy */
  194. key = key;
  195. return result;
  196. }
  197. /*-------------------------------------------------------- */
  198. static bool_t clish_shell_tinyrl_key_enter(tinyrl_t *this, int key)
  199. {
  200. clish_context_t *context = tinyrl__get_context(this);
  201. const clish_command_t *cmd = NULL;
  202. const char *line = tinyrl__get_line(this);
  203. bool_t result = BOOL_FALSE;
  204. char *errmsg = NULL;
  205. /* Inc line counter */
  206. if (context->shell->current_file)
  207. context->shell->current_file->line++;
  208. /* Renew prompt */
  209. clish_shell_renew_prompt(context->shell);
  210. /* nothing to pass simply move down the screen */
  211. if (!*line) {
  212. tinyrl_multi_crlf(this);
  213. tinyrl_done(this);
  214. return BOOL_TRUE;
  215. }
  216. /* try and parse the command */
  217. cmd = clish_shell_resolve_command(context->shell, line);
  218. if (!cmd) {
  219. tinyrl_match_e status = clish_shell_tinyrl_complete(this);
  220. switch (status) {
  221. case TINYRL_MATCH:
  222. case TINYRL_MATCH_WITH_EXTENSIONS:
  223. case TINYRL_COMPLETED_MATCH:
  224. /* re-fetch the line as it may have changed
  225. * due to auto-completion
  226. */
  227. line = tinyrl__get_line(this);
  228. /* get the command to parse? */
  229. cmd = clish_shell_resolve_command(context->shell, line);
  230. /*
  231. * We have had a match but it is not a command
  232. * so add a space so as not to confuse the user
  233. */
  234. if (!cmd)
  235. result = tinyrl_insert_text(this, " ");
  236. break;
  237. default:
  238. /* failed to get a unique match... */
  239. if (!tinyrl__get_isatty(this)) {
  240. /* batch mode */
  241. tinyrl_multi_crlf(this);
  242. errmsg = "Unknown command";
  243. }
  244. break;
  245. }
  246. }
  247. if (cmd) {
  248. clish_pargv_status_t arg_status;
  249. tinyrl_multi_crlf(this);
  250. /* we've got a command so check the syntax */
  251. arg_status = clish_shell_parse(context->shell,
  252. line, &context->cmd, &context->pargv);
  253. switch (arg_status) {
  254. case CLISH_LINE_OK:
  255. tinyrl_done(this);
  256. result = BOOL_TRUE;
  257. break;
  258. case CLISH_BAD_HISTORY:
  259. errmsg = "Bad history entry";
  260. break;
  261. case CLISH_BAD_CMD:
  262. errmsg = "Illegal command line";
  263. break;
  264. case CLISH_BAD_PARAM:
  265. errmsg = "Illegal parameter";
  266. break;
  267. case CLISH_LINE_PARTIAL:
  268. errmsg = "The command is not completed";
  269. break;
  270. default:
  271. errmsg = "Unknown problem";
  272. break;
  273. }
  274. }
  275. /* If error then print message */
  276. if (errmsg) {
  277. if (tinyrl__get_isatty(this) ||
  278. !context->shell->current_file) {
  279. fprintf(stderr, "Syntax error: %s\n", errmsg);
  280. tinyrl_reset_line_state(this);
  281. } else {
  282. char *fname = "stdin";
  283. if (context->shell->current_file->fname)
  284. fname = context->shell->current_file->fname;
  285. fprintf(stderr, "Syntax error on line %s:%u \"%s\": "
  286. "%s\n", fname, context->shell->current_file->line,
  287. line, errmsg);
  288. }
  289. }
  290. /* keep the compiler happy */
  291. key = key;
  292. return result;
  293. }
  294. /*-------------------------------------------------------- */
  295. static bool_t clish_shell_tinyrl_hotkey(tinyrl_t *this, int key)
  296. {
  297. clish_view_t *view;
  298. const char *cmd = NULL;
  299. clish_context_t *context = tinyrl__get_context(this);
  300. clish_shell_t *shell = context->shell;
  301. int i;
  302. char *tmp = NULL;
  303. i = clish_shell__get_depth(shell);
  304. while (i >= 0) {
  305. view = clish_shell__get_pwd_view(shell, i);
  306. cmd = clish_view_find_hotkey(view, key);
  307. if (cmd)
  308. break;
  309. i--;
  310. }
  311. /* Check the global view */
  312. if (i < 0) {
  313. view = shell->global;
  314. cmd = clish_view_find_hotkey(view, key);
  315. }
  316. if (!cmd)
  317. return BOOL_FALSE;
  318. tmp = clish_shell_expand(cmd, SHELL_VAR_NONE, context);
  319. tinyrl_replace_line(this, tmp, 0);
  320. lub_string_free(tmp);
  321. clish_shell_tinyrl_key_enter(this, 0);
  322. return BOOL_TRUE;
  323. }
  324. /*-------------------------------------------------------- */
  325. /* This is the completion function provided for CLISH */
  326. tinyrl_completion_func_t clish_shell_tinyrl_completion;
  327. char **clish_shell_tinyrl_completion(tinyrl_t * tinyrl,
  328. const char *line, unsigned start, unsigned end)
  329. {
  330. lub_argv_t *matches;
  331. clish_context_t *context = tinyrl__get_context(tinyrl);
  332. clish_shell_t *this = context->shell;
  333. clish_shell_iterator_t iter;
  334. const clish_command_t *cmd = NULL;
  335. char *text;
  336. char **result = NULL;
  337. if (tinyrl_is_quoting(tinyrl))
  338. return result;
  339. matches = lub_argv_new(NULL, 0);
  340. text = lub_string_dupn(line, end);
  341. /* Don't bother to resort to filename completion */
  342. tinyrl_completion_over(tinyrl);
  343. /* Search for COMMAND completions */
  344. clish_shell_iterator_init(&iter, CLISH_NSPACE_COMPLETION);
  345. while ((cmd = clish_shell_find_next_completion(this, text, &iter)))
  346. lub_argv_add(matches, clish_command__get_suffix(cmd));
  347. /* Try and resolve a command */
  348. cmd = clish_shell_resolve_command(this, text);
  349. /* Search for PARAM completion */
  350. if (cmd)
  351. clish_shell_param_generator(this, matches, cmd, text, start);
  352. lub_string_free(text);
  353. /* Matches were found */
  354. if (lub_argv__get_count(matches) > 0) {
  355. unsigned i;
  356. char *subst = lub_string_dup(lub_argv__get_arg(matches, 0));
  357. /* Find out substitution */
  358. for (i = 1; i < lub_argv__get_count(matches); i++) {
  359. char *p = subst;
  360. const char *match = lub_argv__get_arg(matches, i);
  361. size_t match_len = strlen(p);
  362. /* identify the common prefix */
  363. while ((tolower(*p) == tolower(*match)) && match_len--) {
  364. p++;
  365. match++;
  366. }
  367. /* Terminate the prefix string */
  368. *p = '\0';
  369. }
  370. result = lub_argv__get_argv(matches, subst);
  371. lub_string_free(subst);
  372. }
  373. lub_argv_delete(matches);
  374. return result;
  375. }
  376. /*-------------------------------------------------------- */
  377. static void clish_shell_tinyrl_init(tinyrl_t * this)
  378. {
  379. bool_t status;
  380. /* bind the '?' key to the help function */
  381. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  382. assert(status);
  383. /* bind the <RET> key to the help function */
  384. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  385. assert(status);
  386. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  387. assert(status);
  388. /* bind the <SPACE> key to auto-complete if necessary */
  389. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  390. assert(status);
  391. /* Set external hotkey callback */
  392. tinyrl__set_hotkey_fn(this, clish_shell_tinyrl_hotkey);
  393. /* Assign timeout callback */
  394. tinyrl__set_timeout_fn(this, clish_shell_timeout_fn);
  395. /* Assign keypress callback */
  396. tinyrl__set_keypress_fn(this, clish_shell_keypress_fn);
  397. }
  398. /*-------------------------------------------------------- */
  399. /*
  400. * Create an instance of the specialised class
  401. */
  402. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  403. FILE * ostream, unsigned stifle)
  404. {
  405. /* call the parent constructor */
  406. tinyrl_t *this = tinyrl_new(istream,
  407. ostream, stifle, clish_shell_tinyrl_completion);
  408. /* now call our own constructor */
  409. if (this)
  410. clish_shell_tinyrl_init(this);
  411. return this;
  412. }
  413. /*-------------------------------------------------------- */
  414. void clish_shell_tinyrl_fini(tinyrl_t * this)
  415. {
  416. /* nothing to do... yet */
  417. this = this;
  418. }
  419. /*-------------------------------------------------------- */
  420. void clish_shell_tinyrl_delete(tinyrl_t * this)
  421. {
  422. /* call our destructor */
  423. clish_shell_tinyrl_fini(this);
  424. /* and call the parent destructor */
  425. tinyrl_delete(this);
  426. }
  427. /*-------------------------------------------------------- */
  428. static int clish_shell_execline(clish_shell_t *this, const char *line, char **out)
  429. {
  430. char *str;
  431. clish_context_t context;
  432. tinyrl_history_t *history;
  433. int lerror = 0;
  434. assert(this);
  435. this->state = SHELL_STATE_OK;
  436. if (!line && !tinyrl__get_istream(this->tinyrl)) {
  437. this->state = SHELL_STATE_SYSTEM_ERROR;
  438. return -1;
  439. }
  440. /* Renew prompt */
  441. clish_shell_renew_prompt(this);
  442. /* Set up the context for tinyrl */
  443. /* Prepare context */
  444. context.cmd = NULL;
  445. context.action = NULL;
  446. context.pargv = NULL;
  447. context.shell = this;
  448. /* Push the specified line or interactive line */
  449. if (line)
  450. str = tinyrl_forceline(this->tinyrl, &context, line);
  451. else
  452. str = tinyrl_readline(this->tinyrl, &context);
  453. lerror = errno;
  454. if (!str) {
  455. switch (lerror) {
  456. case ENOENT:
  457. this->state = SHELL_STATE_EOF;
  458. break;
  459. case ENOEXEC:
  460. this->state = SHELL_STATE_SYNTAX_ERROR;
  461. break;
  462. default:
  463. this->state = SHELL_STATE_SYSTEM_ERROR;
  464. break;
  465. };
  466. return -1;
  467. }
  468. /* Deal with the history list */
  469. if (tinyrl__get_isatty(this->tinyrl)) {
  470. history = tinyrl__get_history(this->tinyrl);
  471. tinyrl_history_add(history, str);
  472. }
  473. /* Let the client know the command line has been entered */
  474. if (this->hooks->cmd_line_fn)
  475. this->hooks->cmd_line_fn(&context, str);
  476. free(str);
  477. /* Execute the provided command */
  478. if (context.cmd && context.pargv) {
  479. int res;
  480. if ((res = clish_shell_execute(&context, out))) {
  481. this->state = SHELL_STATE_SCRIPT_ERROR;
  482. if (context.pargv)
  483. clish_pargv_delete(context.pargv);
  484. return res;
  485. }
  486. }
  487. if (context.pargv)
  488. clish_pargv_delete(context.pargv);
  489. return 0;
  490. }
  491. /*-------------------------------------------------------- */
  492. int clish_shell_forceline(clish_shell_t *this, const char *line, char **out)
  493. {
  494. return clish_shell_execline(this, line, out);
  495. }
  496. /*-------------------------------------------------------- */
  497. int clish_shell_readline(clish_shell_t *this, char **out)
  498. {
  499. return clish_shell_execline(this, NULL, out);
  500. }
  501. /*-------------------------------------------------------- */
  502. FILE * clish_shell__get_istream(const clish_shell_t * this)
  503. {
  504. return tinyrl__get_istream(this->tinyrl);
  505. }
  506. /*-------------------------------------------------------- */
  507. FILE * clish_shell__get_ostream(const clish_shell_t * this)
  508. {
  509. return tinyrl__get_ostream(this->tinyrl);
  510. }
  511. /*-------------------------------------------------------- */
  512. void clish_shell__set_interactive(clish_shell_t * this, bool_t interactive)
  513. {
  514. assert(this);
  515. this->interactive = interactive;
  516. }
  517. /*-------------------------------------------------------- */
  518. bool_t clish_shell__get_interactive(const clish_shell_t * this)
  519. {
  520. assert(this);
  521. return this->interactive;
  522. }
  523. /*-------------------------------------------------------- */
  524. bool_t clish_shell__get_utf8(const clish_shell_t * this)
  525. {
  526. assert(this);
  527. return tinyrl__get_utf8(this->tinyrl);
  528. }
  529. /*-------------------------------------------------------- */
  530. void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
  531. {
  532. assert(this);
  533. tinyrl__set_utf8(this->tinyrl, utf8);
  534. }
  535. /*-------------------------------------------------------- */
  536. void clish_shell__set_timeout(clish_shell_t *this, int timeout)
  537. {
  538. assert(this);
  539. this->idle_timeout = timeout;
  540. }
  541. /*--------------------------------------------------------- */
  542. tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * this)
  543. {
  544. return this->tinyrl;
  545. }
  546. /*----------------------------------------------------------*/
  547. int clish_shell__save_history(const clish_shell_t *this, const char *fname)
  548. {
  549. return tinyrl__save_history(this->tinyrl, fname);
  550. }
  551. /*----------------------------------------------------------*/
  552. int clish_shell__restore_history(clish_shell_t *this, const char *fname)
  553. {
  554. return tinyrl__restore_history(this->tinyrl, fname);
  555. }
  556. /*----------------------------------------------------------*/
  557. void clish_shell__stifle_history(clish_shell_t *this, unsigned int stifle)
  558. {
  559. tinyrl__stifle_history(this->tinyrl, stifle);
  560. }
  561. /*-------------------------------------------------------- */