shell_tinyrl.c 15 KB

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