shell_tinyrl.c 16 KB

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