shell_tinyrl.c 13 KB

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