shell_tinyrl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. clish_context_init(&prompt_context, this);
  26. /* Obtain the prompt */
  27. view = clish_shell__get_view(this);
  28. assert(view);
  29. lub_string_cat(&str, "${_PROMPT_PREFIX}");
  30. lub_string_cat(&str, clish_view__get_prompt(view));
  31. lub_string_cat(&str, "${_PROMPT_SUFFIX}");
  32. prompt = clish_shell_expand(str, SHELL_VAR_NONE, &prompt_context);
  33. assert(prompt);
  34. lub_string_free(str);
  35. tinyrl__set_prompt(this->tinyrl, prompt);
  36. lub_string_free(prompt);
  37. }
  38. /*-------------------------------------------------------- */
  39. static bool_t clish_shell_tinyrl_key_help(tinyrl_t *this, int key)
  40. {
  41. bool_t result = BOOL_TRUE;
  42. if (tinyrl_is_quoting(this)) {
  43. /* if we are in the middle of a quote then simply enter a space */
  44. result = tinyrl_insert_text(this, "?");
  45. } else {
  46. /* get the context */
  47. clish_context_t *context = tinyrl__get_context(this);
  48. clish_shell_t *shell = clish_context__get_shell(context);
  49. tinyrl_crlf(this);
  50. clish_shell_help(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_e clish_shell_tinyrl_expand(tinyrl_t *this)
  64. {
  65. clish_pargv_status_e status = CLISH_LINE_OK;
  66. #if 0
  67. int rtn;
  68. char *buffer;
  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. #endif
  95. this = this;
  96. return status;
  97. }
  98. /*-------------------------------------------------------- */
  99. /*
  100. * This is a CLISH specific completion function.
  101. * If the current prefix is not a recognised prefix then
  102. * an error is flagged.
  103. * If it is a recognisable prefix then possible completions are displayed
  104. * or a unique completion is inserted.
  105. */
  106. static tinyrl_match_e clish_shell_tinyrl_complete(tinyrl_t *this)
  107. {
  108. tinyrl_match_e status;
  109. /* first of all perform any history expansion */
  110. (void)clish_shell_tinyrl_expand(this);
  111. /* perform normal completion */
  112. status = tinyrl_complete(this);
  113. switch (status) {
  114. case TINYRL_NO_MATCH:
  115. if (BOOL_FALSE == tinyrl_is_completion_error_over(this)) {
  116. /* The user hasn't even entered a valid prefix! */
  117. /* tinyrl_crlf(this);
  118. clish_shell_help(context->shell,
  119. tinyrl__get_line(this));
  120. tinyrl_crlf(this);
  121. tinyrl_reset_line_state(this);
  122. */ }
  123. break;
  124. default:
  125. /* the default completion function will have prompted for completions as
  126. * necessary
  127. */
  128. break;
  129. }
  130. return status;
  131. }
  132. /*--------------------------------------------------------- */
  133. static bool_t clish_shell_tinyrl_key_space(tinyrl_t *this, int key)
  134. {
  135. bool_t result = BOOL_FALSE;
  136. tinyrl_match_e status;
  137. clish_context_t *context = tinyrl__get_context(this);
  138. clish_shell_t *shell = clish_context__get_shell(context);
  139. const char *line = tinyrl__get_line(this);
  140. clish_pargv_status_e arg_status;
  141. const clish_command_t *cmd = NULL;
  142. clish_pargv_t *pargv = NULL;
  143. if(tinyrl_is_empty(this)) {
  144. /* ignore space at the begining of the line, don't display commands */
  145. return BOOL_TRUE;
  146. } else if (tinyrl_is_quoting(this)) {
  147. /* if we are in the middle of a quote then simply enter a space */
  148. result = BOOL_TRUE;
  149. } else {
  150. /* Find out if current line is legal. It can be
  151. * fully completed or partially completed.
  152. */
  153. arg_status = clish_shell_parse(shell, line, &cmd, &pargv);
  154. if (pargv)
  155. clish_pargv_delete(pargv);
  156. switch (arg_status) {
  157. case CLISH_LINE_OK:
  158. case CLISH_LINE_PARTIAL:
  159. if (' ' != line[strlen(line) - 1])
  160. result = BOOL_TRUE;
  161. break;
  162. default:
  163. break;
  164. }
  165. /* If current line is illegal try to make auto-comletion. */
  166. if (!result) {
  167. /* perform word completion */
  168. status = clish_shell_tinyrl_complete(this);
  169. switch (status) {
  170. case TINYRL_NO_MATCH:
  171. case TINYRL_AMBIGUOUS:
  172. /* ambiguous result signal an issue */
  173. break;
  174. case TINYRL_COMPLETED_AMBIGUOUS:
  175. /* perform word completion again in case we just did case
  176. modification the first time */
  177. status = clish_shell_tinyrl_complete(this);
  178. if (status == TINYRL_MATCH_WITH_EXTENSIONS) {
  179. /* all is well with the world just enter a space */
  180. result = BOOL_TRUE;
  181. }
  182. break;
  183. case TINYRL_MATCH:
  184. case TINYRL_MATCH_WITH_EXTENSIONS:
  185. case TINYRL_COMPLETED_MATCH:
  186. /* all is well with the world just enter a space */
  187. result = BOOL_TRUE;
  188. break;
  189. }
  190. }
  191. }
  192. if (result)
  193. result = tinyrl_insert_text(this, " ");
  194. /* keep compiler happy */
  195. key = key;
  196. return result;
  197. }
  198. /*-------------------------------------------------------- */
  199. static bool_t clish_shell_tinyrl_key_enter(tinyrl_t *this, int key)
  200. {
  201. clish_context_t *context = tinyrl__get_context(this);
  202. clish_shell_t *shell = clish_context__get_shell(context);
  203. const clish_command_t *cmd = NULL;
  204. const char *line = tinyrl__get_line(this);
  205. bool_t result = BOOL_FALSE;
  206. char *errmsg = NULL;
  207. /* Inc line counter */
  208. if (shell->current_file)
  209. shell->current_file->line++;
  210. /* Renew prompt */
  211. clish_shell_renew_prompt(shell);
  212. /* nothing to pass simply move down the screen */
  213. if (!*line) {
  214. tinyrl_multi_crlf(this);
  215. tinyrl_done(this);
  216. return BOOL_TRUE;
  217. }
  218. /* try and parse the command */
  219. cmd = clish_shell_resolve_command(shell, line);
  220. if (!cmd) {
  221. tinyrl_match_e status = clish_shell_tinyrl_complete(this);
  222. switch (status) {
  223. case TINYRL_MATCH:
  224. case TINYRL_MATCH_WITH_EXTENSIONS:
  225. case TINYRL_COMPLETED_MATCH:
  226. /* re-fetch the line as it may have changed
  227. * due to auto-completion
  228. */
  229. line = tinyrl__get_line(this);
  230. /* get the command to parse? */
  231. cmd = clish_shell_resolve_command(shell, line);
  232. /*
  233. * We have had a match but it is not a command
  234. * so add a space so as not to confuse the user
  235. */
  236. if (!cmd)
  237. result = tinyrl_insert_text(this, " ");
  238. break;
  239. default:
  240. /* failed to get a unique match... */
  241. if (!tinyrl__get_isatty(this)) {
  242. /* batch mode */
  243. tinyrl_multi_crlf(this);
  244. errmsg = "Unknown command";
  245. }
  246. break;
  247. }
  248. }
  249. if (cmd) {
  250. clish_pargv_status_e arg_status;
  251. tinyrl_multi_crlf(this);
  252. /* we've got a command so check the syntax */
  253. arg_status = clish_shell_parse(shell,
  254. line, &context->cmd, &context->pargv);
  255. switch (arg_status) {
  256. case CLISH_LINE_OK:
  257. tinyrl_done(this);
  258. result = BOOL_TRUE;
  259. break;
  260. case CLISH_BAD_HISTORY:
  261. errmsg = "Bad history entry";
  262. break;
  263. case CLISH_BAD_CMD:
  264. errmsg = "Illegal command line";
  265. break;
  266. case CLISH_BAD_PARAM:
  267. errmsg = "Illegal parameter";
  268. break;
  269. case CLISH_LINE_PARTIAL:
  270. errmsg = "The command is not completed";
  271. break;
  272. default:
  273. errmsg = "Unknown problem";
  274. break;
  275. }
  276. }
  277. /* If error then print message */
  278. if (errmsg) {
  279. if (tinyrl__get_isatty(this) || !shell->current_file) {
  280. fprintf(stderr, "Syntax error: %s\n", errmsg);
  281. tinyrl_reset_line_state(this);
  282. } else {
  283. char *fname = "stdin";
  284. if (shell->current_file->fname)
  285. fname = shell->current_file->fname;
  286. fprintf(stderr, "Syntax error on line %s:%u \"%s\": "
  287. "%s\n", fname, shell->current_file->line,
  288. line, errmsg);
  289. }
  290. }
  291. /* keep the compiler happy */
  292. key = key;
  293. return result;
  294. }
  295. /*-------------------------------------------------------- */
  296. static bool_t clish_shell_tinyrl_hotkey(tinyrl_t *this, int key)
  297. {
  298. clish_view_t *view;
  299. const char *cmd = NULL;
  300. clish_context_t *context = tinyrl__get_context(this);
  301. clish_shell_t *shell = clish_context__get_shell(context);
  302. int i;
  303. char *tmp = NULL;
  304. i = clish_shell__get_depth(shell);
  305. while (i >= 0) {
  306. view = clish_shell__get_pwd_view(shell, i);
  307. cmd = clish_view_find_hotkey(view, key);
  308. if (cmd)
  309. break;
  310. i--;
  311. }
  312. /* Check the global view */
  313. if (i < 0) {
  314. view = shell->global;
  315. cmd = clish_view_find_hotkey(view, key);
  316. }
  317. if (!cmd)
  318. return BOOL_FALSE;
  319. tmp = clish_shell_expand(cmd, SHELL_VAR_NONE, context);
  320. tinyrl_replace_line(this, tmp, 0);
  321. lub_string_free(tmp);
  322. clish_shell_tinyrl_key_enter(this, 0);
  323. return BOOL_TRUE;
  324. }
  325. /*-------------------------------------------------------- */
  326. /* This is the completion function provided for CLISH */
  327. tinyrl_completion_func_t clish_shell_tinyrl_completion;
  328. char **clish_shell_tinyrl_completion(tinyrl_t * tinyrl,
  329. const char *line, unsigned start, unsigned end)
  330. {
  331. lub_argv_t *matches;
  332. clish_context_t *context = tinyrl__get_context(tinyrl);
  333. clish_shell_t *this = clish_context__get_shell(context);
  334. clish_shell_iterator_t iter;
  335. const clish_command_t *cmd = NULL;
  336. char *text;
  337. char **result = NULL;
  338. if (tinyrl_is_quoting(tinyrl))
  339. return result;
  340. matches = lub_argv_new(NULL, 0);
  341. text = lub_string_dupn(line, end);
  342. /* Don't bother to resort to filename completion */
  343. tinyrl_completion_over(tinyrl);
  344. /* Search for COMMAND completions */
  345. clish_shell_iterator_init(&iter, CLISH_NSPACE_COMPLETION);
  346. while ((cmd = clish_shell_find_next_completion(this, text, &iter)))
  347. lub_argv_add(matches, clish_command__get_suffix(cmd));
  348. /* Try and resolve a command */
  349. cmd = clish_shell_resolve_command(this, text);
  350. /* Search for PARAM completion */
  351. if (cmd)
  352. clish_shell_param_generator(this, matches, cmd, text, start);
  353. lub_string_free(text);
  354. /* Matches were found */
  355. if (lub_argv__get_count(matches) > 0) {
  356. unsigned i;
  357. char *subst = lub_string_dup(lub_argv__get_arg(matches, 0));
  358. /* Find out substitution */
  359. for (i = 1; i < lub_argv__get_count(matches); i++) {
  360. char *p = subst;
  361. const char *match = lub_argv__get_arg(matches, i);
  362. size_t match_len = strlen(p);
  363. /* identify the common prefix */
  364. while ((tolower(*p) == tolower(*match)) && match_len--) {
  365. p++;
  366. match++;
  367. }
  368. /* Terminate the prefix string */
  369. *p = '\0';
  370. }
  371. result = lub_argv__get_argv(matches, subst);
  372. lub_string_free(subst);
  373. }
  374. lub_argv_delete(matches);
  375. return result;
  376. }
  377. /*-------------------------------------------------------- */
  378. static void clish_shell_tinyrl_init(tinyrl_t * this)
  379. {
  380. bool_t status;
  381. /* bind the '?' key to the help function */
  382. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  383. assert(status);
  384. /* bind the <RET> key to the help function */
  385. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  386. assert(status);
  387. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  388. assert(status);
  389. /* bind the <SPACE> key to auto-complete if necessary */
  390. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  391. assert(status);
  392. /* Set external hotkey callback */
  393. tinyrl__set_hotkey_fn(this, clish_shell_tinyrl_hotkey);
  394. /* Assign timeout callback */
  395. tinyrl__set_timeout_fn(this, clish_shell_timeout_fn);
  396. /* Assign keypress callback */
  397. tinyrl__set_keypress_fn(this, clish_shell_keypress_fn);
  398. }
  399. /*-------------------------------------------------------- */
  400. /*
  401. * Create an instance of the specialised class
  402. */
  403. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  404. FILE * ostream, unsigned stifle)
  405. {
  406. /* call the parent constructor */
  407. tinyrl_t *this = tinyrl_new(istream,
  408. ostream, stifle, clish_shell_tinyrl_completion);
  409. /* now call our own constructor */
  410. if (this)
  411. clish_shell_tinyrl_init(this);
  412. return this;
  413. }
  414. /*-------------------------------------------------------- */
  415. void clish_shell_tinyrl_fini(tinyrl_t * this)
  416. {
  417. /* nothing to do... yet */
  418. this = this;
  419. }
  420. /*-------------------------------------------------------- */
  421. void clish_shell_tinyrl_delete(tinyrl_t * this)
  422. {
  423. /* call our destructor */
  424. clish_shell_tinyrl_fini(this);
  425. /* and call the parent destructor */
  426. tinyrl_delete(this);
  427. }
  428. /*-------------------------------------------------------- */
  429. static int clish_shell_execline(clish_shell_t *this, const char *line, char **out)
  430. {
  431. char *str;
  432. clish_context_t context;
  433. tinyrl_history_t *history;
  434. int lerror = 0;
  435. assert(this);
  436. this->state = SHELL_STATE_OK;
  437. if (!line && !tinyrl__get_istream(this->tinyrl)) {
  438. this->state = SHELL_STATE_SYSTEM_ERROR;
  439. return -1;
  440. }
  441. /* Renew prompt */
  442. clish_shell_renew_prompt(this);
  443. /* Set up the context for tinyrl */
  444. clish_context_init(&context, this);
  445. /* Push the specified line or interactive line */
  446. if (line)
  447. str = tinyrl_forceline(this->tinyrl, &context, line);
  448. else
  449. str = tinyrl_readline(this->tinyrl, &context);
  450. lerror = errno;
  451. if (!str) {
  452. switch (lerror) {
  453. case ENOENT:
  454. this->state = SHELL_STATE_EOF;
  455. break;
  456. case ENOEXEC:
  457. this->state = SHELL_STATE_SYNTAX_ERROR;
  458. break;
  459. default:
  460. this->state = SHELL_STATE_SYSTEM_ERROR;
  461. break;
  462. };
  463. return -1;
  464. }
  465. /* Deal with the history list */
  466. if (tinyrl__get_isatty(this->tinyrl)) {
  467. history = tinyrl__get_history(this->tinyrl);
  468. tinyrl_history_add(history, str);
  469. }
  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. /*-------------------------------------------------------- */