shell_tinyrl.c 13 KB

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