shell_tinyrl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 envrionment.
  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 "tinyrl/tinyrl.h"
  14. #include "tinyrl/history.h"
  15. #include "lub/string.h"
  16. /* This is used to hold context during tinyrl callbacks */
  17. typedef struct _context context_t;
  18. struct _context {
  19. clish_shell_t *shell;
  20. const clish_command_t *cmd;
  21. clish_pargv_t *pargv;
  22. };
  23. /*-------------------------------------------------------- */
  24. static bool_t clish_shell_tinyrl_key_help(tinyrl_t * this, int key)
  25. {
  26. bool_t result = BOOL_TRUE;
  27. if (BOOL_TRUE == tinyrl_is_quoting(this)) {
  28. /* if we are in the middle of a quote then simply enter a space */
  29. result = tinyrl_insert_text(this, "?");
  30. } else {
  31. /* get the context */
  32. context_t *context = tinyrl__get_context(this);
  33. tinyrl_crlf(this);
  34. clish_shell_help(context->shell, tinyrl__get_line(this));
  35. tinyrl_crlf(this);
  36. tinyrl_reset_line_state(this);
  37. }
  38. /* keep the compiler happy */
  39. key = key;
  40. return result;
  41. }
  42. /*-------------------------------------------------------- */
  43. /* Generator function for command completion. STATE lets us
  44. * know whether to start from scratch; without any state
  45. * (i.e. STATE == 0), then we start at the top of the list.
  46. */
  47. /*lint -e818
  48. Pointer paramter 'this' could be declared as pointing to const */
  49. static char *clish_shell_tinyrl_word_generator(tinyrl_t * this,
  50. const char *line, unsigned offset, unsigned state)
  51. {
  52. /* get the context */
  53. context_t *context = tinyrl__get_context(this);
  54. return clish_shell_word_generator(context->shell, line, offset, state);
  55. }
  56. /*lint +e818 */
  57. /*-------------------------------------------------------- */
  58. /*
  59. * Expand the current line with any history substitutions
  60. */
  61. static clish_pargv_status_t clish_shell_tinyrl_expand(tinyrl_t * this)
  62. {
  63. clish_pargv_status_t status = CLISH_LINE_OK;
  64. int rtn;
  65. char *buffer;
  66. /* first of all perform any history substitutions */
  67. rtn = tinyrl_history_expand(tinyrl__get_history(this),
  68. tinyrl__get_line(this), &buffer);
  69. switch (rtn) {
  70. case -1:
  71. /* error in expansion */
  72. status = CLISH_BAD_HISTORY;
  73. break;
  74. case 0:
  75. /*no expansion */
  76. break;
  77. case 1:
  78. /* expansion occured correctly */
  79. tinyrl_replace_line(this, buffer, 1);
  80. break;
  81. case 2:
  82. /* just display line */
  83. fprintf(tinyrl__get_ostream(this), "\n%s", buffer);
  84. free(buffer);
  85. buffer = NULL;
  86. break;
  87. default:
  88. break;
  89. }
  90. free(buffer);
  91. return status;
  92. }
  93. /*-------------------------------------------------------- */
  94. /*
  95. * This is a CLISH specific completion function.
  96. * If the current prefix is not a recognised prefix then
  97. * an error is flagged.
  98. * If it is a recognisable prefix then possible completions are displayed
  99. * or a unique completion is inserted.
  100. */
  101. static tinyrl_match_e clish_shell_tinyrl_complete(tinyrl_t * this)
  102. {
  103. // context_t *context = tinyrl__get_context(this);
  104. tinyrl_match_e status;
  105. /* first of all perform any history expansion */
  106. (void)clish_shell_tinyrl_expand(this);
  107. /* perform normal completion */
  108. status = tinyrl_complete(this);
  109. switch (status) {
  110. case TINYRL_NO_MATCH:
  111. if (BOOL_FALSE == tinyrl_is_completion_error_over(this)) {
  112. /* The user hasn't even entered a valid prefix!!! */
  113. // tinyrl_crlf(this);
  114. // clish_shell_help(context->shell,
  115. // tinyrl__get_line(this));
  116. // tinyrl_crlf(this);
  117. // tinyrl_reset_line_state(this);
  118. }
  119. break;
  120. default:
  121. /* the default completion function will have prompted for completions as
  122. * necessary
  123. */
  124. break;
  125. }
  126. return status;
  127. }
  128. /*--------------------------------------------------------- */
  129. static bool_t clish_shell_tinyrl_key_space(tinyrl_t * this, int key)
  130. {
  131. bool_t result = BOOL_FALSE;
  132. tinyrl_match_e status;
  133. context_t *context = tinyrl__get_context(this);
  134. const char *line = tinyrl__get_line(this);
  135. clish_pargv_status_t arg_status;
  136. const clish_command_t *cmd = NULL;
  137. clish_pargv_t *pargv = NULL;
  138. if (BOOL_TRUE == 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. 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. /* nothing to pass simply move down the screen */
  199. if (!*line) {
  200. tinyrl_crlf(this);
  201. tinyrl_reset_line_state(this);
  202. return BOOL_TRUE;
  203. }
  204. /* try and parse the command */
  205. cmd = clish_shell_resolve_command(context->shell, line);
  206. if (!cmd) {
  207. tinyrl_match_e status = clish_shell_tinyrl_complete(this);
  208. switch (status) {
  209. case TINYRL_MATCH:
  210. case TINYRL_MATCH_WITH_EXTENSIONS:
  211. case TINYRL_COMPLETED_MATCH:
  212. /* re-fetch the line as it may have changed
  213. * due to auto-completion
  214. */
  215. line = tinyrl__get_line(this);
  216. /* get the command to parse? */
  217. cmd = clish_shell_resolve_command(context->shell, line);
  218. /*
  219. * We have had a match but it is not a command
  220. * so add a space so as not to confuse the user
  221. */
  222. if (!cmd)
  223. result = tinyrl_insert_text(this, " ");
  224. break;
  225. default:
  226. /* failed to get a unique match... */
  227. break;
  228. }
  229. }
  230. if (cmd) {
  231. clish_pargv_status_t arg_status;
  232. tinyrl_crlf(this);
  233. /* we've got a command so check the syntax */
  234. arg_status = clish_shell_parse(context->shell,
  235. line, &context->cmd, &context->pargv);
  236. switch (arg_status) {
  237. case CLISH_LINE_OK:
  238. tinyrl_done(this);
  239. result = BOOL_TRUE;
  240. break;
  241. case CLISH_BAD_HISTORY:
  242. fprintf(stderr, "Error: Bad history entry.\n");
  243. break;
  244. case CLISH_BAD_CMD:
  245. fprintf(stderr, "Error: Illegal command line.\n");
  246. break;
  247. case CLISH_BAD_PARAM:
  248. fprintf(stderr, "Error: Illegal parameter.\n");
  249. break;
  250. case CLISH_LINE_PARTIAL:
  251. fprintf(stderr, "Error: The command is not completed.\n");
  252. break;
  253. }
  254. if (CLISH_LINE_OK != arg_status)
  255. tinyrl_reset_line_state(this);
  256. }
  257. /* keep the compiler happy */
  258. key = key;
  259. return result;
  260. }
  261. /*-------------------------------------------------------- */
  262. /* This is the completion function provided for CLISH */
  263. static tinyrl_completion_func_t clish_shell_tinyrl_completion;
  264. static char **clish_shell_tinyrl_completion(tinyrl_t * this,
  265. const char *line, unsigned start, unsigned end)
  266. {
  267. char **matches;
  268. /* don't bother to resort to filename completion */
  269. tinyrl_completion_over(this);
  270. /* perform the matching */
  271. matches = tinyrl_completion(this,
  272. line, start, end, clish_shell_tinyrl_word_generator);
  273. return matches;
  274. }
  275. /*-------------------------------------------------------- */
  276. static void clish_shell_tinyrl_init(tinyrl_t * this)
  277. {
  278. bool_t status;
  279. /* bind the '?' key to the help function */
  280. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  281. assert(status);
  282. /* bind the <RET> key to the help function */
  283. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  284. assert(status);
  285. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  286. assert(status);
  287. /* bind the <SPACE> key to auto-complete if necessary */
  288. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  289. assert(status);
  290. }
  291. /*-------------------------------------------------------- */
  292. /*
  293. * Create an instance of the specialised class
  294. */
  295. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  296. FILE * ostream, unsigned stifle)
  297. {
  298. /* call the parent constructor */
  299. tinyrl_t *this = tinyrl_new(istream,
  300. ostream, stifle, clish_shell_tinyrl_completion);
  301. /* now call our own constructor */
  302. if (this)
  303. clish_shell_tinyrl_init(this);
  304. return this;
  305. }
  306. /*-------------------------------------------------------- */
  307. void clish_shell_tinyrl_fini(tinyrl_t * this)
  308. {
  309. /* nothing to do... yet */
  310. this = this;
  311. }
  312. /*-------------------------------------------------------- */
  313. void clish_shell_tinyrl_delete(tinyrl_t * this)
  314. {
  315. /* call our destructor */
  316. clish_shell_tinyrl_fini(this);
  317. /* and call the parent destructor */
  318. tinyrl_delete(this);
  319. }
  320. /*-------------------------------------------------------- */
  321. bool_t clish_shell_execline(clish_shell_t *this, const char *line, char ** out)
  322. {
  323. char *prompt = NULL;
  324. const clish_view_t *view;
  325. char *str;
  326. context_t con;
  327. tinyrl_history_t *history;
  328. int lerror = 0;
  329. assert(this);
  330. this->state = SHELL_STATE_OK;
  331. if (!line && !tinyrl__get_istream(this->tinyrl)) {
  332. this->state = SHELL_STATE_SYSTEM_ERROR;
  333. return BOOL_FALSE;
  334. }
  335. /* Set up the context for tinyrl */
  336. con.cmd = NULL;
  337. con.pargv = NULL;
  338. con.shell = this;
  339. /* Obtain the prompt */
  340. view = clish_shell__get_view(this);
  341. assert(view);
  342. prompt = clish_view__get_prompt(view,
  343. clish_shell__get_viewid(this));
  344. assert(prompt);
  345. /* Push the specified line or interactive line */
  346. if (line)
  347. str = tinyrl_forceline(this->tinyrl, prompt, &con, line);
  348. else
  349. str = tinyrl_readline(this->tinyrl, prompt, &con);
  350. lerror = errno;
  351. lub_string_free(prompt);
  352. if (!str) {
  353. switch (lerror) {
  354. case ENODATA:
  355. this->state = SHELL_STATE_EOF;
  356. break;
  357. case EBADMSG:
  358. this->state = SHELL_STATE_SYNTAX_ERROR;
  359. break;
  360. default:
  361. this->state = SHELL_STATE_SYSTEM_ERROR;
  362. break;
  363. };
  364. return BOOL_FALSE;
  365. }
  366. /* Deal with the history list */
  367. if (tinyrl__get_isatty(this->tinyrl)) {
  368. history = tinyrl__get_history(this->tinyrl);
  369. tinyrl_history_add(history, str);
  370. }
  371. /* Let the client know the command line has been entered */
  372. if (this->client_hooks->cmd_line_fn)
  373. this->client_hooks->cmd_line_fn(this, str);
  374. free(str);
  375. /* Execute the provided command */
  376. if (con.cmd && con.pargv) {
  377. if (!clish_shell_execute(this, con.cmd, con.pargv, out)) {
  378. this->state = SHELL_STATE_SCRIPT_ERROR;
  379. if (con.pargv)
  380. clish_pargv_delete(con.pargv);
  381. return BOOL_FALSE;
  382. }
  383. }
  384. if (con.pargv)
  385. clish_pargv_delete(con.pargv);
  386. return BOOL_TRUE;
  387. }
  388. /*-------------------------------------------------------- */
  389. bool_t clish_shell_forceline(clish_shell_t *this, const char *line, char ** out)
  390. {
  391. return clish_shell_execline(this, line, out);
  392. }
  393. /*-------------------------------------------------------- */
  394. bool_t clish_shell_readline(clish_shell_t *this, char ** out)
  395. {
  396. return clish_shell_execline(this, NULL, out);
  397. }
  398. /*-------------------------------------------------------- */
  399. FILE * clish_shell__get_istream(const clish_shell_t * this)
  400. {
  401. return tinyrl__get_istream(this->tinyrl);
  402. }
  403. /*-------------------------------------------------------- */
  404. FILE * clish_shell__get_ostream(const clish_shell_t * this)
  405. {
  406. return tinyrl__get_ostream(this->tinyrl);
  407. }
  408. /*-------------------------------------------------------- */
  409. void clish_shell__set_interactive(clish_shell_t * this, bool_t interactive)
  410. {
  411. assert(this);
  412. this->interactive = interactive;
  413. }
  414. /*-------------------------------------------------------- */
  415. bool_t clish_shell__get_interactive(const clish_shell_t * this)
  416. {
  417. assert(this);
  418. return this->interactive;
  419. }
  420. /*-------------------------------------------------------- */
  421. bool_t clish_shell__get_utf8(const clish_shell_t * this)
  422. {
  423. assert(this);
  424. return tinyrl__get_utf8(this->tinyrl);
  425. }
  426. /*-------------------------------------------------------- */
  427. void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
  428. {
  429. assert(this);
  430. tinyrl__set_utf8(this->tinyrl, utf8);
  431. }
  432. /*-------------------------------------------------------- */