shell_tinyrl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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;
  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;
  301. char **result = NULL;
  302. if (tinyrl_is_quoting(tinyrl))
  303. return result;
  304. matches = lub_argv_new(NULL, 0);
  305. text = lub_string_dupn(line, end);
  306. /* Don't bother to resort to filename completion */
  307. tinyrl_completion_over(tinyrl);
  308. /* Search for COMMAND completions */
  309. clish_shell_iterator_init(&iter, CLISH_NSPACE_COMPLETION);
  310. while ((cmd = clish_shell_find_next_completion(this, text, &iter)))
  311. lub_argv_add(matches, clish_command__get_suffix(cmd));
  312. /* Try and resolve a command */
  313. cmd = clish_shell_resolve_command(this, text);
  314. /* Search for PARAM completion */
  315. if (cmd)
  316. clish_shell_param_generator(this, matches, cmd, text, start);
  317. lub_string_free(text);
  318. /* Matches were found */
  319. if (lub_argv__get_count(matches) > 0) {
  320. unsigned i;
  321. char *subst = lub_string_dup(lub_argv__get_arg(matches, 0));
  322. /* Find out substitution */
  323. for (i = 1; i < lub_argv__get_count(matches); i++) {
  324. char *p = subst;
  325. const char *match = lub_argv__get_arg(matches, i);
  326. size_t match_len = strlen(p);
  327. /* identify the common prefix */
  328. while ((tolower(*p) == tolower(*match)) && match_len--) {
  329. p++;
  330. match++;
  331. }
  332. /* Terminate the prefix string */
  333. *p = '\0';
  334. }
  335. result = lub_argv__get_argv(matches, subst);
  336. lub_string_free(subst);
  337. }
  338. lub_argv_delete(matches);
  339. return result;
  340. }
  341. /*-------------------------------------------------------- */
  342. static void clish_shell_tinyrl_init(tinyrl_t * this)
  343. {
  344. bool_t status;
  345. /* bind the '?' key to the help function */
  346. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  347. assert(status);
  348. /* bind the <RET> key to the help function */
  349. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  350. assert(status);
  351. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  352. assert(status);
  353. /* bind the <SPACE> key to auto-complete if necessary */
  354. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  355. assert(status);
  356. /* Assign timeout callback */
  357. tinyrl__set_timeout_fn(this, clish_shell_timeout_fn);
  358. /* Assign keypress callback */
  359. tinyrl__set_keypress_fn(this, clish_shell_keypress_fn);
  360. }
  361. /*-------------------------------------------------------- */
  362. /*
  363. * Create an instance of the specialised class
  364. */
  365. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  366. FILE * ostream, unsigned stifle)
  367. {
  368. /* call the parent constructor */
  369. tinyrl_t *this = tinyrl_new(istream,
  370. ostream, stifle, clish_shell_tinyrl_completion);
  371. /* now call our own constructor */
  372. if (this)
  373. clish_shell_tinyrl_init(this);
  374. return this;
  375. }
  376. /*-------------------------------------------------------- */
  377. void clish_shell_tinyrl_fini(tinyrl_t * this)
  378. {
  379. /* nothing to do... yet */
  380. this = this;
  381. }
  382. /*-------------------------------------------------------- */
  383. void clish_shell_tinyrl_delete(tinyrl_t * this)
  384. {
  385. /* call our destructor */
  386. clish_shell_tinyrl_fini(this);
  387. /* and call the parent destructor */
  388. tinyrl_delete(this);
  389. }
  390. /*-------------------------------------------------------- */
  391. int clish_shell_execline(clish_shell_t *this, const char *line, char **out)
  392. {
  393. char *prompt = NULL;
  394. const clish_view_t *view;
  395. char *str;
  396. clish_context_t context;
  397. tinyrl_history_t *history;
  398. int lerror = 0;
  399. assert(this);
  400. this->state = SHELL_STATE_OK;
  401. if (!line && !tinyrl__get_istream(this->tinyrl)) {
  402. this->state = SHELL_STATE_SYSTEM_ERROR;
  403. return -1;
  404. }
  405. /* Set up the context for tinyrl */
  406. context.cmd = NULL;
  407. context.pargv = NULL;
  408. context.shell = this;
  409. /* Obtain the prompt */
  410. view = clish_shell__get_view(this);
  411. assert(view);
  412. prompt = clish_shell_expand(clish_view__get_prompt(view), SHELL_VAR_ACTION, &context);
  413. assert(prompt);
  414. /* Push the specified line or interactive line */
  415. if (line)
  416. str = tinyrl_forceline(this->tinyrl, prompt, &context, line);
  417. else
  418. str = tinyrl_readline(this->tinyrl, prompt, &context);
  419. lerror = errno;
  420. lub_string_free(prompt);
  421. if (!str) {
  422. switch (lerror) {
  423. case ENOENT:
  424. this->state = SHELL_STATE_EOF;
  425. break;
  426. case EBADMSG:
  427. this->state = SHELL_STATE_SYNTAX_ERROR;
  428. break;
  429. default:
  430. this->state = SHELL_STATE_SYSTEM_ERROR;
  431. break;
  432. };
  433. return -1;
  434. }
  435. /* Deal with the history list */
  436. if (tinyrl__get_isatty(this->tinyrl)) {
  437. history = tinyrl__get_history(this->tinyrl);
  438. tinyrl_history_add(history, str);
  439. }
  440. /* Let the client know the command line has been entered */
  441. if (this->client_hooks->cmd_line_fn)
  442. this->client_hooks->cmd_line_fn(&context, str);
  443. free(str);
  444. /* Execute the provided command */
  445. if (context.cmd && context.pargv) {
  446. int res;
  447. if ((res = clish_shell_execute(&context, out))) {
  448. this->state = SHELL_STATE_SCRIPT_ERROR;
  449. if (context.pargv)
  450. clish_pargv_delete(context.pargv);
  451. return res;
  452. }
  453. }
  454. if (context.pargv)
  455. clish_pargv_delete(context.pargv);
  456. return 0;
  457. }
  458. /*-------------------------------------------------------- */
  459. int clish_shell_forceline(clish_shell_t *this, const char *line, char **out)
  460. {
  461. return clish_shell_execline(this, line, out);
  462. }
  463. /*-------------------------------------------------------- */
  464. int clish_shell_readline(clish_shell_t *this, char **out)
  465. {
  466. return clish_shell_execline(this, NULL, out);
  467. }
  468. /*-------------------------------------------------------- */
  469. FILE * clish_shell__get_istream(const clish_shell_t * this)
  470. {
  471. return tinyrl__get_istream(this->tinyrl);
  472. }
  473. /*-------------------------------------------------------- */
  474. FILE * clish_shell__get_ostream(const clish_shell_t * this)
  475. {
  476. return tinyrl__get_ostream(this->tinyrl);
  477. }
  478. /*-------------------------------------------------------- */
  479. void clish_shell__set_interactive(clish_shell_t * this, bool_t interactive)
  480. {
  481. assert(this);
  482. this->interactive = interactive;
  483. }
  484. /*-------------------------------------------------------- */
  485. bool_t clish_shell__get_interactive(const clish_shell_t * this)
  486. {
  487. assert(this);
  488. return this->interactive;
  489. }
  490. /*-------------------------------------------------------- */
  491. bool_t clish_shell__get_utf8(const clish_shell_t * this)
  492. {
  493. assert(this);
  494. return tinyrl__get_utf8(this->tinyrl);
  495. }
  496. /*-------------------------------------------------------- */
  497. void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
  498. {
  499. assert(this);
  500. tinyrl__set_utf8(this->tinyrl, utf8);
  501. }
  502. /*-------------------------------------------------------- */
  503. void clish_shell__set_timeout(clish_shell_t *this, int timeout)
  504. {
  505. assert(this);
  506. this->idle_timeout = timeout;
  507. }
  508. /*--------------------------------------------------------- */
  509. tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * this)
  510. {
  511. return this->tinyrl;
  512. }
  513. /*-------------------------------------------------------- */