shell_tinyrl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 clish_command_t *cmd = NULL;
  206. const char *line = tinyrl__get_line(this);
  207. bool_t result = BOOL_FALSE;
  208. char *errmsg = NULL;
  209. tinyrl_history_t *history;
  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. // Resolve command
  220. cmd = clish_shell_resolve_command(shell, line);
  221. // Try to complete command if it's not found
  222. if (!cmd) {
  223. tinyrl_match_e status = clish_shell_tinyrl_complete(this);
  224. switch (status) {
  225. case TINYRL_MATCH:
  226. case TINYRL_MATCH_WITH_EXTENSIONS:
  227. case TINYRL_COMPLETED_MATCH:
  228. // Re-fetch the line as it may have changed
  229. // due to auto-completion
  230. line = tinyrl__get_line(this);
  231. cmd = clish_shell_resolve_command(shell, line);
  232. // We have had a match but it is not a command
  233. // so add a space so as not to confuse the user
  234. if (!cmd)
  235. result = tinyrl_insert_text(this, " ");
  236. break;
  237. default:
  238. errmsg = "Unknown command";
  239. break;
  240. }
  241. }
  242. // Workaround on ugly history.
  243. // The line can be the pointer to history entry. Now we must fix it
  244. // and copy string to real buffer.
  245. tinyrl_changed_line(this);
  246. line = tinyrl__get_line(this);
  247. // Add any not-null line to history
  248. if (tinyrl__get_isatty(this)) {
  249. history = tinyrl__get_history(this);
  250. tinyrl_history_add(history, tinyrl__get_line(this));
  251. }
  252. tinyrl_multi_crlf(this);
  253. if (cmd) {
  254. clish_pargv_status_e arg_status;
  255. arg_status = clish_shell_parse(shell,
  256. line, &context->cmd, &context->pargv);
  257. switch (arg_status) {
  258. case CLISH_LINE_OK:
  259. result = BOOL_TRUE;
  260. break;
  261. case CLISH_BAD_HISTORY:
  262. errmsg = "Bad history entry";
  263. break;
  264. case CLISH_BAD_CMD:
  265. errmsg = "Illegal command line";
  266. break;
  267. case CLISH_BAD_PARAM:
  268. errmsg = "Illegal parameter";
  269. break;
  270. case CLISH_LINE_PARTIAL:
  271. errmsg = "Incompleted command";
  272. break;
  273. default:
  274. errmsg = "Unknown problem";
  275. break;
  276. }
  277. }
  278. // If error then print message
  279. if (errmsg) {
  280. if (tinyrl__get_isatty(this) || !shell->current_file) {
  281. fprintf(stderr, "Syntax error: %s\n", errmsg);
  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, line, errmsg);
  288. }
  289. // Wrong line must return bad retval for machine oriented proto
  290. // Let retval=2 means wrong/incompleted command.
  291. // Note there is some ugly architecture aspect - machine retval
  292. // and logging are executed within clish_shell_execute()
  293. // function also. The clish_shell_execute() is for completed
  294. // commands but this function for wrong/incompleted commands.
  295. clish_shell_machine_retval(shell, 2);
  296. /* Call logging callback */
  297. if (clish_shell__get_log(shell) &&
  298. clish_shell_check_hook(context, CLISH_SYM_TYPE_LOG)) {
  299. char *s = NULL;
  300. lub_string_cat(&s, "Syntax error: ");
  301. lub_string_cat(&s, line);
  302. clish_shell_exec_log(context, s, 2);
  303. lub_string_free(s);
  304. }
  305. }
  306. tinyrl_done(this);
  307. key = key; // Happy compiler
  308. return result;
  309. }
  310. /*-------------------------------------------------------- */
  311. static bool_t clish_shell_tinyrl_hotkey(tinyrl_t *this, int key)
  312. {
  313. clish_view_t *view;
  314. const char *cmd = NULL;
  315. clish_context_t *context = tinyrl__get_context(this);
  316. clish_shell_t *shell = clish_context__get_shell(context);
  317. int i;
  318. char *tmp = NULL;
  319. i = clish_shell__get_depth(shell);
  320. while (i >= 0) {
  321. view = clish_shell__get_pwd_view(shell, i);
  322. cmd = clish_view_find_hotkey(view, key);
  323. if (cmd)
  324. break;
  325. i--;
  326. }
  327. /* Check the global view */
  328. if (i < 0) {
  329. view = shell->global;
  330. cmd = clish_view_find_hotkey(view, key);
  331. }
  332. if (!cmd)
  333. return BOOL_FALSE;
  334. tmp = clish_shell_expand(cmd, SHELL_VAR_NONE, context);
  335. tinyrl_replace_line(this, tmp, 0);
  336. lub_string_free(tmp);
  337. clish_shell_tinyrl_key_enter(this, 0);
  338. return BOOL_TRUE;
  339. }
  340. /*-------------------------------------------------------- */
  341. /* This is the completion function provided for CLISH */
  342. tinyrl_completion_func_t clish_shell_tinyrl_completion;
  343. char **clish_shell_tinyrl_completion(tinyrl_t * tinyrl,
  344. const char *line, unsigned start, unsigned end)
  345. {
  346. lub_argv_t *matches;
  347. clish_context_t *context = tinyrl__get_context(tinyrl);
  348. clish_shell_t *this = clish_context__get_shell(context);
  349. clish_shell_iterator_t iter;
  350. const clish_command_t *cmd = NULL;
  351. char *text;
  352. char **result = NULL;
  353. if (tinyrl_is_quoting(tinyrl))
  354. return result;
  355. matches = lub_argv_new(NULL, 0);
  356. text = lub_string_dupn(line, end);
  357. /* Don't bother to resort to filename completion */
  358. tinyrl_completion_over(tinyrl);
  359. /* Search for COMMAND completions */
  360. clish_shell_iterator_init(&iter, CLISH_NSPACE_COMPLETION);
  361. while ((cmd = clish_shell_find_next_completion(this, text, &iter)))
  362. lub_argv_add(matches, clish_command__get_suffix(cmd));
  363. /* Try and resolve a command */
  364. cmd = clish_shell_resolve_command(this, text);
  365. /* Search for PARAM completion */
  366. if (cmd)
  367. clish_shell_param_generator(this, matches, cmd, text, start);
  368. lub_string_free(text);
  369. /* Matches were found */
  370. if (lub_argv__get_count(matches) > 0) {
  371. unsigned i;
  372. char *subst = lub_string_dup(lub_argv__get_arg(matches, 0));
  373. /* Find out substitution */
  374. for (i = 1; i < lub_argv__get_count(matches); i++) {
  375. char *p = subst;
  376. const char *match = lub_argv__get_arg(matches, i);
  377. size_t match_len = strlen(p);
  378. /* identify the common prefix */
  379. while ((tolower(*p) == tolower(*match)) && match_len--) {
  380. p++;
  381. match++;
  382. }
  383. /* Terminate the prefix string */
  384. *p = '\0';
  385. }
  386. result = lub_argv__get_argv(matches, subst);
  387. lub_string_free(subst);
  388. }
  389. lub_argv_delete(matches);
  390. return result;
  391. }
  392. /*-------------------------------------------------------- */
  393. static void clish_shell_tinyrl_init(tinyrl_t * this)
  394. {
  395. bool_t status;
  396. /* bind the '?' key to the help function */
  397. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  398. assert(status);
  399. /* bind the <RET> key to the help function */
  400. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  401. assert(status);
  402. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  403. assert(status);
  404. /* bind the <SPACE> key to auto-complete if necessary */
  405. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  406. assert(status);
  407. /* Set external hotkey callback */
  408. tinyrl__set_hotkey_fn(this, clish_shell_tinyrl_hotkey);
  409. /* Assign timeout callback */
  410. tinyrl__set_timeout_fn(this, clish_shell_timeout_fn);
  411. /* Assign keypress callback */
  412. tinyrl__set_keypress_fn(this, clish_shell_keypress_fn);
  413. }
  414. /*-------------------------------------------------------- */
  415. /*
  416. * Create an instance of the specialised class
  417. */
  418. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  419. FILE * ostream, unsigned stifle)
  420. {
  421. /* call the parent constructor */
  422. tinyrl_t *this = tinyrl_new(istream,
  423. ostream, stifle, clish_shell_tinyrl_completion);
  424. /* now call our own constructor */
  425. if (this)
  426. clish_shell_tinyrl_init(this);
  427. return this;
  428. }
  429. /*-------------------------------------------------------- */
  430. void clish_shell_tinyrl_fini(tinyrl_t * this)
  431. {
  432. /* nothing to do... yet */
  433. this = this;
  434. }
  435. /*-------------------------------------------------------- */
  436. void clish_shell_tinyrl_delete(tinyrl_t * this)
  437. {
  438. /* call our destructor */
  439. clish_shell_tinyrl_fini(this);
  440. /* and call the parent destructor */
  441. tinyrl_delete(this);
  442. }
  443. /*-------------------------------------------------------- */
  444. static int clish_shell_execline(clish_shell_t *this, const char *line, char **out)
  445. {
  446. char *str;
  447. clish_context_t context;
  448. int lerror = 0;
  449. assert(this);
  450. this->state = SHELL_STATE_OK;
  451. if (!line && !tinyrl__get_istream(this->tinyrl)) {
  452. this->state = SHELL_STATE_SYSTEM_ERROR;
  453. return -1;
  454. }
  455. /* Renew prompt */
  456. clish_shell_renew_prompt(this);
  457. /* Set up the context for tinyrl */
  458. clish_context_init(&context, this);
  459. /* Push the specified line or interactive line */
  460. if (line)
  461. str = tinyrl_forceline(this->tinyrl, &context, line);
  462. else
  463. str = tinyrl_readline(this->tinyrl, &context);
  464. lerror = errno;
  465. if (!str) {
  466. switch (lerror) {
  467. case ENOENT:
  468. this->state = SHELL_STATE_EOF;
  469. break;
  470. case ENOEXEC:
  471. this->state = SHELL_STATE_SYNTAX_ERROR;
  472. break;
  473. default:
  474. this->state = SHELL_STATE_SYSTEM_ERROR;
  475. break;
  476. };
  477. return -1;
  478. }
  479. lub_string_free(str);
  480. /* Execute the provided command */
  481. if (context.cmd && context.pargv) {
  482. int res;
  483. if ((res = clish_shell_execute(&context, out))) {
  484. this->state = SHELL_STATE_SCRIPT_ERROR;
  485. if (context.pargv)
  486. clish_pargv_delete(context.pargv);
  487. return res;
  488. }
  489. }
  490. if (context.pargv)
  491. clish_pargv_delete(context.pargv);
  492. return 0;
  493. }
  494. /*-------------------------------------------------------- */
  495. int clish_shell_forceline(clish_shell_t *this, const char *line, char **out)
  496. {
  497. return clish_shell_execline(this, line, out);
  498. }
  499. /*-------------------------------------------------------- */
  500. int clish_shell_readline(clish_shell_t *this, char **out)
  501. {
  502. return clish_shell_execline(this, NULL, out);
  503. }
  504. /*-------------------------------------------------------- */
  505. FILE * clish_shell__get_istream(const clish_shell_t * this)
  506. {
  507. return tinyrl__get_istream(this->tinyrl);
  508. }
  509. /*-------------------------------------------------------- */
  510. FILE * clish_shell__get_ostream(const clish_shell_t * this)
  511. {
  512. return tinyrl__get_ostream(this->tinyrl);
  513. }
  514. /*-------------------------------------------------------- */
  515. bool_t clish_shell__get_utf8(const clish_shell_t * this)
  516. {
  517. assert(this);
  518. return tinyrl__get_utf8(this->tinyrl);
  519. }
  520. /*-------------------------------------------------------- */
  521. void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
  522. {
  523. assert(this);
  524. tinyrl__set_utf8(this->tinyrl, utf8);
  525. }
  526. /*--------------------------------------------------------- */
  527. tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * this)
  528. {
  529. return this->tinyrl;
  530. }
  531. /*----------------------------------------------------------*/
  532. int clish_shell__save_history(const clish_shell_t *this, const char *fname)
  533. {
  534. return tinyrl__save_history(this->tinyrl, fname);
  535. }
  536. /*----------------------------------------------------------*/
  537. int clish_shell__restore_history(clish_shell_t *this, const char *fname)
  538. {
  539. return tinyrl__restore_history(this->tinyrl, fname);
  540. }
  541. /*----------------------------------------------------------*/
  542. void clish_shell__stifle_history(clish_shell_t *this, unsigned int stifle)
  543. {
  544. tinyrl__stifle_history(this->tinyrl, stifle);
  545. }
  546. CLISH_SET(shell, unsigned int, idle_timeout);
  547. CLISH_SET(shell, bool_t, interactive);
  548. CLISH_GET(shell, bool_t, interactive);