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