shell_tinyrl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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,
  51. unsigned offset, unsigned state)
  52. {
  53. /* get the context */
  54. context_t *context = tinyrl__get_context(this);
  55. return clish_shell_word_generator(context->shell, line, offset, state);
  56. }
  57. /*lint +e818 */
  58. /*-------------------------------------------------------- */
  59. /*
  60. * Expand the current line with any history substitutions
  61. */
  62. static clish_pargv_status_t clish_shell_tinyrl_expand(tinyrl_t * this)
  63. {
  64. clish_pargv_status_t status = CLISH_LINE_OK;
  65. int rtn;
  66. char *buffer;
  67. /* first of all perform any history substitutions */
  68. rtn = tinyrl_history_expand(tinyrl__get_history(this),
  69. tinyrl__get_line(this), &buffer);
  70. switch (rtn) {
  71. case -1:
  72. /* error in expansion */
  73. status = CLISH_BAD_HISTORY;
  74. break;
  75. case 0:
  76. /*no expansion */
  77. break;
  78. case 1:
  79. /* expansion occured correctly */
  80. tinyrl_replace_line(this, buffer, 1);
  81. break;
  82. case 2:
  83. /* just display line */
  84. fprintf(tinyrl__get_ostream(this), "\n%s", buffer);
  85. free(buffer);
  86. buffer = NULL;
  87. break;
  88. default:
  89. break;
  90. }
  91. free(buffer);
  92. return status;
  93. }
  94. /*-------------------------------------------------------- */
  95. /*
  96. * This is a CLISH specific completion function.
  97. * If the current prefix is not a recognised prefix then
  98. * an error is flagged.
  99. * If it is a recognisable prefix then possible completions are displayed
  100. * or a unique completion is inserted.
  101. */
  102. static tinyrl_match_e clish_shell_tinyrl_complete(tinyrl_t * this)
  103. {
  104. // context_t *context = tinyrl__get_context(this);
  105. tinyrl_match_e status;
  106. /* first of all perform any history expansion */
  107. (void)clish_shell_tinyrl_expand(this);
  108. /* perform normal completion */
  109. status = tinyrl_complete(this);
  110. switch (status) {
  111. case TINYRL_NO_MATCH:
  112. {
  113. if (BOOL_FALSE == tinyrl_is_completion_error_over(this)) {
  114. /* The user hasn't even entered a valid prefix!!! */
  115. // tinyrl_crlf(this);
  116. // clish_shell_help(context->shell,
  117. // tinyrl__get_line(this));
  118. // tinyrl_crlf(this);
  119. // tinyrl_reset_line_state(this);
  120. }
  121. break;
  122. }
  123. case TINYRL_MATCH:
  124. case TINYRL_MATCH_WITH_EXTENSIONS:
  125. case TINYRL_COMPLETED_MATCH:
  126. case TINYRL_AMBIGUOUS:
  127. case TINYRL_COMPLETED_AMBIGUOUS:
  128. {
  129. /* the default completion function will have prompted for completions as
  130. * necessary
  131. */
  132. break;
  133. }
  134. }
  135. return status;
  136. }
  137. /*--------------------------------------------------------- */
  138. static bool_t clish_shell_tinyrl_key_space(tinyrl_t * this, int key)
  139. {
  140. bool_t result = BOOL_FALSE;
  141. tinyrl_match_e status;
  142. context_t *context = tinyrl__get_context(this);
  143. const char *line = tinyrl__get_line(this);
  144. clish_pargv_status_t arg_status;
  145. const clish_command_t *cmd = NULL;
  146. clish_pargv_t *pargv = NULL;
  147. if (BOOL_TRUE == tinyrl_is_quoting(this)) {
  148. /* if we are in the middle of a quote then simply enter a space */
  149. result = BOOL_TRUE;
  150. } else {
  151. /* Find out if current line is legal. It can be
  152. * fully completed or partially completed.
  153. */
  154. arg_status = clish_shell_parse(context->shell,
  155. line, &cmd, &pargv);
  156. if (pargv)
  157. clish_pargv_delete(pargv);
  158. switch (arg_status) {
  159. case CLISH_LINE_OK:
  160. case CLISH_LINE_PARTIAL:
  161. if (' ' != line[strlen(line) - 1])
  162. result = BOOL_TRUE;
  163. break;
  164. default:
  165. break;
  166. }
  167. /* If current line is illegal try to make auto-comletion. */
  168. if (!result) {
  169. /* perform word completion */
  170. status = clish_shell_tinyrl_complete(this);
  171. switch (status) {
  172. case TINYRL_NO_MATCH:
  173. case TINYRL_AMBIGUOUS:
  174. /* ambiguous result signal an issue */
  175. break;
  176. case TINYRL_COMPLETED_AMBIGUOUS:
  177. /* perform word completion again in case we just did case
  178. modification the first time */
  179. status = clish_shell_tinyrl_complete(this);
  180. if (status == TINYRL_MATCH_WITH_EXTENSIONS) {
  181. /* all is well with the world just enter a space */
  182. result = BOOL_TRUE;
  183. }
  184. break;
  185. case TINYRL_MATCH:
  186. case TINYRL_MATCH_WITH_EXTENSIONS:
  187. case TINYRL_COMPLETED_MATCH:
  188. /* all is well with the world just enter a space */
  189. result = BOOL_TRUE;
  190. break;
  191. }
  192. }
  193. }
  194. if (result)
  195. result = tinyrl_insert_text(this, " ");
  196. /* keep compiler happy */
  197. key = key;
  198. return result;
  199. }
  200. /*-------------------------------------------------------- */
  201. static bool_t clish_shell_tinyrl_key_enter(tinyrl_t * this, int key)
  202. {
  203. context_t *context = tinyrl__get_context(this);
  204. const clish_command_t *cmd = NULL;
  205. const char *line = tinyrl__get_line(this);
  206. bool_t result = BOOL_FALSE;
  207. /* nothing to pass simply move down the screen */
  208. if (!*line) {
  209. tinyrl_crlf(this);
  210. tinyrl_reset_line_state(this);
  211. return BOOL_TRUE;
  212. }
  213. /* try and parse the command */
  214. cmd = clish_shell_resolve_command(context->shell, line);
  215. if (!cmd) {
  216. tinyrl_match_e status = clish_shell_tinyrl_complete(this);
  217. switch (status) {
  218. case TINYRL_MATCH:
  219. case TINYRL_MATCH_WITH_EXTENSIONS:
  220. case TINYRL_COMPLETED_MATCH:
  221. /* re-fetch the line as it may have changed
  222. * due to auto-completion
  223. */
  224. line = tinyrl__get_line(this);
  225. /* get the command to parse? */
  226. cmd = clish_shell_resolve_command
  227. (context->shell, line);
  228. /*
  229. * We have had a match but it is not a command
  230. * so add a space so as not to confuse the user
  231. */
  232. if (!cmd)
  233. result = tinyrl_insert_text(this, " ");
  234. break;
  235. default:
  236. /* failed to get a unique match... */
  237. break;
  238. }
  239. } else {
  240. clish_pargv_status_t arg_status;
  241. tinyrl_crlf(this);
  242. /* we've got a command so check the syntax */
  243. arg_status = clish_shell_parse(context->shell,
  244. line, &context->cmd, &context->pargv);
  245. switch (arg_status) {
  246. case CLISH_LINE_OK:
  247. tinyrl_done(this);
  248. result = BOOL_TRUE;
  249. break;
  250. case CLISH_BAD_HISTORY:
  251. fprintf(stderr, "Error: Bad history entry.\n");
  252. break;
  253. case CLISH_BAD_CMD:
  254. fprintf(stderr, "Error: Illegal command line.\n");
  255. break;
  256. case CLISH_BAD_PARAM:
  257. fprintf(stderr, "Error: Illegal parameter.\n");
  258. break;
  259. case CLISH_LINE_PARTIAL:
  260. fprintf(stderr, "Error: The command is not completed.\n");
  261. break;
  262. }
  263. if (CLISH_LINE_OK != arg_status)
  264. tinyrl_reset_line_state(this);
  265. }
  266. /* keep the compiler happy */
  267. key = key;
  268. return result;
  269. }
  270. /*-------------------------------------------------------- */
  271. /* This is the completion function provided for CLISH */
  272. static tinyrl_completion_func_t clish_shell_tinyrl_completion;
  273. static char **clish_shell_tinyrl_completion(tinyrl_t * this,
  274. const char *line, unsigned start, unsigned end)
  275. {
  276. char **matches;
  277. /* don't bother to resort to filename completion */
  278. tinyrl_completion_over(this);
  279. /* perform the matching */
  280. matches = tinyrl_completion(this,
  281. line, start, end, clish_shell_tinyrl_word_generator);
  282. return matches;
  283. }
  284. /*-------------------------------------------------------- */
  285. static void clish_shell_tinyrl_init(tinyrl_t * this)
  286. {
  287. bool_t status;
  288. /* bind the '?' key to the help function */
  289. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  290. assert(BOOL_TRUE == status);
  291. /* bind the <RET> key to the help function */
  292. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  293. assert(BOOL_TRUE == status);
  294. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  295. assert(BOOL_TRUE == status);
  296. /* bind the <SPACE> key to auto-complete if necessary */
  297. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  298. assert(BOOL_TRUE == status);
  299. }
  300. /*-------------------------------------------------------- */
  301. /*
  302. * Create an instance of the specialised class
  303. */
  304. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  305. FILE * ostream, unsigned stifle)
  306. {
  307. /* call the parent constructor */
  308. tinyrl_t *this = tinyrl_new(istream,
  309. ostream, stifle, clish_shell_tinyrl_completion);
  310. if (NULL != this) {
  311. /* now call our own constructor */
  312. clish_shell_tinyrl_init(this);
  313. }
  314. return this;
  315. }
  316. /*-------------------------------------------------------- */
  317. void clish_shell_tinyrl_fini(tinyrl_t * this)
  318. {
  319. /* nothing to do... yet */
  320. this = this;
  321. }
  322. /*-------------------------------------------------------- */
  323. void clish_shell_tinyrl_delete(tinyrl_t * this)
  324. {
  325. /* call our destructor */
  326. clish_shell_tinyrl_fini(this);
  327. /* and call the parent destructor */
  328. tinyrl_delete(this);
  329. }
  330. /*-------------------------------------------------------- */
  331. bool_t clish_shell_execline(clish_shell_t *this, const char *line, char ** out)
  332. {
  333. char *prompt = NULL;
  334. const clish_view_t *view;
  335. char *str;
  336. context_t con;
  337. tinyrl_history_t *history;
  338. int lerror = 0;
  339. assert(this);
  340. this->state = SHELL_STATE_READY;
  341. if (!line && !tinyrl__get_istream(this->tinyrl)) {
  342. this->state = SHELL_STATE_SYSTEM_ERROR;
  343. return BOOL_FALSE;
  344. }
  345. /* Set up the context for tinyrl */
  346. con.cmd = NULL;
  347. con.pargv = NULL;
  348. con.shell = this;
  349. /* Obtain the prompt */
  350. view = clish_shell__get_view(this);
  351. assert(view);
  352. prompt = clish_view__get_prompt(view,
  353. clish_shell__get_viewid(this));
  354. assert(prompt);
  355. /* Push the specified line or interactive line */
  356. if (line)
  357. str = tinyrl_forceline(this->tinyrl, prompt, &con, line);
  358. else
  359. str = tinyrl_readline(this->tinyrl, prompt, &con);
  360. lerror = errno;
  361. lub_string_free(prompt);
  362. if (!str) {
  363. switch (lerror) {
  364. case ENODATA:
  365. this->state = SHELL_STATE_EOF;
  366. break;
  367. case EBADMSG:
  368. this->state = SHELL_STATE_SYNTAX_ERROR;
  369. break;
  370. default:
  371. this->state = SHELL_STATE_SYSTEM_ERROR;
  372. break;
  373. };
  374. return BOOL_FALSE;
  375. }
  376. /* Deal with the history list */
  377. if (tinyrl__get_isatty(this->tinyrl)) {
  378. history = tinyrl__get_history(this->tinyrl);
  379. tinyrl_history_add(history, str);
  380. }
  381. /* Let the client know the command line has been entered */
  382. if (this->client_hooks->cmd_line_fn)
  383. this->client_hooks->cmd_line_fn(this, str);
  384. free(str);
  385. /* Execute the provided command */
  386. if (con.cmd && con.pargv) {
  387. if (!clish_shell_execute(this, con.cmd, con.pargv, out)) {
  388. this->state = SHELL_STATE_SCRIPT_ERROR;
  389. if (con.pargv)
  390. clish_pargv_delete(con.pargv);
  391. return BOOL_FALSE;
  392. }
  393. }
  394. if (con.pargv)
  395. clish_pargv_delete(con.pargv);
  396. return BOOL_TRUE;
  397. }
  398. /*-------------------------------------------------------- */
  399. bool_t clish_shell_forceline(clish_shell_t *this, const char *line, char ** out)
  400. {
  401. return clish_shell_execline(this, line, out);
  402. }
  403. /*-------------------------------------------------------- */
  404. bool_t clish_shell_readline(clish_shell_t *this, char ** out)
  405. {
  406. return clish_shell_execline(this, NULL, out);
  407. }
  408. /*-------------------------------------------------------- */
  409. FILE * clish_shell__get_istream(const clish_shell_t * this)
  410. {
  411. return tinyrl__get_istream(this->tinyrl);
  412. }
  413. /*-------------------------------------------------------- */
  414. FILE * clish_shell__get_ostream(const clish_shell_t * this)
  415. {
  416. return tinyrl__get_ostream(this->tinyrl);
  417. }
  418. /*-------------------------------------------------------- */
  419. void clish_shell__set_interactive(clish_shell_t * this, bool_t interactive)
  420. {
  421. assert(this);
  422. this->interactive = interactive;
  423. }
  424. /*-------------------------------------------------------- */
  425. bool_t clish_shell__get_interactive(const clish_shell_t * this)
  426. {
  427. assert(this);
  428. return this->interactive;
  429. }
  430. /*-------------------------------------------------------- */
  431. bool_t clish_shell__get_utf8(const clish_shell_t * this)
  432. {
  433. assert(this);
  434. return tinyrl__get_utf8(this->tinyrl);
  435. }
  436. /*-------------------------------------------------------- */
  437. void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
  438. {
  439. assert(this);
  440. tinyrl__set_utf8(this->tinyrl, utf8);
  441. }
  442. /*-------------------------------------------------------- */