1
0

shell_tinyrl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. if(tinyrl_is_empty(this)) {
  145. /* ignore space at the begining of the line, don't display commands */
  146. return BOOL_TRUE;
  147. } else if (tinyrl_is_quoting(this)) {
  148. /* if we are in the middle of a quote then simply enter a space */
  149. result = BOOL_TRUE;
  150. } else {
  151. /* Find out if current line is legal. It can be
  152. * fully completed or partially completed.
  153. */
  154. arg_status = clish_shell_parse(shell, line, &cmd, &pargv);
  155. if (pargv)
  156. clish_pargv_delete(pargv);
  157. switch (arg_status) {
  158. case CLISH_LINE_OK:
  159. case CLISH_LINE_PARTIAL:
  160. if (' ' != line[strlen(line) - 1])
  161. result = BOOL_TRUE;
  162. break;
  163. default:
  164. break;
  165. }
  166. /* If current line is illegal try to make auto-comletion. */
  167. if (!result) {
  168. /* perform word completion */
  169. status = clish_shell_tinyrl_complete(this);
  170. switch (status) {
  171. case TINYRL_NO_MATCH:
  172. case TINYRL_AMBIGUOUS:
  173. /* ambiguous result signal an issue */
  174. break;
  175. case TINYRL_COMPLETED_AMBIGUOUS:
  176. /* perform word completion again in case we just did case
  177. modification the first time */
  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 = "The command is not completed";
  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 command
  291. clish_shell_machine_retval(shell, 2);
  292. }
  293. tinyrl_done(this);
  294. key = key; // Happy compiler
  295. return result;
  296. }
  297. /*-------------------------------------------------------- */
  298. static bool_t clish_shell_tinyrl_hotkey(tinyrl_t *this, int key)
  299. {
  300. clish_view_t *view;
  301. const char *cmd = NULL;
  302. clish_context_t *context = tinyrl__get_context(this);
  303. clish_shell_t *shell = clish_context__get_shell(context);
  304. int i;
  305. char *tmp = NULL;
  306. i = clish_shell__get_depth(shell);
  307. while (i >= 0) {
  308. view = clish_shell__get_pwd_view(shell, i);
  309. cmd = clish_view_find_hotkey(view, key);
  310. if (cmd)
  311. break;
  312. i--;
  313. }
  314. /* Check the global view */
  315. if (i < 0) {
  316. view = shell->global;
  317. cmd = clish_view_find_hotkey(view, key);
  318. }
  319. if (!cmd)
  320. return BOOL_FALSE;
  321. tmp = clish_shell_expand(cmd, SHELL_VAR_NONE, context);
  322. tinyrl_replace_line(this, tmp, 0);
  323. lub_string_free(tmp);
  324. clish_shell_tinyrl_key_enter(this, 0);
  325. return BOOL_TRUE;
  326. }
  327. /*-------------------------------------------------------- */
  328. /* This is the completion function provided for CLISH */
  329. tinyrl_completion_func_t clish_shell_tinyrl_completion;
  330. char **clish_shell_tinyrl_completion(tinyrl_t * tinyrl,
  331. const char *line, unsigned start, unsigned end)
  332. {
  333. lub_argv_t *matches;
  334. clish_context_t *context = tinyrl__get_context(tinyrl);
  335. clish_shell_t *this = clish_context__get_shell(context);
  336. clish_shell_iterator_t iter;
  337. const clish_command_t *cmd = NULL;
  338. char *text;
  339. char **result = NULL;
  340. if (tinyrl_is_quoting(tinyrl))
  341. return result;
  342. matches = lub_argv_new(NULL, 0);
  343. text = lub_string_dupn(line, end);
  344. /* Don't bother to resort to filename completion */
  345. tinyrl_completion_over(tinyrl);
  346. /* Search for COMMAND completions */
  347. clish_shell_iterator_init(&iter, CLISH_NSPACE_COMPLETION);
  348. while ((cmd = clish_shell_find_next_completion(this, text, &iter)))
  349. lub_argv_add(matches, clish_command__get_suffix(cmd));
  350. /* Try and resolve a command */
  351. cmd = clish_shell_resolve_command(this, text);
  352. /* Search for PARAM completion */
  353. if (cmd)
  354. clish_shell_param_generator(this, matches, cmd, text, start);
  355. lub_string_free(text);
  356. /* Matches were found */
  357. if (lub_argv__get_count(matches) > 0) {
  358. unsigned i;
  359. char *subst = lub_string_dup(lub_argv__get_arg(matches, 0));
  360. /* Find out substitution */
  361. for (i = 1; i < lub_argv__get_count(matches); i++) {
  362. char *p = subst;
  363. const char *match = lub_argv__get_arg(matches, i);
  364. size_t match_len = strlen(p);
  365. /* identify the common prefix */
  366. while ((tolower(*p) == tolower(*match)) && match_len--) {
  367. p++;
  368. match++;
  369. }
  370. /* Terminate the prefix string */
  371. *p = '\0';
  372. }
  373. result = lub_argv__get_argv(matches, subst);
  374. lub_string_free(subst);
  375. }
  376. lub_argv_delete(matches);
  377. return result;
  378. }
  379. /*-------------------------------------------------------- */
  380. static void clish_shell_tinyrl_init(tinyrl_t * this)
  381. {
  382. bool_t status;
  383. /* bind the '?' key to the help function */
  384. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  385. assert(status);
  386. /* bind the <RET> key to the help function */
  387. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  388. assert(status);
  389. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  390. assert(status);
  391. /* bind the <SPACE> key to auto-complete if necessary */
  392. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  393. assert(status);
  394. /* Set external hotkey callback */
  395. tinyrl__set_hotkey_fn(this, clish_shell_tinyrl_hotkey);
  396. /* Assign timeout callback */
  397. tinyrl__set_timeout_fn(this, clish_shell_timeout_fn);
  398. /* Assign keypress callback */
  399. tinyrl__set_keypress_fn(this, clish_shell_keypress_fn);
  400. }
  401. /*-------------------------------------------------------- */
  402. /*
  403. * Create an instance of the specialised class
  404. */
  405. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  406. FILE * ostream, unsigned stifle)
  407. {
  408. /* call the parent constructor */
  409. tinyrl_t *this = tinyrl_new(istream,
  410. ostream, stifle, clish_shell_tinyrl_completion);
  411. /* now call our own constructor */
  412. if (this)
  413. clish_shell_tinyrl_init(this);
  414. return this;
  415. }
  416. /*-------------------------------------------------------- */
  417. void clish_shell_tinyrl_fini(tinyrl_t * this)
  418. {
  419. /* nothing to do... yet */
  420. this = this;
  421. }
  422. /*-------------------------------------------------------- */
  423. void clish_shell_tinyrl_delete(tinyrl_t * this)
  424. {
  425. /* call our destructor */
  426. clish_shell_tinyrl_fini(this);
  427. /* and call the parent destructor */
  428. tinyrl_delete(this);
  429. }
  430. /*-------------------------------------------------------- */
  431. static int clish_shell_execline(clish_shell_t *this, const char *line, char **out)
  432. {
  433. char *str;
  434. clish_context_t context;
  435. int lerror = 0;
  436. assert(this);
  437. this->state = SHELL_STATE_OK;
  438. if (!line && !tinyrl__get_istream(this->tinyrl)) {
  439. this->state = SHELL_STATE_SYSTEM_ERROR;
  440. return -1;
  441. }
  442. /* Renew prompt */
  443. clish_shell_renew_prompt(this);
  444. /* Set up the context for tinyrl */
  445. clish_context_init(&context, this);
  446. /* Push the specified line or interactive line */
  447. if (line)
  448. str = tinyrl_forceline(this->tinyrl, &context, line);
  449. else
  450. str = tinyrl_readline(this->tinyrl, &context);
  451. lerror = errno;
  452. if (!str) {
  453. switch (lerror) {
  454. case ENOENT:
  455. this->state = SHELL_STATE_EOF;
  456. break;
  457. case ENOEXEC:
  458. this->state = SHELL_STATE_SYNTAX_ERROR;
  459. break;
  460. default:
  461. this->state = SHELL_STATE_SYSTEM_ERROR;
  462. break;
  463. };
  464. return -1;
  465. }
  466. lub_string_free(str);
  467. /* Execute the provided command */
  468. if (context.cmd && context.pargv) {
  469. int res;
  470. if ((res = clish_shell_execute(&context, out))) {
  471. this->state = SHELL_STATE_SCRIPT_ERROR;
  472. if (context.pargv)
  473. clish_pargv_delete(context.pargv);
  474. return res;
  475. }
  476. }
  477. if (context.pargv)
  478. clish_pargv_delete(context.pargv);
  479. return 0;
  480. }
  481. /*-------------------------------------------------------- */
  482. int clish_shell_forceline(clish_shell_t *this, const char *line, char **out)
  483. {
  484. return clish_shell_execline(this, line, out);
  485. }
  486. /*-------------------------------------------------------- */
  487. int clish_shell_readline(clish_shell_t *this, char **out)
  488. {
  489. return clish_shell_execline(this, NULL, out);
  490. }
  491. /*-------------------------------------------------------- */
  492. FILE * clish_shell__get_istream(const clish_shell_t * this)
  493. {
  494. return tinyrl__get_istream(this->tinyrl);
  495. }
  496. /*-------------------------------------------------------- */
  497. FILE * clish_shell__get_ostream(const clish_shell_t * this)
  498. {
  499. return tinyrl__get_ostream(this->tinyrl);
  500. }
  501. /*-------------------------------------------------------- */
  502. bool_t clish_shell__get_utf8(const clish_shell_t * this)
  503. {
  504. assert(this);
  505. return tinyrl__get_utf8(this->tinyrl);
  506. }
  507. /*-------------------------------------------------------- */
  508. void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
  509. {
  510. assert(this);
  511. tinyrl__set_utf8(this->tinyrl, utf8);
  512. }
  513. /*--------------------------------------------------------- */
  514. tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * this)
  515. {
  516. return this->tinyrl;
  517. }
  518. /*----------------------------------------------------------*/
  519. int clish_shell__save_history(const clish_shell_t *this, const char *fname)
  520. {
  521. return tinyrl__save_history(this->tinyrl, fname);
  522. }
  523. /*----------------------------------------------------------*/
  524. int clish_shell__restore_history(clish_shell_t *this, const char *fname)
  525. {
  526. return tinyrl__restore_history(this->tinyrl, fname);
  527. }
  528. /*----------------------------------------------------------*/
  529. void clish_shell__stifle_history(clish_shell_t *this, unsigned int stifle)
  530. {
  531. tinyrl__stifle_history(this->tinyrl, stifle);
  532. }
  533. CLISH_SET(shell, unsigned int, idle_timeout);
  534. CLISH_SET(shell, bool_t, interactive);
  535. CLISH_GET(shell, bool_t, interactive);