shell_tinyrl.c 15 KB

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