shell_tinyrl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. if (*line) {
  208. /* try and parse the command */
  209. cmd = clish_shell_resolve_command(context->shell, line);
  210. if (NULL == cmd) {
  211. tinyrl_match_e status =
  212. clish_shell_tinyrl_complete(this);
  213. switch (status) {
  214. case TINYRL_NO_MATCH:
  215. case TINYRL_AMBIGUOUS:
  216. case TINYRL_COMPLETED_AMBIGUOUS:
  217. {
  218. /* failed to get a unique match... */
  219. break;
  220. }
  221. case TINYRL_MATCH:
  222. case TINYRL_MATCH_WITH_EXTENSIONS:
  223. case TINYRL_COMPLETED_MATCH:
  224. {
  225. /* re-fetch the line as it may have changed
  226. * due to auto-completion
  227. */
  228. line = tinyrl__get_line(this);
  229. /* get the command to parse? */
  230. cmd =
  231. clish_shell_resolve_command
  232. (context->shell, line);
  233. if (NULL == cmd) {
  234. /*
  235. * We have had a match but it is not a command
  236. * so add a space so as not to confuse the user
  237. */
  238. result =
  239. tinyrl_insert_text(this,
  240. " ");
  241. }
  242. break;
  243. }
  244. }
  245. }
  246. if (NULL != cmd) {
  247. clish_pargv_status_t arg_status;
  248. tinyrl_crlf(this);
  249. /* we've got a command so check the syntax */
  250. arg_status = clish_shell_parse(context->shell,
  251. line,
  252. &context->cmd,
  253. &context->pargv);
  254. switch (arg_status) {
  255. case CLISH_LINE_OK:
  256. tinyrl_done(this);
  257. result = BOOL_TRUE;
  258. break;
  259. case CLISH_BAD_HISTORY:
  260. fprintf(stderr, "Error: Bad history entry.\n");
  261. break;
  262. case CLISH_BAD_CMD:
  263. fprintf(stderr, "Error: Illegal command line.\n");
  264. break;
  265. case CLISH_BAD_PARAM:
  266. fprintf(stderr, "Error: Illegal parameter.\n");
  267. break;
  268. case CLISH_LINE_PARTIAL:
  269. fprintf(stderr, "Error: The command is not completed.\n");
  270. break;
  271. }
  272. if (CLISH_LINE_OK != arg_status)
  273. tinyrl_reset_line_state(this);
  274. }
  275. } else {
  276. /* nothing to pass simply move down the screen */
  277. tinyrl_crlf(this);
  278. tinyrl_reset_line_state(this);
  279. result = BOOL_TRUE;
  280. }
  281. if ((BOOL_FALSE == result) && (BOOL_FALSE == tinyrl__get_isatty(this))) {
  282. /* we've found an error in a script */
  283. context->shell->state = SHELL_STATE_SCRIPT_ERROR;
  284. }
  285. /* keep the compiler happy */
  286. key = key;
  287. return result;
  288. }
  289. /*-------------------------------------------------------- */
  290. /* This is the completion function provided for CLISH */
  291. static tinyrl_completion_func_t clish_shell_tinyrl_completion;
  292. static char **clish_shell_tinyrl_completion(tinyrl_t * this,
  293. const char *line, unsigned start, unsigned end)
  294. {
  295. char **matches;
  296. /* don't bother to resort to filename completion */
  297. tinyrl_completion_over(this);
  298. /* perform the matching */
  299. matches = tinyrl_completion(this,
  300. line, start, end, clish_shell_tinyrl_word_generator);
  301. return matches;
  302. }
  303. /*-------------------------------------------------------- */
  304. static void clish_shell_tinyrl_init(tinyrl_t * this)
  305. {
  306. bool_t status;
  307. /* bind the '?' key to the help function */
  308. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  309. assert(BOOL_TRUE == status);
  310. /* bind the <RET> key to the help function */
  311. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  312. assert(BOOL_TRUE == status);
  313. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  314. assert(BOOL_TRUE == status);
  315. /* bind the <SPACE> key to auto-complete if necessary */
  316. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  317. assert(BOOL_TRUE == status);
  318. }
  319. /*-------------------------------------------------------- */
  320. /*
  321. * Create an instance of the specialised class
  322. */
  323. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  324. FILE * ostream, unsigned stifle)
  325. {
  326. /* call the parent constructor */
  327. tinyrl_t *this = tinyrl_new(istream,
  328. ostream, stifle, clish_shell_tinyrl_completion);
  329. if (NULL != this) {
  330. /* now call our own constructor */
  331. clish_shell_tinyrl_init(this);
  332. }
  333. return this;
  334. }
  335. /*-------------------------------------------------------- */
  336. void clish_shell_tinyrl_fini(tinyrl_t * this)
  337. {
  338. /* nothing to do... yet */
  339. this = this;
  340. }
  341. /*-------------------------------------------------------- */
  342. void clish_shell_tinyrl_delete(tinyrl_t * this)
  343. {
  344. /* call our destructor */
  345. clish_shell_tinyrl_fini(this);
  346. /* and call the parent destructor */
  347. tinyrl_delete(this);
  348. }
  349. /*-------------------------------------------------------- */
  350. bool_t clish_shell_execline(clish_shell_t *this, const char *line, char ** out)
  351. {
  352. char *prompt = NULL;
  353. const clish_view_t *view;
  354. char *str;
  355. context_t con;
  356. tinyrl_history_t *history;
  357. int lerror = 0;
  358. assert(this);
  359. if (!line && !tinyrl__get_istream(this->tinyrl)) {
  360. this->state = SHELL_STATE_SYSTEM_ERROR;
  361. return BOOL_FALSE;
  362. }
  363. /* Set up the context for tinyrl */
  364. con.cmd = NULL;
  365. con.pargv = NULL;
  366. con.shell = this;
  367. /* Obtain the prompt */
  368. view = clish_shell__get_view(this);
  369. assert(view);
  370. prompt = clish_view__get_prompt(view,
  371. clish_shell__get_viewid(this));
  372. assert(prompt);
  373. /* Push the specified line or interactive line */
  374. if (line)
  375. str = tinyrl_forceline(this->tinyrl, prompt, &con, line);
  376. else
  377. str = tinyrl_readline(this->tinyrl, prompt, &con);
  378. lerror = errno;
  379. lub_string_free(prompt);
  380. if (!str) {
  381. switch (lerror) {
  382. case ENODATA:
  383. this->state = SHELL_STATE_EOF;
  384. break;
  385. case EBADMSG:
  386. this->state = SHELL_STATE_SYNTAX_ERROR;
  387. break;
  388. default:
  389. this->state = SHELL_STATE_SYSTEM_ERROR;
  390. break;
  391. };
  392. return BOOL_FALSE;
  393. }
  394. /* Deal with the history list */
  395. if (tinyrl__get_isatty(this->tinyrl)) {
  396. history = tinyrl__get_history(this->tinyrl);
  397. tinyrl_history_add(history, str);
  398. }
  399. /* Let the client know the command line has been entered */
  400. if (this->client_hooks->cmd_line_fn)
  401. this->client_hooks->cmd_line_fn(this, str);
  402. free(str);
  403. /* Execute the provided command */
  404. if (con.cmd && con.pargv) {
  405. if (!clish_shell_execute(this, con.cmd, con.pargv, out)) {
  406. this->state = SHELL_STATE_SCRIPT_ERROR;
  407. if (con.pargv)
  408. clish_pargv_delete(con.pargv);
  409. return BOOL_FALSE;
  410. }
  411. }
  412. if (con.pargv)
  413. clish_pargv_delete(con.pargv);
  414. return BOOL_TRUE;
  415. }
  416. /*-------------------------------------------------------- */
  417. bool_t clish_shell_forceline(clish_shell_t *this, const char *line, char ** out)
  418. {
  419. return clish_shell_execline(this, line, out);
  420. }
  421. /*-------------------------------------------------------- */
  422. bool_t clish_shell_readline(clish_shell_t *this, char ** out)
  423. {
  424. return clish_shell_execline(this, NULL, out);
  425. }
  426. /*-------------------------------------------------------- */
  427. FILE * clish_shell__get_istream(const clish_shell_t * this)
  428. {
  429. return tinyrl__get_istream(this->tinyrl);
  430. }
  431. /*-------------------------------------------------------- */
  432. FILE * clish_shell__get_ostream(const clish_shell_t * this)
  433. {
  434. return tinyrl__get_ostream(this->tinyrl);
  435. }
  436. /*-------------------------------------------------------- */
  437. void clish_shell__set_interactive(clish_shell_t * this, bool_t interactive)
  438. {
  439. assert(this);
  440. this->interactive = interactive;
  441. }
  442. /*-------------------------------------------------------- */
  443. bool_t clish_shell__get_interactive(const clish_shell_t * this)
  444. {
  445. assert(this);
  446. return this->interactive;
  447. }
  448. /*-------------------------------------------------------- */
  449. bool_t clish_shell__get_utf8(const clish_shell_t * this)
  450. {
  451. assert(this);
  452. return tinyrl__get_utf8(this->tinyrl);
  453. }
  454. /*-------------------------------------------------------- */
  455. void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
  456. {
  457. assert(this);
  458. tinyrl__set_utf8(this->tinyrl, utf8);
  459. }
  460. /*-------------------------------------------------------- */