shell_tinyrl.c 15 KB

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